harfbuzz/src/sample.py

53 lines
1.3 KiB
Python
Raw Normal View History

2013-09-13 02:53:07 +02:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
2013-09-13 02:53:07 +02:00
from __future__ import print_function
2013-09-13 02:53:07 +02:00
import sys
from gi.repository import HarfBuzz as hb
from gi.repository import GLib
2013-09-13 02:53:07 +02:00
# Python 2/3 compatibility
try:
unicode
except NameError:
unicode = str
def tounicode(s, encoding='utf-8'):
if not isinstance(s, unicode):
return s.decode(encoding)
else:
return s
fontdata = open (sys.argv[1], 'rb').read ()
2015-01-07 19:51:44 +01:00
text = tounicode(sys.argv[2])
2015-01-19 23:42:11 +01:00
# Need to create GLib.Bytes explicitly until this bug is fixed:
# https://bugzilla.gnome.org/show_bug.cgi?id=729541
blob = hb.glib_blob_create (GLib.Bytes.new (fontdata))
face = hb.face_create (blob, 0)
del blob
font = hb.font_create (face)
upem = hb.face_get_upem (face)
del face
hb.font_set_scale (font, upem, upem)
#hb.ft_font_set_funcs (font)
hb.ot_font_set_funcs (font)
buf = hb.buffer_create ()
2015-01-07 19:51:44 +01:00
hb.buffer_add_utf8 (buf, text.encode('utf-8'), 0, -1)
hb.buffer_guess_segment_properties (buf)
hb.shape (font, buf, [])
del font
infos = hb.buffer_get_glyph_infos (buf)
positions = hb.buffer_get_glyph_positions (buf)
for info,pos in zip(infos, positions):
gid = info.codepoint
cluster = info.cluster
2015-01-07 19:51:44 +01:00
x_advance = pos.x_advance
x_offset = pos.x_offset
y_offset = pos.y_offset
2015-01-07 19:51:44 +01:00
print("gid%d=%d@%d,%d+%d" % (gid, cluster, x_advance, x_offset, y_offset))