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.051"
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 fontmake.__main__.main([
31 "-g", str(SRC_IMPORT / str(fontName[:-4]+"Ext.glyphs")),
32 "-o", "ttf",
33 "--output-dir",
34 str(TEMP),
35 ])
36
37 if "Bold" in fontName:
38 extSource = str(TEMP / str(fontName[:-4].replace("-Bold","Ext-Bold"+".ttf")))
39 outputTTF = str(fontName)
40 else:
41 extSource = str(TEMP / str(fontName[:-4]+"Ext-Regular.ttf"))
42 outputTTF = str(fontName[:-4]+"-Regular.ttf")
43
44
45 # For some reason, the autohinter built into fontmake is not working, so I'm doing it separately here which works
46 subprocess.check_call(
47 [
48 "ttfautohint",
49 "--symbol",
50 "--reference="+str(font),
51 extSource,
52 extSource[:-4] + "-hinted.ttf",
53 ]
54 )
55
56 # Here I'm stripping the .notdef chararter to avoid it showing up in the final font
57 subprocess.check_call(
58 [
59 "pyftsubset",
60 extSource[:-4] + "-hinted.ttf",
61 "--glyphs=*"
62 ]
63 )
64
65 shutil.move(extSource[:-4] + "-hinted.subset.ttf", extSource)
66 os.remove(extSource[:-4] + "-hinted.ttf")
67
68 # Step 2 - Some metadata changes to align with GF expectations
69 # NAME table modifications
70 sourceTTF["name"].removeNames(platformID=1)
71
72 for platformID in [1033, 1041]:
73 name = str(sourceTTF["name"].getName(1,3,1,platformID))
74
75 sourceTTF["name"].setName("Copyright 2022 The BIZ UDGothic Project Authors (https://github.com/googlefonts/morisawa-biz-ud-gothic)",0,3,1,platformID)
76 sourceTTF["name"].setName("Version "+VERSION,5,3,1,platformID)
77
78 if "Bold" in fontName: #aligning psnames with google standards. Shouldn't impact compatibility.
79 sourceTTF["name"].setName(name.replace("BIZ ","BIZ")+"-Bold",6,3,1,platformID)
80 else:
81 sourceTTF["name"].setName(name.replace("BIZ ","BIZ")+"-Regular",6,3,1,platformID)
82
83 if platformID == 1033:
84 sourceTTF["name"].setName(name+" is a trademark of Morisawa Inc.",7,3,1,platformID)
85 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)
86 sourceTTF["name"].setName("https://scripts.sil.org/OFL",14,3,1,platformID)
87
88 sourceTTF["name"].removeNames(nameID=6,platformID=3, langID=1041)
89
90 # OS/2 Table modifications
91 sourceTTF["OS/2"].fsType = 0
92
93 #Step 4 - Export updated source version
94 sourceTTF.save(TEMP / outputTTF)
95
96 # Step 5 - Merge source with extentions
97
98 subprocess.check_call(
99 [
100 "pyftmerge",
101 str(TEMP/outputTTF),
102 str(extSource),
103 "--output-file="+str(EXPORT / str(outputTTF).replace("BIZ-","BIZ")),
104 ]
105 )
106
107 # Due to merging, some things get messed up:
108 # The meta table is dropped
109 # the Unicode cmap tables are dropped
110 # fixedPitch set incorrectly
111 # also tweaks to OS/2 and head also necessary to match original
112
113 finalVersion = TTFont(str(EXPORT / str(outputTTF).replace("BIZ-","BIZ")))
114 finalVersion["meta"] = sourceTTF["meta"]
115
116 cmap0_3_4 = _c_m_a_p.CmapSubtable.newSubtable(4)
117
118 cmap0_3_4.platformID = 0
119 cmap0_3_4.platEncID = 3
120 cmap0_3_4.language = 0
121
122 cmap0_3_4.cmap = finalVersion["cmap"].getcmap(3,1).cmap #copying from merged version
123
124 finalVersion["cmap"].tables.append(cmap0_3_4)
•125 # format_14 (platformID=0, platEncID=5 UVS) should not be duplicated
126 if not any((table.platformID == 0 and table.platEncID == 5) for table in finalVersion["cmap"].tables):
127 finalVersion["cmap"].tables.append(sourceTTF["cmap"].getcmap(0,5))
128
129 if "P" not in fontName:
130 finalVersion["post"].isFixedPitch = 1
131 #finalVersion["OS/2"].panose.bProportion = 9
132
133 finalVersion["head"].fontRevision = float(VERSION)
134
135 finalVersion["head"].flags = 0x000b
136
137 newDSIG = newTable("DSIG")
138 newDSIG.ulVersion = 1
139 newDSIG.usFlag = 0
140 newDSIG.usNumSigs = 0
141 newDSIG.signatureRecords = []
142 finalVersion.tables["DSIG"] = newDSIG
143
144
145 finalVersion.save(EXPORT / str(outputTTF).replace("BIZ-","BIZ"))
146
•147 try:
148 shutil.rmtree("instance_ufo")
149 except Exception as e:
150 print(e)
151 try:
152 shutil.rmtree("master_ufo")
153 except Exception as e:
154 print(e)
155 try:
156 shutil.rmtree("temp")
157 except Exception as e:
158 print(e)