Skip to content

Commit

Permalink
Require fonttools >= 4.52.0
Browse files Browse the repository at this point in the history
It has the CFF to CFF2 fixes.
  • Loading branch information
khaledhosny committed Oct 16, 2024
1 parent f48551f commit 41df6c1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 62 deletions.
64 changes: 3 additions & 61 deletions Lib/ufo2ft/postProcessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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

from ufo2ft.constants import (
GLYPHS_DONT_USE_PRODUCTION_NAMES,
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,25 +318,6 @@ def _get_cff_version(otf):
else:
return None

@staticmethod
def _convert_cff_to_cff2(otf):
logger.info("Converting CFF table to CFF2")

try:
from fontTools.cffLib.CFFToCFF2 import convertCFFToCFF2
except ImportError:
from fontTools.varLib.cff import convertCFFtoCFF2 as convertCFFToCFF2

# 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 @@ -378,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

0 comments on commit 41df6c1

Please sign in to comment.