1#!/usr/bin/env python3 2 3import sys 4import array 5from gi.repository import HarfBuzz as hb 6from gi.repository import GLib 7 8fontdata = open (sys.argv[1], 'rb').read () 9text = sys.argv[2] 10# Need to create GLib.Bytes explicitly until this bug is fixed: 11# https://bugzilla.gnome.org/show_bug.cgi?id=729541 12blob = hb.glib_blob_create (GLib.Bytes.new (fontdata)) 13face = hb.face_create (blob, 0) 14del blob 15font = hb.font_create (face) 16upem = hb.face_get_upem (face) 17del face 18hb.font_set_scale (font, upem, upem) 19#hb.ft_font_set_funcs (font) 20hb.ot_font_set_funcs (font) 21 22buf = hb.buffer_create () 23class Debugger (object): 24 def message (self, buf, font, msg, data, _x_what_is_this): 25 print (msg) 26 return True 27debugger = Debugger () 28hb.buffer_set_message_func (buf, debugger.message, 1, 0) 29 30## 31## Add text to buffer 32## 33# 34# See https://github.com/harfbuzz/harfbuzz/pull/271 35# 36# If you do not care about cluster values reflecting Python 37# string indices, then this is quickest way to add text to 38# buffer: 39# hb.buffer_add_utf8 (buf, text.encode('utf-8'), 0, -1) 40# Otherwise, then following handles both narrow and wide 41# Python builds (the first item in the array is BOM, so we skip it): 42if sys.maxunicode == 0x10FFFF: 43 hb.buffer_add_utf32 (buf, array.array ('I', text.encode ('utf-32'))[1:], 0, -1) 44else: 45 hb.buffer_add_utf16 (buf, array.array ('H', text.encode ('utf-16'))[1:], 0, -1) 46 47 48hb.buffer_guess_segment_properties (buf) 49 50hb.shape (font, buf, []) 51del font 52 53infos = hb.buffer_get_glyph_infos (buf) 54positions = hb.buffer_get_glyph_positions (buf) 55 56for info, pos in zip (infos, positions): 57 gid = info.codepoint 58 cluster = info.cluster 59 x_advance = pos.x_advance 60 x_offset = pos.x_offset 61 y_offset = pos.y_offset 62 63 print ("gid%d=%d@%d,%d+%d" % (gid, cluster, x_advance, x_offset, y_offset)) 64