Files
uberbringer/internal/pkg/escpos/qrcode.go
T
57_WolveandClaude Opus 5.5 1335ed0687
Security / Vulnerability check (push) Successful in 41s
Test / Lint and test (push) Successful in 1m57s
Add the notify and ntfy modules and scoped tokens
Breaking: a job with a raw element prints only with a token issued with
uberbringer token --raw. Every other token, every existing one included,
gets 403 token_scope for it, with the raw elements in details, and keeps
every other route.

The notify module prints a notification card from a title, message,
priority and tags, with topic, click, up to three actions, image,
markdown and time as options; it decodes strictly and refuses a payload
over its length caps. The ntfy module takes one ntfy message as ntfy
sends it, such as the ntfy client's $NTFY_RAW, ignores fields it does
not know, cuts text over the caps, and answers 204 for other events and
binary bodies. Both print every message plain: one message cannot tell
the first of a sequence from a replacement, since a publisher may choose
the sequence ID. internal/pkg/ntfy holds ntfy's message types.

Both print the same card. The priority picks the form: a dark URGENT
rule and a double box, a double box headed HIGH, a single box with the
title in its border, or a rule with no box. Tags print as markers in
front of the title, the rest on a Tags row; links print as QR codes with
their labels; attached PNG, JPEG, GIF and WebP images print after the
card. A line-level markdown subset is laid out without a dependency,
keeping marks inside code spans and URLs, and emoji are removed after a
few typed ones become markers. A card that cannot be laid out prints as
a plain receipt, and the server logs the fault once at ERROR and reports
it to Sentry once. [ntfy.format] and [ntfy.markers] shape the cards; the
markers table is a setting with the user's own keys, also settable as
UBERBRINGER_NTFY_MARKERS=name=value,...

uberbringer token --modules issues a token limited to those modules'
print and preview routes, job status and the module list; each route in
the table says which tokens it takes, and a scope claim that could read
as absent makes the token invalid. image elements take optional, which
also shrinks a too-wide image to the paper; a QR code that could be too
wide for the paper prints with smaller modules; box takes title_bold.
Submit takes a Meta whose source shows in job status and, with a copy
of the request ID, in every worker log line of the job.

The four ntfy cards passed on the TM-T88IV, posted as the client recipe
posts them, and their bytes are pinned in TestPaperReceipts. docs/api.md
describes the modules, the card and the client recipe, with tokens read
from a header file; docs/swagger.json is regenerated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 13:57:08 -06:00

105 lines
4.1 KiB
Go

package escpos
import (
"fmt"
"strings"
)
// QRCorrection is the QR code error correction level. Higher levels survive
// more damage at the cost of a denser code.
type QRCorrection byte
// QRCorrection values for GS ( k function 169.
const (
QRCorrectionL QRCorrection = 48 // about 7% recovery
QRCorrectionM QRCorrection = 49 // about 15% recovery
QRCorrectionQ QRCorrection = 50 // about 25% recovery
QRCorrectionH QRCorrection = 51 // about 30% recovery
)
// QR code limits.
const (
MaxQRData = 7089 // bytes the printer's symbol storage accepts
MinQRModuleSize = 1
MaxQRModuleSize = 16
)
// qrByteCapacity holds, for each QR version from 1 to 40, how many bytes a
// code of that version holds in byte mode at correction levels L, M, Q and
// H (ISO/IEC 18004).
var qrByteCapacity = [40][4]int{
{17, 14, 11, 7}, {32, 26, 20, 14}, {53, 42, 32, 24}, {78, 62, 46, 34},
{106, 84, 60, 44}, {134, 106, 74, 58}, {154, 122, 86, 64}, {192, 152, 108, 84},
{230, 180, 130, 98}, {271, 213, 151, 119}, {321, 251, 177, 137}, {367, 287, 203, 155},
{425, 331, 241, 177}, {458, 362, 258, 194}, {520, 412, 292, 220}, {586, 450, 322, 250},
{644, 504, 364, 280}, {718, 560, 394, 310}, {792, 624, 442, 338}, {858, 666, 482, 382},
{929, 711, 509, 403}, {1003, 779, 565, 439}, {1091, 857, 611, 461}, {1171, 911, 661, 511},
{1273, 997, 715, 535}, {1367, 1059, 751, 593}, {1465, 1125, 805, 625}, {1528, 1190, 868, 658},
{1628, 1264, 908, 698}, {1732, 1370, 982, 742}, {1840, 1452, 1030, 790}, {1952, 1538, 1112, 842},
{2068, 1628, 1168, 898}, {2188, 1722, 1228, 958}, {2303, 1809, 1283, 983}, {2431, 1911, 1351, 1051},
{2563, 1989, 1423, 1093}, {2699, 2099, 1499, 1139}, {2809, 2213, 1579, 1219}, {2953, 2331, 1663, 1273},
}
// QRModules returns how many modules wide a QR code holding n bytes at
// level is at most, or 0 for a level that is not one. It assumes byte mode,
// the least dense, so the printer's own code is never wider; data that
// only a denser mode holds, such as thousands of digits, counts as the
// largest version, 177 modules.
func QRModules(n int, level QRCorrection) int {
if level < QRCorrectionL || level > QRCorrectionH {
return 0
}
column := int(level - QRCorrectionL)
for v, capacity := range qrByteCapacity {
if n <= capacity[column] {
return 17 + 4*(v+1)
}
}
return 17 + 4*len(qrByteCapacity)
}
// ParseQRCorrection returns the correction level named by name: L, M, Q or H.
func ParseQRCorrection(name string) (QRCorrection, error) {
switch strings.ToUpper(strings.TrimSpace(name)) {
case "L":
return QRCorrectionL, nil
case "M":
return QRCorrectionM, nil
case "Q":
return QRCorrectionQ, nil
case "H":
return QRCorrectionH, nil
}
return 0, fmt.Errorf("%w: unknown QR correction level %q (want L, M, Q or H)", ErrInvalidArgument, name)
}
// QRCode appends a model 2 QR code holding data, printed with square modules
// moduleSize dots wide.
func (b *Builder) QRCode(data []byte, moduleSize int, level QRCorrection) error {
switch {
case len(data) == 0:
return fmt.Errorf("%w: QR data is empty", ErrInvalidArgument)
case len(data) > MaxQRData:
return fmt.Errorf("%w: QR data is %d bytes, limit is %d", ErrInvalidArgument, len(data), MaxQRData)
case moduleSize < MinQRModuleSize || moduleSize > MaxQRModuleSize:
return fmt.Errorf("%w: QR module size %d must be within %d-%d",
ErrInvalidArgument, moduleSize, MinQRModuleSize, MaxQRModuleSize)
case level < QRCorrectionL || level > QRCorrectionH:
return fmt.Errorf("%w: unknown QR correction level %d", ErrInvalidArgument, byte(level))
}
// Function 165: select model 2.
b.buf.Write([]byte{gs, '(', 'k', 4, 0, '1', 'A', '2', 0})
// Function 167: module size.
b.buf.Write([]byte{gs, '(', 'k', 3, 0, '1', 'C', byte(moduleSize)})
// Function 169: error correction level.
b.buf.Write([]byte{gs, '(', 'k', 3, 0, '1', 'E', byte(level)})
// Function 180: store data. The length covers cn, fn, m and the data.
n := len(data) + 3
b.buf.Write([]byte{gs, '(', 'k', byte(n), byte(n >> 8), '1', 'P', '0'})
b.buf.Write(data)
// Function 181: print the stored symbol.
b.buf.Write([]byte{gs, '(', 'k', 3, 0, '1', 'Q', '0'})
return nil
}