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
@@ -0,0 +1,96 @@
|
||||
"""QR code encoder and QR component rendering, checked against Nextion Editor 1.68's simulator."""
|
||||
import unittest
|
||||
|
||||
from helpers import HAVE_PIL
|
||||
|
||||
from nxhmi import components as C
|
||||
from nxhmi import qr
|
||||
from nxhmi.container import build
|
||||
|
||||
# module grids read back from the Editor simulator (QR code component, 100x100, dis 8)
|
||||
CAPTURED = {
|
||||
"hello": [ # byte mode
|
||||
"111111100101101111111", "100000100111001000001", "101110101101101011101", "101110100101001011101",
|
||||
"101110100010101011101", "100000100000101000001", "111111101010101111111", "000000001101100000000",
|
||||
"111011111111011000100", "011111011100001000011", "011111101000100011111", "001100000000001000010",
|
||||
"000010110110101010000", "000000001101010100111", "111111101111011100111", "100000101111110110000",
|
||||
"101110101111011100011", "101110100010001100110", "101110101110100010101", "100000101100001010010",
|
||||
"111111101010101100011"],
|
||||
"HELLO WORLD 12345": [ # alphanumeric mode
|
||||
"111111100010101111111", "100000100000101000001", "101110101010001011101", "101110100000101011101",
|
||||
"101110100101101011101", "100000100111001000001", "111111101010101111111", "000000001010000000000",
|
||||
"111011111010111000100", "010100001001001100001", "010101111111011111000", "000101000101111101110",
|
||||
"101011101101000110101", "000000001011010000101", "111111101100101101100", "100000101101111101010",
|
||||
"101110101100010111100", "101110100111001100010", "101110101110001101001", "100000101010000001011",
|
||||
"111111101001101100001"],
|
||||
"0123456789": [ # numeric mode
|
||||
"111111100101101111111", "100000100111001000001", "101110101101101011101", "101110100101001011101",
|
||||
"101110100010101011101", "100000100000101000001", "111111101010101111111", "000000001101100000000",
|
||||
"111011111111011000100", "101010010100001000010", "000111100000100011110", "111000010000001000010",
|
||||
"110101101100101010001", "000000001111010100010", "111111101001011100000", "100000101111110111111",
|
||||
"101110101001011100001", "101110100100001100010", "101110101000100010001", "100000101000001101100",
|
||||
"111111101000101011101"],
|
||||
}
|
||||
|
||||
|
||||
def bits(m):
|
||||
return ["".join("1" if v else "0" for v in row) for row in m]
|
||||
|
||||
|
||||
class Encoder(unittest.TestCase):
|
||||
def test_matches_display(self):
|
||||
for text, grid in CAPTURED.items():
|
||||
self.assertEqual(bits(qr.matrix(text)), grid, text)
|
||||
|
||||
def test_modes(self):
|
||||
self.assertEqual(qr.mode_of(b"0123"), "numeric")
|
||||
self.assertEqual(qr.mode_of(b"HELLO 1"), "alnum")
|
||||
self.assertEqual(qr.mode_of(b"hello"), "byte")
|
||||
self.assertEqual(qr.mode_of("caf\xe9".encode("latin-1")), "byte")
|
||||
|
||||
def test_versions(self):
|
||||
# level L, byte mode: v1 holds 17 bytes, v2 32, v7 154 (all seen in the simulator)
|
||||
self.assertEqual(qr.pick_version(b"x" * 17), 1)
|
||||
self.assertEqual(qr.pick_version(b"x" * 24), 2)
|
||||
self.assertEqual(qr.pick_version(b"x" * 40), 3)
|
||||
self.assertEqual(qr.pick_version(b"x" * 160), 8)
|
||||
self.assertEqual(len(qr.matrix("x" * 160)), 49)
|
||||
|
||||
def test_format_bits_level_l_mask_0(self):
|
||||
m = qr.matrix("hello")
|
||||
fmt = [m[8][c] for c in (0, 1, 2, 3, 4, 5, 7, 8)] + [m[r][8] for r in (7, 5, 4, 3, 2, 1, 0)]
|
||||
value = int("".join("1" if b else "0" for b in fmt), 2) ^ 0x5412
|
||||
self.assertEqual(value >> 10, 0b01000) # level L (01), mask 0
|
||||
|
||||
def test_version_info_block(self):
|
||||
m = qr.matrix("x" * 160) # version 8 has the 6x3 version blocks
|
||||
n = len(m)
|
||||
info = sum(m[c][n - 11 + i % 3] << i for i, c in ((i, i // 3) for i in range(18)))
|
||||
self.assertEqual(info >> 12, 8)
|
||||
|
||||
|
||||
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
|
||||
class Render(unittest.TestCase):
|
||||
def render(self, page):
|
||||
from nxhmi.render import Renderer
|
||||
return Renderer(build([("0.pa", page.pack())])).render(page.name)
|
||||
|
||||
def test_scaling_and_last_run(self):
|
||||
from nxhmi.render import qr_rects
|
||||
rects = qr_rects("hello", 100, 8) # 84 px / 21 modules = 4 px each
|
||||
self.assertIn(((8, 8), (12, 36)), rects) # first column: finder, 7 modules tall
|
||||
self.assertIn(((88, 88), (91, 92)), rects) # last run is one pixel narrower
|
||||
self.assertEqual(qr_rects("hello", 78, 30), []) # 18 px for 21 modules: nothing drawn
|
||||
self.assertEqual(qr_rects("", 100, 8), [])
|
||||
|
||||
def test_component(self):
|
||||
p = C.new_page("p", w=200, h=130)
|
||||
C.add(p, "qrcode", x=0, y=0, w=120, h=100, txt="HELLO WORLD 12345", dis=8, bco=0xFFE0, pco=0x001F)
|
||||
img = self.render(p)
|
||||
self.assertEqual(img.getpixel((2, 2)), (255, 255, 0)) # margin in bco
|
||||
self.assertEqual(img.getpixel((9, 9)), (0, 0, 255)) # finder in pco
|
||||
self.assertEqual(img.getpixel((10, 105)), (0, 0, 255)) # sized by width, runs below h=100
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user