Use Jinja2, start to support sprites
This commit is contained in:
@ -2,9 +2,10 @@ load("@pytools_deps//:requirements.bzl", "requirement")
|
||||
|
||||
py_binary(
|
||||
name = "font",
|
||||
srcs = ["font.py"],
|
||||
srcs = ["font.py", "shared.py"],
|
||||
deps = [
|
||||
requirement("pillow"),
|
||||
requirement("Jinja2"),
|
||||
],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
@ -1,6 +1,13 @@
|
||||
from typing import Tuple
|
||||
from PIL import Image
|
||||
import sys
|
||||
import shared
|
||||
|
||||
TEMPLATE = """
|
||||
// generated code! be nice!
|
||||
#include "sys/sys.h"
|
||||
sys_glyph {{ font_name }}[{{ n_glyphs }}] = { {{- glyphs|join(", ") -}} };
|
||||
""".lstrip()
|
||||
|
||||
|
||||
def main(font_name, n_glyphs, fname_png, fname_c):
|
||||
@ -8,11 +15,13 @@ def main(font_name, n_glyphs, fname_png, fname_c):
|
||||
assert(len(glyphs) == n_glyphs), f"must be exactly {n_glyphs} glyphs"
|
||||
|
||||
with open(fname_c, "wt") as output:
|
||||
output.writelines([
|
||||
"// generated code! be nice!\n",
|
||||
"#include \"sys/sys.h\"\n",
|
||||
f"sys_glyph {font_name}[{n_glyphs}] = {{{', '.join(str(g) for g in glyphs)}}};\n"
|
||||
])
|
||||
output.write(
|
||||
shared.templates.from_string(TEMPLATE).render(
|
||||
font_name=font_name,
|
||||
n_glyphs=n_glyphs,
|
||||
glyphs=glyphs,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def load_glyphs(fname_png: str):
|
||||
|
4
pytools/requirements.lock
Normal file
4
pytools/requirements.lock
Normal file
@ -0,0 +1,4 @@
|
||||
pillow==10.2.0
|
||||
Jinja2==3.1.3
|
||||
## The following requirements were added by pip freeze:
|
||||
MarkupSafe==2.1.5
|
@ -1 +1,2 @@
|
||||
pillow==10.2.0
|
||||
pillow==10.2.0
|
||||
Jinja2==3.1.3
|
6
pytools/shared.py
Normal file
6
pytools/shared.py
Normal file
@ -0,0 +1,6 @@
|
||||
from jinja2 import Environment, BaseLoader, select_autoescape
|
||||
|
||||
templates = Environment(
|
||||
loader=BaseLoader(),
|
||||
autoescape=select_autoescape(),
|
||||
)
|
Reference in New Issue
Block a user