Test / Lint, test and build (push) Successful in 3m17s
Module
- The module path is now the bare "uberbringer"; imports read
uberbringer/internal/... The banner keeps the Gitea URL.
CI and releases
- Workflows move to .gitea/workflows: test.yml (every branch push:
gofmt, goimports, tidy, vet, staticcheck, race tests, goreleaser
snapshot), security.yml (govulncheck weekly and on main) and a hardened
release.yml (v* tags on main only, tags fetched, goreleaser pinned to
2.20.3). The broken labeler workflow is removed.
- go.mod pins toolchain go1.27.4, which fixes GO-2026-6859; workflows
check that the running Go matches it.
- .goreleaser.yaml: tidy check that never writes, reproducible file times,
prerelease: auto, a release header linking CHANGELOG.md, and archives
that ship CHANGELOG.md, the example config and the systemd unit.
- Makefile mirrors the CI gate; .dockerignore is an allowlist.
API and tests
- Every error answers {"error", "code", "details"} with a stable code from
server/errors.go; the framework's own error text is never sent.
- Panics and unexpected errors are logged and reported to Sentry once;
refusals are not logged as failures. /healthz sends no-store.
- TestRoutesConform requires auth, rate limiting and the body limit on
every /api/v1 route.
- Race-prone tests no longer rely on freed ports or sleeps; serve takes
an injected listener.
- The test config uses a placeholder secret instead of a real one.
Install and packaging
- install.sh resolves the latest release through the Gitea API, verifies
checksums, requests a certificate only when none exists, installs the
hardened packaging/uberbringer.service and writes the first token to a
0600 file instead of printing it. It is now executable in git.
Docs
- CLAUDE.md, CHANGELOG.md (0.2.0 with breaking changes and upgrade
steps), docs/design.md, and docs/plans/formatters.md for the planned
CP437 blocks, modules and ntfy printing. README updated throughout.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
296 lines
7.1 KiB
Go
296 lines
7.1 KiB
Go
package receipt
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"image"
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"uberbringer/internal/pkg/escpos"
|
|
"uberbringer/internal/pkg/raster"
|
|
)
|
|
|
|
// Fetcher downloads remote image data.
|
|
type Fetcher interface {
|
|
Fetch(ctx context.Context, url string) ([]byte, error)
|
|
}
|
|
|
|
// Options configures a Renderer.
|
|
type Options struct {
|
|
// Profile describes the printer; its DotsPerLine is the image width.
|
|
Profile escpos.Profile
|
|
|
|
// CodePage is the character table text is transcoded to.
|
|
CodePage escpos.CodePage
|
|
|
|
// Columns, when positive, overrides the Font A characters per line used
|
|
// for word wrapping.
|
|
Columns int
|
|
|
|
// MaxPixels caps decoded image size; MaxHeight caps printed image height
|
|
// in dots.
|
|
MaxPixels int
|
|
MaxHeight int
|
|
}
|
|
|
|
// Renderer turns jobs into ESC/POS command streams. It is safe for concurrent
|
|
// use.
|
|
type Renderer struct {
|
|
options Options
|
|
fetcher Fetcher
|
|
}
|
|
|
|
// NewRenderer returns a Renderer. fetcher loads images given by URL.
|
|
func NewRenderer(options Options, fetcher Fetcher) *Renderer {
|
|
if fetcher == nil {
|
|
panic("receipt.NewRenderer: fetcher must not be nil")
|
|
}
|
|
if options.Profile.DotsPerLine <= 0 || options.Profile.FontAWidth <= 0 || options.Profile.FontBWidth <= 0 {
|
|
panic("receipt.NewRenderer: profile must be set")
|
|
}
|
|
if options.MaxPixels <= 0 || options.MaxHeight <= 0 {
|
|
panic("receipt.NewRenderer: image limits must be positive")
|
|
}
|
|
return &Renderer{options: options, fetcher: fetcher}
|
|
}
|
|
|
|
// Render validates job, loads its images and returns the complete command
|
|
// stream. Nothing is returned unless the whole receipt rendered, so a failure
|
|
// never reaches the printer half-done.
|
|
func (r *Renderer) Render(ctx context.Context, job Job) ([]byte, error) {
|
|
if err := job.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
images, err := r.loadImages(ctx, job)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var body escpos.Builder
|
|
p := printer{b: &body, lineDots: r.textLineDots(), profile: r.options.Profile, codePage: r.options.CodePage}
|
|
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)
|
|
}
|
|
}
|
|
p.endLine()
|
|
if last := job.Elements[len(job.Elements)-1]; last.Type != TypeCut {
|
|
body.Cut(escpos.CutPartial, 0)
|
|
}
|
|
|
|
var out escpos.Builder
|
|
out.Initialize()
|
|
out.SelectCodePage(r.options.CodePage)
|
|
for range job.copies() {
|
|
out.Raw(body.Bytes())
|
|
}
|
|
return bytes.Clone(out.Bytes()), nil
|
|
}
|
|
|
|
// textLineDots is the line width used for text layout.
|
|
func (r *Renderer) textLineDots() int {
|
|
if r.options.Columns > 0 {
|
|
return r.options.Columns * r.options.Profile.FontAWidth
|
|
}
|
|
return r.options.Profile.DotsPerLine
|
|
}
|
|
|
|
// loadImages fetches, decodes and dithers every image before anything is
|
|
// rendered, keyed by element index.
|
|
func (r *Renderer) loadImages(ctx context.Context, job Job) (map[int]*image.Paletted, error) {
|
|
images := make(map[int]*image.Paletted)
|
|
for i, el := range job.Elements {
|
|
if el.Type != TypeImage {
|
|
continue
|
|
}
|
|
img, err := r.loadImage(ctx, el)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("elements[%d] (image): %w", i, err)
|
|
}
|
|
images[i] = img
|
|
}
|
|
return images, nil
|
|
}
|
|
|
|
func (r *Renderer) loadImage(ctx context.Context, el Element) (*image.Paletted, error) {
|
|
spec, err := el.imageSpec()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
data := spec.data
|
|
if spec.url != "" {
|
|
if data, err = r.fetcher.Fetch(ctx, spec.url); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
src, err := raster.Decode(data, r.options.MaxPixels)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
paper := r.options.Profile.DotsPerLine
|
|
width := min(src.Bounds().Dx(), paper)
|
|
if spec.width > 0 {
|
|
if spec.width > paper {
|
|
return nil, fmt.Errorf("width %d is wider than the %d-dot paper", spec.width, paper)
|
|
}
|
|
width = spec.width
|
|
}
|
|
mono, err := raster.Monochrome(src, width)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if h := mono.Bounds().Dy(); h > r.options.MaxHeight {
|
|
return nil, fmt.Errorf("image is %d dots tall after scaling, limit is %d", h, r.options.MaxHeight)
|
|
}
|
|
|
|
var x int
|
|
switch spec.align {
|
|
case escpos.JustifyCenter:
|
|
x = (paper - width) / 2
|
|
case escpos.JustifyRight:
|
|
x = paper - width
|
|
}
|
|
return raster.Place(mono, paper, x)
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
func (p *printer) element(el Element, img *image.Paletted) error {
|
|
switch el.Type {
|
|
case TypeText:
|
|
return p.text(el)
|
|
case TypeFeed:
|
|
lines, err := el.feedLines()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
p.b.Feed(lines)
|
|
p.x = 0
|
|
case TypeQR:
|
|
spec, err := el.qrSpec()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
p.endLine()
|
|
p.b.Justify(spec.align)
|
|
return p.b.QRCode([]byte(el.Data), spec.size, spec.correction)
|
|
case TypeBarcode:
|
|
spec, err := el.barcodeSpec()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
p.endLine()
|
|
p.b.Justify(spec.align)
|
|
p.b.BarcodeHeight(uint8(spec.height))
|
|
if err := p.b.BarcodeWidth(spec.width); err != nil {
|
|
return err
|
|
}
|
|
p.b.BarcodeHRI(spec.hri)
|
|
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)
|
|
return p.b.RasterImage(img)
|
|
case TypeRaw:
|
|
data, err := el.rawBytes()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
p.b.Raw(data)
|
|
// The position after arbitrary bytes is unknown; assume a fresh line.
|
|
p.x = 0
|
|
case TypeCut:
|
|
mode, err := el.cutMode()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
p.endLine()
|
|
p.b.Cut(mode, 0)
|
|
default:
|
|
return fmt.Errorf("unknown type %q", el.Type)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *printer) text(el Element) error {
|
|
spec, err := el.textSpec()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
glyph := p.profile.GlyphWidth(spec.font) * spec.width
|
|
|
|
p.b.Justify(spec.align)
|
|
p.b.Font(spec.font)
|
|
if err := p.b.Size(spec.width, spec.height); err != nil {
|
|
return err
|
|
}
|
|
p.b.Bold(spec.bold)
|
|
p.b.Underline(spec.underline)
|
|
p.b.Reverse(spec.invert)
|
|
|
|
text := sanitize(el.Text)
|
|
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.b.Text(p.codePage.Encode(line))
|
|
p.x += utf8.RuneCountInString(line) * glyph
|
|
}
|
|
} else {
|
|
p.b.Text(p.codePage.Encode(text))
|
|
p.advance(text, glyph)
|
|
}
|
|
if spec.newline {
|
|
p.b.LineFeed()
|
|
p.x = 0
|
|
}
|
|
|
|
// Reset styles so they cannot leak into barcodes, QR codes or raw data.
|
|
p.b.Bold(false)
|
|
p.b.Underline(escpos.UnderlineNone)
|
|
p.b.Reverse(false)
|
|
if err := p.b.Size(1, 1); err != nil {
|
|
return err
|
|
}
|
|
p.b.Font(escpos.FontA)
|
|
return nil
|
|
}
|
|
|
|
// advance updates the position after unwrapped text, which the printer wraps
|
|
// by itself when it overflows.
|
|
func (p *printer) advance(text string, glyph int) {
|
|
if i := strings.LastIndexByte(text, '\n'); i >= 0 {
|
|
p.x = 0
|
|
text = text[i+1:]
|
|
}
|
|
p.x += utf8.RuneCountInString(text) * glyph
|
|
if p.lineDots > 0 {
|
|
p.x %= p.lineDots
|
|
}
|
|
}
|
|
|
|
// endLine finishes a partially printed line so the next element starts at
|
|
// the left margin.
|
|
func (p *printer) endLine() {
|
|
if p.x > 0 {
|
|
p.b.LineFeed()
|
|
p.x = 0
|
|
}
|
|
}
|