-
Notifications
You must be signed in to change notification settings - Fork 1
/
fontbuild.py
149 lines (127 loc) · 4.09 KB
/
fontbuild.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
#!/usr/bin/env python3
"""Build saltcandy123font from SVG files"""
import argparse
import pathlib
import re
import xml.dom.minidom
import fontforge
BASE_DIR = pathlib.Path(__file__).parent.parent
def build_saltcandy123font(*, version: str) -> fontforge.font:
font = fontforge.font()
font.fontname = "saltcandy123font"
font.fullname = font.fontname
font.familyname = font.fontname
font.copyright = "Copyright (c) 2021-2024 saltcandy123"
font.weight = "Regular"
font.os2_weight = 400
font.version = version
font.encoding = "UnicodeBmp"
font.hasvmetrics = 1 # required to enable vwidth settings
font.ascent = 800
font.descent = 200
font.em = 1000
font.hhea_linegap = font.os2_typolinegap = 0
font.os2_stylemap |= 0b1000000 # OS/2 fsSelection REGULAR bit
with open(BASE_DIR.joinpath("OFL.txt")) as f:
license = f.read().strip()
en_us = 0x0409
font.appendSFNTName(en_us, 13, license)
font.appendSFNTName(en_us, 14, "http://scripts.sil.org/OFL")
# Add lookup subtable for vertical writing
font.addLookup(
"gsub_vert",
"gsub_single",
(),
(
(
"vert",
(("DFLT", ("dflt",)),),
),
),
)
font.addLookupSubtable("gsub_vert", "vert")
glyphs_dir = BASE_DIR.joinpath("glyphs")
all_svg_paths = list(glyphs_dir.glob("**/*.svg"))
outline_import_options = {
"scale": False,
}
# Import base glyphs (e.g. u3041.svg)
for svg_path in all_svg_paths:
match = re.search("^u([0-9a-f]+).svg$", svg_path.name)
if not match:
continue
code = int(match.group(1), 16)
glyph = font.createChar(code)
glyph.importOutlines(str(svg_path), **outline_import_options)
glyph.width, glyph.vwidth = read_svg_size(svg_path)
# Import replacement glyphs (e.g. u3041-vert.svg)
for svg_path in all_svg_paths:
match = re.search("^u([0-9a-f]+)-([a-z]+).svg$", svg_path.name)
if not match:
continue
code = int(match.group(1), 16)
subtable_name = match.group(2)
base_glyph = font[code]
glyph_name = f"{base_glyph.glyphname}.{subtable_name}"
glyph = font.createChar(-1, glyph_name)
glyph.importOutlines(str(svg_path), **outline_import_options)
glyph.width, glyph.vwidth = read_svg_size(svg_path)
base_glyph.addPosSub(subtable_name, glyph_name)
# Simplify all the glyphs
font.selection.all()
font.removeOverlap()
font.simplify(
1,
(
"ignoreslopes",
"ignoreextrema",
"smoothcurves",
"choosehv",
"forcelines",
"nearlyhvlines",
"mergelines",
"setstarttoextremum",
"removesingletonpoints",
),
)
return font
def read_svg_size(path_to_svg: pathlib.Path) -> tuple[int, int]:
with xml.dom.minidom.parse(str(path_to_svg)) as doc:
viewbox = doc.childNodes[0].getAttribute("viewBox")
_, _, width, height = [int(n) for n in viewbox.replace(",", " ").split()]
return width, height
def generate_file(*, font: fontforge.font, path: pathlib.Path) -> None:
font.generate(
str(path),
flags=(
"opentype",
"old-kern",
"winkern",
"dummy-dsig",
"no-FFTM-table",
"round", # It's needed to reduce file size of woff
"no-hints",
"no-flex",
"omit-instructions",
),
)
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"-o",
metavar="FILE",
default="saltcandy123font-Regular.ttf",
type=pathlib.Path,
help="write a font to FILE",
)
parser.add_argument(
"-t",
metavar="FONT_VERSION",
default="(None)",
help="build a font as FONT_VERSION",
)
args = parser.parse_args()
font = build_saltcandy123font(version=args.t)
generate_file(font=font, path=args.o)
if __name__ == "__main__":
main()