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>
138 lines
3.9 KiB
Go
138 lines
3.9 KiB
Go
package receipt
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
"net/url"
|
|
"path"
|
|
|
|
"uberbringer/internal/pkg/escpos"
|
|
)
|
|
|
|
// Preview is what a job prints, read back from its command stream.
|
|
type Preview struct {
|
|
// Columns is the number of Font A characters on a line.
|
|
Columns int
|
|
|
|
CodePage escpos.CodePage
|
|
|
|
// LineCount is the number of lines the job prints, all copies counted:
|
|
// text lines, fed lines, and one for each QR code, barcode and image.
|
|
// Options.MaxLines limits it.
|
|
LineCount int
|
|
|
|
// Lines are the printed lines. Image lines name the image's source
|
|
// instead of its size, since a preview loads no images.
|
|
Lines []escpos.PreviewLine
|
|
|
|
// Warnings name the elements with characters the code page lacks and
|
|
// images that would fail to print.
|
|
Warnings []string
|
|
}
|
|
|
|
// Preview validates and renders job as Render does, then reads the command
|
|
// stream back with escpos.Interpret, so that it shows what the bytes say. It
|
|
// neither fetches nor decodes images: each prints as a blank placeholder. A
|
|
// job over Options.MaxLines fails with a *LineLimitError, as it does when it
|
|
// prints.
|
|
func (r *Renderer) Preview(ctx context.Context, job Job) (Preview, error) {
|
|
if err := job.Validate(); err != nil {
|
|
return Preview{}, err
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return Preview{}, err
|
|
}
|
|
images, imageWarnings := r.placeholderImages(job)
|
|
out, err := r.render(job, images)
|
|
if err != nil {
|
|
return Preview{}, err
|
|
}
|
|
lines, err := escpos.Interpret(out.stream, r.options.CodePage, r.options.Profile, out.raw)
|
|
if err != nil {
|
|
return Preview{}, fmt.Errorf("reading back the rendered receipt: %w", err)
|
|
}
|
|
labelImages(lines, job)
|
|
|
|
var warnings []string
|
|
for i := range job.Elements {
|
|
if w, ok := imageWarnings[i]; ok {
|
|
warnings = append(warnings, w)
|
|
}
|
|
if n := out.unknown[i]; n > 0 {
|
|
noun := "characters"
|
|
if n == 1 {
|
|
noun = "character"
|
|
}
|
|
warnings = append(warnings, fmt.Sprintf("elements[%d]: %d %s not in %s printed as ?", i, n, noun, r.options.CodePage))
|
|
}
|
|
}
|
|
return Preview{
|
|
Columns: r.textLineDots() / r.options.Profile.FontAWidth,
|
|
CodePage: r.options.CodePage,
|
|
LineCount: out.lines,
|
|
Lines: lines,
|
|
Warnings: warnings,
|
|
}, nil
|
|
}
|
|
|
|
// placeholderImages returns a blank one-row raster across the paper for
|
|
// every image element, and a warning for each image that would fail to
|
|
// print because it is wider than the paper.
|
|
func (r *Renderer) placeholderImages(job Job) (map[int]*image.Paletted, map[int]string) {
|
|
paper := r.options.Profile.DotsPerLine
|
|
images := make(map[int]*image.Paletted)
|
|
warnings := make(map[int]string)
|
|
for i, el := range job.Elements {
|
|
if el.Type != TypeImage {
|
|
continue
|
|
}
|
|
if el.Width > paper {
|
|
warnings[i] = fmt.Sprintf("elements[%d] (image): width %d is wider than the %d-dot paper, so the job would fail", i, el.Width, paper)
|
|
}
|
|
images[i] = image.NewPaletted(image.Rect(0, 0, paper, 1), color.Palette{color.White, color.Black})
|
|
}
|
|
return images, warnings
|
|
}
|
|
|
|
// labelImages replaces the size of each image placeholder with the source
|
|
// of its element, in order, copy after copy.
|
|
func labelImages(lines []escpos.PreviewLine, job Job) {
|
|
var sources []Element
|
|
for _, el := range job.Elements {
|
|
if el.Type == TypeImage {
|
|
sources = append(sources, el)
|
|
}
|
|
}
|
|
n := 0
|
|
for i := range lines {
|
|
if lines[i].Kind != escpos.LineImage || len(sources) == 0 {
|
|
continue
|
|
}
|
|
el := sources[n%len(sources)]
|
|
n++
|
|
name, data := "base64", "base64"
|
|
if el.URL != "" {
|
|
name, data = imageName(el.URL), el.URL
|
|
}
|
|
lines[i].Text = fmt.Sprintf("[image: %s]", name)
|
|
if el.Width > 0 {
|
|
lines[i].Text = fmt.Sprintf("[image %d dots: %s]", el.Width, name)
|
|
}
|
|
lines[i].Data = data
|
|
}
|
|
}
|
|
|
|
// imageName is the last path segment of an image URL, or its host.
|
|
func imageName(raw string) string {
|
|
u, err := url.Parse(raw)
|
|
if err != nil {
|
|
return raw
|
|
}
|
|
if name := path.Base(u.Path); name != "." && name != "/" {
|
|
return name
|
|
}
|
|
return u.Host
|
|
}
|