1e478ebe4d3f74dc271ffe88680fd29f6b1924c93Behdad Esfahbod#!/usr/bin/python
2b632e7997d9cb6e4782cab6d8c62e8e5edaa4cb0Behdad Esfahbod# -*- coding: utf-8 -*-
3e478ebe4d3f74dc271ffe88680fd29f6b1924c93Behdad Esfahbod
4b632e7997d9cb6e4782cab6d8c62e8e5edaa4cb0Behdad Esfahbodfrom __future__ import print_function
5e478ebe4d3f74dc271ffe88680fd29f6b1924c93Behdad Esfahbodimport sys
6d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbodimport array
7e478ebe4d3f74dc271ffe88680fd29f6b1924c93Behdad Esfahbodfrom gi.repository import HarfBuzz as hb
82cd5323531dcd800549b2cb1cb51d708e72ab2d8Behdad Esfahbodfrom gi.repository import GLib
9e478ebe4d3f74dc271ffe88680fd29f6b1924c93Behdad Esfahbod
1081a31f3eff44a85bb2160d51156a01a18f0a97dfBehdad Esfahbod# Python 2/3 compatibility
1181a31f3eff44a85bb2160d51156a01a18f0a97dfBehdad Esfahbodtry:
1281a31f3eff44a85bb2160d51156a01a18f0a97dfBehdad Esfahbod	unicode
1381a31f3eff44a85bb2160d51156a01a18f0a97dfBehdad Esfahbodexcept NameError:
1481a31f3eff44a85bb2160d51156a01a18f0a97dfBehdad Esfahbod	unicode = str
1581a31f3eff44a85bb2160d51156a01a18f0a97dfBehdad Esfahbod
1681a31f3eff44a85bb2160d51156a01a18f0a97dfBehdad Esfahboddef tounicode(s, encoding='utf-8'):
1781a31f3eff44a85bb2160d51156a01a18f0a97dfBehdad Esfahbod	if not isinstance(s, unicode):
1881a31f3eff44a85bb2160d51156a01a18f0a97dfBehdad Esfahbod		return s.decode(encoding)
1981a31f3eff44a85bb2160d51156a01a18f0a97dfBehdad Esfahbod	else:
2081a31f3eff44a85bb2160d51156a01a18f0a97dfBehdad Esfahbod		return s
2181a31f3eff44a85bb2160d51156a01a18f0a97dfBehdad Esfahbod
22b632e7997d9cb6e4782cab6d8c62e8e5edaa4cb0Behdad Esfahbodfontdata = open (sys.argv[1], 'rb').read ()
23238d6a38f2ceb7d8dceec9365a823f032b3b9f7dBehdad Esfahbodtext = tounicode(sys.argv[2])
24e9f5c65be027eb7759ab819e267e24dff3b017b6Behdad Esfahbod# Need to create GLib.Bytes explicitly until this bug is fixed:
25e9f5c65be027eb7759ab819e267e24dff3b017b6Behdad Esfahbod# https://bugzilla.gnome.org/show_bug.cgi?id=729541
262cd5323531dcd800549b2cb1cb51d708e72ab2d8Behdad Esfahbodblob = hb.glib_blob_create (GLib.Bytes.new (fontdata))
27b632e7997d9cb6e4782cab6d8c62e8e5edaa4cb0Behdad Esfahbodface = hb.face_create (blob, 0)
282cd5323531dcd800549b2cb1cb51d708e72ab2d8Behdad Esfahboddel blob
29b632e7997d9cb6e4782cab6d8c62e8e5edaa4cb0Behdad Esfahbodfont = hb.font_create (face)
30b632e7997d9cb6e4782cab6d8c62e8e5edaa4cb0Behdad Esfahbodupem = hb.face_get_upem (face)
312cd5323531dcd800549b2cb1cb51d708e72ab2d8Behdad Esfahboddel face
32b632e7997d9cb6e4782cab6d8c62e8e5edaa4cb0Behdad Esfahbodhb.font_set_scale (font, upem, upem)
33b632e7997d9cb6e4782cab6d8c62e8e5edaa4cb0Behdad Esfahbod#hb.ft_font_set_funcs (font)
34b632e7997d9cb6e4782cab6d8c62e8e5edaa4cb0Behdad Esfahbodhb.ot_font_set_funcs (font)
35b632e7997d9cb6e4782cab6d8c62e8e5edaa4cb0Behdad Esfahbod
362cd5323531dcd800549b2cb1cb51d708e72ab2d8Behdad Esfahbodbuf = hb.buffer_create ()
378718dae818b22c3933c33bdfc8a7413ef4c3110aBehdad Esfahbodclass Debugger(object):
388718dae818b22c3933c33bdfc8a7413ef4c3110aBehdad Esfahbod	def message (self, buf, font, msg, data, _x_what_is_this):
398718dae818b22c3933c33bdfc8a7413ef4c3110aBehdad Esfahbod		print(msg)
408718dae818b22c3933c33bdfc8a7413ef4c3110aBehdad Esfahbod		return True
418718dae818b22c3933c33bdfc8a7413ef4c3110aBehdad Esfahboddebugger = Debugger()
428718dae818b22c3933c33bdfc8a7413ef4c3110aBehdad Esfahbodhb.buffer_set_message_func (buf, debugger.message, 1, 0)
43d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbod
44d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbod##
45d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbod## Add text to buffer
46d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbod##
47d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbod#
48d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbod# See https://github.com/behdad/harfbuzz/pull/271
49d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbod#
50d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbodif False:
51d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbod	# If you do not care about cluster values reflecting Python
52d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbod	# string indices, then this is quickest way to add text to
53d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbod	# buffer:
54d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbod	hb.buffer_add_utf8 (buf, text.encode('utf-8'), 0, -1)
55d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbod	# Otherwise, then following handles both narrow and wide
56d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbod	# Python builds:
57d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbodelif sys.maxunicode == 0x10FFFF:
58d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbod	hb.buffer_add_utf32 (buf, array.array('I', text.encode('utf-32')), 0, -1)
59d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbodelse:
60d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbod	hb.buffer_add_utf16 (buf, array.array('H', text.encode('utf-16')), 0, -1)
61d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbod
62d3e2a06b0f2587e913a9c3ff1a20c187f260db80Behdad Esfahbod
632cd5323531dcd800549b2cb1cb51d708e72ab2d8Behdad Esfahbodhb.buffer_guess_segment_properties (buf)
642cd5323531dcd800549b2cb1cb51d708e72ab2d8Behdad Esfahbod
65b632e7997d9cb6e4782cab6d8c62e8e5edaa4cb0Behdad Esfahbodhb.shape (font, buf, [])
662cd5323531dcd800549b2cb1cb51d708e72ab2d8Behdad Esfahboddel font
67b632e7997d9cb6e4782cab6d8c62e8e5edaa4cb0Behdad Esfahbod
68b632e7997d9cb6e4782cab6d8c62e8e5edaa4cb0Behdad Esfahbodinfos = hb.buffer_get_glyph_infos (buf)
69b632e7997d9cb6e4782cab6d8c62e8e5edaa4cb0Behdad Esfahbodpositions = hb.buffer_get_glyph_positions (buf)
70b632e7997d9cb6e4782cab6d8c62e8e5edaa4cb0Behdad Esfahbod
71b632e7997d9cb6e4782cab6d8c62e8e5edaa4cb0Behdad Esfahbodfor info,pos in zip(infos, positions):
72b632e7997d9cb6e4782cab6d8c62e8e5edaa4cb0Behdad Esfahbod	gid = info.codepoint
73b632e7997d9cb6e4782cab6d8c62e8e5edaa4cb0Behdad Esfahbod	cluster = info.cluster
74238d6a38f2ceb7d8dceec9365a823f032b3b9f7dBehdad Esfahbod	x_advance = pos.x_advance
75238d6a38f2ceb7d8dceec9365a823f032b3b9f7dBehdad Esfahbod	x_offset = pos.x_offset
76238d6a38f2ceb7d8dceec9365a823f032b3b9f7dBehdad Esfahbod	y_offset = pos.y_offset
77b632e7997d9cb6e4782cab6d8c62e8e5edaa4cb0Behdad Esfahbod
78238d6a38f2ceb7d8dceec9365a823f032b3b9f7dBehdad Esfahbod	print("gid%d=%d@%d,%d+%d" % (gid, cluster, x_advance, x_offset, y_offset))
79