tooling: improve STAT table patching. See issue #308
This commit is contained in:
parent
668c2fec54
commit
3e89206f6a
1 changed files with 52 additions and 26 deletions
|
|
@ -7,42 +7,70 @@ from fontTools.otlLib.builder import buildStatTable
|
||||||
OT_ELIDABLE_AXIS_VALUE_NAME = 0x0002
|
OT_ELIDABLE_AXIS_VALUE_NAME = 0x0002
|
||||||
|
|
||||||
def rebuildStatTable(font, designspace):
|
def rebuildStatTable(font, designspace):
|
||||||
|
#
|
||||||
|
# Changing this code? See discussion at https://github.com/rsms/inter/issues/308
|
||||||
|
#
|
||||||
if not 'fvar' in font:
|
if not 'fvar' in font:
|
||||||
raise Exception('missing fvar table in font')
|
raise Exception('missing fvar table in font')
|
||||||
axes = [dict(tag=a.axisTag, name=a.axisNameID) for a in font['fvar'].axes]
|
axes = [dict(tag=a.axisTag, name=a.axisNameID) for a in font['fvar'].axes]
|
||||||
locations = None
|
|
||||||
|
# isMultiAxis is true when compiling the multi-axis VF,
|
||||||
|
# false when compiling e.g. Inter-roman.var.ttf
|
||||||
|
isMultiAxis = False
|
||||||
if len(axes) > 1:
|
if len(axes) > 1:
|
||||||
# TODO: Compute locations automatically.
|
isMultiAxis = True
|
||||||
# Currently specific to Inter w/ hard-coded values.
|
|
||||||
locations = [
|
|
||||||
{ 'name': 'Regular', 'location': { 'wght': 400, 'slnt': 0 },
|
|
||||||
'flags': OT_ELIDABLE_AXIS_VALUE_NAME },
|
|
||||||
{ 'name': 'Italic', 'location': { 'wght': 400, 'slnt': -10.0 } },
|
|
||||||
]
|
|
||||||
axisTagToName = dict()
|
axisTagToName = dict()
|
||||||
for axis in designspace.axes:
|
for axis in designspace.axes:
|
||||||
axisTagToName[axis.tag] = axis.name
|
axisTagToName[axis.tag] = axis.name
|
||||||
|
|
||||||
weightAxisName = axisTagToName['wght']
|
weightAxisName = axisTagToName['wght']
|
||||||
slantAxisName = axisTagToName.get('slnt', 'Slant')
|
slantAxisName = axisTagToName.get('slnt', 'Slant')
|
||||||
weightAxis = None
|
regularWeightValueEntry = None
|
||||||
for a in axes:
|
|
||||||
if a['tag'] == 'wght':
|
|
||||||
weightAxis = a
|
|
||||||
break
|
|
||||||
weightValues = []
|
weightValues = []
|
||||||
|
slantValues = []
|
||||||
|
extremeSlantValue = 0
|
||||||
for instance in designspace.instances:
|
for instance in designspace.instances:
|
||||||
for axisName, value in instance.location.items():
|
weightValue = instance.location[weightAxisName]
|
||||||
if axisName == slantAxisName:
|
slantValue = instance.location.get(slantAxisName, 0)
|
||||||
# skip italic/oblique/slant
|
if slantValue != 0:
|
||||||
continue
|
# slanted (we only make one entry: "Italic")
|
||||||
weightValue = {
|
if isMultiAxis and weightValue == 400:
|
||||||
'name': instance.styleName,
|
extremeSlantValue = slantValue
|
||||||
'value': instance.location[weightAxisName],
|
slantValues.append({
|
||||||
|
'name': instance.styleName,
|
||||||
|
'value': slantValue,
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
# upright
|
||||||
|
v = {
|
||||||
|
'name': instance.styleName,
|
||||||
|
'value': weightValue,
|
||||||
}
|
}
|
||||||
if weightValue['value'] == 400:
|
if weightValue == 400:
|
||||||
weightValue['flags'] = OT_ELIDABLE_AXIS_VALUE_NAME
|
v['flags'] = OT_ELIDABLE_AXIS_VALUE_NAME
|
||||||
weightValues.append(weightValue)
|
v['linkedValue'] = 700 # style link to "Bold"
|
||||||
weightAxis['values'] = weightValues
|
regularWeightValueEntry = v
|
||||||
|
weightValues.append(v)
|
||||||
|
|
||||||
|
# "Regular" entry for the slant axis, linked with the "Italic" entry
|
||||||
|
if isMultiAxis:
|
||||||
|
slantValues.append({
|
||||||
|
'name': regularWeightValueEntry['name'],
|
||||||
|
'value': 0,
|
||||||
|
'flags': OT_ELIDABLE_AXIS_VALUE_NAME,
|
||||||
|
'linkedValue': extremeSlantValue,
|
||||||
|
})
|
||||||
|
|
||||||
|
for a in axes:
|
||||||
|
tag = a['tag']
|
||||||
|
if tag == 'wght':
|
||||||
|
a['values'] = weightValues
|
||||||
|
elif tag == 'slnt' and len(slantValues) > 0:
|
||||||
|
a['values'] = slantValues
|
||||||
|
|
||||||
|
buildStatTable(font, axes)
|
||||||
|
|
||||||
# axisNameToTag = dict()
|
# axisNameToTag = dict()
|
||||||
# for axis in designspace.axes:
|
# for axis in designspace.axes:
|
||||||
|
|
@ -57,5 +85,3 @@ def rebuildStatTable(font, designspace):
|
||||||
# if instance.styleName == "Regular":
|
# if instance.styleName == "Regular":
|
||||||
# loc['flags'] = OT_ELIDABLE_AXIS_VALUE_NAME
|
# loc['flags'] = OT_ELIDABLE_AXIS_VALUE_NAME
|
||||||
# locations.append(loc)
|
# locations.append(loc)
|
||||||
|
|
||||||
buildStatTable(font, axes, locations)
|
|
||||||
|
|
|
||||||
Reference in a new issue