2020-08-19 03:57:25 +03:00
|
|
|
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
|
|
|
|
|
2020-08-19 03:57:25 +03:00
|
|
|
def rebuildStatTable(font, designspace):
|
2020-08-20 04:01:47 +03:00
|
|
|
#
|
|
|
|
# Changing this code? See discussion at https://github.com/rsms/inter/issues/308
|
|
|
|
#
|
2020-08-19 03:57:25 +03:00
|
|
|
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 04:01:47 +03:00
|
|
|
|
|
|
|
# isMultiAxis is true when compiling the multi-axis VF,
|
|
|
|
# false when compiling e.g. Inter-roman.var.ttf
|
|
|
|
isMultiAxis = False
|
2020-08-20 01:37:10 +03:00
|
|
|
if len(axes) > 1:
|
2020-08-20 04:01:47 +03:00
|
|
|
isMultiAxis = True
|
|
|
|
|
2020-08-20 01:37:10 +03:00
|
|
|
axisTagToName = dict()
|
2020-08-19 03:57:25 +03:00
|
|
|
for axis in designspace.axes:
|
2020-08-20 01:37:10 +03:00
|
|
|
axisTagToName[axis.tag] = axis.name
|
2020-08-20 04:01:47 +03:00
|
|
|
|
2020-08-20 01:37:10 +03:00
|
|
|
weightAxisName = axisTagToName['wght']
|
|
|
|
slantAxisName = axisTagToName.get('slnt', 'Slant')
|
2020-08-20 04:01:47 +03:00
|
|
|
regularWeightValueEntry = None
|
|
|
|
|
2020-08-20 01:37:10 +03:00
|
|
|
weightValues = []
|
2020-08-20 04:01:47 +03:00
|
|
|
slantValues = []
|
|
|
|
extremeSlantValue = 0
|
2020-08-19 03:57:25 +03:00
|
|
|
for instance in designspace.instances:
|
2020-08-20 04:01:47 +03:00
|
|
|
weightValue = instance.location[weightAxisName]
|
|
|
|
slantValue = instance.location.get(slantAxisName, 0)
|
|
|
|
if slantValue != 0:
|
|
|
|
# slanted (we only make one entry: "Italic")
|
|
|
|
if isMultiAxis and weightValue == 400:
|
|
|
|
extremeSlantValue = slantValue
|
|
|
|
slantValues.append({
|
|
|
|
'name': instance.styleName,
|
|
|
|
'value': slantValue,
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
# upright
|
|
|
|
v = {
|
|
|
|
'name': instance.styleName,
|
|
|
|
'value': weightValue,
|
2020-08-20 01:37:10 +03:00
|
|
|
}
|
2020-08-20 04:01:47 +03:00
|
|
|
if weightValue == 400:
|
|
|
|
v['flags'] = OT_ELIDABLE_AXIS_VALUE_NAME
|
|
|
|
v['linkedValue'] = 700 # style link to "Bold"
|
|
|
|
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)
|
2020-08-19 03:57:25 +03:00
|
|
|
|
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)
|