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