From e821f1fb9eb6025cbce8ffd2c3dcf83c8c04ced5 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 12 Dec 2019 11:54:58 +0000 Subject: [PATCH] fontBuilder: add FontBuilder.addFeatureVariations method like the existing addOpenTypeFeatures it simply forwards to featureVars.addFeatureVariations --- Lib/fontTools/fontBuilder.py | 14 +++ Tests/fontBuilder/data/test_var.ttf.ttx | 112 ++++++++++++++++++++++-- Tests/fontBuilder/fontBuilder_test.py | 24 ++++- 3 files changed, 139 insertions(+), 11 deletions(-) diff --git a/Lib/fontTools/fontBuilder.py b/Lib/fontTools/fontBuilder.py index bd6b2e50e..29d58fb6f 100644 --- a/Lib/fontTools/fontBuilder.py +++ b/Lib/fontTools/fontBuilder.py @@ -751,6 +751,20 @@ class FontBuilder(object): from .feaLib.builder import addOpenTypeFeaturesFromString addOpenTypeFeaturesFromString(self.font, features, filename=filename, tables=tables) + def addFeatureVariations(self, conditionalSubstitutions, featureTag="rvrn"): + """Add conditional substitutions to a Variable Font. + + See `fontTools.varLib.featureVars.addFeatureVariations`. + """ + from .varLib import featureVars + + if "fvar" not in self.font: + raise KeyError("'fvar' table is missing; can't add FeatureVariations.") + + featureVars.addFeatureVariations( + self.font, conditionalSubstitutions, featureTag=featureTag + ) + def buildCmapSubTable(cmapping, format, platformID, platEncID): subTable = cmap_classes[format](format) diff --git a/Tests/fontBuilder/data/test_var.ttf.ttx b/Tests/fontBuilder/data/test_var.ttf.ttx index 382d29e10..bc1aae250 100644 --- a/Tests/fontBuilder/data/test_var.ttf.ttx +++ b/Tests/fontBuilder/data/test_var.ttf.ttx @@ -1,5 +1,5 @@ - + @@ -19,7 +19,7 @@ - + @@ -36,7 +36,7 @@ - + @@ -126,7 +126,7 @@ - + @@ -164,12 +164,12 @@ - + - - - - + + + + @@ -269,6 +269,100 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Tests/fontBuilder/fontBuilder_test.py b/Tests/fontBuilder/fontBuilder_test.py index e13f11616..8c5127626 100644 --- a/Tests/fontBuilder/fontBuilder_test.py +++ b/Tests/fontBuilder/fontBuilder_test.py @@ -164,13 +164,20 @@ def test_build_var(tmpdir): pen.lineTo((500, 400)) pen.lineTo((500, 000)) pen.closePath() + glyph1 = pen.glyph() - glyph = pen.glyph() + pen = TTGlyphPen(None) + pen.moveTo((50, 0)) + pen.lineTo((50, 200)) + pen.lineTo((250, 200)) + pen.lineTo((250, 0)) + pen.closePath() + glyph2 = pen.glyph() pen = TTGlyphPen(None) emptyGlyph = pen.glyph() - glyphs = {".notdef": emptyGlyph, "A": glyph, "a": glyph, ".null": emptyGlyph} + glyphs = {".notdef": emptyGlyph, "A": glyph1, "a": glyph2, ".null": emptyGlyph} fb.setupGlyf(glyphs) metrics = {} glyphTable = fb.font["glyf"] @@ -206,6 +213,19 @@ def test_build_var(tmpdir): ] fb.setupGvar(variations) + fb.addFeatureVariations( + [ + ( + [ + {"LEFT": (0.8, 1), "DOWN": (0.8, 1)}, + {"RGHT": (0.8, 1), "UPPP": (0.8, 1)}, + ], + {"A": "a"} + ) + ], + featureTag="rclt", + ) + fb.setupOS2() fb.setupPost() fb.setupDummyDSIG()