-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapple.py
More file actions
86 lines (63 loc) · 2.83 KB
/
Copy pathapple.py
File metadata and controls
86 lines (63 loc) · 2.83 KB
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
import argparse
import logging
from pathlib import Path
from fontTools import ttLib
from shared import get_image_data, prepare_strikes
LOGGER = logging.getLogger(__name__)
def _load_font(path: Path) -> ttLib.TTFont:
if not path.exists():
raise FileNotFoundError(f"Input font not found: {path}")
return ttLib.TTFont(str(path))
def _resolve_asset(path: Path) -> bytes:
if not path.exists():
raise FileNotFoundError(f"Asset image missing: {path}")
return get_image_data(str(path))
def update_sbix_images(input_font: Path, output_font: Path, assets_dir: Path, *, hd: bool, emjc: bool) -> None:
font = _load_font(input_font)
prepare_strikes(font, hd)
sbix_table = font["sbix"]
for ppem, strike in sbix_table.strikes.items():
LOGGER.info("Reading strike of size %sx%s", ppem, ppem)
for name, glyph in strike.glyphs.items():
is_placeholder = glyph.graphicType not in {"emjc", "flip", "png "}
if is_placeholder and glyph.graphicType is not None:
continue
ext = "emjc" if emjc else "png"
asset_path = assets_dir / str(ppem) / f"{name}.{ext}"
if not asset_path.exists():
if not is_placeholder:
LOGGER.debug("Asset image missing: %s", asset_path)
continue
glyph.graphicType = "emjc" if emjc else "png "
glyph.imageData = _resolve_asset(asset_path)
LOGGER.info("Saving changes to %s", output_font)
font.save(str(output_font))
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Replace sbix glyph images with PNG assets.")
parser.add_argument("input_font", type=Path, help="Input TTC/TTF file with sbix table")
parser.add_argument("output_font", type=Path, help="Output TTC/TTF file that will be written")
parser.add_argument("assets_dir", type=Path, help="Directory containing extracted PNG assets")
parser.add_argument(
"--hd",
action="store_true",
help="Expect HD assets (keep 160ppem strike). The default trims 160ppem.",
)
parser.add_argument(
"--emjc",
action="store_true",
help="Use EMJC assets instead of PNG.",
)
parser.add_argument("--log-level", default="INFO", help="Python logging level (default: INFO)")
return parser
def main(argv=None) -> int:
parser = build_parser()
args = parser.parse_args(argv)
logging.basicConfig(level=getattr(logging, args.log_level.upper(), logging.INFO))
try:
update_sbix_images(args.input_font, args.output_font, args.assets_dir, hd=args.hd, emjc=args.emjc)
except Exception as exc: # pylint: disable=broad-except
LOGGER.error("Failed to update sbix images: %s", exc)
return 1
return 0
if __name__ == "__main__":
raise SystemExit(main())