Hash :
d5fc4a73
Author :
Date :
2022-09-14T14:43:20
[instance] add tests for featureVariations Also updated the script that is used to generate tests.With fonttools, we now do instancing first and then subsetting. With different order of subsetting and instancing operations on the same VF file, fonttools seems to generate 2 different font files with different glyph set. 1. do subsetting and then instancing: this seems result in a larger glyph set in the font file. Lookups are collected from both retained features and all possible alternate featurevariations, this leads to a larger glyph set after glyph closurei. And instancer doesn't redo glyph closure, it does lookups pruning only. 2. do instancing and then subsetting: lookups are collected from features that are replaced already and possible alternate feature variations
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
#!/usr/bin/env python3
# Pre-generates the expected output subset files (via fonttools) for
# specified subset test suite(s).
import os
import sys
import shutil
import io
import re
import tempfile
from difflib import unified_diff
from fontTools.ttLib import TTFont
from subprocess import check_call
from subset_test_suite import SubsetTestSuite
def usage():
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)
def generate_expected_output(input_file, unicodes, profile_flags, instance_flags, output_directory, font_name):
input_path = input_file
if instance_flags:
instance_path = os.path.join(tempfile.mkdtemp (), font_name)
args = ["fonttools", "varLib.instancer",
"--no-overlap-flag",
"--no-recalc-bounds",
"--no-recalc-timestamp",
"--output=%s" % instance_path,
input_file]
args.extend(instance_flags)
check_call(args)
input_path = instance_path
fonttools_path = os.path.join(tempfile.mkdtemp (), font_name)
args = ["fonttools", "subset", input_path]
args.extend(["--drop-tables+=DSIG",
"--drop-tables-=sbix",
"--unicodes=%s" % unicodes,
"--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"]
args.extend(profile_flags)
if instance_flags:
args.extend(["--instance=%s" % ','.join(instance_flags)])
check_call(args)
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:
for line in unified_diff (fonttools_ttx.splitlines (1), harfbuzz_ttx.splitlines (1), fonttools_path, harfbuzz_path):
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)
args = sys.argv[1:]
if not args:
usage()
hb_subset, args = args[0], args[1:]
if not args:
usage()
for path in args:
with open(path, mode="r", encoding="utf-8") as f:
test_suite = SubsetTestSuite(path, f.read())
output_directory = test_suite.get_output_directory()
print("Generating output files for %s" % output_directory)
for test in test_suite.tests():
unicodes = test.unicodes()
font_name = test.get_font_name()
print("Creating subset %s/%s" % (output_directory, font_name))
generate_expected_output(test.font_path, unicodes, test.get_profile_flags(),
test.get_instance_flags(), output_directory, font_name)