sample.py revision e9f5c65be027eb7759ab819e267e24dff3b017b6
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 ()
36hb.buffer_add_utf8 (buf, text.encode('utf-8'), 0, -1)
37hb.buffer_guess_segment_properties (buf)
38
39hb.shape (font, buf, [])
40del font
41
42infos = hb.buffer_get_glyph_infos (buf)
43positions = hb.buffer_get_glyph_positions (buf)
44
45for info,pos in zip(infos, positions):
46	gid = info.codepoint
47	cluster = info.cluster
48	x_advance = pos.x_advance
49	x_offset = pos.x_offset
50	y_offset = pos.y_offset
51
52	print("gid%d=%d@%d,%d+%d" % (gid, cluster, x_advance, x_offset, y_offset))
53