blob: 5d65aa09e1ddd3ec3caddc57341234d1030eadef [file] [log] [blame]
Ebrahim Byagowicab2c2c2018-03-29 12:48:47 +04301#!/usr/bin/env python
Behdad Esfahbodb632e792015-01-06 14:05:26 -08002# -*- coding: utf-8 -*-
Behdad Esfahbode478ebe2013-09-12 20:53:07 -04003
Ebrahim Byagowicab2c2c2018-03-29 12:48:47 +04304from __future__ import print_function, division, absolute_import
5
Behdad Esfahbode478ebe2013-09-12 20:53:07 -04006import sys
Behdad Esfahbodd3e2a062016-06-30 11:01:22 -07007import array
Behdad Esfahbode478ebe2013-09-12 20:53:07 -04008from gi.repository import HarfBuzz as hb
Behdad Esfahbod2cd53232015-01-06 19:16:38 -08009from gi.repository import GLib
Behdad Esfahbode478ebe2013-09-12 20:53:07 -040010
Behdad Esfahbod81a31f32015-01-06 15:37:31 -080011# 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
Behdad Esfahbodb632e792015-01-06 14:05:26 -080023fontdata = open (sys.argv[1], 'rb').read ()
Behdad Esfahbod238d6a32015-01-07 10:51:44 -080024text = tounicode(sys.argv[2])
Behdad Esfahbode9f5c652015-01-19 14:42:11 -080025# Need to create GLib.Bytes explicitly until this bug is fixed:
26# https://bugzilla.gnome.org/show_bug.cgi?id=729541
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080027blob = hb.glib_blob_create (GLib.Bytes.new (fontdata))
Behdad Esfahbodb632e792015-01-06 14:05:26 -080028face = hb.face_create (blob, 0)
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080029del blob
Behdad Esfahbodb632e792015-01-06 14:05:26 -080030font = hb.font_create (face)
31upem = hb.face_get_upem (face)
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080032del face
Behdad Esfahbodb632e792015-01-06 14:05:26 -080033hb.font_set_scale (font, upem, upem)
34#hb.ft_font_set_funcs (font)
35hb.ot_font_set_funcs (font)
36
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080037buf = hb.buffer_create ()
Behdad Esfahbod8718dae2015-12-18 19:53:40 +000038class 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)
Behdad Esfahbodd3e2a062016-06-30 11:01:22 -070044
45##
46## Add text to buffer
47##
48#
ebraminio7c6937e2017-11-20 14:49:22 -050049# See https://github.com/harfbuzz/harfbuzz/pull/271
Behdad Esfahbodd3e2a062016-06-30 11:01:22 -070050#
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
Khaled Hosnyd27e5ec2018-10-02 08:25:29 +020057 # Python builds (the first item in the array is BOM, so we skip it):
Behdad Esfahbodd3e2a062016-06-30 11:01:22 -070058elif sys.maxunicode == 0x10FFFF:
Khaled Hosnyd27e5ec2018-10-02 08:25:29 +020059 hb.buffer_add_utf32 (buf, array.array('I', text.encode('utf-32'))[1:], 0, -1)
Behdad Esfahbodd3e2a062016-06-30 11:01:22 -070060else:
Khaled Hosnyd27e5ec2018-10-02 08:25:29 +020061 hb.buffer_add_utf16 (buf, array.array('H', text.encode('utf-16'))[1:], 0, -1)
Behdad Esfahbodd3e2a062016-06-30 11:01:22 -070062
63
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080064hb.buffer_guess_segment_properties (buf)
65
Behdad Esfahbodb632e792015-01-06 14:05:26 -080066hb.shape (font, buf, [])
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080067del font
Behdad Esfahbodb632e792015-01-06 14:05:26 -080068
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
Behdad Esfahbod238d6a32015-01-07 10:51:44 -080075 x_advance = pos.x_advance
76 x_offset = pos.x_offset
77 y_offset = pos.y_offset
Behdad Esfahbodb632e792015-01-06 14:05:26 -080078
Behdad Esfahbod238d6a32015-01-07 10:51:44 -080079 print("gid%d=%d@%d,%d+%d" % (gid, cluster, x_advance, x_offset, y_offset))