blob: fd8504800f24d133d4c19ee9596a0a5456560b38 [file] [log] [blame]
Ebrahim Byagowi8d199072020-02-19 14:56:55 +03301#!/usr/bin/env python3
Ebrahim Byagowicab2c2c2018-03-29 12:48:47 +04302
Behdad Esfahbode478ebe2013-09-12 20:53:07 -04003import sys
Behdad Esfahbodd3e2a062016-06-30 11:01:22 -07004import array
Behdad Esfahbode478ebe2013-09-12 20:53:07 -04005from gi.repository import HarfBuzz as hb
Behdad Esfahbod2cd53232015-01-06 19:16:38 -08006from gi.repository import GLib
Behdad Esfahbode478ebe2013-09-12 20:53:07 -04007
Behdad Esfahbodb632e792015-01-06 14:05:26 -08008fontdata = open (sys.argv[1], 'rb').read ()
Ebrahim Byagowi8d199072020-02-19 14:56:55 +03309text = sys.argv[2]
Behdad Esfahbode9f5c652015-01-19 14:42:11 -080010# Need to create GLib.Bytes explicitly until this bug is fixed:
11# https://bugzilla.gnome.org/show_bug.cgi?id=729541
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080012blob = hb.glib_blob_create (GLib.Bytes.new (fontdata))
Behdad Esfahbodb632e792015-01-06 14:05:26 -080013face = hb.face_create (blob, 0)
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080014del blob
Behdad Esfahbodb632e792015-01-06 14:05:26 -080015font = hb.font_create (face)
16upem = hb.face_get_upem (face)
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080017del face
Behdad Esfahbodb632e792015-01-06 14:05:26 -080018hb.font_set_scale (font, upem, upem)
19#hb.ft_font_set_funcs (font)
20hb.ot_font_set_funcs (font)
21
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080022buf = hb.buffer_create ()
Ebrahim Byagowi2a182122020-07-13 20:41:13 +043023class Debugger (object):
Behdad Esfahbod8718dae2015-12-18 19:53:40 +000024 def message (self, buf, font, msg, data, _x_what_is_this):
Ebrahim Byagowi2a182122020-07-13 20:41:13 +043025 print (msg)
Behdad Esfahbod8718dae2015-12-18 19:53:40 +000026 return True
Ebrahim Byagowi2a182122020-07-13 20:41:13 +043027debugger = Debugger ()
Behdad Esfahbod8718dae2015-12-18 19:53:40 +000028hb.buffer_set_message_func (buf, debugger.message, 1, 0)
Behdad Esfahbodd3e2a062016-06-30 11:01:22 -070029
30##
31## Add text to buffer
32##
33#
ebraminio7c6937e2017-11-20 14:49:22 -050034# See https://github.com/harfbuzz/harfbuzz/pull/271
Behdad Esfahbodd3e2a062016-06-30 11:01:22 -070035#
Ebrahim Byagowi2a182122020-07-13 20:41:13 +043036# 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)
Behdad Esfahbodd3e2a062016-06-30 11:01:22 -070044else:
Ebrahim Byagowi2a182122020-07-13 20:41:13 +043045 hb.buffer_add_utf16 (buf, array.array ('H', text.encode ('utf-16'))[1:], 0, -1)
Behdad Esfahbodd3e2a062016-06-30 11:01:22 -070046
47
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080048hb.buffer_guess_segment_properties (buf)
49
Behdad Esfahbodb632e792015-01-06 14:05:26 -080050hb.shape (font, buf, [])
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080051del font
Behdad Esfahbodb632e792015-01-06 14:05:26 -080052
53infos = hb.buffer_get_glyph_infos (buf)
54positions = hb.buffer_get_glyph_positions (buf)
55
Ebrahim Byagowi2a182122020-07-13 20:41:13 +043056for info, pos in zip (infos, positions):
Behdad Esfahbodb632e792015-01-06 14:05:26 -080057 gid = info.codepoint
58 cluster = info.cluster
Behdad Esfahbod238d6a32015-01-07 10:51:44 -080059 x_advance = pos.x_advance
60 x_offset = pos.x_offset
61 y_offset = pos.y_offset
Behdad Esfahbodb632e792015-01-06 14:05:26 -080062
Ebrahim Byagowi2a182122020-07-13 20:41:13 +043063 print ("gid%d=%d@%d,%d+%d" % (gid, cluster, x_advance, x_offset, y_offset))