From fad65073d068faa61bade0a3f51e167631a464cd Mon Sep 17 00:00:00 2001 From: klemek Date: Sun, 8 Nov 2026 00:33:26 +0100 Subject: [PATCH] feat: add empty tile if missing --- Makefile | 1 + README.md | 6 ++++-- src/glsl_glyphs/generator.py | 5 +---- src/glsl_glyphs/params.py | 7 +++++++ src/glsl_glyphs/reader.py | 24 +++++++++++++++++++----- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 24bd7b5..863c1eb 100644 --- a/Makefile +++ b/Makefile @@ -87,6 +87,7 @@ uv-run-sample-%: .venv ## uv run (project) sample/(xxx) --sentences-input sample/$*_sentences.txt \ --functions \ --includes \ + --add-empty \ .PHONY: ruff ruff: .venv ## ruff check diff --git a/README.md b/README.md index 841e485..adefeb5 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ ```txt usage: glsl-glyphs [-h] [-i INPUT_FILE] [-n NAME] [-o OUTPUT_FILE] [-g NAMES_FILE] [-s SETS_FILE] [-f | --functions | --no-functions] [--sentences-input SENTENCES_FILE] [--sentences-output OUTPUT_FILE] [--sentences-only | --no-sentences-only] - [--includes | --no-includes] [-v | --verbose | --no-verbose] [-q | --quiet | --no-quiet] + [--includes | --no-includes] [--add-empty | --no-add-empty] [-v | --verbose | --no-verbose] [-q | --quiet | --no-quiet] options: -h, --help show this help message and exit @@ -31,6 +31,8 @@ options: generate only sentences from names (does not read input image) --includes, --no-includes use #include preprocessor in GLSL (forge) + --add-empty, --no-add-empty + add EMPTY tile at start of tileset if missing -v, --verbose, --no-verbose more verbose logging -q, --quiet, --no-quiet @@ -83,7 +85,7 @@ release-% release major/minor/patch ### TODO * [x] Initial implementation -* [ ] Generate a separate sentences file +* [x] Generate a separate sentences file * [ ] Custom height glyph map * [ ] Custom width glyph map * [ ] separated sets/patterns diff --git a/src/glsl_glyphs/generator.py b/src/glsl_glyphs/generator.py index 69e1697..c84da95 100644 --- a/src/glsl_glyphs/generator.py +++ b/src/glsl_glyphs/generator.py @@ -79,10 +79,7 @@ class Generator: lines = [] if self.with_includes and self.with_functions: - lines += [ - f"#include {self.target_path.name}", - "" - ] + lines += [f"#include {self.target_path.name}", ""] lines += [ GLSL_SENTENCES_PREFIX.format(self.target_name.upper()), diff --git a/src/glsl_glyphs/params.py b/src/glsl_glyphs/params.py index 8c59be6..5030ba7 100644 --- a/src/glsl_glyphs/params.py +++ b/src/glsl_glyphs/params.py @@ -17,6 +17,7 @@ class Parameters: target_sentences_path: str | None = None includes: bool = False sentences_only: bool = False + add_empty: bool = False verbose: bool = False quiet: bool = False @@ -98,6 +99,12 @@ def parse_parameters(args: typing.Sequence[str]) -> Parameters: default=default_values.verbose, help="use #include preprocessor in GLSL (forge)", ) + parser.add_argument( + "--add-empty", + action=argparse.BooleanOptionalAction, + default=default_values.verbose, + help="add EMPTY tile at start of tileset if missing", + ) parser.add_argument( "-v", "--verbose", diff --git a/src/glsl_glyphs/reader.py b/src/glsl_glyphs/reader.py index f0a3f90..c578d2c 100644 --- a/src/glsl_glyphs/reader.py +++ b/src/glsl_glyphs/reader.py @@ -13,6 +13,8 @@ from .set import GlyphSet if typing.TYPE_CHECKING: from .params import Parameters +MAX_GLYPHS = 256 + class Reader: def __init__(self, params: Parameters) -> None: @@ -28,18 +30,29 @@ class Reader: if params.source_sentences_path else None ) - self.load_sentences = params.source_sentences_path is not None + self.add_empty = params.add_empty self.log = logging.getLogger(__name__) def read(self) -> tuple[list[Glyph], list[GlyphSet], list[Sentence]]: glyphs = self.__read_image() names = self.__read_names() - sets = self.__read_sets() - sentences = self.__read_sentences(names) for glyph, name in zip(glyphs, names, strict=False): glyph.name = name + if self.add_empty and "EMPTY" not in names: + if len(glyphs) >= MAX_GLYPHS: + self.log.error( + "Cannot add missing EMPTY glyph in full %d glyphmap", MAX_GLYPHS + ) + sys.exit(1) + glyphs.insert(0, Glyph(data=[0] * 4, name="EMPTY")) + names.insert(0, "EMPTY") + + sets = self.__read_sets() + + sentences = self.__read_sentences(names) + return glyphs, sets, sentences def read_sentences(self) -> list[Sentence]: @@ -54,9 +67,10 @@ class Reader: "Input image is not an 8x8 multiple (%dx%d)", img.width, img.height ) sys.exit(1) - if img.width * img.height > 256 * 8 * 8: + if img.width * img.height > MAX_GLYPHS * 8 * 8: self.log.error( - "Input image too big, it must have at maximum 256 tiles (currently: %d)", + "Input image too big, it must have at maximum %d tiles (currently: %d)", + MAX_GLYPHS, (img.width * img.height) // (8 * 8), ) sys.exit(1)