Skip to content

Commit

Permalink
Automatically set glyph class in subclassed BaseVariantFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
mm21 committed Nov 29, 2024
1 parent 7b05042 commit bdec64c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,17 +454,14 @@ import itertools
from glyphsynth.lib.variants import BaseVariantExportFactory

# Factory to create variants of AMTComboGlyph
class VariantFactory(BaseVariantExportFactory[AMTComboGlyph]):
class AMTVariantFactory(BaseVariantExportFactory[AMTComboGlyph]):

# Width of resulting matrix
MATRIX_WIDTH = len(COLORS)

# Top-level padding and space between glyph variants
SPACING = UNIT / 10

# Glyph subclass from which to generate variants
glyph_cls = AMTComboGlyph

# Generate variants of colors and letter combinations
def get_params_variants(self) -> Generator[AMTComboParams, None, None]:
for color, letter1, letter2 in itertools.product(
Expand All @@ -477,4 +474,4 @@ class VariantFactory(BaseVariantExportFactory[AMTComboGlyph]):
)
```

The fully-qualified class name of `VariantFactory` can be passed as an argument to `glyphsynth-export`. This will result in a folder structure containing each variant individually, as well as the variant matrix and each individual row/column.
The fully-qualified class name of `AMTVariantFactory` can be passed as an argument to `glyphsynth-export`. This will result in a folder structure containing each variant individually, as well as the variant matrix and each individual row/column.
16 changes: 13 additions & 3 deletions glyphsynth/lib/variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
VArrayGlyph,
)

from ..core._utils import extract_type_param
from ..core.export import ExportSpec

__all__ = [
Expand All @@ -29,7 +30,7 @@ class BaseVariantFactory[GlyphT: BaseGlyph]:
Encapsulates a BaseGlyph subclass and parameter variants.
"""

glyph_cls: type[GlyphT]
_glyph_cls: type[GlyphT]
"""
BaseGlyph subclass to instantiate.
"""
Expand Down Expand Up @@ -69,13 +70,22 @@ def get_variants(self) -> Generator[GlyphT, None, None]:
"""
for params in self.get_params_variants():
glyph_id = _derive_glyph_id(params)
yield self.glyph_cls(glyph_id=glyph_id, params=params)
yield self._glyph_cls(glyph_id=glyph_id, params=params)

def get_params_variants(self) -> Generator[BaseParams, None, None]:
"""
Override to yield parameter variants to export.
"""
yield self.glyph_cls.get_params_cls()()
yield self._glyph_cls.get_params_cls()()

def __init_subclass__(cls):
"""
Populate _glyph_cls with the parameterized class.
"""
glyph_cls = extract_type_param(cls, BaseGlyph)
assert glyph_cls is not None

cls._glyph_cls = glyph_cls


class BaseVariantExportFactory[GlyphT: BaseGlyph](BaseVariantFactory[GlyphT]):
Expand Down
2 changes: 1 addition & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from glyphsynth.lib.alphabet.minimal import UNIT
from glyphsynth.lib.arrays import HArrayGlyph, VArrayGlyph

logging.basicConfig(level=logging.DEBUG)
logging.basicConfig(level=logging.INFO)

OUTPUT_PATH = Path(os.getcwd()) / "test" / "__out__"
SPACING: float = UNIT / 10
Expand Down
5 changes: 2 additions & 3 deletions test/lib/test_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ def draw(self):
letter2.rotate(180)


class VariantFactory(BaseVariantExportFactory[AMTComboGlyph]):
class AMTVariantFactory(BaseVariantExportFactory[AMTComboGlyph]):
MATRIX_WIDTH = len(COLORS)
SPACING = UNIT / 10
glyph_cls = AMTComboGlyph

# generate variants of colors and letter combinations
def get_params_variants(self) -> Generator[AMTComboParams, None, None]:
Expand Down Expand Up @@ -87,5 +86,5 @@ def test_matrix(output_dir: Path):


def test_variants(output_dir: Path):
for spec in VariantFactory():
for spec in AMTVariantFactory():
write_glyph(output_dir / spec.path, spec.glyph)

0 comments on commit bdec64c

Please sign in to comment.