Debian Codegraph @

sources/build.py

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
1 from fontTools.ttLib import TTFont, newTable
2 from fontTools.ttLib.tables import _c_m_a_p
3 from fontTools import merge
4 from pathlib import Path
5 import glob
6 import fontmake.__main__
7 import os, shutil, subprocess
8
9 # Morisawa UD font merging script
10 # For these particular fonts, we have chosen to preserve the existing TTFs as delivered directly from Morisawa, and merge the additional glyphs via a build script.
11 #
12 # This script converts the "extensions" Glyphs file to TTF, autohints it, and then merges it into the existing TTF file via psftmerge. There's some oddities in this process which have to be solved in post-production at the end of the process due to tables getting dropped / metadata being set differently.
13
14 os.mkdir("temp")
15 os.mkdir("fonts")
16 os.mkdir("fonts/ttf")
17
18 IMPORT = Path('sources/ttf')
19 TEMP = Path('temp')
20 EXPORT = Path('fonts/ttf')
21 SRC_IMPORT = Path("sources/extensions")
22 VERSION = "1.06"
23
24 for font in IMPORT.glob("*.ttf"):
25
26 # Step 1 - create ttf for extension to the font
27 fontName = str(font).split("/")[2]
28 sourceTTF = TTFont(font)
29
30 if "Medium" in fontName:
31 fontName = fontName.replace("-Medium","") #switch to follow the pattern of the Regulars
32
33 fontmake.__main__.main([
34 "-g", str(SRC_IMPORT / str(fontName[:-4]+"Ext.glyphs")),
35 "-o", "ttf",
36 "--output-dir",
37 str(TEMP),
38 ])
39
40 if "Heavy" in fontName:
41 extSource = str(TEMP / str(fontName[:-4].replace("-Heavy","Ext-Heavy"+".ttf")))
42 outputTTF = str(fontName)
43 else:
44 extSource = str(TEMP / str(fontName[:-4]+"Ext-Regular.ttf"))
45 outputTTF = str(fontName[:-4]+"-Regular.ttf")
46
47
48 # For some reason, the autohinter built into fontmake is not working, so I'm doing it separately here which works
49 subprocess.check_call(
50 [
51 "ttfautohint",
52 "--symbol",
53 "--reference="+str(font),
54 extSource,
55 extSource[:-4] + "-hinted.ttf",
56 ]
57 )
58
59 # Here I'm stripping the .notdef chararter to avoid it showing up in the final font
60 subprocess.check_call(
61 [
62 "pyftsubset",
63 extSource[:-4] + "-hinted.ttf",
64 "--glyphs=*"
65 ]
66 )
67
68 shutil.move(extSource[:-4] + "-hinted.subset.ttf", extSource)
69 os.remove(extSource[:-4] + "-hinted.ttf")
70
71 # Step 2 - Some metadata changes to align with GF expectations
72 # NAME table modifications
73 sourceTTF["name"].removeNames(platformID=1)
74
75 for platformID in [1033, 1041]:
76
77 if "Medium" in sourceTTF["name"].getDebugName(1):
78 sourceTTF["OS/2"].usWeightClass = 400
79 sourceTTF["name"].removeNames(nameID=16)
80 sourceTTF["name"].removeNames(nameID=17)
81 sourceTTF["OS/2"].panose.bWeight = 4
82
83 name = str(sourceTTF["name"].getName(1,3,1,platformID)).replace(" Medium","")
84
85 sourceTTF["name"].setName("Copyright 2022 The BIZ UDMincho Project Authors (https://github.com/googlefonts/morisawa-biz-ud-mincho)",0,3,1,platformID)
86 sourceTTF["name"].setName(name,1,3,1,platformID)
87 sourceTTF["name"].setName("Version "+VERSION,5,3,1,platformID)
88
89 sourceTTF["name"].setName(str(sourceTTF["name"].getName(4,3,1,platformID)).replace(" Medium",""),4,3,1,platformID)
90
91 if "Heavy" in fontName: #aligning psnames with google standards. Shouldn't impact compatibility.
92 sourceTTF["OS/2"].usWeightClass = 700
93 sourceTTF["OS/2"].panose.bWeight = 7
94 sourceTTF["name"].setName(str(sourceTTF["name"].getName(1,3,1,platformID)).replace(" Heavy",""),1,3,1,platformID)
95 sourceTTF["name"].setName("Bold",2,3,1,platformID)
96 sourceTTF["name"].setName(str(sourceTTF["name"].getName(3,3,1,platformID)).replace("Heavy","Bold"),3,3,1,platformID)
97 sourceTTF["name"].setName(str(sourceTTF["name"].getName(4,3,1,platformID)).replace("Heavy","Bold"),4,3,1,platformID)
98 sourceTTF["name"].setName(str(sourceTTF["name"].getName(1,3,1,1033)).replace("BIZ ","BIZ")+"-Bold",6,3,1,platformID)
99 if platformID != 1041:
100 sourceTTF["name"].setName(str(sourceTTF["name"].getName(7,3,1,platformID)).replace("Heavy","Bold"),7,3,1,platformID)
101 sourceTTF["name"].removeNames(nameID=16)
102 sourceTTF["name"].removeNames(nameID=17)
103
104 else:
105 sourceTTF["name"].setName(str(sourceTTF["name"].getName(1,3,1,1033)).replace("BIZ ","BIZ")+"-Regular",6,3,1,platformID)
106
107 if platformID == 1033:
108 sourceTTF["name"].setName(name+" is a trademark of Morisawa Inc.",7,3,1,platformID)
109 sourceTTF["name"].setName("This Font Software is licensed under the SIL Open Font License, Version 1.1. This license is available with a FAQ at: https://scripts.sil.org/OFL",13,3,1,platformID)
110 sourceTTF["name"].setName("https://scripts.sil.org/OFL",14,3,1,platformID)
111
112 sourceTTF["name"].removeNames(nameID=6,platformID=3, langID=1041)
113
114
115 # OS/2 Table modifications
116 sourceTTF["OS/2"].fsType = 0
117
118 #Step 4 - Export updated source version
119 sourceTTF.save(TEMP / outputTTF)
120
121 # Step 5 - Merge source with extentions
122
123 subprocess.check_call(
124 [
125 "pyftmerge",
126 str(TEMP/outputTTF),
127 str(extSource),
128 "--output-file="+str(EXPORT / str(outputTTF).replace("BIZ-","BIZ").replace("Heavy","Bold")),
129 ]
130 )
131
132 # Due to merging, some things get messed up:
133 # The meta table is dropped
134 # the Unicode cmap tables are dropped
135 # fixedPitch set incorrectly
136 # also tweaks to OS/2 and head also necessary to match original
137
138 finalVersion = TTFont(str(EXPORT / str(outputTTF).replace("BIZ-","BIZ")).replace("Heavy","Bold"))
139 finalVersion["meta"] = sourceTTF["meta"]
140
141 cmap0_3_4 = _c_m_a_p.CmapSubtable.newSubtable(4)
142
143 cmap0_3_4.platformID = 0
144 cmap0_3_4.platEncID = 3
145 cmap0_3_4.language = 0
146
147 cmap0_3_4.cmap = finalVersion["cmap"].getcmap(3,1).cmap #copying from merged version
148
149 finalVersion["cmap"].tables.append(cmap0_3_4)
•150 # format_14 (platformID=0, platEncID=5 UVS) should not be duplicated
151 if not any((table.platformID == 0 and table.platEncID == 5) for table in finalVersion["cmap"].tables):
152 finalVersion["cmap"].tables.append(sourceTTF["cmap"].getcmap(0,5))
153
154 if "P" not in fontName:
155 finalVersion["post"].isFixedPitch = 1
156 #finalVersion["OS/2"].panose.bProportion = 9
157
158 finalVersion["head"].fontRevision = float(VERSION)
159 finalVersion["head"].flags = 0x000b
160
161 newDSIG = newTable("DSIG")
162 newDSIG.ulVersion = 1
163 newDSIG.usFlag = 0
164 newDSIG.usNumSigs = 0
165 newDSIG.signatureRecords = []
166 finalVersion.tables["DSIG"] = newDSIG
167
168 finalVersion["OS/2"].fsType = 0
169
170 finalVersion.save(EXPORT / str(outputTTF).replace("BIZ-","BIZ").replace("Heavy","Bold"))
171
172 #shutil.rmtree("instance_ufo")
•173 try:
174 shutil.rmtree("master_ufo")
175 except Exception as e:
176 print(e)
177 try:
178 shutil.rmtree("temp")
179 except Exception as e:
180 print(e)