Files
uberbringer/docs/plans/blocks.md
T
57_WolveandClaude Opus 5.5 709e7904e9
Security / Vulnerability check (push) Successful in 43s
Test / Lint and test (push) Successful in 2m6s
Fold the formatters plan into design.md
Phases 1-4 of docs/plans/formatters.md are built and described in
design.md, and every phase 5 item now has a plan of its own, under
Planned next. So the plan goes, and what still matters moves into
design.md.

A new decision says that existing jobs keep their meaning and their
bytes: /print stays strictly decoded, TestRenderCompat and
TestPaperReceipts pin the bytes, and Renderer.Render is the only path
to printer bytes. The labels and footer settings join "Modules build
ordinary jobs". Deferred gains nested boxes and goldmark, and the
geometry checks in the HTTP handler that stay rejected. The plan's
other invariants were already design.md decisions.

The plans that cited formatters.md now cite design.md, or drop the
reference: job-store.md, printers.md, blocks.md and templates.md.
job-store.md's phase 5 list moves to design.md's Deferred section once
its other phases are done.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 11:09:30 -06:00

47 KiB
Raw Blame History

Plan: more blocks

Status: planned; nothing is built. The owner accepted every proposed default on 2026-12-01 (Decisions), and design.md's Planned next item for more blocks records them. Each block is built when it is needed, one at a time, so the five stages are independent. Each runs alone in its own session, briefed from its section, in any order; the Overview suggests one. Decisions 2 and 3 apply to every stage, and decision 1 to stages 1-3, which add glyphs. Stage 1 follows decisions 4-6, stage 2 decisions 7-14, stage 3 decisions 15-17, stage 4 decisions 18-21 and stage 5 decisions 22-26. Every paper check needs the owner's OK. A stage's last commit deletes its section here, and moves the stage's decisions from that Planned next item into design.md's bullet "Blocks are laid out when the job renders". Whichever stage finishes last deletes this plan, its findings with it, and design.md's Planned next item for more blocks.

Decisions (owner, 2026-11-30)

  • More blocks are planned as a feature of their own: a table (with ╤╪╧ junctions), a banner, a group, inline spans and a custom rule character.
  • Each is built when it is needed, one at a time.

The owner accepted the details below on 2026-12-01. They are numbered, so the rest of the plan can refer to them.

