File: mkstat-backup.py

package info (click to toggle)
fonts-junicode 2.211%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,448 kB
  • sloc: python: 304; sh: 236; xml: 18; makefile: 16
file content (127 lines) | stat: -rw-r--r-- 4,750 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from fontTools.otlLib import builder
from fontTools import ttLib
import sys
import argparse

whichFont = "neither"
argparser = argparse.ArgumentParser(prog='mkstat',
                                    description='Fix STAT and name tables for Junicode Two.')
argparser.add_argument("inputfile", help='Font to process.')
argparser.add_argument("outputfile", help="Font to output.")
args = argparser.parse_args()

inFont = args.inputfile
outFont = args.outputfile

if "Italic" in inFont:
    whichFont = "italic"
else:
    whichFont = "roman"

weightDict = dict(
    tag="wght",
    name="Weight",
    values=[
        dict(value=300, name="Light"),
        # Glyphs includes both format 1 and 3 entries for Regular. Does not stop crash.
        dict(value=400, name="Regular", flags=0x2),
        dict(value=400, name="Regular", linkedValue=700, flags=0x2),
        dict(value=500, name="Medium"),
        dict(value=600, name="SmBold"),
        dict(value=700, name="Bold"),
        # dict(value=400, name="Regular", linkedValue=700, flags=0x2)
    ]
)

widthDict = dict(
    tag="wdth",
    name="Width",
    values=[
        dict(value=75, name="Cond"),
        dict(value=87.5, name="SmCond"),
        dict(value=100, name="Normal", flags=0x2),
        dict(value=112.5, name="SmExp"),
        dict(value=125, name="Exp"),
    ]
)

enlargeDict = dict(
    tag="ENLA",
    name="Enlarge",
    values=[
        dict(value=0, name="Normal", flags=0x2),
        dict(value=47, name="Enlarged"),
        dict(value=100, name="CapSize"),
    ]
)

format2RomanAxes = [
    weightDict, widthDict, enlargeDict,
    dict(
        tag="ital",
        name="Italic",
        values=[dict(value=0, name="Roman", linkedValue=1, flags=0x2)]
    )
]

format2ItalicAxes = [
    weightDict, widthDict, enlargeDict,
    dict(
        tag="ital",
        name="Italic",
        values=[dict(value=1, name="Italic")]
    )
]

ttfont = ttLib.TTFont(inFont)
if whichFont == "italic":
    print("Adding STAT table to italic font "  + inFont)
    builder.buildStatTable(ttfont,format2ItalicAxes)
    # Add stuff to name table. First the Variations PostScript Name Prefix (table entry 25).
    psNamePrefix = "JunicodeVFItalic"
    ttfont['name'].setName(psNamePrefix, 25, 3, 1, 0x409)
    # Glyphs takes care of the naming table now.
    # Cycle through fvar, getting instance names, building a correct postscriptNameID,
    # recording that in the name table, and adding the ID to the postscriptNameID field
    # of the fvar instance. Whew!
    for inst in ttfont['fvar'].instances:
        # if (inst.coordinates['wght'] == 400.0 and inst.coordinates['wdth'] == 100.0
        #    and inst.coordinates['ENLA'] == 0.0):
        if False:
            inst.subfamilyNameID = 2
            inst.postscriptNameID = 6
        else:
            subfamilyName = ttfont['name'].getName(inst.subfamilyNameID,3,1,0x409).toUnicode()
            if subfamilyName == "Italic":
                subfamilyName = "Regular"
            else:
                subfamilyName = subfamilyName.replace("Italic", "").replace(" ", "")
            # subfamilyName = subfamilyName.replace(" ", "")
            inst.postscriptNameID = ttfont['name'].addName("JunicodeVFItalic-" + subfamilyName,
                                                       platforms=((3,1,0x409),))
    # We don't need platform 1 names. If there are any, remove them.
    ttfont['name'].removeNames(platformID=1)
    ttfont.save(outFont)
elif whichFont == "roman":
    print("Adding STAT table to roman font " + inFont)
    ttfont = ttLib.TTFont(inFont)
    builder.buildStatTable(ttfont,format2RomanAxes)
    # Add stuff to name table. First the Variations PostScript Name Prefix (table entry 25).
    psNamePrefix = "JunicodeVFRoman"
    ttfont['name'].setName(psNamePrefix, 25, 3, 1, 0x409)
    # Cycle through fvar, getting instance names, building a correct postscriptNameID,
    # recording that in the name table, and adding the ID to the postscriptNameID field
    # of the fvar instance. Whew!
    for inst in ttfont['fvar'].instances:
        # if (inst.coordinates['wght'] == 400.0 and inst.coordinates['wdth'] == 100.0
        #     and inst.coordinates['ENLA'] == 0.0):
        if False:
            inst.subfamilyNameID = 2
            inst.postscriptNameID = 6
        else:
            subfamilyName = ttfont['name'].getName(inst.subfamilyNameID,3,1,0x409).toUnicode().replace(" ","")
            inst.postscriptNameID = ttfont['name'].addName("JunicodeVFRoman" + "-" + subfamilyName,
                                                       platforms=((3,1,0x409),))
    # We don't need platform 1 names. If there are any, remove them.
    ttfont['name'].removeNames(platformID=1)
    ttfont.save(outFont)