1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4from __future__ import print_function
5import sys
6from gi.repository import HarfBuzz as hb
7from gi.repository import GLib
8
9# Python 2/3 compatibility
10try:
11	unicode
12except NameError:
13	unicode = str
14
15def tounicode(s, encoding='utf-8'):
16	if not isinstance(s, unicode):
17		return s.decode(encoding)
18	else:
19		return s
20
21fontdata = open (sys.argv[1], 'rb').read ()
22text = 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
25blob = hb.glib_blob_create (GLib.Bytes.new (fontdata))
26face = hb.face_create (blob, 0)
27del blob
28font = hb.font_create (face)
29upem = hb.face_get_upem (face)
30del face
31hb.font_set_scale (font, upem, upem)
32#hb.ft_font_set_funcs (font)
33hb.ot_font_set_funcs (font)
34
35buf = hb.buffer_create ()
36class Debugger(object):
37	def message (self, buf, font, msg, data, _x_what_is_this):
38		print(msg)
39		return True
40debugger = Debugger()
41hb.buffer_set_message_func (buf, debugger.message, 1, 0)
42hb.buffer_add_utf8 (buf, text.encode('utf-8'), 0, -1)
43hb.buffer_guess_segment_properties (buf)
44
45hb.shape (font, buf, [])
46del font
47
48infos = hb.buffer_get_glyph_infos (buf)
49positions = hb.buffer_get_glyph_positions (buf)
50
51for info,pos in zip(infos, positions):
52	gid = info.codepoint
53	cluster = info.cluster
54	x_advance = pos.x_advance
55	x_offset = pos.x_offset
56	y_offset = pos.y_offset
57
58	print("gid%d=%d@%d,%d+%d" % (gid, cluster, x_advance, x_offset, y_offset))
59