#! /usr/bin/env python import sys import math import pango import cairo import pangocairo WIDTH = 400 HEIGHT = 300 def draw_text(cr): cr.translate(100, 100) # Create a PangoLayout, set the font and text */ layout = cr.create_layout() layout.set_font_description(pango.FontDescription("FreeSerif 100")) layout.set_text(u"\N{LEFT SQUARE BRACKET EXTENSION}".encode("utf-8")) width, height = layout.get_pixel_size() print width, height cr.rectangle(0, 0, width, height) cr.set_source_rgb(1, .5, .5) cr.set_line_width(1) cr.stroke() cr.move_to(0, 0) cr.set_source_rgb(0, 0, 0) cr.show_layout(layout) def main(argv): if len(argv) != 2: print >> sys.stderr, "Usage: cairosimple OUTPUT_BASENAME\n" return 1 filename = argv[1] surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT) cr = pangocairo.CairoContext(cairo.Context(surface)) cr.set_source_rgb(1.0, 1.0, 1.0) cr.rectangle(0, 0, WIDTH, HEIGHT) cr.fill() draw_text(cr) surface.write_to_png(filename + ".png") ## output also a PDF file surface = cairo.PDFSurface(filename + ".pdf", WIDTH, HEIGHT) cr = pangocairo.CairoContext(cairo.Context(surface)) draw_text(cr) cr.show_page() surface.finish() if __name__ == '__main__': sys.exit(main(sys.argv))