1
1
mirror of https://github.com/rsms/inter.git synced 2024-11-23 20:55:33 +03:00
inter/misc/fontbuildlib/stat.py

62 lines
2.1 KiB
Python
Raw Normal View History

from fontTools.otlLib.builder import buildStatTable
2020-08-20 01:37:10 +03:00
# [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]
2020-08-20 01:37:10 +03:00
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:
2020-08-20 01:37:10 +03:00
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:
for axisName, value in instance.location.items():
2020-08-20 01:37:10 +03:00
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
2020-08-20 01:37:10 +03:00
# 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)
2020-08-20 01:37:10 +03:00
buildStatTable(font, axes, locations)