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>
185 lines
4.8 KiB
Go
185 lines
4.8 KiB
Go
// Package escpos builds Epson ESC/POS command streams for thermal receipt
|
|
// printers.
|
|
//
|
|
// A [Builder] accumulates commands in memory. Callers send [Builder.Bytes] to
|
|
// the printer in a single write, so an invalid element never leaves a
|
|
// half-printed receipt. Commands follow the Epson ESC/POS Application
|
|
// Programming Guide; byte values are named by the control characters the
|
|
// specification uses (ESC, GS, DLE).
|
|
package escpos
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
// Control characters that introduce ESC/POS commands.
|
|
const (
|
|
ht byte = 0x09
|
|
lf byte = 0x0A
|
|
dle byte = 0x10
|
|
eot byte = 0x04
|
|
esc byte = 0x1B
|
|
gs byte = 0x1D
|
|
)
|
|
|
|
// MaxCharacterScale is the largest character width or height multiplier
|
|
// supported by GS !.
|
|
const MaxCharacterScale = 8
|
|
|
|
// ErrInvalidArgument is wrapped by every validation error the package
|
|
// returns.
|
|
var ErrInvalidArgument = errors.New("escpos: invalid argument")
|
|
|
|
// Justification aligns text, barcodes and QR codes on the line.
|
|
type Justification byte
|
|
|
|
// Justification values for ESC a.
|
|
const (
|
|
JustifyLeft Justification = 0
|
|
JustifyCenter Justification = 1
|
|
JustifyRight Justification = 2
|
|
)
|
|
|
|
// Font selects one of the printer's built-in character fonts.
|
|
type Font byte
|
|
|
|
// Font values for ESC M.
|
|
const (
|
|
FontA Font = 0
|
|
FontB Font = 1
|
|
)
|
|
|
|
// Underline selects the underline thickness.
|
|
type Underline byte
|
|
|
|
// Underline values for ESC -.
|
|
const (
|
|
UnderlineNone Underline = 0
|
|
UnderlineThin Underline = 1
|
|
UnderlineThick Underline = 2
|
|
)
|
|
|
|
// CutMode selects a full or partial paper cut.
|
|
type CutMode byte
|
|
|
|
// CutMode values for GS V function B, which feeds the paper to the cutting
|
|
// position before cutting so the last printed line is not lost.
|
|
const (
|
|
CutFull CutMode = 65
|
|
CutPartial CutMode = 66
|
|
)
|
|
|
|
// Builder accumulates an ESC/POS command stream. The zero value is ready to
|
|
// use. A Builder is not safe for concurrent use.
|
|
type Builder struct {
|
|
buf bytes.Buffer
|
|
}
|
|
|
|
// Bytes returns the accumulated command stream. The slice aliases the
|
|
// Builder's buffer and is valid until the next write.
|
|
func (b *Builder) Bytes() []byte {
|
|
return b.buf.Bytes()
|
|
}
|
|
|
|
// Len returns the number of accumulated bytes.
|
|
func (b *Builder) Len() int {
|
|
return b.buf.Len()
|
|
}
|
|
|
|
// Raw appends p unchanged. It is the escape hatch for commands the Builder
|
|
// does not model.
|
|
func (b *Builder) Raw(p []byte) {
|
|
b.buf.Write(p)
|
|
}
|
|
|
|
// Text appends already-encoded character data. Use [CodePage.Encode] to
|
|
// convert UTF-8 text first.
|
|
func (b *Builder) Text(encoded []byte) {
|
|
b.buf.Write(encoded)
|
|
}
|
|
|
|
// Initialize resets the printer to its power-on settings (ESC @).
|
|
func (b *Builder) Initialize() {
|
|
b.buf.Write([]byte{esc, '@'})
|
|
}
|
|
|
|
// LineFeed prints the line buffer and advances one line (LF).
|
|
func (b *Builder) LineFeed() {
|
|
b.buf.WriteByte(lf)
|
|
}
|
|
|
|
// Feed prints the line buffer and advances lines lines (ESC d n).
|
|
func (b *Builder) Feed(lines uint8) {
|
|
b.buf.Write([]byte{esc, 'd', lines})
|
|
}
|
|
|
|
// MaxLineSpacing is the largest line spacing ESC 3 sets, in motion units.
|
|
const MaxLineSpacing = 255
|
|
|
|
// LineSpacing sets the line spacing in motion units (ESC 3 n).
|
|
func (b *Builder) LineSpacing(units uint8) {
|
|
b.buf.Write([]byte{esc, '3', units})
|
|
}
|
|
|
|
// DefaultLineSpacing restores the default line spacing (ESC 2).
|
|
func (b *Builder) DefaultLineSpacing() {
|
|
b.buf.Write([]byte{esc, '2'})
|
|
}
|
|
|
|
// Bold turns emphasized printing on or off (ESC E n).
|
|
func (b *Builder) Bold(on bool) {
|
|
b.buf.Write([]byte{esc, 'E', boolByte(on)})
|
|
}
|
|
|
|
// Underline sets the underline mode (ESC - n).
|
|
func (b *Builder) Underline(u Underline) {
|
|
b.buf.Write([]byte{esc, '-', byte(u)})
|
|
}
|
|
|
|
// Reverse turns white-on-black printing on or off (GS B n).
|
|
func (b *Builder) Reverse(on bool) {
|
|
b.buf.Write([]byte{gs, 'B', boolByte(on)})
|
|
}
|
|
|
|
// Justify sets the alignment (ESC a n). It takes effect at the start of the
|
|
// next line.
|
|
func (b *Builder) Justify(j Justification) {
|
|
b.buf.Write([]byte{esc, 'a', byte(j)})
|
|
}
|
|
|
|
// Font selects the character font (ESC M n).
|
|
func (b *Builder) Font(f Font) {
|
|
b.buf.Write([]byte{esc, 'M', byte(f)})
|
|
}
|
|
|
|
// Size sets the character width and height multipliers, each 1 to
|
|
// [MaxCharacterScale] (GS ! n).
|
|
func (b *Builder) Size(width, height int) error {
|
|
if width < 1 || width > MaxCharacterScale || height < 1 || height > MaxCharacterScale {
|
|
return fmt.Errorf("%w: character size %dx%d must be within 1-%d",
|
|
ErrInvalidArgument, width, height, MaxCharacterScale)
|
|
}
|
|
b.buf.Write([]byte{gs, '!', byte(width-1)<<4 | byte(height-1)})
|
|
return nil
|
|
}
|
|
|
|
// SelectCodePage selects the character table used to print text (ESC t n).
|
|
func (b *Builder) SelectCodePage(cp CodePage) {
|
|
b.buf.Write([]byte{esc, 't', cp.table()})
|
|
}
|
|
|
|
// Cut feeds the paper to the cutter plus feed motion units and cuts it
|
|
// (GS V m n).
|
|
func (b *Builder) Cut(mode CutMode, feed uint8) {
|
|
b.buf.Write([]byte{gs, 'V', byte(mode), feed})
|
|
}
|
|
|
|
func boolByte(on bool) byte {
|
|
if on {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|