-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
check.py
executable file
·278 lines (250 loc) · 10.7 KB
/
check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Copyright 2016 Unicode Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import datetime
import itertools
import os
import re
import signal
import subprocess
import threading
import time
import xml.etree.ElementTree as etree
import svgutil
FONTTEST_NAMESPACE = "{https://github.com/OpenType/fonttest}"
FONTTEST_ID = FONTTEST_NAMESPACE + "id"
FONTTEST_FONT = FONTTEST_NAMESPACE + "font"
FONTTEST_RENDER = FONTTEST_NAMESPACE + "render"
FONTTEST_VARIATION = FONTTEST_NAMESPACE + "var"
class ConformanceChecker:
def __init__(self, engine):
self.engine = engine
if self.engine == "OpenType.js":
self.command = "node_modules/opentype.js/bin/test-render"
elif self.engine == "fontkit":
self.command = "src/third_party/fontkit/render"
elif self.engine == "Allsorts":
self.command = (
"src/third_party/allsorts/allsorts-tools/target/release/allsorts"
)
else:
self.command = "build/fonttest/fonttest"
self.datestr = self.make_datestr()
self.reports = {} # filename --> HTML ElementTree
self.conformance = {} # testcase -> True|False
self.observed = {} # testcase --> SVG ElementTree
def get_version(self):
if self.engine in {"CoreText", "FreeStack", "TehreerStack", "Allsorts"}:
return subprocess.check_output(
[self.command, "--version", "--engine=" + self.engine]
).decode("utf-8")
if self.engine in ("OpenType.js", "fontkit"):
npm_version = subprocess.check_output(["npm", "--version"]).decode("utf-8")
node_version = subprocess.check_output(["node", "--version"])
node_version = node_version.decode("utf-8").replace("v", "")
engine_version = subprocess.check_output(
["npm", "info", self.engine.lower(), "version"]
).decode("utf-8")
return "%s/%s NPM/%s Node/%s" % (
self.engine,
engine_version,
npm_version,
node_version,
)
def make_datestr(self):
now = datetime.datetime.now()
return "%s %d, %d" % (time.strftime("%B"), now.day, now.year)
def make_command(self, e):
testcase = e.attrib[FONTTEST_ID]
font = os.path.join("fonts", e.attrib[FONTTEST_FONT])
render = e.attrib.get(FONTTEST_RENDER)
variation = e.attrib.get(FONTTEST_VARIATION)
command = [
self.command,
"--font=" + font,
"--testcase=" + testcase,
"--engine=" + self.engine,
]
if render:
command.append("--render=" + render)
if variation:
command.append("--variation=" + variation)
return command
def check(self, testfile):
doc = etree.parse(testfile).getroot()
self.reports[testfile] = doc
for e in doc.findall(".//*[@class='expected']"):
testcase = e.attrib[FONTTEST_ID]
ok, observed = self.render(e)
if ok:
expected_svg = e.find("svg")
self.normalize_svg(expected_svg)
ok = svgutil.is_similar(expected_svg, observed, maxDelta=1.0)
self.add_prefix_to_svg_ids(observed, "OBSERVED")
self.observed[testcase] = observed
self.conformance[testcase] = ok
print("%s %s" % ("PASS" if ok else "FAIL", testcase))
for e in doc.findall(".//*[@class='expected-no-crash']"):
testcase = e.attrib[FONTTEST_ID]
ok, observed = self.render(e)
self.add_prefix_to_svg_ids(observed, "OBSERVED")
self.observed[testcase] = observed
self.conformance[testcase] = ok
print("%s %s" % ("PASS" if ok else "FAIL", testcase))
for testcase, ok in list(self.conformance.items()):
groups = testcase.split("/")
for i in range(len(groups)):
group = "/".join(groups[:i])
self.conformance[group] = ok and self.conformance.get(group, True)
def render(self, e):
command = self.make_command(e)
status, observed, _stderr = run_command(command, timeout_sec=3)
observed = observed.decode("utf-8")
if status == 0:
observed = re.sub(r">\s+<", "><", observed)
observed = observed.replace('xmlns="http://www.w3.org/2000/svg"', "")
observed_svg = etree.fromstring(observed)
self.normalize_svg(observed_svg)
return (True, observed_svg)
else:
return (False, etree.fromstring("<div>⁓</div>"))
def normalize_svg(self, svg):
strip_path = lambda p: re.sub(r"\s+", " ", p).strip()
for path in svg.findall(".//path[@d]"):
path.attrib["d"] = strip_path(path.attrib["d"])
def add_prefix_to_svg_ids(self, svg, prefix):
# The 'id' attribute needs to be globally unique in the HTML document,
# so we add a prefix to distinguish identifiers in the expected versus
# observed SVG image.
for symbol in svg.findall(".//symbol[@id]"):
symbol.attrib["id"] = "%s/%s" % (prefix, symbol.attrib["id"])
href = "{http://www.w3.org/1999/xlink}href"
for use in svg.findall(".//use[@%s]" % href):
assert use.attrib[href][0] == "#", use.attrib[href]
use.attrib[href] = "#%s/%s" % (prefix, use.attrib[href][1:])
def prettify_version_string(self, version):
libs = [x.replace("/", " ") for x in version.split()]
if len(libs) <= 2:
return " and ".join(libs)
else:
return ", ".join(libs[:-1]) + " and " + libs[-1]
def write_report(self, path):
report = etree.parse("testcases/index.html").getroot()
report.find("./body//*[@id='Engine']").text = self.engine
report.find("./body//*[@id='Date']").text = self.datestr
report.find(
"./body//*[@id='EngineVersion']"
).text = self.prettify_version_string(self.get_version())
summary = report.find("./body//*[@id='SummaryText']")
fails = [k for k, v in self.conformance.items() if k and not v]
fails = sorted(set([t.split("/")[0] for t in fails]), key=sortkey)
if len(fails) == 0:
summary.text = "All tests have passed."
else:
summary.text = "Some tests have failed. For details, see "
for f in fails:
if f is not fails[0]:
if f is fails[-1]:
etree.SubElement(summary, None).text = ", and "
else:
etree.SubElement(summary, None).text = ", "
link = etree.SubElement(summary, "a")
link.text, link.attrib["href"] = f, "#" + f
etree.SubElement(summary, None).text = "."
head = report.find("./head")
for sheet in list(head.findall("./link[@rel='stylesheet']")):
href = sheet.attrib.get("href")
if href and "://" not in href:
internalStyle = etree.SubElement(head, "style")
with open(os.path.join("testcases", href), "r") as sheetfile:
internalStyle.text = sheetfile.read()
head.remove(sheet)
for filename in sorted(self.reports.keys(), key=sortkey):
doc = self.reports[filename]
for e in doc.findall(".//*[@class='observed']"):
e.append(self.observed.get(e.attrib[FONTTEST_ID]))
for e in doc.findall(".//*[@class='conformance']"):
if self.conformance.get(e.attrib[FONTTEST_ID]):
e.text, e.attrib["class"] = "✓", "conformance-pass"
else:
e.text, e.attrib["class"] = "✖", "conformance-fail"
for subElement in doc.find("body"):
report.find("body").append(subElement)
with open(path, "wb") as outfile:
xml = etree.tostring(report, encoding="utf-8")
xml = xml.replace(b"svg:", b"") # work around browser bugs
outfile.write(xml)
def sortkey(s):
"""'tests/GVAR-10B.html' --> 'tests/GVAR-0000000010B.html'"""
return re.sub(r"\d+", lambda match: "%09d" % int(match.group(0)), s)
def run_command(cmd, timeout_sec):
child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
timer = threading.Timer(timeout_sec, child.kill)
try:
timer.start()
stdout, stderr = child.communicate()
finally:
timer.cancel()
return child.returncode, stdout, stderr
def build(engine):
if engine == "OpenType.js" or engine == "fontkit":
subprocess.check_call(["npm", "install"])
elif engine == "Allsorts":
subprocess.check_call(
[
"cargo",
"build",
"--release",
"--manifest-path",
"src/third_party/allsorts/allsorts-tools/Cargo.toml",
]
)
else:
if not os.path.exists("build"):
os.mkdir("build")
subprocess.check_call(["cmake", "-GNinja", "../src"], cwd="build")
subprocess.check_call(["ninja", "-C", "build"])
def main():
etree.register_namespace("svg", "http://www.w3.org/2000/svg")
etree.register_namespace("xlink", "http://www.w3.org/1999/xlink")
parser = argparse.ArgumentParser()
parser.add_argument(
"--engine",
choices=[
"FreeStack",
"TehreerStack",
"CoreText",
"DirectWrite",
"OpenType.js",
"fontkit",
"Allsorts",
],
default="FreeStack",
)
parser.add_argument("--output", help="path to report file being written")
args = parser.parse_args()
build(engine=args.engine)
checker = ConformanceChecker(engine=args.engine)
for filename in sorted(os.listdir("testcases"), key=sortkey):
if filename == "index.html" or not filename.endswith(".html"):
continue
checker.check(os.path.join("testcases", filename))
print("PASS" if checker.conformance.get("") else "FAIL")
if args.output:
checker.write_report(args.output)
if __name__ == "__main__":
main()