Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 1 | #!/usr/bin/env python |
Behdad Esfahbod | b632e79 | 2015-01-06 14:05:26 -0800 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Behdad Esfahbod | e478ebe | 2013-09-12 20:53:07 -0400 | [diff] [blame] | 3 | |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 4 | from __future__ import print_function, division, absolute_import |
| 5 | |
Behdad Esfahbod | e478ebe | 2013-09-12 20:53:07 -0400 | [diff] [blame] | 6 | import sys |
Behdad Esfahbod | d3e2a06 | 2016-06-30 11:01:22 -0700 | [diff] [blame] | 7 | import array |
Behdad Esfahbod | e478ebe | 2013-09-12 20:53:07 -0400 | [diff] [blame] | 8 | from gi.repository import HarfBuzz as hb |
Behdad Esfahbod | 2cd5323 | 2015-01-06 19:16:38 -0800 | [diff] [blame] | 9 | from gi.repository import GLib |
Behdad Esfahbod | e478ebe | 2013-09-12 20:53:07 -0400 | [diff] [blame] | 10 | |
Behdad Esfahbod | 81a31f3 | 2015-01-06 15:37:31 -0800 | [diff] [blame] | 11 | # Python 2/3 compatibility |
| 12 | try: |
| 13 | unicode |
| 14 | except NameError: |
| 15 | unicode = str |
| 16 | |
| 17 | def tounicode(s, encoding='utf-8'): |
| 18 | if not isinstance(s, unicode): |
| 19 | return s.decode(encoding) |
| 20 | else: |
| 21 | return s |
| 22 | |
Behdad Esfahbod | b632e79 | 2015-01-06 14:05:26 -0800 | [diff] [blame] | 23 | fontdata = open (sys.argv[1], 'rb').read () |
Behdad Esfahbod | 238d6a3 | 2015-01-07 10:51:44 -0800 | [diff] [blame] | 24 | text = tounicode(sys.argv[2]) |
Behdad Esfahbod | e9f5c65 | 2015-01-19 14:42:11 -0800 | [diff] [blame] | 25 | # Need to create GLib.Bytes explicitly until this bug is fixed: |
| 26 | # https://bugzilla.gnome.org/show_bug.cgi?id=729541 |
Behdad Esfahbod | 2cd5323 | 2015-01-06 19:16:38 -0800 | [diff] [blame] | 27 | blob = hb.glib_blob_create (GLib.Bytes.new (fontdata)) |
Behdad Esfahbod | b632e79 | 2015-01-06 14:05:26 -0800 | [diff] [blame] | 28 | face = hb.face_create (blob, 0) |
Behdad Esfahbod | 2cd5323 | 2015-01-06 19:16:38 -0800 | [diff] [blame] | 29 | del blob |
Behdad Esfahbod | b632e79 | 2015-01-06 14:05:26 -0800 | [diff] [blame] | 30 | font = hb.font_create (face) |
| 31 | upem = hb.face_get_upem (face) |
Behdad Esfahbod | 2cd5323 | 2015-01-06 19:16:38 -0800 | [diff] [blame] | 32 | del face |
Behdad Esfahbod | b632e79 | 2015-01-06 14:05:26 -0800 | [diff] [blame] | 33 | hb.font_set_scale (font, upem, upem) |
| 34 | #hb.ft_font_set_funcs (font) |
| 35 | hb.ot_font_set_funcs (font) |
| 36 | |
Behdad Esfahbod | 2cd5323 | 2015-01-06 19:16:38 -0800 | [diff] [blame] | 37 | buf = hb.buffer_create () |
Behdad Esfahbod | 8718dae | 2015-12-18 19:53:40 +0000 | [diff] [blame] | 38 | class Debugger(object): |
| 39 | def message (self, buf, font, msg, data, _x_what_is_this): |
| 40 | print(msg) |
| 41 | return True |
| 42 | debugger = Debugger() |
| 43 | hb.buffer_set_message_func (buf, debugger.message, 1, 0) |
Behdad Esfahbod | d3e2a06 | 2016-06-30 11:01:22 -0700 | [diff] [blame] | 44 | |
| 45 | ## |
| 46 | ## Add text to buffer |
| 47 | ## |
| 48 | # |
ebraminio | 7c6937e | 2017-11-20 14:49:22 -0500 | [diff] [blame] | 49 | # See https://github.com/harfbuzz/harfbuzz/pull/271 |
Behdad Esfahbod | d3e2a06 | 2016-06-30 11:01:22 -0700 | [diff] [blame] | 50 | # |
| 51 | if 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 Hosny | d27e5ec | 2018-10-02 08:25:29 +0200 | [diff] [blame] | 57 | # Python builds (the first item in the array is BOM, so we skip it): |
Behdad Esfahbod | d3e2a06 | 2016-06-30 11:01:22 -0700 | [diff] [blame] | 58 | elif sys.maxunicode == 0x10FFFF: |
Khaled Hosny | d27e5ec | 2018-10-02 08:25:29 +0200 | [diff] [blame] | 59 | hb.buffer_add_utf32 (buf, array.array('I', text.encode('utf-32'))[1:], 0, -1) |
Behdad Esfahbod | d3e2a06 | 2016-06-30 11:01:22 -0700 | [diff] [blame] | 60 | else: |
Khaled Hosny | d27e5ec | 2018-10-02 08:25:29 +0200 | [diff] [blame] | 61 | hb.buffer_add_utf16 (buf, array.array('H', text.encode('utf-16'))[1:], 0, -1) |
Behdad Esfahbod | d3e2a06 | 2016-06-30 11:01:22 -0700 | [diff] [blame] | 62 | |
| 63 | |
Behdad Esfahbod | 2cd5323 | 2015-01-06 19:16:38 -0800 | [diff] [blame] | 64 | hb.buffer_guess_segment_properties (buf) |
| 65 | |
Behdad Esfahbod | b632e79 | 2015-01-06 14:05:26 -0800 | [diff] [blame] | 66 | hb.shape (font, buf, []) |
Behdad Esfahbod | 2cd5323 | 2015-01-06 19:16:38 -0800 | [diff] [blame] | 67 | del font |
Behdad Esfahbod | b632e79 | 2015-01-06 14:05:26 -0800 | [diff] [blame] | 68 | |
| 69 | infos = hb.buffer_get_glyph_infos (buf) |
| 70 | positions = hb.buffer_get_glyph_positions (buf) |
| 71 | |
| 72 | for info,pos in zip(infos, positions): |
| 73 | gid = info.codepoint |
| 74 | cluster = info.cluster |
Behdad Esfahbod | 238d6a3 | 2015-01-07 10:51:44 -0800 | [diff] [blame] | 75 | x_advance = pos.x_advance |
| 76 | x_offset = pos.x_offset |
| 77 | y_offset = pos.y_offset |
Behdad Esfahbod | b632e79 | 2015-01-06 14:05:26 -0800 | [diff] [blame] | 78 | |
Behdad Esfahbod | 238d6a3 | 2015-01-07 10:51:44 -0800 | [diff] [blame] | 79 | print("gid%d=%d@%d,%d+%d" % (gid, cluster, x_advance, x_offset, y_offset)) |