feat: generate sentences
This commit is contained in:
@@ -84,8 +84,9 @@ uv-run-sample-%: .venv ## uv run (project) sample/(xxx)
|
||||
--input sample/$*_8x8.png \
|
||||
--glyph-names sample/$*_names.txt \
|
||||
--sets sample/$*_sets.csv \
|
||||
--output build/$*.glsl \
|
||||
--sentences-input sample/$*_sentences.txt \
|
||||
--functions \
|
||||
--forge-include \
|
||||
|
||||
.PHONY: ruff
|
||||
ruff: .venv ## ruff check
|
||||
|
||||
@@ -7,8 +7,9 @@
|
||||
## Usage
|
||||
|
||||
```txt
|
||||
usage: glsl-glyphs [-h] -i INPUT_FILE [-n NAME] -o OUTPUT_FILE [-g NAMES_FILE] [-s SETS_FILE] [-f | --functions | --no-functions]
|
||||
[-v | --verbose | --no-verbose] [-q | --quiet | --no-quiet]
|
||||
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] [-v | --verbose | --no-verbose]
|
||||
[-q | --quiet | --no-quiet]
|
||||
|
||||
options:
|
||||
-h, --help show this help message and exit
|
||||
@@ -16,12 +17,16 @@ options:
|
||||
png 8x8 glyph map
|
||||
-n, --name NAME target derived name for GLSL constants and functions (default: glyph)
|
||||
-o, --output OUTPUT_FILE
|
||||
output glsl file
|
||||
output glyphs glsl file (default: build/{name}.glsl}
|
||||
-g, --glyph-names NAMES_FILE
|
||||
glyphs names, one line each
|
||||
-s, --sets SETS_FILE defined sets in CSV: index,length,name
|
||||
-f, --functions, --no-functions
|
||||
add GLSL functions to output
|
||||
--sentences-input SENTENCES_FILE
|
||||
generate sentences from file and names
|
||||
--sentences-output OUTPUT_FILE
|
||||
output sentences glsl file (default: build/{name}_sentences.glsl)
|
||||
-v, --verbose, --no-verbose
|
||||
more verbose logging
|
||||
-q, --quiet, --no-quiet
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
forge
|
||||
FORGE
|
||||
F.O.R.G.E.
|
||||
Fusion
|
||||
Of
|
||||
Real-time
|
||||
Generative
|
||||
Effects
|
||||
Everything you see
|
||||
Pixel by pixel
|
||||
Live
|
||||
Generated
|
||||
Controlled
|
||||
π
|
||||
Kleπek
|
||||
Code as art
|
||||
Art as code
|
||||
@@ -13,8 +13,11 @@ def main() -> int:
|
||||
setup_logs(params)
|
||||
logging.getLogger().info("%s %s", PKG_NAME, PKG_VERSION)
|
||||
|
||||
glyphs, sets = Reader(params).read()
|
||||
glyphs, sets, sentences = Reader(params).read()
|
||||
|
||||
Generator(params).generate_glsl_glyphs(glyphs, sets)
|
||||
|
||||
if len(sentences):
|
||||
Generator(params).generate_glsl_sentences(sentences)
|
||||
|
||||
return 0
|
||||
|
||||
@@ -35,3 +35,30 @@ int {0}_set_glyph(int set, float v)
|
||||
GLSL_SUFFIX = """
|
||||
#endif /* INC_{0} */
|
||||
""".strip()
|
||||
|
||||
GLSL_SENTENCES_PREFIX = """
|
||||
#ifndef INC_{0}_SENTENCES
|
||||
#define INC_{0}_SENTENCES
|
||||
""".strip()
|
||||
|
||||
GLSL_SENTENCES_FUNCTIONS = """
|
||||
float {0}_sentence(vec2 uv, vec2 pos, int n)
|
||||
{{
|
||||
float d = 0.;
|
||||
int i;
|
||||
|
||||
if (n < .0 || n > {1}_SENTENCE_COUNT) {{
|
||||
return d;
|
||||
}}
|
||||
|
||||
for (i = 0; i < {0}_sentences_lengths[n]; i++) {{
|
||||
d += {0}_at(uv, pos + vec2(i, 0.), {0}_sentences[n * {1}_SENTENCE_MAX_LEN + i]);
|
||||
}}
|
||||
|
||||
return d;
|
||||
}}
|
||||
""".strip()
|
||||
|
||||
GLSL_SENTENCES_SUFFIX = """
|
||||
#endif /* INC_{0}_SENTENCES */
|
||||
""".strip()
|
||||
|
||||
@@ -3,8 +3,17 @@ import pathlib
|
||||
import sys
|
||||
import typing
|
||||
|
||||
from .constants import GLSL_FUNCTIONS, GLSL_PREFIX, GLSL_SET_FUNCTIONS, GLSL_SUFFIX
|
||||
from .constants import (
|
||||
GLSL_FUNCTIONS,
|
||||
GLSL_PREFIX,
|
||||
GLSL_SENTENCES_FUNCTIONS,
|
||||
GLSL_SENTENCES_PREFIX,
|
||||
GLSL_SENTENCES_SUFFIX,
|
||||
GLSL_SET_FUNCTIONS,
|
||||
GLSL_SUFFIX,
|
||||
)
|
||||
from .glyph import Glyph
|
||||
from .sentences import Sentence
|
||||
from .set import GlyphSet
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
@@ -19,6 +28,12 @@ class Generator:
|
||||
else:
|
||||
self.target_path = pathlib.Path(params.target_path)
|
||||
self.with_functions = params.functions
|
||||
if params.target_sentences_path is None:
|
||||
self.target_sentences_path = (
|
||||
pathlib.Path("build") / f"{params.target_name}_sentences.glsl"
|
||||
)
|
||||
else:
|
||||
self.target_sentences_path = pathlib.Path(params.target_sentences_path)
|
||||
self.log = logging.getLogger(__name__)
|
||||
|
||||
def generate_glsl_glyphs(self, glyphs: list[Glyph], sets: list[GlyphSet]) -> None:
|
||||
@@ -52,9 +67,38 @@ class Generator:
|
||||
try:
|
||||
self.target_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
self.target_path.write_text(content)
|
||||
self.log.info("Wrote output to %s", self.target_path)
|
||||
self.log.info("Wrote glyphs output to %s", self.target_path)
|
||||
except OSError as e:
|
||||
self.log.error(
|
||||
"Could not write output file at: %s (%s)", self.target_path, e
|
||||
"Could not write glyphs output file at: %s (%s)", self.target_path, e
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
def generate_glsl_sentences(self, sentences: list[Sentence]) -> None:
|
||||
lines = [
|
||||
GLSL_SENTENCES_PREFIX.format(self.target_name.upper()),
|
||||
"",
|
||||
*Sentence.export_glsl(self.target_name, sentences),
|
||||
]
|
||||
|
||||
if self.with_functions:
|
||||
lines += [
|
||||
"",
|
||||
GLSL_SENTENCES_FUNCTIONS.format(
|
||||
self.target_name, self.target_name.upper()
|
||||
),
|
||||
]
|
||||
|
||||
lines += ["", GLSL_SENTENCES_SUFFIX.format(self.target_name.upper())]
|
||||
|
||||
content = "\n".join(lines)
|
||||
|
||||
try:
|
||||
self.target_sentences_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
self.target_sentences_path.write_text(content)
|
||||
self.log.info("Wrote sentences output to %s", self.target_path)
|
||||
except OSError as e:
|
||||
self.log.error(
|
||||
"Could not write sentences output file at: %s (%s)", self.target_path, e
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
@@ -13,6 +13,8 @@ class Parameters:
|
||||
target_path: str | None = None
|
||||
target_name: str = "glyph"
|
||||
functions: bool = False
|
||||
source_sentences_path: str | None = None
|
||||
target_sentences_path: str | None = None
|
||||
verbose: bool = False
|
||||
quiet: bool = False
|
||||
|
||||
@@ -47,9 +49,8 @@ def parse_parameters(args: typing.Sequence[str]) -> Parameters:
|
||||
"-o",
|
||||
"--output",
|
||||
metavar="OUTPUT_FILE",
|
||||
required=True,
|
||||
dest="target_path",
|
||||
help="output glsl file",
|
||||
help="output glyphs glsl file (default: build/{name}.glsl}",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-g",
|
||||
@@ -72,6 +73,18 @@ def parse_parameters(args: typing.Sequence[str]) -> Parameters:
|
||||
default=default_values.functions,
|
||||
help="add GLSL functions to output",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--sentences-input",
|
||||
metavar="SENTENCES_FILE",
|
||||
dest="source_sentences_path",
|
||||
help="generate sentences from file and names",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--sentences-output",
|
||||
metavar="OUTPUT_FILE",
|
||||
dest="target_sentences_path",
|
||||
help="output sentences glsl file (default: build/{name}_sentences.glsl)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-v",
|
||||
"--verbose",
|
||||
|
||||
@@ -5,6 +5,8 @@ import typing
|
||||
|
||||
from PIL import Image, UnidentifiedImageError
|
||||
|
||||
from glsl_glyphs.sentences import Sentence
|
||||
|
||||
from .glyph import Glyph
|
||||
from .set import GlyphSet
|
||||
|
||||
@@ -21,15 +23,24 @@ class Reader:
|
||||
self.source_sets_path = (
|
||||
pathlib.Path(params.source_sets_path) if params.source_sets_path else None
|
||||
)
|
||||
self.source_sentences_path = (
|
||||
pathlib.Path(params.source_sentences_path)
|
||||
if params.source_sentences_path
|
||||
else None
|
||||
)
|
||||
self.load_sentences = params.source_sentences_path is not None
|
||||
self.log = logging.getLogger(__name__)
|
||||
|
||||
def read(self) -> tuple[list[Glyph], list[GlyphSet]]:
|
||||
def read(self) -> tuple[list[Glyph], list[GlyphSet], list[Sentence]]:
|
||||
glyphs = self.__read_image()
|
||||
sets = self.__read_sets()
|
||||
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
|
||||
return glyphs, sets
|
||||
|
||||
return glyphs, sets, sentences
|
||||
|
||||
def __read_image(self) -> list[Glyph]:
|
||||
try:
|
||||
@@ -49,7 +60,9 @@ class Reader:
|
||||
for y in range(img.height // 8):
|
||||
for x in range(img.width // 8):
|
||||
glyphs += [self.__get_img_glyph(img, x, y)]
|
||||
self.log.info("Loaded %d glyphs from %s", len(glyphs), self.source_image_path)
|
||||
self.log.info(
|
||||
"Loaded %d glyphs from %s", len(glyphs), self.source_image_path
|
||||
)
|
||||
except (OSError, UnidentifiedImageError) as e:
|
||||
self.log.error(
|
||||
"Could not read input image at: %s (%s)", self.source_sets_path, e
|
||||
@@ -90,7 +103,9 @@ class Reader:
|
||||
for line in self.source_sets_path.read_text().splitlines()[1:]:
|
||||
index, length, name = line.split(",")
|
||||
sets += [GlyphSet(index=int(index), length=int(length), name=name)]
|
||||
self.log.info("Extracted sets from %s", self.source_sets_path)
|
||||
self.log.info(
|
||||
"Extracted %d sets from %s", len(sets), self.source_sets_path
|
||||
)
|
||||
except ValueError, TypeError:
|
||||
self.log.error(
|
||||
"Could not read sets file at: %s (invalid format)",
|
||||
@@ -109,7 +124,9 @@ class Reader:
|
||||
if self.source_names_path:
|
||||
try:
|
||||
names = self.source_names_path.read_text().splitlines()
|
||||
self.log.info("Extracted names from %s", self.source_names_path)
|
||||
self.log.info(
|
||||
"Extracted %d names from %s", len(names), self.source_names_path
|
||||
)
|
||||
except OSError as e:
|
||||
self.log.warning(
|
||||
"Could not to read names file at: %s (%s)",
|
||||
@@ -119,3 +136,27 @@ class Reader:
|
||||
else:
|
||||
self.log.info("No names file provided")
|
||||
return names
|
||||
|
||||
def __read_sentences(self, names: list[str]) -> list[Sentence]:
|
||||
sentences = []
|
||||
|
||||
if self.source_sentences_path:
|
||||
try:
|
||||
lines = self.source_sentences_path.read_text().splitlines()
|
||||
if len(lines) and len(names) == 0:
|
||||
self.log.error("Cannot extract sentences without names")
|
||||
sys.exit(1)
|
||||
for line in lines:
|
||||
sentences += [Sentence.from_text(line, names)]
|
||||
self.log.info(
|
||||
"Extracted %d sentences from %s",
|
||||
len(sentences),
|
||||
self.source_names_path,
|
||||
)
|
||||
except OSError as e:
|
||||
self.log.warning(
|
||||
"Could not to read names file at: %s (%s)",
|
||||
self.source_names_path,
|
||||
e,
|
||||
)
|
||||
return sentences
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
import dataclasses
|
||||
import logging
|
||||
|
||||
from .glsl import pretty_hex
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class Sentence:
|
||||
text: str
|
||||
data: list[int]
|
||||
|
||||
def glsl(self, max_len: int, last: bool) -> str: # noqa: FBT001
|
||||
raw_hex = ", ".join(
|
||||
pretty_hex(d) for d in self.data + [255] * (max_len - len(self.data))
|
||||
)
|
||||
if not last:
|
||||
raw_hex += ","
|
||||
else:
|
||||
raw_hex += " "
|
||||
return raw_hex + f" // {self.text}"
|
||||
|
||||
@classmethod
|
||||
def export_glsl(
|
||||
cls, name: str, sentences: list[Sentence], indent: int = 4
|
||||
) -> list[str]:
|
||||
max_len = max(len(s.data) for s in sentences)
|
||||
return (
|
||||
[
|
||||
f"#define {name.upper()}_SENTENCE_COUNT {len(sentences)}",
|
||||
f"#define {name.upper()}_SENTENCE_MAX_LEN {max_len}",
|
||||
"",
|
||||
f"const int {name}_sentences[] = int[{len(sentences) * max_len}](",
|
||||
]
|
||||
+ [
|
||||
" " * indent + s.glsl(max_len, last=i == len(sentences) - 1)
|
||||
for i, s in enumerate(sentences)
|
||||
]
|
||||
+ [
|
||||
");",
|
||||
"",
|
||||
f"const int {name}_sentences_lengths[{len(sentences)}](",
|
||||
" " * indent + ", ".join(str(len(s.data)) for s in sentences),
|
||||
");",
|
||||
]
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_text(cls, src_text: str, names: list[str]) -> Sentence:
|
||||
text = src_text
|
||||
if "EMPTY" in names:
|
||||
text = text.replace(" ", "{EMPTY}")
|
||||
else:
|
||||
log.warning("No EMPTY tile in names, removing spaces in '%s'", src_text)
|
||||
text.replace(" ", "")
|
||||
changed = True
|
||||
data = []
|
||||
remaining_text = ""
|
||||
while changed:
|
||||
changed = False
|
||||
min_name = None
|
||||
min_pattern = None
|
||||
min_index = len(text)
|
||||
for name in sorted(names, key=len, reverse=True):
|
||||
pattern = name if len(name) == 1 else f"{{{name}}}"
|
||||
if pattern in text and (index := text.index(pattern)) < min_index:
|
||||
min_name = name
|
||||
min_pattern = pattern
|
||||
min_index = index
|
||||
|
||||
if min_name is not None and min_pattern is not None:
|
||||
remaining_text += text[:min_index]
|
||||
text = text.replace(min_pattern, "", 1)
|
||||
data += [names.index(min_name)]
|
||||
changed = True
|
||||
if len(remaining_text) > 0:
|
||||
log.warning(
|
||||
"Some glyphs where not found in '%s' -> '%s'", src_text, remaining_text
|
||||
)
|
||||
return Sentence(src_text, data)
|
||||
Reference in New Issue
Block a user