Files
crosspoint-reader-mod/src/util/QrUtils.cpp
Adrian Wilkins-Caruana f1e9dc7f30 perf: font-compression improvements (#1056)
## Purpose

This PR includes some preparatory changes that are needed for an
upcoming performant CJK font feature. The changes have no impact on
render time and heap allocation for latin text. **Despite this, I think
these changes stand on their own as a better font
compression/decompression implementation.**

## Summary

- Font decompressor rewrite: Replaced the 4-slot LRU group cache with a
two-tier system — a page buffer (glyphs prewarmed before rendering
begins) and a hot-group fallback (last decompressed group retained for
non-prewarmed
  glyphs). 
- Byte-aligned compressed bitmap format: Glyph bitmaps within compressed
groups are now stored row-padded rather than tightly packed before
DEFLATE compression, improving compression ratios by making identical
pixel rows produce
identical byte patterns. Glyphs are compacted back to packed format on
demand at render time. Reduces flash size by 155 KB.
- Page prewarm system: Added `Page::collectText` and
`Page::getDominantStyle` to extract per-style glyph requirements before
rendering, and `GfxRenderer::prewarmFontCache` to pre-decompress only
the groups needed for the dominant style
   — eliminating mid-render decompression for the common case.
- UTF-8 robustness fixes: `utf8NextCodepoint` now validates continuation
bytes and returns a replacement glyph on malformed input;
`ChapterHtmlSlimParser` correctly preserves incomplete multi-byte
sequences across word-buffer flush
  boundaries rather than splitting them.

---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? _**YES**_ Architecture and
design was done by me, refined a bit by Claude. Code mostly by Claude,
but not entirely.
2026-03-11 21:05:46 +01:00

67 lines
2.3 KiB
C++

#include "QrUtils.h"
#include <Utf8.h>
#include <qrcode.h>
#include <algorithm>
#include <memory>
#include "Logging.h"
void QrUtils::drawQrCode(const GfxRenderer& renderer, const Rect& bounds, const std::string& textPayload) {
// Dynamically calculate the QR code version based on text length
// Version 4 holds ~114 bytes, Version 10 ~395, Version 20 ~1066, up to 40
// qrcode.h max version is 40.
// Formula: approx version = size / 26 + 1 (very rough estimate, better to find best fit)
size_t len = textPayload.length();
// Truncate to max QR capacity at a UTF-8 safe boundary to avoid splitting multi-byte sequences
static constexpr size_t MAX_QR_CAPACITY = 2953; // Version 40, ECC_LOW, byte mode
std::string truncated;
const char* payload = textPayload.c_str();
if (len > MAX_QR_CAPACITY) {
len = utf8SafeTruncateBuffer(textPayload.c_str(), static_cast<int>(MAX_QR_CAPACITY));
truncated = textPayload.substr(0, len);
payload = truncated.c_str();
}
int version = 4;
if (len > 114) version = 10;
if (len > 395) version = 20;
if (len > 1066) version = 30;
if (len > 2110) version = 40;
// Make sure we have a large enough buffer on the heap to avoid blowing the stack
uint32_t bufferSize = qrcode_getBufferSize(version);
auto qrcodeBytes = std::make_unique<uint8_t[]>(bufferSize);
QRCode qrcode;
// Initialize the QR code. We use ECC_LOW for max capacity.
int8_t res = qrcode_initText(&qrcode, qrcodeBytes.get(), version, ECC_LOW, payload);
if (res == 0) {
// Determine the optimal pixel size.
const int maxDim = std::min(bounds.width, bounds.height);
int px = maxDim / qrcode.size;
if (px < 1) px = 1;
// Calculate centering X and Y
const int qrDisplaySize = qrcode.size * px;
const int xOff = bounds.x + (bounds.width - qrDisplaySize) / 2;
const int yOff = bounds.y + (bounds.height - qrDisplaySize) / 2;
// Draw the QR Code
for (uint8_t cy = 0; cy < qrcode.size; cy++) {
for (uint8_t cx = 0; cx < qrcode.size; cx++) {
if (qrcode_getModule(&qrcode, cx, cy)) {
renderer.fillRect(xOff + px * cx, yOff + px * cy, px, px, true);
}
}
}
} else {
// If it fails (e.g. text too large), log an error
LOG_ERR("QR", "Text too large for QR Code version %d", version);
}
}