Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
docs(ui-kit): a drawing that fits the layout in two ways, and its refusal
Soohong Kima0a5a9eb0b0a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 51320ae..576ce40 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -4,6 +4,8 @@ All notable public changes to `sprite-gen` are recorded here. Versions track the## Unreleased+- **Fix: a UI kit drawing that fits the layout in two ways that name its elements differently is refused.** When a drawing fit the layout under more than one drift within the range a cut accepts, nothing compared the cut with other readings of the drawing, and the fit chose the reading whose parts lay nearest their boxes' centres, cutting an element under another's name when the drawing said neither which drift it was drawn with nor which element it was. On a 2:3 sheet (512 × 768 px) whose seven frames were drawn small (20 × 8 px), each inside its box both where the layout puts it and with the layout squeezed to 0.9 from the top edge, a single dot centred on `pip_on`'s box (also `pip_off`'s box squeezed to 0.9) was cut as `pip_on` and `pip_off` reported missing, and `ui-kit` exited 0, writing the kit. After the fit and its three refusals (drift, required frame, off-centre), `ui-kit` now checks the cut against every other reading of the drawing within the limits a fit accepts (along each axis, a scale from 0.875 to 1.125 and an offset of at most 120 px at 1024). A reading's shortfall adds up how far each placed part's sides fall short of lying 1 px inside its box at the drift that makes this sum least while keeping centroids in their widened boxes. An exact search finds any other reading that names the boxes differently whose shortfall is at most the cut's plus 1 px; when one is found, the sheet is refused, naming the differently named elements in layout order ([ui-kit](docs/ui-kit.md#a-drawing-that-fits-the-layout-in-two-ways)). This guarantees that a sheet whose parts lie at least 1 px inside its boxes under an accepted drift is cut under its own names or refused, never cut under another element's name. A sheet whose elements fill most of their boxes is never refused for this; the fit, `layout_fit`, the report, and the other three refusals are unchanged.+- **Tests** — synthetic sheets cover the fix: the counterexample 2:3 sheet (512 × 768 px, seven 20 × 8 px frames, one dot on `pip_on`'s centre and squeezed `pip_off`'s centre) with an 18 px dot and with a 60 px wide dot, refused by `split_ui_kit` and by the CLI (which writes nothing); the control sheet with frames 96 % as wide and 55 % as tall as their boxes squeezed to 0.9 and `pip_off` alone in the band, cut as `pip_off`; and 512 sheets of small drawings, each element a rectangle a random share of its box across (from a twentieth to nine tenths) anywhere inside its moved box at least 1 px clear of its edges, under a drift picked at random within the accepted range, with a random set of band elements left out, verifying that each is cut with exactly the band elements not drawn missing and every drawn element cut as its own drawing, or refused. A sheet whose elements are 4 px squares (16 px, the smallest part kept), each anywhere inside its moved box, with about half the band left out and sixty more such squares scattered over the sheet, is cut or refused within 10 seconds. (Because the search branches only on the parts that name the cut's boxes, not on every part, such a sheet is decided at once.)- **Fix: a drifted UI kit sheet with missing elements cuts each element under its own name instead of a neighbour's.** When a sheet was drawn shorter than its guide with elements left out, a drawn element's centroid could lie in an empty neighbouring widened box. The fit previously started from that wrong assignment and was kept only when it left fewer boxes empty; because the right assignment also left those boxes empty, the fit was dropped and the wrong assignment stood, naming a drawn part missing and an undrawn part present, and cutting a neighbour under a missing element's name. When the layout's own boxes leave a box empty, or a box holds parts whose bounding box is centred outside the box, `ui-kit` now searches a grid of 81 drifts along x and 81 along y (9 scales from 0.9 to 1.1 by 9 offsets from -96 to 96 px at 1024), fits each assignment, and scores it by how close each box's parts are to its centre. A box that holds a neighbour's element adds nothing, so the assignment that gives every drawing its own box scores highest. Repeating the fit from that best assignment until it converges finds the drift. The fit is kept unless the layout's own boxes assign every part as the fitted boxes do and each box's parts are centred in it. Along each axis, a cut accepts a scale from 0.9 to 1.1 and an offset of at most 96 px at 1024; a fit more than a grid step outside that range (a scale below 0.875 or above 1.125, or an offset beyond 120 px at 1024), or a box whose parts are centred in the gap beside it or further, refuses the sheet ([ui-kit](docs/ui-kit.md#a-drawing-that-drifted-from-the-layout)). A sheet whose every box holds parts centred in it is cut exactly as before; a sheet whose every box held parts but some of them centred outside their boxes is now fitted, with the same elements, and its report now has `layout_fit`.- **Tests** — synthetic sheets cover the fix: the reported 2:3 sheet (512 × 768 px, 6 % shorter, `hp_bar_fill`, `pip_off`, `joystick_knob` left out), tested through the CLI with the report outside `--out-dir` and through `game-pack`; the same three elements left out under squeezes from the top of 0.90 to 0.99; each of the 256 sets of band elements that can be left out, on a sheet drawn under a drift picked at random (seeded by the set) within the accepted range (a scale and an offset along x and along y), verifying that exactly the elements not drawn are missing, every drawn element is cut as its own drawing to within its 2 px edge, and a kept fit is the drift the sheet was drawn with to within a hundredth in scale and 4 px in offset; each frame left out under a random accepted drift is refused by its name; the lit dot drawn so far below its box that its centre lies in the gap (`pip_off` left out) is refused by the off-centre reason, naming `pip_on` when the centre is nearer the lit dot's box and `pip_off` when nearer the unlit dot's; a sheet drawn 20 % shorter is refused by the drift reason; and the constants match the documented range.- **Fix: the test suite runs on Windows.** `pyproject.toml` named pytest-timeout's `signal` method, and Windows has no SIGALRM, so pytest stopped with an internal error before its first test unless `-o timeout_method=thread` was given. The method is now left to pytest-timeout, which takes `signal` where SIGALRM exists (Linux, macOS: unchanged) and `thread` on Windows. Test: `tests/packaging/test_project_metadata.py`. `tests/video/test_one_step_cut.py` skips where img2webp is missing, as the other loop tests do, instead of failing all twelve of its tests.diff --git a/docs/ui-kit.md b/docs/ui-kit.mdindex eabb875..e77def7 100644--- a/docs/ui-kit.md+++ b/docs/ui-kit.md@@ -1,6 +1,6 @@# `ui-kit` — one painted sheet to nine-slice UI elements-> Owns: The UI kit layout guide, splitting a transparent UI kit sheet into elements, writing a kit whole into a new directory, fitting the layout to a drawing that drifted from it, required and missing elements, nine-slice insets, content padding and fills · Index: [docs/README.md](README.md)+> Owns: The UI kit layout guide, splitting a transparent UI kit sheet into elements, writing a kit whole into a new directory, fitting the layout to a drawing that drifted from it, refusing a drawing that fits the layout in two ways, required and missing elements, nine-slice insets, content padding and fills · Index: [docs/README.md](README.md)A game UI kit is one transparent image with every part of a game's menus and HUD on it: a panel, a button and its pressed state, a card frame, two bar frames and a timer badge, and on the taller sheet the two bars' fills, an item slot, a counter plate, a lit and an unlit dot, and a touch joystick's base and knob. `gen --layout-guide ui-kit` asks the image model to draw each element in a fixed box. `ui-kit` cuts the sheet along those boxes, measures where each element may stretch, and writes one PNG per element with a JSON report. [`game-pack`](game-pack.md#version-2-images-tiles-and-ui) reads that report. The seven frames are the kit; the taller sheet's other eight elements are optional, so a sheet that leaves one of them out still makes a kit, without it ([missing elements](#missing-elements)).@@ -71,7 +71,7 @@ The steps of the cut:1. **Alpha cleanup.** Faint haze between elements would join them, so it is cleared first (below).2. **Parts.** Pixels with alpha above zero form 8-connected parts. A part smaller than 16 pixels is dropped and counted.-3. **Assignment.** Each part goes to the box that contains its centroid, with every box widened by half the gap (24 px at 1024). When that leaves a box empty, or a box holds parts whose bounding box is centred outside the box, the layout is [fitted to the drawing](#a-drawing-that-drifted-from-the-layout) and the parts are assigned again. A part whose centroid lies in no widened box is reported under `unassigned`; it makes the report `needs-review` when it has at least 16 pixels.+3. **Assignment.** Each part goes to the box that contains its centroid, with every box widened by half the gap (24 px at 1024). When that leaves a box empty, or a box holds parts whose bounding box is centred outside the box, the layout is [fitted to the drawing](#a-drawing-that-drifted-from-the-layout) and the parts are assigned again. A part whose centroid lies in no widened box is reported under `unassigned`; it makes the report `needs-review` when it has at least 16 pixels. The assignment is then checked against every other reading of the drawing, refusing a drawing that fits the layout [in two ways](#a-drawing-that-fits-the-layout-in-two-ways).4. **Crop.** An element is the bounding box of its box's parts, so ornaments that reach past the box stay whole. A box with no part fails the command when its element is required (the sheet has to be generated again), and leaves its element out when it is optional ([missing elements](#missing-elements)).5. **Insets** and **padding** are measured on the cropped element (below).@@ -92,11 +92,32 @@ Refusals happen in this order:* The drift refusal above: `the drawing drifted further from the layout than a cut accepts: fitted to it, the layout is scaled 1.000 along x and 0.800 along y and offset 0.0, 0.0 px, where a cut accepts a scale from 0.9 to 1.1 and an offset of at most 96 px along each axis; generate the sheet again`.* The required-frame refusal: `<element>: its box holds no element; generate the sheet again`.* A box whose parts' bounding box is centred outside the box, that is in the gap beside it or further, where the drawing does not say which element they are: `pip_on: its box holds a part centred outside it (in the gap beside it or further, so the drawing does not say which element it is); generate the sheet again`, and for several boxes `pip_on, badge: their boxes hold parts centred outside them (in the gap beside them or further, so the drawing does not say which elements they are); generate the sheet again`.+* A drawing that fits the layout [in two ways](#a-drawing-that-fits-the-layout-in-two-ways) that name its elements differently: `pip_on: under another drift the cut accepts, the drawing fits the layout at least as well (to within 1 px) with this element named differently, so it does not say which element it is; generate the sheet again`, and for several elements `pip_on, pip_off: under another drift the cut accepts, the drawing fits the layout at least as well (to within 1 px) with these elements named differently, so it does not say which elements they are; generate the sheet again`.In every case `ui-kit` writes nothing and fails.A sheet whose every box holds parts centred in it is never fitted, so it is cut exactly as before. A sheet whose every box held parts but some of them centred outside their boxes (the lower elements drawn higher than their boxes, each still in its own widened box) is now fitted: its elements are the same, and its report now has `layout_fit`. The tests draw a 2:3 sheet 7 % shorter than its guide, from the top edge down: the fitted boxes find all fifteen elements whole. Each of the 256 sets of band elements that can be left out, on a sheet drawn under a drift picked at random within the accepted range, names exactly the elements not drawn as missing and cuts every drawn element as itself.+### A drawing that fits the layout in two ways++A check runs after the fit and after its three refusals (drift, required frame, off-centre). It compares the cut with every other reading of the drawing that fits the layout within the drift a cut accepts.++A reading of the drawing is the layout's boxes moved by one drift within the limits a fit accepts: along each axis, a scale from 0.875 to 1.125 and an offset of at most 120 px at 1024 (the same limits the drift refusal uses, one step of the search's grid outside the range 0.9 to 1.1 and 96 px). Each part (an 8-connected part of 16 pixels or more) is either placed in a box whose widened box holds the part's centroid (widened by half the gap, 24 px at 1024, the widening scaled and moved with the box, as in step 3 of the cut), or left out. A box that holds parts is named by its largest part, by pixel count; a box that holds none is empty.++A reading's shortfall is the sum, for each part it places and each of the part's four sides, of how far that side is from lying 1 px inside its box (0 when it does), at the drift that makes this sum least among the drifts that keep every placed part's centroid in its widened box. A part whose every side lies at least 1 px inside its box ("clear of its box's edges", as the guide asks: "without touching the box's edges") adds nothing; a part that touches or crosses an edge adds how far it falls short.++The cut names each box that holds parts by its largest part. The least shortfall of a reading that names the boxes the same way is that of those largest parts alone, each in its own box. Any other reading that names the boxes otherwise — a box held in one and empty in the other, or held by another part — places every part the cut names a box by (each in a box that is empty or holds a larger part) and leaves out any other part or makes it the largest part of a box: an empty box, or a box whose largest part it outweighs (it is larger by pixel count than every part there).++When such a reading's shortfall is at most the cut's plus 1 px, the sheet is [refused](#a-drawing-that-drifted-from-the-layout) with the fourth refusal of that list, naming the elements the two readings name differently, in layout order. `ui-kit` writes nothing and fails, as for the other refusals. This refusal comes after the other three.++The search is exact, not on a grid: with the cut's largest parts in their own boxes, the names differ only where another part is the largest part of a box (an empty one, or one whose largest part it outweighs): each such placement is checked on its own, with the cut's largest parts, against the shortfall bound. Otherwise one of the cut's largest parts lies elsewhere (in another box, or in a box with a larger part): the cut's largest parts are placed largest first, depth first, each placement kept while some drift keeps every centroid in its widened box with a shortfall within the bound; the other parts stay out (leaving a part out never adds to a shortfall), except that a box that must hold an element and is left empty is given one of them as its largest part, so every such reading is found, wherever its drift lies.++What this guarantees: a sheet whose every part lies at least 1 px inside its box under some drift within the accepted range is cut with exactly its own names (exactly the band elements not drawn are missing, each element is cut as its own drawing), or refused because another reading fits the drawing as well; it is never cut under another element's name. A drawing that fits one drift clearly better (by more than 1 px of shortfall) is named by that drift. A drawing that overflows its box the same way under both readings is refused too.++A sheet whose elements fill most of their boxes, as the guide asks, is not refused for this: another reading would have to move the frames off their boxes. The tests' 256 sets of missing band elements under random accepted drifts, and the square sheet, are cut as before.++The tests (all synthetic sheets): the counterexample 2:3 sheet (512 × 768 px) whose seven frames are drawn small (20 × 8 px each), each inside its box both where the layout puts it and with the layout squeezed to 0.9 from the top edge, and one dot centred on the centre of `pip_on`'s box (which is also the centre of `pip_off`'s box with the layout squeezed to 0.9), with an 18 px dot and with a dot 60 px wide (wider than either dot box), refused by `split_ui_kit` and by the CLI, which writes nothing; the control sheet with frames 96 % as wide and 55 % as tall as their boxes squeezed to 0.9, each inside that squeezed box, with the dot of `pip_off` alone in the band at `pip_off`'s squeezed centre (named `pip_on` instead, the dot and the frames would at best touch their boxes' edges, more than a pixel short of the squeezed reading), cut as `pip_off`; and 512 sheets of small drawings, each element a rectangle a random share of its box across (from a twentieth to nine tenths) anywhere inside its moved box at least 1 px clear of its edges, under a drift picked at random within the accepted range, with a random set of band elements left out: each is cut with exactly the band elements not drawn missing and every element cut as its own drawing, or refused. A sheet whose elements are 4 px squares (16 px, the smallest part kept), each anywhere inside its moved box, with about half the band left out and sixty more such squares scattered over the sheet, is cut or refused within 10 seconds. (Because the search branches only on the parts that name the cut's boxes, not on every part, such a sheet is decided at once.)+## Missing elementsThe seven frames are the kit, and each is required: `panel`, `button`, `button_pressed`, `card`, `hp_bar_frame`, `xp_bar_frame` and `timer_badge`. When a frame's box is empty, after the fit, `ui-kit` writes nothing and fails with `<element>: its box holds no element; generate the sheet again`, or, for several, `panel, card: their boxes hold no element; generate the sheet again`. When the layout is [fitted](#a-drawing-that-drifted-from-the-layout), a box that holds a neighbour's element is never taken for the missing element.@@ -210,7 +231,7 @@ Like every measurement here, `pass` means the numbers found nothing to flag. Loo## Python API-* `sprite_gen.frames.ui_kit.split_ui_kit(image, *, alpha_floor=64, alpha_edge=2) -> (elements, report)`: the elements as images by name, and the report as written to its default place, `<out-dir>/ui-kit.json` (each `file` is `<element>.png`), without `sheet`. A missing optional element is in neither; a `ValueError` when a required box is empty.+* `sprite_gen.frames.ui_kit.split_ui_kit(image, *, alpha_floor=64, alpha_edge=2) -> (elements, report)`: the elements as images by name, and the report as written to its default place, `<out-dir>/ui-kit.json` (each `file` is `<element>.png`), without `sheet`. A missing optional element is in neither; a `ValueError` for every refusal of the cut (a required box empty, a drift beyond the range, a box whose parts are centred outside it, a drawing that fits the layout in two ways).* `sprite_gen.frames.ui_kit.report_file(report, png) -> str` and `element_path(report, file) -> Path`: the `file` a report at `report` gives an element PNG, and the PNG a report's `file` names.* `sprite_gen.frames.ui_kit.measure_insets(image, stretch) -> (insets, notes)` and `measure_padding(image) -> (padding, notes)`: the measures above on one element image, with the review notes they raise.* `sprite_gen.frames.ui_kit.content_rect(record)` and `fit_inside(fill, frame, name) -> (inside, note)`: an element record's content rectangle, and a fill's `inside` with its review note.