-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-instances-simple-copy.py
60 lines (51 loc) · 2.12 KB
/
gen-instances-simple-copy.py
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
#!/bin/env python3
import argparse
import multiprocessing
from pathlib import Path
import ufoLib2
from fontmake.instantiator import Instantiator
from fontTools.designspaceLib import DesignSpaceDocument, InstanceDescriptor
def generate_and_write_autohinted_instance(
instantiator: Instantiator,
instance_descriptor: InstanceDescriptor,
output_dir: Path,
):
print(f"Generating {instance_descriptor.name}")
instance = instantiator.generate_instance(instance_descriptor)
file_stem = f"{instance.info.familyName}-{instance.info.styleName}".replace(" ", "")
instance.save(output_dir / f"{file_stem}.ufo", overwrite=True)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("designspace_path", type=Path)
parser.add_argument("output_dir", type=Path)
args = parser.parse_args()
# 1. Load Designspace and filter out instances that are marked as non-exportable.
designspace = DesignSpaceDocument.fromfile(args.designspace_path)
designspace.instances = [
s
for s in designspace.instances
if s.lib.get("com.schriftgestaltung.export", True)
]
# (Load all sources into memory completely rather than have the instantiator load
# data on demand. This cleanly separates source loading from source preparation in
# profilers. This may distort measurements if the sources have extra layers or any
# data/ data or images.)
designspace.loadSourceFonts(ufoLib2.Font.open, lazy=False)
# 2. Prepare masters.
print("Instantiating instantiator")
generator = Instantiator.from_designspace(designspace, round_geometry=True)
# (Fork one process per instance)
processes = []
pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
for instance in designspace.instances:
print(f"Queueing {instance.name}")
processes.append(
pool.apply_async(
generate_and_write_autohinted_instance,
args=(generator, instance, args.output_dir),
)
)
pool.close()
pool.join()
for process in processes:
process.get() # Catch exceptions.