| 1 | from fontTools.otlLib.builder import buildStatTable, _addName |
| 2 | from fontTools.ttLib import TTFont |
| 3 | import sys |
| 4 | |
| 5 | |
| 6 | AXES = [ |
| 7 | dict( |
| 8 | tag="wght", |
| 9 | name="Weight", |
| 10 | ordering=0, |
| 11 | values=[ |
| 12 | dict(nominalValue=100, rangeMinValue=100, rangeMaxValue=150, name="Thin"), |
| 13 | dict(nominalValue=200, rangeMinValue=150, rangeMaxValue=250, name="ExtraLight"), |
| 14 | dict(nominalValue=300, rangeMinValue=250, rangeMaxValue=350, name="Light"), |
| 15 | dict(nominalValue=400, rangeMinValue=350, rangeMaxValue=450, name="Regular"), |
| 16 | dict(nominalValue=500, rangeMinValue=450, rangeMaxValue=550, name="Medium"), |
| 17 | dict(nominalValue=600, rangeMinValue=550, rangeMaxValue=650, name="SemiBold"), |
| 18 | dict(nominalValue=700, rangeMinValue=650, rangeMaxValue=750, name="Bold"), |
| 19 | dict(nominalValue=800, rangeMinValue=750, rangeMaxValue=850, name="ExtraBold"), |
| 20 | dict(nominalValue=900, rangeMinValue=850, rangeMaxValue=900, name="Black"), |
| 21 | ], |
| 22 | ), |
| 23 | dict( |
| 24 | tag="KSHD", |
| 25 | name="kashida", |
| 26 | ordering=1, |
| 27 | values=[ |
| 28 | dict(nominalValue=100, rangeMinValue=100, rangeMaxValue=150, name="Normal", flags=0x2), |
| 29 | dict(nominalValue=200, rangeMinValue=150, rangeMaxValue=200, name="Taweel"), |
| 30 | ], |
| 31 | ), |
| 32 | dict( |
| 33 | tag="DSTY", |
| 34 | name="dot-style", |
| 35 | ordering=1, |
| 36 | values=[ |
| 37 | dict(nominalValue=1.5, rangeMinValue=1, rangeMaxValue=1.99, name="Normal", flags=0x2), |
| 38 | dict(nominalValue=2.5, rangeMinValue=2, rangeMaxValue=2.99, name="Separate Dots"), |
| 39 | dict(nominalValue=3, rangeMinValue=3, rangeMaxValue=3, name="Line Double Dots"), |
| 40 | ], |
| 41 | ), |
| 42 | ] |
| 43 | |
| 44 | def update_fvar(ttfont): |
| 45 | fvar = ttfont['fvar'] |
| 46 | nametable = ttfont['name'] |
| 47 | family_name = nametable.getName(16, 3, 1, 1033) or nametable.getName(1, 3, 1, 1033) |
| 48 | family_name = family_name.toUnicode().replace(" ", "") |
| 49 | nametable.setName(family_name, 25, 3, 1, 1033) |
| 50 | for instance in fvar.instances: |
| 51 | instance_style = nametable.getName(instance.subfamilyNameID, 3, 1, 1033).toUnicode() |
| 52 | ps_name = f"{family_name}-{instance_style.replace(' ', '')}" |
| 53 | instance.postscriptNameID = _addName(nametable, ps_name, 256) |
| 54 | |
| 55 | |
| 56 | def main(): |
| 57 | filepath = sys.argv[1] |
| 58 | tt = TTFont(filepath) |
| 59 | buildStatTable(tt, AXES) |
| 60 | update_fvar(tt) |
| 61 | tt.save(filepath) |
| 62 | print(f"Added STAT table to {filepath}") |
| 63 | |
| 64 | |
| 65 | if __name__ == "__main__": |
| 66 | main() |