From cab2c2c08c67e7d1606c03700df3e4e9c0dc59fd Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Thu, 29 Mar 2018 12:48:47 +0430 Subject: [PATCH] Make more gen-* scripts py3 compatible (#940) --- src/Makefile.am | 2 +- src/gen-arabic-table.py | 163 +++++++++--------- src/gen-def.py | 2 +- src/gen-indic-table.py | 120 ++++++------- src/gen-unicode-ranges.py | 8 +- src/gen-use-table.py | 3 +- src/hb-ot-shape-complex-indic-table.cc | 2 - src/sample.py | 5 +- test/fuzzing/run-shape-fuzzer-tests.py | 3 +- test/fuzzing/run-subset-fuzzer-tests.py | 3 +- .../text-rendering-tests/extract-tests.py | 3 +- test/shaping/hb_test_tools.py | 3 +- test/shaping/run-tests.py | 3 +- test/subset/run-tests.py | 2 +- 14 files changed, 166 insertions(+), 156 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index df45f73bc..db265e21b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -295,7 +295,7 @@ arabic-table: gen-arabic-table.py ArabicShaping.txt UnicodeData.txt Blocks.txt $(AM_V_GEN) $(builddir)/$^ > hb-ot-shape-complex-arabic-table.hh \ || ($(RM) hb-ot-shape-complex-arabic-table.hh; false) -indic-table: gen-indic-table.py IndicSyllabicCategory-7.0.0.txt IndicMatraCategory-7.0.0.txt Blocks.txt +indic-table: gen-indic-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt Blocks.txt $(AM_V_GEN) $(builddir)/$^ > hb-ot-shape-complex-indic-table.cc \ || ($(RM) hb-ot-shape-complex-indic-table.cc; false) diff --git a/src/gen-arabic-table.py b/src/gen-arabic-table.py index 59bd76012..a3bd30735 100755 --- a/src/gen-arabic-table.py +++ b/src/gen-arabic-table.py @@ -1,10 +1,12 @@ -#!/usr/bin/python +#!/usr/bin/env python + +from __future__ import print_function, division, absolute_import import sys import os.path if len (sys.argv) != 4: - print >>sys.stderr, "usage: ./gen-arabic-table.py ArabicShaping.txt UnicodeData.txt Blocks.txt" + print ("usage: ./gen-arabic-table.py ArabicShaping.txt UnicodeData.txt Blocks.txt", file=sys.stderr) sys.exit (1) files = [file (x) for x in sys.argv[1:]] @@ -65,9 +67,9 @@ def print_joining_table(f): assert short not in short_value.values() short_value[value] = short - print + print () for value,short in short_value.items(): - print "#define %s %s" % (short, value) + print ("#define %s %s" % (short, value)) uu = sorted(values.keys()) num = len(values) @@ -82,15 +84,15 @@ def print_joining_table(f): ranges.append([u,u]) last = u - print - print "static const uint8_t joining_table[] =" - print "{" + print () + print ("static const uint8_t joining_table[] =") + print ("{") last_block = None offset = 0 for start,end in ranges: - print - print "#define joining_offset_0x%04xu %d" % (start, offset) + print () + print ("#define joining_offset_0x%04xu %d" % (start, offset)) for u in range(start, end+1): @@ -99,53 +101,53 @@ def print_joining_table(f): if block != last_block or u == start: if u != start: - print + print () if block in all_blocks: - print "\n /* %s */" % block + print ("\n /* %s */" % block) else: - print "\n /* FILLER */" + print ("\n /* FILLER */") last_block = block if u % 32 != 0: - print - print " /* %04X */" % (u//32*32), " " * (u % 32), + print () + print (" /* %04X */" % (u//32*32), " " * (u % 32), end="") if u % 32 == 0: - print - print " /* %04X */ " % u, - sys.stdout.write("%s," % short_value[value]) - print + print () + print (" /* %04X */ " % u, end="") + print ("%s," % short_value[value], end="") + print () offset += end - start + 1 - print + print () occupancy = num * 100. / offset - print "}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy) - print + print ("}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy)) + print () page_bits = 12; - print - print "static unsigned int" - print "joining_type (hb_codepoint_t u)" - print "{" - print " switch (u >> %d)" % page_bits - print " {" + print () + print ("static unsigned int") + print ("joining_type (hb_codepoint_t u)") + print ("{") + print (" switch (u >> %d)" % page_bits) + print (" {") pages = set([u>>page_bits for u in [s for s,e in ranges]+[e for s,e in ranges]]) for p in sorted(pages): - print " case 0x%0Xu:" % p + print (" case 0x%0Xu:" % p) for (start,end) in ranges: if p not in [start>>page_bits, end>>page_bits]: continue offset = "joining_offset_0x%04xu" % start - print " if (hb_in_range (u, 0x%04Xu, 0x%04Xu)) return joining_table[u - 0x%04Xu + %s];" % (start, end, start, offset) - print " break;" - print "" - print " default:" - print " break;" - print " }" - print " return X;" - print "}" - print + print (" if (hb_in_range (u, 0x%04Xu, 0x%04Xu)) return joining_table[u - 0x%04Xu + %s];" % (start, end, start, offset)) + print (" break;") + print ("") + print (" default:") + print (" break;") + print (" }") + print (" return X;") + print ("}") + print () for value,short in short_value.items(): - print "#undef %s" % (short) - print + print ("#undef %s" % (short)) + print () def print_shaping_table(f): @@ -186,9 +188,9 @@ def print_shaping_table(f): shapes[items[0]] = {} shapes[items[0]][shape] = c - print - print "static const uint16_t shaping_table[][4] =" - print "{" + print () + print ("static const uint16_t shaping_table[][4] =") + print ("{") keys = shapes.keys () min_u, max_u = min (keys), max (keys) @@ -196,13 +198,13 @@ def print_shaping_table(f): s = [shapes[u][shape] if u in shapes and shape in shapes[u] else 0 for shape in ['initial', 'medial', 'final', 'isolated']] value = ', '.join ("0x%04Xu" % c for c in s) - print " {%s}, /* U+%04X %s */" % (value, u, names[u] if u in names else "") + print (" {%s}, /* U+%04X %s */" % (value, u, names[u] if u in names else "")) - print "};" - print - print "#define SHAPING_TABLE_FIRST 0x%04Xu" % min_u - print "#define SHAPING_TABLE_LAST 0x%04Xu" % max_u - print + print ("};") + print () + print ("#define SHAPING_TABLE_FIRST 0x%04Xu" % min_u) + print ("#define SHAPING_TABLE_LAST 0x%04Xu" % max_u) + print () ligas = {} for pair in ligatures.keys (): @@ -218,52 +220,51 @@ def print_shaping_table(f): ligas[liga[0]] = [] ligas[liga[0]].append ((liga[1], c)) max_i = max (len (ligas[l]) for l in ligas) - print - print "static const struct ligature_set_t {" - print " uint16_t first;" - print " struct ligature_pairs_t {" - print " uint16_t second;" - print " uint16_t ligature;" - print " } ligatures[%d];" % max_i - print "} ligature_table[] =" - print "{" + print () + print ("static const struct ligature_set_t {") + print (" uint16_t first;") + print (" struct ligature_pairs_t {") + print (" uint16_t second;") + print (" uint16_t ligature;") + print (" } ligatures[%d];" % max_i) + print ("} ligature_table[] =") + print ("{") keys = ligas.keys () keys.sort () for first in keys: - print " { 0x%04Xu, {" % (first) + print (" { 0x%04Xu, {" % (first)) for liga in ligas[first]: - print " { 0x%04Xu, 0x%04Xu }, /* %s */" % (liga[0], liga[1], names[liga[1]]) - print " }}," + print (" { 0x%04Xu, 0x%04Xu }, /* %s */" % (liga[0], liga[1], names[liga[1]])) + print (" }},") - print "};" - print + print ("};") + print () -print "/* == Start of generated table == */" -print "/*" -print " * The following table is generated by running:" -print " *" -print " * ./gen-arabic-table.py ArabicShaping.txt UnicodeData.txt Blocks.txt" -print " *" -print " * on files with these headers:" -print " *" +print ("/* == Start of generated table == */") +print ("/*") +print (" * The following table is generated by running:") +print (" *") +print (" * ./gen-arabic-table.py ArabicShaping.txt UnicodeData.txt Blocks.txt") +print (" *") +print (" * on files with these headers:") +print (" *") for h in headers: for l in h: - print " * %s" % (l.strip()) -print " */" -print -print "#ifndef HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH" -print "#define HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH" -print + print (" * %s" % (l.strip())) +print (" */") +print () +print ("#ifndef HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH") +print ("#define HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH") +print () read_blocks (files[2]) print_joining_table (files[0]) print_shaping_table (files[1]) -print -print "#endif /* HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH */" -print -print "/* == End of generated table == */" - +print () +print ("#endif /* HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH */") +print () +print ("/* == End of generated table == */") diff --git a/src/gen-def.py b/src/gen-def.py index de35eb7d0..9a997d65a 100755 --- a/src/gen-def.py +++ b/src/gen-def.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -from __future__ import print_function +from __future__ import print_function, division, absolute_import import io, os, re, sys diff --git a/src/gen-indic-table.py b/src/gen-indic-table.py index 735b9015e..7627be31b 100755 --- a/src/gen-indic-table.py +++ b/src/gen-indic-table.py @@ -1,9 +1,11 @@ -#!/usr/bin/python +#!/usr/bin/env python + +from __future__ import print_function, division, absolute_import import sys if len (sys.argv) != 4: - print >>sys.stderr, "usage: ./gen-indic-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt Blocks.txt" + print ("usage: ./gen-indic-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt Blocks.txt", file=sys.stderr) sys.exit (1) ALLOWED_SINGLES = [0x00A0, 0x25CC] @@ -87,21 +89,21 @@ for u in ALLOWED_SINGLES: singles[u] = data[u] del data[u] -print "/* == Start of generated table == */" -print "/*" -print " * The following table is generated by running:" -print " *" -print " * ./gen-indic-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt Blocks.txt" -print " *" -print " * on files with these headers:" -print " *" +print ("/* == Start of generated table == */") +print ("/*") +print (" * The following table is generated by running:") +print (" *") +print (" * ./gen-indic-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt Blocks.txt") +print (" *") +print (" * on files with these headers:") +print (" *") for h in headers: for l in h: - print " * %s" % (l.strip()) -print " */" -print -print '#include "hb-ot-shape-complex-indic-private.hh"' -print + print (" * %s" % (l.strip())) +print (" */") +print () +print ('#include "hb-ot-shape-complex-indic-private.hh"') +print () # Shorten values short = [{ @@ -130,7 +132,7 @@ for i in range (2): what = ["INDIC_SYLLABIC_CATEGORY", "INDIC_MATRA_CATEGORY"] what_short = ["ISC", "IMC"] for i in range (2): - print + print () vv = values[i].keys () vv.sort () for v in vv: @@ -143,14 +145,14 @@ for i in range (2): raise Exception ("Duplicate short value alias", v, all_shorts[i][s]) all_shorts[i][s] = v short[i][v] = s - print "#define %s_%s %s_%s %s/* %3d chars; %s */" % \ - (what_short[i], s, what[i], v.upper (), \ - ' '* ((48-1 - len (what[i]) - 1 - len (v)) / 8), \ - values[i][v], v) -print -print "#define _(S,M) INDIC_COMBINE_CATEGORIES (ISC_##S, IMC_##M)" -print -print + print ("#define %s_%s %s_%s %s/* %3d chars; %s */" % + (what_short[i], s, what[i], v.upper (), + ' '* ((48-1 - len (what[i]) - 1 - len (v)) // 8), + values[i][v], v)) +print () +print ("#define _(S,M) INDIC_COMBINE_CATEGORIES (ISC_##S, IMC_##M)") +print () +print () total = 0 used = 0 @@ -158,20 +160,20 @@ last_block = None def print_block (block, start, end, data): global total, used, last_block if block and block != last_block: - print - print - print " /* %s */" % block + print () + print () + print (" /* %s */" % block) num = 0 assert start % 8 == 0 assert (end+1) % 8 == 0 for u in range (start, end+1): if u % 8 == 0: - print - print " /* %04X */" % u, + print () + print (" /* %04X */" % u, end="") if u in data: num += 1 d = data.get (u, defaults) - sys.stdout.write ("%9s" % ("_(%s,%s)," % (short[0][d[0]], short[1][d[1]]))) + print ("%9s" % ("_(%s,%s)," % (short[0][d[0]], short[1][d[1]])), end="") total += end - start + 1 used += num @@ -186,7 +188,7 @@ num = 0 offset = 0 starts = [] ends = [] -print "static const INDIC_TABLE_ELEMENT_TYPE indic_table[] = {" +print ("static const INDIC_TABLE_ELEMENT_TYPE indic_table[] = {") for u in uu: if u <= last: continue @@ -206,54 +208,54 @@ for u in uu: if last >= 0: ends.append (last + 1) offset += ends[-1] - starts[-1] - print - print - print "#define indic_offset_0x%04xu %d" % (start, offset) + print () + print () + print ("#define indic_offset_0x%04xu %d" % (start, offset)) starts.append (start) print_block (block, start, end, data) last = end ends.append (last + 1) offset += ends[-1] - starts[-1] -print -print +print () +print () occupancy = used * 100. / total page_bits = 12 -print "}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy) -print -print "INDIC_TABLE_ELEMENT_TYPE" -print "hb_indic_get_categories (hb_codepoint_t u)" -print "{" -print " switch (u >> %d)" % page_bits -print " {" +print ("}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy)) +print () +print ("INDIC_TABLE_ELEMENT_TYPE") +print ("hb_indic_get_categories (hb_codepoint_t u)") +print ("{") +print (" switch (u >> %d)" % page_bits) +print (" {") pages = set([u>>page_bits for u in starts+ends+singles.keys()]) for p in sorted(pages): - print " case 0x%0Xu:" % p + print (" case 0x%0Xu:" % p) for u,d in singles.items (): if p != u>>page_bits: continue - print " if (unlikely (u == 0x%04Xu)) return _(%s,%s);" % (u, short[0][d[0]], short[1][d[1]]) + print (" if (unlikely (u == 0x%04Xu)) return _(%s,%s);" % (u, short[0][d[0]], short[1][d[1]])) for (start,end) in zip (starts, ends): if p not in [start>>page_bits, end>>page_bits]: continue offset = "indic_offset_0x%04xu" % start - print " if (hb_in_range (u, 0x%04Xu, 0x%04Xu)) return indic_table[u - 0x%04Xu + %s];" % (start, end-1, start, offset) - print " break;" - print "" -print " default:" -print " break;" -print " }" -print " return _(x,x);" -print "}" -print -print "#undef _" + print (" if (hb_in_range (u, 0x%04Xu, 0x%04Xu)) return indic_table[u - 0x%04Xu + %s];" % (start, end-1, start, offset)) + print (" break;") + print ("") +print (" default:") +print (" break;") +print (" }") +print (" return _(x,x);") +print ("}") +print () +print ("#undef _") for i in range (2): print vv = values[i].keys () vv.sort () for v in vv: - print "#undef %s_%s" % \ - (what_short[i], short[i][v]) -print -print "/* == End of generated table == */" + print ("#undef %s_%s" % + (what_short[i], short[i][v])) +print () +print ("/* == End of generated table == */") # Maintain at least 30% occupancy in the table */ if occupancy < 30: diff --git a/src/gen-unicode-ranges.py b/src/gen-unicode-ranges.py index 3b59cd862..30249a8f8 100644 --- a/src/gen-unicode-ranges.py +++ b/src/gen-unicode-ranges.py @@ -4,6 +4,8 @@ # Input is a tab seperated list of unicode ranges from the otspec # (https://docs.microsoft.com/en-us/typography/opentype/spec/os2#ulunicoderange1). +from __future__ import print_function, division, absolute_import + import io import re import sys @@ -11,7 +13,7 @@ import sys reload(sys) sys.setdefaultencoding('utf-8') -print (u"""static Range os2UnicodeRangesSorted[] = +print ("""static Range os2UnicodeRangesSorted[] = {""") args = sys.argv[1:] @@ -47,6 +49,6 @@ for ranges in all_ranges: end = ("0x%X" % ranges[1]).rjust(8) bit = ("%s" % ranges[2]).rjust(3) - print " {%s, %s, %s}, // %s" % (start, end, bit, ranges[3]) + print (" {%s, %s, %s}, // %s" % (start, end, bit, ranges[3])) -print (u"""};"""); +print ("""};""") diff --git a/src/gen-use-table.py b/src/gen-use-table.py index ecbdfb951..5726002a1 100755 --- a/src/gen-use-table.py +++ b/src/gen-use-table.py @@ -1,6 +1,7 @@ #!/usr/bin/env python -from __future__ import print_function +from __future__ import print_function, division, absolute_import + import sys if len (sys.argv) != 5: diff --git a/src/hb-ot-shape-complex-indic-table.cc b/src/hb-ot-shape-complex-indic-table.cc index 867cfb33b..5817b8346 100644 --- a/src/hb-ot-shape-complex-indic-table.cc +++ b/src/hb-ot-shape-complex-indic-table.cc @@ -430,7 +430,6 @@ hb_indic_get_categories (hb_codepoint_t u) } #undef _ - #undef ISC_A #undef ISC_Bi #undef ISC_BJN @@ -466,7 +465,6 @@ hb_indic_get_categories (hb_codepoint_t u) #undef ISC_Vo #undef ISC_M #undef ISC_VI - #undef IMC_B #undef IMC_BL #undef IMC_BR diff --git a/src/sample.py b/src/sample.py index 844fa4c8c..8f97195f2 100755 --- a/src/sample.py +++ b/src/sample.py @@ -1,7 +1,8 @@ -#!/usr/bin/python +#!/usr/bin/env python # -*- coding: utf-8 -*- -from __future__ import print_function +from __future__ import print_function, division, absolute_import + import sys import array from gi.repository import HarfBuzz as hb diff --git a/test/fuzzing/run-shape-fuzzer-tests.py b/test/fuzzing/run-shape-fuzzer-tests.py index 43378d1ba..fea0b01b4 100755 --- a/test/fuzzing/run-shape-fuzzer-tests.py +++ b/test/fuzzing/run-shape-fuzzer-tests.py @@ -1,6 +1,7 @@ #!/usr/bin/env python -from __future__ import print_function +from __future__ import print_function, division, absolute_import + import sys, os, subprocess srcdir = os.environ.get ("srcdir", ".") diff --git a/test/fuzzing/run-subset-fuzzer-tests.py b/test/fuzzing/run-subset-fuzzer-tests.py index 229881a4c..235752311 100755 --- a/test/fuzzing/run-subset-fuzzer-tests.py +++ b/test/fuzzing/run-subset-fuzzer-tests.py @@ -1,6 +1,7 @@ #!/usr/bin/env python -from __future__ import print_function +from __future__ import print_function, division, absolute_import + import sys, os, subprocess srcdir = os.environ.get ("srcdir", ".") diff --git a/test/shaping/data/text-rendering-tests/extract-tests.py b/test/shaping/data/text-rendering-tests/extract-tests.py index 8e5909fac..36963e5e1 100755 --- a/test/shaping/data/text-rendering-tests/extract-tests.py +++ b/test/shaping/data/text-rendering-tests/extract-tests.py @@ -1,6 +1,7 @@ #!/usr/bin/env python -from __future__ import print_function +from __future__ import print_function, division, absolute_import + import sys import xml.etree.ElementTree as ET diff --git a/test/shaping/hb_test_tools.py b/test/shaping/hb_test_tools.py index f7e594347..de4516ee8 100644 --- a/test/shaping/hb_test_tools.py +++ b/test/shaping/hb_test_tools.py @@ -1,6 +1,7 @@ #!/usr/bin/env python -from __future__ import print_function +from __future__ import print_function, division, absolute_import + import sys, os, re, difflib, unicodedata, errno, cgi from itertools import * diff --git a/test/shaping/run-tests.py b/test/shaping/run-tests.py index c65e7142c..73b61c21a 100755 --- a/test/shaping/run-tests.py +++ b/test/shaping/run-tests.py @@ -1,6 +1,7 @@ #!/usr/bin/env python -from __future__ import print_function +from __future__ import print_function, division, absolute_import + import sys, os, subprocess diff --git a/test/subset/run-tests.py b/test/subset/run-tests.py index 6d28b24db..0b119fee6 100755 --- a/test/subset/run-tests.py +++ b/test/subset/run-tests.py @@ -3,7 +3,7 @@ # Runs a subsetting test suite. Compares the results of subsetting via harfbuz # to subsetting via fonttools. -from __future__ import print_function +from __future__ import print_function, division, absolute_import import io from difflib import unified_diff