From 6a812ce925563f1dcebc13258dba9afbd706669c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 May 2024 14:44:11 -0600 Subject: [PATCH] [CFFToCFF2] Fix for non-FDArray fonts and subroutines --- Lib/fontTools/cffLib/CFFToCFF2.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/Lib/fontTools/cffLib/CFFToCFF2.py b/Lib/fontTools/cffLib/CFFToCFF2.py index 78347c666..37463a5b9 100644 --- a/Lib/fontTools/cffLib/CFFToCFF2.py +++ b/Lib/fontTools/cffLib/CFFToCFF2.py @@ -43,7 +43,15 @@ def _convertCFFToCFF2(cff, otFont): fdArray = topDict.FDArray if hasattr(topDict, "FDArray") else None charStrings = topDict.CharStrings 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(): cs, fdIndex = charStrings.getItemAndSelector(glyphName) @@ -70,13 +78,21 @@ def _convertCFFToCFF2(cff, otFont): for glyphName in charStrings.keys(): cs, fdIndex = charStrings.getItemAndSelector(glyphName) 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 # CharString that has an explicit width encoded will throw back to us. extractor = T2WidthExtractor( - localSubrs[fdIndex] if localSubrs else [], + thisLocalSubrs, globalSubrs, nominalWidthXError, 0, @@ -94,7 +110,7 @@ def _convertCFFToCFF2(cff, otFont): op = program.pop(0) bias = extractor.localBias if op == "callsubr" else extractor.globalBias subrNumber += bias - subrSet = localSubrs[fdIndex] if op == "callsubr" else globalSubrs + subrSet = thisLocalSubrs if op == "callsubr" else globalSubrs subrProgram = subrSet[subrNumber].program program[:0] = subrProgram # Now pop the actual width