From 09cb94dfa772cc2503b9c9021c80202b59f76682 Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Wed, 16 Oct 2024 11:57:10 +0300 Subject: [PATCH] Require fonttools >= 4.52.0 It has the CFF to CFF2 fixes. --- Lib/ufo2ft/postProcessor.py | 64 ++----------------------------------- setup.py | 2 +- 2 files changed, 4 insertions(+), 62 deletions(-) diff --git a/Lib/ufo2ft/postProcessor.py b/Lib/ufo2ft/postProcessor.py index 0eeb8661..4e2bcbd4 100644 --- a/Lib/ufo2ft/postProcessor.py +++ b/Lib/ufo2ft/postProcessor.py @@ -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 @@ -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}" @@ -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}") @@ -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() diff --git a/setup.py b/setup.py index 74b5fdab..be01e4cc 100644 --- a/setup.py +++ b/setup.py @@ -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",