1
1
mirror of https://github.com/rsms/inter.git synced 2024-11-23 11:43:47 +03:00

VF: improved STAT table patch

This commit is contained in:
Rasmus Andersson 2020-08-19 15:37:10 -07:00
parent 88477a4105
commit ddb81e2646

View File

@ -1,34 +1,61 @@
# from fontTools.designspaceLib import DesignSpaceDocument
# from fontTools.ttLib.tables import otTables as ot
# from fontTools.ttLib import ttFont
from fontTools.otlLib.builder import buildStatTable
# [from OpenType spec on STAT, flags]
# If set, it indicates that the axis value represents the “normal” value
# for the axis and may be omitted when composing name strings.
OT_ELIDABLE_AXIS_VALUE_NAME = 0x0002
def rebuildStatTable(font, designspace):
if not 'fvar' in font:
raise Exception('missing fvar table in font')
axes = [dict(tag=a.axisTag, name=a.axisNameID) for a in font['fvar'].axes]
# axes = []
# for a in statTable.DesignAxisRecord.Axis:
# axes.append({ 'tag': a.AxisTag, 'name': a.AxisNameID, 'ordering': a.AxisOrdering })
axisNameToTag = dict()
locations = None
if len(axes) > 1:
# TODO: Compute locations automatically.
# 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()
for axis in designspace.axes:
axisNameToTag[axis.name] = axis.tag
locations = []
axisTagToName[axis.tag] = axis.name
weightAxisName = axisTagToName['wght']
slantAxisName = axisTagToName.get('slnt', 'Slant')
weightAxis = None
for a in axes:
if a['tag'] == 'wght':
weightAxis = a
break
weightValues = []
for instance in designspace.instances:
location = dict()
for axisName, value in instance.location.items():
tag = axisNameToTag[axisName]
location[tag] = value
locations.append({ 'name': instance.styleName, 'location': location })
if axisName == slantAxisName:
# skip italic/oblique/slant
continue
weightValue = {
'name': instance.styleName,
'value': instance.location[weightAxisName],
}
if weightValue['value'] == 400:
weightValue['flags'] = OT_ELIDABLE_AXIS_VALUE_NAME
weightValues.append(weightValue)
weightAxis['values'] = weightValues
# axisNameToTag = dict()
# for axis in designspace.axes:
# axisNameToTag[axis.name] = axis.tag
# locations = []
# for instance in designspace.instances:
# location = dict()
# for axisName, value in instance.location.items():
# tag = axisNameToTag[axisName]
# location[tag] = value
# loc = { 'name': instance.styleName, 'location': location }
# if instance.styleName == "Regular":
# loc['flags'] = OT_ELIDABLE_AXIS_VALUE_NAME
# locations.append(loc)
buildStatTable(font, axes, locations)
# font = ttFont.TTFont("build/fonts/var/Inter.var.ttf")
# designspace = DesignSpaceDocument.fromfile('build/ufo/inter.designspace')
# rebuildStatTable(font, designspace)
# print("write build/tmp/Inter.var-patched.ttf")
# font.save("build/tmp/Inter.var-patched.ttf")