Skip to content

fix(printer): thread arabicShaping flag through backend and frontend print paths - #452

Merged
khaira777 merged 15 commits into
mainfrom
fm/fm-flocafe-450-fixes
Aug 22, 2026
Merged

khaira777 merged 15 commits into
mainfrom
fm/fm-flocafe-450-fixes

Conversation

@khaira777

@khaira777 khaira777 commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Intent

Finish PR #450 (fix for issue #437 'Not showing Items in Bill': receipt item lines silently disappear from desktop ESC/POS receipts when the global Arabic/Persian shaping support printer setting is enabled) and drive it to merge-ready. The PR threads the arabicShaping flag consistently through backend and frontend print paths, preserves warning reasons in warnings-toast classification, allows Persian formatting characters in shaping guards, sanitizes C0/DEL control bytes from raw shaped output, bounds shaped lines to printer width, and reproduces encoder centering centrally inside safePrinterText. All open review comments (greptile P1 centered-alignment and P2 control-byte security findings on frontend/src/lib/printer/warnings.ts) are addressed by commits already on the branch head. CAPTAIN'S STANDING RULING (feed exactly this to any gate asking about centering): keep the shared helper behavior - safePrinterText's centralized centering is authoritative; do NOT restructure call sites to use encoder centerCols instead. The new fix commit on fm/fm-flocafe-450-fixes repairs a TS2722 strict-null-check break (alignableEnc.align called without optional chaining at warnings.ts:122/127) left behind by a cancelled review-pipeline run; that typecheck failure broke both the 'Lint & Build Validation (Linux)' and 'End-to-End Playwright Tests' CI jobs on the latest branch push. Constraint: English-only receipt output must remain byte-identical.

What Changed

  • Added printerArabicShaping toggle to the POS settings store, settings UI, and i18n (en/es/fa/pt) so merchants can opt in to sending raw Arabic/Persian text to firmware-capable printers
  • Threaded the arabicShaping override from frontend renderer requests (usePrinter, orders/page.tsx) through the backend Express routes (print-bill, print-kot) into printReceipt, printKOT, prepareReceipt, and buildEscPos, where the profile's default is only overridden when an explicit boolean arrives from the request body
  • Extended safePrinterText in warnings.ts to pass Arabic-only lines through the unsupported-character guard when shaping is enabled, sanitizing C0/DEL control bytes, bounding shaped output to printer column width, and reproducing encoder centering via raw bytes; localized the warnings toast with shaping-aware hints
  • Added truncateShapedLine helpers and column-aware truncation in buildEscPos and receipt templates so shaped lines never exceed printer width, and allowed Persian formatting characters (RLM, ZWNJ, ZWJ, ellipsis) in the shaping guard regex

Risk Assessment

✅ Low: Change is well-bounded: threads a single boolean flag through existing print paths with correct guard logic at each boundary, preserves ASCII byte-identical output, and comprehensive tests cover all material code paths.

Testing

Verified PR #450 end-to-end: both TypeScript checks pass, all 411 targeted printer and print-API tests pass (360 printer + 28 bills-print-api + 8 receipt-printing + 4 receipt-column-width + 3 printer-width-refresh + 8 printer-migrations), English byte-identical receipt output is confirmed by the existing regression test, and the TS2722 strict-null-check fix on optional align chaining is in place and typechecks cleanly.

