Debian Codegraph @

tests/fnttxtrender

raw
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later
3 # Copyright 2013, SIL International, All rights reserved.
4
5 import argparse
6 import codecs
7 import importlib
8 import json
9 import json.encoder
10 import os
11 import pathlib
12 import re
13 import sys
14 from fontTools.ttLib import TTFont
15 from functools import partial
16 from xml.etree.ElementTree import parse
17 from typing import Tuple
18
19
20 class GrFont(object):
21 def __init__(self, fname, size, rtl, feats={}, script=0, lang=0):
22 self.fname = fname
23 self.size = size
24 self.rtl = int(rtl)
25 self.grface = gr.Face(fname)
26 self.feats = self.grface.get_featureval(lang)
27 self.script = script
28 for f, v in feats.items():
29 fref = self.grface.get_featureref(f)
30 self.feats.set(fref, v)
31 if size > 0:
32 size = size * 96 / 72.
33 self.font = gr.Font(self.grface, size)
34 else:
35 self.font = None
36
37 def glyphs(self, text, includewidth=False):
38 seg = gr.Segment(self.font, self.grface,
39 self.script, text, self.rtl,
40 feats=self.feats)
41 res = []
42 for s in seg.slots:
43 res.append((s.gid, s.origin, seg.cinfo(s.original).unicode))
44 if includewidth:
45 res.append((None, seg.advance, 0))
46 del seg
47 return res
48
49
50 tables = {
51 'gr': ('GDEF', 'GSUB', 'GPOS'),
52 'ot': ('Gloc', 'Glat', 'Silf', 'Sill', 'Feat'),
53 'hb': (),
54 'hbot': ('Gloc', 'Glat', 'Silf', 'Sill', 'Feat'),
55 'icu': ('Gloc', 'Glat', 'Silf', 'Sill', 'Feat')
56 }
57
58
59 def roundfloat(f: float, res: float) -> float:
60 try:
61 return (int(f / res) * res)
62 except ValueError:
63 pass
64 return 0
65
66
67 def roundhexpoint(pt, bits):
68 if bits == 0:
69 return pt
70 res = []
71 for p in pt:
72 if type(p) == int:
73 res.append(p)
74 continue
75 s = p.hex()
76 m = s[s.index('.')+1:s.index('p')]
77 c = len(m) - len(m.lstrip('0'))
78 t = int(m, 16)
79 # b = 4 * len(m) - t.bit_length()
80 if bits < 4 * len(m):
81 t = t & ~((1 << (4 * len(m) - bits)) - 1)
82 n = s[0:s.index('.')+1+c]+hex(t)[2:].rstrip('L')+s[s.index('p'):]
83 r = float.fromhex(n)
84 res.append(r)
85 return res
86
87
88 def name(tt, gl, rounding):
89 if gl[0] is None:
90 x = "_adv_"
91 elif gl[0] != 0:
92 x = tt.getGlyphName(gl[0])
93 else:
94 x = "0:{:04X}".format(gl[2])
95 return (x, roundfloat(gl[1][0], rounding), roundfloat(gl[1][1], rounding))
96
97
98 def cmaplookup(tt, c):
99 cmap = tt['cmap'].getcmap(3, 1) or tt['cmap'].getcmap(1, 0)
100 if cmap:
101 return cmap.cmap.get(ord(c), '.notdef')
102 return '.notdef'
103
104
105 def makelabel(name, line, word):
106 if name:
107 if word > 1:
108 return "{} {}".format(name, word)
109 else:
110 return name
111 else:
112 return "{}.{}".format(line, word)
113
114
115 def ftostr(x, dp=6):
116 res = ("{:." + str(dp) + "f}").format(x)
117 if res.endswith("." + ("0" * dp)):
118 res = res[:-dp-1]
119 else:
120 res = re.sub(r"0*$", "", res)
121 return res
122
123
124 def scalecmp(a, b, e):
125 return abs(a - b) > e
126 # if a != 0:
127 # return (abs(1. - b / a) > e/a)
128 # else:
129 # return (abs(b) > e)
130
131
132 def cmpgls(tests, bases, epsilon):
133 if len(tests) != len(bases):
134 return True
135 for i in range(len(tests)):
136 if tests[i][0] != bases[i][0]:
137 return True
138 for j in range(1, 3):
139 if scalecmp(tests[i][j], bases[i][j], epsilon):
140 return True
141 return False
142
143
144 # Have the JSONEncoder use ftostr to render floats to 2dp rather than lots
145 json.encoder.FLOAT_REPR = ftostr
146
147
148 class JsonLog(object):
149 def __init__(self, f, fpaths, args, inputs):
150 self.out = f
151 self.opts = args
152 self.out.write("{\n")
153 self.encoder = json.JSONEncoder()
154
155 def logentry(self, label, linenumber, wordnumber, string, gglyphs, bases):
156 s = makelabel(label, linenumber, wordnumber)
157 self.out.write('\"'+s+'\": ')
158 # have to use iterencode here to get json.encoder.FLOAT_REP to work
159 # res = "\n".join(map(lambda x: "".join(
160 # self.encoder.iterencode([(g[0], roundpt(g[1], 0.01))
161 # for g in x])), gglyphs))
162 res = "".join(self.encoder.iterencode(gglyphs))
163 self.out.write(res)
164 self.out.write(', \n')
165
166 def logend(self):
167 self.out.write('"":[]}\n')
168
169
170 def LineReader(infile, spliton=None):
171 f = codecs.open(infile, encoding="utf_8")
172 for t in f.readlines():
173 s = t.strip()
174 if spliton is not None:
175 res = (None, s.split(spliton))
176 else:
177 res = (None, (s, ))
178 yield res
179
180
181 def FtmlReader(infile, spliton=None):
182 etree = parse(infile)
183 for e in etree.iter('test'):
184 t = e.get('label', "")
185 s = e.find('string')
186 if spliton is not None:
187 res = (t, s.text.split(spliton))
188 else:
189 res = (t, (s.text, ))
190 yield res
191
192
193 def feature(arg: str) -> Tuple[str, int]:
194 k, v = arg.split('=')
195 return (k.strip(), int(v.strip(), 0))
196
197
198 texttypes = {
199 'txt': LineReader,
200 'ftml': FtmlReader
201 }
202
203 parser = argparse.ArgumentParser(
204 description='''
205 Render text with a Graphite font and log the Graphite engine
206 tracing output. Optionally compare to an existing log for
207 regression testing purposes.''',
208 epilog='''
209 If the first font is above the output file in the filesystem
210 hierarchy, it may not load. On firefox, ensure that the
211 configuration option security.fileuri.strict_origin_policy is set
212 to false to allow locally loaded html files to access fonts
213 anywhere on the local filesystem. Alternatively use --copy to copy
214 the font and reference that.''')
215 parser.add_argument("font", nargs='+', type=pathlib.Path,
216 help="Fonts to render with")
217 parser.add_argument("-t", "--text", type=pathlib.Path,
218 help="text file to test each line from")
219 parser.add_argument("-o", "--output", type=pathlib.Path,
220 help="file to log results to")
221 parser.add_argument("-c", "--compare", type=pathlib.Path,
222 help="json file to compare results against")
223 parser.add_argument("-q", "--quiet", action="store_true",
224 help="Don't output position results")
225 parser.add_argument("-f", "--feat", action="append", type=feature, default={},
226 help="id=value pairs, may be repeated")
227 parser.add_argument("-l", "--lang", default=0,
228 help="language to tag text with")
229 parser.add_argument("-s", "--script", default=0, help="script of text")
230 parser.add_argument("-r", "--rtl", default=False, action="store_true",
231 help="right to left")
232 parser.add_argument("-p", "--split", action="store_const", const=' ',
233 help="Split on spaces")
234 parser.add_argument("--texttype", choices=texttypes, default='txt',
235 help="Type of text input file [default %(default)s]")
236 parser.add_argument("-e", "--error", type=float, dest='epsilon', default=0.0,
237 help="Amount of fuzz to allow in values"
238 " [default: %(default)s]")
239 parser.add_argument("-b", "--bits", type=int, default=0,
240 help="numbers compare equal if this many bits are the same"
241 " [default %(default)s]")
242 parser.add_argument("-d", "--dp", type=int, default=1,
243 help="Output numbers to this many decimal places"
244 " [default %(default)s]")
245 parser.add_argument("--graphite_library", type=pathlib.Path,
246 help="Path to graphite library instead of system version.")
247 opts = parser.parse_args()
248
249 rounding = 0.1 ** opts.dp
250 feats = dict(opts.feat)
251 reader = texttypes[opts.texttype](opts.text, opts.split)
252 outfile = (opts.output.open(mode="w", encoding='utf_8')
253 if opts.output else sys.stdout)
254 cjson = json.load(opts.compare.open()) if opts.compare else None
255 fpaths = map(partial(os.path.relpath,
256 start=(opts.output.parent if opts.output else os.curdir)),
257 opts.font)
258 if opts.bits: opts.epsilon = 0.5 ** opts.bits
259
260 # Import graphite here to allow the graphite module to see the modified
261 # environment if --graphite_library is used.
262 if opts.graphite_library:
263 os.environ['PYGRAPHITE2_LIBRARY_PATH'] = str(opts.graphite_library)
264 gr = importlib.import_module('graphite2')
265
266 font = GrFont(opts.font[0], 0, opts.rtl, feats, opts.script, opts.lang)
267 tt = TTFont(opts.font[0])
268
269 count = 0
270 errors = 0
271 log = None
272 for label, words in reader:
273 if words[0] is None: continue
274 count += 1
275 wcount = 0
276 for s in words:
277 wcount += 1
278
279 t = makelabel(label, count, wcount)
280 gls = [list(map(partial(name, tt, rounding=rounding),
281 font.glyphs(s, includewidth=True)))]
282 if gls[-1][-1][0] is None:
283 gls[-1] = ('_adv_', gls[-1][-1][1], gls[-1][-1][2])
284 if cjson is not None and cmpgls(gls[0], cjson[t][0], opts.epsilon):
285 errors += 1
286 print(t + " Failed")
287 if opts.quiet: continue
288 if log is None:
289 log = JsonLog(outfile, fpaths, opts, opts.font)
290 bases = [(cmaplookup(tt, x), (0, 0)) for x in s]
291 log.logentry(label, count, wcount, s, gls, bases)
292 if log is not None: log.logend()
293 outfile.close()
294 sys.exit(errors)