puzzleworks 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. puzzleworks-0.1.0/LICENSE +21 -0
  2. puzzleworks-0.1.0/PKG-INFO +129 -0
  3. puzzleworks-0.1.0/README.md +113 -0
  4. puzzleworks-0.1.0/pyproject.toml +25 -0
  5. puzzleworks-0.1.0/setup.cfg +4 -0
  6. puzzleworks-0.1.0/src/puzzleworks/__init__.py +148 -0
  7. puzzleworks-0.1.0/src/puzzleworks/_generators/__init__.py +0 -0
  8. puzzleworks-0.1.0/src/puzzleworks/_generators/center_stage.py +235 -0
  9. puzzleworks-0.1.0/src/puzzleworks/_generators/cipher.py +1148 -0
  10. puzzleworks-0.1.0/src/puzzleworks/_generators/compound_path.py +420 -0
  11. puzzleworks-0.1.0/src/puzzleworks/_generators/criss_cross.py +1193 -0
  12. puzzleworks-0.1.0/src/puzzleworks/_generators/criss_cross_shape.py +982 -0
  13. puzzleworks-0.1.0/src/puzzleworks/_generators/crossword.py +709 -0
  14. puzzleworks-0.1.0/src/puzzleworks/_generators/jumble.py +755 -0
  15. puzzleworks-0.1.0/src/puzzleworks/_generators/masyu.py +611 -0
  16. puzzleworks-0.1.0/src/puzzleworks/_generators/maze_letter.py +1275 -0
  17. puzzleworks-0.1.0/src/puzzleworks/_generators/maze_perfect.py +1085 -0
  18. puzzleworks-0.1.0/src/puzzleworks/_generators/regions.py +825 -0
  19. puzzleworks-0.1.0/src/puzzleworks/_generators/single_word_search.py +602 -0
  20. puzzleworks-0.1.0/src/puzzleworks/_generators/stacked_puzzle.py +333 -0
  21. puzzleworks-0.1.0/src/puzzleworks/_generators/word_find.py +1018 -0
  22. puzzleworks-0.1.0/src/puzzleworks/_generators/word_ladder.py +785 -0
  23. puzzleworks-0.1.0/src/puzzleworks/_generators/word_spiral.py +520 -0
  24. puzzleworks-0.1.0/src/puzzleworks/_runtime.py +38 -0
  25. puzzleworks-0.1.0/src/puzzleworks/masks/heart.png +0 -0
  26. puzzleworks-0.1.0/src/puzzleworks.egg-info/PKG-INFO +129 -0
  27. puzzleworks-0.1.0/src/puzzleworks.egg-info/SOURCES.txt +28 -0
  28. puzzleworks-0.1.0/src/puzzleworks.egg-info/dependency_links.txt +1 -0
  29. puzzleworks-0.1.0/src/puzzleworks.egg-info/requires.txt +2 -0
  30. puzzleworks-0.1.0/src/puzzleworks.egg-info/top_level.txt +1 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 One Red Chair
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,129 @@
1
+ Metadata-Version: 2.4
2
+ Name: puzzleworks
3
+ Version: 0.1.0
4
+ Summary: Generate printable and interactive puzzles with Python.
5
+ License-Expression: MIT
6
+ Keywords: puzzles,maze,word search,crossword,printable
7
+ Classifier: Development Status :: 3 - Alpha
8
+ Classifier: Programming Language :: Python :: 3 :: Only
9
+ Classifier: Topic :: Games/Entertainment :: Puzzle Games
10
+ Requires-Python: >=3.10
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Requires-Dist: Pillow>=9.2
14
+ Requires-Dist: reportlab>=3.6
15
+ Dynamic: license-file
16
+
17
+ # PuzzleWorks
18
+
19
+ PuzzleWorks generates printable and interactive puzzles from Python. It includes 16 generators with a shared interface and a built-in heart mask for Perfect Maze and Shaped Criss-Cross.
20
+
21
+ ## Install
22
+
23
+ Python 3.10 or newer is required. After the package is published to PyPI, install it with:
24
+
25
+ ```sh
26
+ python -m pip install puzzleworks
27
+ ```
28
+
29
+ From the directory containing `pyproject.toml`, run:
30
+
31
+ ```sh
32
+ python -m pip install .
33
+ ```
34
+
35
+ ## Create a puzzle
36
+
37
+ ```python
38
+ from puzzleworks import generate
39
+
40
+ generate(
41
+ "maze-perfect",
42
+ shape="heart",
43
+ output_type="png",
44
+ output_dir="my_puzzles",
45
+ output_basename="heart_maze",
46
+ seed=42,
47
+ )
48
+ ```
49
+
50
+ This writes `heart_maze.png` and `heart_maze_solved.png` to `my_puzzles/`. For a standalone interactive version, choose `output_type="html"` and open the resulting file in a browser.
51
+
52
+ ## Use your own mask
53
+
54
+ Perfect Maze and Shaped Criss-Cross accept a black silhouette on a white background. Pass `shape="mask"` and `mask_path="your_shape.png"`. They each expose `mask_threshold` and `mask_invert` for other mask images. Perfect Maze also supports `mask_preserve_aspect=True` for custom masks. The built-in heart preserves its proportions by default. The included mask file is under `src/puzzleworks/masks/heart.png`.
55
+
56
+ ```python
57
+ entries = [
58
+ ("DOME", "Capitol topper"),
59
+ ("EQUATOR", "It keeps north and south apart"),
60
+ ("ROTUNDA", "A round room"),
61
+ ("ARC", "A curved line"),
62
+ ("CRESCENT", "A phase of the Moon"),
63
+ ("TORQUE", "A twisting force"),
64
+ ("EQUATION", "A statement of equality"),
65
+ ("NODE", "A connection point"),
66
+ ("EQUINOXES", "Days when day and night are nearly equal"),
67
+ ("SEGMENT", "A portion cut off"),
68
+ ("TRAJECTORY", "A flight path"),
69
+ ("YAWED", "Turned off course"),
70
+ ]
71
+
72
+ generate("criss-cross-shape", entries, shape="heart", output_basename="heart_chain")
73
+ ```
74
+
75
+ In a Shaped Criss-Cross, each answer ends with the next answer's first letter, including the last answer and the first.
76
+
77
+ ## Settings
78
+
79
+ Use `defaults(name)` to see every setting for a generator. The returned dictionary is a fresh copy. Pass a partial settings dictionary, keyword overrides, or both. A keyword override takes precedence over the same option in the dictionary.
80
+
81
+ ```python
82
+ from pprint import pprint
83
+ from puzzleworks import defaults, generate
84
+
85
+ pprint(defaults("word-find"))
86
+
87
+ print_settings = dict(output_type="png", dpi=300, output_dir="my_puzzles")
88
+ generate(
89
+ "word-find",
90
+ "horse, barn, arena, trail",
91
+ settings=print_settings,
92
+ output_basename="barn_words",
93
+ grid_size_width=12,
94
+ grid_size_height=12,
95
+ seed=12,
96
+ )
97
+ ```
98
+
99
+ - Use `output_dir`, `output_basename`, `output_type`, and `dpi` across generators. DPI applies to print output. Files go into `./puzzles/` by default. Set `save_to_desktop=True` to use your Desktop. An explicit `output_dir` takes precedence.
100
+ - The original per-generator setting names are available, including grouped settings in Compound Path. Compound Path also accepts shared options like `output_basename` at the top level. For nested overrides, passing `{"grid": {"rows": 5}}` keeps the other grid settings at their defaults.
101
+ - Randomized generators accept `seed`. Masyu still accepts `random_seed`, and Word Ladder still accepts `title` and `key_title` alongside `title_text` and `key_title_text`. Perfect Maze accepts both `cell_size` and the original `cell_px`.
102
+ - Set `wordlist_path` for Crossword. Use a text file with one word or `WORD;SCORE` per line. Standard grids need a large list with words of varied lengths. The repository does not bundle a full word list.
103
+
104
+ ## Generators
105
+
106
+ Pass the listed inputs to `generate()` in order after the puzzle name. Call `puzzles()` to get the names and formats in Python.
107
+
108
+ | Puzzle name | Inputs | Output formats |
109
+ | --- | --- | --- |
110
+ | `center-stage` | Across word list, down word list, center word | PNG |
111
+ | `cipher` | Phrase | HTML, PNG, PDF |
112
+ | `compound-path` | List of comma-separated paths. Add `*` after a word to hide it in the puzzle. | PNG |
113
+ | `criss-cross` | List of `(word, clue)` pairs | PNG, PDF, HTML |
114
+ | `criss-cross-shape` | List of `(word, clue)` pairs forming a closed chain | PNG, PDF |
115
+ | `crossword` | Word list configured through `wordlist_path` | PNG, PDF |
116
+ | `jumble` | Comma-separated words, solution phrase | HTML, PNG |
117
+ | `masyu` | None | PNG |
118
+ | `maze-letter` | Phrase | HTML, PNG, PDF |
119
+ | `maze-perfect` | None | HTML, PNG |
120
+ | `regions` | None | PNG, PDF |
121
+ | `single-word-search` | Word to find | PNG |
122
+ | `stacked-puzzle` | Comma-separated words, target phrase | PNG, PDF |
123
+ | `word-find` | Comma-separated words | HTML, PNG, PDF |
124
+ | `word-ladder` | A list or comma-separated sequence of words differing by one letter at each step | HTML, PNG, or both |
125
+ | `word-spiral` | Inward `(word, clue)` pairs, outward `(word, clue)` pairs. Their concatenated answers must read in opposite directions. | PNG, PDF |
126
+
127
+ ## License
128
+
129
+ PuzzleWorks is released under the MIT License. See the included `LICENSE` file.
@@ -0,0 +1,113 @@
1
+ # PuzzleWorks
2
+
3
+ PuzzleWorks generates printable and interactive puzzles from Python. It includes 16 generators with a shared interface and a built-in heart mask for Perfect Maze and Shaped Criss-Cross.
4
+
5
+ ## Install
6
+
7
+ Python 3.10 or newer is required. After the package is published to PyPI, install it with:
8
+
9
+ ```sh
10
+ python -m pip install puzzleworks
11
+ ```
12
+
13
+ From the directory containing `pyproject.toml`, run:
14
+
15
+ ```sh
16
+ python -m pip install .
17
+ ```
18
+
19
+ ## Create a puzzle
20
+
21
+ ```python
22
+ from puzzleworks import generate
23
+
24
+ generate(
25
+ "maze-perfect",
26
+ shape="heart",
27
+ output_type="png",
28
+ output_dir="my_puzzles",
29
+ output_basename="heart_maze",
30
+ seed=42,
31
+ )
32
+ ```
33
+
34
+ This writes `heart_maze.png` and `heart_maze_solved.png` to `my_puzzles/`. For a standalone interactive version, choose `output_type="html"` and open the resulting file in a browser.
35
+
36
+ ## Use your own mask
37
+
38
+ Perfect Maze and Shaped Criss-Cross accept a black silhouette on a white background. Pass `shape="mask"` and `mask_path="your_shape.png"`. They each expose `mask_threshold` and `mask_invert` for other mask images. Perfect Maze also supports `mask_preserve_aspect=True` for custom masks. The built-in heart preserves its proportions by default. The included mask file is under `src/puzzleworks/masks/heart.png`.
39
+
40
+ ```python
41
+ entries = [
42
+ ("DOME", "Capitol topper"),
43
+ ("EQUATOR", "It keeps north and south apart"),
44
+ ("ROTUNDA", "A round room"),
45
+ ("ARC", "A curved line"),
46
+ ("CRESCENT", "A phase of the Moon"),
47
+ ("TORQUE", "A twisting force"),
48
+ ("EQUATION", "A statement of equality"),
49
+ ("NODE", "A connection point"),
50
+ ("EQUINOXES", "Days when day and night are nearly equal"),
51
+ ("SEGMENT", "A portion cut off"),
52
+ ("TRAJECTORY", "A flight path"),
53
+ ("YAWED", "Turned off course"),
54
+ ]
55
+
56
+ generate("criss-cross-shape", entries, shape="heart", output_basename="heart_chain")
57
+ ```
58
+
59
+ In a Shaped Criss-Cross, each answer ends with the next answer's first letter, including the last answer and the first.
60
+
61
+ ## Settings
62
+
63
+ Use `defaults(name)` to see every setting for a generator. The returned dictionary is a fresh copy. Pass a partial settings dictionary, keyword overrides, or both. A keyword override takes precedence over the same option in the dictionary.
64
+
65
+ ```python
66
+ from pprint import pprint
67
+ from puzzleworks import defaults, generate
68
+
69
+ pprint(defaults("word-find"))
70
+
71
+ print_settings = dict(output_type="png", dpi=300, output_dir="my_puzzles")
72
+ generate(
73
+ "word-find",
74
+ "horse, barn, arena, trail",
75
+ settings=print_settings,
76
+ output_basename="barn_words",
77
+ grid_size_width=12,
78
+ grid_size_height=12,
79
+ seed=12,
80
+ )
81
+ ```
82
+
83
+ - Use `output_dir`, `output_basename`, `output_type`, and `dpi` across generators. DPI applies to print output. Files go into `./puzzles/` by default. Set `save_to_desktop=True` to use your Desktop. An explicit `output_dir` takes precedence.
84
+ - The original per-generator setting names are available, including grouped settings in Compound Path. Compound Path also accepts shared options like `output_basename` at the top level. For nested overrides, passing `{"grid": {"rows": 5}}` keeps the other grid settings at their defaults.
85
+ - Randomized generators accept `seed`. Masyu still accepts `random_seed`, and Word Ladder still accepts `title` and `key_title` alongside `title_text` and `key_title_text`. Perfect Maze accepts both `cell_size` and the original `cell_px`.
86
+ - Set `wordlist_path` for Crossword. Use a text file with one word or `WORD;SCORE` per line. Standard grids need a large list with words of varied lengths. The repository does not bundle a full word list.
87
+
88
+ ## Generators
89
+
90
+ Pass the listed inputs to `generate()` in order after the puzzle name. Call `puzzles()` to get the names and formats in Python.
91
+
92
+ | Puzzle name | Inputs | Output formats |
93
+ | --- | --- | --- |
94
+ | `center-stage` | Across word list, down word list, center word | PNG |
95
+ | `cipher` | Phrase | HTML, PNG, PDF |
96
+ | `compound-path` | List of comma-separated paths. Add `*` after a word to hide it in the puzzle. | PNG |
97
+ | `criss-cross` | List of `(word, clue)` pairs | PNG, PDF, HTML |
98
+ | `criss-cross-shape` | List of `(word, clue)` pairs forming a closed chain | PNG, PDF |
99
+ | `crossword` | Word list configured through `wordlist_path` | PNG, PDF |
100
+ | `jumble` | Comma-separated words, solution phrase | HTML, PNG |
101
+ | `masyu` | None | PNG |
102
+ | `maze-letter` | Phrase | HTML, PNG, PDF |
103
+ | `maze-perfect` | None | HTML, PNG |
104
+ | `regions` | None | PNG, PDF |
105
+ | `single-word-search` | Word to find | PNG |
106
+ | `stacked-puzzle` | Comma-separated words, target phrase | PNG, PDF |
107
+ | `word-find` | Comma-separated words | HTML, PNG, PDF |
108
+ | `word-ladder` | A list or comma-separated sequence of words differing by one letter at each step | HTML, PNG, or both |
109
+ | `word-spiral` | Inward `(word, clue)` pairs, outward `(word, clue)` pairs. Their concatenated answers must read in opposite directions. | PNG, PDF |
110
+
111
+ ## License
112
+
113
+ PuzzleWorks is released under the MIT License. See the included `LICENSE` file.
@@ -0,0 +1,25 @@
1
+ [build-system]
2
+ requires = ["setuptools>=77"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "puzzleworks"
7
+ version = "0.1.0"
8
+ description = "Generate printable and interactive puzzles with Python."
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = "MIT"
12
+ license-files = ["LICENSE"]
13
+ dependencies = ["Pillow>=9.2", "reportlab>=3.6"]
14
+ keywords = ["puzzles", "maze", "word search", "crossword", "printable"]
15
+ classifiers = [
16
+ "Development Status :: 3 - Alpha",
17
+ "Programming Language :: Python :: 3 :: Only",
18
+ "Topic :: Games/Entertainment :: Puzzle Games",
19
+ ]
20
+
21
+ [tool.setuptools.packages.find]
22
+ where = ["src"]
23
+
24
+ [tool.setuptools.package-data]
25
+ puzzleworks = ["masks/*.png"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,148 @@
1
+ """Generate printable and interactive puzzles from Python."""
2
+
3
+ from collections.abc import Mapping
4
+ from copy import deepcopy
5
+ from importlib import import_module, resources
6
+ from typing import NamedTuple
7
+
8
+ from ._runtime import generation_context, output_directory
9
+
10
+ __all__ = ["defaults", "generate", "puzzles"]
11
+
12
+
13
+ class _Puzzle(NamedTuple):
14
+ module: str
15
+ entry: str
16
+ formats: tuple[str, ...]
17
+ accepts_settings: bool = True
18
+
19
+
20
+ _PUZZLES = {
21
+ "center-stage": _Puzzle("center_stage", "generate_center_stage", ("png",)),
22
+ "cipher": _Puzzle("cipher", "generate", ("html", "png", "pdf")),
23
+ "compound-path": _Puzzle("compound_path", "generate", ("png",)),
24
+ "criss-cross": _Puzzle("criss_cross", "create_crisscross", ("png", "pdf", "html")),
25
+ "criss-cross-shape": _Puzzle("criss_cross_shape", "create_puzzle", ("png", "pdf")),
26
+ "crossword": _Puzzle("crossword", "generate_crossword", ("png", "pdf"), False),
27
+ "jumble": _Puzzle("jumble", "generate_jumble", ("html", "png")),
28
+ "masyu": _Puzzle("masyu", "generate_masyu", ("png",)),
29
+ "maze-letter": _Puzzle("maze_letter", "generate", ("html", "png", "pdf")),
30
+ "maze-perfect": _Puzzle("maze_perfect", "create_maze", ("html", "png"), False),
31
+ "regions": _Puzzle("regions", "main", ("png", "pdf"), False),
32
+ "single-word-search": _Puzzle("single_word_search", "main", ("png",), False),
33
+ "stacked-puzzle": _Puzzle("stacked_puzzle", "generate", ("png", "pdf")),
34
+ "word-find": _Puzzle("word_find", "generate", ("html", "png", "pdf")),
35
+ "word-ladder": _Puzzle("word_ladder", "main", ("html", "png", "both"), False),
36
+ "word-spiral": _Puzzle("word_spiral", "run_puzzle", ("png", "pdf"), False),
37
+ }
38
+
39
+ _COMPOUND_ALIASES = {
40
+ "output_dir": ("output", "output_dir"),
41
+ "output_basename": ("output", "basename"),
42
+ "save_to_desktop": ("output", "save_to_desktop"),
43
+ "dpi": ("output", "dpi"),
44
+ "font_path": ("text", "font_path"),
45
+ "background_color": ("colors", "background"),
46
+ "cell_size": ("grid", "cell_size"),
47
+ }
48
+ _ALIASES = {
49
+ "masyu": {"seed": "random_seed"},
50
+ "maze-perfect": {"cell_size": "cell_px"},
51
+ "word-ladder": {"title_text": "title", "key_title_text": "key_title"},
52
+ }
53
+
54
+
55
+ def puzzles() -> dict[str, tuple[str, ...]]:
56
+ """Return the supported puzzle names and their available output formats."""
57
+ return {name: puzzle.formats for name, puzzle in _PUZZLES.items()}
58
+
59
+
60
+ def _resolve(name):
61
+ canonical = name.strip().lower().replace("_", "-")
62
+ if canonical not in _PUZZLES:
63
+ raise ValueError(f"Unknown puzzle {name!r}. Choose from {', '.join(_PUZZLES)}.")
64
+ puzzle = _PUZZLES[canonical]
65
+ module = import_module(f"._generators.{puzzle.module}", __name__)
66
+ return canonical, puzzle, module
67
+
68
+
69
+ def _defaults(name, puzzle, module):
70
+ options = deepcopy(module.SETTINGS)
71
+ output = options["output"] if name == "compound-path" else options
72
+ output.update(output_dir=None, save_to_desktop=False)
73
+ options["output_type"] = options.get("output_type", puzzle.formats[0]).lower()
74
+ # These generators previously drew from Python's unconfigured global RNG.
75
+ if name in {"criss-cross", "regions", "stacked-puzzle"}:
76
+ options["seed"] = None
77
+ return options
78
+
79
+
80
+ def defaults(name: str) -> dict:
81
+ """Return a fresh copy of one generator's settings."""
82
+ return _defaults(*_resolve(name))
83
+
84
+
85
+ def _merge(target, overrides):
86
+ for key, value in overrides.items():
87
+ if key not in target:
88
+ raise ValueError(f"Unknown setting {key!r}. Use defaults(name) to see the settings.")
89
+ if isinstance(target[key], dict):
90
+ if not isinstance(value, Mapping):
91
+ raise TypeError(f"The {key!r} setting must be a mapping.")
92
+ _merge(target[key], value)
93
+ else:
94
+ target[key] = deepcopy(value)
95
+
96
+
97
+ def _apply(name, target, overrides):
98
+ if name == "compound-path":
99
+ other = {key: value for key, value in overrides.items() if key not in _COMPOUND_ALIASES}
100
+ _merge(target, other)
101
+ for alias, (group, key) in _COMPOUND_ALIASES.items():
102
+ if alias in overrides:
103
+ target[group][key] = deepcopy(overrides[alias])
104
+ else:
105
+ aliases = _ALIASES.get(name, {})
106
+ normal = {aliases.get(key, key): value for key, value in overrides.items()}
107
+ _merge(target, normal)
108
+
109
+
110
+ def generate(name: str, *inputs, settings: Mapping | None = None, **overrides) -> None:
111
+ """Generate a puzzle using its required inputs and write its files to disk."""
112
+ name, puzzle, module = _resolve(name)
113
+ options = _defaults(name, puzzle, module)
114
+ if settings is not None:
115
+ _apply(name, options, settings)
116
+ _apply(name, options, overrides)
117
+ output_type = options["output_type"].strip().lower()
118
+ if output_type not in puzzle.formats:
119
+ raise ValueError(f"{name} supports these output formats: {', '.join(puzzle.formats)}.")
120
+ options["output_type"] = output_type
121
+
122
+ output = options["output"] if name == "compound-path" else options
123
+ output["output_dir"] = output_directory(output)
124
+ output["output_dir"].mkdir(parents=True, exist_ok=True)
125
+
126
+ def run():
127
+ with generation_context(options):
128
+ entry = getattr(module, puzzle.entry)
129
+ if puzzle.accepts_settings:
130
+ entry(*inputs, settings=options)
131
+ else:
132
+ entry(*inputs)
133
+
134
+ if name in {"maze-perfect", "criss-cross-shape"}:
135
+ shape = options["shape"].strip().lower()
136
+ if shape == "heart":
137
+ options["shape"] = "mask"
138
+ if name == "maze-perfect":
139
+ options["mask_preserve_aspect"] = True
140
+ heart = resources.files(__name__).joinpath("masks", "heart.png")
141
+ with resources.as_file(heart) as path:
142
+ options["mask_path"] = path
143
+ run()
144
+ return
145
+ if options["mask_path"] and "shape" not in (settings or {}) and "shape" not in overrides:
146
+ shape = "mask"
147
+ options["shape"] = shape
148
+ run()
@@ -0,0 +1,235 @@
1
+ from pathlib import Path
2
+ from typing import List, Optional
3
+ from PIL import Image, ImageDraw, ImageFont
4
+
5
+ from .._runtime import output_directory
6
+
7
+ # =========================
8
+ # SETTINGS
9
+ # =========================
10
+ SETTINGS = {
11
+ "output_basename": "center_stage",
12
+ "dpi": 300,
13
+
14
+ "cell_size": 120,
15
+ "outer_margin": 100,
16
+ "title_gap": 100,
17
+
18
+ # Title cells
19
+ "title_cell_scale": 0.75, # 75% smaller than grid cells
20
+ "title_font_scale": 0.48,
21
+ "title_cells_bold": True,
22
+ "title_cell_gap": 8, # space between title cells
23
+
24
+ # Grid letters
25
+ "letter_font_scale": 0.55,
26
+ "outer_letters_bold": False,
27
+ "center_letters_bold": True,
28
+
29
+ # Stroke / corners
30
+ "grid_line_width": 4,
31
+ "cell_corner_radius_ratio": 0.12,
32
+
33
+ # Colors
34
+ "background_color": "#FFFFFF",
35
+ "grid_color": (0, 0, 0),
36
+ "letter_color": (0, 0, 0),
37
+
38
+ # Very dark red title
39
+ "title_color": (85, 10, 15),
40
+
41
+ # Shared shading
42
+ "shade_fill_color": (210, 220, 230),
43
+
44
+ "font_path": None,
45
+ }
46
+
47
+
48
+ # =========================
49
+ # COLOR HELPER
50
+ # =========================
51
+ def lighten_color(color, factor=0.40):
52
+ r, g, b = color
53
+ r = int(r + (255 - r) * factor)
54
+ g = int(g + (255 - g) * factor)
55
+ b = int(b + (255 - b) * factor)
56
+ return (r, g, b)
57
+
58
+
59
+ # =========================
60
+ # HELPERS
61
+ # =========================
62
+ def get_output_dir(settings):
63
+ return output_directory(settings)
64
+
65
+
66
+ def load_font(font_path: Optional[str], size: int, bold: bool = False):
67
+ if font_path:
68
+ return ImageFont.truetype(font_path, size)
69
+
70
+ candidates = []
71
+ if bold:
72
+ candidates += ["DejaVuSans-Bold.ttf", "arialbd.ttf"]
73
+ candidates += ["DejaVuSans.ttf", "arial.ttf"]
74
+
75
+ for name in candidates:
76
+ try:
77
+ return ImageFont.truetype(name, size)
78
+ except Exception:
79
+ continue
80
+ return ImageFont.load_default()
81
+
82
+
83
+ def in_shape(r, c):
84
+ return (r in (1, 2, 3)) or (c in (1, 2, 3))
85
+
86
+
87
+ def in_center(r, c):
88
+ return 1 <= r <= 3 and 1 <= c <= 3
89
+
90
+
91
+ def build_grid(across, down):
92
+ grid = [["" for _ in range(5)] for _ in range(5)]
93
+
94
+ for r in range(3):
95
+ for c in range(5):
96
+ grid[r + 1][c] = across[r].upper()[c]
97
+
98
+ for c in range(3):
99
+ for r in range(5):
100
+ grid[r][c + 1] = down[c].upper()[r]
101
+
102
+ return grid
103
+
104
+
105
+ def center_text(d, x, y, cs, text, font, fill):
106
+ bb = d.textbbox((0, 0), text, font=font)
107
+ tw = bb[2] - bb[0]
108
+ th = bb[3] - bb[1]
109
+ tx = x + (cs - tw) / 2 - bb[0]
110
+ ty = y + (cs - th) / 2 - bb[1]
111
+ d.text((tx, ty), text, font=font, fill=fill)
112
+
113
+
114
+ # =========================
115
+ # DRAW
116
+ # =========================
117
+ def draw_center_stage(across, down, center_word, settings, show_solution):
118
+
119
+ cs = settings["cell_size"]
120
+ margin = settings["outer_margin"]
121
+ lw = settings["grid_line_width"]
122
+ radius = int(cs * settings["cell_corner_radius_ratio"])
123
+
124
+ if len(center_word) != 9:
125
+ raise ValueError("center_word must be exactly 9 letters.")
126
+
127
+ center_word = center_word.upper()
128
+ grid = build_grid(across, down)
129
+
130
+ # Smaller title cells
131
+ title_cs = int(cs * settings["title_cell_scale"])
132
+ title_radius = int(title_cs * settings["cell_corner_radius_ratio"])
133
+ gap_between_title = settings.get("title_cell_gap", 0)
134
+
135
+ W = margin * 2 + cs * 9
136
+ H = margin * 2 + title_cs + settings["title_gap"] + cs * 5
137
+
138
+ img = Image.new("RGB", (W, H), settings["background_color"])
139
+ d = ImageDraw.Draw(img)
140
+
141
+ # Derived border color
142
+ accent_border = lighten_color(settings["title_color"], 0.15)
143
+
144
+ # Fonts
145
+ title_font = load_font(
146
+ settings["font_path"],
147
+ int(title_cs * settings["title_font_scale"]),
148
+ bold=settings["title_cells_bold"]
149
+ )
150
+
151
+ base_size = int(cs * settings["letter_font_scale"])
152
+ outer_font = load_font(settings["font_path"], base_size, bold=settings["outer_letters_bold"])
153
+ center_font = load_font(settings["font_path"], base_size, bold=settings["center_letters_bold"])
154
+
155
+ # -------------------------
156
+ # TITLE ROW
157
+ # -------------------------
158
+ row_w = title_cs * 9 + gap_between_title * 8
159
+ tx0 = margin + (cs * 9 - row_w) // 2
160
+ ty0 = margin
161
+
162
+ for i, ch in enumerate(center_word):
163
+ x = tx0 + i * (title_cs + gap_between_title)
164
+ y = ty0
165
+
166
+ d.rounded_rectangle(
167
+ [x, y, x + title_cs, y + title_cs],
168
+ radius=title_radius,
169
+ outline=accent_border,
170
+ width=lw,
171
+ fill=settings["shade_fill_color"]
172
+ )
173
+
174
+ center_text(d, x, y, title_cs, ch, title_font, settings["title_color"])
175
+
176
+ # -------------------------
177
+ # GRID
178
+ # -------------------------
179
+ gx = margin + (cs * 9 - cs * 5) // 2
180
+ gy = margin + title_cs + settings["title_gap"]
181
+
182
+ for r in range(5):
183
+ for c in range(5):
184
+ if not in_shape(r, c):
185
+ continue
186
+
187
+ x = gx + c * cs
188
+ y = gy + r * cs
189
+
190
+ is_center = in_center(r, c)
191
+
192
+ border_color = accent_border if is_center else settings["grid_color"]
193
+ fill = settings["shade_fill_color"] if is_center else None
194
+
195
+ d.rounded_rectangle(
196
+ [x, y, x + cs, y + cs],
197
+ radius=radius,
198
+ outline=border_color,
199
+ width=lw,
200
+ fill=fill
201
+ )
202
+
203
+ if not show_solution and is_center:
204
+ continue
205
+
206
+ letter = grid[r][c]
207
+ if letter:
208
+ font = center_font if is_center else outer_font
209
+ center_text(d, x, y, cs, letter, font, settings["letter_color"])
210
+
211
+ return img
212
+
213
+
214
+ # =========================
215
+ # GENERATOR
216
+ # =========================
217
+ def generate_center_stage(across_words, down_words, center_word, settings=SETTINGS):
218
+
219
+ puzzle = draw_center_stage(across_words, down_words, center_word, settings, False)
220
+ key = draw_center_stage(across_words, down_words, center_word, settings, True)
221
+
222
+ out_dir = get_output_dir(settings)
223
+ out_dir.mkdir(parents=True, exist_ok=True)
224
+
225
+ puzzle.save(out_dir / f"{settings['output_basename']}.png",
226
+ dpi=(settings["dpi"], settings["dpi"]))
227
+ key.save(out_dir / f"{settings['output_basename']}_key.png",
228
+ dpi=(settings["dpi"], settings["dpi"]))
229
+
230
+ print("Saved puzzle and key.")
231
+
232
+
233
+ # =========================
234
+ # EXAMPLE
235
+ # =========================