Evidence: Printer tests full output (360/360 passed)
All 360 printer tests passed including: buildEscPos control bytes, Arabic shaping gate, English byte-identical verification, frontend shaping propagation across compact/classic/detailed/tax-bill templates, shaped KOT add-on width bounds, control-byte sanitization, Persian centering, 58mm width bounds, wide IRR bounds, browser Rial/Toman tests, and warning classification.
Evidence: Bills print API tests (28/28 passed)
Test 9: prepareReceipt arabicShaping override (#437) — without override, Persian item line is skipped; English item line still prints; with arabicShaping:true override, Persian item line is emitted and no unsupported-line warnings remain.

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

✅ **Test** - passed

✅ No issues found.

  • npx tsc
  • cd frontend && npx tsc --noEmit -p tsconfig.json
  • npx ts-node --transpile-only -P tests/tsconfig.json tests/printer.test.ts (360/360 passed)
  • npx ts-node --transpile-only -P tests/tsconfig.json tests/bills-print-api.test.ts (28/28 passed)
  • npx ts-node --transpile-only -P tests/tsconfig.json tests/receipt-printing.test.ts (8/8 passed)
  • npx ts-node --transpile-only -P tests/tsconfig.json tests/receipt-column-width.test.ts (4/4 passed)
  • node tests/run-electron-node-test.cjs tests/printer-width-refresh.test.ts (3/3 passed)
  • node tests/run-electron-node-test.cjs tests/printer-usb-device-path-migration.test.ts (8/8 passed)
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

Fixes #437
Refs #438

…nes (#437)

ESC/POS encoders silently skipped every receipt line containing
Persian/Arabic script unless the printer profile declared
arabicShaping, and nothing exposed that flag — so merchants with
Persian product names got bills with no items at all.

Add a renderer-global 'printerArabicShaping' setting that threads
through the same paths as the existing useUnicode option:

- pos-settings store + Settings → Printing toggle (en/fa/es/pt)
- frontend encoders: safePrinterText emits pure ASCII+Arabic lines
  when shaping is enabled; other non-ASCII scripts stay blocked
- backend print-bill/print-kot accept an arabicShaping body field;
  an explicit boolean overrides the profile default, absence keeps it
- warning toast now localized (printWarnings.* namespace) naming the
  new setting when Persian/Arabic lines were dropped

Default stays off so generic printers never receive unshaped text.
Addresses greptile P1/P2 on #450: raw() shaping output now strips
C0/DEL control bytes (preventing printer-command injection via labels)
and reproduces encoder centering via an explicit column budget at
centered call sites, so double-width Persian business names keep their
heading padding.
The cancelled review pipeline left safePrinterText calling the optional
align() method without optional chaining, so Next.js typecheck failed
with TS2722 at warnings.ts:122 and :127 and broke both the Lint & Build
and Playwright CI jobs. centerRawLine already guarantees align exists;
optional chaining keeps runtime behavior identical.
@greptile-apps

greptile-apps Bot commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR consistently carries the Arabic/Persian firmware-shaping preference through browser, API, receipt, tax-bill, KOT, and preview printing paths.

  • Adds a persisted printer-capability toggle and localized settings/warning text.
  • Applies explicit request overrides while preserving backend printer-profile defaults when the flag is omitted.
  • Sanitizes control bytes, bounds shaped lines to printer width, and preserves centered raw output.
  • Expands regression coverage for propagation, mixed-script rejection, sanitization, width limits, and unchanged English output.

Confidence Score: 5/5

The PR appears safe to merge, with no concrete changed-code failure remaining after reviewing the shaping, propagation, sanitization, and width-handling paths.

Explicit boolean overrides are carried through the active print paths, profile defaults remain intact when no override is supplied, and shaped output is sanitized and bounded without altering established English output.

Important Files Changed

Filename Overview
frontend/src/lib/printer/warnings.ts Adds shaping-safe line classification, control-byte sanitization, display-width bounding, and centralized raw-text centering without an established defect.
frontend/src/hooks/usePrinter.ts Propagates the persisted shaping preference through local ESC/POS and backend receipt, tax-bill, and KOT paths.
main/routes/printers.ts Accepts only explicit boolean shaping overrides and threads them through bill preview, receipt dispatch, and KOT dispatch.
main/printers/thermal.ts Resolves request overrides against printer-profile defaults and applies sanitization and width limits to backend shaped output.
frontend/src/lib/printer/receipt-encoder.ts Threads shaping behavior through classic, compact, and detailed receipt fields while supplying column budgets where needed.
frontend/src/lib/printer/kot-encoder.ts Enables shaping for KOT metadata, items, add-ons, and instructions while retaining truncation and warning behavior.
tests/printer.test.ts Adds broad regression coverage for shaping gates, control-byte removal, centering, width limits, propagation, and English byte identity.
tests/bills-print-api.test.ts Verifies that the explicit backend override restores Persian item lines while omitted overrides preserve prior behavior.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Printer settings toggle] --> B[POS settings store]
  B --> C[Receipt and tax-bill printing]
  B --> D[KOT printing]
  B --> E[Receipt preview]
  C --> F{Print transport}
  D --> F
  F -->|Frontend ESC/POS| G[safePrinterText]
  F -->|Backend printer API| H[Route boolean override]
  E --> H
  H --> I[prepareReceipt / printReceipt / printKOT]
  I --> J[Profile default unless explicitly overridden]
  J --> K[buildEscPos]
  G --> L[Sanitize, bound, and emit shaped text]
  K --> L
Loading

Reviews (1): Last reviewed commit: "fix(printer): satisfy strict null check ..." | Re-trigger Greptile

@khaira777
khaira777 merged commit 68e14a8 into main Aug 22, 2026
13 checks passed
@khaira777
khaira777 deleted the fm/fm-flocafe-450-fixes branch August 22, 2026 02:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Not showing Items in Bill

1 participant