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,336 @@
|
||||
package receipt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"uberbringer/internal/pkg/escpos"
|
||||
)
|
||||
|
||||
// Block limits and defaults.
|
||||
const (
|
||||
maxBlockSize = 3
|
||||
maxPadding = 3
|
||||
maxColumns = 255
|
||||
defaultPadding = 1
|
||||
)
|
||||
|
||||
// boxStyle selects the border glyphs of a box.
|
||||
type boxStyle int
|
||||
|
||||
const (
|
||||
boxSingle boxStyle = iota
|
||||
boxDouble
|
||||
boxDoubleH
|
||||
boxDoubleV
|
||||
boxASCII
|
||||
)
|
||||
|
||||
// ruleStyle selects the line a rule is drawn with.
|
||||
type ruleStyle int
|
||||
|
||||
const (
|
||||
ruleSingle ruleStyle = iota
|
||||
ruleDouble
|
||||
ruleLight
|
||||
ruleMedium
|
||||
ruleDark
|
||||
ruleSolid
|
||||
ruleDashed
|
||||
ruleDotted
|
||||
ruleASCII
|
||||
)
|
||||
|
||||
// field is one JSON field of Element and the test for whether a job set it.
|
||||
type field struct {
|
||||
name string
|
||||
set func(Element) bool
|
||||
}
|
||||
|
||||
// fields lists every Element field in JSON order.
|
||||
var fields = []field{
|
||||
{"text", func(e Element) bool { return e.Text != "" }},
|
||||
{"bold", func(e Element) bool { return e.Bold }},
|
||||
{"size", func(e Element) bool { return e.Size != 0 }},
|
||||
{"width", func(e Element) bool { return e.Width != 0 }},
|
||||
{"height", func(e Element) bool { return e.Height != 0 }},
|
||||
{"font", func(e Element) bool { return e.Font != "" }},
|
||||
{"align", func(e Element) bool { return e.Align != "" }},
|
||||
{"underline", func(e Element) bool { return e.Underline != 0 }},
|
||||
{"invert", func(e Element) bool { return e.Invert }},
|
||||
{"newline", func(e Element) bool { return e.Newline != nil }},
|
||||
{"wrap", func(e Element) bool { return e.Wrap != nil }},
|
||||
{"lines", func(e Element) bool { return e.Lines != 0 }},
|
||||
{"data", func(e Element) bool { return e.Data != "" }},
|
||||
{"correction", func(e Element) bool { return e.Correction != "" }},
|
||||
{"symbology", func(e Element) bool { return e.Symbology != "" }},
|
||||
{"hri", func(e Element) bool { return e.HRI != "" }},
|
||||
{"url", func(e Element) bool { return e.URL != "" }},
|
||||
{"base64", func(e Element) bool { return e.Base64 != "" }},
|
||||
{"hex", func(e Element) bool { return e.Hex != "" }},
|
||||
{"mode", func(e Element) bool { return e.Mode != "" }},
|
||||
{"children", func(e Element) bool { return e.Children != nil }},
|
||||
{"title", func(e Element) bool { return e.Title != "" }},
|
||||
{"title_style", func(e Element) bool { return e.TitleStyle != "" }},
|
||||
{"footer", func(e Element) bool { return e.Footer != "" }},
|
||||
{"style", func(e Element) bool { return e.Style != "" }},
|
||||
{"padding", func(e Element) bool { return e.Padding != nil }},
|
||||
{"fit", func(e Element) bool { return e.Fit }},
|
||||
{"columns", func(e Element) bool { return e.Columns != 0 }},
|
||||
{"pairs", func(e Element) bool { return e.Pairs != nil }},
|
||||
{"leader", func(e Element) bool { return e.Leader != "" }},
|
||||
}
|
||||
|
||||
// blockOnly are the fields no other type takes.
|
||||
var blockOnly = fieldSet("children title title_style footer style padding fit columns pairs leader")
|
||||
|
||||
// blockFields are the fields each block takes at the top level.
|
||||
var blockFields = map[string]map[string]bool{
|
||||
TypeBox: fieldSet("text bold underline invert align size font children title title_style footer style padding fit columns"),
|
||||
TypeRule: fieldSet("text bold align size font style columns"),
|
||||
TypeKV: fieldSet("pairs leader size font columns"),
|
||||
}
|
||||
|
||||
// childFields are the fields each type takes inside a box.
|
||||
var childFields = map[string]map[string]bool{
|
||||
TypeText: fieldSet("text bold underline invert align wrap newline"),
|
||||
TypeFeed: fieldSet("lines"),
|
||||
TypeRule: fieldSet("text bold align style"),
|
||||
TypeKV: fieldSet("pairs leader"),
|
||||
}
|
||||
|
||||
// inherited are the fields a child takes from its box.
|
||||
var inherited = fieldSet("size width height font")
|
||||
|
||||
func fieldSet(names string) map[string]bool {
|
||||
set := make(map[string]bool)
|
||||
for _, name := range strings.Fields(names) {
|
||||
set[name] = true
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
// checkFields reports fields set on el that its type does not take. The
|
||||
// blocks and the children of a box take only their own fields; the other
|
||||
// types ignore unrelated fields, except the block fields.
|
||||
func (el Element) checkFields(inBox bool) []string {
|
||||
allowed, strict := blockFields[el.Type]
|
||||
if inBox {
|
||||
allowed, strict = childFields[el.Type]
|
||||
}
|
||||
known := strict || slices.Contains([]string{TypeText, TypeFeed, TypeQR, TypeBarcode, TypeImage, TypeRaw, TypeCut}, el.Type)
|
||||
if !known {
|
||||
return nil
|
||||
}
|
||||
var problems []string
|
||||
for _, f := range fields {
|
||||
if !f.set(el) {
|
||||
continue
|
||||
}
|
||||
switch {
|
||||
case inBox && inherited[f.name]:
|
||||
problems = append(problems, fmt.Sprintf("%s is inherited from the box", f.name))
|
||||
case inBox && !allowed[f.name]:
|
||||
problems = append(problems, fmt.Sprintf("field %q does not apply to %s inside a box", f.name, el.Type))
|
||||
case strict && !allowed[f.name], !strict && blockOnly[f.name]:
|
||||
problems = append(problems, fmt.Sprintf("field %q does not apply to %s", f.name, el.Type))
|
||||
}
|
||||
}
|
||||
return problems
|
||||
}
|
||||
|
||||
// blockScale returns the size and font of a block, which scale its borders
|
||||
// and its text together.
|
||||
func (el Element) blockScale() (int, escpos.Font, error) {
|
||||
size := max(el.Size, 1)
|
||||
if el.Size < 0 || el.Size > maxBlockSize {
|
||||
return 0, 0, fmt.Errorf("size must be 1-%d", maxBlockSize)
|
||||
}
|
||||
font, err := parseFont(el.Font)
|
||||
return size, font, err
|
||||
}
|
||||
|
||||
func checkColumns(columns int) error {
|
||||
if columns < 0 || columns > maxColumns {
|
||||
return fmt.Errorf("columns must be 1-%d", maxColumns)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type boxSpec struct {
|
||||
style boxStyle
|
||||
capped bool
|
||||
text textStyle
|
||||
align escpos.Justification
|
||||
padding int
|
||||
fit bool
|
||||
columns int
|
||||
size int
|
||||
font escpos.Font
|
||||
}
|
||||
|
||||
func (el Element) boxSpec() (boxSpec, error) {
|
||||
s := boxSpec{
|
||||
text: textStyle{bold: el.Bold, invert: el.Invert},
|
||||
padding: defaultPadding,
|
||||
fit: el.Fit,
|
||||
columns: el.Columns,
|
||||
}
|
||||
var err error
|
||||
if s.size, s.font, err = el.blockScale(); err != nil {
|
||||
return s, err
|
||||
}
|
||||
if s.style, err = parseBoxStyle(el.Style); err != nil {
|
||||
return s, err
|
||||
}
|
||||
switch strings.ToLower(el.TitleStyle) {
|
||||
case "", "inline":
|
||||
case "capped":
|
||||
s.capped = true
|
||||
default:
|
||||
return s, fmt.Errorf("title_style must be inline or capped")
|
||||
}
|
||||
if el.Padding != nil {
|
||||
if *el.Padding < 0 || *el.Padding > maxPadding {
|
||||
return s, fmt.Errorf("padding must be 0-%d", maxPadding)
|
||||
}
|
||||
s.padding = *el.Padding
|
||||
}
|
||||
if err := checkColumns(el.Columns); err != nil {
|
||||
return s, err
|
||||
}
|
||||
if s.text.underline, err = parseUnderline(el.Underline); err != nil {
|
||||
return s, err
|
||||
}
|
||||
if s.align, err = parseAlign(el.Align); err != nil {
|
||||
return s, err
|
||||
}
|
||||
switch {
|
||||
case el.Text != "" && len(el.Children) > 0:
|
||||
return s, fmt.Errorf("set text or children, not both")
|
||||
case el.Text == "" && len(el.Children) == 0:
|
||||
return s, fmt.Errorf("text or children is required")
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func parseBoxStyle(s string) (boxStyle, error) {
|
||||
switch strings.ToLower(s) {
|
||||
case "", "single":
|
||||
return boxSingle, nil
|
||||
case "double":
|
||||
return boxDouble, nil
|
||||
case "double-h":
|
||||
return boxDoubleH, nil
|
||||
case "double-v":
|
||||
return boxDoubleV, nil
|
||||
case "ascii":
|
||||
return boxASCII, nil
|
||||
}
|
||||
return 0, fmt.Errorf("style must be single, double, double-h, double-v or ascii")
|
||||
}
|
||||
|
||||
type ruleSpec struct {
|
||||
style ruleStyle
|
||||
align escpos.Justification
|
||||
bold bool
|
||||
columns int
|
||||
size int
|
||||
font escpos.Font
|
||||
}
|
||||
|
||||
// ruleSpec checks a rule. Inside a box a rule is a divider, which only
|
||||
// comes in the weights the borders have.
|
||||
func (el Element) ruleSpec(inBox bool) (ruleSpec, error) {
|
||||
s := ruleSpec{align: escpos.JustifyCenter, bold: el.Bold, columns: el.Columns}
|
||||
var err error
|
||||
if s.size, s.font, err = el.blockScale(); err != nil {
|
||||
return s, err
|
||||
}
|
||||
if s.style, err = parseRuleStyle(el.Style); err != nil {
|
||||
return s, err
|
||||
}
|
||||
if inBox && s.style != ruleSingle && s.style != ruleDouble {
|
||||
return s, fmt.Errorf("inside a box, style must be single or double")
|
||||
}
|
||||
if el.Align != "" {
|
||||
if s.align, err = parseAlign(el.Align); err != nil {
|
||||
return s, err
|
||||
}
|
||||
}
|
||||
if err := checkColumns(el.Columns); err != nil {
|
||||
return s, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func parseRuleStyle(s string) (ruleStyle, error) {
|
||||
switch strings.ToLower(s) {
|
||||
case "", "single":
|
||||
return ruleSingle, nil
|
||||
case "double":
|
||||
return ruleDouble, nil
|
||||
case "light":
|
||||
return ruleLight, nil
|
||||
case "medium":
|
||||
return ruleMedium, nil
|
||||
case "dark":
|
||||
return ruleDark, nil
|
||||
case "solid":
|
||||
return ruleSolid, nil
|
||||
case "dashed":
|
||||
return ruleDashed, nil
|
||||
case "dotted":
|
||||
return ruleDotted, nil
|
||||
case "ascii":
|
||||
return ruleASCII, nil
|
||||
}
|
||||
return 0, fmt.Errorf("style must be single, double, light, medium, dark, solid, dashed, dotted or ascii")
|
||||
}
|
||||
|
||||
type kvSpec struct {
|
||||
leader string
|
||||
columns int
|
||||
size int
|
||||
font escpos.Font
|
||||
}
|
||||
|
||||
func (el Element) kvSpec() (kvSpec, error) {
|
||||
s := kvSpec{columns: el.Columns}
|
||||
var err error
|
||||
if s.size, s.font, err = el.blockScale(); err != nil {
|
||||
return s, err
|
||||
}
|
||||
switch el.Leader {
|
||||
case "", ".":
|
||||
s.leader = "."
|
||||
case "·", " ", "_", "-", "─":
|
||||
s.leader = el.Leader
|
||||
case "space":
|
||||
s.leader = " "
|
||||
default:
|
||||
return s, fmt.Errorf(`leader must be ".", "·", " ", "_", "-" or "─"`)
|
||||
}
|
||||
if err := checkColumns(el.Columns); err != nil {
|
||||
return s, err
|
||||
}
|
||||
switch n := len(el.Pairs); {
|
||||
case n == 0:
|
||||
return s, fmt.Errorf("pairs: at least one pair is required")
|
||||
case n > MaxPairs:
|
||||
return s, fmt.Errorf("pairs: %d pairs, limit is %d", n, MaxPairs)
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// pairProblems reports every pair without a key.
|
||||
func (el Element) pairProblems() []string {
|
||||
var problems []string
|
||||
for i, p := range el.Pairs {
|
||||
if p.Key == "" {
|
||||
problems = append(problems, fmt.Sprintf("pairs[%d]: key is required", i))
|
||||
}
|
||||
}
|
||||
return problems
|
||||
}
|
||||
Reference in new issue
Block a user