blob: f8d221699d31069780fdd1323095c9086298e9e6 [file] [log] [blame]
Behdad Esfahbode478ebe2013-09-12 20:53:07 -04001#!/usr/bin/python
Behdad Esfahbodb632e792015-01-06 14:05:26 -08002# -*- coding: utf-8 -*-
Behdad Esfahbode478ebe2013-09-12 20:53:07 -04003
Behdad Esfahbodb632e792015-01-06 14:05:26 -08004from __future__ import print_function
Behdad Esfahbode478ebe2013-09-12 20:53:07 -04005import sys
6from gi.repository import HarfBuzz as hb
Behdad Esfahbod2cd53232015-01-06 19:16:38 -08007from gi.repository import GLib
Behdad Esfahbode478ebe2013-09-12 20:53:07 -04008
Behdad Esfahbod81a31f32015-01-06 15:37:31 -08009# Python 2/3 compatibility
10try:
11 unicode
12except NameError:
13 unicode = str
14
15def tounicode(s, encoding='utf-8'):
16 if not isinstance(s, unicode):
17 return s.decode(encoding)
18 else:
19 return s
20
Behdad Esfahbodb632e792015-01-06 14:05:26 -080021fontdata = open (sys.argv[1], 'rb').read ()
Behdad Esfahbod238d6a32015-01-07 10:51:44 -080022text = tounicode(sys.argv[2])
Behdad Esfahbode9f5c652015-01-19 14:42:11 -080023# Need to create GLib.Bytes explicitly until this bug is fixed:
24# https://bugzilla.gnome.org/show_bug.cgi?id=729541
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080025blob = hb.glib_blob_create (GLib.Bytes.new (fontdata))
Behdad Esfahbodb632e792015-01-06 14:05:26 -080026face = hb.face_create (blob, 0)
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080027del blob
Behdad Esfahbodb632e792015-01-06 14:05:26 -080028font = hb.font_create (face)
29upem = hb.face_get_upem (face)
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080030del face
Behdad Esfahbodb632e792015-01-06 14:05:26 -080031hb.font_set_scale (font, upem, upem)
32#hb.ft_font_set_funcs (font)
33hb.ot_font_set_funcs (font)
34
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080035buf = hb.buffer_create ()
Behdad Esfahbod238d6a32015-01-07 10:51:44 -080036hb.buffer_add_utf8 (buf, text.encode('utf-8'), 0, -1)
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080037hb.buffer_guess_segment_properties (buf)
38
Behdad Esfahbodb632e792015-01-06 14:05:26 -080039hb.shape (font, buf, [])
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080040del font
Behdad Esfahbodb632e792015-01-06 14:05:26 -080041
42infos = hb.buffer_get_glyph_infos (buf)
43positions = hb.buffer_get_glyph_positions (buf)
44
45for info,pos in zip(infos, positions):
46 gid = info.codepoint
47 cluster = info.cluster
Behdad Esfahbod238d6a32015-01-07 10:51:44 -080048 x_advance = pos.x_advance
49 x_offset = pos.x_offset
50 y_offset = pos.y_offset
Behdad Esfahbodb632e792015-01-06 14:05:26 -080051
Behdad Esfahbod238d6a32015-01-07 10:51:44 -080052 print("gid%d=%d@%d,%d+%d" % (gid, cluster, x_advance, x_offset, y_offset))