Skip to content

Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.

docs(ui-kit): reading the drawing, its refusal order and its guarantee

Soohong Kim8959188898a1
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 576ce40..c0e48a8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,10 +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.
+- **Behavior change: the UI kit cut reads the drawing with one exact judgment instead of a centring heuristic.** Previously, each part went to the box that holds its centroid, with every box widened by half the gap. When that left a box empty, the layout was fitted to the drawing (least squares of box centres to the centres of what the boxes held, then assign again, at most 8 rounds) and kept when it left fewer required boxes empty, or as many and fewer boxes in all, reported as `layout_fit`. The only refusal of the cut was an empty frame. With several band elements missing on a drifted sheet, a drawn element could be cut under a neighbour's name, a drawn part named missing, or an undrawn element present; a drawing that fit the layout in two ways was cut silently under one of them; and a fit dragged by a small element placed off its box's centre gave wrong readings. The cut is the one reading within 1 px of the least cost, found by one exact search over the drifts the cut considers (a scale from 0.875 to 1.125 and an offset of at most 120 px at 1024), and every refusal comes from that one result ([ui-kit](docs/ui-kit.md#reading-the-drawing)). Every drawn element is cut under its own name, or the sheet is refused. A drawing that fits the layout in two ways is refused, naming both readings. New refusals: drifted further, ambiguous, off-centre, and off the layout. A sheet drawn on its guide is cut exactly as before, report bytes included. A sheet whose every part lies at least 1 px inside its box under some drift within the accepted range (along each axis a scale from 0.9 to 1.1 and an offset of at most 96 px at 1024), with all seven frames drawn, is cut exactly as drawn or refused as fitting the layout in two ways, never refused for another reason. A sheet with stray marks (parts in no box under the drift it was drawn with) is outside it.
+- **Tests** — synthetic sheets cover the change: the two-way ambiguous sheet (512 × 768 px, seven 20 × 8 px frames, one dot on `pip_on` and squeezed `pip_off`) with an 18 px and a 60 px dot, refused by `split_ui_kit` and the CLI; the control sheet cut as `pip_off`; 512 sheets of small drawings and 256 sheets of full-size frames with small band drawings cut as drawn or refused as ambiguous, with three sheets previously refused as drifted now cut as drawn; each of the 256 sets of missing band elements under random accepted drifts; squeezes from the top by 0.90 to 0.99; a 17-sheet counterexample search; sheets refused as drifted further (20 % shorter), off-centre (dot in the gap), and required (each frame left out); and 4 px scattered squares proved off the layout within 10 seconds. A square sheet and a 2:3 sheet drawn on their guide are cut byte for byte as before, with no `layout_fit`.
- **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.
- **Fix: `sprite-gen prepare --request` keeps the request's `style`.** The subcommand declared its own copy of prepare's options, with `--style` defaulting to the default style sentence and `--cell-size` to 256, so a flag nobody gave counted as given: the request's `style` was replaced by the default sentence under a note that `--style, --cell-size` were given both ways (`scripts/prepare_sprite_run.py` and `prepare.run` were right). The subcommand now takes prepare's own declaration (`prepare.add_arguments`), which also gives it `--directions` and `--mirror`, missing until now. A request with no style and no cell still gets the default sentence and 256. Test: `tests/gen/test_flags_over_spec.py` (through `sprite-gen prepare`: a request alone keeps its style and cell with no note, a given `--style` wins and says so, the subcommand takes every option prepare declares).
- **Behavior change: `pixel-draw` runs on your Claude subscription first, and on `ANTHROPIC_API_KEY` only without one** — the order `codex` and `grok` keep. When `claude auth status` says Claude Code is signed in with a subscription (`claude.ai` or `oauth_token`), every call — the drawings, the row repairs, the pick — goes through the unmodified `claude` binary in print mode, from an empty directory, with no tool, no session written and no CLAUDE.md, hook, skill, plugin or MCP server loaded, and costs nothing per call: `report.json` says `auth_source: claude-login`, each call's `mode` is `subscription` and its `cost_usd` `0`, its usage kept. The child never sees `ANTHROPIC_API_KEY`, which print mode would use instead of the login. Without a subscription login the run uses the key as before, billed per call and said on stderr; with neither it is refused naming both, before the run folder is made. A login that refuses a call, has expired or has reached its usage limit fails the call; the run never switches to the key on its own. `--batch` is the API's Message Batches and runs on the key even when signed in. An answer the binary continues past the output cap is read whole from every message, not from its `result`, which holds only the last part; one it cannot finish is read as `stop_reason: max_tokens`, so the one redraw one effort lower happens on either route. One call may take up to `SPRITE_GEN_CLAUDE_CALL_TIMEOUT_SECONDS` (1800). When candidates ask at once, none of their requests leaves before the billing line has been printed. `report.json`'s `auth_source` is what the calls ran on by their records, `mixed` (with `auth_sources`) for a run resumed under the other credential. A `--resume` that reads every call back no longer prints the billing line ([pixel-draw](docs/pixel-draw.md#which-credential-a-call-runs-on)).
diff --git a/docs/ui-kit.md b/docs/ui-kit.md
index e77def7..8b4da03 100644
--- a/docs/ui-kit.md
+++ b/docs/ui-kit.md
@@ -71,56 +71,54 @@ 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. 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).
+3. **Assignment.** The drawing is read once ([reading the drawing](#reading-the-drawing)). A reading puts each part in the first box, in layout order, whose moved box, widened by half the gap, holds its centroid, or in no box. A part in no box is reported under `unassigned` and makes the report `needs-review` when it has at least 16 pixels. Every refusal comes from the same reading.
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).
-### A drawing that drifted from the layout
+### Reading the drawing
-An image model may draw the layout a little off. The whole drawing can come out a few percent shorter than the guide, and then the lower an element is, the farther it sits above its box, until its centroid lies in the widened box above its own: that box holds two elements, and the element's own box none. So, when the layout's own boxes leave a box empty, or a box holds parts whose bounding box is centred outside the box:
+A drift is (sx, tx, sy, ty): it moves each layout box (x, y, w, h), scaled to the sheet, to (x · sx + tx, y · sy + ty, w · sx, h · sy). Offsets are in the sheet's pixels. Along each axis, the drift a cut accepts is a scale from 0.9 to 1.1 and an offset of at most 96 px at 1024 (48 px on a 512 px wide sheet). The cut considers the drifts within that range and a quarter of it beyond either end: a scale from 0.875 to 1.125 and an offset of at most 120 px at 1024. A drawing drawn at the edge of the accepted range is read under its own drift, and a reading just beyond the edge still counts as a rival.
-1. **Search.** The layout's boxes are moved by each drift of a grid over the range a cut accepts. Along each axis the grid has 9 scales, from 0.9 to 1.1, by 9 offsets, from -96 to 96 px at 1024 (81 drifts along x times 81 along y). For each drift the parts are assigned to the moved boxes as in step 3 of the cut (by centroid, each box widened by half the gap, the widening scaled with the box). Each such assignment is fitted (as in the Fit step) and scored: each box that holds parts adds 1 - d, where d is how far the centre of its parts' bounding box lies from the centre of its fitted box, as a share of half the box's width or height (the larger of the two shares), and nothing when that centre lies on the box's edge or outside it. A box that holds a neighbour's element so adds nothing, and the assignment that gives every drawing its own box scores highest. An assignment that holds fewer than 3 boxes is not scored.
-2. **Fit.** Along x and along y, take the scale and offset that move the layout's box centres closest (least squares) to the centres of what the boxes hold (the bounding box of each box's parts). Along an axis on which every box that holds parts is centred on one line, take an offset alone.
-3. **Assign again.** Move every box by the fit, its position and its size, and its widening by half the gap with it, and assign the parts to the moved boxes.
-4. Repeat from the Fit, starting from the search's best assignment, until the assignment comes out the same, at most 8 rounds. Fewer than 3 boxes that hold parts, or a scale that is not positive, end it without a fit. (If no drift of the grid gives an assignment of 3 boxes, there is no fit; then so many frames are empty that the sheet is refused for its empty frames.)
-5. **Keep it or not.** The layout's own boxes stand when they assign every part as the fitted boxes do and each box's parts are centred in it (then the report has no `layout_fit` and each `box` is the layout's). Otherwise the fit is kept: the report has `layout_fit` and each `box` is the fitted box.
+A reading at a drift assigns each part (an 8-connected part of 16 pixels or more, after the alpha cleanup) to the first box, in layout order, whose moved box widened by half the gap (24 px at 1024, the widening scaled with the box) holds the part's centroid, or to no box. A reading is valid when every frame's box (the seven required elements) holds a part.
-Along each axis, the drift a cut accepts is a scale from 0.9 to 1.1 and an offset of at most 96 px at 1024 (the offset is in the sheet's pixels: 96 px on a 1024 px wide sheet, 48 px on a 512 px wide one). A drawing drawn at the edge of that range, with its elements a little off their boxes' centres, is fitted a little to either side of it, so a kept fit is refused only when it lies more than one step of the grid outside the range: a scale below 0.875 or above 1.125, or an offset beyond 120 px at 1024, along either axis.
+A reading's cost, in pixels: each part in a box adds, for each of its four sides, how far that side falls short of lying 1 px inside the moved box (0 when it does: the guide asks for each element inside its box, without touching the box's edges). Each part in no box adds half the gap + 1 px + half its smaller extent (its bounding box's smaller side). That is about the least a part of its size costs against a box whose widened box does not hold its centroid. It is the same under every drift, so a part in no box never pulls the reading, and it grows with the part, so leaving a large element that overflows its box out of every box never costs less than keeping it. Two readings whose costs differ by at most 1 px fit the drawing equally well.
-Refusals happen in this order:
+One search finds the least cost of any reading under the drifts the cut considers, and the least cost of a valid reading. The candidates: when the least valid cost is within 1 px of the least cost, the valid readings within 1 px of the least valid cost. Otherwise, every reading within 1 px of the least cost (then none of them is valid). The sheet is cut as its one candidate when no refusal in the list below applies; two or more candidates refuse it as ambiguous (refusal 3) unless an earlier refusal applies. Everything the cut reports and every refusal comes from that one result: there is no second notion of the reading.
-* 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`.
+Refusals happen in this order (in every case `ui-kit` writes nothing and fails; each refusal is a `ValueError` whose `reason` is the word in parentheses):
-In every case `ui-kit` writes nothing and fails.
+1. Drifted further (`drifted`): the cheapest candidate costs more than 1 px (no drift the cut considers keeps the parts clear of their boxes' edges); the least-squares fit of the layout's box centres to the centres of what each of its boxes holds (the bounding box of the box's parts; along an axis on which every such box is centred on one line, an offset alone) lies outside 0.875–1.125 / 120 px at 1024; and at that fit the drawing, read afresh, costs more than 1 px less than the cheapest candidate. So the refusal says something about the drawing, not only about a fit: for a small or off-centre drawing a fit alone means little. Message: `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 numbers are the fit's).
+2. Required frame (`required`): the candidates are not valid. The refusal names the frames whose boxes are empty in every candidate: `<element>: its box holds no element; generate the sheet again`, for several `panel, card: their boxes hold no element; generate the sheet again`. What it means: no reading within 1 px of the best holds a part in that box. On a sheet whose frame was not drawn, that is literally so; on a sheet with stray marks (see below), the cheapest readings may leave out a tiny frame to place the marks. When no frame is empty in every candidate, the sheet is refused as ambiguous (the next refusal) instead.
+3. Ambiguous (`ambiguous`): the candidates put some part in different boxes. The refusal names the boxes whose contents differ, in layout order: `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 `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`. Readings are compared on every part's box, not only on the elements' names: each element is cut as the bounding box of its parts, so two readings that keep every name but put one small part in another element's box would cut different rects. The exception carries the two readings (`witnesses`): each its drift and the box of every part.
+4. Off-centre (`off-centre`): no drift whose cost is within 1 px of the cut's, that keeps the cut's parts in their widened boxes, puts the centre of each box's parts' bounding box inside the moved box. The refusal names the boxes whose parts are centred outside them at the cut's least-cost drift: `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`, for several `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`.
+5. Off the layout (`off-layout`): the search reached its work bound, 6000 spans of drifts, having proven that every reading costs more than 1 px: `the drawing does not lie on the layout under any drift a cut accepts: every reading leaves its parts at least N px across their boxes' edges or in no box; generate the sheet again`, N the proven least cost rounded down. A drawing whose parts lie inside their boxes has a reading of cost 0, so it is never refused this way. The bound is a count of spans, not a time, set well above what drawn sheets need.
-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.
+When the drawing is cut, each element is the bounding box of its box's parts, so ornaments that reach past the box stay whole. Parts in no box are reported under `unassigned` and make the report `needs-review`. `missing` lists the optional elements whose boxes are empty.
-### A drawing that fits the layout in two ways
+The report's drift is the first of these that reads the drawing as the cut does, at a cost within 1 px of the cut's: the layout's own boxes (then the report has no `layout_fit` and each `box` is the layout's box, so a sheet drawn on its guide is reported exactly as before); the least-squares fit of the cut (the fit described in refusal 1); the centring drift found for refusal 4; the cut's least-cost drift. Each element's `box` is its layout box moved by that drift, and `layout_fit` gives the drift.
-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.
+The guarantee: take a sheet whose parts all lie at least 1 px inside their boxes under some drift within the accepted range, with all seven frames drawn. It is cut exactly as drawn: each part under the name of the box it was drawn in, and `missing` exactly the band elements not drawn. Or it is refused as ambiguous, and the refusal names two readings that fit within 1 px and put the parts differently. It is never refused for another reason and never cut otherwise, and a cut is never one of two readings within 1 px. Why, briefly: the drawn reading costs 0, so the least cost and the least valid cost are 0, and every candidate is a valid reading with every part inside its box; the drifted and off-layout refusals need a least cost above 1 px; the required refusal needs no valid candidate, and the drawn reading is one; the off-centre refusal needs no centring drift, and the drawn drift centres every box's parts, since each part lies inside its box.
-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.
+Outside the guarantee: a sheet with parts that lie in no box under the drift it was drawn with (stray marks, specks in the gaps) is outside the guarantee. There the drawing alone does not say what was meant: a tiny element that some drift leaves in a gap and a stray mark that some drift places in a box look the same, and no rule that sees only the drawing can tell them apart. The cut prefers the reading that places parts in boxes (a part in no box costs more than any part inside its box), which keeps every drawing inside its boxes exact. On a sheet with stray marks, a mark within reach of a box may be cut as an element or joined to one, or the cheapest readings may leave out a tiny frame to place the marks, so the sheet is refused for that frame. Whatever the outcome, it is still the one reading within 1 px of the least cost, and every refusal's condition holds. Clear stray marks from the sheet, or generate it again.
-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 search is exact: no grid and no sampling. It is a best-first branch and bound over the drifts the cut considers. A span of drifts is bounded below by the least each part can cost in it. It is split across its widest side, and solved whole once its undecided parts can be read in only a few ways: each way's cost is then convex and piecewise linear along each axis, and least at a corner. "Within 1 px" means within 1 px. The search stops early once it has found two valid readings that cost 1 px or less (the drawing is ambiguous whatever else there is). It stops at its work bound only once every reading is proven to cost more than 1 px.
-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).
+The tests (all synthetic sheets):
-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.)
+* The two-way sheet: 2:3 (512 × 768 px), seven frames 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, and one dot centred on `pip_on`'s box (also `pip_off`'s box squeezed to 0.9), with an 18 px dot and a dot 60 px wide; refused as ambiguous by `split_ui_kit` and by the CLI, which writes nothing.
+* The control: frames 96 % as wide and 55 % as tall as their boxes squeezed to 0.9, each inside that squeezed box, and the dot of `pip_off` alone in the band at `pip_off`'s squeezed centre: cut as `pip_off`.
+* 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) and 256 sheets of full-size frames with such small band drawings: each is cut exactly as drawn, or refused as ambiguous, and then the test checks both readings the refusal names with its own evaluation of the reading at each drift: it reads the drawing as the refusal says, every frame holds a part, it costs at most 1 px, and the two differ. No other refusal passes.
+* Three of those sheets, which the earlier cut refused as drifted further although each fits the layout one way, are cut as drawn, by the CLI too.
+* Each of the 256 sets of band elements that can be left out, on a sheet whose elements fill most of their boxes, under a random accepted drift: exactly the elements not drawn are missing, every drawn element is cut as itself, and a `layout_fit` is the drift drawn to within a hundredth in scale and 4 px in offset.
+* A sheet squeezed from the top by 0.90 to 0.99 with three band elements left out names exactly those missing.
+* A counterexample search, which proves nothing when it finds none: on 17 sheets, thousands of drifts at random (uniform over the drifts the cut considers, and near the drift drawn and the cut's), evaluated by the test itself. None reads the drawing at a lower cost than the least the cut found, and for a cut sheet no valid reading within 1 px of the least valid cost reads it otherwise.
+* Still refused with their own reasons: a sheet drawn 20 % shorter (drifted further), the lit dot drawn into the gap below its box (off-centre, named by the box whose widened box holds it), each frame left out (required).
+* 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 decided within 10 seconds: most squares lie in no box under any drift, and the search proves every reading costs more than 1 px within its work bound and refuses it as off the layout.
+* A square sheet and a 2:3 sheet drawn on their guide are cut byte for byte as before, with no `layout_fit`.
## Missing elements
-The 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.
+The 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 in every candidate ([reading the drawing](#reading-the-drawing)), `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`. That means no reading within 1 px of the best holds a part in that box; when no frame is empty in every candidate, the sheet is refused as ambiguous instead.
The eight elements of the 2:3 band are optional: the two fills, the slot, the badge, the two dots and the joystick base and knob. When one's box is empty, the kit is made without it:
@@ -217,21 +215,21 @@ Solid pixels and edge pixels are never changed, so a stroke stays whole as long
}
```
-`box` is the layout box scaled to this sheet (its fitted box when the report has `layout_fit`) and `rect` the element's bounding box, both in sheet pixels. A fill also has [`inside`](#fills); no other element has it. `file` is the element PNG's path from the report's own directory, with `/` separators: `panel.png` when the report is in `--out-dir` (the default), `../elements/panel.png` for `--out-dir elements --report reports/kit.json`. A reader joins it to the report's directory, as [`game-pack`](game-pack.md#version-2-request) does, so a report and its elements move together. `size`, `insets` and `padding` are in pixels of the element PNG. Elements appear in layout order. `status` is `needs-review` when any `review_reasons` apply; the PNGs are written either way, so the elements can be inspected.
+`box` is the layout box scaled to this sheet (its moved box when the report has `layout_fit`) and `rect` the element's bounding box, both in sheet pixels. A fill also has [`inside`](#fills); no other element has it. `file` is the element PNG's path from the report's own directory, with `/` separators: `panel.png` when the report is in `--out-dir` (the default), `../elements/panel.png` for `--out-dir elements --report reports/kit.json`. A reader joins it to the report's directory, as [`game-pack`](game-pack.md#version-2-request) does, so a report and its elements move together. `size`, `insets` and `padding` are in pixels of the element PNG. Elements appear in layout order. `status` is `needs-review` when any `review_reasons` apply; the PNGs are written either way, so the elements can be inspected.
-A 2:3 report also has `missing`, the optional elements whose boxes were empty, in layout order ([missing elements](#missing-elements)). When the layout was [fitted to the drawing](#a-drawing-that-drifted-from-the-layout), the report has `layout_fit`, after `missing` and before `elements`; for a synthetic 1024 × 1536 sheet drawn 7 % shorter than its guide:
+A 2:3 report also has `missing`, the optional elements whose boxes were empty, in layout order ([missing elements](#missing-elements)). When the layout was moved to read the drawing ([reading the drawing](#reading-the-drawing)), the report has `layout_fit`, after `missing` and before `elements`; for a synthetic 1024 × 1536 sheet drawn 7 % shorter than its guide:
```json
"layout_fit": { "scale": { "x": 0.9969, "y": 0.9301 }, "offset": { "x": -0.2, "y": 0.0 } }
```
-A layout box at (x, y, w, h), scaled to the sheet, was fitted to (x · scale.x + offset.x, y · scale.y + offset.y, w · scale.x, h · scale.y). `offset` is in sheet pixels, rounded to a tenth; `scale` is rounded to four decimals.
+A layout box at (x, y, w, h), scaled to the sheet, is moved to (x · scale.x + offset.x, y · scale.y + offset.y, w · scale.x, h · scale.y). `offset` is in sheet pixels, rounded to a tenth; `scale` is rounded to four decimals.
Like every measurement here, `pass` means the numbers found nothing to flag. Look at the elements stretched to a few sizes before shipping them.
## 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` 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.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. The least cost of a cut sheet is not in the report. `split_ui_kit` raises `sprite_gen.frames.ui_kit.Refusal`, a `ValueError`, for every refusal of the cut. Its message is the refusal's. `reason` is one of `drifted`, `required`, `ambiguous`, `off-centre`, `off-layout`. `least` is the least cost of a reading the search found, or for `off-layout` the cost every reading is proven to exceed. `parts` are the parts read. For `ambiguous`, `witnesses` holds the two readings (each a `Reading`: `drift` (sx, tx, sy, ty), `boxes`, the box of each part of `parts` by name, None for none, and `cost`). The shape refusal (neither square nor 2:3) stays a plain `ValueError`, as before.
* `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.