summaryrefslogtreecommitdiff
path: root/scripts/generate-font-subsets.pe
blob: 453b037e55390b53ad8ddd0b75f23bac5534ee58 (plain) (blame)
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
#!/usr/bin/env fontforge

# Run using:
#
# $ ./generate-font-subsets.pe <target directory>
#
# By default, the target directory is "generated".
#
# 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
#
# TODO: Add cjkv profile
#
# IMPORTANT: Must generate Apple format (0x10) or include old-fashioned 'kern' table for kerning to work in Prawn
#
# generate flags (additive):
# * 0x00 - OpenType
# * 0x10 - Apple
# * 0x80 - OpenType and Apple
# * 0x90 - Neither OpenType or Apple
# * 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)
#
# QUESTION: do we need to apply RoundToInt() after ClearInstrs()?
# FIXME: need to generate cjk font

genflags = 0x08
#genflags = 0x10 + 0x08
copy_fonts = ["NotoSerif-Regular.ttf", "NotoSerif-Bold.ttf", "NotoSerif-Italic.ttf", "NotoSerif-BoldItalic.ttf", "mplus-1p-regular.ttf", "mplus-1p-bold.ttf", "mplus-1p-light.ttf"]
#copy_fonts = ["NotoSerif-Regular.ttf", "NotoSerif-Bold.ttf", "NotoSerif-Italic.ttf", "NotoSerif-BoldItalic.ttf"]
#copy_fonts = ["LiberationMono-Regular.ttf", "LiberationMono-Bold.ttf", "LiberationMono-Italic.ttf", "LiberationMono-BoldItalic.ttf"]
#copy_fonts = ["NotoSerif-Regular.ttf"]
#copy_fonts = []
num_copy_fonts = SizeOf(copy_fonts)
copy_scripts = ["latin", "latin-ext", "latin-cyrillic", "multilingual"]
#copy_scripts = ["latin"]
num_copy_scripts = SizeOf(copy_scripts)
code_fonts = ["mplus-1mn-light.ttf", "mplus-1mn-regular.ttf", "mplus-1mn-medium.ttf", "mplus-1mn-bold.ttf"]
#code_fonts = []
num_code_fonts = SizeOf(code_fonts)
code_script = "ascii"
#code_script = "multilingual"
if ($argc == 2)
  output_dir = $argv[1]
else
  #output_dir = "../data/fonts"
  output_dir = "generated"
endif

fi = 0
while (fi < num_copy_fonts)
  new_basename = ToLower(copy_fonts[fi]:r)
  is_mplus = Strstr(new_basename, "mplus") >= 0
  # remove hyphen from mplus-1
  if (is_mplus)
    new_basename = "mplus1" + StrJoin(StrSplit(new_basename, "mplus-1"), "")
  endif
  si = 0
  while (si < num_copy_scripts)
    script = copy_scripts[si]
    Open(copy_fonts[fi])
    SelectAll()
    # Remove TrueType instructions
    ClearInstrs()
    SelectNone()
    # Basic Latin (e.g., English)
    SelectMore(0u0020,0u007e)
    # Latin-1 Supplement (covers core Western European languages)
    SelectMore(0u00a0,0u00ff)
    # Mathematical Operators (e.g., infinity, sum, partial differential)
    SelectMore(0u2200,0u22ff)
    # General Punctuation (most of it) (e.g., dashes, smart quotes, bullet)
    SelectMore(0u2000,0u203a)
    # Trademark sign (selected from Letterlike Symbols set)
    SelectMore(0u2122)
    # Geometric Shapes (e.g., list bullets)
    SelectMore(0u25a0,0u25ff)
    # Greek (frequently used for math and bullets)
    SelectMore(0u0370,0u03ff)
    # Additional Currency Symbols (QUESTION: do we need all of them in basic latin? Euro Sign is 0u20ac)
    #SelectMore(0u20a0,0u20d0)
    SelectMore(0u20ac)
    if (script == "latin-ext" || script == "latin-cyrillic" || script == "multilingual")
      # Latin Extended-A, Latin Extended-B
      SelectMore(0u0100,0u024f)
      # IPA Extensions (i.e., core phonetics)
      #SelectMore(0u0250,0u02af)
      # Upside-down e (from IPA Extensions)
      SelectMore(0u0259)
      # Spacing Modifier Letters (e.g., diacritics)
      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 == "multilingual")
      # Cyrillic
      SelectMore(0u0400,0u04ff)
      # Cyrillic Supplement (QUESTION should this go in multilingual only?)
      SelectMore(0u0500,0u052f)
    endif
    if (script == "multilingual")
      # Greek Extended
      SelectMore(0u1f00,0u1fff)
      # Latin Extended Additional (Vietnamese, Hindi, ...)
      SelectMore(0u1e00,0u1eff)
    endif
    # BOM
    SelectMore(0ufeff)
    # No-break space
    SelectMore(0u00a0)
    # Non-marking return (QUESTION do we really need this?)
    #SelectMore(0u000d)
    SelectInvert()
    Clear()
    SelectNone()
    # Generate BOM from no-break space (for M+ fonts)
    if (is_mplus)
      Select(0u00a0)
      Copy()
      SelectNone()
      Select(0ufeff)
      Paste()
      SetWidth(0)
      SelectNone()
    # Generate no-break hyphen from hyphen (for Noto Serif fonts)
    #else
    #  Select(0u002d)
    #  Copy()
    #  SelectNone()
    #  Select(0u2011)
    #  Paste()
    #  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()

    new_filename = new_basename + "-" + script + ".ttf"
    new_filepath = output_dir + "/" + new_filename
    Print("Generating " + new_filename + "...")
    Generate(new_filepath, "", genflags)
    Close()
    if (is_mplus)
      # Regenerate 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
while (fi < num_code_fonts)
  new_basename = code_fonts[fi]:r
  # remove hyphen from mplus-1
  new_basename = "mplus1" + StrJoin(StrSplit(new_basename, "mplus-1"), "")
  new_suffix = "-" + code_script + ".ttf"
  Open(code_fonts[fi])
  SelectAll()
  # NOTE: M+ fonts don't have hinting, so technically this is redundant
  ClearInstrs()
  SelectNone()
  if (code_script == "ascii")
    # Basic Latin (e.g., Code)
    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)
      SelectMore(0u2460,0u2473)
      new_suffix = "-ascii-conums.ttf"
    endif
    SelectInvert()
    Clear()
    SelectNone()
  endif
  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-bolditalic", "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-bolditalic"
  endif
  # Generate BOM from no-break space (for M+ fonts)
  Select(0u00a0)
  Copy()
  SelectNone()
  Select(0ufeff)
  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 to drop invalid cmap format 14 table (ignore warnings)
  Open(new_filepath)
  Generate(new_filepath, "", genflags)
  Close()
  fi = fi + 1
endloop