Changes

Jump to navigation Jump to search
Line 26: Line 26:     
Note that the second attributo of ''draw_rectangle'' tells us about if the rectangle is filled or not. In this case is True, so we use ''cr.fill()'' in the new way but if we want just the outside lines of the rectangle we should use ''cr.stroke()''
 
Note that the second attributo of ''draw_rectangle'' tells us about if the rectangle is filled or not. In this case is True, so we use ''cr.fill()'' in the new way but if we want just the outside lines of the rectangle we should use ''cr.stroke()''
 +
 +
=== Write some text ===
 +
 +
This is the old way to do it:
 +
 +
title = _('You finished!')
 +
layout = self.area.create_pango_layout(title)
 +
layout.set_font_description(pango.FontDescription('Serif Bold 16'))   
 +
size = layout.get_size()
 +
tx = x+w/2-(size[0]/pango.SCALE)/2
 +
ty = y + 100
 +
self.area.window.draw_layout(gc, tx, ty, layout)
 +
 +
and this is the way that I used to do it with pangocairo:
 +
 +
title = _('You finished!')
 +
pango_cr = pangocairo.CairoContext(cr)
 +
pango_cr.set_source_rgb(0, 0, 0)
 +
pango_layout = cr.create_layout()
 +
pango_layout.set_font_description(pango.FontDescription('Serif Bold 16'))
 +
pango_layout.set_text(title)
 +
size = pango_layout.get_size()
 +
tx = x + (w / 2) - (size[0] / pango.SCALE) / 2
 +
ty = y + 100
 +
pango_cr.move_to(tx, ty)
 +
pango_cr.show_layout(pango_layout)
 +
pango_cr.stroke()
 +
 +
 +
=== Draw a line ===
 +
 +
This is the old way:
 +
 +
gc.foreground = self.area.get_colormap().alloc_color(0, 0, 0)
 +
self.area.window.draw_line(gc, int(b.x), int(b.y + b.size / 2),
 +
                            int(b.x), int(b.y + b.size))
 +
 +
and should be replaced by this new way using cairo
 +
 +
cr.set_source_rgb(0, 0, 0)
 +
cr.move_to(int(b.x), int(b.y + b.size / 2))
 +
cr.line_to(int(b.x), int(b.y + b.size))
 +
cr.stroke()
 +
 +
=== Draw an arc ===
 +
 +
Old way to do it (notice the second argument of ''draw_arc'': '''filled'''):
 +
 +
gc.foreground = self.area.get_colormap().alloc_color(b.color[0],b.color[1],b.color[2])
 +
self.area.window.draw_arc(gc, True, x-b.size/2, y-b.size/2, b.size, b.size, 0, 360*64)
 +
 +
new way:
 +
 +
cr.set_source_rgb(b.color[0], b.color[1], b.color[2])
 +
cr.arc(b.x, b.y, b.size / 2, 0, 2 * math.pi)
 +
cr.fill()
 +
 +
== Tips & tricks ==
 +
 +
=== Convert cairo.Surface to GdkPixbuf ===
 +
 +
import StringIO
 +
 +
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
 +
cr = cairo.Context(surface)
 +
 +
[... draw something into the surface ...]
 +
 +
pixbuf_data = StringIO.StringIO()
 +
surface.write_to_png(pixbuf_data)
 +
pxb_loader = gtk.gdk.PixbufLoader(image_type='png')
 +
pxb_loader.write(pixbuf_data.getvalue())
 +
temp_pix = pxb_loader.get_pixbuf()
 +
pxb_loader.close()
 +
 +
=== Paint a GdkPixbuf into the canvas ===
 +
 +
cr.set_source_pixbuf(self.backgroundpixbuf, 0, 0)
 +
cr.rectangle(x, 0, self.backgroundpixbuf.get_width(),
 +
              self.backgroundpixbuf.get_height())
 +
cr.paint()
    
= Code Snippets =
 
= Code Snippets =
266

edits

Navigation menu