Thanks, that sample code is very helpful for making the transition from cairo text rendering to cairo+pango text rendering.

Are there python examples for pango font metrics?
When just using cairo's font rendering, I would make calls such as
x_bearing, y_bearing, width, height, x_advance, y_advance = ctx.text_extents( "how do i measure this in pango?" )

How do I measure these attributes in pango?

Thanks
Erik

On 11/9/06, Behdad Esfahbod <[EMAIL PROTECTED]> wrote:
On Mon, 2006-11-06 at 21:08 -0500, Erik Blankinship wrote:
> Thanks for the warning about cairo's font rendering.
>
> Could you please send a reference to how to use pangocairo with
> pycairo?  I think that would be helpful for everyone on this list who
> might use text in their application.

Attaching a complete test case.  If you can find your way in the
example, the better, otherwise some explanation follows that may be
helpful mapping the C APIs to the pygtk ones.


In the C API, there are three objects of interest:

  cairo_t
  PangoContext
  PangoLayout

You can get a cairo_t for your widget using

        gdk_cairo_create (widget->window)

You can also get a PangoContext using

        gtk_widget_get_pango_context (widget)

You can create a PangoLayout either using a PangoContext:

        pango_layout_new (pangocontext)

or get it directly from the widget:

        gtk_widget_create_pango_layout (widget)

There's also a third way, which is to create one using a cairo_t:

        pango_cairo_create_layout (cr)


Of these, you should avoid the last one if possible, because that
doesn't derive font and other properties from the widget style.


In pygtk, there is a convenience class hierarchy that doesn't really map
to the C api, so it's worth mentioning to reduce confusion:

There is the cairo.Context object which is a cairo_t.

Then there is a pangocairo.CairoContext object which is a descendant of
cairo.Context, with additional methods mapping to the functions like
pango_cairo_create_layout() and pango_cairo_show_layout()...  It
actually makes sense, since those are functions taking a cairo_t as
their first argument, so they can be thought of as extended cairo_t
methods.

Then there is gtk.gdk.CairoContext that derives from
pangocairo.CairoContext and extends that with some gdk methods, namely
set_source_color and set_source_pixbuf.  Those too map to gkd_cairo_*()
functions that take a cairo_t as their first argument.

Now the cool part is that widget.window.cairo_create () returns a
gtk.gdk.CairoContext, so you can do a show_layout() on it directly,
without having to create a pangocairo.CairoContext out of it.

The only thing to note is to not use the widget.window.cairo_create ()
context to create a PangoLayout directly (create_layout()) and use
widget.get_pango_context() instead, for reasons stated above.

behdad


> Erik
>
> On 11/6/06, Behdad Esfahbod < [EMAIL PROTECTED]> wrote:
>         On Fri, 2006-11-03 at 17:27 -0500, Erik Blankinship wrote:
>         > Hello
>         >
>         > If I want to use the system font in my cairo context, where
>         do I find
>         > the font file to load in?
>
>         NEVER EVER use cairo's text rendering capabilities to render
>         any text.
>         Not in olpc at least.  Use pangocairo ALL THE TIME.
>
>         And for families, like others suggested, use "sans-serif",
>         "serif", and
>         "monospace".
>
>
>         > Thanks,
>         > Erik
>
>         --
>         behdad
>         http://behdad.org/
>
>
>
--
behdad
http://behdad.org/



_______________________________________________
Sugar mailing list
[email protected]
http://mailman.laptop.org/mailman/listinfo/sugar

Reply via email to