summaryrefslogtreecommitdiff
path: root/misc/fontbuildlib/stat.py
blob: 7fafb8a0937611f4f36ab75b7f24483fe042fbd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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]
  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:
    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():
      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)