#! /usr/bin/env python import cairo class Diagram(object): def __init__(self, filename, width, height): self.surface = cairo.SVGSurface(filename + '.svg', width, height) cr = self.cr = cairo.Context(self.surface) cr.scale(width, height) # Note: bug doesn't show up if no save/restore. cr.save() self.draw_dest(self.cr) cr.restore() cr.show_page() self.surface.finish() def draw_dest(self, cr): cr.set_source_rgb(1, 1, 1) cr.rectangle(0, 0, 1, 1) cr.fill() class BugText(Diagram): def draw_dest(self, cr): Diagram.draw_dest(self, cr) cr.set_source_rgb(0.0, 0.0, 0.0) cr.select_font_face("Arial", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD) cr.set_font_size(1.2) # Uncomment to fix bug! #self.ff = cr.get_font_face() x_bearing, y_bearing, width, height = cr.text_extents("a")[:4] cr.move_to(0.5 - width / 2 - x_bearing, 0.5 - height / 2 - y_bearing) cr.show_text("a") if __name__ == '__main__': BugText('showtext', 300, 200)