| 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.051" |
| 23 |
|
| 24 |
for font in IMPORT.glob("*.ttf"): |
| 25 |
|
| 26 |
|
| 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 |
|
| 46 |
subprocess.check_call( |
| 47 |
[ |
| 48 |
"ttfautohint", |
| 49 |
"--symbol", |
| 50 |
"--reference="+str(font), |
| 51 |
extSource, |
| 52 |
extSource[:-4] + "-hinted.ttf", |
| 53 |
] |
| 54 |
) |
| 55 |
|
| 56 |
|
| 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 |
|
| 69 |
|
| 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: |
| 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 |
|
| 91 |
sourceTTF["OS/2"].fsType = 0 |
| 92 |
|
| 93 |
|
| 94 |
sourceTTF.save(TEMP / outputTTF) |
| 95 |
|
| 96 |
|
| 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 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 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 |
| 123 |
|
| 124 |
finalVersion["cmap"].tables.append(cmap0_3_4) |
| •125 |
|
| 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 |
|
| 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) |