[cffLib] Add CFF<->CFF2 convertors that work on otFont

This commit is contained in:
Behdad Esfahbod 2024-05-16 16:27:31 -07:00
parent b009b614f3
commit 0a7433f847
7 changed files with 35 additions and 41 deletions

View File

@ -14,7 +14,7 @@ __all__ = ["convertCFF2ToCFF", "main"]
log = logging.getLogger("fontTools.cffLib") log = logging.getLogger("fontTools.cffLib")
def convertCFF2ToCFF(cff, otFont): def _convertCFF2ToCFF(cff, otFont):
"""Converts this object from CFF2 format to CFF format. This conversion """Converts this object from CFF2 format to CFF format. This conversion
is done 'in-place'. The conversion cannot be reversed. is done 'in-place'. The conversion cannot be reversed.
@ -79,6 +79,14 @@ def convertCFF2ToCFF(cff, otFont):
cs.program.insert(0, width - private.nominalWidthX) 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): def main(args=None):
"""Convert CFF OTF font to CFF2 OTF font""" """Convert CFF OTF font to CFF2 OTF font"""
if args is None: if args is None:
@ -136,13 +144,8 @@ def main(args=None):
) )
font = TTFont(infile, recalcTimestamp=options.recalc_timestamp, recalcBBoxes=False) font = TTFont(infile, recalcTimestamp=options.recalc_timestamp, recalcBBoxes=False)
cff = font["CFF2"].cff
cff.convertCFF2ToCFF(font) convertCFF2ToCFF(font)
del font["CFF2"]
table = font["CFF "] = newTable("CFF ")
table.cff = cff
log.info( log.info(
"Saving %s", "Saving %s",

View File

@ -21,7 +21,7 @@ __all__ = ["convertCFFToCFF2", "main"]
log = logging.getLogger("fontTools.cffLib") log = logging.getLogger("fontTools.cffLib")
def convertCFFToCFF2(cff, otFont): def _convertCFFToCFF2(cff, otFont):
"""Converts this object from CFF format to CFF2 format. This conversion """Converts this object from CFF format to CFF2 format. This conversion
is done 'in-place'. The conversion cannot be reversed. is done 'in-place'. The conversion cannot be reversed.
@ -113,6 +113,14 @@ def convertCFFToCFF2(cff, otFont):
cff.decompile(file, otFont, isCFF2=True) 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): def main(args=None):
"""Convert CFF OTF font to CFF2 OTF font""" """Convert CFF OTF font to CFF2 OTF font"""
if args is None: if args is None:
@ -170,13 +178,8 @@ def main(args=None):
) )
font = TTFont(infile, recalcTimestamp=options.recalc_timestamp, recalcBBoxes=False) font = TTFont(infile, recalcTimestamp=options.recalc_timestamp, recalcBBoxes=False)
cff = font["CFF "].cff
del font["CFF "]
cff.convertCFFToCFF2(font) convertCFFToCFF2(font)
table = font["CFF2"] = newTable("CFF2")
table.cff = cff
log.info( log.info(
"Saving %s", "Saving %s",

View File

@ -386,14 +386,14 @@ class CFFFontSet(object):
self.minor = int(attrs["value"]) self.minor = int(attrs["value"])
def convertCFFToCFF2(self, otFont): def convertCFFToCFF2(self, otFont):
from .CFFToCFF2 import convertCFFToCFF2 from .CFFToCFF2 import _convertCFFToCFF2
convertCFFToCFF2(self, otFont) _convertCFFToCFF2(self, otFont)
def convertCFF2ToCFF(self, otFont): def convertCFF2ToCFF(self, otFont):
from .CFF2ToCFF import convertCFF2ToCFF from .CFF2ToCFF import _convertCFF2ToCFF
convertCFF2ToCFF(self, otFont) _convertCFF2ToCFF(self, otFont)
def desubroutinize(self): def desubroutinize(self):
for fontName in self.fontNames: for fontName in self.fontNames:

View File

@ -845,9 +845,10 @@ def _add_CFF2(varFont, model, master_fonts):
glyphOrder = varFont.getGlyphOrder() glyphOrder = varFont.getGlyphOrder()
if "CFF2" not in varFont: 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) ordered_fonts_list = model.reorderMasters(master_fonts, model.reverseMapping)
# re-ordering the master list simplifies building the CFF2 data item lists. # re-ordering the master list simplifies building the CFF2 data item lists.
merge_region_fonts(varFont, model, ordered_fonts_list, glyphOrder) merge_region_fonts(varFont, model, ordered_fonts_list, glyphOrder)

View File

@ -13,7 +13,6 @@ from fontTools.cffLib import (
) )
from io import BytesIO from io import BytesIO
from fontTools.cffLib.specializer import specializeCommands, commandsToProgram from fontTools.cffLib.specializer import specializeCommands, commandsToProgram
from fontTools.cffLib.CFFToCFF2 import convertCFFToCFF2 as lib_convertCFFToCFF2
from fontTools.ttLib import newTable from fontTools.ttLib import newTable
from fontTools import varLib from fontTools import varLib
from fontTools.varLib.models import allEqual from fontTools.varLib.models import allEqual
@ -50,16 +49,6 @@ def addCFFVarStore(varFont, varModel, varDataList, masterSupports):
fontDict.Private.vstore = topDict.VarStore 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): def conv_to_int(num):
if isinstance(num, float) and num.is_integer(): if isinstance(num, float) and num.is_integer():
return int(num) return int(num)

View File

@ -601,10 +601,9 @@ def instantiateCFF2(
varStore = topDict.VarStore.otVarStore varStore = topDict.VarStore.otVarStore
if not varStore: if not varStore:
if downgrade: if downgrade:
table = varfont["CFF "] = newTable("CFF ") from fontTools.cffLib.CFF2ToCFF import convertCFF2ToCFF
table.cff = cff
cff.convertCFF2ToCFF(varfont) convertCFF2ToCFF(varfont)
del varfont["CFF2"]
return return
cff.desubroutinize() cff.desubroutinize()
@ -821,10 +820,9 @@ def instantiateCFF2(
del private.vstore del private.vstore
if downgrade: if downgrade:
table = varfont["CFF "] = newTable("CFF ") from fontTools.cffLib.CFF2ToCFF import convertCFF2ToCFF
table.cff = cff
cff.convertCFF2ToCFF(varfont) convertCFF2ToCFF(varfont)
del varfont["CFF2"]
def _instantiateGvarGlyph( def _instantiateGvarGlyph(

View File

@ -330,9 +330,9 @@ def test_build_cff_to_cff2(tmpdir):
} }
fb.setupCFF("TestFont", {}, charStrings, {}) 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(): def test_setupNameTable_no_mac():