| 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 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 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 |
|
| 27 |
fontName = str(font).split("/")[2] |
| 28 |
sourceTTF = TTFont(font) |
| 29 |
|
| 30 |
if "Medium" in fontName: |
| 31 |
fontName = fontName.replace("-Medium","") |
| 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 |
|
| 49 |
subprocess.check_call( |
| 50 |
[ |
| 51 |
"ttfautohint", |
| 52 |
"--symbol", |
| 53 |
"--reference="+str(font), |
| 54 |
extSource, |
| 55 |
extSource[:-4] + "-hinted.ttf", |
| 56 |
] |
| 57 |
) |
| 58 |
|
| 59 |
|
| 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 |
|
| 72 |
|
| 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: |
| 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 |
|
| 116 |
sourceTTF["OS/2"].fsType = 0 |
| 117 |
|
| 118 |
|
| 119 |
sourceTTF.save(TEMP / outputTTF) |
| 120 |
|
| 121 |
|
| 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 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 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 |
| 148 |
|
| 149 |
finalVersion["cmap"].tables.append(cmap0_3_4) |
| •150 |
|
| 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 |
|
| 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 |
|
| •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) |