2 # -*- coding: utf-8 -*-
4 from __future__ import print_function
6 from gi.repository import HarfBuzz as hb
7 from gi.repository import GLib
9 # Python 2/3 compatibility
15 def tounicode(s, encoding='utf-8'):
16 if not isinstance(s, unicode):
17 return s.decode(encoding)
21 fontdata = open (sys.argv[1], 'rb').read ()
22 text = tounicode(sys.argv[2])
23 # Need to create GLib.Bytes explicitly until this bug is fixed:
24 # https://bugzilla.gnome.org/show_bug.cgi?id=729541
25 blob = hb.glib_blob_create (GLib.Bytes.new (fontdata))
26 face = hb.face_create (blob, 0)
28 font = hb.font_create (face)
29 upem = hb.face_get_upem (face)
31 hb.font_set_scale (font, upem, upem)
32 #hb.ft_font_set_funcs (font)
33 hb.ot_font_set_funcs (font)
35 buf = hb.buffer_create ()
36 hb.buffer_add_utf8 (buf, text.encode('utf-8'), 0, -1)
37 hb.buffer_guess_segment_properties (buf)
39 hb.shape (font, buf, [])
42 infos = hb.buffer_get_glyph_infos (buf)
43 positions = hb.buffer_get_glyph_positions (buf)
45 for info,pos in zip(infos, positions):
47 cluster = info.cluster
48 x_advance = pos.x_advance
49 x_offset = pos.x_offset
50 y_offset = pos.y_offset
52 print("gid%d=%d@%d,%d+%d" % (gid, cluster, x_advance, x_offset, y_offset))