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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
#!/usr/bin/env fontforge
# Prerequisites:
#
# - fontforge: https://fontforge.github.io/en-US/
#
# Run using:
#
# $ ./subset-fonts.pe <source directory> <output directory>
#
# Example:
#
# $ ./subset-fonts.pe source-fonts ../data/fonts
#
# NOTE: Ignore "GID out of range" warnings; fontforge has to kick the tires a bit to flush out these dead references.
#
# Use with Noto Serif fonts from https://code.google.com/p/noto/source/browse/#svn%2Ftrunk%2Ffonts%2Findividual%2Funhinted
# Use with M+ fonts from http://mplus-fonts.sourceforge.jp/mplus-outline-fonts/download/index-en.html
#
# See Unicode Blocks: http://jrgraphix.net/research/unicode_blocks.php
#
# IMPORTANT: Must generate Apple format (0x10) or include old-fashioned 'kern' table for kerning to work in Prawn.
# Prawn misses some of the kern pairs when using the Apply format, so the old-fashioned 'kern' table is best (0x90).
# Keep in mind, though, that the old-fashioned 'kern' table only supported a limited number of pairs.
#
# generate flags (additive):
# * 0x00 - OpenType
# * 0x10 - Apple
# * 0x80 - OpenType and Apple
# * 0x90 - Neither OpenType or Apple (implicitly generates an old-style 'kern' table)
# * 0x800 - Generate old-style 'kern' table
# * 0x08 - exclude TrueType instructions
# * 0x04 - exclude PS glyph names; short format only (PDF readers use these names to guess characters when copying text)
# * 0x4000000 - drop glyphs with Unicode value of -1
#
# NOTE best choice for Prawn only is 0x90 + 0x08
# NOTE best choice for web only is 0x00 + 0x08
# NOTE best choice for Prawn & web is 0x00 + 0x800 + 0x08
genflags = 0x90 + 0x08
copy_fonts = ["NotoSerif-Regular.ttf", "NotoSerif-Bold.ttf", "NotoSerif-Italic.ttf", "NotoSerif-BoldItalic.ttf", "mplus-1p-regular.ttf"]
# FIXME reenable copy_scripts to be customized at top level; disabled due to support for mplus1p-regular-fallback
# copy_scripts = ["subset"]
code_fonts = ["mplus-1mn-light.ttf", "mplus-1mn-regular.ttf", "mplus-1mn-medium.ttf", "mplus-1mn-bold.ttf"]
code_scripts = ["ascii"]
if ($argc >= 2)
source_dir = $argv[1]
else
source_dir = "."
endif
if ($argc >= 3)
output_dir = $argv[2]
else
output_dir = "generated"
endif
fa_src_filepath = StrJoin([source_dir, "fontawesome-webfont.ttf"], "/")
mplus1p_src_filepath = StrJoin([source_dir, "mplus-1p-regular.ttf"], "/")
fi = 0
num_copy_fonts = SizeOf(copy_fonts)
while (fi < num_copy_fonts)
copy_font = copy_fonts[fi]
src_filepath = StrJoin([source_dir, copy_font], "/")
new_basename = ToLower(copy_font:r)
if (Strstr(new_basename, "bolditalic") >= 0)
new_basename = StrJoin(StrSplit(new_basename, "bolditalic"), "bold_italic")
endif
is_mplus = Strstr(new_basename, "mplus") >= 0
is_noto = Strstr(new_basename, "noto") >= 0
copy_scripts = ["subset"]
# remove hyphen from mplus-1 file basename
if (is_mplus)
new_basename = "mplus1" + StrJoin(StrSplit(new_basename, "mplus-1"), "")
copy_scripts = ["fallback"]
endif
si = 0
num_copy_scripts = SizeOf(copy_scripts)
while (si < num_copy_scripts)
script = copy_scripts[si]
new_filename = new_basename + "-" + script + ".ttf"
new_filepath = output_dir + "/" + new_filename
if (is_noto)
Print("Stealing glyphs from other fonts...")
if (new_basename == "notoserif-regular")
Print("Stealing ballot boxes from FontAwesome...")
# Grab ballot boxes from FontAwesome
Open(fa_src_filepath)
# relocate 0uf046 -> 0u2611
Select(0uf046); Copy(); Select(0u2611); Paste()
# relocate 0uf096 -> 0u2610
Select(0uf096); Copy(); Select(0u2610); Paste()
# select and copy ballot boxes
Select(0u2610,0u2611)
Copy()
Close()
Open(src_filepath)
Select(0u2610,0u2611)
Paste()
SetWidth(1664)
SelectNone()
# NOTE it shouldn't be necessary to write the file here, but for some reason it doesn't work otherwise in this script
Generate(new_filepath, "", genflags)
src_filepath = new_filepath
Close()
endif
Print("Stealing double arrows from M+ 1p...")
Open(mplus1p_src_filepath)
from_em = $em
SelectSingletons(0u21d0,0u21d2)
Copy()
Close()
Open(src_filepath)
SelectSingletons(0u21d0,0u21d2)
Paste()
# TODO to be strictly accurate, multiply $em by 1.0
scale_factor = ($em / from_em) * 100
Scale(scale_factor, scale_factor)
SetWidth(2048)
CenterInWidth()
# Move single and double arrows up to align with middle of X
SelectMoreSingletons(0u2190,0u2192)
Move(0, 380)
SelectNone()
Print("Done stealing glyphs from other fonts")
else
Open(src_filepath)
endif
SelectAll()
# Remove TrueType instructions (i.e., hinting)
ClearInstrs()
SelectNone()
# Basic Latin (e.g., English)
SelectMore(0u0020,0u007e)
# Latin-1 Supplement (covers core Western European languages)
SelectMore(0u00a1,0u00fd)
# Latin Extended-A (covers Czech, Dutch, Polish & Turkish, esp. names)
SelectMore(0u0100,0u017f)
# General Punctuation (most of it) (e.g., dashes, curved quotes, bullet, ellipsis)
SelectMore(0u2000,0u203a)
# More picky general punctuation
## Spaces
#SelectMore(0u2000,0u200b)
## Dashes
#SelectMore(0u2012,0u2015)
## Curved quotes
#SelectMore(0u2018,0u2019)
#SelectMore(0u201c,0u201d)
#SelectMore(0u2039,0u203a)
## Daggars
#SelectMore(0u2020,0u2021)
## Bullet
#SelectMore(0u2022)
## Ellipsis
#SelectMore(0u2026)
# Additional Currency Symbols
#SelectMore(0u20a0,0u20d0)
# ...or just the Euro sign
SelectMore(0u20ac)
# Trademark sign (selected from Letterlike Symbols set)
SelectMore(0u2122)
# Mathematical Operators (e.g., infinity, sum, partial differential)
SelectMore(0u2200,0u22ff)
# Geometric Shapes (e.g., list bullets)
SelectMore(0u25a0,0u25ff)
# Greek (frequently used for math and bullets)
SelectMore(0u0370,0u03ff)
if (is_noto)
# Single arrows (present in Noto Serif, but misaligned) and double arrows (imported from M+ 1p)
SelectMoreSingletons(0u2190,0u2192,0u21d0,0u21d2)
if (new_basename == "notoserif-regular")
# Ballot boxes (imported from FontAwesome)
SelectMore(0u2610,0u2611)
endif
# Check mark (missing from Noto Serif)
#SelectMore(0u2713)
endif
if (is_mplus)
# Single arrows
SelectMore(0u2190,0u2195)
# Double arrows
SelectMore(0u21d0,0u21d5)
# Ballot boxes
SelectMore(0u2610,0u2611)
# Check mark
SelectMore(0u2713)
endif
if (script == "latin-ext" || script == "latin-ext-cyrillic" || script == "fallback")
# Latin Extended-B
SelectMore(0u0180,0u024f)
# IPA Extensions (i.e., core phonetics)
#SelectMore(0u0250,0u02af)
# Upside-down e (from IPA Extensions)
SelectMore(0u0259)
# Spacing Modifier Letters (i.e., IPA tone marks, and modifiers for aspiration and palatalization) (missing from Noto Serif)
SelectMore(0u02b0,0u02ff)
# Latin Ligatures (e.g., fi) (Noto Serif doesn't auto-detect them, so leave them off)
#SelectMore(0ufb00,0ufb06)
endif
if (script == "latin-cyrillic" || script == "latin-ext-cyrillic" || script == "subset" || script == "fallback")
# Cyrillic
SelectMore(0u0400,0u04ff)
endif
if (script == "subset" || script == "fallback")
# Non-optimal selection for Vietnamese
# Latin Extended-A, Latin Extended Additional
#SelectMore(0u0100,0u017f)
#SelectMore(0u1e00,0u1eff)
# Optimal selection for Vietnamese (see http://blog.int3ractive.com/2010/06/optimal-unicode-range-for-vietnamese.html)
# NOTE Latin Extended-A may already included at this point, so 0u0102-0u0169 may be redundant
SelectMore(0u0102,0u0103)
SelectMore(0u0110,0u0111)
SelectMore(0u0128,0u0129)
SelectMore(0u0168,0u0169)
SelectMore(0u01a0,0u01b0)
SelectMore(0u1ea0,0u1ef9)
if (is_mplus)
# CJK Symbols and Punctuation (not present in mainstream Noto Serif fonts)
SelectMore(0u3000,0u303f)
# Hiragana
SelectMore(0u3040,0u309f)
# Katakana
SelectMore(0u30a0,0u30ff)
# Full-width roman characters and half-width katakana
SelectMore(0uff00,0uffef)
# CJK Unified Ideographs (for Japanese, aka kanji) (not present in mainstream Noto Serif fonts)
SelectMore(0u4e00,0u9faf)
endif
endif
#if (script == "fallback")
# # Cyrillic Supplement
# #SelectMore(0u0500,0u052f)
# # Greek Extended (i.e., Polytonic)
# #SelectMore(0u1f00,0u1fff)
# # Or just select all them symbols...
# SelectAll()
#endif
# BOM (zero-width no-break space) and no-break space
SelectMoreSingletons(0ufeff,0u00a0)
# Drop all glyphs that weren't selected
SelectInvert()
Clear()
SelectNone()
if (is_mplus)
# Generate BOM (zero-width no-break space), zero-width space, and word joiner from no-break space (for M+ fonts)
Select(0u00a0)
Copy()
SelectNone()
SelectSingletons(0ufeff,0u200b,0u2060)
Paste()
SetWidth(0)
SelectNone()
# Generate narrow no-break space from thin space (for M+ fonts)
Select(0u2009)
Copy()
SelectNone()
Select(0u202f)
Paste()
SetWidth(226)
# Generate hair space from thin space (for M+ fonts)
Select(0u2009)
Copy()
SelectNone()
Select(0u200a)
Paste()
SetWidth(94)
SelectNone()
else
# Generate word joiner from BOM (zero-width no-break space)
Select(0ufeff)
Copy()
Select(0u2060)
Paste()
SelectNone()
# Generate no-break hyphen from hyphen (for Noto Serif fonts)
Select(0u002d)
Copy()
SelectNone()
Select(0u2011)
Paste()
SelectNone()
# Fix width of .null character
#Select(0u0000)
#SetWidth(0)
#SelectNone()
endif
# Generate line feed from no-break space (works around error "cmap format 14 is not supported" in ttfunk)
# FIXME another option here is to select all the characters referenced by the cmap format 14 table
Select(0u00a0)
Copy()
SelectNone()
Select(0u000a)
Paste()
SetWidth(0)
SelectNone()
Print("Generating " + new_filename + "...")
Generate(new_filepath, "", genflags)
Close()
if (is_mplus)
# Regenerate font to drop invalid cmap format 14 table (ignore warnings)
Open(new_filepath)
Generate(new_filepath, "", genflags)
Close()
endif
si = si + 1
endloop
fi = fi + 1
endloop
fi = 0
num_code_fonts = SizeOf(code_fonts)
while (fi < num_code_fonts)
code_font = code_fonts[fi]
src_filepath = StrJoin([source_dir, code_font], "/")
new_basename_base = code_font:r
# remove hyphen from mplus-1 file basename
new_basename_base = "mplus1" + StrJoin(StrSplit(new_basename_base, "mplus-1"), "")
si = 0
num_code_scripts = SizeOf(code_scripts)
while (si < num_code_scripts)
new_basename = new_basename_base
script = code_scripts[si]
new_suffix = "-" + script + ".ttf"
Open(src_filepath)
SelectAll()
# NOTE: M+ fonts don't have hinting, so technically this is redundant
ClearInstrs()
SelectNone()
# Basic Latin (e.g., English)
SelectMore(0u0020,0u007e)
# No-break space
SelectMore(0u00a0)
# Box drawing symbols (for unix `tree` output)
SelectMore(0u2500,0u257f)
if (new_basename == "mplus1mn-regular")
# Enclosed numbers (1-20 circled and filled)
SelectMore(0u2460,0u2473)
SelectMore(0u2776,0u277f)
SelectMore(0u24eb,0u24f4)
if (script == "ascii")
new_suffix = "-" + script + "-conums.ttf"
endif
endif
SelectInvert()
Clear()
SelectNone()
SetFontNames(new_basename, "M+ 1mn")
# repurpose light as italic
if (new_basename == "mplus1mn-light")
SetFontNames("mplus1mn-italic", "M+ 1mn", "M+ 1mn Italic")
SetOS2Value("Weight", 400)
SetPanose(2, 5)
SetTTFName(0x409, 2, "Italic")
SetTTFName(0x409, 16, "")
SetTTFName(0x409, 17, "")
SetTTFName(0x411, 16, "")
SetTTFName(0x411, 17, "")
new_basename = "mplus1mn-italic"
# repurpose medium as bold
elseif (new_basename == "mplus1mn-medium")
SetFontNames("mplus1mn-bold", "M+ 1mn", "M+ 1mn Bold")
SetOS2Value("Weight", 700)
SetPanose(2, 8)
SetTTFName(0x409, 2, "Bold")
SetTTFName(0x409, 16, "")
SetTTFName(0x409, 17, "")
SetTTFName(0x411, 16, "")
SetTTFName(0x411, 17, "")
new_basename = "mplus1mn-bold"
# repurpose bold as bold italic
elseif (new_basename == "mplus1mn-bold")
SetFontNames("mplus1mn-bold_italic", "M+ 1mn", "M+ 1mn Bold Italic")
SetOS2Value("Weight", 700)
SetPanose(2, 8)
SetTTFName(0x409, 2, "Bold Italic")
SetTTFName(0x409, 16, "")
SetTTFName(0x409, 17, "")
SetTTFName(0x411, 16, "")
SetTTFName(0x411, 17, "")
new_basename = "mplus1mn-bold_italic"
endif
## Adjust width of box drawing symbols (not working)
#Select(0u2500,0u257f)
#SetWidth(50, 2)
#SelectNone()
# Generate BOM (zero-width no-break space) from no-break space
Select(0u00a0)
Copy()
SelectNone()
Select(0ufeff)
Paste()
SetWidth(0)
SelectNone()
# Generate line feed from no-break space
Select(0u00a0)
Copy()
SelectNone()
Select(0u000a)
Paste()
SetWidth(0)
SelectNone()
new_filename = new_basename + new_suffix
new_filepath = output_dir + "/" + new_filename
Print("Generating " + new_filename + "...")
Generate(new_filepath, "", genflags)
Close()
# Regenerate font to drop invalid cmap format 14 table (ignore warnings)
Open(new_filepath)
Generate(new_filepath, "", genflags)
Close()
si = si + 1
endloop
fi = fi + 1
endloop
|