caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ivan Gotovchits <ivg@ieee.org>
To: Hendrik Boom <hendrik@topoi.pooq.com>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] Using Pango with Cairo in Ocaml
Date: Mon, 3 Aug 2015 06:52:56 -0400	[thread overview]
Message-ID: <5EF3DBC5-DA38-44C9-AFEC-F5E009D869E8@ieee.org> (raw)
In-Reply-To: <20150803014654.GA30848@topoi.pooq.com>

 



> On Aug 2, 2015, at 9:46 PM, Hendrik Boom <hendrik@topoi.pooq.com> wrote:
> 
> I'm writing here because I seem to have exhausted the wisdom of the 
> ocam-beginners mailing list.  If there's a better place for me to have 
> sent this, and there probably is, please let me know.
> 
> 
> I'm trying to use Pango with Cairo, and to do it in OCaml.
> 
> I have an example written in C, that draws a multicoloured wheel of 
> copies of the word "text".  I got it from
> https://developer.gnome.org/pango/stable/pango-Cairo-Rendering.html
> 
> It works fine in C.
> 
> But I'm running into a snag with the OCaml conversion.
> 
> To make a layout, Pango wants to use a value of type Pango.context, and 
> all I have from Cairo is a value of type Cairo.context.
> 
> There seems to be no way to obtain a Pango context from a Cairo 
> context.
> 
> The obvious thing would seem to be to look at the C code to see what it 
> did, and look for the corresponding function in the OCaml library 
> source.
> 
> But it turns out the C code just uses the Cairo context as a Pango 
> context without any conversion at all.  Pango, apparently, will just 
> talk to a Cairo context as if it were a Pango context.  Presumably 
> that's the normal way to use Cairo in C.
> 
> Now I'm not just saying that in the working C code, the Pango context 
> and the Cairo context are the same type; that would be meaningless 
> because both aree of the general-purpose gobject type.  I'm saying they 
> are the same *value*, i.e., the same pointer, pointing to the same 
> thing.
> 
> My immediate question is thus, how to I get Pango.context from a 
> Cairo.context.  I seem to want a type change in OCaml without a change 
> in value.

There is an unsafe way to do this, that you should only do, if you understand, what you're doing. It is a typecasting in fact:

    external pango_of_cairo : Cairo.context -> Pango.context = "%identity"

This will define a function that will create (typecast) pango context from a cairo. 

If underneath they're different type, then at best you will get segfault. 


> 
> Pango.context would appear to be defined in pango.ml:28
> 
> type context = [`pangocontext] obj
> 
> 
> and for cairo.context I can merely find the line
> 
> type context
> 
> in both cairo.ml1 and cairo.ml.
> 
> (I didn't think type definitions could be this abbreviated, but there 
> it is.  Is this some way of mentioning a type definition elsewhere tht 
> I haven't found yet?)
> 
> 
> In the earlier cairo, the one before cairo2, there was a function fo 
> convert a Cairo context to a Pango context.  It is not present in 
> cairo2.

Maybe it is for good reason, I don't know. I need to view the implementation, that
is not currently under hand.
> 
> Now it is possibile to obtain a Cairo context from a gtk window, and 
> also a Pango context.  But since cairo can be used independently of gtk 
> windows (for example, to prooduce a pdf), there should be a mechanism 
> to obtain a pango context from a cairo context independent of any use 
> of gtk.
> 
> Or is there some deep secret about OCaml that I haven't divined yet (I 
> am an OCaml beginner) that there's some way of using the fact that the 
> two types are the same in C?
> 
> Or is it a bug in Cairo or Pango that this conversion is absent?
> 
> -- hendrik
> 
> 
> 
> -- 
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

  reply	other threads:[~2015-08-03 10:53 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-03  1:46 Hendrik Boom
2015-08-03 10:52 ` Ivan Gotovchits [this message]
2015-08-03 13:00   ` Ivan
2015-08-03 18:03     ` Hendrik Boom
2015-08-03 18:46       ` Ivan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5EF3DBC5-DA38-44C9-AFEC-F5E009D869E8@ieee.org \
    --to=ivg@ieee.org \
    --cc=caml-list@inria.fr \
    --cc=hendrik@topoi.pooq.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).