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>
114 lines
3.8 KiB
Go
114 lines
3.8 KiB
Go
package escpos_test
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
|
|
"uberbringer/internal/pkg/escpos"
|
|
)
|
|
|
|
// Glyph groups the receipt blocks draw with.
|
|
const (
|
|
singleBox = "┌┐└┘─│├┤┬┴┼"
|
|
doubleBox = "╔╗╚╝═║╠╣╦╩╬"
|
|
mixedBox = "╒╓╕╖╘╙╛╜╞╟╡╢╤╥╧╨╪╫"
|
|
shades = "░▒▓█"
|
|
halfBlocks = "▀▄"
|
|
sideHalves = "▌▐"
|
|
markers = "·°■"
|
|
bulletOp = "∙"
|
|
check = "√"
|
|
neverPrinted = "►◄→←" // these map to control codes 0x10-0x1F
|
|
bulletGeneral = "•"
|
|
)
|
|
|
|
func TestCanEncode(t *testing.T) {
|
|
type want struct {
|
|
cp escpos.CodePage
|
|
glyphs string
|
|
encodes bool
|
|
}
|
|
tests := []want{
|
|
{escpos.CodePage437, singleBox + doubleBox + mixedBox + shades + halfBlocks + sideHalves + markers + bulletOp + check, true},
|
|
{escpos.CodePage437, neverPrinted + bulletGeneral, false},
|
|
|
|
{escpos.CodePage850, singleBox + doubleBox + shades + halfBlocks + markers, true},
|
|
{escpos.CodePage850, mixedBox + sideHalves + bulletOp + check + neverPrinted + bulletGeneral, false},
|
|
{escpos.CodePage858, singleBox + doubleBox + shades + halfBlocks + markers + "€", true},
|
|
{escpos.CodePage858, mixedBox + sideHalves + bulletOp + check + neverPrinted + bulletGeneral, false},
|
|
|
|
// Windows-1252 has no box drawing or shades, but has the bullet
|
|
// at 0x95 and the typographic quotes and dashes.
|
|
{escpos.CodePage1252, singleBox + doubleBox + mixedBox + shades + halfBlocks + sideHalves + bulletOp + check + "■" + neverPrinted, false},
|
|
{escpos.CodePage1252, "·°" + bulletGeneral + "“”‘’–—…€™×", true},
|
|
|
|
// Every code page: printable ASCII yes, controls and newlines no.
|
|
{escpos.CodePage437, "A z~ !", true},
|
|
{escpos.CodePage437, "\n\t\x00\x1b\x7f\u0085\uFFFD", false},
|
|
{escpos.CodePage1252, "\n\t\x00\x1b\x7f\u0085\uFFFD", false},
|
|
{escpos.CodePage437, "“”‘’–—…€™×🎉漢", false},
|
|
}
|
|
for _, tt := range tests {
|
|
for _, r := range tt.glyphs {
|
|
if got := tt.cp.CanEncode(r); got != tt.encodes {
|
|
t.Errorf("%s CanEncode(%q) = %v, want %v", tt.cp, r, got, tt.encodes)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestCanEncodeMatchesEncode checks that CanEncode is true exactly when
|
|
// Encode prints the rune as itself rather than as '?' or not at all.
|
|
func TestCanEncodeMatchesEncode(t *testing.T) {
|
|
for _, cp := range []escpos.CodePage{escpos.CodePage437, escpos.CodePage850, escpos.CodePage858, escpos.CodePage1252} {
|
|
for r := rune(0); r < 0x2700; r++ {
|
|
if r == '?' {
|
|
continue
|
|
}
|
|
out := cp.Encode(string(r))
|
|
printsItself := len(out) == 1 && out[0] != '?' && out[0] >= 0x20
|
|
if got := cp.CanEncode(r); got != printsItself {
|
|
t.Errorf("%s CanEncode(%U) = %v, but Encode gives % X", cp, r, got, out)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDecodeRoundTrip(t *testing.T) {
|
|
tests := []struct {
|
|
cp escpos.CodePage
|
|
roundTrip int // bytes 0x20-0xFF that decode to a rune encoding back to them
|
|
}{
|
|
{escpos.CodePage437, 223},
|
|
{escpos.CodePage850, 223},
|
|
{escpos.CodePage858, 223},
|
|
{escpos.CodePage1252, 218}, // 0x81, 0x8D, 0x8F, 0x90 and 0x9D are unassigned
|
|
}
|
|
for _, tt := range tests {
|
|
n := 0
|
|
for c := 0x20; c <= 0xFF; c++ {
|
|
b := []byte{byte(c)}
|
|
s := tt.cp.Decode(b)
|
|
r := []rune(s)
|
|
if len(r) != 1 {
|
|
t.Fatalf("%s Decode(% X) = %q, want one rune", tt.cp, b, s)
|
|
}
|
|
if !tt.cp.CanEncode(r[0]) {
|
|
continue
|
|
}
|
|
if got := tt.cp.Encode(s); !bytes.Equal(got, b) {
|
|
t.Errorf("%s: % X decodes to %q, which encodes to % X", tt.cp, b, s, got)
|
|
}
|
|
n++
|
|
}
|
|
if n != tt.roundTrip {
|
|
t.Errorf("%s: %d bytes round-trip, want %d", tt.cp, n, tt.roundTrip)
|
|
}
|
|
}
|
|
|
|
text := "┌─ Grüße £5 ─┐ ░▒▓ ·√°"
|
|
if got := escpos.CodePage437.Decode(escpos.CodePage437.Encode(text)); got != text {
|
|
t.Errorf("round trip = %q, want %q", got, text)
|
|
}
|
|
}
|