Render QR codes pixel-exact with an own encoder
nxhmi.qr is a dependency-free QR encoder. Measured in the Editor simulator: numeric/alphanumeric/byte mode by content, level L, mask 0, smallest version; scaled to w - 2*dis by width (not clipped to h), last column run one pixel narrower, logo centred, nothing when too small. Basic rendering is now 20/20 pixel-exact. Also: unknown component kinds give a clean CLI error instead of a traceback. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f0bcc608b1
commit
842591a14c
10 files changed
+429
-9
No files matched your search
+40
-1
@@ -86,6 +86,37 @@ def gauge_needle(w: int, h: int, val: int, wid: int):
|
||||
for ax in range(lo, lo + wid) for ay in range(lo, lo + wid)}
|
||||
|
||||
|
||||
|
||||
def qr_rects(txt, w: int, dis: int):
|
||||
"""Dark rectangles ((x0, y0), (x1, y1)) exclusive, relative to the component, for a QR code
|
||||
component: the code (see nxhmi.qr) scaled to w - 2*dis pixels starting at (dis, dis);
|
||||
pixel p shows module p*N//size. The display draws dark runs column by column; the run still
|
||||
open at the very end (last column, reaching the bottom) comes out one pixel narrower.
|
||||
The code is square (sized by the width) and is not clipped to the component's height."""
|
||||
from . import qr
|
||||
if isinstance(txt, str):
|
||||
txt = txt.encode("latin-1", "replace")
|
||||
if not txt:
|
||||
return []
|
||||
m = qr.matrix(txt)
|
||||
n, size = len(m), w - 2 * dis
|
||||
if size < n: # less than a pixel per module: nothing drawn
|
||||
return []
|
||||
edge = [-(-k * size // n) for k in range(n + 1)] # first pixel of module k = ceil(k*size/n)
|
||||
rects = []
|
||||
for col in range(n):
|
||||
r = 0
|
||||
while r < n:
|
||||
if not m[r][col]:
|
||||
r += 1
|
||||
continue
|
||||
r0 = r
|
||||
while r < n and m[r][col]:
|
||||
r += 1
|
||||
x1 = edge[col + 1] - (1 if (col == n - 1 and r == n) else 0)
|
||||
rects.append(((dis + edge[col], dis + edge[r0]), (dis + x1, dis + edge[r])))
|
||||
return rects
|
||||
|
||||
def rgb(v: int):
|
||||
r, g, b = v >> 11, (v >> 5) & 63, v & 31
|
||||
return (r << 3 | r >> 2, g << 2 | g >> 4, b << 3 | b >> 2)
|
||||
@@ -437,4 +468,12 @@ class Renderer:
|
||||
|
||||
def qrcode(self, img, c):
|
||||
x, y, w, h = self._box(c)
|
||||
img.paste(rgb(c.get("bco") or 0xFFFF), (x, y, x + w, y + h))
|
||||
bco, pco = c.get("bco"), c.get("pco")
|
||||
img.paste(rgb(0xFFFF if bco is None else bco), (x, y, x + w, y + h))
|
||||
rects = qr_rects(c.get("txt") or "", w, c.get("dis") or 0)
|
||||
for (ax, ay), (bx, by) in rects:
|
||||
img.paste(rgb(pco or 0), (x + ax, y + ay, x + bx, y + by)) # not clipped to h (like the display)
|
||||
if rects and c.get("sta") == 1: # logo picture, centred in the box
|
||||
src = self.pic(c.get("pic"))
|
||||
if src:
|
||||
self._paste(img, src, 0, 0, src.width, src.height, x + (w - src.width) // 2, y + (h - src.height) // 2)
|
||||
Reference in new issue
Block a user