Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[postProcessor] fontTools.varLib.cff.convertCFFtoCFF2 is deprecated #882

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 3 additions & 58 deletions Lib/ufo2ft/postProcessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
from io import BytesIO

from fontTools.cffLib.CFFToCFF2 import convertCFFToCFF2
from fontTools.ttLib import TTFont
from fontTools.ttLib.standardGlyphOrder import standardGlyphOrder

Expand Down Expand Up @@ -134,7 +135,8 @@ def process_cff(self, *, optimizeCFF=True, cffVersion=None, subroutinizer=None):
cffInputVersion == CFFVersion.CFF
and cffOutputVersion == CFFVersion.CFF2
):
self._convert_cff_to_cff2(self.otf)
logger.info("Converting CFF table to CFF2")
convertCFFToCFF2(self.otf)
else:
raise NotImplementedError(
"Unsupported CFF conversion {cffInputVersion} => {cffOutputVersion}"
Expand Down Expand Up @@ -316,22 +318,6 @@ def _get_cff_version(otf):
else:
return None

@staticmethod
def _convert_cff_to_cff2(otf):
from fontTools.varLib.cff import convertCFFtoCFF2

logger.info("Converting CFF table to CFF2")

# convertCFFtoCFF2 doesn't strip T2CharStrings' widths, so we do it ourselves
# https://github.com/fonttools/fonttools/issues/1835
charstrings = otf["CFF "].cff[0].CharStrings
for glyph_name in otf.getGlyphOrder():
cs = charstrings[glyph_name]
cs.decompile()
cs.program = _stripCharStringWidth(cs.program)

convertCFFtoCFF2(otf)

@classmethod
def _subroutinize(cls, backend, otf, cffVersion):
subroutinize = getattr(cls, f"_subroutinize_with_{backend.value}")
Expand Down Expand Up @@ -375,47 +361,6 @@ def apply_fontinfo(self):
compiler.compile()


# Adapted from fontTools.cff.specializer.programToCommands
# https://github.com/fonttools/fonttools/blob/babca16
# /Lib/fontTools/cffLib/specializer.py#L40-L122
# When converting from CFF to CFF2 we need to drop the charstrings' widths.
# This function returns a new charstring program without the initial width value.
# TODO: Move to fontTools?
def _stripCharStringWidth(program):
seenWidthOp = False
result = []
stack = []
for token in program:
if not isinstance(token, str):
stack.append(token)
continue

if (not seenWidthOp) and token in {
"hstem",
"hstemhm",
"vstem",
"vstemhm",
"cntrmask",
"hintmask",
"hmoveto",
"vmoveto",
"rmoveto",
"endchar",
}:
seenWidthOp = True
parity = token in {"hmoveto", "vmoveto"}
numArgs = len(stack)
if numArgs and (numArgs % 2) ^ parity:
stack.pop(0) # pop width

result.extend(stack)
result.append(token)
stack = []
if stack:
result.extend(stack)
return result


def _reloadFont(font: TTFont) -> TTFont:
"""Recompile a font to arrive at the final internal layout."""
stream = BytesIO()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
setup_requires=pytest_runner + wheel + ["setuptools_scm"],
tests_require=["pytest>=2.8"],
install_requires=[
"fonttools[ufo]>=4.50.0",
"fonttools[ufo]>=4.52.0",
"cffsubr>=0.3.0",
"booleanOperations>=0.9.0",
"fontMath>=0.9.3",
Expand Down