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/
#!/usr/bin/env python2.4
# -*- coding:utf8 -*-
import cairo
import pygtk
pygtk.require('2.0')
import gtk
import gtk.gdk
import pango
import gobject
class PangoCairoDemo(gtk.Widget):
def do_realize(self):
self.set_flags(self.flags() | gtk.REALIZED)
self.window = gtk.gdk.Window(
self.get_parent_window(),
width=self.allocation.width,
height=self.allocation.height,
window_type=gtk.gdk.WINDOW_CHILD,
wclass=gtk.gdk.INPUT_OUTPUT,
event_mask=self.get_events() | gtk.gdk.EXPOSURE_MASK)
self.window.set_user_data(self)
self.style.attach(self.window)
self.style.set_background(self.window, gtk.STATE_NORMAL)
self.window.move_resize(*self.allocation)
def do_unrealize(self):
self.window.destroy()
def do_expose_event(self, event):
context = self.window.cairo_create()
context.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
context.clip()
pangocontext = self.get_pango_context()
self.draw(context, pangocontext)
return False
def get_layout (self, pangocontext):
font = pango.FontDescription("sans 18")
layout = pango.Layout(pangocontext)
layout.set_font_description(font)
layout.set_text("PangoCairo is so c000l... پنگوکایرو خیلی جالب است...")
return layout
def draw(self, context, pangocontext):
context.set_source_rgb (1, 1, 1)
context.paint()
context.set_source_rgb (0, 0, 0)
layout = self.get_layout (pangocontext)
width = self.allocation.width
layout.set_width(width * pango.SCALE)
context.move_to(0, 0)
context.show_layout(layout)
def run(self):
window = gtk.Window()
window.add(self)
window.connect("destroy", gtk.main_quit)
window.show_all()
gtk.main()
gobject.type_register(PangoCairoDemo)
if __name__ == "__main__":
PangoCairoDemo().run()
signature.asc
Description: This is a digitally signed message part
_______________________________________________ Sugar mailing list [email protected] http://mailman.laptop.org/mailman/listinfo/sugar
