Skip to content

Commit

Permalink
[varLib/cff] Allow sparse masters
Browse files Browse the repository at this point in the history
Fixes #3233
  • Loading branch information
behdad committed Aug 1, 2023
1 parent 91731f7 commit e375827
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
5 changes: 2 additions & 3 deletions Lib/fontTools/misc/psCharStrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,6 @@ def op_vstem3(self, index):


class T2CharString(object):

operandEncoding = t2OperandEncoding
operators, opcodes = buildOperatorDict(t2Operators)
decompilerClass = SimpleT2Decompiler
Expand Down Expand Up @@ -1150,6 +1149,8 @@ def draw(self, pen, blender=None):
self.width = extractor.width

def calcBounds(self, glyphSet):
self.decompile()
print(self.program)
boundsPen = BoundsPen(glyphSet)
self.draw(boundsPen)
return boundsPen.bounds
Expand Down Expand Up @@ -1313,7 +1314,6 @@ def fromXML(self, name, attrs, content):


class T1CharString(T2CharString):

operandEncoding = t1OperandEncoding
operators, opcodes = buildOperatorDict(t1Operators)

Expand Down Expand Up @@ -1347,7 +1347,6 @@ def draw(self, pen):


class DictDecompiler(object):

operandEncoding = cffDictOperandEncoding

def __init__(self, strings, parent=None):
Expand Down
29 changes: 23 additions & 6 deletions Lib/fontTools/varLib/cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,25 @@ def merge_region_fonts(varFont, model, ordered_fonts_list, glyphOrder):
addCFFVarStore(varFont, model, cvData.varDataList, cvData.masterSupports)


def _get_cs(charstrings, glyphName):
def _get_cs(charstrings, glyphName, filterEmpty=False):
if glyphName not in charstrings:
return None
return charstrings[glyphName]
cs = charstrings[glyphName]

if filterEmpty:
cs.decompile()
# print(cs.program)
if cs.program == []: # CFF2 empty charstring
return None
elif (
len(cs.program) <= 2
and cs.program[-1] == "endchar"
and (len(cs.program) == 1 or type(cs.program[0]) in (int, float))
): # CFF1 empty charstring
cs.program = []
return None

return cs


def _add_new_vsindex(
Expand All @@ -373,16 +388,15 @@ def _add_new_vsindex(


def merge_charstrings(glyphOrder, num_masters, top_dicts, masterModel):

vsindex_dict = {}
vsindex_by_key = {}
varDataList = []
masterSupports = []
default_charstrings = top_dicts[0].CharStrings
for gid, gname in enumerate(glyphOrder):
all_cs = [_get_cs(td.CharStrings, gname) for td in top_dicts]
if len([gs for gs in all_cs if gs is not None]) == 1:
continue
all_cs = [
_get_cs(td.CharStrings, gname, i != 0) for i, td in enumerate(top_dicts)
]
model, model_cs = masterModel.getSubModel(all_cs)
# create the first pass CFF2 charstring, from
# the default charstring.
Expand All @@ -402,6 +416,9 @@ def merge_charstrings(glyphOrder, num_masters, top_dicts, masterModel):
region_charstring.outlineExtractor = MergeOutlineExtractor
region_charstring.draw(var_pen)

if not region_cs:
continue

# Collapse each coordinate list to a blend operator and its args.
new_cs = var_pen.getCharString(
private=default_charstring.private,
Expand Down

0 comments on commit e375827

Please sign in to comment.