-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.py
180 lines (150 loc) · 6.55 KB
/
build.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from glyphsLib.cli import main
from fontTools.ttLib import newTable, TTFont
import shutil
import subprocess
import multiprocessing
import multiprocessing.pool
from pathlib import Path
import glob
import argparse
import ufo2ft
import ufoLib2
import os
def GASP_set(font:TTFont):
if "gasp" not in font:
font["gasp"] = newTable("gasp")
font["gasp"].gaspRange = {}
if font["gasp"].gaspRange != {65535: 0x000A}:
font["gasp"].gaspRange = {65535: 0x000A}
def fontExport(name: str, sources:Path, path:Path):
for file in list(path.glob("*.ufo")):
fontName = str(file).split("/")[2].split('.')[0]
print ("["+fontName+"] Processing")
exportfont = ufoLib2.Font.open(file)
variant = "Regular"
if "Medium" in str(file):
variant = "Medium"
elif "ExtraBold" in str(file):
variant = "ExtraBold"
elif "Bold" in str(file):
variant = "Bold"
sharedFont = ufoLib2.Font.open(sources / "ufo_shared" / str("KaiseiShared-"+variant+".ufo"))
print ("["+fontName+"] Importing shared glyphs")
for glyph in sharedFont:
exportfont.addGlyph(sharedFont[glyph.name])
print ("["+fontName+"] Adding feature code")
featureSet = sources / "features.fea"
exportfont.features.text = featureSet.read_text()
print ("["+fontName+"] Compiling")
static_ttf = ufo2ft.compileTTF(exportfont)
if name == "haruno":
static_ttf["name"].addMultilingualName({'ja':'解星-春の海'}, static_ttf, nameID = 1, windows=True, mac=False)
elif name == "tokumin":
static_ttf["name"].addMultilingualName({'ja':'解星-特ミン'}, static_ttf, nameID = 1, windows=True, mac=False)
elif name == "opti":
static_ttf["name"].addMultilingualName({'ja':'解星-オプティ'}, static_ttf, nameID = 1, windows=True, mac=False)
elif name == "decol":
static_ttf["name"].addMultilingualName({'ja':'解星-デコール'}, static_ttf, nameID = 1, windows=True, mac=False)
if "Medium" in str(file):
static_ttf["OS/2"].usWeightClass = 500
elif "ExtraBold" in str(file):
static_ttf["OS/2"].usWeightClass = 800
elif "Bold" in str(file):
static_ttf["OS/2"].usWeightClass = 700
print ("["+fontName+"] Adding stub DSIG")
static_ttf["DSIG"] = newTable("DSIG") #need that stub dsig
static_ttf["DSIG"].ulVersion = 1
static_ttf["DSIG"].usFlag = 0
static_ttf["DSIG"].usNumSigs = 0
static_ttf["DSIG"].signatureRecords = []
static_ttf["head"].flags |= 1 << 3 #sets flag to always round PPEM to integer
print ("["+fontName+"] Merging BASE")
static_ttf["BASE"] = newTable("BASE")
base = TTFont()
if variant == "Medium" or variant == "Regular":
base.importXML(sources / "BASE_regular.ttx")
else:
base.importXML(sources / str("BASE_"+variant.lower()+".ttx"))
static_ttf["BASE"] = base["BASE"]
print ("["+fontName+"] Saving")
os.makedirs("fonts/ttf/"+name, exist_ok=True)
outputName = "fonts/ttf/"+name+"/Kaisei"+name.capitalize()+"-"+variant+".ttf"
if name == "haruno":
outputName = "fonts/ttf/"+name+"/KaiseiHarunoUmi-"+variant+".ttf"
GASP_set(static_ttf)
static_ttf.save(outputName)
shutil.rmtree(str(path))
def execute(name:str, sources:Path):
print ("Generating "+name+" UFO")
outputPath = sources / str("ufo_"+name)
os.makedirs(outputPath, exist_ok=True)
glyphsName = "Kaisei-"+name.capitalize()+".glyphs"
if name == "haruno":
glyphsName = "Kaisei-HarunoUmi.glyphs"
main(("glyphs2ufo", str(sources / glyphsName), "-m", str(outputPath)))
fontExport(name, sources, outputPath)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="build FontKai fonts")
parser.add_argument("-D", "--decol", action="store_true", dest="decol", help="Export the Decol font")
parser.add_argument("-H", "--haruno", action="store_true", dest="haruno", help="Export the HarunoUmi font")
parser.add_argument("-O", "--opti", action="store_true", dest="opti", help="Export the Opti font")
parser.add_argument("-T", "--tokumin", action="store_true", dest="tokumin", help="Export the Tokumin font")
parser.add_argument("-A", "--all", action="store_true", dest="all", help="Export all fonts")
parser.add_argument("-S", "--shared", action="store_true", dest="shared", help="Regenerate the shared source")
args = parser.parse_args()
sources = Path("sources")
if args.all:
args.decol = True
args.haruno = True
args.opti = True
args.tokumin = True
if args.decol or args.haruno or args.opti or args.tokumin or args.shared:
os.makedirs("sources/ufo_shared", exist_ok=True)
if args.shared:
print ("Generating shared UFO")
if os.path.isfile(sources / "Kaisei-Shared.glyphs"):
main(("glyphs2ufo", str(sources / "Kaisei-Shared.glyphs"), "-m", str(sources / "ufo_shared")))
else:
print ("Cannot locate the 'shared' Glyphs file. Please confirm the file is unzipped.")
pool = multiprocessing.pool.Pool(processes=multiprocessing.cpu_count())
processes = []
if args.decol:
processes.append(
pool.apply_async(
execute,
("decol", sources)
)
)
if args.haruno:
processes.append(
pool.apply_async(
execute,
("haruno",sources)
)
)
if args.opti:
processes.append(
pool.apply_async(
execute,
("opti",sources)
)
)
if args.tokumin:
processes.append(
pool.apply_async(
execute,
("tokumin",sources)
)
)
pool.close()
pool.join()
for process in processes:
process.get()
del processes, pool
elif args.shared:
if os.path.isfile(sources / "Kaisei-Shared.glyphs"):
main(("glyphs2ufo", str(sources / "Kaisei-Shared.glyphs"), "-m", str(sources / "ufo_shared")))
else:
print ("Cannot locate the 'shared' Glyphs file. Please confirm the file is unzipped.")
else:
print ("No fonts selected for export")