* [Caml-list] Using Pango with Cairo in Ocaml @ 2015-08-03 1:46 Hendrik Boom 2015-08-03 10:52 ` Ivan Gotovchits 0 siblings, 1 reply; 5+ messages in thread From: Hendrik Boom @ 2015-08-03 1:46 UTC (permalink / raw) To: caml-list; +Cc: Hendrik Boom 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. 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. 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 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Using Pango with Cairo in Ocaml 2015-08-03 1:46 [Caml-list] Using Pango with Cairo in Ocaml Hendrik Boom @ 2015-08-03 10:52 ` Ivan Gotovchits 2015-08-03 13:00 ` Ivan 0 siblings, 1 reply; 5+ messages in thread From: Ivan Gotovchits @ 2015-08-03 10:52 UTC (permalink / raw) To: Hendrik Boom; +Cc: caml-list > 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 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Using Pango with Cairo in Ocaml 2015-08-03 10:52 ` Ivan Gotovchits @ 2015-08-03 13:00 ` Ivan 2015-08-03 18:03 ` Hendrik Boom 0 siblings, 1 reply; 5+ messages in thread From: Ivan @ 2015-08-03 13:00 UTC (permalink / raw) To: Hendrik Boom; +Cc: caml-list Ivan Gotovchits <ivg@ieee.org> writes: > > > > >> 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. I checked the source code, and it looks to me, that the types are different, so it wouldn't be safe to use this conversion. Looks like, that currently cairo library doesn't support this functionality, i.e., there is no way to get pango context from cairo. You can fix this, by stubbing `pango_cairo_create_context` function directly or using OCaml cstubs library. Or you can create an issue on a cairo2 issue tracker. ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Using Pango with Cairo in Ocaml 2015-08-03 13:00 ` Ivan @ 2015-08-03 18:03 ` Hendrik Boom 2015-08-03 18:46 ` Ivan 0 siblings, 1 reply; 5+ messages in thread From: Hendrik Boom @ 2015-08-03 18:03 UTC (permalink / raw) To: caml-list On Mon, Aug 03, 2015 at 09:00:11AM -0400, Ivan wrote: > Ivan Gotovchits <ivg@ieee.org> writes: > > > > > > > > > > >> 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. > > I checked the source code, and it looks to me, that the types are > different, so it wouldn't be safe to use this conversion. So even though the same pointer works in C, the OCaml binding makes a distinction, presumably in the direction of sanity, stability, and readability of code. I've heard C programmers curse GTK's statically typeless object system as worse thann even C's pretence of static typing. Maybe that's relevant. > Looks like, that currently cairo library doesn't support this > functionality, i.e., there is no way to get pango context from cairo. > > You can fix this, by stubbing `pango_cairo_create_context` function > directly or using OCaml cstubs library. I don't yet understand enough about OCaml, Pango, and Cairo to do this myself. More study definitely needed. For example, I still don't undersand the declaration type context I found on line 100 of cairo2.0.4.6/src/cairo.ml. It seems to me that a type declaration should, for exapmle, say what the type is. Such a thing might make sense in an interface definition where you want to hide the details from the programmer.... Evidently I still have much to learn. First, though, I'll restrict myself to gtk and make both a Cairo context and a Pango context from a gtk window, and hope they will work together. That's a workaround that leaves out the possibility of making pdf's. But I'll have *something* working. I've noticed a numer of functions that ask if the Cairo context has changed that belongs to a Pango context, which suggests that they do need to be connected. Maybe there's a way to get a Cairo context from a Pango context. That's a workaround. Might not work if I ever need multipls Pango contexts to work with one Cairo same context, which I believe *is* part of the intended usage of Pango. > > Or you can create an issue on a cairo2 issue tracker. Where do I find the issue tracker? -- hendrik ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Using Pango with Cairo in Ocaml 2015-08-03 18:03 ` Hendrik Boom @ 2015-08-03 18:46 ` Ivan 0 siblings, 0 replies; 5+ messages in thread From: Ivan @ 2015-08-03 18:46 UTC (permalink / raw) To: Hendrik Boom; +Cc: caml-list Hendrik Boom <hendrik@topoi.pooq.com> writes: > On Mon, Aug 03, 2015 at 09:00:11AM -0400, Ivan wrote: >> Ivan Gotovchits <ivg@ieee.org> writes: >> >> > >> > >> > >> > >> >> 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. >> >> I checked the source code, and it looks to me, that the types are >> different, so it wouldn't be safe to use this conversion. > > So even though the same pointer works in C, the OCaml binding makes a > distinction, presumably in the direction of sanity, stability, and > readability of code. I've heard C programmers curse GTK's statically > typeless object system as worse thann even C's pretence of static > typing. Maybe that's relevant. Yep, underneath the hood, this two libraries just store different data structures. >> Looks like, that currently cairo library doesn't support this >> functionality, i.e., there is no way to get pango context from cairo. >> >> You can fix this, by stubbing `pango_cairo_create_context` function >> directly or using OCaml cstubs library. > > I don't yet understand enough about OCaml, Pango, and Cairo to do this > myself. More study definitely needed. For example, I still don't > undersand the declaration > > type context > > I found on line 100 of cairo2.0.4.6/src/cairo.ml. > It seems to me that a type declaration should, for exapmle, say what > the type is. Such a thing might make sense in an interface definition > where you want to hide the details from the programmer.... > Evidently I still have much to learn. > That's part is easy. type void declares an uninhabited type. That means, that it is impossible to create a regular ocaml value of that type (if we forget about bottom type of expressions that don't terminate normally, e.g., exceptions). It is a pretty normal OCaml declaration, and is useful, for example, to declare phantom types. But it is also used when one binds foreign code (written not in OCaml). When you introduce an external function to OCaml, it has no other choices but to believe, that the function has the type, that you've specified. For example, type context external create_context : unit -> context = "my_c_stub" declares, that my_c_stub is a C-stub function, that accepts an OCaml value of type unit, and return an OCaml value of type context. Even, since it declared as uninhabited, it is indeed inhabited with some C data type, like `GObject *`. >> >> Or you can create an issue on a cairo2 issue tracker. > > Where do I find the issue tracker? I suspect this is a good place to start: http://cairo.forge.ocamlcore.org/ Not sure whether it is alive. You can also try to contact library authors: Christophe Troestler <Christophe.Troestler@umons.ac.be>, Pierre Hauweele <antegallya@gmail.com> ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2015-08-03 18:44 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2015-08-03 1:46 [Caml-list] Using Pango with Cairo in Ocaml Hendrik Boom 2015-08-03 10:52 ` Ivan Gotovchits 2015-08-03 13:00 ` Ivan 2015-08-03 18:03 ` Hendrik Boom 2015-08-03 18:46 ` Ivan
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox