2020-02-19 12:26:55 +01:00
|
|
|
#!/usr/bin/env python3
|
2018-03-29 10:18:47 +02:00
|
|
|
|
2013-09-13 02:53:07 +02:00
|
|
|
import sys
|
2016-06-30 20:01:22 +02:00
|
|
|
import array
|
2013-09-13 02:53:07 +02:00
|
|
|
from gi.repository import HarfBuzz as hb
|
2015-01-07 04:16:38 +01:00
|
|
|
from gi.repository import GLib
|
2013-09-13 02:53:07 +02:00
|
|
|
|
2015-01-06 23:05:26 +01:00
|
|
|
fontdata = open (sys.argv[1], 'rb').read ()
|
2020-02-19 12:26:55 +01:00
|
|
|
text = 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
|
2015-01-07 04:16:38 +01:00
|
|
|
blob = hb.glib_blob_create (GLib.Bytes.new (fontdata))
|
2015-01-06 23:05:26 +01:00
|
|
|
face = hb.face_create (blob, 0)
|
2015-01-07 04:16:38 +01:00
|
|
|
del blob
|
2015-01-06 23:05:26 +01:00
|
|
|
font = hb.font_create (face)
|
|
|
|
upem = hb.face_get_upem (face)
|
2015-01-07 04:16:38 +01:00
|
|
|
del face
|
2015-01-06 23:05:26 +01:00
|
|
|
hb.font_set_scale (font, upem, upem)
|
|
|
|
#hb.ft_font_set_funcs (font)
|
|
|
|
hb.ot_font_set_funcs (font)
|
|
|
|
|
2015-01-07 04:16:38 +01:00
|
|
|
buf = hb.buffer_create ()
|
2015-12-18 20:53:40 +01:00
|
|
|
class Debugger(object):
|
|
|
|
def message (self, buf, font, msg, data, _x_what_is_this):
|
|
|
|
print(msg)
|
|
|
|
return True
|
|
|
|
debugger = Debugger()
|
|
|
|
hb.buffer_set_message_func (buf, debugger.message, 1, 0)
|
2016-06-30 20:01:22 +02:00
|
|
|
|
|
|
|
##
|
|
|
|
## Add text to buffer
|
|
|
|
##
|
|
|
|
#
|
2017-11-20 20:49:22 +01:00
|
|
|
# See https://github.com/harfbuzz/harfbuzz/pull/271
|
2016-06-30 20:01:22 +02:00
|
|
|
#
|
|
|
|
if False:
|
|
|
|
# If you do not care about cluster values reflecting Python
|
|
|
|
# string indices, then this is quickest way to add text to
|
|
|
|
# buffer:
|
|
|
|
hb.buffer_add_utf8 (buf, text.encode('utf-8'), 0, -1)
|
|
|
|
# Otherwise, then following handles both narrow and wide
|
2018-10-02 08:25:29 +02:00
|
|
|
# Python builds (the first item in the array is BOM, so we skip it):
|
2016-06-30 20:01:22 +02:00
|
|
|
elif sys.maxunicode == 0x10FFFF:
|
2018-10-02 08:25:29 +02:00
|
|
|
hb.buffer_add_utf32 (buf, array.array('I', text.encode('utf-32'))[1:], 0, -1)
|
2016-06-30 20:01:22 +02:00
|
|
|
else:
|
2018-10-02 08:25:29 +02:00
|
|
|
hb.buffer_add_utf16 (buf, array.array('H', text.encode('utf-16'))[1:], 0, -1)
|
2016-06-30 20:01:22 +02:00
|
|
|
|
|
|
|
|
2015-01-07 04:16:38 +01:00
|
|
|
hb.buffer_guess_segment_properties (buf)
|
|
|
|
|
2015-01-06 23:05:26 +01:00
|
|
|
hb.shape (font, buf, [])
|
2015-01-07 04:16:38 +01:00
|
|
|
del font
|
2015-01-06 23:05:26 +01:00
|
|
|
|
|
|
|
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-06 23:05:26 +01:00
|
|
|
|
2015-01-07 19:51:44 +01:00
|
|
|
print("gid%d=%d@%d,%d+%d" % (gid, cluster, x_advance, x_offset, y_offset))
|