diff --git a/Lib/fontTools/cffLib/CFF2ToCFF.py b/Lib/fontTools/cffLib/CFF2ToCFF.py index d500a44dc..a712b2f60 100644 --- a/Lib/fontTools/cffLib/CFF2ToCFF.py +++ b/Lib/fontTools/cffLib/CFF2ToCFF.py @@ -14,7 +14,7 @@ __all__ = ["convertCFF2ToCFF", "main"] log = logging.getLogger("fontTools.cffLib") -def convertCFF2ToCFF(cff, otFont): +def _convertCFF2ToCFF(cff, otFont): """Converts this object from CFF2 format to CFF format. This conversion is done 'in-place'. The conversion cannot be reversed. @@ -79,6 +79,14 @@ def convertCFF2ToCFF(cff, otFont): cs.program.insert(0, width - private.nominalWidthX) +def convertCFF2ToCFF(font): + cff = font["CFF2"].cff + _convertCFF2ToCFF(cff, font) + del font["CFF2"] + table = font["CFF "] = newTable("CFF ") + table.cff = cff + + def main(args=None): """Convert CFF OTF font to CFF2 OTF font""" if args is None: @@ -136,13 +144,8 @@ def main(args=None): ) font = TTFont(infile, recalcTimestamp=options.recalc_timestamp, recalcBBoxes=False) - cff = font["CFF2"].cff - cff.convertCFF2ToCFF(font) - - del font["CFF2"] - table = font["CFF "] = newTable("CFF ") - table.cff = cff + convertCFF2ToCFF(font) log.info( "Saving %s", diff --git a/Lib/fontTools/cffLib/CFFToCFF2.py b/Lib/fontTools/cffLib/CFFToCFF2.py index ef88b2fea..6d2dafdc4 100644 --- a/Lib/fontTools/cffLib/CFFToCFF2.py +++ b/Lib/fontTools/cffLib/CFFToCFF2.py @@ -21,7 +21,7 @@ __all__ = ["convertCFFToCFF2", "main"] log = logging.getLogger("fontTools.cffLib") -def convertCFFToCFF2(cff, otFont): +def _convertCFFToCFF2(cff, otFont): """Converts this object from CFF format to CFF2 format. This conversion is done 'in-place'. The conversion cannot be reversed. @@ -113,6 +113,14 @@ def convertCFFToCFF2(cff, otFont): cff.decompile(file, otFont, isCFF2=True) +def convertCFFToCFF2(font): + cff = font["CFF "].cff + del font["CFF "] + _convertCFFToCFF2(cff, font) + table = font["CFF2"] = newTable("CFF2") + table.cff = cff + + def main(args=None): """Convert CFF OTF font to CFF2 OTF font""" if args is None: @@ -170,13 +178,8 @@ def main(args=None): ) font = TTFont(infile, recalcTimestamp=options.recalc_timestamp, recalcBBoxes=False) - cff = font["CFF "].cff - del font["CFF "] - cff.convertCFFToCFF2(font) - - table = font["CFF2"] = newTable("CFF2") - table.cff = cff + convertCFFToCFF2(font) log.info( "Saving %s", diff --git a/Lib/fontTools/cffLib/__init__.py b/Lib/fontTools/cffLib/__init__.py index 89f4860ec..bd954f515 100644 --- a/Lib/fontTools/cffLib/__init__.py +++ b/Lib/fontTools/cffLib/__init__.py @@ -386,14 +386,14 @@ class CFFFontSet(object): self.minor = int(attrs["value"]) def convertCFFToCFF2(self, otFont): - from .CFFToCFF2 import convertCFFToCFF2 + from .CFFToCFF2 import _convertCFFToCFF2 - convertCFFToCFF2(self, otFont) + _convertCFFToCFF2(self, otFont) def convertCFF2ToCFF(self, otFont): - from .CFF2ToCFF import convertCFF2ToCFF + from .CFF2ToCFF import _convertCFF2ToCFF - convertCFF2ToCFF(self, otFont) + _convertCFF2ToCFF(self, otFont) def desubroutinize(self): for fontName in self.fontNames: diff --git a/Lib/fontTools/varLib/__init__.py b/Lib/fontTools/varLib/__init__.py index 1e0f2ec2f..6d0e00ee1 100644 --- a/Lib/fontTools/varLib/__init__.py +++ b/Lib/fontTools/varLib/__init__.py @@ -845,9 +845,10 @@ def _add_CFF2(varFont, model, master_fonts): glyphOrder = varFont.getGlyphOrder() if "CFF2" not in varFont: - from .cff import convertCFFtoCFF2 + from fontTools.cffLib.CFFToCFF2 import convertCFFToCFF2 + + convertCFFToCFF2(varFont) - convertCFFtoCFF2(varFont) ordered_fonts_list = model.reorderMasters(master_fonts, model.reverseMapping) # re-ordering the master list simplifies building the CFF2 data item lists. merge_region_fonts(varFont, model, ordered_fonts_list, glyphOrder) diff --git a/Lib/fontTools/varLib/cff.py b/Lib/fontTools/varLib/cff.py index 989a5a60b..393c793e3 100644 --- a/Lib/fontTools/varLib/cff.py +++ b/Lib/fontTools/varLib/cff.py @@ -13,7 +13,6 @@ from fontTools.cffLib import ( ) from io import BytesIO from fontTools.cffLib.specializer import specializeCommands, commandsToProgram -from fontTools.cffLib.CFFToCFF2 import convertCFFToCFF2 as lib_convertCFFToCFF2 from fontTools.ttLib import newTable from fontTools import varLib from fontTools.varLib.models import allEqual @@ -50,16 +49,6 @@ def addCFFVarStore(varFont, varModel, varDataList, masterSupports): fontDict.Private.vstore = topDict.VarStore -def convertCFFtoCFF2(varFont): - # Convert base font to a single master CFF2 font. - cffTable = varFont["CFF "] - lib_convertCFFToCFF2(cffTable.cff, varFont) - newCFF2 = newTable("CFF2") - newCFF2.cff = cffTable.cff - varFont["CFF2"] = newCFF2 - del varFont["CFF "] - - def conv_to_int(num): if isinstance(num, float) and num.is_integer(): return int(num) diff --git a/Lib/fontTools/varLib/instancer/__init__.py b/Lib/fontTools/varLib/instancer/__init__.py index 2d61124de..703d29d28 100644 --- a/Lib/fontTools/varLib/instancer/__init__.py +++ b/Lib/fontTools/varLib/instancer/__init__.py @@ -601,10 +601,9 @@ def instantiateCFF2( varStore = topDict.VarStore.otVarStore if not varStore: if downgrade: - table = varfont["CFF "] = newTable("CFF ") - table.cff = cff - cff.convertCFF2ToCFF(varfont) - del varfont["CFF2"] + from fontTools.cffLib.CFF2ToCFF import convertCFF2ToCFF + + convertCFF2ToCFF(varfont) return cff.desubroutinize() @@ -821,10 +820,9 @@ def instantiateCFF2( del private.vstore if downgrade: - table = varfont["CFF "] = newTable("CFF ") - table.cff = cff - cff.convertCFF2ToCFF(varfont) - del varfont["CFF2"] + from fontTools.cffLib.CFF2ToCFF import convertCFF2ToCFF + + convertCFF2ToCFF(varfont) def _instantiateGvarGlyph( diff --git a/Tests/fontBuilder/fontBuilder_test.py b/Tests/fontBuilder/fontBuilder_test.py index c831d02e8..0cbf5d0ce 100644 --- a/Tests/fontBuilder/fontBuilder_test.py +++ b/Tests/fontBuilder/fontBuilder_test.py @@ -330,9 +330,9 @@ def test_build_cff_to_cff2(tmpdir): } fb.setupCFF("TestFont", {}, charStrings, {}) - from fontTools.varLib.cff import convertCFFtoCFF2 + from fontTools.cffLib.CFFToCFF2 import convertCFFToCFF2 - convertCFFtoCFF2(fb.font) + convertCFFToCFF2(fb.font) def test_setupNameTable_no_mac():