From 31cf5c9ee6989c67728f28bbe9944949046ae731 Mon Sep 17 00:00:00 2001 From: Just van Rossum Date: Wed, 24 Apr 2024 16:09:37 +0200 Subject: [PATCH 1/2] If the designspace file does not contain instances, make them up from axis value labels, if they exist --- src/fontra_compile/compile_fontmake_action.py | 42 ++++ tests/data/MutatorSans-fontmake.ttx | 222 +++++++++++++++++- tests/data/MutatorSans.fontra/font-data.json | 40 +++- tests/test_workflow.py | 3 + 4 files changed, 297 insertions(+), 10 deletions(-) diff --git a/src/fontra_compile/compile_fontmake_action.py b/src/fontra_compile/compile_fontmake_action.py index 7c813c0..87974dd 100644 --- a/src/fontra_compile/compile_fontmake_action.py +++ b/src/fontra_compile/compile_fontmake_action.py @@ -1,3 +1,4 @@ +import itertools import os import pathlib import subprocess @@ -10,6 +11,7 @@ from fontra.backends.copy import copyFont from fontra.core.protocols import ReadableFontBackend from fontra.workflow.actions import OutputActionProtocol, registerActionClass +from fontTools.designspaceLib import DesignSpaceDocument @registerActionClass("compile-fontmake") @@ -46,6 +48,8 @@ async def process( async with aclosing(dsBackend): await copyFont(self.input, dsBackend, continueOnError=continueOnError) + addInstances(designspacePath) + command = [ "fontmake", "-m", @@ -62,3 +66,41 @@ async def process( command.append(value) subprocess.run(command, check=True) + + +def addInstances(designspacePath): + dsDoc = DesignSpaceDocument.fromfile(designspacePath) + if dsDoc.instances: + # There are instances + return + + # We will make up instances based on the axis value labels + + sortOrder = { + "wght": 0, + "wdth": 1, + "ital": 2, + "slnt": 3, + } + axes = sorted(dsDoc.axes, key=lambda axis: sortOrder.get(axis.tag, 100)) + + elidedFallbackName = dsDoc.elidedFallbackName or "Regular" + dsDoc.elidedFallbackName = elidedFallbackName + + axisLabels = [ + [ + (axis.name, label.name if not label.elidable else None, label.userValue) + for label in axis.axisLabels + ] + for axis in axes + ] + + for items in itertools.product(*axisLabels): + location = {name: value for (name, valueLabel, value) in items} + nameParts = [valueLabel for (name, valueLabel, value) in items if valueLabel] + if not nameParts: + nameParts = [elidedFallbackName] + styleName = " ".join(nameParts) + dsDoc.addInstanceDescriptor(styleName=styleName, userLocation=location) + + dsDoc.write(designspacePath) diff --git a/tests/data/MutatorSans-fontmake.ttx b/tests/data/MutatorSans-fontmake.ttx index fc9d925..7ceccf4 100644 --- a/tests/data/MutatorSans-fontmake.ttx +++ b/tests/data/MutatorSans-fontmake.ttx @@ -1,5 +1,5 @@ - + @@ -63,12 +63,12 @@ - + - - + + @@ -1597,6 +1597,51 @@ Weight + + Condensed Light + + + Light + + + Wide Light + + + Condensed + + + Regular + + + Wide + + + Condensed Medium + + + Medium + + + Wide Medium + + + Condensed Black + + + Black + + + Wide Black + + + width + + + Normal + + + weight + License same as MutatorMath. BSD 3-clause. [test-token: C] @@ -1627,6 +1672,51 @@ Weight + + Condensed Light + + + Light + + + Wide Light + + + Condensed + + + Regular + + + Wide + + + Condensed Medium + + + Medium + + + Wide Medium + + + Condensed Black + + + Black + + + Wide Black + + + width + + + Normal + + + weight + @@ -1902,17 +1992,61 @@ - + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1936,6 +2070,78 @@ 900.0 257 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/data/MutatorSans.fontra/font-data.json b/tests/data/MutatorSans.fontra/font-data.json index ea0ff30..8cf30d1 100644 --- a/tests/data/MutatorSans.fontra/font-data.json +++ b/tests/data/MutatorSans.fontra/font-data.json @@ -7,6 +7,7 @@ "copyright": "License same as MutatorMath. BSD 3-clause. [test-token: C]", "licenseDescription": "License same as MutatorMath. BSD 3-clause. [test-token: C]" }, +"axes": { "axes": [ { "name": "width", @@ -14,7 +15,22 @@ "tag": "wdth", "minValue": 0, "defaultValue": 0, -"maxValue": 1000 +"maxValue": 1000, +"valueLabels": [ +{ +"name": "Condensed", +"value": 0 +}, +{ +"name": "Normal", +"value": 400, +"elidable": true +}, +{ +"name": "Wide", +"value": 1000 +} +] }, { "name": "weight", @@ -32,9 +48,29 @@ 900, 850 ] +], +"valueLabels": [ +{ +"name": "Light", +"value": 100 +}, +{ +"name": "Regular", +"value": 400, +"elidable": true +}, +{ +"name": "Medium", +"value": 600 +}, +{ +"name": "Black", +"value": 900 +} ] } -], +] +}, "sources": {}, "customData": {} } diff --git a/tests/test_workflow.py b/tests/test_workflow.py index 056c180..7353f89 100644 --- a/tests/test_workflow.py +++ b/tests/test_workflow.py @@ -54,6 +54,9 @@ async def test_workflow(tmpdir, workflowSource, ttxFileName): outTTXPath = tmpdir / (outPath.stem + ".ttx") subprocess.run(["ttx", "-o", outTTXPath, outPath], check=True) + # # Write expected + # ttxPath.write_text(outTTXPath.read_text()) + ttxLines = cleanupTTX(outTTXPath.read_text()) expectedLines = cleanupTTX(ttxPath.read_text()) assert expectedLines == ttxLines, outTTXPath From 0064840424282ec86657e315975330184a156e1f Mon Sep 17 00:00:00 2001 From: Just van Rossum Date: Wed, 24 Apr 2024 17:28:28 +0200 Subject: [PATCH 2/2] Changed axis order, to get WWI style names in fvar instances --- src/fontra_compile/compile_fontmake_action.py | 3 + tests/data/MutatorSans-fontmake.ttx | 2088 ++++++++--------- tests/data/MutatorSans.fontra/font-data.json | 46 +- tests/data/MutatorSans.ttx | 142 +- 4 files changed, 1141 insertions(+), 1138 deletions(-) diff --git a/src/fontra_compile/compile_fontmake_action.py b/src/fontra_compile/compile_fontmake_action.py index 87974dd..3b5194e 100644 --- a/src/fontra_compile/compile_fontmake_action.py +++ b/src/fontra_compile/compile_fontmake_action.py @@ -101,6 +101,9 @@ def addInstances(designspacePath): if not nameParts: nameParts = [elidedFallbackName] styleName = " ".join(nameParts) + + # TODO: styleName seems to be ignored, and the instance names are derived + # from axis labels elsewhere. Figure out where this happens. dsDoc.addInstanceDescriptor(styleName=styleName, userLocation=location) dsDoc.write(designspacePath) diff --git a/tests/data/MutatorSans-fontmake.ttx b/tests/data/MutatorSans-fontmake.ttx index 7ceccf4..17aae8c 100644 --- a/tests/data/MutatorSans-fontmake.ttx +++ b/tests/data/MutatorSans-fontmake.ttx @@ -63,12 +63,12 @@ - + - - + + @@ -1592,19 +1592,19 @@ - Width + Weight - Weight + Width - Condensed Light + Light Condensed Light - Wide Light + Light Wide Condensed @@ -1616,31 +1616,31 @@ Wide - Condensed Medium + Medium Condensed Medium - Wide Medium + Medium Wide - Condensed Black + Black Condensed Black - Wide Black + Black Wide - width + weight - Normal + width - weight + Normal License same as MutatorMath. BSD 3-clause. [test-token: C] @@ -1667,19 +1667,19 @@ License same as MutatorMath. BSD 3-clause. [test-token: C] - Width + Weight - Weight + Width - Condensed Light + Light Condensed Light - Wide Light + Light Wide Condensed @@ -1691,31 +1691,31 @@ Wide - Condensed Medium + Medium Condensed Medium - Wide Medium + Medium Wide - Condensed Black + Black Condensed Black - Wide Black + Black Wide - width + weight - Normal + width - weight + Normal @@ -1767,7 +1767,7 @@ - + @@ -1778,14 +1778,14 @@ - - - + + + - - + + @@ -1795,7 +1795,7 @@ - + @@ -1803,37 +1803,37 @@ - + - + - + - + - - + + - - + + @@ -1854,7 +1854,7 @@ - + @@ -1880,7 +1880,7 @@ - + @@ -1909,16 +1909,16 @@ - - - - + + + + - + @@ -1935,8 +1935,8 @@ - - + + @@ -1991,13 +1991,13 @@ - - + + - - + + @@ -2006,44 +2006,44 @@ - - + + - + - - + + - + - - + + - - - + + + - - - + + + - - + + @@ -2051,16 +2051,6 @@ - - - wdth - 0x0 - 0.0 - 0.0 - 1000.0 - 256 - - wght @@ -2068,79 +2058,89 @@ 100.0 100.0 900.0 + 256 + + + + + wdth + 0x0 + 0.0 + 0.0 + 1000.0 257 - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -2149,45 +2149,29 @@ - + - + - + - + - - - - - - - - - - - - - - - - @@ -2206,7 +2190,23 @@ + + + + + + + + + + + + + + + + @@ -2230,11 +2230,6 @@ - - - - - @@ -2242,22 +2237,17 @@ + + + + + - - - - - - - - - - @@ -2270,7 +2260,17 @@ + + + + + + + + + + @@ -2281,37 +2281,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -2418,30 +2387,61 @@ - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -2471,51 +2471,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -2563,7 +2518,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -2609,39 +2609,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -2677,7 +2644,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -2711,17 +2711,6 @@ - - - - - - - - - - - @@ -2748,7 +2737,18 @@ + + + + + + + + + + + @@ -2772,15 +2772,6 @@ - - - - - - - - - @@ -2803,7 +2794,16 @@ + + + + + + + + + @@ -2814,58 +2814,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -2972,7 +2920,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -3025,14 +3025,6 @@ - - - - - - - - @@ -3045,7 +3037,15 @@ + + + + + + + + @@ -3056,15 +3056,6 @@ - - - - - - - - - @@ -3077,7 +3068,16 @@ + + + + + + + + + @@ -3088,11 +3088,6 @@ - - - - - @@ -3101,7 +3096,12 @@ + + + + + @@ -3113,39 +3113,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -3181,7 +3148,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -3215,35 +3215,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -3275,7 +3246,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -3305,31 +3305,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - @@ -3357,7 +3332,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + @@ -3383,28 +3383,6 @@ - - - - - - - - - - - - - - - - - - - - - - @@ -3429,7 +3407,29 @@ + + + + + + + + + + + + + + + + + + + + + + @@ -3452,13 +3452,6 @@ - - - - - - - @@ -3469,7 +3462,14 @@ + + + + + + + @@ -3478,29 +3478,6 @@ - - - - - - - - - - - - - - - - - - - - - - - @@ -3526,7 +3503,30 @@ + + + + + + + + + + + + + + + + + + + + + + + @@ -3550,6 +3550,18 @@ + + + + + + + + + + + + @@ -3571,19 +3583,7 @@ - - - - - - - - - - - - @@ -3602,50 +3602,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -3691,7 +3648,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -3735,43 +3735,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -3811,7 +3774,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -3849,37 +3849,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -3929,7 +3898,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -3977,51 +3977,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -4063,13 +4018,58 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -4115,71 +4115,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -4247,7 +4182,72 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -4312,8 +4312,8 @@ - + @@ -4379,74 +4379,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -4511,13 +4443,81 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -4585,8 +4585,8 @@ - + @@ -4654,8 +4654,8 @@ - + @@ -4724,13 +4724,6 @@ - - - - - - - @@ -4741,7 +4734,14 @@ + + + + + + + @@ -4750,35 +4750,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -4810,7 +4781,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -4840,25 +4840,6 @@ - - - - - - - - - - - - - - - - - - - @@ -4880,7 +4861,26 @@ + + + + + + + + + + + + + + + + + + + @@ -4900,29 +4900,6 @@ - - - - - - - - - - - - - - - - - - - - - - - @@ -4948,7 +4925,30 @@ + + + + + + + + + + + + + + + + + + + + + + + @@ -4972,27 +4972,6 @@ - - - - - - - - - - - - - - - - - - - - - @@ -5016,7 +4995,28 @@ + + + + + + + + + + + + + + + + + + + + + @@ -5037,26 +5037,7 @@ - - - - - - - - - - - - - - - - - - - - + @@ -5078,7 +5059,26 @@ + + + + + + + + + + + + + + + + + + + @@ -5098,6 +5098,18 @@ + + + + + + + + + + + + @@ -5119,19 +5131,7 @@ - - - - - - - - - - - - @@ -5151,17 +5151,6 @@ - - - - - - - - - - - @@ -5175,7 +5164,18 @@ + + + + + + + + + + + @@ -5187,12 +5187,6 @@ - - - - - - @@ -5209,19 +5203,19 @@ + + + + + + - - - - - - @@ -5238,7 +5232,13 @@ + + + + + + @@ -5246,13 +5246,6 @@ - - - - - - - @@ -5269,7 +5262,14 @@ + + + + + + + @@ -5284,12 +5284,6 @@ - - - - - - @@ -5306,22 +5300,19 @@ + + + + + + - - - - - - - - - @@ -5333,7 +5324,16 @@ + + + + + + + + + @@ -5343,12 +5343,6 @@ - - - - - - @@ -5366,22 +5360,19 @@ + + + + + + - - - - - - - - - @@ -5393,7 +5384,16 @@ + + + + + + + + + @@ -5412,8 +5412,8 @@ - + @@ -5421,12 +5421,6 @@ - - - - - - @@ -5435,7 +5429,13 @@ + + + + + + @@ -5443,26 +5443,26 @@ - + - + - + - + - + - + - + @@ -5472,15 +5472,6 @@ - - - - - - - - - @@ -5492,25 +5483,25 @@ - - - + + - + - - + - - + + - + + + @@ -5522,7 +5513,16 @@ + + + + + + + + + @@ -5533,24 +5533,24 @@ - + - + - + - + - + @@ -5559,15 +5559,6 @@ - - - - - - - - - @@ -5579,7 +5570,16 @@ + + + + + + + + + diff --git a/tests/data/MutatorSans.fontra/font-data.json b/tests/data/MutatorSans.fontra/font-data.json index 8cf30d1..50a3a05 100644 --- a/tests/data/MutatorSans.fontra/font-data.json +++ b/tests/data/MutatorSans.fontra/font-data.json @@ -10,29 +10,6 @@ "axes": { "axes": [ { -"name": "width", -"label": "width", -"tag": "wdth", -"minValue": 0, -"defaultValue": 0, -"maxValue": 1000, -"valueLabels": [ -{ -"name": "Condensed", -"value": 0 -}, -{ -"name": "Normal", -"value": 400, -"elidable": true -}, -{ -"name": "Wide", -"value": 1000 -} -] -}, -{ "name": "weight", "label": "weight", "tag": "wght", @@ -68,6 +45,29 @@ "value": 900 } ] +}, +{ +"name": "width", +"label": "width", +"tag": "wdth", +"minValue": 0, +"defaultValue": 0, +"maxValue": 1000, +"valueLabels": [ +{ +"name": "Condensed", +"value": 0 +}, +{ +"name": "Normal", +"value": 400, +"elidable": true +}, +{ +"name": "Wide", +"value": 1000 +} +] } ] }, diff --git a/tests/data/MutatorSans.ttx b/tests/data/MutatorSans.ttx index afb24b3..1b9d54a 100644 --- a/tests/data/MutatorSans.ttx +++ b/tests/data/MutatorSans.ttx @@ -1,5 +1,5 @@ - + @@ -63,12 +63,12 @@ - + - - + + @@ -1502,10 +1502,10 @@ - width + weight - weight + width V000 @@ -1514,10 +1514,10 @@ V001 - width + weight - weight + width V000 @@ -1567,16 +1567,6 @@ - - - wdth - 0x0 - 0.0 - 0.0 - 1000.0 - 256 - - wght @@ -1584,6 +1574,16 @@ 100.0 100.0 900.0 + 256 + + + + + wdth + 0x0 + 0.0 + 0.0 + 1000.0 257 @@ -1659,8 +1659,8 @@ - + @@ -1701,8 +1701,8 @@ - + @@ -1730,8 +1730,8 @@ - + @@ -1877,8 +1877,8 @@ - + @@ -1999,8 +1999,8 @@ - + @@ -2097,8 +2097,8 @@ - + @@ -2198,8 +2198,8 @@ - + @@ -2281,8 +2281,8 @@ - + @@ -2435,8 +2435,8 @@ - + @@ -2520,8 +2520,8 @@ - + @@ -2580,8 +2580,8 @@ - + @@ -2624,8 +2624,8 @@ - + @@ -2696,8 +2696,8 @@ - + @@ -2778,8 +2778,8 @@ - + @@ -2852,8 +2852,8 @@ - + @@ -2922,8 +2922,8 @@ - + @@ -2977,8 +2977,8 @@ - + @@ -3041,8 +3041,8 @@ - + @@ -3105,8 +3105,8 @@ - + @@ -3197,8 +3197,8 @@ - + @@ -3293,8 +3293,8 @@ - + @@ -3339,8 +3339,8 @@ - + @@ -3421,8 +3421,8 @@ - + @@ -3532,8 +3532,8 @@ - + @@ -3673,8 +3673,8 @@ - + @@ -3725,8 +3725,8 @@ - + @@ -3881,8 +3881,8 @@ - + @@ -3933,8 +3933,8 @@ - + @@ -3985,8 +3985,8 @@ - + @@ -4069,8 +4069,8 @@ - + @@ -4137,8 +4137,8 @@ - + @@ -4203,8 +4203,8 @@ - + @@ -4271,8 +4271,8 @@ - + @@ -4339,8 +4339,8 @@ - + @@ -4401,8 +4401,8 @@ - + @@ -4461,8 +4461,8 @@ - + @@ -4505,8 +4505,8 @@ - + @@ -4547,8 +4547,8 @@ - + @@ -4592,8 +4592,8 @@ - + @@ -4637,8 +4637,8 @@ - + @@ -4682,8 +4682,8 @@ - + @@ -4717,8 +4717,8 @@ - + @@ -4759,8 +4759,8 @@ - + @@ -4795,8 +4795,8 @@ - + @@ -4829,8 +4829,8 @@ - + @@ -4843,8 +4843,8 @@ - + @@ -4911,8 +4911,8 @@ - + @@ -4943,8 +4943,8 @@ - + @@ -4977,8 +4977,8 @@ - + @@ -5009,8 +5009,8 @@ - + @@ -5037,8 +5037,8 @@ - + @@ -5066,8 +5066,8 @@ - + @@ -5092,8 +5092,8 @@ - +