-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathafinate
executable file
·165 lines (129 loc) · 5.49 KB
/
afinate
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
# afinate tool to create Alright Font font files
#
# AF is the "Alright Fonts Format" for low resource platforms, it aims
# to provide a way for microcontroller based projects to render high quality
# vector fonts.
import sys, argparse, struct, math, builtins
from python_alright_fonts import Glyph, Point, Encoder
# parse command line arguments
# ===========================================================================
parser = argparse.ArgumentParser(description="Create an Alright Font (.af) file containing the specified set of glyphs.")
parser.add_argument("--font", type=argparse.FileType("rb"), required=True, help="the font (.ttf or .otf) that you want to extract glyphs from")
parser.add_argument("--format", type=str, default="af", choices=["af", "c", "python"], help="the output format (either 'af', 'c', or 'python'")
parser.add_argument("--quality", type=str, choices=["low", "medium", "high"], default="medium", help="the quality of decomposed bezier curves - affects font file size. (default: \"medium\")")
parser.add_argument("--characters", type=str, help="the list of characters that you want to extract. (default: ASCII character set)")
parser.add_argument("--corpus", type=argparse.FileType("r"), help="corpus to select characters from")
parser.add_argument("--quiet", action="store_true", help="suppress all progress and debug messages")
parser.add_argument("out", type=str, help="the output filename")
args = parser.parse_args()
# override print() to allow quiet mode to suppress output
def print(*a, **kw):
if not args.quiet:
builtins.print(*a, **kw)
# load the requested font file
# ===========================================================================
print("> loading font", args.font.name)
quality_map = {
"low": 25,
"medium": 5,
"high": 1
}
try:
encoder = Encoder(args.font, quality=quality_map[args.quality])
except:
print("Failed to load font - stopping.")
sys.exit(1)
# determine glyph metric and coordinate scaling
# ===========================================================================
# the bounding box defines the largest bounding box of all glyphs
print(" - bounding box {}, {} -> {}, {}".format(
encoder.bbox_l, encoder.bbox_t, encoder.bbox_r, encoder.bbox_b))
# then upscale from the normalised scale to our preferred scale
print(" - scale factor {}".format(round(encoder.scale_factor, 3)))
# get the list of character code points or ascii character codes
# ===========================================================================
# default to the standard ascii character set
if not args.characters and not args.corpus:
character_codepoints = [i for i in range(0, 128)]
# user supplied list of characters as command line argument
if args.characters:
character_codepoints = sorted([ord(c) for c in set(args.characters)])
# user supplied corpus file to extract characters from
if args.corpus:
while True:
line = args.corpus.readline()
if not line:
break
character_codepoints = sorted(set(character_codepoints + [ord(c) for c in line]))
# extract the glyph metrics and contours for all printable glyphs
# ===========================================================================
print("> extracting {} glyphs".format(len(character_codepoints)))
glyphs = {}
contours = {}
printable_count = 0
for codepoint in character_codepoints:
glyph = encoder.get_glyph(codepoint)
if glyph:
print(" \\u{:04} {} : {:>2} contours / {:>3} points".format(
codepoint,
"'" + chr(codepoint) + "'",
len(glyph.contours),
sum([len(c) for c in glyph.contours])
))
printable_count += 1
else:
print(" \\u{:04} missing or not printable, skipping".format(codepoint))
# write glyph dictionary to file
print("> write output file in {} format".format(format))
if len(encoder.glyphs) == 0:
print("No printable glyphs - stopping.")
sys.exit(1)
# write header to file
print(" - header")
result = bytes()
result += b"af!?"
result += struct.pack(">H", len(encoder.glyphs))
result += struct.pack(">H", 0) # no flags to set
print(" - glyph dictionary")
for codepoint, glyph in encoder.glyphs.items():
result += encoder.get_packed_glyph(glyph)
print(" - glyph contours")
for codepoint, glyph in encoder.glyphs.items():
result += encoder.get_packed_glyph_contours(glyph)
# write out the resulting paf font file in requested format
# ===========================================================================
with open(args.out, "wb") as outfile:
# Alright Fonts binary font file
if args.format == "af":
outfile.write(result)
# c(++) const array
if args.format == "c":
outfile.write(b"const unsigned char _font[] = {\n")
i = 0
while i < len(result):
line = result[i:i + 12]
outfile.write(b" ")
for char in line:
outfile.write("0x{:02x}".format(char).encode("ascii"))
if i < len(result) - 1:
outfile.write(b", ")
i += 1
outfile.write(b"\n")
outfile.write(b"};\n")
# python array
if args.format == "python":
ooutfileut.write(b"_font = bytes([\n")
i = 0
while i < len(result):
line = result[i:i + 12]
outfile.write(b" ")
for char in line:
outfile.write("0x{:02x}".format(char).encode("ascii"))
if i < len(result) - 1:
outfile.write(b", ")
i += 1
outfile.write(b"\n")
outfile.write(b"])\n")
pass
print("> output file size {} bytes and contains {} characters (avg. {} bytes per character)".format(len(result), printable_count, int(len(result) / printable_count)))