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
|
||||
}
|
||||
@@ -0,0 +1,405 @@
|
||||
package receipt
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"uberbringer/internal/pkg/escpos"
|
||||
)
|
||||
|
||||
// Room a box needs around its title and footer, and a rule or divider
|
||||
// around its label: corners, fills of at least one (title, footer) or two
|
||||
// (labels) characters, and a space on each side of the text.
|
||||
const (
|
||||
inlineTitleRoom = 6 // ┌─ Title ─┐
|
||||
cappedTitleRoom = 8 // ╔═╡ Title ╞═╗
|
||||
footerRoom = 7 // └─ footer ──┘
|
||||
labelRoom = 6 // ── Label ──
|
||||
minBoxWidth = 3
|
||||
|
||||
// kvMinWidth fits a one-character key, a space, a two-character
|
||||
// leader, a space and a one-character value.
|
||||
kvMinWidth = 6
|
||||
)
|
||||
|
||||
// layout lays out blocks in one code page. It normalizes their text as it
|
||||
// goes and counts the characters that print as '?'.
|
||||
type layout struct {
|
||||
cp escpos.CodePage
|
||||
unknown int
|
||||
}
|
||||
|
||||
func (lo *layout) text(s string) string {
|
||||
s, n := normalize(s, lo.cp)
|
||||
lo.unknown += n
|
||||
return expandTabs(s, 0)
|
||||
}
|
||||
|
||||
func (lo *layout) line(s string) string {
|
||||
s, n := normalizeLine(s, lo.cp)
|
||||
lo.unknown += n
|
||||
return s
|
||||
}
|
||||
|
||||
// centre places lines of width cells in the middle of cols cells.
|
||||
func centre(lines []blockLine, width, cols int) []blockLine {
|
||||
margin := (cols - width) / 2
|
||||
if margin <= 0 {
|
||||
return lines
|
||||
}
|
||||
for i, l := range lines {
|
||||
lines[i] = append(blockLine{plain(spaces(margin))}, l...)
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
// blockWidth is the width of a block with the given columns field on a
|
||||
// line of cols cells.
|
||||
func blockWidth(columns, cols int) int {
|
||||
if columns > 0 {
|
||||
return min(columns, cols)
|
||||
}
|
||||
return cols
|
||||
}
|
||||
|
||||
// Kinds of item in a box body.
|
||||
const (
|
||||
itemText = iota
|
||||
itemFeed
|
||||
itemDivider
|
||||
itemKV
|
||||
)
|
||||
|
||||
// boxItem is one part of a box body, with its text already normalized.
|
||||
type boxItem struct {
|
||||
kind int
|
||||
|
||||
// itemText.
|
||||
text string
|
||||
style textStyle
|
||||
align escpos.Justification
|
||||
wrap bool
|
||||
|
||||
// itemFeed.
|
||||
lines int
|
||||
|
||||
// itemDivider: label, style and alignment, and the weight of the line.
|
||||
double bool
|
||||
|
||||
// itemKV.
|
||||
pairs []kvRow
|
||||
leader string
|
||||
}
|
||||
|
||||
// kvRow is a Pair with its text normalized.
|
||||
type kvRow struct {
|
||||
key, value string
|
||||
bold bool
|
||||
}
|
||||
|
||||
// box lays out a box on a line of cols cells. Every line it returns, apart
|
||||
// from the centring margin, is exactly the box width.
|
||||
func (lo *layout) box(el Element, spec boxSpec, cols int) []blockLine {
|
||||
set := bordersFor(spec.style, lo.cp)
|
||||
items := lo.boxItems(el, spec)
|
||||
title, footer := lo.line(el.Title), lo.line(el.Footer)
|
||||
titleRoom := inlineTitleRoom
|
||||
if spec.capped {
|
||||
titleRoom = cappedTitleRoom
|
||||
}
|
||||
|
||||
width := blockWidth(spec.columns, cols)
|
||||
padding := spec.padding
|
||||
if spec.fit {
|
||||
need := minBoxWidth + 2*padding
|
||||
if title != "" {
|
||||
need = max(need, runeLen(title)+titleRoom)
|
||||
}
|
||||
if footer != "" {
|
||||
need = max(need, runeLen(footer)+footerRoom)
|
||||
}
|
||||
for _, it := range items {
|
||||
if it.kind == itemDivider {
|
||||
if it.text != "" {
|
||||
// The label needs its room inside the two junctions.
|
||||
need = max(need, runeLen(it.text)+labelRoom+2)
|
||||
}
|
||||
continue
|
||||
}
|
||||
need = max(need, it.naturalWidth(width-2-2*padding)+2+2*padding)
|
||||
}
|
||||
width = min(width, need)
|
||||
}
|
||||
// Narrow boxes give up padding before they give up content.
|
||||
width = max(width, minBoxWidth)
|
||||
padding = min(padding, (width-minBoxWidth)/2)
|
||||
content := width - 2 - 2*padding
|
||||
|
||||
lines := []blockLine{set.top(title, spec.capped, width)}
|
||||
for _, it := range items {
|
||||
if it.kind == itemDivider {
|
||||
lines = append(lines, lo.divider(set, it, width))
|
||||
continue
|
||||
}
|
||||
for _, row := range it.contentLines(content) {
|
||||
line := blockLine{plain(string(set.vertical) + spaces(padding))}
|
||||
line = append(line, row...)
|
||||
line = append(line, plain(spaces(padding)+string(set.vertical)))
|
||||
lines = append(lines, line)
|
||||
}
|
||||
}
|
||||
lines = append(lines, set.bottom(footer, width))
|
||||
return centre(lines, width, cols)
|
||||
}
|
||||
|
||||
// boxItems turns the body of a box, its text or its children, into items.
|
||||
// The job has been validated, so the children's specs parse; their errors
|
||||
// are not checked again here.
|
||||
func (lo *layout) boxItems(el Element, spec boxSpec) []boxItem {
|
||||
if len(el.Children) == 0 {
|
||||
return []boxItem{{kind: itemText, text: lo.text(el.Text), style: spec.text, align: spec.align, wrap: true}}
|
||||
}
|
||||
var items []boxItem
|
||||
for _, child := range el.Children {
|
||||
switch child.Type {
|
||||
case TypeText:
|
||||
ts, _ := child.textSpec()
|
||||
align := ts.align
|
||||
if child.Align == "" {
|
||||
align = spec.align
|
||||
}
|
||||
items = append(items, boxItem{
|
||||
kind: itemText,
|
||||
text: lo.text(child.Text),
|
||||
style: textStyle{bold: ts.bold, underline: ts.underline, invert: ts.invert},
|
||||
align: align,
|
||||
wrap: ts.wrap,
|
||||
})
|
||||
case TypeFeed:
|
||||
n, _ := child.feedLines()
|
||||
items = append(items, boxItem{kind: itemFeed, lines: int(n)})
|
||||
case TypeRule:
|
||||
rs, _ := child.ruleSpec(true)
|
||||
items = append(items, boxItem{
|
||||
kind: itemDivider,
|
||||
text: lo.line(child.Text),
|
||||
style: textStyle{bold: rs.bold},
|
||||
align: rs.align,
|
||||
double: rs.style == ruleDouble,
|
||||
})
|
||||
case TypeKV:
|
||||
ks, _ := child.kvSpec()
|
||||
items = append(items, boxItem{kind: itemKV, pairs: lo.kvRows(child.Pairs), leader: leaderFor(ks.leader, lo.cp)})
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
// naturalWidth is the content width a text or kv item needs, given at most
|
||||
// limit cells; fit boxes shrink to the widest item.
|
||||
func (it boxItem) naturalWidth(limit int) int {
|
||||
limit = max(limit, 1)
|
||||
switch it.kind {
|
||||
case itemText:
|
||||
widest := 0
|
||||
for _, row := range it.rows(limit) {
|
||||
widest = max(widest, runeLen(row))
|
||||
}
|
||||
return widest
|
||||
case itemKV:
|
||||
widest := 0
|
||||
for _, p := range it.pairs {
|
||||
widest = max(widest, runeLen(p.key)+4+runeLen(p.value))
|
||||
}
|
||||
return min(widest, limit)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// rows splits a text item into lines of at most width runes.
|
||||
func (it boxItem) rows(width int) []string {
|
||||
if it.wrap {
|
||||
return wrap(it.text, width, width)
|
||||
}
|
||||
var rows []string
|
||||
for _, para := range strings.Split(it.text, "\n") {
|
||||
r := []rune(para)
|
||||
for len(r) > width {
|
||||
rows = append(rows, string(r[:width]))
|
||||
r = r[width:]
|
||||
}
|
||||
rows = append(rows, string(r))
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// contentLines lays out a text, feed or kv item at exactly width cells.
|
||||
func (it boxItem) contentLines(width int) []blockLine {
|
||||
switch it.kind {
|
||||
case itemFeed:
|
||||
lines := make([]blockLine, it.lines)
|
||||
for i := range lines {
|
||||
lines[i] = blockLine{plain(spaces(width))}
|
||||
}
|
||||
return lines
|
||||
case itemKV:
|
||||
return kvLines(it.pairs, it.leader, width)
|
||||
}
|
||||
var lines []blockLine
|
||||
for _, row := range it.rows(width) {
|
||||
n := runeLen(row)
|
||||
left := 0
|
||||
switch it.align {
|
||||
case escpos.JustifyCenter:
|
||||
left = (width - n) / 2
|
||||
case escpos.JustifyRight:
|
||||
left = width - n
|
||||
}
|
||||
lines = append(lines, blockLine{
|
||||
plain(spaces(left)),
|
||||
{text: row, style: it.style},
|
||||
plain(spaces(width - n - left)),
|
||||
})
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
// top draws the top border, with the title set into it.
|
||||
func (b borders) top(title string, capped bool, width int) blockLine {
|
||||
h := string(b.horizontal)
|
||||
inner := width - 2
|
||||
room := inlineTitleRoom
|
||||
if capped {
|
||||
room = cappedTitleRoom
|
||||
}
|
||||
title = truncate(title, width-room)
|
||||
if title == "" {
|
||||
return blockLine{plain(string(b.topLeft) + fill(h, inner) + string(b.topRight))}
|
||||
}
|
||||
head := string(b.topLeft) + h + " "
|
||||
tail := " "
|
||||
if capped {
|
||||
head = string(b.topLeft) + h + string(b.capLeft) + " "
|
||||
tail = " " + string(b.capRight)
|
||||
}
|
||||
rest := width - runeLen(head) - runeLen(title) - runeLen(tail) - 1
|
||||
return blockLine{
|
||||
plain(head),
|
||||
plain(title),
|
||||
plain(tail + fill(h, rest) + string(b.topRight)),
|
||||
}
|
||||
}
|
||||
|
||||
// bottom draws the bottom border, with the footer set into it on the right.
|
||||
func (b borders) bottom(footer string, width int) blockLine {
|
||||
h := string(b.horizontal)
|
||||
footer = truncate(footer, width-footerRoom)
|
||||
if footer == "" {
|
||||
return blockLine{plain(string(b.bottomLeft) + fill(h, width-2) + string(b.bottomRight))}
|
||||
}
|
||||
rest := width - runeLen(footer) - 6
|
||||
return blockLine{
|
||||
plain(string(b.bottomLeft) + fill(h, rest) + " "),
|
||||
plain(footer),
|
||||
plain(" " + fill(h, 2) + string(b.bottomRight)),
|
||||
}
|
||||
}
|
||||
|
||||
// divider draws a rule inside a box, joined to its borders.
|
||||
func (lo *layout) divider(b borders, it boxItem, width int) blockLine {
|
||||
left, h, right := b.divider(it.double, lo.cp)
|
||||
line := blockLine{plain(string(left))}
|
||||
line = append(line, labelled(string(h), it.text, it.style, it.align, width-2)...)
|
||||
return append(line, plain(string(right)))
|
||||
}
|
||||
|
||||
// labelled draws a line of pattern, width cells long, with label set into
|
||||
// it at align.
|
||||
func labelled(pattern, label string, style textStyle, align escpos.Justification, width int) blockLine {
|
||||
label = truncate(label, width-labelRoom)
|
||||
if label == "" {
|
||||
return blockLine{plain(fill(pattern, width))}
|
||||
}
|
||||
rest := width - runeLen(label) - 2
|
||||
var left int
|
||||
switch align {
|
||||
case escpos.JustifyLeft:
|
||||
left = 2
|
||||
case escpos.JustifyRight:
|
||||
left = rest - 2
|
||||
default:
|
||||
left = rest / 2
|
||||
}
|
||||
return blockLine{
|
||||
plain(fillTowards(pattern, left, true) + " "),
|
||||
{text: label, style: style},
|
||||
plain(" " + fillTowards(pattern, rest-left, false)),
|
||||
}
|
||||
}
|
||||
|
||||
// rule lays out a rule on a line of cols cells.
|
||||
func (lo *layout) rule(el Element, spec ruleSpec, cols int) []blockLine {
|
||||
width := blockWidth(spec.columns, cols)
|
||||
line := labelled(rulePattern(spec.style, lo.cp), lo.line(el.Text), textStyle{bold: spec.bold}, spec.align, width)
|
||||
return centre([]blockLine{line}, width, cols)
|
||||
}
|
||||
|
||||
// kv lays out a kv block on a line of cols cells.
|
||||
func (lo *layout) kv(el Element, spec kvSpec, cols int) []blockLine {
|
||||
width := blockWidth(spec.columns, cols)
|
||||
lines := kvLines(lo.kvRows(el.Pairs), leaderFor(spec.leader, lo.cp), width)
|
||||
return centre(lines, width, cols)
|
||||
}
|
||||
|
||||
func (lo *layout) kvRows(pairs []Pair) []kvRow {
|
||||
rows := make([]kvRow, len(pairs))
|
||||
for i, p := range pairs {
|
||||
rows[i] = kvRow{key: lo.line(p.Key), value: lo.text(p.Value), bold: p.Bold}
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// kvLines lays out rows at exactly width cells: the key, a space, a leader
|
||||
// of at least two, a space and the value. A value too long for the line
|
||||
// wraps to the room after the key, and its further lines are right-aligned.
|
||||
func kvLines(rows []kvRow, leader string, width int) []blockLine {
|
||||
if width < kvMinWidth {
|
||||
return narrowKVLines(rows, width)
|
||||
}
|
||||
var lines []blockLine
|
||||
for _, row := range rows {
|
||||
style := textStyle{bold: row.bold}
|
||||
key := truncate(row.key, max(width-8, 1))
|
||||
room := max(width-runeLen(key)-4, 1)
|
||||
values := wrap(row.value, room, room)
|
||||
first := values[0]
|
||||
dots := width - runeLen(key) - 2 - runeLen(first)
|
||||
tail := blockLine{plain(" "), {text: first, style: style}}
|
||||
if first == "" {
|
||||
dots++
|
||||
tail = nil
|
||||
}
|
||||
line := blockLine{{text: key, style: style}, plain(" " + fill(leader, dots))}
|
||||
lines = append(lines, append(line, tail...))
|
||||
for _, v := range values[1:] {
|
||||
lines = append(lines, blockLine{plain(spaces(width - runeLen(v))), {text: v, style: style}})
|
||||
}
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
// narrowKVLines lays out rows on lines too narrow for a key, a leader and a
|
||||
// value side by side: the key on a line of its own, then the value,
|
||||
// right-aligned.
|
||||
func narrowKVLines(rows []kvRow, width int) []blockLine {
|
||||
width = max(width, 1)
|
||||
var lines []blockLine
|
||||
for _, row := range rows {
|
||||
style := textStyle{bold: row.bold}
|
||||
key := truncate(row.key, width)
|
||||
lines = append(lines, blockLine{{text: key, style: style}, plain(spaces(width - runeLen(key)))})
|
||||
for _, v := range wrap(row.value, width, width) {
|
||||
lines = append(lines, blockLine{plain(spaces(width - runeLen(v))), {text: v, style: style}})
|
||||
}
|
||||
}
|
||||
return lines
|
||||
}
|
||||
@@ -0,0 +1,356 @@
|
||||
package receipt_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"uberbringer/internal/pkg/escpos"
|
||||
"uberbringer/internal/pkg/receipt"
|
||||
)
|
||||
|
||||
func intPtr(n int) *int { return &n }
|
||||
|
||||
// previewLines previews job on a TM-T88 in cp and returns its text lines,
|
||||
// without the final cut.
|
||||
func previewLines(t *testing.T, cp escpos.CodePage, elements ...receipt.Element) []string {
|
||||
t.Helper()
|
||||
p, err := newPreviewer(t, "tm-t88", cp).Preview(context.Background(), receipt.Job{Elements: elements})
|
||||
if err != nil {
|
||||
t.Fatalf("Preview: %v", err)
|
||||
}
|
||||
var lines []string
|
||||
for _, l := range p.Lines {
|
||||
if l.Kind == escpos.LineText {
|
||||
lines = append(lines, l.Text)
|
||||
}
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
func expectLines(t *testing.T, got []string, want ...string) {
|
||||
t.Helper()
|
||||
if strings.Join(got, "\n") != strings.Join(want, "\n") {
|
||||
t.Errorf("printed:\n%s\nwant:\n%s", strings.Join(got, "\n"), strings.Join(want, "\n"))
|
||||
}
|
||||
}
|
||||
|
||||
// TestMockups pins the boxes the README shows, each printed from one
|
||||
// element.
|
||||
func TestMockups(t *testing.T) {
|
||||
t.Run("double box, capped title, centred", func(t *testing.T) {
|
||||
got := previewLines(t, escpos.CodePage437, receipt.Element{
|
||||
Type: "box", Style: "double", Title: "ACHTUNG", TitleStyle: "capped", Align: "center",
|
||||
Text: "The coffee machine is being descaled.\nDo not use until 10:30.",
|
||||
})
|
||||
expectLines(t, got,
|
||||
"╔═╡ ACHTUNG ╞════════════════════════════╗",
|
||||
"║ The coffee machine is being descaled. ║",
|
||||
"║ Do not use until 10:30. ║",
|
||||
"╚════════════════════════════════════════╝",
|
||||
)
|
||||
})
|
||||
t.Run("fit, single", func(t *testing.T) {
|
||||
got := previewLines(t, escpos.CodePage437, receipt.Element{Type: "box", Fit: true, Padding: intPtr(3), Text: "!!! ACHTUNG !!!"})
|
||||
expectLines(t, got,
|
||||
" ┌─────────────────────┐",
|
||||
" │ !!! ACHTUNG !!! │",
|
||||
" └─────────────────────┘",
|
||||
)
|
||||
})
|
||||
t.Run("fit, double", func(t *testing.T) {
|
||||
got := previewLines(t, escpos.CodePage437, receipt.Element{Type: "box", Style: "double", Fit: true, Padding: intPtr(2), Text: "!!! ACHTUNG !!!"})
|
||||
expectLines(t, got,
|
||||
" ╔═══════════════════╗",
|
||||
" ║ !!! ACHTUNG !!! ║",
|
||||
" ╚═══════════════════╝",
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
// TestBoxMatrix lays out every box style, title style, padding, size and
|
||||
// font in every golden code page, and checks the geometry of each line.
|
||||
func TestBoxMatrix(t *testing.T) {
|
||||
styles := []string{"single", "double", "double-h", "double-v", "ascii"}
|
||||
const (
|
||||
tops = "┌╔╒╓+"
|
||||
topEnds = "┐╗╕╖+"
|
||||
sides = "│║|"
|
||||
bottoms = "└╚╘╙+"
|
||||
botEnds = "┘╝╛╜+"
|
||||
joins = "├╞╟╠+"
|
||||
joinEnd = "┤╡╢╣+"
|
||||
)
|
||||
for _, cp := range goldenCodePages {
|
||||
r := newPreviewer(t, "tm-t88", cp)
|
||||
for _, style := range styles {
|
||||
for _, titleStyle := range []string{"inline", "capped"} {
|
||||
for padding := range 4 {
|
||||
for size := 1; size <= 3; size++ {
|
||||
for _, font := range []string{"a", "b"} {
|
||||
el := receipt.Element{
|
||||
Type: "box", Style: style, Title: "Title", TitleStyle: titleStyle,
|
||||
Padding: intPtr(padding), Size: size, Font: font, Footer: "foot",
|
||||
Children: []receipt.Element{
|
||||
{Type: "text", Text: "Some text that wraps across the lines of the box."},
|
||||
{Type: "rule", Text: "label"},
|
||||
{Type: "kv", Pairs: []receipt.Pair{{Key: "k", Value: "value"}}},
|
||||
{Type: "rule", Style: "double"},
|
||||
},
|
||||
}
|
||||
name := fmt.Sprintf("%s/%s/%s/p%d/s%d/%s", cp, style, titleStyle, padding, size, font)
|
||||
p, err := r.Preview(context.Background(), receipt.Job{Elements: []receipt.Element{el}})
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %v", name, err)
|
||||
}
|
||||
glyph := map[string]int{"a": 12, "b": 9}[font]
|
||||
cols := 512 / (glyph * size)
|
||||
var lines []string
|
||||
for _, l := range p.Lines {
|
||||
if l.Kind == escpos.LineText {
|
||||
lines = append(lines, l.Text)
|
||||
}
|
||||
}
|
||||
for i, line := range lines {
|
||||
runes := []rune(line)
|
||||
if len(runes) != cols {
|
||||
t.Errorf("%s: line %d %q is %d wide, want %d", name, i, line, len(runes), cols)
|
||||
continue
|
||||
}
|
||||
first, last := string(runes[0]), string(runes[len(runes)-1])
|
||||
var wantFirst, wantLast string
|
||||
switch {
|
||||
case i == 0:
|
||||
wantFirst, wantLast = tops, topEnds
|
||||
case i == len(lines)-1:
|
||||
wantFirst, wantLast = bottoms, botEnds
|
||||
case strings.Contains(joins, first):
|
||||
wantFirst, wantLast = joins, joinEnd
|
||||
default:
|
||||
wantFirst, wantLast = sides, sides
|
||||
}
|
||||
if !strings.Contains(wantFirst, first) || !strings.Contains(wantLast, last) {
|
||||
t.Errorf("%s: line %d %q does not start and end with a border", name, i, line)
|
||||
}
|
||||
for _, r := range line {
|
||||
if !cp.CanEncode(r) {
|
||||
t.Errorf("%s: line %d has %q, which %s cannot print", name, i, r, cp)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBlockStyleRuns(t *testing.T) {
|
||||
p, err := newPreviewer(t, "tm-t88", escpos.CodePage437).Preview(context.Background(), receipt.Job{Elements: []receipt.Element{
|
||||
{Type: "box", Text: "Bold", Bold: true, Underline: 1, Invert: true, Title: "T"},
|
||||
{Type: "kv", Pairs: []receipt.Pair{{Key: "Key", Value: "Value", Bold: true}}},
|
||||
{Type: "rule", Text: "Label", Bold: true, Size: 2},
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
styled := func(line int) []escpos.Run { return p.Lines[line].Runs }
|
||||
bold := escpos.Run{Bold: true, Font: escpos.FontA, W: 1, H: 1}
|
||||
tests := []struct {
|
||||
line int
|
||||
want []escpos.Run
|
||||
}{
|
||||
{0, nil}, // the top border, title included, is plain
|
||||
{1, []escpos.Run{{From: 2, To: 6, Bold: true, Underline: escpos.UnderlineThin, Invert: true, Font: escpos.FontA, W: 1, H: 1}}},
|
||||
{2, nil},
|
||||
{3, []escpos.Run{withSpan(bold, 0, 3), withSpan(bold, 37, 42)}},
|
||||
{4, []escpos.Run{
|
||||
{From: 0, To: 8, Font: escpos.FontA, W: 2, H: 2},
|
||||
{From: 8, To: 13, Bold: true, Font: escpos.FontA, W: 2, H: 2},
|
||||
{From: 13, To: 21, Font: escpos.FontA, W: 2, H: 2},
|
||||
}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := styled(tt.line); fmt.Sprint(got) != fmt.Sprint(tt.want) {
|
||||
t.Errorf("line %d %q runs = %+v, want %+v", tt.line, p.Lines[tt.line].Text, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func withSpan(r escpos.Run, from, to int) escpos.Run {
|
||||
r.From, r.To = from, to
|
||||
return r
|
||||
}
|
||||
|
||||
func TestBoxLineSpacing(t *testing.T) {
|
||||
r := receipt.NewRenderer(receipt.Options{
|
||||
Profile: escpos.Profile{Name: "tm-t88", DotsPerLine: 512, FontAWidth: 12, FontBWidth: 9},
|
||||
CodePage: escpos.CodePage437,
|
||||
MaxPixels: 1000,
|
||||
MaxHeight: 1000,
|
||||
BoxLineSpacing: 24,
|
||||
}, &fakeFetcher{})
|
||||
out, err := r.Render(context.Background(), receipt.Job{Elements: []receipt.Element{
|
||||
{Type: "rule"},
|
||||
{Type: "box", Text: "spaced"},
|
||||
{Type: "kv", Pairs: []receipt.Pair{{Key: "a", Value: "b"}}},
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
set, reset := []byte{0x1B, '3', 24}, []byte{0x1B, '2'}
|
||||
if bytes.Count(out, set) != 1 || bytes.Count(out, reset) != 1 {
|
||||
t.Fatalf("want one ESC 3 24 and one ESC 2, got % X", out)
|
||||
}
|
||||
top := bytes.Index(out, escpos.CodePage437.Encode("┌"))
|
||||
bottom := bytes.Index(out, escpos.CodePage437.Encode("┘"))
|
||||
if i := bytes.Index(out, set); i > top || bytes.Index(out, reset) < bottom {
|
||||
t.Error("the line spacing must be set before the box and restored after it")
|
||||
}
|
||||
if i := bytes.Index(out, set); i < bytes.LastIndex(out[:top], escpos.CodePage437.Encode("─")) {
|
||||
t.Error("the rule before the box must keep the default spacing")
|
||||
}
|
||||
|
||||
plain := render(t, receipt.Job{Elements: []receipt.Element{{Type: "box", Text: "default"}}})
|
||||
if bytes.Contains(plain, set[:2]) || bytes.Contains(plain, reset) {
|
||||
t.Error("without BoxLineSpacing no line spacing command may be sent")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLineLimit(t *testing.T) {
|
||||
r := receipt.NewRenderer(receipt.Options{
|
||||
Profile: escpos.Profile{Name: "tm-t88", DotsPerLine: 512, FontAWidth: 12, FontBWidth: 9},
|
||||
CodePage: escpos.CodePage437,
|
||||
MaxPixels: 1000,
|
||||
MaxHeight: 1000,
|
||||
MaxLines: 100,
|
||||
}, &fakeFetcher{})
|
||||
tests := []struct {
|
||||
name string
|
||||
job receipt.Job
|
||||
lines int // 0 when the job is within the limit
|
||||
}{
|
||||
{"within", receipt.Job{Elements: []receipt.Element{{Type: "feed", Lines: 99}}}, 0},
|
||||
{"exactly the limit", receipt.Job{Elements: []receipt.Element{{Type: "feed", Lines: 99}, {Type: "text", Text: "x"}}}, 0},
|
||||
{"one over", receipt.Job{Elements: []receipt.Element{{Type: "feed", Lines: 100}, {Type: "text", Text: "x"}}}, 101},
|
||||
{"copies count", receipt.Job{Copies: 3, Elements: []receipt.Element{{Type: "feed", Lines: 40}}}, 120},
|
||||
{"wrapped text", receipt.Job{Elements: []receipt.Element{{Type: "text", Text: strings.Repeat("word ", 1000)}}}, 120},
|
||||
{"box", receipt.Job{Elements: []receipt.Element{{Type: "box", Text: strings.Repeat("\n", 98)}}}, 101},
|
||||
{"kv rows in a box", receipt.Job{Elements: []receipt.Element{{Type: "box", Children: []receipt.Element{{Type: "kv", Pairs: manyPairs(100)}}}}}, 102},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, renderErr := r.Render(context.Background(), tt.job)
|
||||
p, previewErr := r.Preview(context.Background(), tt.job)
|
||||
for name, err := range map[string]error{"Render": renderErr, "Preview": previewErr} {
|
||||
var limit *receipt.LineLimitError
|
||||
switch {
|
||||
case tt.lines == 0 && err != nil:
|
||||
t.Errorf("%s: %v", name, err)
|
||||
case tt.lines > 0 && !errors.As(err, &limit):
|
||||
t.Errorf("%s: err = %v, want *LineLimitError", name, err)
|
||||
case tt.lines > 0 && tt.name != "wrapped text" && limit.Lines != tt.lines:
|
||||
t.Errorf("%s: %d lines, want %d", name, limit.Lines, tt.lines)
|
||||
case tt.lines > 0 && limit.Error() != fmt.Sprintf("receipt would print %d lines, limit is 100", limit.Lines):
|
||||
t.Errorf("%s: message %q", name, limit.Error())
|
||||
}
|
||||
}
|
||||
if tt.lines == 0 && p.LineCount > 100 {
|
||||
t.Errorf("LineCount = %d", p.LineCount)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func manyPairs(n int) []receipt.Pair {
|
||||
pairs := make([]receipt.Pair, n)
|
||||
for i := range pairs {
|
||||
pairs[i] = receipt.Pair{Key: "k", Value: "v"}
|
||||
}
|
||||
return pairs
|
||||
}
|
||||
|
||||
func TestPreview(t *testing.T) {
|
||||
fetcher := &fakeFetcher{err: errors.New("preview must not fetch")}
|
||||
r := receipt.NewRenderer(receipt.Options{
|
||||
Profile: escpos.Profile{Name: "tm-t88", DotsPerLine: 512, FontAWidth: 12, FontBWidth: 9},
|
||||
CodePage: escpos.CodePage437,
|
||||
MaxPixels: 1000,
|
||||
MaxHeight: 1000,
|
||||
}, fetcher)
|
||||
job := receipt.Job{Copies: 2, Elements: []receipt.Element{
|
||||
{Type: "text", Text: "Tea “Earl Grey” 🍵", Align: "center"},
|
||||
{Type: "raw", Hex: "1B 61 01 07"},
|
||||
{Type: "image", URL: "https://images.example/logo.png?size=2", Width: 384},
|
||||
{Type: "image", Base64: "iVBORw0KGgo=", Width: 600},
|
||||
{Type: "qr", Data: "https://example.com"},
|
||||
{Type: "barcode", Symbology: "ean8", Data: "9638507"},
|
||||
{Type: "cut", Mode: "full"},
|
||||
}}
|
||||
p, err := r.Preview(context.Background(), job)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(fetcher.urls) != 0 {
|
||||
t.Errorf("preview fetched %v", fetcher.urls)
|
||||
}
|
||||
copyLines := []string{
|
||||
strings.Repeat(" ", 12) + `Tea "Earl Grey" ?`,
|
||||
"[raw 4 bytes]",
|
||||
"[image 384 dots: logo.png]",
|
||||
"[image 600 dots: base64]",
|
||||
"[qr: https://example.com]",
|
||||
"[barcode ean8: 9638507]",
|
||||
" - - - - - - - - full cut - - - - - - - -",
|
||||
}
|
||||
var got []string
|
||||
for _, l := range p.Lines {
|
||||
got = append(got, l.Text)
|
||||
}
|
||||
expectLines(t, got, append(copyLines, copyLines...)...)
|
||||
if p.Lines[2].Data != "https://images.example/logo.png?size=2" || p.Lines[3].Data != "base64" {
|
||||
t.Errorf("image data = %q, %q", p.Lines[2].Data, p.Lines[3].Data)
|
||||
}
|
||||
if p.Columns != 42 || p.CodePage != escpos.CodePage437 || p.LineCount != 10 {
|
||||
t.Errorf("columns %d, code page %s, %d lines; want 42, cp437 and 10", p.Columns, p.CodePage, p.LineCount)
|
||||
}
|
||||
wantWarnings := []string{
|
||||
"elements[0]: 1 character not in cp437 printed as ?",
|
||||
"elements[3] (image): width 600 is wider than the 512-dot paper, so the job would fail",
|
||||
}
|
||||
if strings.Join(p.Warnings, "\n") != strings.Join(wantWarnings, "\n") {
|
||||
t.Errorf("warnings = %q, want %q", p.Warnings, wantWarnings)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreviewRejectsInvalidJobs(t *testing.T) {
|
||||
_, err := newPreviewer(t, "tm-t88", escpos.CodePage437).Preview(context.Background(), receipt.Job{})
|
||||
var ve *receipt.ValidationError
|
||||
if !errors.As(err, &ve) {
|
||||
t.Errorf("err = %v, want *ValidationError", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRenderBlocksBytes pins the commands a block is printed with.
|
||||
func TestRenderBlocksBytes(t *testing.T) {
|
||||
out := render(t, receipt.Job{Elements: []receipt.Element{
|
||||
{Type: "text", Text: "x", Newline: boolPtr(false)},
|
||||
{Type: "rule", Columns: 4, Size: 2, Text: "", Style: "double"},
|
||||
}})
|
||||
want := []byte{
|
||||
'x', 0x1B, 'E', 0, 0x1B, '-', 0, 0x1D, 'B', 0, 0x1D, '!', 0, 0x1B, 'M', 0, // the text and its reset
|
||||
0x0A, // the inline line is ended before the block
|
||||
0x1B, 'a', 0, 0x1B, 'M', 0, 0x1D, '!', 0x11, // left, Font A, size 2
|
||||
0x1B, 'E', 0, 0x1B, '-', 0, 0x1D, 'B', 0, // plain
|
||||
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, // centred on 24 columns
|
||||
0xCD, 0xCD, 0xCD, 0xCD, 0x0A,
|
||||
0x1B, 'E', 0, 0x1B, '-', 0, 0x1D, 'B', 0, 0x1D, '!', 0, 0x1B, 'M', 0, // reset
|
||||
}
|
||||
if !bytes.Contains(out, want) {
|
||||
t.Errorf("stream % X does not contain % X", out, want)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package receipt
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"uberbringer/internal/pkg/escpos"
|
||||
)
|
||||
|
||||
// textStyle is how a run of block text prints. Borders, fills, padding and
|
||||
// margins are always plain.
|
||||
type textStyle struct {
|
||||
bold bool
|
||||
underline escpos.Underline
|
||||
invert bool
|
||||
}
|
||||
|
||||
// run is text printed in one style. Each rune is one character cell.
|
||||
type run struct {
|
||||
text string
|
||||
style textStyle
|
||||
}
|
||||
|
||||
// blockLine is one printed line of a block.
|
||||
type blockLine []run
|
||||
|
||||
func plain(s string) run {
|
||||
return run{text: s}
|
||||
}
|
||||
|
||||
func spaces(n int) string {
|
||||
return strings.Repeat(" ", max(n, 0))
|
||||
}
|
||||
|
||||
// fill repeats pattern to exactly n runes.
|
||||
func fill(pattern string, n int) string {
|
||||
p := []rune(pattern)
|
||||
if n <= 0 || len(p) == 0 {
|
||||
return ""
|
||||
}
|
||||
out := make([]rune, n)
|
||||
for i := range out {
|
||||
out[i] = p[i%len(p)]
|
||||
}
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// fillTowards is fill for the n runes before (towards the right end) or
|
||||
// after something, laid out so that the pattern starts next to it. A
|
||||
// dashed fill then always ends in a dash, never a space, at a label.
|
||||
func fillTowards(pattern string, n int, before bool) string {
|
||||
p := []rune(pattern)
|
||||
if n <= 0 || len(p) == 0 {
|
||||
return ""
|
||||
}
|
||||
out := make([]rune, n)
|
||||
for i := range out {
|
||||
d := i
|
||||
if before {
|
||||
d = n - 1 - i
|
||||
}
|
||||
out[i] = p[d%len(p)]
|
||||
}
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// truncate shortens s to at most n runes, marking the cut with "...".
|
||||
func truncate(s string, n int) string {
|
||||
r := []rune(s)
|
||||
switch {
|
||||
case n <= 0:
|
||||
return ""
|
||||
case len(r) <= n:
|
||||
return s
|
||||
case n <= 3:
|
||||
return string(r[:n])
|
||||
}
|
||||
return string(r[:n-3]) + "..."
|
||||
}
|
||||
|
||||
func runeLen(s string) int {
|
||||
return utf8.RuneCountInString(s)
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
package receipt_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"uberbringer/internal/pkg/escpos"
|
||||
"uberbringer/internal/pkg/receipt"
|
||||
)
|
||||
|
||||
// updateCompat rewrites the .hex files from the current renderer. It is a
|
||||
// flag of its own so that refreshing other golden files can never rewrite
|
||||
// these by accident.
|
||||
var updateCompat = flag.Bool("update-compat", false, "rewrite testdata/compat/*.hex from the current renderer")
|
||||
|
||||
// compatFixture is one testdata/compat/*.json file: a job and the renderer
|
||||
// options it is rendered with.
|
||||
type compatFixture struct {
|
||||
Options struct {
|
||||
Model string `json:"model"`
|
||||
CodePage string `json:"codepage"`
|
||||
Columns int `json:"columns"`
|
||||
} `json:"options"`
|
||||
Job receipt.Job `json:"job"`
|
||||
}
|
||||
|
||||
// TestRenderCompat renders each fixture job and compares the bytes with its
|
||||
// .hex file, so that any change to the bytes of these jobs fails a test.
|
||||
func TestRenderCompat(t *testing.T) {
|
||||
paths, err := filepath.Glob(filepath.Join("testdata", "compat", "*.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(paths) == 0 {
|
||||
t.Fatal("no fixtures in testdata/compat")
|
||||
}
|
||||
for _, path := range paths {
|
||||
name := strings.TrimSuffix(filepath.Base(path), ".json")
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got := renderCompatFixture(t, path)
|
||||
hexPath := strings.TrimSuffix(path, ".json") + ".hex"
|
||||
if *updateCompat {
|
||||
if err := os.WriteFile(hexPath, []byte(formatHex(got)), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
raw, err := os.ReadFile(hexPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want, err := hex.DecodeString(strings.Join(strings.Fields(string(raw)), ""))
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %v", hexPath, err)
|
||||
}
|
||||
if !bytes.Equal(got, want) {
|
||||
at := firstDifference(got, want)
|
||||
t.Errorf("rendered %d bytes, want %d; first difference at byte %d:\n got % X\nwant % X",
|
||||
len(got), len(want), at, window(got, at), window(want, at))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// updatePaper rewrites the bytes of receipts that were checked on paper.
|
||||
// Use it only after the new bytes have been printed and checked again.
|
||||
var updatePaper = flag.Bool("update-paper", false, "rewrite testdata/paper/*.hex from the current renderer")
|
||||
|
||||
// paperReceipts are jobs whose printed result was checked on the TM-T88IV,
|
||||
// with the file their bytes are pinned in. The bytes are rendered for a
|
||||
// TM-T88 in CP437 with the default options.
|
||||
var paperReceipts = []struct{ job, hex string }{
|
||||
{filepath.Join("..", "..", "..", "examples", "blocks.json"), filepath.Join("testdata", "paper", "blocks.hex")},
|
||||
}
|
||||
|
||||
// TestPaperReceipts fails when the bytes of a receipt checked on paper
|
||||
// change, so that the change is printed and checked again.
|
||||
func TestPaperReceipts(t *testing.T) {
|
||||
for _, receiptFile := range paperReceipts {
|
||||
t.Run(filepath.Base(receiptFile.job), func(t *testing.T) {
|
||||
profile, err := escpos.LookupProfile("tm-t88")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := receipt.NewRenderer(receipt.Options{
|
||||
Profile: profile,
|
||||
CodePage: escpos.CodePage437,
|
||||
MaxPixels: 25_000_000,
|
||||
MaxHeight: 4000,
|
||||
MaxLines: 2000,
|
||||
}, &fakeFetcher{})
|
||||
got, err := r.Render(context.Background(), readJob(t, receiptFile.job))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if *updatePaper {
|
||||
if err := os.MkdirAll(filepath.Dir(receiptFile.hex), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(receiptFile.hex, []byte(formatHex(got)), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
raw, err := os.ReadFile(receiptFile.hex)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want, err := hex.DecodeString(strings.Join(strings.Fields(string(raw)), ""))
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %v", receiptFile.hex, err)
|
||||
}
|
||||
if !bytes.Equal(got, want) {
|
||||
at := firstDifference(got, want)
|
||||
t.Errorf("%s renders %d bytes, %d were checked on paper; first difference at byte %d:\n got % X\nwant % X\nPrint it, have it checked, then run with -update-paper.",
|
||||
receiptFile.job, len(got), len(want), at, window(got, at), window(want, at))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func renderCompatFixture(t *testing.T, path string) []byte {
|
||||
t.Helper()
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var fixture compatFixture
|
||||
dec := json.NewDecoder(bytes.NewReader(data))
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(&fixture); err != nil {
|
||||
t.Fatalf("%s: %v", path, err)
|
||||
}
|
||||
opts := fixture.Options
|
||||
if opts.Model == "" {
|
||||
opts.Model = "tm-t88"
|
||||
}
|
||||
if opts.CodePage == "" {
|
||||
opts.CodePage = "cp437"
|
||||
}
|
||||
profile, err := escpos.LookupProfile(opts.Model)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
codePage, err := escpos.ParseCodePage(opts.CodePage)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
renderer := receipt.NewRenderer(receipt.Options{
|
||||
Profile: profile,
|
||||
CodePage: codePage,
|
||||
Columns: opts.Columns,
|
||||
MaxPixels: 10_000_000,
|
||||
MaxHeight: 4000,
|
||||
}, &fakeFetcher{})
|
||||
out, err := renderer.Render(context.Background(), fixture.Job)
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// formatHex writes data as lowercase hex, 32 bytes per line.
|
||||
func formatHex(data []byte) string {
|
||||
var b strings.Builder
|
||||
for len(data) > 0 {
|
||||
n := min(32, len(data))
|
||||
b.WriteString(hex.EncodeToString(data[:n]))
|
||||
b.WriteByte('\n')
|
||||
data = data[n:]
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func firstDifference(a, b []byte) int {
|
||||
for i := range min(len(a), len(b)) {
|
||||
if a[i] != b[i] {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return min(len(a), len(b))
|
||||
}
|
||||
|
||||
// window returns up to 16 bytes of data starting a few bytes before at.
|
||||
func window(data []byte, at int) []byte {
|
||||
from := max(0, at-4)
|
||||
return data[min(from, len(data)):min(at+12, len(data))]
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
package receipt_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"uberbringer/internal/pkg/escpos"
|
||||
"uberbringer/internal/pkg/receipt"
|
||||
)
|
||||
|
||||
// FuzzBlocks lays out random boxes, rules and kv blocks on random printers
|
||||
// and code pages, and checks that every line is exactly the block's width,
|
||||
// no wider than the line, with the borders in the first and last columns,
|
||||
// and that no control byte reaches the printer inside the text.
|
||||
func FuzzBlocks(f *testing.F) {
|
||||
f.Add(uint8(0), uint8(0), uint8(0), uint8(1), uint8(0), uint8(1), false, uint8(0), "Title", "Footer", "Some text that wraps", "key", "value")
|
||||
f.Add(uint8(0x3C), uint8(1), uint8(9), uint8(2), uint8(1), uint8(3), true, uint8(12), "“A long title” — with folds…", "", "line\nline\ttab 🎉👨\u200D👩\u200D👧", "k", "")
|
||||
f.Add(uint8(1), uint8(2), uint8(6), uint8(0), uint8(0), uint8(0), false, uint8(200), "", "", "Label", "", "")
|
||||
f.Add(uint8(2), uint8(3), uint8(4), uint8(1), uint8(1), uint8(2), false, uint8(7), "", "", "", "A key that is long", "a value that wraps over several lines")
|
||||
f.Add(uint8(0xF8), uint8(4), uint8(3), uint8(2), uint8(0), uint8(0), true, uint8(3), "\x1b@\x00", "\x07", "\x1bd\x05 text \x1dV", "\x0a", "\x09")
|
||||
f.Fuzz(func(t *testing.T, kind, printerSel, style, size, font, padding uint8, fit bool, columns uint8,
|
||||
title, footer, text, key, value string) {
|
||||
el := fuzzBlock(kind, style, size, font, padding, fit, columns, title, footer, text, key, value)
|
||||
job := receipt.Job{Elements: []receipt.Element{el}}
|
||||
if job.Validate() != nil {
|
||||
return
|
||||
}
|
||||
models := []string{"tm-t88", "tm-t20", "generic-58mm"}
|
||||
codePages := []escpos.CodePage{escpos.CodePage437, escpos.CodePage850, escpos.CodePage858, escpos.CodePage1252}
|
||||
profile, err := escpos.LookupProfile(models[int(printerSel)%len(models)])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cp := codePages[int(printerSel/3)%len(codePages)]
|
||||
override := 0
|
||||
if printerSel&0x80 != 0 {
|
||||
override = 30
|
||||
}
|
||||
r := receipt.NewRenderer(receipt.Options{
|
||||
Profile: profile, CodePage: cp, Columns: override,
|
||||
MaxPixels: 1000, MaxHeight: 1000, MaxLines: 100_000,
|
||||
}, &fakeFetcher{})
|
||||
|
||||
out, err := r.Render(context.Background(), job)
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
p, err := r.Preview(context.Background(), job)
|
||||
if err != nil {
|
||||
t.Fatalf("Preview: %v", err)
|
||||
}
|
||||
var lines []string
|
||||
for _, l := range p.Lines {
|
||||
if l.Kind == escpos.LineText {
|
||||
lines = append(lines, l.Text)
|
||||
}
|
||||
}
|
||||
if len(lines) == 0 {
|
||||
t.Fatal("a block printed no lines")
|
||||
}
|
||||
|
||||
lineDots := profile.DotsPerLine
|
||||
if override > 0 {
|
||||
lineDots = override * profile.FontAWidth
|
||||
}
|
||||
blockSize := max(el.Size, 1)
|
||||
cols := lineDots / (profile.GlyphWidth(fontOf(el.Font)) * blockSize)
|
||||
width := len([]rune(lines[0]))
|
||||
for i, line := range lines {
|
||||
n := len([]rune(line))
|
||||
if n != width || n > cols {
|
||||
t.Fatalf("line %d %q is %d wide; want %d like line 0, and at most %d", i, line, n, width, cols)
|
||||
}
|
||||
}
|
||||
if el.Type == "box" {
|
||||
checkBoxBorders(t, lines)
|
||||
}
|
||||
|
||||
// No tab may reach the printer, and every line feed ends a line.
|
||||
if bytes.IndexByte(out, '\t') >= 0 {
|
||||
t.Errorf("stream has a tab: % X", out)
|
||||
}
|
||||
if n := bytes.Count(out, []byte{'\n'}); n != len(lines) {
|
||||
t.Errorf("stream has %d line feeds for %d lines", n, len(lines))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func fontOf(name string) escpos.Font {
|
||||
if strings.EqualFold(name, "b") {
|
||||
return escpos.FontB
|
||||
}
|
||||
return escpos.FontA
|
||||
}
|
||||
|
||||
// fuzzBlock builds a box, rule or kv element from the fuzzer's values. The
|
||||
// low bits of kind pick the type; for a box, the others pick its children.
|
||||
func fuzzBlock(kind, style, size, font, padding uint8, fit bool, columns uint8, title, footer, text, key, value string) receipt.Element {
|
||||
boxStyles := []string{"single", "double", "double-h", "double-v", "ascii"}
|
||||
ruleStyles := []string{"single", "double", "light", "medium", "dark", "solid", "dashed", "dotted", "ascii"}
|
||||
leaders := []string{".", "·", " ", "_", "-", "─"}
|
||||
aligns := []string{"left", "center", "right"}
|
||||
el := receipt.Element{Size: 1 + int(size)%3, Columns: int(columns)}
|
||||
if font%2 == 1 {
|
||||
el.Font = "b"
|
||||
}
|
||||
switch kind % 3 {
|
||||
case 0:
|
||||
el.Type = "box"
|
||||
el.Style = boxStyles[int(style)%len(boxStyles)]
|
||||
el.Title, el.Footer, el.Fit = title, footer, fit
|
||||
el.Padding = intPtr(int(padding) % 4)
|
||||
el.Align = aligns[int(style)%len(aligns)]
|
||||
if style&0x10 != 0 {
|
||||
el.TitleStyle = "capped"
|
||||
}
|
||||
children := kind >> 2
|
||||
if children == 0 {
|
||||
el.Text = text
|
||||
if el.Text == "" {
|
||||
el.Text = "x"
|
||||
}
|
||||
break
|
||||
}
|
||||
if children&1 != 0 {
|
||||
el.Children = append(el.Children, receipt.Element{Type: "text", Text: text, Align: aligns[int(padding)%3], Wrap: boolPtr(children&0x10 == 0)})
|
||||
}
|
||||
if children&2 != 0 {
|
||||
el.Children = append(el.Children, receipt.Element{Type: "rule", Text: key, Style: []string{"single", "double"}[int(style)%2], Align: aligns[int(size)%3]})
|
||||
}
|
||||
if children&4 != 0 {
|
||||
el.Children = append(el.Children, receipt.Element{Type: "kv", Leader: leaders[int(font)%len(leaders)], Pairs: []receipt.Pair{{Key: key + "k", Value: value}}})
|
||||
}
|
||||
if children&8 != 0 {
|
||||
el.Children = append(el.Children, receipt.Element{Type: "feed", Lines: 1 + int(padding)%3})
|
||||
}
|
||||
case 1:
|
||||
el.Type = "rule"
|
||||
el.Style = ruleStyles[int(style)%len(ruleStyles)]
|
||||
el.Text = text
|
||||
el.Align = aligns[int(padding)%3]
|
||||
default:
|
||||
el.Type = "kv"
|
||||
el.Leader = leaders[int(style)%len(leaders)]
|
||||
el.Pairs = []receipt.Pair{{Key: key + "k", Value: value}, {Key: title + "t", Value: text, Bold: fit}}
|
||||
}
|
||||
return el
|
||||
}
|
||||
|
||||
// checkBoxBorders checks that each box line, after its centring margin,
|
||||
// starts and ends with a border glyph.
|
||||
func checkBoxBorders(t *testing.T, lines []string) {
|
||||
t.Helper()
|
||||
const (
|
||||
left = "┌╔╒╓+│║|└╚╘╙├╞╟╠"
|
||||
right = "┐╗╕╖+│║|┘╝╛╜┤╡╢╣"
|
||||
)
|
||||
margin := len(lines[0]) - len(strings.TrimLeft(lines[0], " "))
|
||||
for i, line := range lines {
|
||||
runes := []rune(line[margin:])
|
||||
if line[:margin] != strings.Repeat(" ", margin) || len(runes) < 2 {
|
||||
t.Fatalf("line %d %q does not share the box margin %d", i, line, margin)
|
||||
}
|
||||
if !strings.ContainsRune(left, runes[0]) || !strings.ContainsRune(right, runes[len(runes)-1]) {
|
||||
t.Fatalf("line %d %q does not start and end with a border", i, line)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package receipt
|
||||
|
||||
import "uberbringer/internal/pkg/escpos"
|
||||
|
||||
// borders are the glyphs one box style draws with.
|
||||
type borders struct {
|
||||
topLeft, topRight, bottomLeft, bottomRight rune
|
||||
horizontal, vertical rune
|
||||
|
||||
// capLeft and capRight close the top border around a capped title.
|
||||
capLeft, capRight rune
|
||||
|
||||
// doubleH and doubleV give the weight of the lines, so that dividers
|
||||
// and caps can join them.
|
||||
doubleH, doubleV bool
|
||||
ascii bool
|
||||
}
|
||||
|
||||
var borderSets = map[boxStyle]borders{
|
||||
boxSingle: {'┌', '┐', '└', '┘', '─', '│', '┤', '├', false, false, false},
|
||||
boxDouble: {'╔', '╗', '╚', '╝', '═', '║', '╡', '╞', true, true, false},
|
||||
boxDoubleH: {'╒', '╕', '╘', '╛', '═', '│', '╡', '╞', true, false, false},
|
||||
boxDoubleV: {'╓', '╖', '╙', '╜', '─', '║', '┤', '├', false, true, false},
|
||||
boxASCII: {'+', '+', '+', '+', '-', '|', '|', '|', false, false, true},
|
||||
}
|
||||
|
||||
// borderFallback is the order in which styles are tried when the code page
|
||||
// lacks a glyph of the style asked for. ascii prints everywhere.
|
||||
var borderFallback = map[boxStyle][]boxStyle{
|
||||
boxSingle: {boxSingle, boxASCII},
|
||||
boxDouble: {boxDouble, boxSingle, boxASCII},
|
||||
boxDoubleH: {boxDoubleH, boxDouble, boxSingle, boxASCII},
|
||||
boxDoubleV: {boxDoubleV, boxDouble, boxSingle, boxASCII},
|
||||
boxASCII: {boxASCII},
|
||||
}
|
||||
|
||||
// bordersFor returns the first style in style's fallback chain that cp can
|
||||
// print. Caps the code page lacks switch to the other vertical weight, so
|
||||
// on cp850 a double box caps its title with ╣ ╠ instead of ╡ ╞.
|
||||
func bordersFor(style boxStyle, cp escpos.CodePage) borders {
|
||||
for _, s := range borderFallback[style] {
|
||||
b := borderSets[s]
|
||||
if !encodesAll(cp, b.topLeft, b.topRight, b.bottomLeft, b.bottomRight, b.horizontal, b.vertical) {
|
||||
continue
|
||||
}
|
||||
if !b.ascii && !encodesAll(cp, b.capLeft, b.capRight) {
|
||||
left, right := junction(true, b.doubleH)
|
||||
if encodesAll(cp, left, right) {
|
||||
b.capLeft, b.capRight = right, left
|
||||
} else {
|
||||
b.capLeft, b.capRight = b.vertical, b.vertical
|
||||
}
|
||||
}
|
||||
return b
|
||||
}
|
||||
return borderSets[boxASCII]
|
||||
}
|
||||
|
||||
// junction returns the glyphs where a horizontal line meets a vertical one
|
||||
// from the right (├) and from the left (┤), for the given line weights.
|
||||
func junction(verticalDouble, horizontalDouble bool) (left, right rune) {
|
||||
switch {
|
||||
case verticalDouble && horizontalDouble:
|
||||
return '╠', '╣'
|
||||
case verticalDouble:
|
||||
return '╟', '╢'
|
||||
case horizontalDouble:
|
||||
return '╞', '╡'
|
||||
}
|
||||
return '├', '┤'
|
||||
}
|
||||
|
||||
// divider returns the glyphs of a divider across a box with borders b: a
|
||||
// double line when double is set, joined to both sides. A junction the code
|
||||
// page lacks first switches to the other line weight, so on cp850 ╟─╢
|
||||
// becomes ╠═╣ and ╞═╡ becomes ├─┤.
|
||||
func (b borders) divider(double bool, cp escpos.CodePage) (left, horizontal, right rune) {
|
||||
if !b.ascii {
|
||||
for _, d := range []bool{double, !double} {
|
||||
left, right := junction(b.doubleV, d)
|
||||
horizontal := '─'
|
||||
if d {
|
||||
horizontal = '═'
|
||||
}
|
||||
if encodesAll(cp, left, horizontal, right) {
|
||||
return left, horizontal, right
|
||||
}
|
||||
}
|
||||
}
|
||||
if double {
|
||||
return '+', '=', '+'
|
||||
}
|
||||
return '+', '-', '+'
|
||||
}
|
||||
|
||||
// rulePatterns are the fills of each rule style, best first; the pattern
|
||||
// repeats across the line.
|
||||
var rulePatterns = map[ruleStyle][]string{
|
||||
ruleSingle: {"─", "-"},
|
||||
ruleDouble: {"═", "="},
|
||||
ruleLight: {"░", "#"},
|
||||
ruleMedium: {"▒", "#"},
|
||||
ruleDark: {"▓", "#"},
|
||||
ruleSolid: {"█", "#"},
|
||||
ruleDashed: {"- "},
|
||||
ruleDotted: {"·", "."},
|
||||
ruleASCII: {"-"},
|
||||
}
|
||||
|
||||
// leaderFallbacks lists replacements for kv leaders a code page lacks.
|
||||
var leaderFallbacks = map[string][]string{
|
||||
"·": {"·", "."},
|
||||
"─": {"─", "-"},
|
||||
}
|
||||
|
||||
// rulePattern returns the fill of style that cp can print.
|
||||
func rulePattern(style ruleStyle, cp escpos.CodePage) string {
|
||||
return firstPrintable(rulePatterns[style], cp, "-")
|
||||
}
|
||||
|
||||
// leaderFor returns leader, or its replacement when cp lacks it.
|
||||
func leaderFor(leader string, cp escpos.CodePage) string {
|
||||
if alternatives, ok := leaderFallbacks[leader]; ok {
|
||||
return firstPrintable(alternatives, cp, ".")
|
||||
}
|
||||
return leader
|
||||
}
|
||||
|
||||
func firstPrintable(candidates []string, cp escpos.CodePage, last string) string {
|
||||
for _, c := range candidates {
|
||||
if printable(c, cp) {
|
||||
return c
|
||||
}
|
||||
}
|
||||
return last
|
||||
}
|
||||
|
||||
func encodesAll(cp escpos.CodePage, runes ...rune) bool {
|
||||
for _, r := range runes {
|
||||
if !cp.CanEncode(r) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
package receipt_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"uberbringer/internal/pkg/escpos"
|
||||
"uberbringer/internal/pkg/receipt"
|
||||
)
|
||||
|
||||
var update = flag.Bool("update", false, "rewrite testdata/blocks/*.txt from the current renderer")
|
||||
|
||||
// goldenCodePages are the code pages every block golden is rendered in, to
|
||||
// cover the glyph fallbacks.
|
||||
var goldenCodePages = []escpos.CodePage{escpos.CodePage437, escpos.CodePage850, escpos.CodePage1252}
|
||||
|
||||
// TestBlockGoldens previews each testdata/blocks/*.json job on a TM-T88 in
|
||||
// every golden code page and compares the lines with NAME.CODEPAGE.txt.
|
||||
// Run with -update to rewrite the .txt files, then review the diff.
|
||||
func TestBlockGoldens(t *testing.T) {
|
||||
paths, err := filepath.Glob(filepath.Join("testdata", "blocks", "*.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(paths) == 0 {
|
||||
t.Fatal("no jobs in testdata/blocks")
|
||||
}
|
||||
for _, path := range paths {
|
||||
job := readJob(t, path)
|
||||
for _, cp := range goldenCodePages {
|
||||
name := strings.TrimSuffix(filepath.Base(path), ".json") + "." + cp.String()
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got := previewText(t, newPreviewer(t, "tm-t88", cp), job)
|
||||
golden := filepath.Join("testdata", "blocks", name+".txt")
|
||||
if *update {
|
||||
if err := os.WriteFile(golden, []byte(got), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
want, err := os.ReadFile(golden)
|
||||
if err != nil {
|
||||
t.Fatalf("%v (run go test -run TestBlockGoldens -update)", err)
|
||||
}
|
||||
if got != string(want) {
|
||||
t.Errorf("preview differs from %s:\n%s", golden, diffLines(string(want), got))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestExampleJobs keeps the jobs in examples/ valid: each previews on a
|
||||
// TM-T88 in CP437 with nothing printed as '?'.
|
||||
func TestExampleJobs(t *testing.T) {
|
||||
paths, err := filepath.Glob(filepath.Join("..", "..", "..", "examples", "*.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(paths) == 0 {
|
||||
t.Fatal("no jobs in examples/")
|
||||
}
|
||||
for _, path := range paths {
|
||||
t.Run(filepath.Base(path), func(t *testing.T) {
|
||||
p, err := newPreviewer(t, "tm-t88", escpos.CodePage437).Preview(context.Background(), readJob(t, path))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(p.Warnings) > 0 {
|
||||
t.Errorf("warnings: %q", p.Warnings)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func readJob(t *testing.T, path string) receipt.Job {
|
||||
t.Helper()
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var job receipt.Job
|
||||
dec := json.NewDecoder(bytes.NewReader(data))
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(&job); err != nil {
|
||||
t.Fatalf("%s: %v", path, err)
|
||||
}
|
||||
return job
|
||||
}
|
||||
|
||||
func newPreviewer(t *testing.T, model string, cp escpos.CodePage) *receipt.Renderer {
|
||||
t.Helper()
|
||||
profile, err := escpos.LookupProfile(model)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return receipt.NewRenderer(receipt.Options{
|
||||
Profile: profile,
|
||||
CodePage: cp,
|
||||
MaxPixels: 10_000_000,
|
||||
MaxHeight: 4000,
|
||||
MaxLines: 2000,
|
||||
}, &fakeFetcher{})
|
||||
}
|
||||
|
||||
// previewText is the preview of job as text: its lines, then its warnings.
|
||||
func previewText(t *testing.T, r *receipt.Renderer, job receipt.Job) string {
|
||||
t.Helper()
|
||||
p, err := r.Preview(context.Background(), job)
|
||||
if err != nil {
|
||||
t.Fatalf("Preview: %v", err)
|
||||
}
|
||||
var b strings.Builder
|
||||
for _, l := range p.Lines {
|
||||
b.WriteString(l.Text)
|
||||
b.WriteByte('\n')
|
||||
}
|
||||
if len(p.Warnings) > 0 {
|
||||
b.WriteString("=== warnings\n")
|
||||
for _, w := range p.Warnings {
|
||||
b.WriteString(w)
|
||||
b.WriteByte('\n')
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// diffLines shows the lines of got that differ from want.
|
||||
func diffLines(want, got string) string {
|
||||
w, g := strings.Split(want, "\n"), strings.Split(got, "\n")
|
||||
var b strings.Builder
|
||||
for i := range max(len(w), len(g)) {
|
||||
var wl, gl string
|
||||
if i < len(w) {
|
||||
wl = w[i]
|
||||
}
|
||||
if i < len(g) {
|
||||
gl = g[i]
|
||||
}
|
||||
if wl != gl {
|
||||
fmt.Fprintf(&b, "line %d:\n want |%s|\n got |%s|\n", i+1, wl, gl)
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
+124
-20
@@ -3,16 +3,25 @@
|
||||
package receipt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"uberbringer/internal/pkg/escpos"
|
||||
)
|
||||
|
||||
// Element types.
|
||||
// ErrTrailingData is returned by DecodeJob for input with more after the
|
||||
// job.
|
||||
var ErrTrailingData = errors.New("trailing data after the job")
|
||||
|
||||
// Element types. Box, rule and kv are blocks: they are laid out to the
|
||||
// paper width when the job is rendered.
|
||||
const (
|
||||
TypeText = "text"
|
||||
TypeFeed = "feed"
|
||||
@@ -21,12 +30,16 @@ const (
|
||||
TypeImage = "image"
|
||||
TypeRaw = "raw"
|
||||
TypeCut = "cut"
|
||||
TypeBox = "box"
|
||||
TypeRule = "rule"
|
||||
TypeKV = "kv"
|
||||
)
|
||||
|
||||
// Job limits.
|
||||
// Job limits. MaxElements counts the children of boxes too.
|
||||
const (
|
||||
MaxElements = 500
|
||||
MaxCopies = 5
|
||||
MaxPairs = 100
|
||||
)
|
||||
|
||||
// Element defaults.
|
||||
@@ -45,10 +58,13 @@ type Job struct {
|
||||
Copies int `json:"copies,omitempty"`
|
||||
}
|
||||
|
||||
// Element is one item on a receipt. Type selects which fields apply; fields
|
||||
// that do not apply to the type are ignored. Width and Height mean character
|
||||
// multipliers for text, module width and bar height for barcodes, and target
|
||||
// width in dots for images.
|
||||
// Element is one item on a receipt. Type selects which fields apply. On the
|
||||
// blocks (box, rule and kv) and inside a box, a field that does not apply is
|
||||
// an error; on the other types it is ignored, except for the block fields.
|
||||
// Width and Height mean character multipliers for text, module width and
|
||||
// bar height for barcodes, and target width in dots for images. A block's
|
||||
// width is set by Columns, in characters of its own size and font, never by
|
||||
// Width; without Columns a block spans the line.
|
||||
type Element struct {
|
||||
Type string `json:"type"`
|
||||
|
||||
@@ -83,6 +99,42 @@ type Element struct {
|
||||
|
||||
// Cut.
|
||||
Mode string `json:"mode,omitempty"`
|
||||
|
||||
// Blocks. Style is the border or line style of a box or rule. Children
|
||||
// are the body of a box, one level deep.
|
||||
Children []Element `json:"children,omitempty"`
|
||||
Title string `json:"title,omitempty"`
|
||||
TitleStyle string `json:"title_style,omitempty"`
|
||||
Footer string `json:"footer,omitempty"`
|
||||
Style string `json:"style,omitempty"`
|
||||
Padding *int `json:"padding,omitempty"`
|
||||
Fit bool `json:"fit,omitempty"`
|
||||
Columns int `json:"columns,omitempty"`
|
||||
Pairs []Pair `json:"pairs,omitempty"`
|
||||
Leader string `json:"leader,omitempty"`
|
||||
}
|
||||
|
||||
// Pair is one row of a kv block: a key, a leader and a value.
|
||||
type Pair struct {
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
Bold bool `json:"bold,omitempty"`
|
||||
}
|
||||
|
||||
// DecodeJob strictly decodes one JSON job. An unknown field is an error, so
|
||||
// that a typo fails loudly instead of being ignored, and so is anything
|
||||
// after the job. It does not validate the job.
|
||||
func DecodeJob(data []byte) (Job, error) {
|
||||
var job Job
|
||||
dec := json.NewDecoder(bytes.NewReader(data))
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(&job); err != nil {
|
||||
return Job{}, err
|
||||
}
|
||||
if _, err := dec.Token(); !errors.Is(err, io.EOF) {
|
||||
return Job{}, ErrTrailingData
|
||||
}
|
||||
return job, nil
|
||||
}
|
||||
|
||||
// ValidationError lists every problem found in a job.
|
||||
@@ -99,19 +151,23 @@ func (e *ValidationError) Error() string {
|
||||
// to the printer unless the entire receipt can be printed.
|
||||
func (j Job) Validate() error {
|
||||
var problems []string
|
||||
switch {
|
||||
children := 0
|
||||
for _, el := range j.Elements {
|
||||
children += len(el.Children)
|
||||
}
|
||||
switch total := len(j.Elements) + children; {
|
||||
case len(j.Elements) == 0:
|
||||
problems = append(problems, "elements: at least one element is required")
|
||||
case len(j.Elements) > MaxElements:
|
||||
problems = append(problems, fmt.Sprintf("elements: %d elements, limit is %d", len(j.Elements), MaxElements))
|
||||
case total > MaxElements && children > 0:
|
||||
problems = append(problems, fmt.Sprintf("elements: %d elements including %d box children, limit is %d", total, children, MaxElements))
|
||||
case total > MaxElements:
|
||||
problems = append(problems, fmt.Sprintf("elements: %d elements, limit is %d", total, MaxElements))
|
||||
}
|
||||
if j.Copies < 0 || j.Copies > MaxCopies {
|
||||
problems = append(problems, fmt.Sprintf("copies must be 1-%d", MaxCopies))
|
||||
}
|
||||
for i, el := range j.Elements {
|
||||
if err := el.validate(); err != nil {
|
||||
problems = append(problems, fmt.Sprintf("elements[%d] (%s): %v", i, el.Type, err))
|
||||
}
|
||||
problems = append(problems, el.problems(fmt.Sprintf("elements[%d]", i), false)...)
|
||||
}
|
||||
if len(problems) > 0 {
|
||||
return &ValidationError{Problems: problems}
|
||||
@@ -123,11 +179,44 @@ func (j Job) copies() int {
|
||||
return max(j.Copies, 1)
|
||||
}
|
||||
|
||||
func (el Element) validate() error {
|
||||
// problems returns every problem with el, and with its children when it is
|
||||
// a box, each prefixed with its path, e.g. "elements[0].children[3] (qr): ".
|
||||
func (el Element) problems(path string, inBox bool) []string {
|
||||
prefix := fmt.Sprintf("%s (%s): ", path, el.Type)
|
||||
var messages []string
|
||||
for _, msg := range el.validate(inBox) {
|
||||
messages = append(messages, prefix+msg)
|
||||
}
|
||||
if el.Type == TypeBox && !inBox {
|
||||
for i, child := range el.Children {
|
||||
messages = append(messages, child.problems(fmt.Sprintf("%s.children[%d]", path, i), true)...)
|
||||
}
|
||||
}
|
||||
return messages
|
||||
}
|
||||
|
||||
// validate checks el's own fields. inBox is set for the children of a box,
|
||||
// which allow fewer types and fields.
|
||||
func (el Element) validate(inBox bool) []string {
|
||||
if inBox {
|
||||
switch el.Type {
|
||||
case TypeText, TypeFeed, TypeRule, TypeKV:
|
||||
case TypeBox, TypeQR, TypeBarcode, TypeImage, TypeRaw, TypeCut:
|
||||
return []string{"not allowed inside a box; place it after the box"}
|
||||
case "":
|
||||
return []string{"type is required"}
|
||||
default:
|
||||
return []string{fmt.Sprintf("unknown type %q (inside a box, want text, feed, rule or kv)", el.Type)}
|
||||
}
|
||||
}
|
||||
problems := el.checkFields(inBox)
|
||||
var err error
|
||||
switch el.Type {
|
||||
case TypeText:
|
||||
_, err = el.textSpec()
|
||||
if err == nil && inBox && el.Newline != nil && !*el.Newline {
|
||||
err = fmt.Errorf("newline:false is not allowed inside a box")
|
||||
}
|
||||
case TypeFeed:
|
||||
_, err = el.feedLines()
|
||||
case TypeQR:
|
||||
@@ -140,12 +229,22 @@ func (el Element) validate() error {
|
||||
_, err = el.rawBytes()
|
||||
case TypeCut:
|
||||
_, err = el.cutMode()
|
||||
case TypeBox:
|
||||
_, err = el.boxSpec()
|
||||
case TypeRule:
|
||||
_, err = el.ruleSpec(inBox)
|
||||
case TypeKV:
|
||||
_, err = el.kvSpec()
|
||||
problems = append(problems, el.pairProblems()...)
|
||||
case "":
|
||||
err = fmt.Errorf("type is required")
|
||||
default:
|
||||
err = fmt.Errorf("unknown type %q (want text, feed, qr, barcode, image, raw or cut)", el.Type)
|
||||
err = fmt.Errorf("unknown type %q (want text, feed, qr, barcode, image, raw, cut, box, rule or kv)", el.Type)
|
||||
}
|
||||
return err
|
||||
if err != nil {
|
||||
problems = append(problems, err.Error())
|
||||
}
|
||||
return problems
|
||||
}
|
||||
|
||||
type textSpec struct {
|
||||
@@ -187,15 +286,20 @@ func (el Element) textSpec() (textSpec, error) {
|
||||
if s.align, err = parseAlign(el.Align); err != nil {
|
||||
return s, err
|
||||
}
|
||||
switch el.Underline {
|
||||
case 0, 1, 2:
|
||||
s.underline = escpos.Underline(el.Underline)
|
||||
default:
|
||||
return s, fmt.Errorf("underline must be 0, 1 or 2")
|
||||
if s.underline, err = parseUnderline(el.Underline); err != nil {
|
||||
return s, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func parseUnderline(n int) (escpos.Underline, error) {
|
||||
switch n {
|
||||
case 0, 1, 2:
|
||||
return escpos.Underline(n), nil
|
||||
}
|
||||
return 0, fmt.Errorf("underline must be 0, 1 or 2")
|
||||
}
|
||||
|
||||
func (el Element) feedLines() (uint8, error) {
|
||||
switch {
|
||||
case el.Lines == 0:
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
package receipt
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"golang.org/x/text/unicode/norm"
|
||||
|
||||
"uberbringer/internal/pkg/escpos"
|
||||
)
|
||||
|
||||
// zeroWidthJoiner joins emoji into one sequence, such as a family.
|
||||
const zeroWidthJoiner = '\u200D'
|
||||
|
||||
// folds lists ASCII look-alikes for characters the code pages often lack,
|
||||
// best first. A fold is used only when the code page cannot print the
|
||||
// character itself, and only when it can print every rune of the fold.
|
||||
var folds = map[rune][]string{
|
||||
'“': {`"`},
|
||||
'”': {`"`},
|
||||
'„': {`"`},
|
||||
'‘': {"'"},
|
||||
'’': {"'"},
|
||||
'–': {"-"},
|
||||
'—': {"--"},
|
||||
'…': {"..."},
|
||||
'•': {"·", "."},
|
||||
'→': {"->"},
|
||||
'←': {"<-"},
|
||||
'✓': {"√", "x"},
|
||||
'✔': {"√", "x"},
|
||||
'×': {"x"},
|
||||
'€': {"EUR"},
|
||||
'™': {"TM"},
|
||||
'\u00A0': {" "},
|
||||
}
|
||||
|
||||
// normalize prepares text for printing in cp, so that each rune of the
|
||||
// result prints as one character and wrap can count columns in runes. It
|
||||
// removes control characters, composes the text to NFC, folds characters
|
||||
// the code page lacks to look-alikes, drops invisible selectors and
|
||||
// joiners, and turns each emoji sequence and every other character cp
|
||||
// cannot print into one '?'. It returns the text and the number of '?' it
|
||||
// introduced.
|
||||
func normalize(s string, cp escpos.CodePage) (string, int) {
|
||||
s = sanitize(s)
|
||||
if printable(s, cp) {
|
||||
return s, 0
|
||||
}
|
||||
runes := []rune(norm.NFC.String(s))
|
||||
var (
|
||||
b strings.Builder
|
||||
unknown int
|
||||
)
|
||||
b.Grow(len(s))
|
||||
for i := 0; i < len(runes); i++ {
|
||||
r := runes[i]
|
||||
switch {
|
||||
case r == '\n' || r == '\t' || cp.CanEncode(r):
|
||||
b.WriteRune(r)
|
||||
case invisible(r):
|
||||
default:
|
||||
if fold, ok := foldFor(r, cp); ok {
|
||||
b.WriteString(fold)
|
||||
continue
|
||||
}
|
||||
if pictographic(r) {
|
||||
i = emojiSequenceEnd(runes, i)
|
||||
}
|
||||
b.WriteByte('?')
|
||||
unknown++
|
||||
}
|
||||
}
|
||||
return b.String(), unknown
|
||||
}
|
||||
|
||||
// normalizeLine is normalize for text that must stay on one line, such as
|
||||
// a title: newlines and tabs become spaces.
|
||||
func normalizeLine(s string, cp escpos.CodePage) (string, int) {
|
||||
s, unknown := normalize(s, cp)
|
||||
return strings.Map(func(r rune) rune {
|
||||
if r == '\n' || r == '\t' {
|
||||
return ' '
|
||||
}
|
||||
return r
|
||||
}, s), unknown
|
||||
}
|
||||
|
||||
// printable reports whether cp prints every rune of s as itself.
|
||||
func printable(s string, cp escpos.CodePage) bool {
|
||||
for _, r := range s {
|
||||
if r != '\n' && r != '\t' && !cp.CanEncode(r) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func foldFor(r rune, cp escpos.CodePage) (string, bool) {
|
||||
for _, fold := range folds[r] {
|
||||
if printable(fold, cp) {
|
||||
return fold, true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// invisible reports whether r only changes how the characters around it
|
||||
// look: variation selectors, zero-width spaces and joiners, emoji skin
|
||||
// tones, tag characters and the keycap mark.
|
||||
func invisible(r rune) bool {
|
||||
switch {
|
||||
case r == '\uFE0E' || r == '\uFE0F':
|
||||
case r >= '\u200B' && r <= zeroWidthJoiner:
|
||||
case r >= 0x1F3FB && r <= 0x1F3FF:
|
||||
case r >= 0xE0000 && r <= 0xE007F:
|
||||
case r == '\u20E3':
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// pictographic reports whether r is in a block of symbols that are drawn
|
||||
// as emoji.
|
||||
func pictographic(r rune) bool {
|
||||
switch {
|
||||
case r >= 0x1F000 && r <= 0x1FAFF:
|
||||
case r >= 0x2190 && r <= 0x21FF:
|
||||
case r >= 0x2300 && r <= 0x23FF:
|
||||
case r >= 0x25A0 && r <= 0x27BF:
|
||||
case r >= 0x2B00 && r <= 0x2BFF:
|
||||
case r == 0x203C || r == 0x2049 || r == 0x3030 || r == 0x303D || r == 0x3297 || r == 0x3299:
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func regionalIndicator(r rune) bool {
|
||||
return r >= 0x1F1E6 && r <= 0x1F1FF
|
||||
}
|
||||
|
||||
// emojiSequenceEnd returns the index of the last rune of the emoji sequence
|
||||
// that starts at runes[i]: the emoji with its selectors and modifiers, any
|
||||
// emoji joined to it with a zero-width joiner, or the second half of a flag.
|
||||
func emojiSequenceEnd(runes []rune, i int) int {
|
||||
if regionalIndicator(runes[i]) && i+1 < len(runes) && regionalIndicator(runes[i+1]) {
|
||||
return i + 1
|
||||
}
|
||||
for i+1 < len(runes) {
|
||||
next := runes[i+1]
|
||||
switch {
|
||||
case next == zeroWidthJoiner && i+2 < len(runes) && pictographic(runes[i+2]):
|
||||
i += 2
|
||||
case invisible(next) && next != zeroWidthJoiner:
|
||||
i++
|
||||
default:
|
||||
return i
|
||||
}
|
||||
}
|
||||
return i
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
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
|
||||
}
|
||||
@@ -26,14 +26,19 @@ func (f *fakeFetcher) Fetch(_ context.Context, url string) ([]byte, error) {
|
||||
return f.data, f.err
|
||||
}
|
||||
|
||||
func newRenderer(t *testing.T, fetcher receipt.Fetcher) *receipt.Renderer {
|
||||
func tmT20(t *testing.T) escpos.Profile {
|
||||
t.Helper()
|
||||
profile, err := escpos.LookupProfile("tm-t20")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return profile
|
||||
}
|
||||
|
||||
func newRenderer(t *testing.T, fetcher receipt.Fetcher) *receipt.Renderer {
|
||||
t.Helper()
|
||||
return receipt.NewRenderer(receipt.Options{
|
||||
Profile: profile,
|
||||
Profile: tmT20(t),
|
||||
CodePage: escpos.CodePage437,
|
||||
MaxPixels: 10_000_000,
|
||||
MaxHeight: 4000,
|
||||
@@ -75,39 +80,21 @@ func render(t *testing.T, job receipt.Job) []byte {
|
||||
return out
|
||||
}
|
||||
|
||||
// printedLines returns the text the printer would print between line feeds,
|
||||
// with ESC/POS commands removed. It understands only the commands the
|
||||
// renderer emits for text jobs.
|
||||
// printedLines returns the text lines a TM-T20 prints for out, read back
|
||||
// with escpos.Interpret. Placeholders such as cuts are left out.
|
||||
func printedLines(t *testing.T, out []byte) []string {
|
||||
t.Helper()
|
||||
var lines []string
|
||||
var cur []byte
|
||||
for i := 0; i < len(out); i++ {
|
||||
switch c := out[i]; c {
|
||||
case 0x1B: // ESC x n, or ESC @ / ESC 2
|
||||
switch out[i+1] {
|
||||
case '@', '2':
|
||||
i++
|
||||
default:
|
||||
i += 2
|
||||
}
|
||||
case 0x1D: // GS x n, or GS V m n
|
||||
if out[i+1] == 'V' {
|
||||
i += 3
|
||||
} else {
|
||||
i += 2
|
||||
}
|
||||
case 0x0A:
|
||||
lines = append(lines, string(cur))
|
||||
cur = nil
|
||||
default:
|
||||
cur = append(cur, c)
|
||||
lines, err := escpos.Interpret(out, escpos.CodePage437, tmT20(t), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Interpret: %v", err)
|
||||
}
|
||||
var texts []string
|
||||
for _, l := range lines {
|
||||
if l.Kind == escpos.LineText {
|
||||
texts = append(texts, l.Text)
|
||||
}
|
||||
}
|
||||
if len(cur) > 0 {
|
||||
lines = append(lines, string(cur))
|
||||
}
|
||||
return lines
|
||||
return texts
|
||||
}
|
||||
|
||||
func TestRenderTextJob(t *testing.T) {
|
||||
@@ -167,7 +154,7 @@ func TestRenderInlineTextContinuesLine(t *testing.T) {
|
||||
{Type: "text", Text: "Total: ", Newline: boolPtr(false)},
|
||||
{Type: "text", Text: "£5.00", Bold: true},
|
||||
}})
|
||||
if got := printedLines(t, out); len(got) != 1 || got[0] != "Total: \x9C5.00" {
|
||||
if got := printedLines(t, out); len(got) != 1 || got[0] != "Total: £5.00" {
|
||||
t.Errorf("printed %q, want one line", got)
|
||||
}
|
||||
}
|
||||
@@ -188,8 +175,12 @@ func TestRenderInlineMixedSizesWraps(t *testing.T) {
|
||||
func TestRenderNoWrap(t *testing.T) {
|
||||
long := strings.Repeat("y", 60)
|
||||
out := render(t, receipt.Job{Elements: []receipt.Element{{Type: "text", Text: long, Wrap: boolPtr(false)}}})
|
||||
if got := printedLines(t, out); len(got) != 1 || got[0] != long {
|
||||
t.Errorf("printed %q, want the text untouched", got)
|
||||
if !bytes.Contains(out, []byte(long+"\n")) {
|
||||
t.Error("the renderer must send unwrapped text untouched")
|
||||
}
|
||||
// The printer wraps it at its own edge, 48 columns on a TM-T20.
|
||||
if got := printedLines(t, out); len(got) != 2 || got[0] != long[:48] || got[1] != long[48:] {
|
||||
t.Errorf("printed %q, want 48 and 12 characters", got)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+199
-17
@@ -26,13 +26,33 @@ type Options struct {
|
||||
CodePage escpos.CodePage
|
||||
|
||||
// Columns, when positive, overrides the Font A characters per line used
|
||||
// for word wrapping.
|
||||
// for word wrapping and block widths.
|
||||
Columns int
|
||||
|
||||
// MaxPixels caps decoded image size; MaxHeight caps printed image height
|
||||
// in dots.
|
||||
MaxPixels int
|
||||
MaxHeight int
|
||||
|
||||
// MaxLines, when positive, fails a job that would print more lines,
|
||||
// all copies counted. It is a backstop against runaway text.
|
||||
MaxLines int
|
||||
|
||||
// BoxLineSpacing, when positive, is the line spacing in motion units
|
||||
// (ESC 3) used while a box prints, to close the gaps in its vertical
|
||||
// borders. Zero keeps the printer's default spacing.
|
||||
BoxLineSpacing int
|
||||
}
|
||||
|
||||
// LineLimitError is returned for a job that would print more lines than
|
||||
// Options.MaxLines allows.
|
||||
type LineLimitError struct {
|
||||
Lines, Limit int
|
||||
}
|
||||
|
||||
// Error implements error.
|
||||
func (e *LineLimitError) Error() string {
|
||||
return fmt.Sprintf("receipt would print %d lines, limit is %d", e.Lines, e.Limit)
|
||||
}
|
||||
|
||||
// Renderer turns jobs into ESC/POS command streams. It is safe for concurrent
|
||||
@@ -53,6 +73,9 @@ func NewRenderer(options Options, fetcher Fetcher) *Renderer {
|
||||
if options.MaxPixels <= 0 || options.MaxHeight <= 0 {
|
||||
panic("receipt.NewRenderer: image limits must be positive")
|
||||
}
|
||||
if options.MaxLines < 0 || options.BoxLineSpacing < 0 || options.BoxLineSpacing > escpos.MaxLineSpacing {
|
||||
panic("receipt.NewRenderer: MaxLines must not be negative and BoxLineSpacing must be 0-255")
|
||||
}
|
||||
return &Renderer{options: options, fetcher: fetcher}
|
||||
}
|
||||
|
||||
@@ -67,26 +90,68 @@ func (r *Renderer) Render(ctx context.Context, job Job) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, err := r.render(job, images)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out.stream, nil
|
||||
}
|
||||
|
||||
// rendered is a job's command stream and what Preview needs to read it back.
|
||||
type rendered struct {
|
||||
stream []byte
|
||||
|
||||
// raw holds the byte range of every raw element in stream.
|
||||
raw []escpos.Span
|
||||
|
||||
// lines is the number of lines the stream prints.
|
||||
lines int
|
||||
|
||||
// unknown counts, by element index, the characters printed as '?'.
|
||||
unknown map[int]int
|
||||
}
|
||||
|
||||
// render lays out a validated job with its images loaded.
|
||||
func (r *Renderer) render(job Job, images map[int]*image.Paletted) (rendered, error) {
|
||||
var body escpos.Builder
|
||||
p := printer{b: &body, lineDots: r.textLineDots(), profile: r.options.Profile, codePage: r.options.CodePage}
|
||||
p := printer{
|
||||
b: &body,
|
||||
lineDots: r.textLineDots(),
|
||||
profile: r.options.Profile,
|
||||
codePage: r.options.CodePage,
|
||||
lineSpacing: r.options.BoxLineSpacing,
|
||||
}
|
||||
unknown := make(map[int]int)
|
||||
for i, el := range job.Elements {
|
||||
if err := p.element(el, images[i]); err != nil {
|
||||
return nil, fmt.Errorf("rendering elements[%d] (%s): %w", i, el.Type, err)
|
||||
return rendered{}, fmt.Errorf("rendering elements[%d] (%s): %w", i, el.Type, err)
|
||||
}
|
||||
if p.unknown > 0 {
|
||||
unknown[i] = p.unknown
|
||||
p.unknown = 0
|
||||
}
|
||||
}
|
||||
p.endLine()
|
||||
if last := job.Elements[len(job.Elements)-1]; last.Type != TypeCut {
|
||||
body.Cut(escpos.CutPartial, 0)
|
||||
}
|
||||
lines := p.lines * job.copies()
|
||||
if limit := r.options.MaxLines; limit > 0 && lines > limit {
|
||||
return rendered{}, &LineLimitError{Lines: lines, Limit: limit}
|
||||
}
|
||||
|
||||
var out escpos.Builder
|
||||
out.Initialize()
|
||||
out.SelectCodePage(r.options.CodePage)
|
||||
var spans []escpos.Span
|
||||
for range job.copies() {
|
||||
offset := out.Len()
|
||||
for _, s := range p.raw {
|
||||
spans = append(spans, escpos.Span{From: s.From + offset, To: s.To + offset})
|
||||
}
|
||||
out.Raw(body.Bytes())
|
||||
}
|
||||
return bytes.Clone(out.Bytes()), nil
|
||||
return rendered{stream: bytes.Clone(out.Bytes()), raw: spans, lines: lines, unknown: unknown}, nil
|
||||
}
|
||||
|
||||
// textLineDots is the line width used for text layout.
|
||||
@@ -159,23 +224,37 @@ func (r *Renderer) loadImage(ctx context.Context, el Element) (*image.Paletted,
|
||||
// printer writes elements to a Builder while tracking the horizontal print
|
||||
// position, in dots, so inline text can continue and wrap across elements.
|
||||
type printer struct {
|
||||
b *escpos.Builder
|
||||
lineDots int
|
||||
profile escpos.Profile
|
||||
codePage escpos.CodePage
|
||||
x int
|
||||
b *escpos.Builder
|
||||
lineDots int
|
||||
profile escpos.Profile
|
||||
codePage escpos.CodePage
|
||||
lineSpacing int
|
||||
x int
|
||||
|
||||
// lines counts printed lines: text lines, fed lines, and one for each
|
||||
// QR code, barcode and image.
|
||||
lines int
|
||||
|
||||
// unknown counts the characters of the current element printed as '?'.
|
||||
unknown int
|
||||
|
||||
// raw holds the byte range of each raw element in the Builder.
|
||||
raw []escpos.Span
|
||||
}
|
||||
|
||||
func (p *printer) element(el Element, img *image.Paletted) error {
|
||||
switch el.Type {
|
||||
case TypeText:
|
||||
return p.text(el)
|
||||
case TypeBox, TypeRule, TypeKV:
|
||||
return p.block(el)
|
||||
case TypeFeed:
|
||||
lines, err := el.feedLines()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.b.Feed(lines)
|
||||
p.lines += int(lines)
|
||||
p.x = 0
|
||||
case TypeQR:
|
||||
spec, err := el.qrSpec()
|
||||
@@ -184,6 +263,7 @@ func (p *printer) element(el Element, img *image.Paletted) error {
|
||||
}
|
||||
p.endLine()
|
||||
p.b.Justify(spec.align)
|
||||
p.lines++
|
||||
return p.b.QRCode([]byte(el.Data), spec.size, spec.correction)
|
||||
case TypeBarcode:
|
||||
spec, err := el.barcodeSpec()
|
||||
@@ -197,18 +277,22 @@ func (p *printer) element(el Element, img *image.Paletted) error {
|
||||
return err
|
||||
}
|
||||
p.b.BarcodeHRI(spec.hri)
|
||||
p.lines++
|
||||
return p.b.Barcode(spec.symbology, el.Data)
|
||||
case TypeImage:
|
||||
p.endLine()
|
||||
// Alignment is baked into the image; see Renderer.loadImage.
|
||||
p.b.Justify(escpos.JustifyLeft)
|
||||
p.lines++
|
||||
return p.b.RasterImage(img)
|
||||
case TypeRaw:
|
||||
data, err := el.rawBytes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
from := p.b.Len()
|
||||
p.b.Raw(data)
|
||||
p.raw = append(p.raw, escpos.Span{From: from, To: p.b.Len()})
|
||||
// The position after arbitrary bytes is unknown; assume a fresh line.
|
||||
p.x = 0
|
||||
case TypeCut:
|
||||
@@ -240,28 +324,33 @@ func (p *printer) text(el Element) error {
|
||||
p.b.Underline(spec.underline)
|
||||
p.b.Reverse(spec.invert)
|
||||
|
||||
text := sanitize(el.Text)
|
||||
text, unknown := normalize(el.Text, p.codePage)
|
||||
p.unknown += unknown
|
||||
if spec.wrap {
|
||||
text = expandTabs(text, p.x/glyph)
|
||||
lines := wrap(text, (p.lineDots-p.x)/glyph, p.lineDots/glyph)
|
||||
for i, line := range lines {
|
||||
if i > 0 {
|
||||
p.b.LineFeed()
|
||||
p.x = 0
|
||||
p.lineFeed()
|
||||
}
|
||||
p.b.Text(p.codePage.Encode(line))
|
||||
p.x += utf8.RuneCountInString(line) * glyph
|
||||
}
|
||||
} else {
|
||||
p.b.Text(p.codePage.Encode(text))
|
||||
p.lines += strings.Count(text, "\n")
|
||||
p.advance(text, glyph)
|
||||
}
|
||||
if spec.newline {
|
||||
p.b.LineFeed()
|
||||
p.x = 0
|
||||
p.lineFeed()
|
||||
}
|
||||
|
||||
// Reset styles so they cannot leak into barcodes, QR codes or raw data.
|
||||
return p.resetStyle()
|
||||
}
|
||||
|
||||
// resetStyle restores plain Font A at scale 1, so styles cannot leak into
|
||||
// barcodes, QR codes or raw data.
|
||||
func (p *printer) resetStyle() error {
|
||||
p.b.Bold(false)
|
||||
p.b.Underline(escpos.UnderlineNone)
|
||||
p.b.Reverse(false)
|
||||
@@ -272,6 +361,100 @@ func (p *printer) text(el Element) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// block lays out a box, rule or kv element and prints it line by line,
|
||||
// each line left-justified and exactly as wide as the layout made it.
|
||||
func (p *printer) block(el Element) error {
|
||||
lo := layout{cp: p.codePage}
|
||||
var (
|
||||
lines []blockLine
|
||||
size int
|
||||
font escpos.Font
|
||||
box bool
|
||||
)
|
||||
switch el.Type {
|
||||
case TypeBox:
|
||||
spec, err := el.boxSpec()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
size, font, box = spec.size, spec.font, true
|
||||
lines = lo.box(el, spec, p.blockColumns(font, size))
|
||||
case TypeRule:
|
||||
spec, err := el.ruleSpec(false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
size, font = spec.size, spec.font
|
||||
lines = lo.rule(el, spec, p.blockColumns(font, size))
|
||||
default:
|
||||
spec, err := el.kvSpec()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
size, font = spec.size, spec.font
|
||||
lines = lo.kv(el, spec, p.blockColumns(font, size))
|
||||
}
|
||||
p.unknown += lo.unknown
|
||||
|
||||
p.endLine()
|
||||
spaced := box && p.lineSpacing > 0
|
||||
if spaced {
|
||||
p.b.LineSpacing(uint8(p.lineSpacing))
|
||||
}
|
||||
p.b.Justify(escpos.JustifyLeft)
|
||||
p.b.Font(font)
|
||||
if err := p.b.Size(size, size); err != nil {
|
||||
return err
|
||||
}
|
||||
var current textStyle
|
||||
p.b.Bold(false)
|
||||
p.b.Underline(escpos.UnderlineNone)
|
||||
p.b.Reverse(false)
|
||||
for _, line := range lines {
|
||||
for _, r := range line {
|
||||
if r.text == "" {
|
||||
continue
|
||||
}
|
||||
p.changeStyle(current, r.style)
|
||||
current = r.style
|
||||
p.b.Text(p.codePage.Encode(r.text))
|
||||
}
|
||||
p.lineFeed()
|
||||
}
|
||||
if err := p.resetStyle(); err != nil {
|
||||
return err
|
||||
}
|
||||
if spaced {
|
||||
p.b.DefaultLineSpacing()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// blockColumns is how many characters of font at size fit on a text line.
|
||||
func (p *printer) blockColumns(font escpos.Font, size int) int {
|
||||
return max(p.lineDots/(p.profile.GlyphWidth(font)*size), 1)
|
||||
}
|
||||
|
||||
// changeStyle sends the commands that turn style from into style to.
|
||||
func (p *printer) changeStyle(from, to textStyle) {
|
||||
if from.bold != to.bold {
|
||||
p.b.Bold(to.bold)
|
||||
}
|
||||
if from.underline != to.underline {
|
||||
p.b.Underline(to.underline)
|
||||
}
|
||||
if from.invert != to.invert {
|
||||
p.b.Reverse(to.invert)
|
||||
}
|
||||
}
|
||||
|
||||
// lineFeed ends the current line.
|
||||
func (p *printer) lineFeed() {
|
||||
p.b.LineFeed()
|
||||
p.lines++
|
||||
p.x = 0
|
||||
}
|
||||
|
||||
// advance updates the position after unwrapped text, which the printer wraps
|
||||
// by itself when it overflows.
|
||||
func (p *printer) advance(text string, glyph int) {
|
||||
@@ -289,7 +472,6 @@ func (p *printer) advance(text string, glyph int) {
|
||||
// the left margin.
|
||||
func (p *printer) endLine() {
|
||||
if p.x > 0 {
|
||||
p.b.LineFeed()
|
||||
p.x = 0
|
||||
p.lineFeed()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
inline text before a box
|
||||
+- Children -----------------------------+
|
||||
| Centred by the box. |
|
||||
| Left by its own align. |
|
||||
| Bold and right. |
|
||||
| |
|
||||
| |
|
||||
| Unwrapped text is split at the content |
|
||||
| width instead of at spaces. |
|
||||
| Tabs line up from the content edge. |
|
||||
| Newline true is allowed. |
|
||||
+----------------------------------------+
|
||||
After the box, text continues at the left
|
||||
margin.
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,15 @@
|
||||
inline text before a box
|
||||
┌─ Children ─────────────────────────────┐
|
||||
│ Centred by the box. │
|
||||
│ Left by its own align. │
|
||||
│ Bold and right. │
|
||||
│ │
|
||||
│ │
|
||||
│ Unwrapped text is split at the content │
|
||||
│ width instead of at spaces. │
|
||||
│ Tabs line up from the content edge. │
|
||||
│ Newline true is allowed. │
|
||||
└────────────────────────────────────────┘
|
||||
After the box, text continues at the left
|
||||
margin.
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,15 @@
|
||||
inline text before a box
|
||||
┌─ Children ─────────────────────────────┐
|
||||
│ Centred by the box. │
|
||||
│ Left by its own align. │
|
||||
│ Bold and right. │
|
||||
│ │
|
||||
│ │
|
||||
│ Unwrapped text is split at the content │
|
||||
│ width instead of at spaces. │
|
||||
│ Tabs line up from the content edge. │
|
||||
│ Newline true is allowed. │
|
||||
└────────────────────────────────────────┘
|
||||
After the box, text continues at the left
|
||||
margin.
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,13 @@
|
||||
{"elements": [
|
||||
{"type": "text", "text": "inline text before a box ", "newline": false},
|
||||
{"type": "box", "title": "Children", "align": "center", "children": [
|
||||
{"type": "text", "text": "Centred by the box."},
|
||||
{"type": "text", "text": "Left by its own align.", "align": "left"},
|
||||
{"type": "text", "text": "Bold and right.", "bold": true, "align": "right"},
|
||||
{"type": "feed", "lines": 2},
|
||||
{"type": "text", "text": "Unwrapped text is split at the content width instead of at spaces.", "wrap": false, "align": "left"},
|
||||
{"type": "text", "text": "Tabs\tline up\tfrom the content edge.", "align": "left"},
|
||||
{"type": "text", "text": "Newline true is allowed.", "newline": true}
|
||||
]},
|
||||
{"type": "text", "text": "After the box, text continues at the left margin."}
|
||||
]}
|
||||
@@ -0,0 +1,52 @@
|
||||
+-----+
|
||||
| fit |
|
||||
+-----+
|
||||
+- A title longer than the text -+
|
||||
| short |
|
||||
+--------------------------------+
|
||||
+--------------------------+
|
||||
| short |
|
||||
+- a footer that is long --+
|
||||
+-| capped |-+
|
||||
| x |
|
||||
+------------+
|
||||
+---------------------------------------+
|
||||
| A fit box with more text than fits on |
|
||||
| one line wraps at the full width. |
|
||||
+---------------------------------------+
|
||||
+--------------+
|
||||
|Line one |
|
||||
|is longer than|
|
||||
|line three |
|
||||
+--------------+
|
||||
+---------------------+
|
||||
| Fit ............ kv |
|
||||
+-- a divider label --+
|
||||
| |
|
||||
+---------------------+
|
||||
+- columns 20 -----+
|
||||
| A fixed width, |
|
||||
| centred on the |
|
||||
| paper. |
|
||||
+------------------+
|
||||
+-------------------+
|
||||
| Odd columns |
|
||||
| centre with the |
|
||||
| extra space on |
|
||||
| the right. |
|
||||
+-------------------+
|
||||
+- columns 200 --------------------------+
|
||||
| Clamped to the paper. |
|
||||
+----------------------------------------+
|
||||
+----------------------+
|
||||
| fit inside a columns |
|
||||
| limit that is |
|
||||
| narrower than the |
|
||||
| text |
|
||||
+----------------------+
|
||||
+-+
|
||||
|x|
|
||||
|y|
|
||||
|z|
|
||||
+-+
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,52 @@
|
||||
┌─────┐
|
||||
│ fit │
|
||||
└─────┘
|
||||
┌─ A title longer than the text ─┐
|
||||
│ short │
|
||||
└────────────────────────────────┘
|
||||
┌──────────────────────────┐
|
||||
│ short │
|
||||
└─ a footer that is long ──┘
|
||||
╔═╡ capped ╞═╗
|
||||
║ x ║
|
||||
╚════════════╝
|
||||
┌───────────────────────────────────────┐
|
||||
│ A fit box with more text than fits on │
|
||||
│ one line wraps at the full width. │
|
||||
└───────────────────────────────────────┘
|
||||
┌──────────────┐
|
||||
│Line one │
|
||||
│is longer than│
|
||||
│line three │
|
||||
└──────────────┘
|
||||
┌─────────────────────┐
|
||||
│ Fit ............ kv │
|
||||
├── a divider label ──┤
|
||||
│ │
|
||||
└─────────────────────┘
|
||||
┌─ columns 20 ─────┐
|
||||
│ A fixed width, │
|
||||
│ centred on the │
|
||||
│ paper. │
|
||||
└──────────────────┘
|
||||
┌───────────────────┐
|
||||
│ Odd columns │
|
||||
│ centre with the │
|
||||
│ extra space on │
|
||||
│ the right. │
|
||||
└───────────────────┘
|
||||
┌─ columns 200 ──────────────────────────┐
|
||||
│ Clamped to the paper. │
|
||||
└────────────────────────────────────────┘
|
||||
┌──────────────────────┐
|
||||
│ fit inside a columns │
|
||||
│ limit that is │
|
||||
│ narrower than the │
|
||||
│ text │
|
||||
└──────────────────────┘
|
||||
┌─┐
|
||||
│x│
|
||||
│y│
|
||||
│z│
|
||||
└─┘
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,52 @@
|
||||
┌─────┐
|
||||
│ fit │
|
||||
└─────┘
|
||||
┌─ A title longer than the text ─┐
|
||||
│ short │
|
||||
└────────────────────────────────┘
|
||||
┌──────────────────────────┐
|
||||
│ short │
|
||||
└─ a footer that is long ──┘
|
||||
╔═╣ capped ╠═╗
|
||||
║ x ║
|
||||
╚════════════╝
|
||||
┌───────────────────────────────────────┐
|
||||
│ A fit box with more text than fits on │
|
||||
│ one line wraps at the full width. │
|
||||
└───────────────────────────────────────┘
|
||||
┌──────────────┐
|
||||
│Line one │
|
||||
│is longer than│
|
||||
│line three │
|
||||
└──────────────┘
|
||||
┌─────────────────────┐
|
||||
│ Fit ............ kv │
|
||||
├── a divider label ──┤
|
||||
│ │
|
||||
└─────────────────────┘
|
||||
┌─ columns 20 ─────┐
|
||||
│ A fixed width, │
|
||||
│ centred on the │
|
||||
│ paper. │
|
||||
└──────────────────┘
|
||||
┌───────────────────┐
|
||||
│ Odd columns │
|
||||
│ centre with the │
|
||||
│ extra space on │
|
||||
│ the right. │
|
||||
└───────────────────┘
|
||||
┌─ columns 200 ──────────────────────────┐
|
||||
│ Clamped to the paper. │
|
||||
└────────────────────────────────────────┘
|
||||
┌──────────────────────┐
|
||||
│ fit inside a columns │
|
||||
│ limit that is │
|
||||
│ narrower than the │
|
||||
│ text │
|
||||
└──────────────────────┘
|
||||
┌─┐
|
||||
│x│
|
||||
│y│
|
||||
│z│
|
||||
└─┘
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,18 @@
|
||||
{"elements": [
|
||||
{"type": "box", "fit": true, "text": "fit"},
|
||||
{"type": "box", "fit": true, "title": "A title longer than the text", "text": "short"},
|
||||
{"type": "box", "fit": true, "footer": "a footer that is long", "text": "short"},
|
||||
{"type": "box", "fit": true, "style": "double", "title": "capped", "title_style": "capped", "text": "x"},
|
||||
{"type": "box", "fit": true, "text": "A fit box with more text than fits on one line wraps at the full width."},
|
||||
{"type": "box", "fit": true, "padding": 0, "text": "Line one\nis longer than\nline three"},
|
||||
{"type": "box", "fit": true, "children": [
|
||||
{"type": "kv", "pairs": [{"key": "Fit", "value": "kv"}]},
|
||||
{"type": "rule", "text": "a divider label"},
|
||||
{"type": "feed"}
|
||||
]},
|
||||
{"type": "box", "columns": 20, "title": "columns 20", "text": "A fixed width, centred on the paper."},
|
||||
{"type": "box", "columns": 21, "text": "Odd columns centre with the extra space on the right."},
|
||||
{"type": "box", "columns": 200, "title": "columns 200", "text": "Clamped to the paper."},
|
||||
{"type": "box", "columns": 24, "fit": true, "text": "fit inside a columns limit that is narrower than the text"},
|
||||
{"type": "box", "columns": 2, "padding": 3, "text": "xyz"}
|
||||
]}
|
||||
@@ -0,0 +1,16 @@
|
||||
+- padding 0 ----------------------------+
|
||||
|Text runs right up to the borders when |
|
||||
|the padding is zero. |
|
||||
+----------------------------------------+
|
||||
+- padding 1 ----------------------------+
|
||||
| One space each side is the default |
|
||||
| padding. |
|
||||
+----------------------------------------+
|
||||
+- padding 2 ----------------------------+
|
||||
| Two spaces each side. |
|
||||
+----------------------------------------+
|
||||
+- padding 3 ----------------------------+
|
||||
| Three spaces each side, the most |
|
||||
| there is. |
|
||||
+----------------------------------------+
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,16 @@
|
||||
┌─ padding 0 ────────────────────────────┐
|
||||
│Text runs right up to the borders when │
|
||||
│the padding is zero. │
|
||||
└────────────────────────────────────────┘
|
||||
┌─ padding 1 ────────────────────────────┐
|
||||
│ One space each side is the default │
|
||||
│ padding. │
|
||||
└────────────────────────────────────────┘
|
||||
┌─ padding 2 ────────────────────────────┐
|
||||
│ Two spaces each side. │
|
||||
└────────────────────────────────────────┘
|
||||
╔═ padding 3 ════════════════════════════╗
|
||||
║ Three spaces each side, the most ║
|
||||
║ there is. ║
|
||||
╚════════════════════════════════════════╝
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,16 @@
|
||||
┌─ padding 0 ────────────────────────────┐
|
||||
│Text runs right up to the borders when │
|
||||
│the padding is zero. │
|
||||
└────────────────────────────────────────┘
|
||||
┌─ padding 1 ────────────────────────────┐
|
||||
│ One space each side is the default │
|
||||
│ padding. │
|
||||
└────────────────────────────────────────┘
|
||||
┌─ padding 2 ────────────────────────────┐
|
||||
│ Two spaces each side. │
|
||||
└────────────────────────────────────────┘
|
||||
╔═ padding 3 ════════════════════════════╗
|
||||
║ Three spaces each side, the most ║
|
||||
║ there is. ║
|
||||
╚════════════════════════════════════════╝
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,6 @@
|
||||
{"elements": [
|
||||
{"type": "box", "padding": 0, "title": "padding 0", "text": "Text runs right up to the borders when the padding is zero."},
|
||||
{"type": "box", "padding": 1, "title": "padding 1", "text": "One space each side is the default padding."},
|
||||
{"type": "box", "padding": 2, "title": "padding 2", "text": "Two spaces each side.", "align": "center"},
|
||||
{"type": "box", "padding": 3, "style": "double", "title": "padding 3", "text": "Three spaces each side, the most there is.", "align": "right"}
|
||||
]}
|
||||
@@ -0,0 +1,22 @@
|
||||
+- size 1 -------------------------------+
|
||||
| Forty-two columns of Font A. |
|
||||
+----------------------------------- a --+
|
||||
+- size 2 ----------+
|
||||
| Twenty-one |
|
||||
| columns. |
|
||||
+-------------- a --+
|
||||
+- size 3 ---+
|
||||
| Fourteen |
|
||||
| columns. |
|
||||
+------- a --+
|
||||
+- size 1 font b --------------------------------------+
|
||||
| Fifty-six columns of Font B on the same paper. |
|
||||
+------------------------------------------------- b --+
|
||||
+- size 2 font b ----------+
|
||||
| Twenty-eight columns. |
|
||||
+--------------------- b --+
|
||||
+- size 3 b -----+
|
||||
| Eighteen |
|
||||
| columns. |
|
||||
+----------- b --+
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,22 @@
|
||||
┌─ size 1 ───────────────────────────────┐
|
||||
│ Forty-two columns of Font A. │
|
||||
└─────────────────────────────────── a ──┘
|
||||
┌─ size 2 ──────────┐
|
||||
│ Twenty-one │
|
||||
│ columns. │
|
||||
└────────────── a ──┘
|
||||
╔═ size 3 ═══╗
|
||||
║ Fourteen ║
|
||||
║ columns. ║
|
||||
╚═══════ a ══╝
|
||||
┌─ size 1 font b ──────────────────────────────────────┐
|
||||
│ Fifty-six columns of Font B on the same paper. │
|
||||
└───────────────────────────────────────────────── b ──┘
|
||||
┌─ size 2 font b ──────────┐
|
||||
│ Twenty-eight columns. │
|
||||
└───────────────────── b ──┘
|
||||
╔═ size 3 b ═════╗
|
||||
║ Eighteen ║
|
||||
║ columns. ║
|
||||
╚═══════════ b ══╝
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,22 @@
|
||||
┌─ size 1 ───────────────────────────────┐
|
||||
│ Forty-two columns of Font A. │
|
||||
└─────────────────────────────────── a ──┘
|
||||
┌─ size 2 ──────────┐
|
||||
│ Twenty-one │
|
||||
│ columns. │
|
||||
└────────────── a ──┘
|
||||
╔═ size 3 ═══╗
|
||||
║ Fourteen ║
|
||||
║ columns. ║
|
||||
╚═══════ a ══╝
|
||||
┌─ size 1 font b ──────────────────────────────────────┐
|
||||
│ Fifty-six columns of Font B on the same paper. │
|
||||
└───────────────────────────────────────────────── b ──┘
|
||||
┌─ size 2 font b ──────────┐
|
||||
│ Twenty-eight columns. │
|
||||
└───────────────────── b ──┘
|
||||
╔═ size 3 b ═════╗
|
||||
║ Eighteen ║
|
||||
║ columns. ║
|
||||
╚═══════════ b ══╝
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,8 @@
|
||||
{"elements": [
|
||||
{"type": "box", "size": 1, "title": "size 1", "footer": "a", "text": "Forty-two columns of Font A."},
|
||||
{"type": "box", "size": 2, "title": "size 2", "footer": "a", "text": "Twenty-one columns."},
|
||||
{"type": "box", "size": 3, "style": "double", "title": "size 3", "footer": "a", "text": "Fourteen columns."},
|
||||
{"type": "box", "size": 1, "font": "b", "title": "size 1 font b", "footer": "b", "text": "Fifty-six columns of Font B on the same paper."},
|
||||
{"type": "box", "size": 2, "font": "b", "title": "size 2 font b", "footer": "b", "text": "Twenty-eight columns."},
|
||||
{"type": "box", "size": 3, "font": "B", "style": "double", "title": "size 3 b", "footer": "b", "text": "Eighteen columns."}
|
||||
]}
|
||||
@@ -0,0 +1,41 @@
|
||||
+- single -------------------------------+
|
||||
| Inline title, default style. |
|
||||
+----------------------------------------+
|
||||
+- double -------------------------------+
|
||||
| Inline title. |
|
||||
+----------------------------------------+
|
||||
+- double-h -----------------------------+
|
||||
| Inline title. |
|
||||
+----------------------------------------+
|
||||
+- double-v -----------------------------+
|
||||
| Inline title. |
|
||||
+----------------------------------------+
|
||||
+- ascii --------------------------------+
|
||||
| Inline title. |
|
||||
+----------------------------------------+
|
||||
+-| single |-----------------------------+
|
||||
| Capped title. |
|
||||
+----------------------------------------+
|
||||
+-| double |-----------------------------+
|
||||
| Capped title. |
|
||||
+----------------------------------------+
|
||||
+-| double-h |---------------------------+
|
||||
| Capped title. |
|
||||
+----------------------------------------+
|
||||
+-| double-v |---------------------------+
|
||||
| Capped title. |
|
||||
+----------------------------------------+
|
||||
+-| ascii |------------------------------+
|
||||
| Capped title. |
|
||||
+----------------------------------------+
|
||||
+----------------------------------------+
|
||||
| No title, a footer, and a long |
|
||||
| paragraph that wraps inside the |
|
||||
| borders. |
|
||||
| A second paragraph. |
|
||||
+------------------------ 29 Nov 21:04 --+
|
||||
+----------------------------------------+
|
||||
| Bold, underlined and inverted body |
|
||||
| text; the borders stay plain. |
|
||||
+----------------------------------------+
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,41 @@
|
||||
┌─ single ───────────────────────────────┐
|
||||
│ Inline title, default style. │
|
||||
└────────────────────────────────────────┘
|
||||
╔═ double ═══════════════════════════════╗
|
||||
║ Inline title. ║
|
||||
╚════════════════════════════════════════╝
|
||||
╒═ double-h ═════════════════════════════╕
|
||||
│ Inline title. │
|
||||
╘════════════════════════════════════════╛
|
||||
╓─ double-v ─────────────────────────────╖
|
||||
║ Inline title. ║
|
||||
╙────────────────────────────────────────╜
|
||||
+- ascii --------------------------------+
|
||||
| Inline title. |
|
||||
+----------------------------------------+
|
||||
┌─┤ single ├─────────────────────────────┐
|
||||
│ Capped title. │
|
||||
└────────────────────────────────────────┘
|
||||
╔═╡ double ╞═════════════════════════════╗
|
||||
║ Capped title. ║
|
||||
╚════════════════════════════════════════╝
|
||||
╒═╡ double-h ╞═══════════════════════════╕
|
||||
│ Capped title. │
|
||||
╘════════════════════════════════════════╛
|
||||
╓─┤ double-v ├───────────────────────────╖
|
||||
║ Capped title. ║
|
||||
╙────────────────────────────────────────╜
|
||||
+-| ascii |------------------------------+
|
||||
| Capped title. |
|
||||
+----------------------------------------+
|
||||
╔════════════════════════════════════════╗
|
||||
║ No title, a footer, and a long ║
|
||||
║ paragraph that wraps inside the ║
|
||||
║ borders. ║
|
||||
║ A second paragraph. ║
|
||||
╚════════════════════════ 29 Nov 21:04 ══╝
|
||||
┌────────────────────────────────────────┐
|
||||
│ Bold, underlined and inverted body │
|
||||
│ text; the borders stay plain. │
|
||||
└────────────────────────────────────────┘
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,41 @@
|
||||
┌─ single ───────────────────────────────┐
|
||||
│ Inline title, default style. │
|
||||
└────────────────────────────────────────┘
|
||||
╔═ double ═══════════════════════════════╗
|
||||
║ Inline title. ║
|
||||
╚════════════════════════════════════════╝
|
||||
╔═ double-h ═════════════════════════════╗
|
||||
║ Inline title. ║
|
||||
╚════════════════════════════════════════╝
|
||||
╔═ double-v ═════════════════════════════╗
|
||||
║ Inline title. ║
|
||||
╚════════════════════════════════════════╝
|
||||
+- ascii --------------------------------+
|
||||
| Inline title. |
|
||||
+----------------------------------------+
|
||||
┌─┤ single ├─────────────────────────────┐
|
||||
│ Capped title. │
|
||||
└────────────────────────────────────────┘
|
||||
╔═╣ double ╠═════════════════════════════╗
|
||||
║ Capped title. ║
|
||||
╚════════════════════════════════════════╝
|
||||
╔═╣ double-h ╠═══════════════════════════╗
|
||||
║ Capped title. ║
|
||||
╚════════════════════════════════════════╝
|
||||
╔═╣ double-v ╠═══════════════════════════╗
|
||||
║ Capped title. ║
|
||||
╚════════════════════════════════════════╝
|
||||
+-| ascii |------------------------------+
|
||||
| Capped title. |
|
||||
+----------------------------------------+
|
||||
╔════════════════════════════════════════╗
|
||||
║ No title, a footer, and a long ║
|
||||
║ paragraph that wraps inside the ║
|
||||
║ borders. ║
|
||||
║ A second paragraph. ║
|
||||
╚════════════════════════ 29 Nov 21:04 ══╝
|
||||
┌────────────────────────────────────────┐
|
||||
│ Bold, underlined and inverted body │
|
||||
│ text; the borders stay plain. │
|
||||
└────────────────────────────────────────┘
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,14 @@
|
||||
{"elements": [
|
||||
{"type": "box", "title": "single", "text": "Inline title, default style."},
|
||||
{"type": "box", "style": "double", "title": "double", "text": "Inline title."},
|
||||
{"type": "box", "style": "double-h", "title": "double-h", "text": "Inline title."},
|
||||
{"type": "box", "style": "double-v", "title": "double-v", "text": "Inline title."},
|
||||
{"type": "box", "style": "ascii", "title": "ascii", "text": "Inline title."},
|
||||
{"type": "box", "title": "single", "title_style": "capped", "text": "Capped title."},
|
||||
{"type": "box", "style": "double", "title": "double", "title_style": "capped", "text": "Capped title."},
|
||||
{"type": "box", "style": "double-h", "title": "double-h", "title_style": "capped", "text": "Capped title."},
|
||||
{"type": "box", "style": "double-v", "title": "double-v", "title_style": "capped", "text": "Capped title."},
|
||||
{"type": "box", "style": "ascii", "title": "ascii", "title_style": "capped", "text": "Capped title."},
|
||||
{"type": "box", "style": "DOUBLE", "footer": "29 Nov 21:04", "text": "No title, a footer, and a long paragraph that wraps inside the borders.\nA second paragraph."},
|
||||
{"type": "box", "text": "Bold, underlined and inverted body text; the borders stay plain.", "bold": true, "underline": 1, "invert": true, "align": "right"}
|
||||
]}
|
||||
@@ -0,0 +1,23 @@
|
||||
+- An inline title that is far too l... -+
|
||||
| Cut with an ellipsis. |
|
||||
+----------------------------------------+
|
||||
+-| A capped title that is far too ... |-+
|
||||
| Cut two columns earlier. |
|
||||
+----------------------------------------+
|
||||
+- Exactly thirty-six characters long!! -+
|
||||
| The longest inline title that fits. |
|
||||
+----------------------------------------+
|
||||
+- Thirty-seven characters long, too!!! -+
|
||||
| One more and it is cut. |
|
||||
+----------------------------------------+
|
||||
+----------------------------------------+
|
||||
| Cut with an ellipsis. |
|
||||
+- A footer that is far too long fo... --+
|
||||
+- Title with a newline and a tab -------+
|
||||
| Newlines and tabs in titles and |
|
||||
| footers become spaces. |
|
||||
+------------------------- Footer also --+
|
||||
+- A lon... -+
|
||||
| Big |
|
||||
+- A lo... --+
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,23 @@
|
||||
┌─ An inline title that is far too l... ─┐
|
||||
│ Cut with an ellipsis. │
|
||||
└────────────────────────────────────────┘
|
||||
╔═╡ A capped title that is far too ... ╞═╗
|
||||
║ Cut two columns earlier. ║
|
||||
╚════════════════════════════════════════╝
|
||||
┌─ Exactly thirty-six characters long!! ─┐
|
||||
│ The longest inline title that fits. │
|
||||
└────────────────────────────────────────┘
|
||||
┌─ Thirty-seven characters long, too!!! ─┐
|
||||
│ One more and it is cut. │
|
||||
└────────────────────────────────────────┘
|
||||
┌────────────────────────────────────────┐
|
||||
│ Cut with an ellipsis. │
|
||||
└─ A footer that is far too long fo... ──┘
|
||||
┌─ Title with a newline and a tab ───────┐
|
||||
│ Newlines and tabs in titles and │
|
||||
│ footers become spaces. │
|
||||
└───────────────────────── Footer also ──┘
|
||||
┌─ A lon... ─┐
|
||||
│ Big │
|
||||
└─ A lo... ──┘
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,23 @@
|
||||
┌─ An inline title that is far too l... ─┐
|
||||
│ Cut with an ellipsis. │
|
||||
└────────────────────────────────────────┘
|
||||
╔═╣ A capped title that is far too ... ╠═╗
|
||||
║ Cut two columns earlier. ║
|
||||
╚════════════════════════════════════════╝
|
||||
┌─ Exactly thirty-six characters long!! ─┐
|
||||
│ The longest inline title that fits. │
|
||||
└────────────────────────────────────────┘
|
||||
┌─ Thirty-seven characters long, too!!! ─┐
|
||||
│ One more and it is cut. │
|
||||
└────────────────────────────────────────┘
|
||||
┌────────────────────────────────────────┐
|
||||
│ Cut with an ellipsis. │
|
||||
└─ A footer that is far too long fo... ──┘
|
||||
┌─ Title with a newline and a tab ───────┐
|
||||
│ Newlines and tabs in titles and │
|
||||
│ footers become spaces. │
|
||||
└───────────────────────── Footer also ──┘
|
||||
┌─ A lon... ─┐
|
||||
│ Big │
|
||||
└─ A lo... ──┘
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,9 @@
|
||||
{"elements": [
|
||||
{"type": "box", "title": "An inline title that is far too long for the top border of the box", "text": "Cut with an ellipsis."},
|
||||
{"type": "box", "style": "double", "title_style": "capped", "title": "A capped title that is far too long for the top border of the box", "text": "Cut two columns earlier."},
|
||||
{"type": "box", "title": "Exactly thirty-six characters long!!", "text": "The longest inline title that fits."},
|
||||
{"type": "box", "title": "Thirty-seven characters long, too!!!", "text": "One more and it is cut."},
|
||||
{"type": "box", "footer": "A footer that is far too long for the bottom border", "text": "Cut with an ellipsis."},
|
||||
{"type": "box", "title": "Title\nwith a newline\tand a tab", "footer": "Footer\nalso", "text": "Newlines and tabs in titles and footers become spaces."},
|
||||
{"type": "box", "size": 3, "title": "A long title", "footer": "A long footer", "text": "Big"}
|
||||
]}
|
||||
@@ -0,0 +1,45 @@
|
||||
+- single box ---------------------------+
|
||||
| single rule |
|
||||
+----------------------------------------+
|
||||
| double rule |
|
||||
+========================================+
|
||||
| labelled |
|
||||
+-- Left --------------------------------+
|
||||
+---------------- Centre ----------------+
|
||||
+=============================== Right ==+
|
||||
| end |
|
||||
+----------------------------------------+
|
||||
+- double box ---------------------------+
|
||||
| single rule |
|
||||
+----------------------------------------+
|
||||
| double rule |
|
||||
+================ Label =================+
|
||||
| end |
|
||||
+----------------------------------------+
|
||||
+- double-h box -------------------------+
|
||||
| single rule |
|
||||
+----------------------------------------+
|
||||
| double rule |
|
||||
+================ Label =================+
|
||||
| end |
|
||||
+----------------------------------------+
|
||||
+- double-v box -------------------------+
|
||||
| single rule |
|
||||
+----------------------------------------+
|
||||
| double rule |
|
||||
+================ Label =================+
|
||||
| end |
|
||||
+----------------------------------------+
|
||||
+- ascii box ----------------------------+
|
||||
| single rule |
|
||||
+----------------------------------------+
|
||||
| double rule |
|
||||
+================ Label =================+
|
||||
| end |
|
||||
+----------------------------------------+
|
||||
+------------+
|
||||
| narrow |
|
||||
+-- too... --+
|
||||
+--- fits ---+
|
||||
+------------+
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,45 @@
|
||||
┌─ single box ───────────────────────────┐
|
||||
│ single rule │
|
||||
├────────────────────────────────────────┤
|
||||
│ double rule │
|
||||
╞════════════════════════════════════════╡
|
||||
│ labelled │
|
||||
├── Left ────────────────────────────────┤
|
||||
├──────────────── Centre ────────────────┤
|
||||
╞═══════════════════════════════ Right ══╡
|
||||
│ end │
|
||||
└────────────────────────────────────────┘
|
||||
╔═ double box ═══════════════════════════╗
|
||||
║ single rule ║
|
||||
╟────────────────────────────────────────╢
|
||||
║ double rule ║
|
||||
╠════════════════ Label ═════════════════╣
|
||||
║ end ║
|
||||
╚════════════════════════════════════════╝
|
||||
╒═ double-h box ═════════════════════════╕
|
||||
│ single rule │
|
||||
├────────────────────────────────────────┤
|
||||
│ double rule │
|
||||
╞════════════════ Label ═════════════════╡
|
||||
│ end │
|
||||
╘════════════════════════════════════════╛
|
||||
╓─ double-v box ─────────────────────────╖
|
||||
║ single rule ║
|
||||
╟────────────────────────────────────────╢
|
||||
║ double rule ║
|
||||
╠════════════════ Label ═════════════════╣
|
||||
║ end ║
|
||||
╙────────────────────────────────────────╜
|
||||
+- ascii box ----------------------------+
|
||||
| single rule |
|
||||
+----------------------------------------+
|
||||
| double rule |
|
||||
+================ Label =================+
|
||||
| end |
|
||||
+----------------------------------------+
|
||||
┌────────────┐
|
||||
│ narrow │
|
||||
├── too... ──┤
|
||||
├─── fits ───┤
|
||||
└────────────┘
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,45 @@
|
||||
┌─ single box ───────────────────────────┐
|
||||
│ single rule │
|
||||
├────────────────────────────────────────┤
|
||||
│ double rule │
|
||||
├────────────────────────────────────────┤
|
||||
│ labelled │
|
||||
├── Left ────────────────────────────────┤
|
||||
├──────────────── Centre ────────────────┤
|
||||
├─────────────────────────────── Right ──┤
|
||||
│ end │
|
||||
└────────────────────────────────────────┘
|
||||
╔═ double box ═══════════════════════════╗
|
||||
║ single rule ║
|
||||
╠════════════════════════════════════════╣
|
||||
║ double rule ║
|
||||
╠════════════════ Label ═════════════════╣
|
||||
║ end ║
|
||||
╚════════════════════════════════════════╝
|
||||
╔═ double-h box ═════════════════════════╗
|
||||
║ single rule ║
|
||||
╠════════════════════════════════════════╣
|
||||
║ double rule ║
|
||||
╠════════════════ Label ═════════════════╣
|
||||
║ end ║
|
||||
╚════════════════════════════════════════╝
|
||||
╔═ double-v box ═════════════════════════╗
|
||||
║ single rule ║
|
||||
╠════════════════════════════════════════╣
|
||||
║ double rule ║
|
||||
╠════════════════ Label ═════════════════╣
|
||||
║ end ║
|
||||
╚════════════════════════════════════════╝
|
||||
+- ascii box ----------------------------+
|
||||
| single rule |
|
||||
+----------------------------------------+
|
||||
| double rule |
|
||||
+================ Label =================+
|
||||
| end |
|
||||
+----------------------------------------+
|
||||
┌────────────┐
|
||||
│ narrow │
|
||||
├── too... ──┤
|
||||
├─── fits ───┤
|
||||
└────────────┘
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,46 @@
|
||||
{"elements": [
|
||||
{"type": "box", "title": "single box", "children": [
|
||||
{"type": "text", "text": "single rule"},
|
||||
{"type": "rule"},
|
||||
{"type": "text", "text": "double rule"},
|
||||
{"type": "rule", "style": "double"},
|
||||
{"type": "text", "text": "labelled"},
|
||||
{"type": "rule", "text": "Left", "align": "left"},
|
||||
{"type": "rule", "text": "Centre"},
|
||||
{"type": "rule", "text": "Right", "align": "right", "style": "double", "bold": true},
|
||||
{"type": "text", "text": "end"}
|
||||
]},
|
||||
{"type": "box", "style": "double", "title": "double box", "children": [
|
||||
{"type": "text", "text": "single rule"},
|
||||
{"type": "rule"},
|
||||
{"type": "text", "text": "double rule"},
|
||||
{"type": "rule", "style": "double", "text": "Label"},
|
||||
{"type": "text", "text": "end"}
|
||||
]},
|
||||
{"type": "box", "style": "double-h", "title": "double-h box", "children": [
|
||||
{"type": "text", "text": "single rule"},
|
||||
{"type": "rule"},
|
||||
{"type": "text", "text": "double rule"},
|
||||
{"type": "rule", "style": "double", "text": "Label"},
|
||||
{"type": "text", "text": "end"}
|
||||
]},
|
||||
{"type": "box", "style": "double-v", "title": "double-v box", "children": [
|
||||
{"type": "text", "text": "single rule"},
|
||||
{"type": "rule"},
|
||||
{"type": "text", "text": "double rule"},
|
||||
{"type": "rule", "style": "double", "text": "Label"},
|
||||
{"type": "text", "text": "end"}
|
||||
]},
|
||||
{"type": "box", "style": "ascii", "title": "ascii box", "children": [
|
||||
{"type": "text", "text": "single rule"},
|
||||
{"type": "rule"},
|
||||
{"type": "text", "text": "double rule"},
|
||||
{"type": "rule", "style": "double", "text": "Label"},
|
||||
{"type": "text", "text": "end"}
|
||||
]},
|
||||
{"type": "box", "columns": 14, "children": [
|
||||
{"type": "text", "text": "narrow"},
|
||||
{"type": "rule", "text": "too long a label"},
|
||||
{"type": "rule", "text": "fits"}
|
||||
]}
|
||||
]}
|
||||
@@ -0,0 +1,15 @@
|
||||
+- “Quoted” — title… --------------------+
|
||||
| ‘Single’ – dash; ellipsis… • bullet -> |
|
||||
| arrow <- back x x × € ™ done |
|
||||
+----------------------------------------+
|
||||
Emoji: ? one sequence, ? a family, ? a
|
||||
flag, 1 a keycap, ? a sign.
|
||||
Price .......................... 12 € — 3×
|
||||
Emoji ................................. ??
|
||||
---------------- “Label” -----------------
|
||||
Decomposed: café, naïve; zerowidth and
|
||||
joiner gone.
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
=== warnings
|
||||
elements[1]: 4 characters not in cp1252 printed as ?
|
||||
elements[2]: 2 characters not in cp1252 printed as ?
|
||||
@@ -0,0 +1,15 @@
|
||||
┌─ "Quoted" -- title... ─────────────────┐
|
||||
│ 'Single' - dash; ellipsis... · bullet │
|
||||
│ -> arrow <- back √ √ x EUR TM done │
|
||||
└────────────────────────────────────────┘
|
||||
Emoji: ? one sequence, ? a family, ? a
|
||||
flag, 1 a keycap, ? a sign.
|
||||
Price ....................... 12 EUR -- 3x
|
||||
Emoji ................................. ??
|
||||
──────────────── "Label" ─────────────────
|
||||
Decomposed: café, naïve; zerowidth and
|
||||
joiner gone.
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
=== warnings
|
||||
elements[1]: 4 characters not in cp437 printed as ?
|
||||
elements[2]: 2 characters not in cp437 printed as ?
|
||||
@@ -0,0 +1,15 @@
|
||||
┌─ "Quoted" -- title... ─────────────────┐
|
||||
│ 'Single' - dash; ellipsis... · bullet │
|
||||
│ -> arrow <- back x x × EUR TM done │
|
||||
└────────────────────────────────────────┘
|
||||
Emoji: ? one sequence, ? a family, ? a
|
||||
flag, 1 a keycap, ? a sign.
|
||||
Price ....................... 12 EUR -- 3×
|
||||
Emoji ................................. ??
|
||||
──────────────── "Label" ─────────────────
|
||||
Decomposed: café, naïve; zerowidth and
|
||||
joiner gone.
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
=== warnings
|
||||
elements[1]: 4 characters not in cp850 printed as ?
|
||||
elements[2]: 2 characters not in cp850 printed as ?
|
||||
@@ -0,0 +1,7 @@
|
||||
{"elements": [
|
||||
{"type": "box", "title": "“Quoted” — title…", "text": "‘Single’ – dash; ellipsis… • bullet → arrow ← back ✓ ✔ × € ™ done"},
|
||||
{"type": "text", "text": "Emoji: 👍🏽 one sequence, 👨\u200D👩\u200D👧 a family, 🇩🇪 a flag, 1\uFE0F\u20E3 a keycap, ⚠\uFE0F a sign."},
|
||||
{"type": "kv", "pairs": [{"key": "Price", "value": "12 € — 3×"}, {"key": "Emoji", "value": "🎉🎉"}]},
|
||||
{"type": "rule", "text": "“Label”"},
|
||||
{"type": "text", "text": "Decomposed: cafe\u0301, nai\u0308ve; zero\u200Bwidth and joiner\u200D gone."}
|
||||
]}
|
||||
@@ -0,0 +1,29 @@
|
||||
Host ............................... nas01
|
||||
Note ... The scrub finished with 0 errors.
|
||||
Next scrub Sunday 03:00.
|
||||
Used ................................. 94%
|
||||
Empty ....................................
|
||||
A key that is much too long to ... ..... v
|
||||
URL .. https://example.com/a/very/long/pat
|
||||
h/without/any/spaces/in/it
|
||||
Lines .................... first paragraph
|
||||
second paragraph
|
||||
··········································
|
||||
Middle dots ···························· ·
|
||||
Spaces justified
|
||||
space the same
|
||||
Underscores ____________________________ _
|
||||
Dashes --------------------------------- -
|
||||
Line ----------------------------------- ?
|
||||
Columns .... 24, centred
|
||||
Size .............. 2
|
||||
Total ........ £12.50
|
||||
Font B .......................... fifty-six columns wide
|
||||
+- kv in a box --------------------------+
|
||||
| Inside ........... the content width |
|
||||
| Wraps ..... a value that is too long |
|
||||
| for one line of the box |
|
||||
+----------------------------------------+
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
=== warnings
|
||||
elements[7]: 1 character not in cp1252 printed as ?
|
||||
@@ -0,0 +1,27 @@
|
||||
Host ............................... nas01
|
||||
Note ... The scrub finished with 0 errors.
|
||||
Next scrub Sunday 03:00.
|
||||
Used ................................. 94%
|
||||
Empty ....................................
|
||||
A key that is much too long to ... ..... v
|
||||
URL .. https://example.com/a/very/long/pat
|
||||
h/without/any/spaces/in/it
|
||||
Lines .................... first paragraph
|
||||
second paragraph
|
||||
··········································
|
||||
Middle dots ···························· ·
|
||||
Spaces justified
|
||||
space the same
|
||||
Underscores ____________________________ _
|
||||
Dashes --------------------------------- -
|
||||
Line ─────────────────────────────────── ─
|
||||
Columns .... 24, centred
|
||||
Size .............. 2
|
||||
Total ........ £12.50
|
||||
Font B .......................... fifty-six columns wide
|
||||
┌─ kv in a box ──────────────────────────┐
|
||||
│ Inside ........... the content width │
|
||||
│ Wraps ..... a value that is too long │
|
||||
│ for one line of the box │
|
||||
└────────────────────────────────────────┘
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,27 @@
|
||||
Host ............................... nas01
|
||||
Note ... The scrub finished with 0 errors.
|
||||
Next scrub Sunday 03:00.
|
||||
Used ................................. 94%
|
||||
Empty ....................................
|
||||
A key that is much too long to ... ..... v
|
||||
URL .. https://example.com/a/very/long/pat
|
||||
h/without/any/spaces/in/it
|
||||
Lines .................... first paragraph
|
||||
second paragraph
|
||||
··········································
|
||||
Middle dots ···························· ·
|
||||
Spaces justified
|
||||
space the same
|
||||
Underscores ____________________________ _
|
||||
Dashes --------------------------------- -
|
||||
Line ─────────────────────────────────── ─
|
||||
Columns .... 24, centred
|
||||
Size .............. 2
|
||||
Total ........ £12.50
|
||||
Font B .......................... fifty-six columns wide
|
||||
┌─ kv in a box ──────────────────────────┐
|
||||
│ Inside ........... the content width │
|
||||
│ Wraps ..... a value that is too long │
|
||||
│ for one line of the box │
|
||||
└────────────────────────────────────────┘
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
{"elements": [
|
||||
{"type": "kv", "pairs": [
|
||||
{"key": "Host", "value": "nas01"},
|
||||
{"key": "Note", "value": "The scrub finished with 0 errors. Next scrub Sunday 03:00."},
|
||||
{"key": "Used", "value": "94%", "bold": true},
|
||||
{"key": "Empty", "value": ""},
|
||||
{"key": "A key that is much too long to leave room for its value", "value": "v"},
|
||||
{"key": "URL", "value": "https://example.com/a/very/long/path/without/any/spaces/in/it"},
|
||||
{"key": "Lines", "value": "first paragraph\nsecond paragraph"}
|
||||
]},
|
||||
{"type": "rule", "style": "dotted"},
|
||||
{"type": "kv", "leader": "·", "pairs": [{"key": "Middle dots", "value": "·"}]},
|
||||
{"type": "kv", "leader": " ", "pairs": [{"key": "Spaces", "value": "justified"}]},
|
||||
{"type": "kv", "leader": "space", "pairs": [{"key": "space", "value": "the same"}]},
|
||||
{"type": "kv", "leader": "_", "pairs": [{"key": "Underscores", "value": "_"}]},
|
||||
{"type": "kv", "leader": "-", "pairs": [{"key": "Dashes", "value": "-"}]},
|
||||
{"type": "kv", "leader": "─", "pairs": [{"key": "Line", "value": "─"}]},
|
||||
{"type": "kv", "columns": 24, "pairs": [{"key": "Columns", "value": "24, centred"}]},
|
||||
{"type": "kv", "size": 2, "pairs": [{"key": "Size", "value": "2"}, {"key": "Total", "value": "£12.50", "bold": true}]},
|
||||
{"type": "kv", "font": "b", "pairs": [{"key": "Font B", "value": "fifty-six columns wide"}]},
|
||||
{"type": "box", "title": "kv in a box", "padding": 2, "children": [
|
||||
{"type": "kv", "pairs": [{"key": "Inside", "value": "the content width"}, {"key": "Wraps", "value": "a value that is too long for one line of the box"}]}
|
||||
]}
|
||||
]}
|
||||
@@ -0,0 +1,24 @@
|
||||
+-| ACHTUNG |----------------------------+
|
||||
| The coffee machine is being descaled. |
|
||||
| Do not use until 10:30. |
|
||||
+----------------------------------------+
|
||||
|
||||
+---------------------+
|
||||
| !!! ACHTUNG !!! |
|
||||
+---------------------+
|
||||
|
||||
+-------------------+
|
||||
| !!! ACHTUNG !!! |
|
||||
+-------------------+
|
||||
|
||||
+- NAS REPORT ---------------------------+
|
||||
| Host ........................... nas01 |
|
||||
| Pool ................... tank (raidz2) |
|
||||
| Used ............................. 94% |
|
||||
+-- Services ----------------------------+
|
||||
| · grafana up |
|
||||
| · offsite-backup DOWN since 19:00 |
|
||||
+------------------------ 29 Nov 21:04 --+
|
||||
================= TOTAL ==================
|
||||
Total: £12.50
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,24 @@
|
||||
╔═╡ ACHTUNG ╞════════════════════════════╗
|
||||
║ The coffee machine is being descaled. ║
|
||||
║ Do not use until 10:30. ║
|
||||
╚════════════════════════════════════════╝
|
||||
|
||||
┌─────────────────────┐
|
||||
│ !!! ACHTUNG !!! │
|
||||
└─────────────────────┘
|
||||
|
||||
╔═══════════════════╗
|
||||
║ !!! ACHTUNG !!! ║
|
||||
╚═══════════════════╝
|
||||
|
||||
┌─ NAS REPORT ───────────────────────────┐
|
||||
│ Host ........................... nas01 │
|
||||
│ Pool ................... tank (raidz2) │
|
||||
│ Used ............................. 94% │
|
||||
├── Services ────────────────────────────┤
|
||||
│ · grafana up │
|
||||
│ · offsite-backup DOWN since 19:00 │
|
||||
└──────────────────────── 29 Nov 21:04 ──┘
|
||||
═════════════════ TOTAL ══════════════════
|
||||
Total: £12.50
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,24 @@
|
||||
╔═╣ ACHTUNG ╠════════════════════════════╗
|
||||
║ The coffee machine is being descaled. ║
|
||||
║ Do not use until 10:30. ║
|
||||
╚════════════════════════════════════════╝
|
||||
|
||||
┌─────────────────────┐
|
||||
│ !!! ACHTUNG !!! │
|
||||
└─────────────────────┘
|
||||
|
||||
╔═══════════════════╗
|
||||
║ !!! ACHTUNG !!! ║
|
||||
╚═══════════════════╝
|
||||
|
||||
┌─ NAS REPORT ───────────────────────────┐
|
||||
│ Host ........................... nas01 │
|
||||
│ Pool ................... tank (raidz2) │
|
||||
│ Used ............................. 94% │
|
||||
├── Services ────────────────────────────┤
|
||||
│ · grafana up │
|
||||
│ · offsite-backup DOWN since 19:00 │
|
||||
└──────────────────────── 29 Nov 21:04 ──┘
|
||||
═════════════════ TOTAL ══════════════════
|
||||
Total: £12.50
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,17 @@
|
||||
{"elements": [
|
||||
{"type": "box", "style": "double", "title": "ACHTUNG", "title_style": "capped", "align": "center",
|
||||
"text": "The coffee machine is being descaled.\nDo not use until 10:30."},
|
||||
{"type": "feed"},
|
||||
{"type": "box", "fit": true, "padding": 3, "text": "!!! ACHTUNG !!!"},
|
||||
{"type": "feed"},
|
||||
{"type": "box", "style": "double", "fit": true, "padding": 2, "text": "!!! ACHTUNG !!!"},
|
||||
{"type": "feed"},
|
||||
{"type": "box", "title": "NAS REPORT", "footer": "29 Nov 21:04", "children": [
|
||||
{"type": "kv", "pairs": [{"key": "Host", "value": "nas01"}, {"key": "Pool", "value": "tank (raidz2)"}, {"key": "Used", "value": "94%", "bold": true}]},
|
||||
{"type": "rule", "text": "Services", "align": "left"},
|
||||
{"type": "text", "text": "· grafana up\n· offsite-backup DOWN since 19:00"}
|
||||
]},
|
||||
{"type": "rule", "style": "double", "text": "TOTAL"},
|
||||
{"type": "text", "text": "Total: ", "newline": false},
|
||||
{"type": "text", "text": "£12.50", "bold": true}
|
||||
]}
|
||||
@@ -0,0 +1,22 @@
|
||||
------------------------------------------
|
||||
==========================================
|
||||
##########################################
|
||||
##########################################
|
||||
##########################################
|
||||
##########################################
|
||||
- - - - - - - - - - - - - - - - - - - - -
|
||||
··········································
|
||||
------------------------------------------
|
||||
----------------- TODAY ------------------
|
||||
================= TOTAL ==================
|
||||
-- Notes ---------------------------------
|
||||
--------------------------------- Right --
|
||||
################# URGENT #################
|
||||
- dashed - - - - - - - - - - - - - - - -
|
||||
- - - - - - - - - dashed - - - - - - - - -
|
||||
································ dotted ··
|
||||
-- A label far too long to fit insid... --
|
||||
-------- 20 --------
|
||||
======== BIG ========
|
||||
------------------------ Font B ------------------------
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,22 @@
|
||||
──────────────────────────────────────────
|
||||
══════════════════════════════════════════
|
||||
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
|
||||
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
|
||||
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
|
||||
██████████████████████████████████████████
|
||||
- - - - - - - - - - - - - - - - - - - - -
|
||||
··········································
|
||||
------------------------------------------
|
||||
───────────────── TODAY ──────────────────
|
||||
═════════════════ TOTAL ══════════════════
|
||||
── Notes ─────────────────────────────────
|
||||
───────────────────────────────── Right ──
|
||||
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ URGENT ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
|
||||
- dashed - - - - - - - - - - - - - - - -
|
||||
- - - - - - - - - dashed - - - - - - - - -
|
||||
································ dotted ··
|
||||
── A label far too long to fit insid... ──
|
||||
──────── 20 ────────
|
||||
════════ BIG ════════
|
||||
──────────────────────── Font B ────────────────────────
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
@@ -0,0 +1,22 @@
|
||||
──────────────────────────────────────────
|
||||
══════════════════════════════════════════
|
||||
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
|
||||
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
|
||||
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
|
||||
██████████████████████████████████████████
|
||||
- - - - - - - - - - - - - - - - - - - - -
|
||||
··········································
|
||||
------------------------------------------
|
||||
───────────────── TODAY ──────────────────
|
||||
═════════════════ TOTAL ══════════════════
|
||||
── Notes ─────────────────────────────────
|
||||
───────────────────────────────── Right ──
|
||||
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ URGENT ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
|
||||
- dashed - - - - - - - - - - - - - - - -
|
||||
- - - - - - - - - dashed - - - - - - - - -
|
||||
································ dotted ··
|
||||
── A label far too long to fit insid... ──
|
||||
──────── 20 ────────
|
||||
════════ BIG ════════
|
||||
──────────────────────── Font B ────────────────────────
|
||||
- - - - - - - partial cut - - - - - - - -
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
{"elements": [
|
||||
{"type": "rule"},
|
||||
{"type": "rule", "style": "double"},
|
||||
{"type": "rule", "style": "light"},
|
||||
{"type": "rule", "style": "medium"},
|
||||
{"type": "rule", "style": "dark"},
|
||||
{"type": "rule", "style": "solid"},
|
||||
{"type": "rule", "style": "dashed"},
|
||||
{"type": "rule", "style": "dotted"},
|
||||
{"type": "rule", "style": "ascii"},
|
||||
{"type": "rule", "text": "TODAY"},
|
||||
{"type": "rule", "style": "double", "text": "TOTAL", "bold": true},
|
||||
{"type": "rule", "text": "Notes", "align": "left"},
|
||||
{"type": "rule", "text": "Right", "align": "right"},
|
||||
{"type": "rule", "style": "dark", "text": "URGENT"},
|
||||
{"type": "rule", "style": "dashed", "text": "dashed", "align": "left"},
|
||||
{"type": "rule", "style": "dashed", "text": "dashed"},
|
||||
{"type": "rule", "style": "dotted", "text": "dotted", "align": "right"},
|
||||
{"type": "rule", "text": "A label far too long to fit inside a rule on this paper"},
|
||||
{"type": "rule", "columns": 20, "text": "20"},
|
||||
{"type": "rule", "size": 2, "style": "double", "text": "BIG"},
|
||||
{"type": "rule", "font": "b", "text": "Font B"}
|
||||
]}
|
||||
@@ -0,0 +1,8 @@
|
||||
1b401b74101b61001b4d001d21001b45001b2d001d420093536d617274207175
|
||||
6f7465739420616e64209173696e676c6592209620656e206461736820972065
|
||||
6d0a64617368208520656c6c697073697320952062756c6c65740a1b45001b2d
|
||||
001d42001d21001b4d001b61001b4d001d21001b45001b2d001d42008020392c
|
||||
3939209920d720f720a920ae20b020b120b520bc20bd20be209c208a209e209f
|
||||
0a1b45001b2d001d42001d21001b4d001b61001b4d001d21001b45001b2d001d
|
||||
42004d697373696e6720686572653a203f203f203f203f203f203f0a1b45001b
|
||||
2d001d42001d21001b4d001d564200
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"options": {"codepage": "cp1252"},
|
||||
"job": {
|
||||
"elements": [
|
||||
{"type": "text", "text": "“Smart quotes” and ‘single’ – en dash — em dash … ellipsis • bullet"},
|
||||
{"type": "text", "text": "€ 9,99 ™ × ÷ © ® ° ± µ ¼ ½ ¾ œ Š ž Ÿ"},
|
||||
{"type": "text", "text": "Missing here: ─ │ ░ √ 🎉 Ω"}
|
||||
]
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
1b401b74001b61001b4d001d21001b45001b2d001d4200b2b2b2b2b2b2b2b2b2
|
||||
b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2
|
||||
b20a1b45001b2d001d42001d21001b4d001b61011b4d001d21001b45001b2d00
|
||||
1d4200dac4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4bf0ab320202021
|
||||
21212041434854554e4720212121202020b30ac0c4c4c4c4c4c4c4c4c4c4c4c4
|
||||
c4c4c4c4c4c4c4c4c4d90a1b45001b2d001d42001d21001b4d001b61001b4d00
|
||||
1d21001b45001b2d001d4200c9cdbb20d6c4b720d5cdb820b0b1b2db20dcdfdd
|
||||
de20fe20faf9fb20f8f1fd20e620abac0ae0e1e2e3e4e50a1b45001b2d001d42
|
||||
001d21001b4d001b61001b4d001d21001b45001b2d001d42009c31322e353020
|
||||
9a6265726272696e6765722063616682206e618b7665208020a420a8ad209d20
|
||||
9b0a1b45001b2d001d42001d21001b4d001b61001b4d001d21001b45001b2d00
|
||||
1d42004d697373696e673a203f203f203f2c2070726573656e743a20ea0a1b45
|
||||
001b2d001d42001d21001b4d001b61001b4d001d21001b45001b2d001d420043
|
||||
6f6e74726f6c732073747269707065640a62656c6c20676f6e654020616e6420
|
||||
45534320746f6f0a1b45001b2d001d42001d21001b4d001b61001b4d001d2100
|
||||
1b45001b2d001d42004e6fff627265616b2073706163650a1b45001b2d001d42
|
||||
001d21001b4d001d564200
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"job": {
|
||||
"elements": [
|
||||
{"type": "text", "text": "▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓"},
|
||||
{"type": "text", "text": "┌─────────────────────┐\n│ !!! ACHTUNG !!! │\n└─────────────────────┘", "align": "center"},
|
||||
{"type": "text", "text": "╔═╗ ╓─╖ ╒═╕ ░▒▓█ ▄▀▌▐ ■ ·∙√ °±² µ ½¼ αßΓπΣσ"},
|
||||
{"type": "text", "text": "£12.50 Überbringer café naïve Ç ñ ¿¡ ¥ ¢"},
|
||||
{"type": "text", "text": "Missing: 🎉 漢 ¤, present: Ω"},
|
||||
{"type": "text", "text": "Controls\u0007 stripped\r\nbell gone\u001b@ and ESC too\u0000"},
|
||||
{"type": "text", "text": "No\u00A0break space"}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
1b401b74021b61001b4d001d21001b45001b2d001d4200c9cdbbbac8bc20dac4
|
||||
bfb3c0d920c3b420ccb920b0b1b2db20cf20b820a9209d209b209e0a1b45001b
|
||||
2d001d42001d21001b4d001b61001b4d001d21001b45001b2d001d42009a6265
|
||||
72677294e165209c352085828ce48120b790d7e59a0a1b45001b2d001d42001d
|
||||
21001b4d001b61001b4d001d21001b45001b2d001d42004d697373696e672068
|
||||
6572653a203f203f203f203f0a1b45001b2d001d42001d21001b4d001d564200
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"options": {"codepage": "cp850"},
|
||||
"job": {
|
||||
"elements": [
|
||||
{"type": "text", "text": "╔═╗║╚╝ ┌─┐│└┘ ├┤ ╠╣ ░▒▓█ ¤ © ® Ø ø ×"},
|
||||
{"type": "text", "text": "Übergröße £5 àéîõü ÀÉÎÕÜ"},
|
||||
{"type": "text", "text": "Missing here: √ ╒ ╟ Ω"}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
1b401b74131b61001b4d001d21001b45001b2d001d420050726963653a203132
|
||||
2c353020d5202863703835382068617320746865206575726f207369676e290a
|
||||
1b45001b2d001d42001d21001b4d001b61001b4d001d21001b45001b2d001d42
|
||||
00c9cdbb20b0b1b2209c20e10a1b45001b2d001d42001d21001b4d001d564200
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"options": {"codepage": "cp858"},
|
||||
"job": {
|
||||
"elements": [
|
||||
{"type": "text", "text": "Price: 12,50 € (cp858 has the euro sign)"},
|
||||
{"type": "text", "text": "╔═╗ ░▒▓ £ ß"}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
1b401b74001b61001b4d001d21001b45001b2d001d42004265666f7265207468
|
||||
6520636f6465730a1b45001b2d001d42001d21001b4d001b64031b64011b6101
|
||||
1d286b0400314132001d286b03003143061d286b03003145311d286b16003150
|
||||
3068747470733a2f2f6578616d706c652e636f6d1d286b03003151301b61021d
|
||||
286b0400314132001d286b03003143031d286b03003145331d286b1c00315030
|
||||
536d616c6c2051522c206869676820636f7272656374696f6e1d286b03003151
|
||||
301b61001d286b0400314132001d286b03003143101d286b03003145321d286b
|
||||
1800315030515220617420746865206c656674207769746820511d286b030031
|
||||
51301b61001d68501d77031d48021d6b490a7b424142432d7b7b317d1b61011d
|
||||
683c1d77021d48031d6b430d343030363338313333333933311b61001d68501d
|
||||
77031d48001d6b4407393633383530371b61021d68501d77031d48011d6b410b
|
||||
30333630303032393134351b61001d68ff1d77061d48021d6b42083031323334
|
||||
3536351b61001d68501d77031d48021d6b450c434f44452033392d242f2b251b
|
||||
61001b4d001d21001b45001b2d001d4200696e6c696e65206265666f72652072
|
||||
6177201b45001b2d001d42001d21001b4d001b61011b61001b4d001d21001b45
|
||||
001b2d001d42006166746572207261770a1b45001b2d001d42001d21001b4d00
|
||||
1d5641001b61001b4d001d21001b45001b2d001d420041667465722061206675
|
||||
6c6c206375740a1b45001b2d001d42001d21001b4d001d5642001b61001b4d00
|
||||
1d21001b45001b2d001d42004265666f72652074686520636f6465730a1b4500
|
||||
1b2d001d42001d21001b4d001b64031b64011b61011d286b0400314132001d28
|
||||
6b03003143061d286b03003145311d286b160031503068747470733a2f2f6578
|
||||
616d706c652e636f6d1d286b03003151301b61021d286b0400314132001d286b
|
||||
03003143031d286b03003145331d286b1c00315030536d616c6c2051522c2068
|
||||
69676820636f7272656374696f6e1d286b03003151301b61001d286b04003141
|
||||
32001d286b03003143101d286b03003145321d286b1800315030515220617420
|
||||
746865206c656674207769746820511d286b03003151301b61001d68501d7703
|
||||
1d48021d6b490a7b424142432d7b7b317d1b61011d683c1d77021d48031d6b43
|
||||
0d343030363338313333333933311b61001d68501d77031d48001d6b44073936
|
||||
33383530371b61021d68501d77031d48011d6b410b3033363030303239313435
|
||||
1b61001d68ff1d77061d48021d6b420830313233343536351b61001d68501d77
|
||||
031d48021d6b450c434f44452033392d242f2b251b61001b4d001d21001b4500
|
||||
1b2d001d4200696e6c696e65206265666f726520726177201b45001b2d001d42
|
||||
001d21001b4d001b61011b61001b4d001d21001b45001b2d001d420061667465
|
||||
72207261770a1b45001b2d001d42001d21001b4d001d5641001b61001b4d001d
|
||||
21001b45001b2d001d4200416674657220612066756c6c206375740a1b45001b
|
||||
2d001d42001d21001b4d001d564200
|
||||
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"job": {
|
||||
"copies": 2,
|
||||
"elements": [
|
||||
{"type": "text", "text": "Before the codes"},
|
||||
{"type": "feed", "lines": 3},
|
||||
{"type": "feed"},
|
||||
{"type": "qr", "data": "https://example.com", "align": "center"},
|
||||
{"type": "qr", "data": "Small QR, high correction", "size": 3, "correction": "H", "align": "right"},
|
||||
{"type": "qr", "data": "QR at the left with Q", "size": 16, "correction": "q"},
|
||||
{"type": "barcode", "symbology": "code128", "data": "ABC-{1}"},
|
||||
{"type": "barcode", "symbology": "ean13", "data": "4006381333931", "height": 60, "width": 2, "hri": "both", "align": "center"},
|
||||
{"type": "barcode", "symbology": "ean8", "data": "9638507", "hri": "none"},
|
||||
{"type": "barcode", "symbology": "upca", "data": "03600029145", "hri": "above", "align": "right"},
|
||||
{"type": "barcode", "symbology": "upce", "data": "01234565", "width": 6, "height": 255},
|
||||
{"type": "barcode", "symbology": "code39", "data": "CODE 39-$/+%"},
|
||||
{"type": "text", "text": "inline before raw ", "newline": false},
|
||||
{"type": "raw", "hex": "1B 61 01"},
|
||||
{"type": "text", "text": "after raw"},
|
||||
{"type": "cut", "mode": "full"},
|
||||
{"type": "text", "text": "After a full cut"},
|
||||
{"type": "cut"}
|
||||
]
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
1b401b74001b61001d76300040000600ff548000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000000000eaa20f00000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000000000b4907f00000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
000000000000000000000000000000009203f500000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000000000403faa00000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000011fd5400000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
000000000000000000000000000000001b61001d76300040000c000000000000
|
||||
000000000000000000000000000000000000000000000000fffedaa900000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
000000000000000000000000000000000000000000000000ff6b6aa4a9200000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
000000000000000000000000000000000000000000000000fbb5aa4800ff0000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
000000000000000000000000000000000000000000000000dd5a490221ff0000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
000000000000000000000000000000000000000000000000eb6aa4503eed0000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
000000000000000000000000000000000000000000000000554910007ff70000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
000000000000000000000000000000000000000000000000b524400fdf5a0000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
000000000000000000000000000000000000000000000000aa910a9ff5aa0000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000000000000000000000000044440fef6ed50000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000000000000000000000000029009fbbb5540000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000008007edfd552a0000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000002403ff55aa900000
|
||||
0000000000000000000000000000000000000000000000000000001b61001b4d
|
||||
001d21001b45001b2d001d42006265747765656e1b45001b2d001d42001d2100
|
||||
1b4d000a1b61001d763000400003000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
000000000000000000000000001e820000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000105f0000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000005f41d564200
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"job": {
|
||||
"elements": [
|
||||
{"type": "image", "base64": "iVBORw0KGgoAAAANSUhEUgAAABgAAAAGCAAAAAD8wAHPAAAAJElEQVR4nGJh4MYOWFRhLGIkVLFKqGLVgeCxqGIR5Obm5gYMAArvByihTpiMAAAAAElFTkSuQmCC"},
|
||||
{"type": "image", "base64": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAGCAAAAAD8wAHPAAAAJElEQVR4nGJh4MYOWFRhLGIkVLFKqGLVgeCxqGIR5Obm5gYMAArvByihTpiMAAAAAElFTkSuQmCC", "align": "center", "width": 48},
|
||||
{"type": "text", "text": "between", "newline": false},
|
||||
{"type": "image", "base64": "iVBORw0KGgoAAAANSUhEUgAAABgAAAAGCAAAAAD8wAHPAAAAJElEQVR4nGJh4MYOWFRhLGIkVLFKqGLVgeCxqGIR5Obm5gYMAArvByihTpiMAAAAAElFTkSuQmCC", "align": "right", "width": 13}
|
||||
]
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
1b401b74001b61001b4d001d21001b45001b2d001d4200546f74616c3a201b45
|
||||
001b2d001d42001d21001b4d001b61001b4d001d21001b45011b2d001d420031
|
||||
322e35300a1b45001b2d001d42001d21001b4d001b61001b4d001d21001b4500
|
||||
1b2d001d42007878787878787878787878787878787878787878787878787878
|
||||
78787878787878787878787878781b45001b2d001d42001d21001b4d001b6100
|
||||
1b4d001d21111b45001b2d001d42000a4249474f4e450a1b45001b2d001d4200
|
||||
1d21001b4d001b61001b4d001d21001b45001b2d001d42004d69786564201b45
|
||||
001b2d001d42001d21001b4d001b61001b4d001d21111b45001b2d001d420053
|
||||
495a4553201b45001b2d001d42001d21001b4d001b61001b4d001d21001b4500
|
||||
1b2d001d42006f6e206f6e65206c696e652074686174206b656570730a676f69
|
||||
6e6720756e74696c2069742068617320746f20777261701b45001b2d001d4200
|
||||
1d21001b4d001b61001b4d001d21001b45001b2d001d420020616e6420656e64
|
||||
7320686572652e0a1b45001b2d001d42001d21001b4d001b61001b4d001d2100
|
||||
1b45001b2d001d42004974656d20202020517479202020202050726963650a1b
|
||||
45001b2d001d42001d21001b4d001b61001b4d001d21001b45001b2d001d4200
|
||||
54656120202020203220202020202020332e30300a1b45001b2d001d42001d21
|
||||
001b4d001b61001b4d001d21001b45001b2d001d42006162631b45001b2d001d
|
||||
42001d21001b4d001b61001b4d001d21001b45001b2d001d4200202020202061
|
||||
667465722061207461620a20202020202020206e657874206c696e650a1b4500
|
||||
1b2d001d42001d21001b4d001b61001b4d001d21001b45001b2d001d42006e6f
|
||||
206e65776c696e652061742074686520656e641b45001b2d001d42001d21001b
|
||||
4d000a1d564200
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"job": {
|
||||
"elements": [
|
||||
{"type": "text", "text": "Total: ", "newline": false},
|
||||
{"type": "text", "text": "12.50", "bold": true},
|
||||
{"type": "text", "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "newline": false},
|
||||
{"type": "text", "text": "BIGONE", "size": 2},
|
||||
{"type": "text", "text": "Mixed ", "newline": false},
|
||||
{"type": "text", "text": "SIZES ", "size": 2, "newline": false},
|
||||
{"type": "text", "text": "on one line that keeps going until it has to wrap", "newline": false},
|
||||
{"type": "text", "text": " and ends here."},
|
||||
{"type": "text", "text": "Item\tQty\tPrice"},
|
||||
{"type": "text", "text": "Tea\t2\t3.00"},
|
||||
{"type": "text", "text": "abc", "newline": false},
|
||||
{"type": "text", "text": "\tafter a tab\n\tnext line"},
|
||||
{"type": "text", "text": "no newline at the end", "newline": false}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
1b401b74001b61001b4d001d21001b45001b2d001d420054686972747920636f
|
||||
6c756d6e73206f6620466f6e7420412066726f6d0a74686520636f6c756d6e73
|
||||
206f766572726964652c206f6e20612035380a6d6d2070726f66696c652e0a1b
|
||||
45001b2d001d42001d21001b4d001b61001b4d011d21001b45001b2d001d4200
|
||||
466f6e7420422075736573207468652073616d65206c696e6520776964746820
|
||||
696e20646f74732c0a736f206974206669747320666f72747920636861726163
|
||||
7465727320686572652e0a1b45001b2d001d42001d21001b4d001b61011b4d00
|
||||
1d21111b45001b2d001d420054696e790a1b45001b2d001d42001d21001b4d00
|
||||
1d564200
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"options": {"model": "generic-58mm", "columns": 30},
|
||||
"job": {
|
||||
"elements": [
|
||||
{"type": "text", "text": "Thirty columns of Font A from the columns override, on a 58 mm profile."},
|
||||
{"type": "text", "text": "Font B uses the same line width in dots, so it fits forty characters here.", "font": "b"},
|
||||
{"type": "text", "text": "Tiny", "size": 2, "align": "center"}
|
||||
]
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
1b401b74001b61001b4d001d21001b45001b2d001d4200797979797979797979
|
||||
7979797979797979797979797979797979797979797979797979797979797979
|
||||
797979797979797979797979797979797979790a1b45001b2d001d42001d2100
|
||||
1b4d001b61001b4d001d21001b45001b2d001d4200556e777261707065640a77
|
||||
6974682061206e65776c696e6509616e642061207461620a1b45001b2d001d42
|
||||
001d21001b4d001b61001b4d001d21001b45001b2d001d4200696e6c696e6520
|
||||
756e77726170706564201b45001b2d001d42001d21001b4d001b61001b4d001d
|
||||
21001b45001b2d001d42007468656e2077726170706564207465787420746861
|
||||
740a636f6e74696e756573206f6e207468652073616d65206c696e6520616e64
|
||||
2077726170730a1b45001b2d001d42001d21001b4d001b61001b4d001d21001b
|
||||
45001b2d001d4200b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2
|
||||
b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b20a1b45001b2d001d42001d21001b
|
||||
4d001d564200
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"job": {
|
||||
"elements": [
|
||||
{"type": "text", "text": "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy", "wrap": false},
|
||||
{"type": "text", "text": "Unwrapped\nwith a newline\tand a tab", "wrap": false},
|
||||
{"type": "text", "text": "inline unwrapped ", "wrap": false, "newline": false},
|
||||
{"type": "text", "text": "then wrapped text that continues on the same line and wraps"},
|
||||
{"type": "text", "text": "▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓", "wrap": false}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
1b401b74001b61001b4d001d21001b45001b2d001d4200506c61696e20746578
|
||||
740a1b45001b2d001d42001d21001b4d001b61001b4d001d21001b45011b2d00
|
||||
1d4200426f6c640a1b45001b2d001d42001d21001b4d001b61001b4d001d2100
|
||||
1b45001b2d011d42005468696e20756e6465726c696e650a1b45001b2d001d42
|
||||
001d21001b4d001b61001b4d001d21001b45001b2d021d4200546869636b2075
|
||||
6e6465726c696e650a1b45001b2d001d42001d21001b4d001b61001b4d001d21
|
||||
001b45001b2d001d4201496e7665727465640a1b45001b2d001d42001d21001b
|
||||
4d001b61001b4d011d21001b45001b2d001d4200466f6e742042206973206e61
|
||||
72726f7765720a1b45001b2d001d42001d21001b4d001b61011b4d001d21001b
|
||||
45001b2d001d420043656e747265640a1b45001b2d001d42001d21001b4d001b
|
||||
61021b4d001d21001b45001b2d001d420052696768740a1b45001b2d001d4200
|
||||
1d21001b4d001b61001b4d001d21111b45001b2d001d420053697a6520320a1b
|
||||
45001b2d001d42001d21001b4d001b61011b4d001d21221b45011b2d001d4200
|
||||
53697a65203320626f6c640a63656e747265640a1b45001b2d001d42001d2100
|
||||
1b4d001b61001b4d001d21101b45001b2d001d4200576964650a1b45001b2d00
|
||||
1d42001d21001b4d001b61001b4d001d21021b45001b2d001d420054616c6c0a
|
||||
1b45001b2d001d42001d21001b4d001b61001b4d001d21771b45001b2d001d42
|
||||
0045696768740a1b45001b2d001d42001d21001b4d001b61021b4d011d21101b
|
||||
45011b2d021d420145766572797468696e670a1b45001b2d001d42001d21001b
|
||||
4d001d564200
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"job": {
|
||||
"elements": [
|
||||
{"type": "text", "text": "Plain text"},
|
||||
{"type": "text", "text": "Bold", "bold": true},
|
||||
{"type": "text", "text": "Thin underline", "underline": 1},
|
||||
{"type": "text", "text": "Thick underline", "underline": 2},
|
||||
{"type": "text", "text": "Inverted", "invert": true},
|
||||
{"type": "text", "text": "Font B is narrower", "font": "b"},
|
||||
{"type": "text", "text": "Centred", "align": "center"},
|
||||
{"type": "text", "text": "Right", "align": "right"},
|
||||
{"type": "text", "text": "Size 2", "size": 2},
|
||||
{"type": "text", "text": "Size 3 bold centred", "size": 3, "bold": true, "align": "centre"},
|
||||
{"type": "text", "text": "Wide", "width": 2},
|
||||
{"type": "text", "text": "Tall", "height": 3},
|
||||
{"type": "text", "text": "Eight", "size": 8},
|
||||
{"type": "text", "text": "Everything", "bold": true, "underline": 2, "invert": true, "font": "B", "align": "RIGHT", "width": 2, "height": 1}
|
||||
]
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
1b401b74001b61001b4d001d21001b45001b2d001d420054686520544d2d5432
|
||||
302070726f66696c65206861732035373620646f74732c20736f20466f6e7420
|
||||
412077726170730a617420666f7274792d656967687420636f6c756d6e732069
|
||||
6e7374656164206f6620666f7274792d74776f2e0a1b45001b2d001d42001d21
|
||||
001b4d001b61001b4d011d21001b45001b2d001d4200466f6e74204220676574
|
||||
732073697874792d666f757220636f6c756d6e73206f6e20746861742070726f
|
||||
66696c652c207768696368206d616b657320746869730a73656e74656e636520
|
||||
77726170206d756368206c617465722e0a1b45001b2d001d42001d21001b4d00
|
||||
1b61001b4d001d21111b45001b2d001d420053697a652074776f3a207477656e
|
||||
74792d666f75720a636f6c756d6e7320706572206c696e6520686572652e0a1b
|
||||
45001b2d001d42001d21001b4d001d564200
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"options": {"model": "tm-t20"},
|
||||
"job": {
|
||||
"elements": [
|
||||
{"type": "text", "text": "The TM-T20 profile has 576 dots, so Font A wraps at forty-eight columns instead of forty-two."},
|
||||
{"type": "text", "text": "Font B gets sixty-four columns on that profile, which makes this sentence wrap much later.", "font": "b"},
|
||||
{"type": "text", "text": "Size two: twenty-four columns per line here.", "size": 2}
|
||||
]
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
1b401b74001b61001b4d001d21001b45001b2d001d42004c6f6e672070617261
|
||||
677261706873207772617020617420776f726420626f756e6461726965730a66
|
||||
6f72207468652070617065722077696474682c20736f206120776f7264206973
|
||||
206e657665720a73706c6974206163726f73732074776f206c696e65732e2054
|
||||
686579206b656570207772617070696e670a666f72206173206c6f6e67206173
|
||||
207468657265206973207465787420746f207072696e742e0a1b45001b2d001d
|
||||
42001d21001b4d001b61001b4d001d21111b45001b2d001d420053697a652074
|
||||
776f2077726170732061740a68616c662074686520636f6c756d6e73206f660a
|
||||
73697a65206f6e652c2077686963682069730a7477656e74792d6f6e65206865
|
||||
72652e0a1b45001b2d001d42001d21001b4d001b61001b4d001d21221b45001b
|
||||
2d001d420053697a652074687265650a77726170732061740a666f7572746565
|
||||
6e0a636f6c756d6e732e0a1b45001b2d001d42001d21001b4d001b61001b4d01
|
||||
1d21001b45001b2d001d4200466f6e74204220676574732066696674792d7369
|
||||
7820636f6c756d6e73206f6e206120544d2d5438382c20736f20746869730a6c
|
||||
6f6e676572206c696e65206f662074657874207772617073206c617465722074
|
||||
68616e20466f6e74204120776f756c642e0a1b45001b2d001d42001d21001b4d
|
||||
001b61001b4d001d21001b45001b2d001d420046697273742070617261677261
|
||||
70682e0a5365636f6e64207061726167726170682061667465722061206e6577
|
||||
6c696e652e0a0a20202020496e64656e74656420706172616772617068206166
|
||||
746572206120626c616e6b206c696e650a6b656570732069747320696e64656e
|
||||
746174696f6e207768656e206974207772617073206f6e746f0a6d6f7265206c
|
||||
696e65732e0a1b45001b2d001d42001d21001b4d001b61001b4d001d21001b45
|
||||
001b2d001d42007365650a68747470733a2f2f6578616d706c652e636f6d2f61
|
||||
2f766572792f6c6f6e672f706174682f746861742f0a69732f6c6f6e6765722f
|
||||
7468616e2f6f6e652f77686f6c652f6c696e652f6f662f7468652f7265636569
|
||||
0a7074206f6b0a1b45001b2d001d42001d21001b4d001b61001b4d001d21001b
|
||||
45001b2d001d4200616161612020202062626262202020747261696c696e6720
|
||||
737061636573206174206120627265616b0a6172652064726f70706564202020
|
||||
2020202020202020202020202020202020656e642020200a1b45001b2d001d42
|
||||
001d21001b4d001b61011b4d001d21001b45001b2d001d420043656e74726564
|
||||
207465787420616c736f20777261707320617420776f726420626f756e646172
|
||||
6965730a616e642065616368206c696e652069732063656e7472656420627920
|
||||
746865207072696e7465722e0a1b45001b2d001d42001d21001b4d001d564200
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"job": {
|
||||
"elements": [
|
||||
{"type": "text", "text": "Long paragraphs wrap at word boundaries for the paper width, so a word is never split across two lines. They keep wrapping for as long as there is text to print."},
|
||||
{"type": "text", "text": "Size two wraps at half the columns of size one, which is twenty-one here.", "size": 2},
|
||||
{"type": "text", "text": "Size three wraps at fourteen columns.", "size": 3},
|
||||
{"type": "text", "text": "Font B gets fifty-six columns on a TM-T88, so this longer line of text wraps later than Font A would.", "font": "b"},
|
||||
{"type": "text", "text": "First paragraph.\nSecond paragraph after a newline.\n\n Indented paragraph after a blank line keeps its indentation when it wraps onto more lines."},
|
||||
{"type": "text", "text": "see https://example.com/a/very/long/path/that/is/longer/than/one/whole/line/of/the/receipt ok"},
|
||||
{"type": "text", "text": "aaaa bbbb trailing spaces at a break are dropped end "},
|
||||
{"type": "text", "text": "Centred text also wraps at word boundaries and each line is centred by the printer.", "align": "center"}
|
||||
]
|
||||
}
|
||||
}
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
1b401b74001b61011b4d001d21111b45011b2d001d4200426c6f636b2063616c
|
||||
6962726174696f6e0a1b45001b2d001d42001d21001b4d001b61011b4d011d21
|
||||
001b45001b2d001d42004c6f6f6b206174207468652067617073206265747765
|
||||
656e20746865206c696e6573206f662074686520766572746963616c0a626f72
|
||||
646572732c20616e642061742074686520636f726e65727320616e64206a756e
|
||||
6374696f6e732e0a1b45001b2d001d42001d21001b4d001b64011b61001b4d00
|
||||
1d21001b45001b2d001d4200c4c42072756c657320c4c4c4c4c4c4c4c4c4c4c4
|
||||
c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c40a1b45001b2d001d4200
|
||||
1d21001b4d001b61001b4d001d21001b45001b2d001d4200c4c4c4c4c4c4c4c4
|
||||
c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4
|
||||
c4c40a1b45001b2d001d42001d21001b4d001b61001b4d001d21001b45001b2d
|
||||
001d4200cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd
|
||||
cdcdcdcdcdcdcdcdcdcdcdcdcdcd0a1b45001b2d001d42001d21001b4d001b61
|
||||
001b4d001d21001b45001b2d001d4200b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0
|
||||
b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b00a1b45001b2d
|
||||
001d42001d21001b4d001b61001b4d001d21001b45001b2d001d4200b1b1b1b1
|
||||
b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1
|
||||
b1b1b1b1b1b10a1b45001b2d001d42001d21001b4d001b61001b4d001d21001b
|
||||
45001b2d001d4200b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2
|
||||
b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b20a1b45001b2d001d42001d21001b
|
||||
4d001b61001b4d001d21001b45001b2d001d4200dbdbdbdbdbdbdbdbdbdbdbdb
|
||||
dbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdb0a1b
|
||||
45001b2d001d42001d21001b4d001b61001b4d001d21001b45001b2d001d4200
|
||||
2d202d202d202d202d202d202d202d202d202d202d202d202d202d202d202d20
|
||||
2d202d202d202d202d200a1b45001b2d001d42001d21001b4d001b61001b4d00
|
||||
1d21001b45001b2d001d4200fafafafafafafafafafafafafafafafafafafafa
|
||||
fafafafafafafafafafafafafafafafafafafafafafa0a1b45001b2d001d4200
|
||||
1d21001b4d001b61001b4d001d21001b45001b2d001d42002d2d2d2d2d2d2d2d
|
||||
2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d
|
||||
2d2d0a1b45001b2d001d42001d21001b4d001b61001b4d001d21001b45001b2d
|
||||
001d4200cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd201b4501544f54414c1b45
|
||||
0020cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd0a1b45001b2d001d42001d21
|
||||
001b4d001b64011b61001b4d001d21001b45001b2d001d4200dac42073696e67
|
||||
6c6520c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4
|
||||
c4c4bf0ab320496e6c696e65207469746c652c2070616464696e6720312e2020
|
||||
20202020202020202020202020b30ac3c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4
|
||||
c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4b40ab32041207369
|
||||
6e676c6520646976696465722061626f76652c206120646f75626c65206f6e65
|
||||
202020b30ab32062656c6f772e20202020202020202020202020202020202020
|
||||
2020202020202020202020202020b30ac6cdcd20646f75626c65206469766964
|
||||
657220cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdb50ab320486f73
|
||||
74202e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e206e61
|
||||
73303120b30ab3201b4501557365641b4500202e2e2e2e2e2e2e2e2e2e2e2e2e
|
||||
2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e201b45013934251b450020b30ac0c4c4
|
||||
c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4
|
||||
c4c4c4c4c4c4d90a1b45001b2d001d42001d21001b4d001b61001b4d001d2100
|
||||
1b45001b2d001d4200c9cdb52041434854554e4720c6cdcdcdcdcdcdcdcdcdcd
|
||||
cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdbb0aba2054686520636f66666565
|
||||
206d616368696e65206973206265696e672064657363616c65642e2020ba0aba
|
||||
2020202020202020446f206e6f742075736520756e74696c2031303a33302e20
|
||||
2020202020202020ba0ac8cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd
|
||||
cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdbc0a1b45001b2d001d42001d21
|
||||
001b4d001b61001b4d001d21001b45001b2d001d4200c9cd20646f75626c6520
|
||||
cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdbb
|
||||
0aba2053696e676c6520646976696465723a2020202020202020202020202020
|
||||
20202020202020202020ba0ac7c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4
|
||||
c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4b60aba20446f75626c6520
|
||||
646976696465723a202020202020202020202020202020202020202020202020
|
||||
ba0acccdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd
|
||||
cdcdcdcdcdcdcdcdcdcdcdb90aba202020202020202020202020202020202020
|
||||
2020202020202020466f6f7465722062656c6f772e20ba0ac8cdcdcdcdcdcdcd
|
||||
cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd203239204e6f762032313a303420cd
|
||||
cdbc0a1b45001b2d001d42001d21001b4d001b61001b4d001d21001b45001b2d
|
||||
001d4200d5cdb520646f75626c652d6820c6cdcdcdcdcdcdcdcdcdcdcdcdcdcd
|
||||
cdcdcdcdcdcdcdcdcdcdcdcdcdb80ab320446f75626c6520686f72697a6f6e74
|
||||
616c2c2073696e676c6520766572746963616c2020202020b30ab3206c696e65
|
||||
732e202020202020202020202020202020202020202020202020202020202020
|
||||
202020b30ac6cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd
|
||||
cdcdcdcdcdcdcdcdcdcdcdcdcdcdb50ab3202020202020202020202020202020
|
||||
202020456e642e202020202020202020202020202020202020b30ad4cdcdcdcd
|
||||
cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd
|
||||
cdcdcdcdbe0a1b45001b2d001d42001d21001b4d001b61001b4d001d21001b45
|
||||
001b2d001d4200d6c420646f75626c652d7620c4c4c4c4c4c4c4c4c4c4c4c4c4
|
||||
c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4b70aba2053696e676c6520686f72697a
|
||||
6f6e74616c2c20646f75626c6520766572746963616c2020202020ba0aba206c
|
||||
696e65732e202020202020202020202020202020202020202020202020202020
|
||||
202020202020ba0ac7c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4
|
||||
c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4b60aba202020202020202020202020
|
||||
202020202020456e642e202020202020202020202020202020202020ba0ad3c4
|
||||
c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4
|
||||
c4c4c4c4c4c4c4bd0a1b45001b2d001d42001d21001b4d001b61001b4d001d21
|
||||
001b45001b2d001d42002b2d206173636969202d2d2d2d2d2d2d2d2d2d2d2d2d
|
||||
2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2b0a7c20506c61696e20415343
|
||||
494920666f7220636f646520706167657320776974686f757420626f78207c0a
|
||||
7c2064726177696e672e20202020202020202020202020202020202020202020
|
||||
2020202020202020207c0a2b2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d
|
||||
2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2b0a1b45001b2d001d42001d
|
||||
21001b4d001b64011b61001b4d001d21001b45001b2d001d4200202020202020
|
||||
202020202020202020dac4c4c4c4c4c4c4c4c4bf0a2020202020202020202020
|
||||
20202020b370616464696e672030b30a202020202020202020202020202020c0
|
||||
c4c4c4c4c4c4c4c4c4d90a1b45001b2d001d42001d21001b4d001b61001b4d00
|
||||
1d21001b45001b2d001d42002020202020202020202020202020dac4c4c4c4c4
|
||||
c4c4c4c4c4c4bf0a2020202020202020202020202020b32070616464696e6720
|
||||
3120b30a2020202020202020202020202020c0c4c4c4c4c4c4c4c4c4c4c4d90a
|
||||
1b45001b2d001d42001d21001b4d001b61001b4d001d21001b45001b2d001d42
|
||||
0020202020202020202020202020c9cdcdcdcdcdcdcdcdcdcdcdcdcdbb0a2020
|
||||
2020202020202020202020ba202070616464696e6720322020ba0a2020202020
|
||||
2020202020202020c8cdcdcdcdcdcdcdcdcdcdcdcdcdbc0a1b45001b2d001d42
|
||||
001d21001b4d001b61001b4d001d21001b45001b2d001d420020202020202020
|
||||
2020dac4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4bf0a202020202020
|
||||
202020b32020202121212041434854554e4720212121202020b30a2020202020
|
||||
20202020c0c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4d90a1b45001b
|
||||
2d001d42001d21001b4d001b64011b61001b4d001d21111b45001b2d001d4200
|
||||
dac42073697a65203220c4c4c4c4c4c4c4c4c4c4bf0ab3205477656e74792d6f
|
||||
6e652020202020202020b30ab320636f6c756d6e732e20202020202020202020
|
||||
b30ac0c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4d90a1b45001b2d001d42
|
||||
001d21001b4d001b61001b4d001d21221b45001b2d001d42002020c9cdcdcdcd
|
||||
cdcdcdcdbb0a2020ba2073697a65203320ba0a2020c8cdcdcdcdcdcdcdcdbc0a
|
||||
1b45001b2d001d42001d21001b4d001b61001b4d011d21001b45001b2d001d42
|
||||
00dac420666f6e74206220c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4
|
||||
c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4bf0ab320466f6e74
|
||||
2042206973206e6172726f77657220616e642073686f727465722c20736f2069
|
||||
747320626f726465727320736974202020b30ab320636c6f73657220746f6765
|
||||
746865722e202020202020202020202020202020202020202020202020202020
|
||||
20202020202020202020b30ac0c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4
|
||||
c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c420353620636f6c756d6e73
|
||||
20c4c4d90a1b45001b2d001d42001d21001b4d001b61001b4d011d21111b4500
|
||||
1b2d001d42002020202020c9cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdbb0a2020
|
||||
202020ba20666f6e7420622c2073697a65203220ba0a2020202020c8cdcdcdcd
|
||||
cdcdcdcdcdcdcdcdcdcdcdcdbc0a1b45001b2d001d42001d21001b4d001d5642
|
||||
00
|
||||
@@ -0,0 +1,132 @@
|
||||
package receipt_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"uberbringer/internal/pkg/receipt"
|
||||
)
|
||||
|
||||
func problems(t *testing.T, job receipt.Job) []string {
|
||||
t.Helper()
|
||||
err := job.Validate()
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
var ve *receipt.ValidationError
|
||||
if !errors.As(err, &ve) {
|
||||
t.Fatalf("err = %v, want *ValidationError", err)
|
||||
}
|
||||
return ve.Problems
|
||||
}
|
||||
|
||||
func TestValidateBlocks(t *testing.T) {
|
||||
text := receipt.Element{Type: "text", Text: "x"}
|
||||
box := func(children ...receipt.Element) receipt.Element {
|
||||
return receipt.Element{Type: "box", Children: children}
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
el receipt.Element
|
||||
want []string // the complete list of problems
|
||||
}{
|
||||
{"qr inside a box", box(text, text, text, receipt.Element{Type: "qr", Data: "x"}),
|
||||
[]string{"elements[0].children[3] (qr): not allowed inside a box; place it after the box"}},
|
||||
{"every type that cannot go in a box", box(
|
||||
receipt.Element{Type: "box", Text: "x"}, receipt.Element{Type: "image", URL: "https://x/y"},
|
||||
receipt.Element{Type: "raw", Hex: "1B"}, receipt.Element{Type: "cut"},
|
||||
receipt.Element{Type: "barcode", Symbology: "ean8", Data: "9638507"}),
|
||||
[]string{
|
||||
"elements[0].children[0] (box): not allowed inside a box; place it after the box",
|
||||
"elements[0].children[1] (image): not allowed inside a box; place it after the box",
|
||||
"elements[0].children[2] (raw): not allowed inside a box; place it after the box",
|
||||
"elements[0].children[3] (cut): not allowed inside a box; place it after the box",
|
||||
"elements[0].children[4] (barcode): not allowed inside a box; place it after the box",
|
||||
}},
|
||||
{"unknown child type", box(receipt.Element{Type: "list"}),
|
||||
[]string{`elements[0].children[0] (list): unknown type "list" (inside a box, want text, feed, rule or kv)`}},
|
||||
{"child without a type", box(receipt.Element{}),
|
||||
[]string{"elements[0].children[0] (): type is required"}},
|
||||
{"divider style", box(text, receipt.Element{Type: "rule", Style: "dark"}),
|
||||
[]string{"elements[0].children[1] (rule): inside a box, style must be single or double"}},
|
||||
{"text and children", receipt.Element{Type: "box", Text: "x", Children: []receipt.Element{text}},
|
||||
[]string{"elements[0] (box): set text or children, not both"}},
|
||||
{"no body", receipt.Element{Type: "box", Title: "x"},
|
||||
[]string{"elements[0] (box): text or children is required"}},
|
||||
{"pair without a key", receipt.Element{Type: "kv", Pairs: []receipt.Pair{{Key: "a"}, {Key: "b"}, {Value: "c"}}},
|
||||
[]string{"elements[0] (kv): pairs[2]: key is required"}},
|
||||
{"field that does not apply to a block", receipt.Element{Type: "rule", Pairs: []receipt.Pair{{Key: "a"}}, Data: "d"},
|
||||
[]string{`elements[0] (rule): field "data" does not apply to rule`, `elements[0] (rule): field "pairs" does not apply to rule`}},
|
||||
{"block field on an old type", receipt.Element{Type: "text", Text: "x", Title: "t", Children: []receipt.Element{text}},
|
||||
[]string{`elements[0] (text): field "children" does not apply to text`, `elements[0] (text): field "title" does not apply to text`}},
|
||||
{"inherited fields", box(receipt.Element{Type: "text", Text: "x", Size: 2, Font: "b"}),
|
||||
[]string{"elements[0].children[0] (text): size is inherited from the box", "elements[0].children[0] (text): font is inherited from the box"}},
|
||||
{"newline false inside a box", box(receipt.Element{Type: "text", Text: "x", Newline: boolPtr(false)}),
|
||||
[]string{"elements[0].children[0] (text): newline:false is not allowed inside a box"}},
|
||||
{"columns on a child", box(receipt.Element{Type: "kv", Pairs: []receipt.Pair{{Key: "a"}}, Columns: 10}),
|
||||
[]string{`elements[0].children[0] (kv): field "columns" does not apply to kv inside a box`}},
|
||||
{"child problems and box problems together", receipt.Element{Type: "box", Padding: intPtr(4), Children: []receipt.Element{{Type: "feed", Lines: 300}}},
|
||||
[]string{"elements[0] (box): padding must be 0-3", "elements[0].children[0] (feed): lines must be 1-255"}},
|
||||
{"box size", receipt.Element{Type: "box", Text: "x", Size: 4}, []string{"elements[0] (box): size must be 1-3"}},
|
||||
{"box style", receipt.Element{Type: "box", Text: "x", Style: "triple"},
|
||||
[]string{"elements[0] (box): style must be single, double, double-h, double-v or ascii"}},
|
||||
{"title style", receipt.Element{Type: "box", Text: "x", TitleStyle: "boxed"}, []string{"elements[0] (box): title_style must be inline or capped"}},
|
||||
{"box columns", receipt.Element{Type: "box", Text: "x", Columns: -1}, []string{"elements[0] (box): columns must be 1-255"}},
|
||||
{"box underline", receipt.Element{Type: "box", Text: "x", Underline: 3}, []string{"elements[0] (box): underline must be 0, 1 or 2"}},
|
||||
{"box font", receipt.Element{Type: "box", Text: "x", Font: "c"}, []string{"elements[0] (box): font must be a or b"}},
|
||||
{"rule style", receipt.Element{Type: "rule", Style: "wavy"},
|
||||
[]string{"elements[0] (rule): style must be single, double, light, medium, dark, solid, dashed, dotted or ascii"}},
|
||||
{"rule align", receipt.Element{Type: "rule", Align: "middle"}, []string{"elements[0] (rule): align must be left, center or right"}},
|
||||
{"leader", receipt.Element{Type: "kv", Leader: "*", Pairs: []receipt.Pair{{Key: "a"}}},
|
||||
[]string{`elements[0] (kv): leader must be ".", "·", " ", "_", "-" or "─"`}},
|
||||
{"no pairs", receipt.Element{Type: "kv"}, []string{"elements[0] (kv): pairs: at least one pair is required"}},
|
||||
{"too many pairs", receipt.Element{Type: "kv", Pairs: manyPairs(101)}, []string{"elements[0] (kv): pairs: 101 pairs, limit is 100"}},
|
||||
{"kv size", receipt.Element{Type: "kv", Size: 9, Pairs: []receipt.Pair{{Key: "a"}}}, []string{"elements[0] (kv): size must be 1-3"}},
|
||||
|
||||
{"old types stay lenient", receipt.Element{Type: "feed", Text: "ignored", Bold: true}, nil},
|
||||
{"a full box", receipt.Element{
|
||||
Type: "box", Style: "double-v", Title: "t", TitleStyle: "capped", Footer: "f", Padding: intPtr(0),
|
||||
Fit: true, Columns: 30, Size: 2, Font: "b", Align: "right", Bold: true, Underline: 2, Invert: true, Children: []receipt.Element{
|
||||
{Type: "text", Text: "x", Bold: true, Underline: 1, Invert: true, Align: "center", Wrap: boolPtr(false), Newline: boolPtr(true)},
|
||||
{Type: "feed", Lines: 2},
|
||||
{Type: "rule", Style: "double", Text: "l", Align: "left", Bold: true},
|
||||
{Type: "kv", Leader: "─", Pairs: []receipt.Pair{{Key: "k", Value: "v", Bold: true}}},
|
||||
}}, nil},
|
||||
{"a full rule", receipt.Element{Type: "rule", Style: "dashed", Text: "l", Align: "right", Bold: true, Columns: 255, Size: 3, Font: "a"}, nil},
|
||||
{"a full kv", receipt.Element{Type: "kv", Leader: "space", Columns: 1, Size: 1, Font: "B", Pairs: manyPairs(100)}, nil},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := problems(t, receipt.Job{Elements: []receipt.Element{tt.el}})
|
||||
if strings.Join(got, "\n") != strings.Join(tt.want, "\n") {
|
||||
t.Errorf("problems:\n%s\nwant:\n%s", strings.Join(got, "\n"), strings.Join(tt.want, "\n"))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateCountsChildren(t *testing.T) {
|
||||
children := make([]receipt.Element, receipt.MaxElements)
|
||||
for i := range children {
|
||||
children[i] = receipt.Element{Type: "feed"}
|
||||
}
|
||||
got := problems(t, receipt.Job{Elements: []receipt.Element{{Type: "box", Children: children}}})
|
||||
want := "elements: 501 elements including 500 box children, limit is 500"
|
||||
if len(got) != 1 || got[0] != want {
|
||||
t.Errorf("problems = %q, want %q", got, want)
|
||||
}
|
||||
if got := problems(t, receipt.Job{Elements: []receipt.Element{{Type: "box", Children: children[:499]}}}); got != nil {
|
||||
t.Errorf("500 elements with children: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateNestingIsOneLevel(t *testing.T) {
|
||||
inner := receipt.Element{Type: "box", Children: []receipt.Element{{Type: "qr"}}}
|
||||
got := problems(t, receipt.Job{Elements: []receipt.Element{{Type: "box", Children: []receipt.Element{inner}}}})
|
||||
// The inner box is refused as a whole; its own children are not looked at.
|
||||
want := "elements[0].children[0] (box): not allowed inside a box; place it after the box"
|
||||
if len(got) != 1 || got[0] != want {
|
||||
t.Errorf("problems = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user