-
Notifications
You must be signed in to change notification settings - Fork 0
/
amalgamate-noto.py
89 lines (73 loc) · 2.7 KB
/
amalgamate-noto.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
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
from __future__ import annotations
import collections
import logging
from pathlib import Path
import glyphsLib
import glyphsLib.classes
import ufoLib2
import ufoLib2.objects
NOTO_SOURES = Path("../noto-source/src/")
amalgamated_fonts = collections.defaultdict(ufoLib2.Font)
def insert_glyphs_into_amalgamated_font(
source: ufoLib2.Font, target: ufoLib2.Font, suffix: str | None
):
for glyph in source:
if suffix is not None:
suffixed_name = f"{glyph.name}.{suffix}"
else:
suffixed_name = glyph.name
if glyph.name in target:
if suffixed_name not in target:
# Add glyphs with a suffix just to drive up the glyph count.
# No copy necessary because the source is dropped anyway.
target.layers.defaultLayer.insertGlyph(
glyph, name=suffixed_name, copy=False
)
else:
logging.warning(
"Glyph %s from %s already in amalgamated font %s",
glyph.name,
path.name,
amalgamated_name(source),
)
else:
target.addGlyph(glyph)
def suffix_from_name(path: Path) -> str | None:
stem = path.stem
if "Display" in stem:
return "display"
elif "Mono" in stem:
return "mono"
elif "Serif" in stem:
return "serif"
elif "Sans" in stem:
return "sans"
return None
def amalgamated_name(ufo: ufoLib2.Font) -> str:
if ufo.info.styleName is None:
raise ValueError(f"Style name for {ufo} is None")
name = ufo.info.styleName.replace(" ", "")
if name == "Semibold":
name = "SemiBold"
return name
for path in sorted(NOTO_SOURES.glob("*.glyphs")):
logging.warning("Processing %s", path.name)
suffix = suffix_from_name(path)
with open(path) as f:
g: glyphsLib.classes.GSFont = glyphsLib.load(f)
ufos: list[ufoLib2.Font] = glyphsLib.to_ufos(
g, minimal=True, propagate_anchors=False
) # type: ignore
for source in ufos:
target = amalgamated_fonts[amalgamated_name(source)]
insert_glyphs_into_amalgamated_font(source, target, suffix)
print(amalgamated_fonts.keys())
for path in sorted(NOTO_SOURES.glob("**/*.ufo")):
logging.warning("Processing %s", path.name)
suffix = suffix_from_name(path)
source = ufoLib2.Font.open(path)
target = amalgamated_fonts[amalgamated_name(source)]
insert_glyphs_into_amalgamated_font(source, target, suffix)
for style, ufo in amalgamated_fonts.items():
logging.warning("Saving %s (%s glyphs)", style, len(ufo.keys()))
ufo.save(f"NotoAmalgamated-{style.replace(' ', '')}.ufo", overwrite=True)