|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +"""Build saltcandy123font from SVG files""" |
| 4 | + |
| 5 | +import argparse |
| 6 | +import json |
| 7 | +import pathlib |
| 8 | +import re |
| 9 | +import xml.dom.minidom |
| 10 | + |
| 11 | +import fontforge |
| 12 | + |
| 13 | +BASE_DIR = pathlib.Path(__file__).parent |
| 14 | + |
| 15 | + |
| 16 | +def build_saltcandy123font(*, version: str) -> fontforge.font: |
| 17 | + font = fontforge.font() |
| 18 | + font.fontname = "saltcandy123font" |
| 19 | + font.fullname = font.fontname |
| 20 | + font.familyname = font.fontname |
| 21 | + font.copyright = "Copyright (C) saltcandy123" |
| 22 | + font.weight = "Regular" |
| 23 | + font.os2_weight = 400 |
| 24 | + font.version = version |
| 25 | + |
| 26 | + for svg_path in BASE_DIR.joinpath("glyphs").iterdir(): |
| 27 | + match = re.search("^u([0-9a-f]{4}).svg$", svg_path.name) |
| 28 | + if not match: |
| 29 | + continue |
| 30 | + code = int(match.group(1), 16) |
| 31 | + glyph = font.createChar(code) |
| 32 | + glyph.importOutlines(str(svg_path)) |
| 33 | + with xml.dom.minidom.parse(str(svg_path)) as doc: |
| 34 | + glyph.width = int(doc.childNodes[0].getAttribute("width")) |
| 35 | + |
| 36 | + return font |
| 37 | + |
| 38 | + |
| 39 | +def generate_npm_package_metadata(*, version=str) -> dict: |
| 40 | + return { |
| 41 | + "name": "@saltcandy123/saltcandy123font", |
| 42 | + "version": version, |
| 43 | + "description": "A simple handwritten font created by @saltcandy123", |
| 44 | + "repository": "https://github.com/saltcandy123/saltcandy123font", |
| 45 | + "author": "saltcandy123", |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | +def main(): |
| 50 | + parser = argparse.ArgumentParser(description=__doc__) |
| 51 | + parser.add_argument("font_version", help="font version (e.g. 0.1.2)") |
| 52 | + args = parser.parse_args() |
| 53 | + |
| 54 | + dist_dir = BASE_DIR.joinpath("dist") |
| 55 | + dist_dir.mkdir(exist_ok=True) |
| 56 | + |
| 57 | + font = build_saltcandy123font(version=args.font_version) |
| 58 | + |
| 59 | + for ext in ["ttf", "woff"]: |
| 60 | + font.generate(str(dist_dir.joinpath(f"saltcandy123font.{ext}"))) |
| 61 | + |
| 62 | + package_metadata = generate_npm_package_metadata(version=args.font_version) |
| 63 | + with open(dist_dir.joinpath("package.json"), "w") as f: |
| 64 | + json.dump(package_metadata, f, indent=2) |
| 65 | + |
| 66 | + with open(dist_dir.joinpath("README.md"), "w") as f: |
| 67 | + with open(BASE_DIR.joinpath("README-npm.md")) as f_src: |
| 68 | + f.write(f_src.read()) |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + main() |
0 commit comments