For every stage:

  1. Glyphs the code page lacks. Each new glyph set falls back as the box styles do (glyphs.go:29-35): double-h and double-v to double, then single, then ascii. A set falls back whole when the code page lacks any of its glyphs, junctions included, as bordersFor already checks corners and lines (glyphs.go:40-57). A fallback is silent, as for boxes and rules; the preview shows the glyphs used. Investigation findings, GLYPHS BY CODE PAGE, lists what each page has.
    • cp858 is cp850 with € in place of ı, so every box, junction and shade glyph is the same. The goldens stay in cp437, cp850 and cp1252 (golden_test.go:22). Each of stages 1-3 adds a unit test that its glyph choice on cp858 equals cp850's.
    • Trade-off: a user on cp850 who asks for double-h gets double without being told.
  2. Proving each block on paper. Each stage adds one or two short receipts, examples/<block>.json and, where one would be long, a second. They show the new block's styles and edge cases. TestExampleJobs keeps every example valid with nothing printed as ? (golden_test.go:62), so an example grows with the commits of its stage. With the owner's OK each prints on the TM-T88IV, one at a time. Once he passes one, its bytes are pinned in TestPaperReceipts (compat_test.go:100) as testdata/paper/<block>.hex, rendered with the cut feed it printed with (cutFeed: 3, the daemon's default). The pin folds into the stage's last feature commit (git commit --fixup, then git rebase --autosquash main), whose body says it was checked on paper on the TM-T88IV.
    • Trade-off: one more pinned receipt per block, regenerated only after a new paper check.
  3. Modules. No module changes in these stages. A module's output is pinned on paper (testdata/paper/ntfy-*.hex, note.hex), so changing it is a change of its own, with its own paper check, made when the owner asks. The candidates:
    • spans: the markdown subset keeps **bold** as bold instead of dropping the marks (markdown.go:53);
    • banner: the urgent card's URGENT heading, today a dark rule with a bold label (card.go:196);
    • table: a digest's rows, today text lines of time and title (digest.go:68).
    • Trade-off: the new blocks first reach receipts only through /print.

For stage 1, the rule character:

  1. The field and the characters it takes. A new field char on rule, one character that repeats across the line in place of the style's pattern: {"type": "rule", "char": "*", "text": "TODAY"}. Validation is structural: exactly one rune as sent, which is not a control character, not a newline or tab, not a combining mark (Unicode Mn or Me), not a format character (Cf, such as the soft hyphen and the bidi controls) and not one of the invisible runes normalize drops (normalize.go:165-176). A space is allowed: a rule of spaces centres its label. "char": "" means unset, as an empty string does on every other field, so the tag is maxLength=1 alone, which counts code points as validation does; TestBoundTagsMatchValidation learns to check a field with an upper bound only.
    • A decomposed é, an e and a combining accent, is two runes and is refused, although NFC would compose it. Trade-off: a client must send composed text.
  2. The fallback where the code page lacks the character. The character is composed to NFC first, as text is, so Å (U+212B) becomes Å (U+00C5). Then, in order, the character itself; its fold, when the fold is one character the page prints (• folds to ·, normalize.go:26); then the pattern of the rule's style, which defaults to single and itself ends in - (glyphs.go:98-108). So "char": "♦", "style": "double" prints ═ on cp850 and = on cp1252. Folds of several characters (→ to ->, € to EUR) are not used as patterns. The fallback counts no ?, so the preview gives no warning.
    • Trade-off: a character the page lacks never prints as ?, so a client does not learn it fell back unless it reads the preview.
  3. Inside a box. char is refused on a divider inside a box (field "char" does not apply to rule inside a box). A divider joins the side borders with junctions (glyphs.go:77-94), and only ─ and ═ have junctions.
    • Trade-off: a card cannot have a * * * divider.

For stage 2, the table:

  1. The JSON shape. The names are those of decision 14:
    {"type": "table", "style": "double-h",
     "cols": [{"head": "Item"}, {"head": "Qty", "align": "right"}, {"head": "Price", "align": "right"}],
     "rows": [["Flat white", "2", "7.00"], ["Almond croissant", "1", "3.50"]],
     "foot": [["Total", "3", "10.50"]]}
    
    • cols defines the columns, one object each: head (the header text, may be empty), align (left, center or right; default left), width (a fixed width in characters) and grow (decision 9).
    • rows are the body, each an array of exactly as many strings as cols; a cell may be empty.
    • foot are rows printed after a separator, in bold, for totals.
    • columns, size, font, padding and fit mean what they mean on a box.
    • The header row prints, in bold, when any head is not empty.
    • Trade-off: a row cannot be bold unless it is in foot, and one table has one header.
  2. Borders. style takes the box styles, single, double, double-h, double-v and ascii, and a new none. The default is double-h, the look the owner named: ╒═╤═╕, │, ╞═╪═╡, ╘═╧═╛. The header and foot separators use the frame's horizontal line, so a double-h table has ╞═╪═╡. No lines run between body rows.
    • none draws no frame. Columns are separated by the blank cell where a vertical line would be, with the padding on each side, so the gap is 1 + 2 × padding spaces. The header and foot separators are a rule of ─ across the table, - where the page lacks it.
    • On cp850 and cp858, double-h and double-v print as double (╔═╦═╗, ╠═╬═╣, ╚═╩═╝). On cp1252 every frame is ascii: + at every corner and junction, - and |, and = for the separators when the style asked for double horizontal lines, as dividers do (glyphs.go:90-93).
    • Trade-off: the default differs from the box's, single.
  3. Column widths. A column with width is fixed at that many characters; the others size by content. A column may set grow, a weight from 1 to 10, to take spare room; grow and width together are refused. Architecture, TABLE LAYOUT, gives the exact rule:
    • each auto column's least width is its longest word and its natural width its longest line, header included;
    • widths are given out in rounds, fixed widths and least widths first, then natural widths, each round shared in proportion to what each column still wants;
    • spare room goes to the columns with grow in proportion to it, or, when none has grow, to the first auto column, where receipts put the item's name. A table whose columns are all fixed shrinks to them and is centred.
    • fit: true shrinks the table to its natural width and centres it, as it does a box; columns is then the most it may take.
    • Shares are whole characters, given by largest remainder, ties to the leftmost column, so a layout is the same on every run.
    • A fixed width is what a column gets when the room allows. On a line too narrow for every column's want, a fixed column shrinks with the others, so the table still fits.
    • Trade-off: a table whose widest column is not the first must say grow.
  4. Wrapping in cells. Each cell wraps at spaces to its column's width with today's wrap (layout.go:56-68); \n starts a new paragraph; only a word longer than the column splits. A row is as tall as its tallest cell; the others are filled with blank lines below their text. Each line of a cell is aligned as its column; header cells too. Cells are normalised as box text is (blocklayout.go:33-37), tabs expanded from the cell's own left edge.
    • Trade-off: no vertical alignment other than top, and no alignment on a decimal point.
  5. When the columns do not fit the paper. Layout never fails on a valid job (design.md, "Blocks are laid out when the job renders"; invariant 2 here), so a table always prints. In order:
    • Padding goes first, as a narrow box gives up its padding before its content (blocklayout.go:139-141). It drops, one step at a time, while the room is less than every column's want: its longest word, or less where its natural or fixed width is less.
    • Each column needs its want counted up to 8 characters. While the room at padding 0 holds every column's need, the table keeps its columns, and only words longer than 8 characters can split.
    • Below that, the table prints stacked: each row as key-value lines, the column's head as the key and the cell as the value, laid out as a kv block is (blocklayout.go:386-410), with a . leader. Where a head would be cut, which kvLines does to a key longer than the width less 8, the whole table uses the narrow form instead: each head on a line of its own, the cell below it, right-aligned (blocklayout.go:475-490). A cell whose column has no head prints alone, on the left. Rows are separated by a rule in the frame's horizontal line; foot rows follow, in bold.
    • Decision 7's table needs 8 + 3 + 5 = 16 characters for its cells and 4 for its borders, so it stacks below 20: on a TM-T88 at size 3 (14 characters), not at size 2 (21).
    • Trade-off: a stacked table looks unlike the table asked for, but no word of up to 8 characters is split.
  6. Limits. 1-8 columns; 1-100 rows; 0-10 foot rows; each row exactly as many cells as cols; width 0-255; grow 0-10; padding 0-3. A row with the wrong number of cells is a problem named by its index, rows[3]: 2 cells, want 3. Rows and cells do not count toward the 500-element limit, as kv pairs do not. The text a table holds is bounded by webserver.body_limit (4 MiB) and its lines by format.max_lines.
    • Today max_lines is checked only once the whole job is laid out (render.go:148-151), so 500 tables each just under the limit, or one cell of 4 MiB in a narrow column, build about a million lines first. Stage 2 therefore first makes the renderer stop as soon as the lines so far, times the copies, pass max_lines, checked after each element, at each table row and inside a cell's wrap. The error then reads receipt would print at least 2001 lines, limit is 2000, which changes the message api.md quotes (api.md:195-198), under Changed in the CHANGELOG. The code, too_many_lines, stays.
    • The tags: cols minItems=1,maxItems=8; rows minItems=1,maxItems=100; foot maxItems=10 alone, since no foot is the default; in Column, width minimum=0,maximum=255, grow minimum=0,maximum=10 and align enum=left,enum=center,enum=right. No tag can say that a row has as many cells as cols; validation holds that rule, and the field's doc comment states it. TestBoundTagsMatchValidation gains rows for Column, and TestEnumTagsMatchValidation's enumTag (schema_test.go:16-29) takes the type, so that Column.Align is held too.
    • Trade-off: a long order of more than 100 lines needs two tables; a job over the limit is told only a lower bound on its lines.
  7. A table inside a box. Allowed, one level, as kv and list are. It takes cols, rows, foot, style and padding there; size and font come from the box, and the table spans the box's content width. A table narrower than that, all fixed or fit, is centred in it and padded with spaces to it, so every box line keeps its width. A fit box measures the table at its natural width. A framed table inside a box draws its own frame inside the box's; api.md suggests none there.
    • Trade-off: a framed table inside a frame is busy, and nothing stops it.
  8. The JSON field names. cols, rows, foot, and in a column head, align, width and grow. Not columns for the column definitions: on every block columns is already the block's width in characters (job.go:170-173).
    • Trade-off: cols beside columns, and foot (rows) beside a box's footer (text in its bottom border), can be misread.

For stage 3, the banner:

  1. What a banner is. Large text that spans the line, for a heading such as ORDER 42: {"type": "banner", "text": "ORDER 42"}. Its fields are text (required), style, size, align (default center), bold and font. Three styles:
    • band, the default: white on black. Each line is padded with spaces to the banner's columns, the text placed in it by align, and printed reversed, so the band covers the whole line.
    • plain: the large text alone.
    • ruled: a line of ▄ above and of ▀ below, at size 1, full width; = where the page lacks them (cp1252).
    • Every banner line, the ruled lines included, is sent centred (ESC a 1) with no margin spaces, so the printer centres it in its print area and the dots left over split evenly at both edges. They are fewer than one character at the line's size: 32 dots for 10 columns of size 4 on 512 dots. A plain line is sent with the ESC a of its align instead.
    • The banner's columns come from the smaller of the line's dots and the profile's, so that a printer.columns wider than the paper cannot make the printer wrap a band.
    • Trade-off: the band is narrower than the paper by up to one character less a dot, split between the edges. On the TM-T88's 512 dots in Font A that is 32 dots at sizes 4, 5 and 8, and 8 dots at the other sizes. With printer.columns narrower than the paper, a banner centres on the paper while the other blocks centre within the narrower line.
  2. Its size. size is 1-8, or 0 for automatic. Automatic picks the largest size from 4 down to 2 at which every paragraph of the text fits on one line: the line holds lineDots / (glyph × size) characters, as blockColumns computes (render.go:484-486). On a TM-T88 in Font A that is 10, 14 or 21 characters; on 58 mm paper 8, 10 or 16. Text too long at size 2 wraps at size 2. Width and height are always equal.
    • Trade-off: automatic never goes past size 4, since each line of size 8 is 192 dots, 27 mm, of paper.
  3. Where a banner may go. At the top level and, once stage 5 has shipped, in a group; never in a box. A box prints every line at its one size (render.go:456), so its borders could not join a banner's lines.
    • Trade-off: a card that wants a big heading puts the banner above the box.

For stage 4, inline spans:

  1. The shape. A text element takes spans in place of text: an array of runs, each with text (required, may hold \n), bold, underline (0-2), invert, and at the top level size, width and height (1-8 each):
    {"type": "text", "align": "center", "spans": [
      {"text": "Table 4 · "}, {"text": "2 guests", "bold": true},
      {"text": " · "}, {"text": "ALLERGY", "invert": true, "size": 2}]}
    
    • With spans, the element keeps font, align and newline; its own text, bold, underline, invert, size, width and height are refused, so a style is never set in two places. "wrap": false is refused too, since the printer's own wrapping and the position after it assume one character width (render.go:382-386, render.go:508-519). 1-100 spans; a span's text may not be empty.
    • The tags: spans minItems=1,maxItems=100; in Span, text required,minLength=1, underline minimum=0,maximum=2, and size, width and height minimum=0,maximum=8, 0 meaning the default.
    • Text today already continues a line with "newline": false and wraps across sizes (render.go:354-392, TestRenderInlineMixedSizesWraps). Spans add what that cannot do: a word that changes style part-way stays one word; a mixed line can be centred or right-aligned as one line, since ESC a takes effect only at the start of a line (Investigation findings, ESC/POS); spans work inside a box, where "newline": false is refused (job.go:295-297); and one element replaces several towards the 500-element limit.
    • Trade-off: a field that only text takes; the older types refuse it, as they refuse optional (block.go:93-96).
  2. Wrapping with mixed sizes. Wrap measures in dots. A character of a run of width multiplier w takes w × glyph dots; a line holds lineDots dots, less the position already used, as today. Words are found across runs, so **8**.50 is one word. Each run is normalised on its own, so a combining mark at the start of a run cannot compose with the run before it. The printer aligns characters of different heights on one line at their baseline (Investigation findings, ESC/POS).
    • For text of one size the new wrap gives exactly today's lines (layout.go:56-115), so the compat fixtures wrap, inline and text-styles keep their bytes. It keeps today's rules, each of which a test pins: a run of spaces fits whole or not at all, so wrap(" word", 2, 10) gives ["", "word"]; a first word that does not fit a continued line leaves an empty first line; a word longer than the whole line moves to a fresh line before it splits at the edge; the last line keeps its trailing spaces, which "newline": false relies on; and where a character is wider than the whole line, as with printer.columns of 1 at size 2, the first line is empty and each character takes a line, as wrap("ab", 0, 1) gives ["", "a", "b"].
    • A tab fills with spaces of its run's width up to the next multiple of 8 such characters from the line's start, at least one; for text of one size that is today's expandTabs from p.x / glyph (render.go:373).
    • Stage 4's first commit makes the change alone. Today's wrap moves into a _test.go file as the reference that a property test compares with; a copy outside the tests, with no caller, would fail staticcheck.
    • Trade-off: none known.
  3. Spans in other blocks. Spans only in text, at the top level and as a text child of a box. kv values, list items, table cells and banners stay plain strings.
    • Trade-off: a bold word in a table cell must wait.
  4. Sizes inside a box. Inside a box, spans take bold, underline and invert only. A box lays out in cells of one size and prints each line at that size (render.go:456), and a taller span would leave the side borders short on its line.
    • Trade-off: no large word inside a card.

For stage 5, the group:

  1. What a group holds. {"type": "group", "title": "Kitchen", "children": [...]}. Its children are at least one element of any top-level type but group, raw and cut. They have no limit of their own, as a box's children have none: children of a group and children of a box inside it count toward the 500-element limit. children is shared with a box, so a limit of a group's own could not go into its tag. A group cannot go inside a box. It prints its heading, then each child as if it stood at the top level.
    • Templates do not need it: Expr's concat and flatten turn one value into several elements of a flat list (docs/plans/templates.md, decision 11). A group gives a heading and a shared font where one is wanted.
    • raw is refused because the token scope is checked on top-level elements only (server/auth.go:135-150); stage 5 makes that check walk groups too, so the refusal does not rest on validation alone. cut is refused so that a group is never split by a cut.
    • Trade-off: no group inside a group.
  2. Keep-together. A group adds nothing beyond what every receipt has. A job renders to one stream and is sent on one connection (design.md:39-46), so no other job's lines can come between a group's lines, and a receipt has no pages to break across. A group refuses cut (decision 22), so no cut falls inside one. The plan names keep-together only to record that it means nothing more on a receipt.
    • Trade-off: none known.
  3. Shared style. The group's font and size (1-3) are the defaults for children that take them and do not set their own.
    • font: text, box, rule, kv, list, and, once their stages have shipped, table and banner.
    • size: the same, less the banner, whose 0 means automatic, and less a text that sets width, height or spans, which carry their own sizes. A qr's size is its module size, so it never takes the group's.
    • A child's own value wins, unlike a box, whose children may not set them (block.go:115-116).
    • Trade-off: a group's size can make a child's block wider than it was written for, as any size does.
  4. The heading. title prints as a rule with the title set in on the left, ── Kitchen ──────────, in the group's font and size. style picks the rule's line from the rule styles, and title_bold prints the title bold. No title, no heading.
    • Trade-off: one look for every heading.
  5. Paths in errors and warnings. A child is named by its path, elements[2].children[1], and a box child inside it elements[2].children[1].children[0], in validation problems, render errors and preview warnings. Images inside a group are loaded before anything renders, as every image is (render.go:175-193), keyed by path.
    • Trade-off: none known.

Investigation findings

Checked against the code at 10b6c57, offline: CanEncode over each glyph in the four code pages, and the files cited read. Nothing was printed, and no config.toml was read.

GLYPHS BY CODE PAGE Checked with escpos.CodePage.CanEncode (codepage.go:105-118), which counts a glyph only where it maps to a byte of 0x20 or above, other than DEL:

Glyphs cp437 cp850, cp858 cp1252
single ┌┐└┘─│├┤┬┴┼ all all none
double ╔╗╚╝═║╠╣╦╩╬ all all none
double-h ╒╕╘╛╞╡╤╧╪ all none none
double-v ╓╖╙╜╟╢╥╨╫ all none none
shades and blocks ░▒▓█▀▄▌▐■ all all but ▌▐ none
rule characters *~=#+_^· all all all
∙ ≈ ≡ all none none
¤ § × ¯ ¦ none all all
• none none prints
▪ ♦ ◆ ★ ● ○ ◦ none none none

So the ╤╪╧ junctions print only on cp437; on cp850 and cp858 a double-h table falls back to double whole, and on cp1252 to ascii. cp850 and cp858 differ only at 0xD5 (ı and €).

THE CODE THE BLOCKS SHARE

  • A block's width is the line in characters of its own size and font: lineDots / (glyph × size) (render.go:484-486), where lineDots is the profile's dots or printer.columns × the Font A width (render.go:168-173). columns narrows a block, which is then centred (blocklayout.go:46-64).
  • Blocks lay out in cells: after normalize, one rune is one cell (cells.go:18-22, normalize.go:37-44), and wrap counts runes (layout.go:48-115). A block prints every line at one size and font, changing only bold, underline and invert between runs (render.go:409-499).
  • Validation is structural and reports every problem (job.go:228-254). Children are checked one level deep, for a box only (job.go:262-274). The element count adds box children (job.go:232-243). Fields are checked per type by tables (block.go:52-113).
  • Text, as opposed to blocks, tracks the print position in dots and wraps across elements of different sizes (render.go:354-392).
  • Render validates, loads every image, then lays out the whole job before it returns a byte (render.go:91-107); max_lines is checked on the complete job (render.go:148-151). The preview renders the same way and reads the bytes back (preview.go:35-78).
  • Images, unknown-character counts and preview image labels are keyed by top-level index (render.go:134-143, render.go:178-193, preview.go:84-130). The token's raw scope is checked on top-level elements (server/auth.go:135-150).
  • The style enum on Element lists the box and rule styles together (job.go:160-163). TestEnumTagsMatchValidation holds each enum to validation, a value being valid when one of the listed elements accepts it (schema_test.go:86-138); TestBoundTagsMatchValidation does the same for limits (schema_test.go:163).

ESC/POS

  • Unverified, since Epson's reference refuses automated fetches: ESC a takes effect only at the start of a line in standard mode, and characters of different heights on one line align at their baseline (GS !). The preview already models the first (interpret.go:220-224); it does not model the second, so only paper shows it.
  • Reverse printing (GS B) covers spaces, so spaces padded to the line make a band. Whether two lines of a reversed banner touch or leave a white gap depends on the line spacing against the character height.

WHERE THE FINDINGS ARE UNSURE

  • ESC a at the start of a line only, and baseline alignment under GS !: not read in the reference. Stages 3 and 4 read them before their code.
  • Whether the lines of a reversed banner touch: stage 3's paper check settles it.

THE OTHER PLANS

  • job-store.md's preview image (its phase 3a) draws box drawing and shading from the cell geometry, every other glyph from a bitmap font. The new junctions are box drawing and need nothing more. Banners and spans need drawing at a run's W and H, which escpos.Run already carries (interpret.go:29-38). That phase's golden PNGs include each block that has shipped.
  • printers.md: each printer renders with its own profile, so a table can stack on a narrow printer and not on a wide one. format.max_lines is global (printers.md, the keys that belong to each printer), so a table that stacks into more lines can fail on one printer of a multicast.
  • remote-printers.md, the decision on where rendering happens: main renders in full and the agent writes the bytes unchanged. Nothing here changes that.

SOURCES

Overview

Five blocks, each built alone when a need appears. Suggested order, when needs are equal:

  1. Rule character (stage 1): the smallest, a field on an existing block.
  2. Table (stage 2): the block the owner described in most detail, and the one an order or a status list needs. The largest stage.
  3. Banner (stage 3): small, and a candidate for the urgent card.
  4. Inline spans (stage 4): touches the text path that every receipt uses, so its refactor is proven alone first.
  5. Group (stage 5): a heading and a shared font over several elements, built when one is wanted. Templates do not need it (docs/plans/templates.md, decision 11).

What prints, on cp437 and a TM-T88 at size 1:

***************** TODAY ******************

╒══════════════════════════╤═════╤═══════╕
│ Item                     │ Qty │ Price │
╞══════════════════════════╪═════╪═══════╡
│ Flat white               │   2 │  7.00 │
│ Almond croissant         │   1 │  3.50 │
╞══════════════════════════╪═════╪═══════╡
│ Total                    │   3 │ 10.50 │
╘══════════════════════════╧═════╧═══════╛

── Kitchen ───────────────────────────────
Prep for 18:00

The rule is {"type": "rule", "char": "*", "text": "TODAY"}, and the table is decision 7's example. Columns 2 and 3 are as wide as their widest cells, and the first takes the rest: 42 characters less 4 borders and 6 spaces of padding leave 32, of which Qty takes 3 and Price 5. The last two lines are a group's heading and its first child. A banner prints ORDER 42 at size 4 as one white-on-black band of 10 characters, and spans print styled words within one wrapped line, which a text preview shows as plain text.

The same table at size 3, 14 characters wide, stacks. Its first row and the separator after it:

Item .... Flat
         white
Qty ........ 2
Price ... 7.00
══════════════

COMPATIBILITY

  • Every existing element, field and response keeps its meaning. TestRenderCompat, TestPaperReceipts and the block goldens keep their bytes.
  • New types and fields only. /print stays strictly decoded, so an older daemon refuses a job that uses them, as it refuses any unknown field.
  • The style enum gains none, band, plain and ruled; the type enum gains table, banner and group.
  • A job over format.max_lines is refused with a message that gives a lower bound on its lines (decision 12).

INVARIANTS

  1. Nothing reaches the printer unless the whole receipt rendered. Every new block lays out inside Renderer.render, before Render returns a byte.
  2. Layout never fails on a valid job. Widths clamp, padding gives way, tables stack, banners wrap. The only render-time failures stay max_lines and images.
  3. Validation stays structural. No problem depends on the printer's width or code page.
  4. Jobs stay printer-neutral. Every width comes from the profile, or printer.columns, at render time; nothing hard-codes 42 or 512.
  5. Text stays text. Every new string is normalised and stripped of control characters before it is encoded, and no new field sends bytes as given.
  6. Layout never panics. The fuzz target FuzzBlocks (fuzz_test.go:18) covers each new block. Its width check, every line as wide as the first and no wider than the line at the block's size (fuzz_test.go:67-79), holds for a table, framed or stacked, as it stands. A banner checks its text lines against its chosen size and its ruled lines against size 1. A text with spans checks that no line passes lineDots in dots. A group checks each child as it would at the top level.

Architecture

FIELDS AND TYPES

  • job.go: TypeTable, TypeBanner and TypeGroup, in the type enum. New fields on Element: char, cols ([]Column), rows and foot ([][]string), spans ([]Span). Children serves a box and a group. New types Column (head, align, width, grow) and Span (text, bold, underline, invert, size, width, height), each with jsonschema tags and doc comments whose first paragraph is the schema description.
  • Each stage also rewrites the doc comments, the schema descriptions, of the existing names that list the blocks or speak of a box only. They are the type constants' comment, Element, Type, and each field a new block shares. Text, Align and Size change for a banner. Style changes for a table, a banner and a group. Padding, Fit and Columns change for a table. Children, Title and TitleBold change for a group, and so do Elements and MaxElements.
  • block.go: the fields table (block.go:52-88) gains each new field; blockOnly gains char, cols, rows and foot; ownFields maps spans to text; blockFields and childFields gain the new types. checkFields refuses only fields listed there (block.go:140), so a field left out would be ignored silently. The first stage to run adds a test that every json tag of Element has its entry in the table.
  • job.go: each new type joins the "not allowed inside a box" case (job.go:282) as it lands, so that a box child of that type is refused by name and not as an unknown type; a table moves out of it once it may go inside a box.
  • tools/genopenapi: TestRequestSchemasAreStrict (generate_test.go:191) lists Column and Span, with their required fields.
  • problems and validate take where an element stands (top level, in a box, in a group) instead of inBox, so paths go two levels deep (stage 5).

TABLE LAYOUT For a table of n columns, width W characters (blockWidth, blocklayout.go:59-64) and padding p:

  1. The room for cells is R = W − (n + 1) − 2pn for a frame, or R = W − (n − 1)(1 + 2p) for none.
  2. Each column wants want = min(word, natural, width) and needs need = min(want, 8), each at least 1, where word is its longest word, natural its longest line and width its fixed width if set, the header included.
  3. p drops, one step at a time, while R < Σ want and p > 0. If R < Σ need at p = 0, the table stacks (decision 11).
  4. Each column starts at need. Round 1 brings fixed columns to width and auto columns to their least width, their longest word; round 2 brings auto columns to natural. A round that cannot be met in full shares the room left in proportion to what each column still wants, by largest remainder, ties to the left.
  5. Room still left goes to the grow columns in proportion, or to the first auto column, unless fit is set or every column is fixed; the table is then narrower than W and centred.
  6. Lines: the top border, the header and its separator, the body, the foot and its separator, the bottom border. Every line is exactly the table's width before centring, as box lines are (blocklayout.go:105-158). Borders, fills and padding are plain; header and foot cells are bold.

BANNER PRINTING A banner is not a cell block: it prints its text lines at its size and its ruled lines at size 1, each sent with ESC a 1 (a plain line with its align's), reversed for band, with no margin spaces. Each line counts as one toward max_lines, as a line of large text does today.

SPANS text with spans lays out runs: each run normalised, then wrapped by dots (decision 19), then printed with changeStyle (render.go:488-499) extended to size. The position in dots carries over to the next element as today.

GROUPS printer.element prints a group's heading as a rule, then each child through printer.element with the group's defaults filled in. loadImages, the preview's placeholders and labels, the unknown-character counts and checkRaw walk group children by path.

Phases

Every commit below that changes behaviour carries its own tests, goldens, docs, regenerated docs/swagger.json where a type changes, and CHANGELOG entry, under Added unless its stage says otherwise. Each stage's paper receipt is pinned as decision 2 says.

Stage 1: The rule character

Goal. rule takes char, one character repeated across the line, with its fold and then its style as the fallback. Decisions 1-6.

Work.

  • block.go: char in the fields tables, on rule at the top level only; ruleSpec checks it (decision 4).
  • glyphs.go: the pattern for a rule with char (decision 5).
  • blocklayout.go: rule uses it (blocklayout.go:362-366).
  • job.go: Char with minLength=1,maxLength=1.
  • examples/rule-char.json: *, ~, =, ■, • (folding to ·), ♦ (falling back to the style), each with a label, and one at size: 2.
  • docs/api.md § rule and § Glyphs and code pages; design.md, the decision.

Tests.

  • testdata/blocks/rule-char.json, goldens in the three code pages.
  • Validation: two runes, a combining mark, a format character, a control character, \n, char inside a box, each refused with its message; a space accepted; "" as unset.
  • The fallback order on every code page, U+212B composed first, cp858 equal to cp850.
  • TestBoundTagsMatchValidation covers char's upper bound alone. FuzzBlocks sets char.
  • If no other stage has run: the test that every json tag of Element is in the fields table.
  • A golden PNG of examples/rule-char.json, if job-store phase 3a has shipped.

Commits.

  1. Add a custom character to the rule block

Done when.

  • The full gate passes.
  • Paper, with the owner's OK: examples/rule-char.json. Look at each character, the label's place and spacing, the line's ends at both edges, and the size-2 rule. Wait for his report, then pin the bytes.

Stage 2: The table

Goal. A table block with columns, rows, a header and foot rows, in every box style and none, sized by content to the paper, wrapping in cells and stacking when its columns do not fit. It may go inside a box. Decisions 1-3 and 7-14.

Work.

  • render.go: the printer knows MaxLines and the copies, and stops as soon as the lines so far, times the copies, pass the limit (decision 12); LineLimitError says "at least" when it stopped early. docs/api.md's too_many_lines example (api.md:195-198); CHANGELOG under Changed.
  • job.go: TypeTable, Column, Cols, Rows, Foot; the style enum gains none.
  • block.go: tableSpec, with the checks of decision 12, and the problems of each row by index.
  • glyphs.go: the table's border sets, junctions included, with the fallback of decision 1; none.
  • blocklayout.go (or a new tablelayout.go): TABLE LAYOUT; the stacked form through kvLines; a table item in a box, with its natural width for fit.
  • render.go: table in printer.element and printer.block; the table's layout and its cells' wrap check the line limit at each row.
  • Two receipts: examples/table.json, decision 7's table in double-h, single and none; examples/table-narrow.json, a narrow one by columns that wraps cells, one that stacks, and a none table in a box.
  • docs/api.md: § table with examples and the field table, the Elements intro (api.md:307-308), the Blocks intro (api.md:409-419), § Glyphs and code pages, and § box's children (api.md:463-475) for commit 4; README's list of blocks (README.md:11); design.md, the decisions.

Tests.

  • testdata/blocks/table-styles.json, table-widths.json (fixed, auto, grow, fit, all fixed), table-wrap.json, table-narrow.json (padding given up, words split, stacked) and table-box.json, goldens in the three code pages.
  • Width sums: every line of every golden table is exactly the table's width; a table test on the tm-t20 and generic-58mm profiles, so widths follow the profile.
  • Distribution: largest remainder and ties to the left, by table test.
  • Validation: each limit of decision 12, a ragged row, grow with width, an unknown align, each refused with its path.
  • The early stop: 500 text elements of 10 lines each against a limit of 100 stop at the eleventh element, by a count of elements rendered; a table of 100 rows in a 1-character column stops at the row that passes the limit, by testing.AllocsPerRun against a table that fits. Under the limit, every compat fixture keeps its bytes.
  • cp858 equal to cp850; schema tag tests; FuzzBlocks with tables.
  • Golden PNGs of the two examples, if job-store phase 3a has shipped.

Commits.

  1. Stop rendering a receipt once it passes max_lines
  2. Add the table block
  3. Add foot rows to the table block
  4. Allow a table inside a box

Commit 2 includes the stacked form: without it, a framed table on a line narrower than 2n + 1 characters would print lines wider than the block. The examples and goldens grow with the commits, since TestExampleJobs checks every example at each one. Commit 2 has decision 7's table without its foot, in double-h, single and none, the narrow one that wraps and the one that stacks. Commit 3 adds the foot, which the stacked form prints too. Commit 4 adds the none table in a box, with testdata/blocks/table-box.json. If commit 2 passes the 800-line split point, "Add a frameless style to the table block" comes out of it, after it.

Done when.

  • The full gate passes.
  • Paper, with the owner's OK, one receipt at a time: examples/table.json, then examples/table-narrow.json. Look at the ╤╪╧ junctions meeting the vertical lines (the gaps the box borders show are expected), columns lining up, right-aligned numbers, wrapped cells, the bold header and foot, and the stacked form. Wait for his report on each, then pin the bytes.

Stage 3: The banner

Goal. A banner block of large text across the line in band, plain or ruled, sized automatically from the paper. Decisions 1-3 and 15-17.

Work.

  • Before the code: read ESC a and GS B in Epson's command reference, in a browser. If a finding under ESC/POS does not hold, stop and ask the owner.
  • job.go: TypeBanner; the style enum gains band, plain and ruled.
  • block.go: bannerSpec: text required, size 0-8, style, align, bold, font.
  • render.go and blocklayout.go: the automatic size (decision 16), the lines, BANNER PRINTING.
  • Two receipts: examples/banner.json, ORDER 42 in each style at the automatic size and in Font B; examples/banner-large.json, a two-line band and size 8.
  • docs/api.md: § banner, the Elements intro, the Blocks intro (a banner takes size 0-8 and no columns), § Glyphs and code pages (▄ ▀ as = on cp1252); README's list of blocks; design.md, the decisions.

Tests.

  • testdata/blocks/banner.json, goldens in the three code pages.
  • The automatic size on tm-t88, tm-t20 and generic-58mm, and with printer.columns set.
  • The bytes: ESC a 1, the size, reverse on and off, no margin spaces, by TestRenderBlocksBytes's pattern (blocks_test.go:367).
  • Validation; a banner inside a box refused; cp858 equal to cp850 for the ruled lines; schema tag tests; FuzzBlocks with banners.
  • Golden PNGs of the two examples, if job-store phase 3a has shipped.

Commits.

  1. Add the banner block

Done when.

  • The full gate passes.
  • Paper, with the owner's OK, one receipt at a time: examples/banner.json, then examples/banner-large.json. Look at the band's edges and the even white margin on each side, the text centred in the band, whether two lines of a band touch, the ruled lines, and size 8. Wait for his report on each, then pin the bytes.

Stage 4: Inline spans

Goal. text takes spans: styled runs within one line or wrapped paragraph, at the top level and in a box. Decisions 2, 3 and 18-21.

Work.

  • Before the code: read ESC a and GS ! in Epson's command reference, in a browser. If a finding under ESC/POS does not hold, stop and ask the owner.
  • layout.go: wrap measures each character's width (decision 19), with today's wrap its case of equal widths. This commit changes no byte.
  • job.go: Span and Spans.
  • block.go and job.go: the checks of decision 18, and decision 21's inside a box; ownFields gains spans.
  • render.go: text with spans: normalise each run, wrap by dots, print runs with style and size changes, reset the style after.
  • blocklayout.go: a text child with spans lays out as styled runs in cells.
  • examples/spans.json: decision 18's line; a paragraph that wraps across bold, underlined, inverted and size-2 runs; a word that changes style part-way; and, from commit 3, spans in a box.
  • docs/api.md § text and § box; design.md, the decisions.

Tests.

  • The new wrap equals today's for every case of layout_test.go and for random text of one size (a property test). TestRenderCompat passes unchanged.
  • Mixed sizes break where the dots run out, not the characters; a word across runs stays whole; a character wider than the line takes its own line.
  • testdata/blocks/spans.json, goldens in the three code pages; the preview's runs carry each span's style and size.
  • Validation: text with spans, an element style with spans, "wrap": false with spans, an empty span, a span size inside a box, 101 spans, each refused; spans on a qr refused.
  • Tabs in runs of different widths stop where decision 19 says.
  • Schema tag tests; FuzzBlocks with spans.
  • A golden PNG of examples/spans.json, if job-store phase 3a has shipped.

Commits.

  1. Wrap text by the width of each character
  2. Add inline spans to the text element
  3. Allow spans in text inside a box

Commit 1 changes no behaviour, so it has no docs or CHANGELOG entry. Commit 2's examples/spans.json has no box; commit 3 adds the spans in a box.

Done when.

  • The full gate passes.
  • Paper, with the owner's OK: examples/spans.json. Look at the baseline of mixed sizes, where lines break, styles starting and stopping at the right letter, the centred line, and the box borders beside styled runs. Wait for his report, then pin the bytes.

Stage 5: The group

Goal. A group holds elements under an optional heading, with a shared font and size. Decisions 2, 3 and 22-26.

Work.

  • job.go and block.go: validation by where an element stands (Architecture, FIELDS AND TYPES), with no change to what a box accepts or to any message. This commit changes no behaviour.
  • job.go: TypeGroup; the element count adds group children and the box children inside them.
  • block.go: groupSpec: children, title, title_bold, style (a rule style), size and font.
  • render.go and preview.go: GROUPS, images and warnings by path.
  • server/auth.go: checkRaw walks every child list, a box's and a group's, by path.
  • examples/group.json: a heading, a box and a kv inside, Font B shared, a child with its own size, and a table if stage 2 has shipped.
  • docs/api.md: § group, the Elements intro, the Blocks intro, § box (a group cannot go inside one), the 500-element limit counting group children; README's list of blocks; design.md, the decisions.

Tests.

  • The validation refactor: every existing validation test passes unchanged.
  • Paths in problems, render errors and preview warnings; an image inside a group loads, previews with its label, and an optional one that fails prints [image unavailable].
  • The element count across groups and boxes; a group in a group, raw, cut and a group in a box, each refused.
  • checkRaw names a raw element inside a group, built without validation, as elements[0].children[1] (raw).
  • The shared font and size, and a child's own value winning; a qr keeps its size.
  • testdata/blocks/group.json, goldens in the three code pages; schema tag tests; FuzzBlocks with groups.
  • A golden PNG of examples/group.json, if job-store phase 3a has shipped.

Commits.

  1. Validate elements by where they stand in the job
  2. Add the group block

Commit 1 changes no behaviour, so it has no docs or CHANGELOG entry.

Done when.

  • The full gate passes.
  • Paper, with the owner's OK: examples/group.json. Look at the heading, the children's spacing and the shared font. Wait for his report, then pin the bytes.