@@ -4,12 +4,11 @@ import argparse
|
||||
import difflib
|
||||
from pathlib import Path
|
||||
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
from PIL import Image, ImageDraw, ImageFont, features
|
||||
|
||||
|
||||
REPO = Path(__file__).resolve().parents[1]
|
||||
FONT_PATH = REPO / "package/assets/fonts/petaluma/PetalumaScript.otf"
|
||||
OUTPUT_PATH = REPO / "src/vamp_style/font.cpp"
|
||||
FONT_SIZE = 32
|
||||
FONT_HEIGHT = 36
|
||||
FONT_MAX_WIDTH = 28
|
||||
@@ -63,7 +62,9 @@ def pair_adjustments(font: ImageFont.FreeTypeFont) -> list[tuple[int, int]]:
|
||||
|
||||
|
||||
def generate() -> str:
|
||||
font = ImageFont.truetype(str(FONT_PATH), FONT_SIZE)
|
||||
if not features.check_feature("raqm"):
|
||||
raise RuntimeError("Font generation requires Pillow with Raqm support; install libfribidi0 on Debian/Ubuntu.")
|
||||
font = ImageFont.truetype(str(FONT_PATH), FONT_SIZE, layout_engine=ImageFont.Layout.RAQM)
|
||||
min_top = min(font.getbbox(chr(value))[1] for value in range(FIRST_CODEPOINT, LAST_CODEPOINT + 1))
|
||||
y_offset = max(0, -min_top)
|
||||
adjustments = pair_adjustments(font)
|
||||
@@ -173,27 +174,31 @@ def generate() -> str:
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
parser.add_argument("--check", action="store_true")
|
||||
args = parser.parse_args()
|
||||
|
||||
generated = generate()
|
||||
if args.check:
|
||||
current = OUTPUT_PATH.read_text()
|
||||
if not args.output.is_file():
|
||||
parser.error(f"generated font is missing: {args.output}")
|
||||
current = args.output.read_text(encoding="utf-8")
|
||||
if current == generated:
|
||||
print("pico font is up to date")
|
||||
return 0
|
||||
diff = difflib.unified_diff(
|
||||
current.splitlines(),
|
||||
generated.splitlines(),
|
||||
fromfile=str(OUTPUT_PATH),
|
||||
tofile=f"{OUTPUT_PATH} (generated)",
|
||||
fromfile=str(args.output),
|
||||
tofile=f"{args.output} (generated)",
|
||||
lineterm="",
|
||||
)
|
||||
print("\n".join(diff))
|
||||
return 1
|
||||
|
||||
OUTPUT_PATH.write_text(generated)
|
||||
print(f"generated {OUTPUT_PATH}")
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
args.output.write_text(generated, encoding="utf-8")
|
||||
print(f"generated {args.output}")
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user