[CFFToCFF2] Fix for non-FDArray fonts and subroutines

This commit is contained in:
Behdad Esfahbod 2024-05-24 14:44:11 -06:00
parent 369a21353a
commit 6a812ce925

View File

@ -43,7 +43,15 @@ def _convertCFFToCFF2(cff, otFont):
fdArray = topDict.FDArray if hasattr(topDict, "FDArray") else None fdArray = topDict.FDArray if hasattr(topDict, "FDArray") else None
charStrings = topDict.CharStrings charStrings = topDict.CharStrings
globalSubrs = cff.GlobalSubrs globalSubrs = cff.GlobalSubrs
localSubrs = [getattr(fd.Private, "Subrs", []) for fd in fdArray] if fdArray else [] localSubrs = (
[getattr(fd.Private, "Subrs", []) for fd in fdArray]
if fdArray
else (
[topDict.Private.Subrs]
if hasattr(topDict, "Private") and hasattr(topDict.Private, "Subrs")
else []
)
)
for glyphName in charStrings.keys(): for glyphName in charStrings.keys():
cs, fdIndex = charStrings.getItemAndSelector(glyphName) cs, fdIndex = charStrings.getItemAndSelector(glyphName)
@ -70,13 +78,21 @@ def _convertCFFToCFF2(cff, otFont):
for glyphName in charStrings.keys(): for glyphName in charStrings.keys():
cs, fdIndex = charStrings.getItemAndSelector(glyphName) cs, fdIndex = charStrings.getItemAndSelector(glyphName)
program = cs.program program = cs.program
if fdIndex == None:
fdIndex = 0 thisLocalSubrs = (
localSubrs[fdIndex]
if fdIndex
else (
getattr(topDict.Private, "Subrs", [])
if hasattr(topDict, "Private")
else []
)
)
# Intentionally use custom type for nominalWidthX, such that any # Intentionally use custom type for nominalWidthX, such that any
# CharString that has an explicit width encoded will throw back to us. # CharString that has an explicit width encoded will throw back to us.
extractor = T2WidthExtractor( extractor = T2WidthExtractor(
localSubrs[fdIndex] if localSubrs else [], thisLocalSubrs,
globalSubrs, globalSubrs,
nominalWidthXError, nominalWidthXError,
0, 0,
@ -94,7 +110,7 @@ def _convertCFFToCFF2(cff, otFont):
op = program.pop(0) op = program.pop(0)
bias = extractor.localBias if op == "callsubr" else extractor.globalBias bias = extractor.localBias if op == "callsubr" else extractor.globalBias
subrNumber += bias subrNumber += bias
subrSet = localSubrs[fdIndex] if op == "callsubr" else globalSubrs subrSet = thisLocalSubrs if op == "callsubr" else globalSubrs
subrProgram = subrSet[subrNumber].program subrProgram = subrSet[subrNumber].program
program[:0] = subrProgram program[:0] = subrProgram
# Now pop the actual width # Now pop the actual width