File: make-variable-font.py

package info (click to toggle)
fonts-cantarell 0.303.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,760 kB
  • sloc: python: 859; makefile: 9; sh: 1
file content (71 lines) | stat: -rw-r--r-- 2,242 bytes parent folder | download | duplicates (2)
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
#!/bin/env python3

# Note: To parallelize this, the cubic to quadratic filter could be run on all sources
# sequentially and the resulting single-layer UFOs written to disk, then compiling the
# masters could be done in parallel and finally, merging into a variable font happens
# sequentially.

import argparse
import tempfile
import subprocess
import os
from pathlib import Path

import cffsubr
import fontTools.designspaceLib
import fontTools.varLib
import fontTools.ttLib
import statmake.classes
import statmake.lib
import ufo2ft
import ufoLib2

parser = argparse.ArgumentParser()
parser.add_argument(
    "designspace_path", type=Path, help="The path to the Designspace file."
)
parser.add_argument(
    "stylespace_path", type=Path, help="The path to the Stylespace file."
)
parser.add_argument(
    "psautohint_path", type=Path, help="The path to the psautohint executable."
)
parser.add_argument("output_path", type=Path, help="The variable TTF output path.")
args = parser.parse_args()

designspace_path = args.designspace_path.resolve()
stylespace_path = args.stylespace_path.resolve()
output_path = args.output_path.resolve()
psautohint_path = args.psautohint_path.resolve()


# 1. Load Designspace and filter out instances that are marked as non-exportable.
designspace = fontTools.designspaceLib.DesignSpaceDocument.fromfile(designspace_path)
designspace.loadSourceFonts(ufoLib2.Font.open)

designspace.instances = [
    s for s in designspace.instances if s.lib.get("com.schriftgestaltung.export", True)
]

# 2. Compile variable OTF from the masters. Do not optimize, because we have to do
# it again after autohinting.
varfont = ufo2ft.compileVariableCFF2(
    designspace,
    inplace=True,
    useProductionNames=True,
    optimizeCFF=ufo2ft.CFFOptimization.NONE,
)

# 3. Generate STAT table.
stylespace = statmake.classes.Stylespace.from_file(stylespace_path)
statmake.lib.apply_stylespace_to_variable_font(stylespace, varfont, {})

# 4. Save. External tools after this point.
varfont.save(output_path)

# 5. Autohint
subprocess.check_call([os.fspath(args.psautohint_path), os.fspath(output_path)])

# 6. Subroutinize (compress)
varfont = fontTools.ttLib.TTFont(output_path)
cffsubr.subroutinize(varfont).save(output_path)