From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 60FD27EEBF for ; Mon, 3 Aug 2015 20:44:27 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of ivg@ieee.org) identity=pra; client-ip=209.85.192.49; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="ivg@ieee.org"; x-sender="ivg@ieee.org"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of ivg@ieee.org designates 209.85.192.49 as permitted sender) identity=mailfrom; client-ip=209.85.192.49; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="ivg@ieee.org"; x-sender="ivg@ieee.org"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-qg0-f49.google.com) identity=helo; client-ip=209.85.192.49; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="ivg@ieee.org"; x-sender="postmaster@mail-qg0-f49.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0DzAAD7tb9VmzHAVdFSCRkBAQGDUmmpLAsGk0KBYYV5AoE0TAEBAQEBARIBAQEBAQYLCwkhLoQjAQEBAQIBEhVHCwULCxgJJQ8BBA0TAQUBIhMih3cDCggFCKZ3gS4+MZt7DYUtAQEBAQEFAQEBAR4KhhWELoECgk+BVQhcB4QsBZR5gT+DPIVmgWuBR0ZugmyCY4lPhWM1gRcXgh0cgW9TAYJLAQEB X-IPAS-Result: A0DzAAD7tb9VmzHAVdFSCRkBAQGDUmmpLAsGk0KBYYV5AoE0TAEBAQEBARIBAQEBAQYLCwkhLoQjAQEBAQIBEhVHCwULCxgJJQ8BBA0TAQUBIhMih3cDCggFCKZ3gS4+MZt7DYUtAQEBAQEFAQEBAR4KhhWELoECgk+BVQhcB4QsBZR5gT+DPIVmgWuBR0ZugmyCY4lPhWM1gRcXgh0cgW9TAYJLAQEB X-IronPort-AV: E=Sophos;i="5.15,603,1432591200"; d="scan'208";a="141932925" Received: from mail-qg0-f49.google.com ([209.85.192.49]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 03 Aug 2015 20:44:26 +0200 Received: by qged69 with SMTP id d69so95077473qge.0 for ; Mon, 03 Aug 2015 11:44:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version:content-type; bh=akYtyD/Qlk4p1QjRAhxQ+qguChQGjOOsd9SBB4Qt7jQ=; b=l0O0NdGpeDHelPhRUL3HMm+E5cyO9QiH2HxvRoEl2IYhTXeBjYbrTmxZ9JWiKAJbcU ejwSlx6xyvzhm33nInxJcpEkK7q7TeyTk9o+VD0oeqwzYxUJI0FoHOGwR2EyEwAbbXry koCiVzIJk4JTc5W2Ij2PfLr8jz8DHvOtsIeopbeJlcjryb4RpTUklGH7i/XB61kIdT74 HZtDYNFC43CmzMGEnYBPOl2cpeYbfX/aLk9QHmGIbiXDp3F3ju96RQ12e/OF38us5y7U LwHPQwUGXC44hLR1Fzz0+inc9u63evl62PbkpFqeWRFakNNUDrnt1cuvS2JZSGjA1WNq LfUg== X-Gm-Message-State: ALoCoQm7ZPomeLZ7XOWLKbSkQbFXwhWdRip2M8FDiNdhQKWUFDqobsh7ombsT1qZglbWkCRM6Bxv X-Received: by 10.140.107.101 with SMTP id g92mr26169295qgf.58.1438627464385; Mon, 03 Aug 2015 11:44:24 -0700 (PDT) Received: from zulu.ivg@ieee.org (CWILL-CYLAB.WV.CC.CMU.EDU. [128.237.199.230]) by smtp.gmail.com with ESMTPSA id 65sm7306655qks.30.2015.08.03.11.44.23 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Mon, 03 Aug 2015 11:44:23 -0700 (PDT) From: Ivan To: Hendrik Boom Cc: caml-list@inria.fr References: <20150803014654.GA30848@topoi.pooq.com> <5EF3DBC5-DA38-44C9-AFEC-F5E009D869E8@ieee.org> <87oaiottlw.fsf@ieee.org> <20150803180353.GA31467@topoi.pooq.com> Date: Mon, 03 Aug 2015 14:46:04 -0400 In-Reply-To: <20150803180353.GA31467@topoi.pooq.com> (Hendrik Boom's message of "Mon, 3 Aug 2015 14:03:53 -0400") Message-ID: <87k2tctdlf.fsf@ieee.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Caml-list] Using Pango with Cairo in Ocaml Hendrik Boom writes: > On Mon, Aug 03, 2015 at 09:00:11AM -0400, Ivan wrote: >> Ivan Gotovchits writes: >> >> > >> > >> > >> > >> >> On Aug 2, 2015, at 9:46 PM, Hendrik Boom 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 , Pierre Hauweele