Merge pull request #3518 from fonttools/cff-to-cff2-remove-unused-subroutines

[CFFToCFF2] Remove unused subroutines if necessary
This commit is contained in:
Cosimo Lupo 2024-05-24 16:35:47 +01:00 committed by GitHub
commit 54bc2a5a8c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 9 deletions

View File

@ -65,6 +65,7 @@ def _convertCFFToCFF2(cff, otFont):
program[min(i, j) :] = []
# Clean up glyph charstrings
removeUnusedSubrs = False
nominalWidthXError = _NominalWidthUsedError()
for glyphName in charStrings.keys():
cs, fdIndex = charStrings.getItemAndSelector(glyphName)
@ -86,10 +87,9 @@ def _convertCFFToCFF2(cff, otFont):
# Program has explicit width. We want to drop it, but can't
# just pop the first number since it may be a subroutine call.
# Instead, when seeing that, we embed the subroutine and recurse.
# This has the problem that some subroutines might become unused.
# We don't currently prune those. Subset module has code for this
# kind of stuff, possibly plug it in here if pruning becomes needed.
# If this ever happened, we later prune unused subroutines.
while program[1] in ["callsubr", "callgsubr"]:
removeUnusedSubrs = True
subrNumber = program.pop(0)
op = program.pop(0)
bias = extractor.localBias if op == "callsubr" else extractor.globalBias
@ -103,6 +103,9 @@ def _convertCFFToCFF2(cff, otFont):
if program and program[-1] == "endchar":
program.pop()
if removeUnusedSubrs:
cff.remove_unused_subroutines()
# Upconvert TopDict
cff.major = 2

View File

@ -362,8 +362,7 @@ def remove_hints(cff):
# we have seen any non-hint operators so far and do the right
# thing, recursively... Good luck understanding that :(
css = set()
for g in font.charset:
c, _ = cs.getItemAndSelector(g)
for c in cs.values():
c.decompile()
subrs = getattr(c.private, "Subrs", [])
decompiler = _DehintingT2Decompiler(
@ -422,8 +421,7 @@ def remove_unused_subroutines(cff):
# Renumber subroutines to remove unused ones
# Mark all used subroutines
for g in font.charset:
c, _ = cs.getItemAndSelector(g)
for c in cs.values():
subrs = getattr(c.private, "Subrs", [])
decompiler = _MarkingT2Decompiler(subrs, c.globalSubrs, c.private)
decompiler.execute(c)
@ -449,8 +447,7 @@ def remove_unused_subroutines(cff):
subrs._new_bias = calcSubrBias(subrs._used)
# Renumber glyph charstrings
for g in font.charset:
c, _ = cs.getItemAndSelector(g)
for c in cs.values():
subrs = getattr(c.private, "Subrs", None)
_cs_subset_subroutines(c, subrs, font.GlobalSubrs)