From 0218e07f76e3ff413a220a71a5d4f8102a90f17b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 19 Nov 2018 16:22:49 -0500 Subject: [PATCH] [varLib.mutator] Move some code out of merger --- Lib/fontTools/varLib/merger.py | 63 +++------------------------------ Lib/fontTools/varLib/mutator.py | 58 ++++++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 64 deletions(-) diff --git a/Lib/fontTools/varLib/merger.py b/Lib/fontTools/varLib/merger.py index 3270bf519..e5f02fede 100644 --- a/Lib/fontTools/varLib/merger.py +++ b/Lib/fontTools/varLib/merger.py @@ -813,7 +813,9 @@ def merge(merger, self, lst): class MutatorMerger(AligningMerger): """A merger that takes a variable font, and instantiates - an instance.""" + an instance. While there's no "merging" to be done per se, + the operation can benefit from many operations that the + aligning merger does.""" def __init__(self, font, location): Merger.__init__(self, font) @@ -827,63 +829,10 @@ class MutatorMerger(AligningMerger): self.instancer = VarStoreInstancer(store, font['fvar'].axes, location) - def instantiate(self): - font = self.font - - for tableTag in 'GSUB','GPOS': - if not tableTag in font: - continue - table = font[tableTag].table - if not hasattr(table, 'FeatureVariations'): - continue - variations = table.FeatureVariations - for record in variations.FeatureVariationRecord: - applies = True - for condition in record.ConditionSet.ConditionTable: - if condition.Format == 1: - axisIdx = condition.AxisIndex - axisTag = self.font['fvar'].axes[axisIdx].axisTag - Min = condition.FilterRangeMinValue - Max = condition.FilterRangeMaxValue - loc = self.location[axisTag] - if not (Min <= loc <= Max): - applies = False - else: - applies = False - if not applies: - break - - if applies: - assert record.FeatureTableSubstitution.Version == 0x00010000 - for rec in record.FeatureTableSubstitution.SubstitutionRecord: - table.FeatureList.FeatureRecord[rec.FeatureIndex].Feature = rec.Feature - break - del table.FeatureVariations - - - self.mergeTables(font, [font], ['GPOS']) - - if 'GDEF' in font: - gdef = font['GDEF'].table - if gdef.Version >= 0x00010003: - del gdef.VarStore - gdef.Version = 0x00010002 - if gdef.MarkGlyphSetsDef is None: - del gdef.MarkGlyphSetsDef - gdef.Version = 0x00010000 - if not (gdef.LigCaretList or - gdef.MarkAttachClassDef or - gdef.GlyphClassDef or - gdef.AttachList or - (gdef.Version >= 0x00010002 and gdef.MarkGlyphSetsDef)): - del font['GDEF'] - @MutatorMerger.merger(ot.Anchor) def merge(merger, self, lst): - # Most other structs are merged with self pointing to a copy of base font. - # Anchors are sometimes created later and initialized to have 0/None members. - # Hence the copy. + # Hack till we become selfless. self.__dict__ = lst[0].__dict__.copy() if self.Format != 3: @@ -911,9 +860,7 @@ def merge(merger, self, lst): @MutatorMerger.merger(otBase.ValueRecord) def merge(merger, self, lst): - # Most other structs are merged with self pointing to a copy of base font. - # ValueRecords are sometimes created later and initialized to have 0/None members. - # Hence the copy. + # Hack till we become selfless. self.__dict__ = lst[0].__dict__.copy() instancer = merger.instancer diff --git a/Lib/fontTools/varLib/mutator.py b/Lib/fontTools/varLib/mutator.py index 9616dbb0c..cabb92b73 100644 --- a/Lib/fontTools/varLib/mutator.py +++ b/Lib/fontTools/varLib/mutator.py @@ -115,12 +115,59 @@ def instantiateVariableFont(varfont, location, inplace=False): setattr(varfont[tableTag], itemName, getattr(varfont[tableTag], itemName) + delta) - if 'GDEF' in varfont: - log.info("Mutating GDEF/GPOS/GSUB tables") - merger = MutatorMerger(varfont, loc) + log.info("Mutating FeatureVariations") + for tableTag in 'GSUB','GPOS': + if not tableTag in varfont: + continue + table = varfont[tableTag].table + if not hasattr(table, 'FeatureVariations'): + continue + variations = table.FeatureVariations + for record in variations.FeatureVariationRecord: + applies = True + for condition in record.ConditionSet.ConditionTable: + if condition.Format == 1: + axisIdx = condition.AxisIndex + axisTag = fvar.axes[axisIdx].axisTag + Min = condition.FilterRangeMinValue + Max = condition.FilterRangeMaxValue + v = loc[axisTag] + if not (Min <= v <= Max): + applies = False + else: + applies = False + if not applies: + break + + if applies: + assert record.FeatureTableSubstitution.Version == 0x00010000 + for rec in record.FeatureTableSubstitution.SubstitutionRecord: + table.FeatureList.FeatureRecord[rec.FeatureIndex].Feature = rec.Feature + break + del table.FeatureVariations + + if 'GDEF' in varfont and varfont['GDEF'].table.Version >= 0x00010003: + log.info("Mutating GDEF/GPOS/GSUB tables") + gdef = varfont['GDEF'].table + instancer = VarStoreInstancer(gdef.VarStore, fvar.axes, loc) + + merger = MutatorMerger(varfont, loc) + merger.mergeTables(varfont, [varfont], ['GPOS']) + + # Downgrade GDEF. + del gdef.VarStore + gdef.Version = 0x00010002 + if gdef.MarkGlyphSetsDef is None: + del gdef.MarkGlyphSetsDef + gdef.Version = 0x00010000 + + if not (gdef.LigCaretList or + gdef.MarkAttachClassDef or + gdef.GlyphClassDef or + gdef.AttachList or + (gdef.Version >= 0x00010002 and gdef.MarkGlyphSetsDef)): + del varfont['GDEF'] - log.info("Building interpolated tables") - merger.instantiate() addidef = False for glyph in glyf.glyphs.values(): @@ -130,7 +177,6 @@ def instantiateVariableFont(varfont, location, inplace=False): addidef = any(op.startswith("GETVARIATION") for op in instructions) if addidef: break - if addidef: log.info("Adding IDEF to fpgm table for GETVARIATION opcode") asm = []