From 988350586f607c7a46bbb658a2abecfd004f41fb Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Sat, 18 Jun 2016 21:12:19 +0200 Subject: [PATCH 1/2] =?UTF-8?q?[tests]=20Workaround=20Python=202=20?= =?UTF-8?q?=E2=80=9Cnarrow=E2=80=9D=20builds?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The so-called Python 2 “narrow” builds support UCS2 only, this is a workaround to allow unichr to work with any Unicode character in such builds. This fixes Travis-CI failure as it has narrow Python 2 builds. Copied from: https://github.com/behdad/fonttools/blob/master/Lib/fontTools/misc/py23.py --- test/shaping/hb_test_tools.py | 38 ++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/test/shaping/hb_test_tools.py b/test/shaping/hb_test_tools.py index 747699b87..747398299 100644 --- a/test/shaping/hb_test_tools.py +++ b/test/shaping/hb_test_tools.py @@ -7,7 +7,43 @@ from itertools import * diff_symbols = "-+=*&^%$#@!~/" diff_colors = ['red', 'green', 'blue'] -if sys.version_info[0] >= 3: +try: + unichr = unichr + + if sys.maxunicode < 0x10FFFF: + # workarounds for Python 2 "narrow" builds with UCS2-only support. + + _narrow_unichr = unichr + + def unichr(i): + """ + Return the unicode character whose Unicode code is the integer 'i'. + The valid range is 0 to 0x10FFFF inclusive. + + >>> _narrow_unichr(0xFFFF + 1) + Traceback (most recent call last): + File "", line 1, in ? + ValueError: unichr() arg not in range(0x10000) (narrow Python build) + >>> unichr(0xFFFF + 1) == u'\U00010000' + True + >>> unichr(1114111) == u'\U0010FFFF' + True + >>> unichr(0x10FFFF + 1) + Traceback (most recent call last): + File "", line 1, in ? + ValueError: unichr() arg not in range(0x110000) + """ + try: + return _narrow_unichr(i) + except ValueError: + try: + padded_hex_str = hex(i)[2:].zfill(8) + escape_str = "\\U" + padded_hex_str + return escape_str.decode("unicode-escape") + except UnicodeDecodeError: + raise ValueError('unichr() arg not in range(0x110000)') + +except NameError: unichr = chr class ColorFormatter: From 07461d06d242cd5cfda7ccb891189f074a89b460 Mon Sep 17 00:00:00 2001 From: Kelvin Date: Sat, 18 Jun 2016 22:46:38 +0000 Subject: [PATCH 2/2] Use UTF-32 in Python sample --- src/sample.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sample.py b/src/sample.py index 19a4fdcbc..cfbc12265 100755 --- a/src/sample.py +++ b/src/sample.py @@ -20,6 +20,7 @@ def tounicode(s, encoding='utf-8'): fontdata = open (sys.argv[1], 'rb').read () text = tounicode(sys.argv[2]) +codepoints = list(map(ord, text)) # Need to create GLib.Bytes explicitly until this bug is fixed: # https://bugzilla.gnome.org/show_bug.cgi?id=729541 blob = hb.glib_blob_create (GLib.Bytes.new (fontdata)) @@ -39,7 +40,7 @@ class Debugger(object): return True debugger = Debugger() hb.buffer_set_message_func (buf, debugger.message, 1, 0) -hb.buffer_add_utf8 (buf, text.encode('utf-8'), 0, -1) +hb.buffer_add_utf32 (buf, codepoints, 0, len(codepoints)) hb.buffer_guess_segment_properties (buf) hb.shape (font, buf, [])