Skip to content

Commit

Permalink
If the designspace file does not contain instances, make them up from…
Browse files Browse the repository at this point in the history
… axis value labels, if they exist
  • Loading branch information
justvanrossum committed Apr 24, 2024
1 parent ff19e1b commit 31cf5c9
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 10 deletions.
42 changes: 42 additions & 0 deletions src/fontra_compile/compile_fontmake_action.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import itertools
import os
import pathlib
import subprocess
Expand All @@ -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")
Expand Down Expand Up @@ -46,6 +48,8 @@ async def process(
async with aclosing(dsBackend):
await copyFont(self.input, dsBackend, continueOnError=continueOnError)

addInstances(designspacePath)

command = [
"fontmake",
"-m",
Expand All @@ -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)
222 changes: 214 additions & 8 deletions tests/data/MutatorSans-fontmake.ttx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ttFont sfntVersion="\x00\x01\x00\x00" ttLibVersion="4.50">
<ttFont sfntVersion="\x00\x01\x00\x00" ttLibVersion="4.51">

<GlyphOrder>
<!-- The 'id' attribute is only for humans; it is ignored when parsed. -->
Expand Down Expand Up @@ -63,12 +63,12 @@
<!-- Most of this table will be recalculated by the compiler -->
<tableVersion value="1.0"/>
<fontRevision value="1.002"/>
<checkSumAdjustment value="0x506c1a2a"/>
<checkSumAdjustment value="0xe539c676"/>
<magicNumber value="0x5f0f3cf5"/>
<flags value="00000000 00000011"/>
<unitsPerEm value="1000"/>
<created value="Wed Mar 20 09:19:05 2024"/>
<modified value="Wed Mar 20 09:19:05 2024"/>
<created value="Wed Apr 24 14:06:02 2024"/>
<modified value="Wed Apr 24 14:06:02 2024"/>
<xMin value="0"/>
<yMin value="-250"/>
<xMax value="559"/>
Expand Down Expand Up @@ -1597,6 +1597,51 @@
<namerecord nameID="257" platformID="1" platEncID="0" langID="0x0" unicode="True">
Weight
</namerecord>
<namerecord nameID="258" platformID="1" platEncID="0" langID="0x0" unicode="True">
Condensed Light
</namerecord>
<namerecord nameID="259" platformID="1" platEncID="0" langID="0x0" unicode="True">
Light
</namerecord>
<namerecord nameID="260" platformID="1" platEncID="0" langID="0x0" unicode="True">
Wide Light
</namerecord>
<namerecord nameID="261" platformID="1" platEncID="0" langID="0x0" unicode="True">
Condensed
</namerecord>
<namerecord nameID="262" platformID="1" platEncID="0" langID="0x0" unicode="True">
Regular
</namerecord>
<namerecord nameID="263" platformID="1" platEncID="0" langID="0x0" unicode="True">
Wide
</namerecord>
<namerecord nameID="264" platformID="1" platEncID="0" langID="0x0" unicode="True">
Condensed Medium
</namerecord>
<namerecord nameID="265" platformID="1" platEncID="0" langID="0x0" unicode="True">
Medium
</namerecord>
<namerecord nameID="266" platformID="1" platEncID="0" langID="0x0" unicode="True">
Wide Medium
</namerecord>
<namerecord nameID="267" platformID="1" platEncID="0" langID="0x0" unicode="True">
Condensed Black
</namerecord>
<namerecord nameID="268" platformID="1" platEncID="0" langID="0x0" unicode="True">
Black
</namerecord>
<namerecord nameID="269" platformID="1" platEncID="0" langID="0x0" unicode="True">
Wide Black
</namerecord>
<namerecord nameID="270" platformID="1" platEncID="0" langID="0x0" unicode="True">
width
</namerecord>
<namerecord nameID="271" platformID="1" platEncID="0" langID="0x0" unicode="True">
Normal
</namerecord>
<namerecord nameID="272" platformID="1" platEncID="0" langID="0x0" unicode="True">
weight
</namerecord>
<namerecord nameID="0" platformID="3" platEncID="1" langID="0x409">
License same as MutatorMath. BSD 3-clause. [test-token: C]
</namerecord>
Expand Down Expand Up @@ -1627,6 +1672,51 @@
<namerecord nameID="257" platformID="3" platEncID="1" langID="0x409">
Weight
</namerecord>
<namerecord nameID="258" platformID="3" platEncID="1" langID="0x409">
Condensed Light
</namerecord>
<namerecord nameID="259" platformID="3" platEncID="1" langID="0x409">
Light
</namerecord>
<namerecord nameID="260" platformID="3" platEncID="1" langID="0x409">
Wide Light
</namerecord>
<namerecord nameID="261" platformID="3" platEncID="1" langID="0x409">
Condensed
</namerecord>
<namerecord nameID="262" platformID="3" platEncID="1" langID="0x409">
Regular
</namerecord>
<namerecord nameID="263" platformID="3" platEncID="1" langID="0x409">
Wide
</namerecord>
<namerecord nameID="264" platformID="3" platEncID="1" langID="0x409">
Condensed Medium
</namerecord>
<namerecord nameID="265" platformID="3" platEncID="1" langID="0x409">
Medium
</namerecord>
<namerecord nameID="266" platformID="3" platEncID="1" langID="0x409">
Wide Medium
</namerecord>
<namerecord nameID="267" platformID="3" platEncID="1" langID="0x409">
Condensed Black
</namerecord>
<namerecord nameID="268" platformID="3" platEncID="1" langID="0x409">
Black
</namerecord>
<namerecord nameID="269" platformID="3" platEncID="1" langID="0x409">
Wide Black
</namerecord>
<namerecord nameID="270" platformID="3" platEncID="1" langID="0x409">
width
</namerecord>
<namerecord nameID="271" platformID="3" platEncID="1" langID="0x409">
Normal
</namerecord>
<namerecord nameID="272" platformID="3" platEncID="1" langID="0x409">
weight
</namerecord>
</name>

<post>
Expand Down Expand Up @@ -1902,17 +1992,61 @@
<DesignAxisRecord>
<Axis index="0">
<AxisTag value="wdth"/>
<AxisNameID value="256"/> <!-- Width -->
<AxisNameID value="270"/> <!-- width -->
<AxisOrdering value="0"/>
</Axis>
<Axis index="1">
<AxisTag value="wght"/>
<AxisNameID value="257"/> <!-- Weight -->
<AxisNameID value="272"/> <!-- weight -->
<AxisOrdering value="1"/>
</Axis>
</DesignAxisRecord>
<!-- AxisValueCount=0 -->
<ElidedFallbackNameID value="2"/> <!-- Regular -->
<!-- AxisValueCount=7 -->
<AxisValueArray>
<AxisValue index="0" Format="1">
<AxisIndex value="0"/>
<Flags value="0"/>
<ValueNameID value="261"/> <!-- Condensed -->
<Value value="0.0"/>
</AxisValue>
<AxisValue index="1" Format="1">
<AxisIndex value="0"/>
<Flags value="2"/> <!-- ElidableAxisValueName -->
<ValueNameID value="271"/> <!-- Normal -->
<Value value="400.0"/>
</AxisValue>
<AxisValue index="2" Format="1">
<AxisIndex value="0"/>
<Flags value="0"/>
<ValueNameID value="263"/> <!-- Wide -->
<Value value="1000.0"/>
</AxisValue>
<AxisValue index="3" Format="1">
<AxisIndex value="1"/>
<Flags value="0"/>
<ValueNameID value="259"/> <!-- Light -->
<Value value="100.0"/>
</AxisValue>
<AxisValue index="4" Format="1">
<AxisIndex value="1"/>
<Flags value="2"/> <!-- ElidableAxisValueName -->
<ValueNameID value="262"/> <!-- Regular -->
<Value value="400.0"/>
</AxisValue>
<AxisValue index="5" Format="1">
<AxisIndex value="1"/>
<Flags value="0"/>
<ValueNameID value="265"/> <!-- Medium -->
<Value value="600.0"/>
</AxisValue>
<AxisValue index="6" Format="1">
<AxisIndex value="1"/>
<Flags value="0"/>
<ValueNameID value="268"/> <!-- Black -->
<Value value="900.0"/>
</AxisValue>
</AxisValueArray>
<ElidedFallbackNameID value="262"/> <!-- Regular -->
</STAT>

<fvar>
Expand All @@ -1936,6 +2070,78 @@
<MaxValue>900.0</MaxValue>
<AxisNameID>257</AxisNameID>
</Axis>

<!-- Condensed Light -->
<NamedInstance flags="0x0" subfamilyNameID="258">
<coord axis="wdth" value="0.0"/>
<coord axis="wght" value="100.0"/>
</NamedInstance>

<!-- Light -->
<NamedInstance flags="0x0" subfamilyNameID="259">
<coord axis="wdth" value="400.0"/>
<coord axis="wght" value="100.0"/>
</NamedInstance>

<!-- Wide Light -->
<NamedInstance flags="0x0" subfamilyNameID="260">
<coord axis="wdth" value="1000.0"/>
<coord axis="wght" value="100.0"/>
</NamedInstance>

<!-- Condensed -->
<NamedInstance flags="0x0" subfamilyNameID="261">
<coord axis="wdth" value="0.0"/>
<coord axis="wght" value="400.0"/>
</NamedInstance>

<!-- Regular -->
<NamedInstance flags="0x0" subfamilyNameID="262">
<coord axis="wdth" value="400.0"/>
<coord axis="wght" value="400.0"/>
</NamedInstance>

<!-- Wide -->
<NamedInstance flags="0x0" subfamilyNameID="263">
<coord axis="wdth" value="1000.0"/>
<coord axis="wght" value="400.0"/>
</NamedInstance>

<!-- Condensed Medium -->
<NamedInstance flags="0x0" subfamilyNameID="264">
<coord axis="wdth" value="0.0"/>
<coord axis="wght" value="600.0"/>
</NamedInstance>

<!-- Medium -->
<NamedInstance flags="0x0" subfamilyNameID="265">
<coord axis="wdth" value="400.0"/>
<coord axis="wght" value="600.0"/>
</NamedInstance>

<!-- Wide Medium -->
<NamedInstance flags="0x0" subfamilyNameID="266">
<coord axis="wdth" value="1000.0"/>
<coord axis="wght" value="600.0"/>
</NamedInstance>

<!-- Condensed Black -->
<NamedInstance flags="0x0" subfamilyNameID="267">
<coord axis="wdth" value="0.0"/>
<coord axis="wght" value="900.0"/>
</NamedInstance>

<!-- Black -->
<NamedInstance flags="0x0" subfamilyNameID="268">
<coord axis="wdth" value="400.0"/>
<coord axis="wght" value="900.0"/>
</NamedInstance>

<!-- Wide Black -->
<NamedInstance flags="0x0" subfamilyNameID="269">
<coord axis="wdth" value="1000.0"/>
<coord axis="wght" value="900.0"/>
</NamedInstance>
</fvar>

<gvar>
Expand Down
40 changes: 38 additions & 2 deletions tests/data/MutatorSans.fontra/font-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,30 @@
"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",
"label": "width",
"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",
Expand All @@ -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": {}
}
Loading

0 comments on commit 31cf5c9

Please sign in to comment.