tizen 2.3.1 release
[framework/uifw/harfbuzz.git] / src / sample.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 from __future__ import print_function
5 import sys
6 from gi.repository import HarfBuzz as hb
7 from gi.repository import GLib
8
9 # Python 2/3 compatibility
10 try:
11         unicode
12 except NameError:
13         unicode = str
14
15 def tounicode(s, encoding='utf-8'):
16         if not isinstance(s, unicode):
17                 return s.decode(encoding)
18         else:
19                 return s
20
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)
27 del blob
28 font = hb.font_create (face)
29 upem = hb.face_get_upem (face)
30 del face
31 hb.font_set_scale (font, upem, upem)
32 #hb.ft_font_set_funcs (font)
33 hb.ot_font_set_funcs (font)
34
35 buf = hb.buffer_create ()
36 hb.buffer_add_utf8 (buf, text.encode('utf-8'), 0, -1)
37 hb.buffer_guess_segment_properties (buf)
38
39 hb.shape (font, buf, [])
40 del font
41
42 infos = hb.buffer_get_glyph_infos (buf)
43 positions = hb.buffer_get_glyph_positions (buf)
44
45 for 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))