2020-02-19 12:26:55 +01:00
|
|
|
#!/usr/bin/env python3
|
2018-01-26 22:57:48 +01:00
|
|
|
|
|
|
|
# Pre-generates the expected output subset files (via fonttools) for
|
|
|
|
# specified subset test suite(s).
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2021-08-04 03:57:18 +02:00
|
|
|
import shutil
|
|
|
|
import io
|
|
|
|
import re
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
from difflib import unified_diff
|
|
|
|
from fontTools.ttLib import TTFont
|
2018-01-26 22:57:48 +01:00
|
|
|
|
|
|
|
from subprocess import check_call
|
|
|
|
from subset_test_suite import SubsetTestSuite
|
|
|
|
|
|
|
|
|
|
|
|
def usage():
|
2021-08-04 03:57:18 +02:00
|
|
|
print("Usage: generate-expected-outputs.py hb-subset <test suite file> ...")
|
|
|
|
|
|
|
|
|
|
|
|
def strip_check_sum (ttx_string):
|
|
|
|
return re.sub ('checkSumAdjustment value=["]0x([0-9a-fA-F])+["]',
|
|
|
|
'checkSumAdjustment value="0x00000000"',
|
|
|
|
ttx_string, count=1)
|
2018-01-26 22:57:48 +01:00
|
|
|
|
|
|
|
|
2021-08-04 03:57:18 +02:00
|
|
|
def generate_expected_output(input_file, unicodes, profile_flags, output_directory, font_name):
|
2021-08-12 01:47:35 +02:00
|
|
|
fonttools_path = os.path.join(tempfile.mkdtemp (), font_name)
|
2018-02-27 22:15:40 +01:00
|
|
|
args = ["fonttools", "subset", input_file]
|
2021-06-10 00:36:40 +02:00
|
|
|
args.extend(["--drop-tables+=DSIG",
|
2020-01-21 22:37:28 +01:00
|
|
|
"--drop-tables-=sbix",
|
2018-02-27 22:15:40 +01:00
|
|
|
"--unicodes=%s" % unicodes,
|
2021-08-04 03:57:18 +02:00
|
|
|
"--output-file=%s" % fonttools_path])
|
|
|
|
args.extend(profile_flags)
|
|
|
|
check_call(args)
|
|
|
|
with io.StringIO () as fp:
|
|
|
|
with TTFont (fonttools_path) as font:
|
|
|
|
font.saveXML (fp)
|
|
|
|
fonttools_ttx = strip_check_sum (fp.getvalue ())
|
|
|
|
|
|
|
|
harfbuzz_path = os.path.join(tempfile.mkdtemp (), font_name)
|
|
|
|
args = [
|
|
|
|
hb_subset,
|
|
|
|
"--font-file=" + input_file,
|
|
|
|
"--output-file=" + harfbuzz_path,
|
|
|
|
"--unicodes=%s" % unicodes,
|
|
|
|
"--drop-tables+=DSIG",
|
|
|
|
"--drop-tables-=sbix"]
|
2019-05-16 19:57:33 +02:00
|
|
|
args.extend(profile_flags)
|
2018-02-27 22:15:40 +01:00
|
|
|
check_call(args)
|
2021-08-04 03:57:18 +02:00
|
|
|
with io.StringIO () as fp:
|
|
|
|
with TTFont (harfbuzz_path) as font:
|
|
|
|
font.saveXML (fp)
|
|
|
|
harfbuzz_ttx = strip_check_sum (fp.getvalue ())
|
|
|
|
|
|
|
|
if harfbuzz_ttx != fonttools_ttx:
|
2021-08-12 01:47:35 +02:00
|
|
|
for line in unified_diff (fonttools_ttx.splitlines (1), harfbuzz_ttx.splitlines (1), fonttools_path, harfbuzz_path):
|
2021-08-04 03:57:18 +02:00
|
|
|
sys.stdout.write (line)
|
|
|
|
sys.stdout.flush ()
|
|
|
|
raise Exception ('ttx for fonttools and harfbuzz does not match.')
|
|
|
|
|
|
|
|
output_path = os.path.join(output_directory, font_name)
|
|
|
|
shutil.copy(harfbuzz_path, output_path)
|
2018-01-26 22:57:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
args = sys.argv[1:]
|
2021-08-04 03:57:18 +02:00
|
|
|
if not args:
|
|
|
|
usage()
|
|
|
|
hb_subset, args = args[0], args[1:]
|
2018-01-26 22:57:48 +01:00
|
|
|
if not args:
|
|
|
|
usage()
|
|
|
|
|
|
|
|
for path in args:
|
2020-05-28 21:41:19 +02:00
|
|
|
with open(path, mode="r", encoding="utf-8") as f:
|
2018-01-26 22:57:48 +01:00
|
|
|
test_suite = SubsetTestSuite(path, f.read())
|
|
|
|
output_directory = test_suite.get_output_directory()
|
|
|
|
|
2018-12-30 13:07:28 +01:00
|
|
|
print("Generating output files for %s" % output_directory)
|
2018-01-26 22:57:48 +01:00
|
|
|
for test in test_suite.tests():
|
|
|
|
unicodes = test.unicodes()
|
|
|
|
font_name = test.get_font_name()
|
2018-12-30 13:07:28 +01:00
|
|
|
print("Creating subset %s/%s" % (output_directory, font_name))
|
2018-02-27 22:15:40 +01:00
|
|
|
generate_expected_output(test.font_path, unicodes, test.get_profile_flags(),
|
2021-08-04 03:57:18 +02:00
|
|
|
output_directory, font_name)
|