#! /usr/bin/env python import sys import cairo import math WIDTH = 400 HEIGHT = 300 def draw_text(cr): cr.translate(100, 100) cr.select_font_face ("Code 2000", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) cr.set_font_size (100) utf8 = u"\N{LEFT SQUARE BRACKET EXTENSION}".encode("utf-8") x_bearing, y_bearing, width, height, x_advance, y_advance = cr.text_extents (utf8) cr.rectangle(0, 0, width, height) cr.set_source_rgb(1, .5, .5) cr.set_line_width(1) cr.stroke() cr.move_to(-x_bearing, -y_bearing) cr.set_source_rgb(0, 0, 0) cr.show_text(utf8) ## add a dot at the end of the advance vector cr.new_path() cr.arc(x_advance, height + y_advance, 10, 0, 2*math.pi) cr.set_source_rgb(1, .5, .5) cr.fill() def main(argv): if len(argv) != 2: print >> sys.stderr, "Usage: %s OUTPUT_BASENAME\n" % sys.argv[0] return 1 filename = argv[1] surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT) cr = 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 = cairo.Context(surface) draw_text(cr) cr.show_page() surface.finish() if __name__ == '__main__': sys.exit(main(sys.argv))