[subset] Compute xAvgCharWidth even when --no-prune-unicode-ranges is given

This commit is contained in:
Jany Belluz 2023-03-01 16:28:51 +00:00
parent d4c5eac780
commit 760c7376fe
2 changed files with 36 additions and 9 deletions

View File

@ -3081,7 +3081,6 @@ class Options(object):
) )
def __init__(self, **kwargs): def __init__(self, **kwargs):
self.drop_tables = self._drop_tables_default[:] self.drop_tables = self._drop_tables_default[:]
self.no_subset_tables = self._no_subset_tables_default[:] self.no_subset_tables = self._no_subset_tables_default[:]
self.passthrough_tables = False # keep/drop tables we can't subset self.passthrough_tables = False # keep/drop tables we can't subset
@ -3226,7 +3225,6 @@ class Subsetter(object):
pass pass
def __init__(self, options=None): def __init__(self, options=None):
if not options: if not options:
options = Options() options = Options()
@ -3275,7 +3273,6 @@ class Subsetter(object):
log.info("%s pruned", tag) log.info("%s pruned", tag)
def _closure_glyphs(self, font): def _closure_glyphs(self, font):
realGlyphs = set(font.getGlyphOrder()) realGlyphs = set(font.getGlyphOrder())
self.orig_glyph_order = glyph_order = font.getGlyphOrder() self.orig_glyph_order = glyph_order = font.getGlyphOrder()
@ -3465,11 +3462,14 @@ class Subsetter(object):
for tag in font.keys(): for tag in font.keys():
if tag == "GlyphOrder": if tag == "GlyphOrder":
continue continue
if tag == "OS/2" and self.options.prune_unicode_ranges: if tag == "OS/2":
old_uniranges = font[tag].getUnicodeRanges() if self.options.prune_unicode_ranges:
new_uniranges = font[tag].recalcUnicodeRanges(font, pruneOnly=True) old_uniranges = font[tag].getUnicodeRanges()
if old_uniranges != new_uniranges: new_uniranges = font[tag].recalcUnicodeRanges(font, pruneOnly=True)
log.info("%s Unicode ranges pruned: %s", tag, sorted(new_uniranges)) if old_uniranges != new_uniranges:
log.info(
"%s Unicode ranges pruned: %s", tag, sorted(new_uniranges)
)
if self.options.recalc_average_width: if self.options.recalc_average_width:
old_avg_width = font[tag].xAvgCharWidth old_avg_width = font[tag].xAvgCharWidth
new_avg_width = font[tag].recalcAvgCharWidth(font) new_avg_width = font[tag].recalcAvgCharWidth(font)
@ -3506,7 +3506,6 @@ class Subsetter(object):
@timer("load font") @timer("load font")
def load_font(fontFile, options, checkChecksums=0, dontLoadGlyphNames=False, lazy=True): def load_font(fontFile, options, checkChecksums=0, dontLoadGlyphNames=False, lazy=True):
font = ttLib.TTFont( font = ttLib.TTFont(
fontFile, fontFile,
checkChecksums=checkChecksums, checkChecksums=checkChecksums,

View File

@ -1859,5 +1859,33 @@ def test_subset_COLR_glyph_closure(tmp_path):
assert "grave" not in color_layers assert "grave" not in color_layers
def test_subset_recalc_xAvgCharWidth(ttf_path):
# Note that the font in in the *ttLib*/data/TestTTF-Regular.ttx file,
# not this subset/data folder.
font = TTFont(ttf_path)
xAvgCharWidth_before = font["OS/2"].xAvgCharWidth
subset_path = ttf_path.with_suffix(".subset.ttf")
subset.main(
[
str(ttf_path),
f"--output-file={subset_path}",
# Keep only the ellipsis, which is very wide, that ought to bump up the average
"--glyphs=ellipsis",
"--recalc-average-width",
"--no-prune-unicode-ranges",
]
)
subset_font = TTFont(subset_path)
xAvgCharWidth_after = subset_font["OS/2"].xAvgCharWidth
# Check that the value gets updated
assert xAvgCharWidth_after != xAvgCharWidth_before
# Check that the value gets updated to the actual new value
subset_font["OS/2"].recalcAvgCharWidth(subset_font)
assert xAvgCharWidth_after == subset_font["OS/2"].xAvgCharWidth
if __name__ == "__main__": if __name__ == "__main__":
sys.exit(unittest.main()) sys.exit(unittest.main())