Add box, rule and kv blocks, text folding and receipt preview
Phase 1 of docs/plans/formatters.md: single and double CP437 boxes from
one /print element, and a way to see a receipt without using paper.
- receipt: box, rule and kv elements, laid out when the job renders from
the printer profile, size and font (never fixed widths). Boxes take a
title (inline or capped), a footer, padding, fit and columns, and a
body of text or children (text, feed, kv, and rules as dividers joined
to the borders). Glyphs a code page lacks fall back per set: double-h
and double-v to double, then single, then ascii; a missing junction
switches line weight first.
- Validation names nested elements (elements[0].children[3] (qr)),
checks fields per block type, allows one level of children and counts
them toward the 500-element limit. Block fields on the older types are
problems, as they were unknown fields before.
- Text is composed to NFC and characters the code page lacks fold to
look-alikes (curly quotes, dashes, ellipsis, arrows, check marks, euro,
trademark), as the owner approved on 2026-11-29. An emoji sequence
prints as one '?'. Encodable text is untouched.
- escpos: CodePage.CanEncode and Decode, and Interpret, which reads a
command stream back as the printer would print it. It takes the
profile so that it can follow the printer's own wrapping and
alignment; raw spans stay opaque.
- POST /api/v1/preview and `uberbringer preview` show the lines a job
prints, as text or JSON with styles, without fetching images.
- Config: [format] max_lines (default 2000), refused with 422
too_many_lines on preview; printer.box_line_spacing (default 0).
Byte compatibility: testdata/compat fixtures were rendered by 9b91109
before the renderer changed, and still match it and the new renderer.
examples/blocks.json was printed on the TM-T88IV; the owner judged the
border gaps at the default spacing fine (2026-11-30), so
box_line_spacing stays 0, and TestPaperReceipts pins its bytes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9fdea32b4f
commit
4562f0cebb
109 files changed
+6383
-487
No files matched your search
@@ -0,0 +1,90 @@
|
||||
package receipt
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"uberbringer/internal/pkg/escpos"
|
||||
)
|
||||
|
||||
func TestNormalize(t *testing.T) {
|
||||
cp437, cp850, cp858, cp1252 := escpos.CodePage437, escpos.CodePage850, escpos.CodePage858, escpos.CodePage1252
|
||||
tests := []struct {
|
||||
name string
|
||||
cp escpos.CodePage
|
||||
in string
|
||||
want string
|
||||
unknown int
|
||||
}{
|
||||
{"decomposed é becomes one rune", cp437, "cafe\u0301", "café", 0},
|
||||
{"decomposed ü too", cp850, "u\u0308ber", "über", 0},
|
||||
{"smart quotes", cp437, "“double” „low” ‘single’", `"double" "low" 'single'`, 0},
|
||||
{"dashes", cp437, "a–b—c", "a-b--c", 0},
|
||||
{"ellipsis", cp437, "wait…", "wait...", 0},
|
||||
{"bullet", cp437, "• item", "· item", 0},
|
||||
{"arrows", cp437, "a → b ← c", "a -> b <- c", 0},
|
||||
{"check marks", cp437, "✓ ✔", "√ √", 0},
|
||||
{"check marks without √", cp850, "✓ ✔", "x x", 0},
|
||||
{"times", cp437, "3×", "3x", 0},
|
||||
{"times kept where it prints", cp850, "3×", "3×", 0},
|
||||
{"euro", cp437, "5 €", "5 EUR", 0},
|
||||
{"euro kept on cp858", cp858, "5 €", "5 €", 0},
|
||||
{"trademark", cp437, "Brand™", "BrandTM", 0},
|
||||
{"cp1252 keeps its own typography", cp1252, "“q” – — … • € ™", "“q” – — … • € ™", 0},
|
||||
{"one emoji", cp437, "a 🎉 b", "a ? b", 1},
|
||||
{"skin tone", cp437, "👍🏽", "?", 1},
|
||||
{"zero-width joiner sequence", cp437, "👨\u200D👩\u200D👧 family", "? family", 1},
|
||||
{"flags pair up", cp437, "🇩🇪🇫🇷", "??", 2},
|
||||
{"tag sequence", cp437, "🏴\U000E0067\U000E0062\U000E0065\U000E006E\U000E0067\U000E007F!", "?!", 1},
|
||||
{"variation selector after an emoji", cp437, "⚠\uFE0F careful", "? careful", 1},
|
||||
{"keycap", cp437, "1\uFE0F\u20E3 first", "1 first", 0},
|
||||
{"VS16 after a printable character", cp437, "x\uFE0F", "x", 0},
|
||||
{"zero-width characters dropped", cp437, "a\u200Bb\u200Cc\u200Dd", "abcd", 0},
|
||||
{"other missing characters", cp437, "漢字 ¤", "?? ?", 3},
|
||||
{"controls stripped", cp437, "a\x1b@b\r\n\tc\x00", "a@b\n\tc", 0},
|
||||
{"two emoji are two", cp437, "🎉🎉", "??", 2},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, unknown := normalize(tt.in, tt.cp)
|
||||
if got != tt.want || unknown != tt.unknown {
|
||||
t.Errorf("normalize(%q, %s) = %q, %d; want %q, %d", tt.in, tt.cp, got, unknown, tt.want, tt.unknown)
|
||||
}
|
||||
for _, r := range got {
|
||||
if r != '\n' && r != '\t' && !tt.cp.CanEncode(r) {
|
||||
t.Errorf("result %q has %q, which %s cannot print", got, r, tt.cp)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeLeavesPrintableTextAlone(t *testing.T) {
|
||||
samples := map[escpos.CodePage]string{
|
||||
escpos.CodePage437: "┌─┐│└┘ ╔═╗ ░▒▓█ £ é ü Ω ·√° \u00A0 plain ASCII ~!@#",
|
||||
escpos.CodePage850: "╔═╗ ┌─┐ ░▒▓ × © ® Ø ß £ àéîõü \u00A0",
|
||||
escpos.CodePage858: "€ ╔═╗ ß",
|
||||
escpos.CodePage1252: "“q” ‘q’ – — … • € ™ × é",
|
||||
}
|
||||
for cp, s := range samples {
|
||||
if got, unknown := normalize(s, cp); got != s || unknown != 0 {
|
||||
t.Errorf("%s: normalize(%q) = %q, %d", cp, s, got, unknown)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeRunsBeforeWrap(t *testing.T) {
|
||||
// The ellipsis takes three columns, so "word…" no longer fits in 6.
|
||||
text, _ := normalize("ab word…", escpos.CodePage437)
|
||||
if got := wrap(text, 6, 6); !reflect.DeepEqual(got, []string{"ab", "word..", "."}) {
|
||||
t.Errorf("wrap = %q", got)
|
||||
}
|
||||
line, _ := normalizeLine("a\nb\tc", escpos.CodePage437)
|
||||
if line != "a b c" {
|
||||
t.Errorf("normalizeLine = %q", line)
|
||||
}
|
||||
if strings.ContainsAny(line, "\n\t") {
|
||||
t.Error("normalizeLine kept a newline or tab")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user