Debian Codegraph @

docs/gftools-builder/README.md

raw symbols SCIP
1 # gftools-builder: Config-driven font project builder
2
3 This utility wraps fontmake and a number of post-processing fixes to
4 build variable, static OTF, static TTF and webfonts from Glyphs,
5 Designspace/UFO or UFO sources, in a way that attempts to bring the
6 outputs close to the [Google Fonts guidelines](https://googlefonts.github.io/gf-guide/).
7
8 ## Basic builds
9
10 The builder is driven by a configuration file in the [YAML](https://circleci.com/blog/what-is-yaml-a-beginner-s-guide/) format.
11 By convention, these files are placed in the `sources/` subdirectory
12 of your font repository, and are usually called `config.yaml`. (You can
13 give them different names if you have multiple families in the same
14 repository; `config.yaml` is just a convention. We'll look at building
15 multiple families later.)
16
17 The most basic configuration file simply tells the builder where to find
18 the font sources:
19
20 ```yaml
21 sources:
22 - Texturina.glyphs
23 - Texturina-Italic.glyphs
24 ```
25
26 To build this font family from the command line, use:
27
28 ```shell
29 $ gftools builder sources/config.yaml
30 ```
31
32 This will then build:
33
34 * Variable fonts for both sources, placing them in the `fonts/variable` directory; `gftools-fix` will be run on them, and they will have a STAT table build which relates the upright to the italic and vice versa.
35 * All static instances with TrueType outlines, in `fonts/ttf`. These will be autohinted, and `gftools-fix` will be run on them.
36 * All static instances with CFF outlines, in `fonts/otf`. `gftools-fix` will be run on them.
37 * Variable and static TTF WOFF2 webfonts, in `fonts/webfonts`.
38
39 In other words, the command above will produce 166 different outputs!
40
41 ## Customizing the basic build process
42
43 A more complete YAML configuration file for the Texturina family would look like this:
44
45
46 ```yaml
47 sources:
48 - Texturina.glyphs
49 - Texturina-Italic.glyphs
50 axisOrder:
51 - opsz
52 - wght
53 outputDir: "../fonts"
54 familyName: Texturina
55 version: 1.005
56 stat:
57 - name: Width
58 tag: wdth
59 values:
60 - name: UltraCondensed
61 value: 50
62 ...
63 - name: Weight
64 tag: wght
65 values:
66 - name: Regular
67 value: 400
68 flags: 2
69 ...
70 instances:
71 Texturina[wght].ttf:
72 - coordinates:
73 wght: 400
74 - coordinates:
75 wght: 500
76 - familyName: "Texturina Exotic"
77 styleName: "Medium"
78 coordinates:
79 wght: 500
80 ...
81 Texturina-Italic[wght].ttf:
82 - coordinates:
83 wght: 700
84 ...
85 vttSources:
86 Texturina[wght].ttf: vtt-roman.ttx
87 Texturina-Italic[wght].ttf: vtt-italic.ttx
88 ...
89 ```
90
91 The build can be customized by adding the following keys to the YAML file:
92
93 - `sources`: Required. An array of Glyphs, UFO or designspace source
94 files.
95
96 - `logLevel`: Debugging log level. Defaults to `INFO`.
97
98 - `stat`: A STAT table configuration. This may be either a list of
99 axes and values as demonstrated above, or a dictionary mapping each
100 variable font to a per-source list. If neither `stylespaceFile` or
101 `stat` are provided, a STAT table is generated automatically using
102 `gftools.stat`. The Flags key's values are explained in the [OpenType
103 spec](https://learn.microsoft.com/en-us/typography/opentype/spec/stat#flags).
104
105 - `instances`: A list of static font TTF instances to generate from
106 each variable font as demonstrated above. If this argument isn\'t
107 provided, static TTFs will be generated for each instance that is
108 specified in the source files.
109
110 - `buildVariable`: Build variable fonts. Defaults to true.
111
112 - `buildStatic`: Build static fonts (OTF or TTF depending on `$buildOTF`
113 and `$buildTTF`). Defaults to true.
114
115 - `buildOTF`: Build OTF fonts. Defaults to true.
116
117 - `buildTTF`: Build TTF fonts. Defaults to true.
118
119 - `buildWebfont`: Build WOFF2 fonts. Defaults to `$buildStatic`.
120
121 - `outputDir`: Where to put the fonts. Defaults to `../fonts/`
122
123 - `vfDir`: Where to put variable fonts. Defaults to
124 `$outputDir/variable`.
125
126 - `ttDir`: Where to put TrueType static fonts. Defaults to
127 `$outputDir/ttf`.
128
129 - `otDir`: Where to put CFF static fonts. Defaults to
130 `$outputDir/otf`.
131
132 - `woffDir`: Where to put WOFF2 static fonts. Defaults to
133 `$outputDir/webfonts`.
134
135 - `cleanUp`: Whether or not to remove temporary files. Defaults to
136 `true`.
137
138 - `autohintTTF`: Whether or not to autohint TTF files. Defaults to
139 `true`.
140
141 - `autohintOTF`: Whether or not to autohint OTF files. Defaults to
142 `false`.
143
144 - `ttfaUseScript`: Whether or not to detect a font\'s primary script
145 and add a `-D<script>` flag to ttfautohint. Defaults to `false`.
146
147 - `vttSources`: To patch a manual VTT hinting program (ttx format) to
148 font binaries.
149
150 - `familyName`: Family name for variable fonts. Defaults to family
151 name of first source file.
152
153 - `flattenComponents`: Whether to flatten components on export.
154 Defaults to `true`.
155
156 - `decomposeTransformedComponents`: Whether to decompose transformed
157 components on export. Defaults to `true`.
158
159 - `googleFonts`: Whether this font is destined for release on Google
160 Fonts. Used by GitHub Actions. Defaults to `false`.
161
162 - `category`: If this font is destined for release on Google Fonts, a
163 list of the categories it should be catalogued under. Used by GitHub
164 Actions. Must be set if `googleFonts` is set.
165
166 - `fvarInstanceAxisDflts`: Mapping to set every fvar instance\'s
167 non-wght axis value e.g if a font has a wdth and wght axis, we can
168 set the wdth to be 100 for every fvar instance. Defaults to `None`
169
170 - `expandFeaturesToInstances`: Resolve all includes in the sources\'
171 features, so that generated instances can be compiled without
172 errors. Defaults to `true`.
173
174 - `reverseOutlineDirection`: Reverse the outline direction when
175 compiling TTFs (no effect for OTFs). Defaults to fontmake\'s
176 default.
177
178 - `removeOutlineOverlaps`: Remove overlaps when compiling fonts.
179 Defaults to fontmake\'s default.
180
181 - `glyphData`: An array of custom GlyphData XML files for with glyph
182 info (production name, script, category, subCategory, etc.).
183 Used only for Glyphs sources.
184
185 - `includeSourceFixes`: Whether to pass the `--include-source-fixes`
186 argument when fixing the fonts. Defaults to true.
187
188 - `checkCompatibility`: Check the compatibility of outlines before building
189 a variable font. Defaults to true.
190
191 - `extraFontmakeArgs`: A string of additional arguments to pass to
192 `fontmake` when building all fonts.
193
194 - `extraVariableFontmakeArgs`: A string of additional arguments to pass to
195 `fontmake` when building variable fonts.
196
197 - `extraStaticFontmakeArgs`: A string of additional arguments to pass to
198 `fontmake` when building static fonts.
199
200 - `buildSmallCap`: Automatically build smallcap families from source with a
201 `smcp` feature. Defaults to true.
202
203 - `splitItalic`: For variable fonts containing a `slnt` or `ital` axis,
204 subspace them into separate roman and italic VF files to comply with
205 Google Fonts' specification. Defaults to true.
206
207 - `localMetadata`: A field that's ignored so you can put whatever you like in it.
208
209 - `filenameSuffix`: A suffix appended to the family name part of filenames.
210
211 - `postCompile`: A list of operations to be run after the font compilation step; you can use this (together with the above option) to build variant fonts, rename families, bake in stylistic sets, etc.
212
213 - `includeSubsets`: A list of subsets to include in the font, using the
214 `gftools-add-ds-subsets` tool. Each subset can be specified by a
215 name, a range, or a source file. See the [gftools-add-ds-subsets documentation](https://github.com/googlefonts/gftools/blob/main/Lib/gftools/scripts/add_ds_subsets.py) for more information.
216
217 ## *Really* customizing the build process
218
219 If the options above aren't enough for you - let's say you want to run your
220 own post-processing script, or you're creating multiple versions of the same
221 font and using `pyftfeaturefreeze` or remapping the cmap table, or you want
222 to use `paintcompiler` to make a COLRv1 variant, ... or you just need to do
223 things differently - then you can provide the builder with a *recipe*.
224
225 By default, the builder takes the configuration you give it and applies a
226 *recipe provider*. The default recipe provider is `googlefonts`,
227 so a basic configuration file is equivalent to
228
229 ```yaml
230 sources:
231 - Texturina.glyphs
232 - Texturina-Italic.glyphs
233 recipeProvider: googlefonts
234 ```
235
236 This recipe provider (implemented in the Python module
237 `gftools.builder.recipeproviders.googlefonts`) reads the configuration
238 and determines the target files to build and the steps to take in order
239 to build them, and under the hood, it adds a *recipe* to the
240 configuration.
241
242 Hence, to fully customize the build process, your configuration file
243 can provide its own recipe. This can be done in two ways: (1) If you
244 add a `recipe` map to your config file *without* specifying a
245 `recipeProvider`, then your provided recipe is what gets built. (2)
246 If you add a `recipe` map *and* specify a `recipeProvider`, the
247 recipe provider generates a recipe first, and your recipe is then
248 overlaid on top of it, allowing you to customize certain targets
249 or add additional ones.
250
251 To see the recipe that it generates internally, you can use the `--generate`
252 flag, like so: `gftools builder --generate sources/config.yaml`;
253 this will emit a new configuration file, with the recipe attached:
254
255 ```yaml
256 sources:
257 - Texturina.glyphs
258 - Texturina-Italic.glyphs
259 recipeProvider: googlefonts
260 recipe:
261 ../fonts/otf/Texturina-Black.otf:
262 - source: Texturina.glyphs
263 - operation: instantiateUfo
264 instance_name: Texturina Black
265 - operation: buildOTF
266 args: --filter ... --filter FlattenComponentsFilter --filter DecomposeTransformedComponentsFilter
267 - operation: fix
268 ../fonts/otf/Texturina-BlackItalic.otf:
269 - source: Texturina-Italic.glyphs
270 - operation: instantiateUfo
271 instance_name: Texturina Black Italic
272 ...
273 ```
274
275 > (I've slightly reordered this file from the *actual* output that you
276 would get, in order to make it easier to follow, but the contents are
277 equivalent.)
278
279 As you can see, a recipe maps a *target* to a number of *steps*. Each step
280 begins with a *source* and consists of a number of *operations*, each of
281 which can have arguments. Files are passed from operation to operation, with the builder determining any temporary file names needed.
282
283 > Two additional possibilities, for completeness:
284 > 1) You can also switch sources midway through if an operation created a new source.
285 > 2) Optionally, the steps can end with one or more *postprocess* steps.
286 > We'll talk about those later.
287
288 The builder will then look at the list of targets and steps, and determine
289 the most efficient way to build the targets, so you don't need to attempt
290 to optimize the build process yourself. This might mean that the list of
291 steps you give seem duplicative. For example, if you want to build
292 unhinted and hinted versions of a font, you might say:
293
294 ```yaml
295 recipe:
296 ../fonts/unhinted/Texturina-Black.ttf:
297 - source: Texturina.glyphs
298 - operation: instantiateUfo
299 instance_name: Texturina Black
300 - operation: buildTTF
301 ../fonts/hinted/Texturina-Black.ttf:
302 - source: Texturina.glyphs
303 - operation: instantiateUfo
304 instance_name: Texturina Black
305 - operation: buildTTF
306 - operation: autohint
307 ```
308
309 With this recipe, the builder will *not* create an instance UFO and
310 build a TTF, then create another instance UFO and build a TTF again and
311 run autohint on it. Instead, it will realise that it can first build
312 `../fonts/unhinted/Texturina-Black.ttf`, and then it can get from there
313 to `../fonts/unhinted/Texturina-Black.ttf` by running an autohinter.
314
315 In fact, if you have the `pydot` package installed, you can run
316 `gftools-builder --graph sources/config.yaml` and it will illustrate its
317 build process by leaving a `graph.png` file in the `sources` directory:
318
319 ![graph.png](graph.png)
320
321 ### Operations available
322
323 "Operations" are implemented as Python classes under the package
324 `gftools.builder.operations`. Currently provided operations include
325 (in rough order of frequency of use):
326
327 - *buildVariable*: Runs `fontmake -o variable` on the input file. Any `args` are added to the fontmake command line.
328 - *buildTTF* and *buildOTF*: Analogously runs `fontmake -o ttf` and `fontmake -o otf`
329 - *instantiateUFO*: Creates an interpolated UFO for a given `instance_name`.
330 - *autohint*: Runs `gftools-autohint` on a TTF.
331 - *buildStat*: *Postprocesses* one or more variable fonts to add a STAT table to them.
332 - *fix*: Runs `gftools-fix` on a binary font.
333 - *compress*: Creates a WOFF2 web font.
334 - *rename*: Renames a font's family name to the provided `name` argument.
335 - *exec*: An escape hatch for arbitrary processing - executes the executable named in the `exe` argument with the arguments provided in `args`.
336 - *copy*: Copies a file. Used internally when generating multiple variants from the same intermediate file.
337 - *featureFreeze*: Runs `pyftfeaturefreeze` with the arguments provided in `args`.
338 - *subspace*: Runs `fonttools varLib.instancer` to subspace a variable font according to the values in `axes`. `args` are added to the command line.
339 - *hbsubset*: Uses `hb-subset` to slim down a font binary.
340 - *addSubset*: Adds a subset from another font using `gftools-add-ds-subsets`
341 - `directory`: the intermediary folder used to store the source(s) the subset(s) is taken from
342 - `subsets`: a list of subset configurations to merge in
343 - `from` (required): can be a pre-configured Noto source ("Noto Sans", "Noto Serif", "Noto Sans Devanagari", "Noto Sans Linear B"), or:
344 - `repo`: the GitHub slug for the repository, e.g. `googlefonts/gftools`. You can specify a git revision by suffixing this with `@v1.0.0`, or use `@latest` for the latest *published* release
345 - `path`: the path within the repo that has the source file
346 - `name`: a named Google Fonts subset, e.g. `GF_Latin_Core`
347 - `ranges`: a list unicode codepoint range to include
348 - `start`: the start of the range (as hex or decimal)
349 - `end`: the end of the range (as hex or decimal)
350 - `glyphNames`: a list of glyph names to include
351 - `glyphNamesFile`: path to a file with glyph names to include, one per line (comments using `#` or `//` allowed)
352 - `layoutHandling`: "subset", "closure" or "ignore" ([further reading](https://github.com/googlefonts/ufomerge/blob/bb9a82ff3039b8aa0cba58372158bd3c0e5cb770/Lib/ufomerge/__init__.py#L512-L521))
353 - `force`: replace existing glyphs in your sources, instead of skipping them
354 - `exclude_glyphs`: whitespace-delimited glyph names to exclude from merging
355 - `exclude_glyphs_file`: path to a file with glyphs names to exclude from merging, one per line (comments using `#` or `//` allowed)
356 - `exclude_codepoints`: whitespace-delimited unicode codepoints to exclude from merging
357 - `exclude_codepoints_file`: path to a file with with unicode codepoints to exclude from merging, one per line (comments using `#` or `//` allowed)
358 - *buildVTT*: Uses `gftools-build-vtt` with the configuration file provided in `vttfile` to add VTT hinting to a font binary.
359 - *remap*: Uses `gftools-remap-font` to alter a font binary's `cmap` table.
360 - *paintcompiler*: Runs paintcompiler on a font to add a COLRv1 table.
361
362 ### Switching sources
363
364 In some cases, an operation can run on a source file to produce a different
365 source. If the builder knows about this (such as when running the
366 *instantiateUFO* operation), all is well. But if you are using the *exec*
367 operation to do things the builder is not aware of, you will need to tell
368 the builder about the new source file so that it can carry on from there:
369
370 ```yaml
371 Handwriter-Guides.ttf:
372 - source: Handwriter.glyphs
373 - operation: exec
374 exe: python3 ../scripts/add-guidelines.py
375 args: -o Handwriter-Guidelines.glyphs Handwriter.glyphs
376 - source: Handwriter-Guidelines.glyphs
377 - operation: buildTTF
378 ```
379
380 ### Post-processing and dependencies
381
382 The STAT table produces an interesting challenge, in two ways. Let's take
383 the case of Texturina, with upright and italic source files:
384
385 ```yaml
386 sources:
387 - Texturina.glyphs
388 - Texturina-Italic.glyphs
389 ```
390
391 After building the two variable fonts, we then want to use
392 `gftools-build-stat` to link them together via the `STAT` table.
393 `gftools-build-stat` takes a number of font binaries and modifies them
394 in-place. This means that we can only run the `buildSTAT` operation
395 after *both* targets have been completely built. The way that the
396 `googlefonts` recipe provider achieves this is to add the following
397 *postprocessing* step to one of the files. Somewhere in the generated
398 recipe, you will find this:
399
400 ```yaml
401 ../fonts/variable/Texturina-Italic[opsz,wght].ttf:
402 - source: Texturina-Italic.glyphs
403 - operation: buildVariable
404 ...
405 - operation: fix
406 - postprocess: buildStat
407 needs:
408 - ../fonts/variable/Texturina[opsz,wght].ttf
409 ```
410
411 This last step (a) uses the `needs` key to declare a dependency on
412 another target - in other words, this step can only be run once
413 `../fonts/variable/Texturina[opsz,wght].ttf` has been fully generated,
414 and (b) uses `postprocess` instead of `operation` to declare that the
415 input file and output file are the same. The builder will then ensure
416 that the operation is only called when both variable fonts are built.
417
418 ### Creating your own recipe provider
419
420 If you have multiple families you want to build in the same way, or if
421 you just have a *really* complex build process, then you can create
422 your own recipe provider as a Python module. To do this, create a Python
423 file containing a single class inheriting from
424 `gftools.builder.recipeproviders.RecipeProviderBase`, and implement a
425 `write_recipe` method.
426
427 This method is expected to read `self.config`, a Python object
428 representing the YAML configuration file, and return a Python dictionary
429 similar to the `recipe` entry. You can use
430 `gftools.builder.recipeproviders.googlefonts` and
431 `gftools.builder.recipeproviders.noto` as inspiration.
432
433 Finally ensure that your Python file is in your Python module load path,
434 and add the name of the module to your config file:
435
436 ```
437 sources:
438 - Texturina.glyphs
439 - Texturina-Italic.glyphs
440 recipeProvider: texturina
441 ```
442
443 This will load `texturina.py` (or `texturina/__init__.py`), find an
444 appropriate subclass, and use that to derive the recipe.