60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
import logging
|
|
import pathlib
|
|
import sys
|
|
import typing
|
|
|
|
from .constants import GLSL_FUNCTIONS, GLSL_PREFIX, GLSL_SET_FUNCTIONS, GLSL_SUFFIX
|
|
from .glyph import Glyph
|
|
from .set import GlyphSet
|
|
|
|
if typing.TYPE_CHECKING:
|
|
from .params import Parameters
|
|
|
|
|
|
class Generator:
|
|
def __init__(self, params: Parameters) -> None:
|
|
self.target_name = params.target_name
|
|
if params.target_path is None:
|
|
self.target_path = pathlib.Path("build") / f"{params.target_name}.glsl"
|
|
else:
|
|
self.target_path = pathlib.Path(params.target_path)
|
|
self.with_functions = params.functions
|
|
self.log = logging.getLogger(__name__)
|
|
|
|
def generate_glsl_glyphs(self, glyphs: list[Glyph], sets: list[GlyphSet]) -> None:
|
|
lines = [
|
|
GLSL_PREFIX.format(self.target_name.upper()),
|
|
"",
|
|
*Glyph.export_glsl(self.target_name, glyphs),
|
|
]
|
|
|
|
if len(sets):
|
|
lines += ["", *GlyphSet.export_glsl(self.target_name, sets)]
|
|
|
|
if self.with_functions:
|
|
lines += [
|
|
"",
|
|
GLSL_FUNCTIONS.format(self.target_name, self.target_name.upper()),
|
|
]
|
|
|
|
if len(sets):
|
|
lines += [
|
|
"",
|
|
GLSL_SET_FUNCTIONS.format(
|
|
self.target_name, self.target_name.upper()
|
|
),
|
|
]
|
|
|
|
lines += ["", GLSL_SUFFIX.format(self.target_name.upper())]
|
|
|
|
content = "\n".join(lines)
|
|
|
|
try:
|
|
self.target_path.parent.mkdir(parents=True, exist_ok=True)
|
|
self.target_path.write_text(content)
|
|
except OSError as e:
|
|
self.log.error(
|
|
"Could not write output file at: %s (%s)", self.target_path, e
|
|
)
|
|
sys.exit(1)
|