Add graphical output to hbtestfont
[framework/uifw/harfbuzz.git] / contrib / python / scripts / hbtestfont
1 #!/usr/bin/python
2
3 import harfbuzz, optparse
4 try:
5     import gtk
6     import gobject
7     import cairo
8     from gtk import gdk
9 except :
10     raise SystemExit
11 import pygtk
12 if gtk.pygtk_version < (2, 8) :
13     print "PyGtk 2.8 or later required"
14     raise SystemExit
15
16 class GlyphsWindow (gtk.Widget) :
17     def __init__(self, fontname, size, glyphs) :
18         gtk.Widget.__init__(self)
19         self.fontname = fontname
20         self.size = size
21         self.glyphs = glyphs
22
23     def do_realize(self) :
24         self.set_flags(gtk.REALIZED)
25         self.window = gdk.Window(
26                 self.get_parent_window(),
27                 width = self.allocation.width,
28                 height = self.allocation.height,
29                 window_type = gdk.WINDOW_CHILD,
30                 wclass = gdk.INPUT_OUTPUT,
31                 event_mask = self.get_events() | gdk.EXPOSURE_MASK)
32         self.window.set_user_data(self)
33         self.style.attach(self.window)
34         self.style.set_background(self.window, gtk.STATE_NORMAL)
35         self.window.move_resize(*self.allocation)
36         
37     def do_unrealize(self) :
38         self.window.destroy()
39
40     def do_expose_event(self, event) :
41         cr = self.window.cairo_create()
42         cr.set_matrix(cairo.Matrix(1, 0, 0, 1, 0, 2 * self.size))
43         cr.set_font_face(cairo.ToyFontFace(self.fontname))
44         cr.set_font_size(self.size)
45         cr.show_glyphs(self.glyphs)      # [(gid, originx, originy)]
46
47 buffer = None
48
49 def tracefn(ft, aType, index) :
50     print aType + "(" + str(index) + "): " + str(buffer.get_info())
51
52 usage = '''usage: %prog [options] fontfile "codepoints"
53     Generates output of glyphs and positions. Each entry is of the form:
54         glyphid>cluster@(offsetx,offsety)+(advancex,advancey)
55
56     codepoints is a space separated list of hex values of Unicode codepoints'''
57 p = optparse.OptionParser(usage=usage)
58 p.add_option('-s', '--size', default=12, type="int", help="point size")
59 p.add_option('-l', '--lang', help="language code")
60 p.add_option('-c', '--script', help="script code")
61 p.add_option('-f', '--feature', action='append', help="define a feature key=val")
62 p.add_option('-n', '--fontname', help='Font to use to render glyphs')
63 p.add_option('-d', '--debug', action='store_true', help="Output trace info")
64 (opts, args) = p.parse_args()
65
66 ft = harfbuzz.ft(args[0], opts.size)
67 text = "".join(unichr(int(c, 16)) for c in args[1].split(" "))
68 bytes = text.encode('utf_8')
69 buffer = harfbuzz.buffer(bytes, len(text))
70 if (opts.lang or opts.script) : buffer.set_scriptlang(opts.script if opts.script else "", opts.lang if opts.lang else "")
71 features = {}
72 if opts.feature :
73     for f in opts.feature :
74         k, v = f.split("=")
75         features[k] = v
76 ft.shape(buffer, features = features)
77 res = buffer.get_info(64)       # scale for 26.6
78 print res
79 if opts.fontname :
80     glyphs = []
81     org = [0, 0]
82     for g in res :
83         glyphs.append((g.gid, org[0] + g.offset[0], org[1] + g.offset[1]))
84         org[0] += g.advance[0]
85         org[1] += g.advance[1]
86     gobject.type_register(GlyphsWindow)
87     win = gtk.Window()
88     win.resize(org[0] + 10, 3 * opts.size + 40)
89     win.connect('delete-event', gtk.main_quit)
90     frame = gtk.Frame("glyphs")
91     win.add(frame)
92     w = GlyphsWindow(opts.fontname, opts.size, glyphs)
93     frame.add(w)
94     win.show_all()
95     gtk.main()