openhmi-nx: decode, encode, render Nextion HMI projects; .tft research
Black-box reverse-engineered .HMI format (container, three checksums, pages, all 31 component types, pictures, fonts), readable JSON, page renderer matching Nextion Editor, demo project built from code and a test suite (66 tests). See docs/COVERAGE.md for status. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
69e22cfb06
70 files changed
+25058
No files matched your search
@@ -0,0 +1,93 @@
|
||||
"""Pictures and fonts."""
|
||||
import io
|
||||
import random
|
||||
import struct
|
||||
import unittest
|
||||
|
||||
from helpers import FIXTURE, HAVE_PIL
|
||||
|
||||
from nxhmi import fonts, pictures
|
||||
from nxhmi.container import HMIFile
|
||||
|
||||
|
||||
class Pictures(unittest.TestCase):
|
||||
def test_layout_rotated_bottom_up(self):
|
||||
px = {(0, 0): (255, 0, 0), (1, 0): (0, 255, 0), (0, 1): (0, 0, 255), (1, 1): (255, 255, 255)}
|
||||
raw = pictures.to_rgb565(lambda x, y: px[(x, y)], 2, 2)
|
||||
self.assertEqual([struct.unpack_from("<H", raw, 2 * i)[0] for i in range(4)], [0x001F, 0xF800, 0xFFFF, 0x07E0])
|
||||
|
||||
def test_truncation(self):
|
||||
raw = pictures.to_rgb565(lambda x, y: (7, 3, 255), 1, 1)
|
||||
self.assertEqual(struct.unpack("<H", raw)[0], (0 << 11) | (0 << 5) | 31)
|
||||
|
||||
def test_headers(self):
|
||||
i = pictures.make_picture(b"\0" * 8, 2, 2)
|
||||
self.assertEqual(pictures.info(i), {"format": 0, "width": 2, "height": 2, "length": 8})
|
||||
s = pictures.make_source(b"PNGDATA", 2, 2)
|
||||
self.assertEqual(pictures.source_file(s), b"PNGDATA")
|
||||
self.assertEqual(pictures.source_info(s)["type"], "png")
|
||||
|
||||
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
|
||||
def test_image_roundtrip(self):
|
||||
from PIL import Image
|
||||
img = Image.new("RGB", (5, 3))
|
||||
for x in range(5):
|
||||
for y in range(3):
|
||||
img.putpixel((x, y), (x * 50, y * 100, 200))
|
||||
b = io.BytesIO(); img.save(b, "PNG")
|
||||
pic, src = pictures.from_image_file(b.getvalue())
|
||||
self.assertEqual(pictures.source_file(src), b.getvalue())
|
||||
back = pictures.to_image(pic)
|
||||
for x in range(5):
|
||||
for y in range(3):
|
||||
r, g, bb = img.getpixel((x, y))
|
||||
self.assertEqual(back.getpixel((x, y))[0] >> 3, r >> 3)
|
||||
|
||||
def test_editor_pictures_decode(self):
|
||||
h = HMIFile.load(FIXTURE)
|
||||
for n in h.names():
|
||||
if n.endswith(".i"):
|
||||
self.assertEqual(pictures.info(h.read(n))["format"], 0)
|
||||
|
||||
|
||||
class Fonts(unittest.TestCase):
|
||||
def test_rle_roundtrip_random(self):
|
||||
random.seed(3)
|
||||
for _ in range(60):
|
||||
w, h = random.choice([(8, 16), (16, 32), (12, 24), (5, 7)])
|
||||
rows = [[1 if random.random() < 0.35 else 0 for _ in range(w)] for _ in range(h)]
|
||||
self.assertEqual(fonts.decode_glyph(b"\x01" + fonts.encode_glyph(rows, w, h), w, h), rows)
|
||||
|
||||
def test_codes(self):
|
||||
# pixels are scanned per column from the bottom row up; rows are returned top -> bottom
|
||||
col = [r[0] for r in fonts.decode_glyph(b"\x01\x7e", 1, 32)] # 01 1 11110: 30 transparent, 2 ink
|
||||
self.assertEqual(col, [1, 1] + [0] * 30)
|
||||
self.assertEqual(fonts.decode_glyph(b"\x01\x22", 1, 2), [[1], [1]]) # 00 1 00010: 2 ink
|
||||
self.assertEqual(fonts.decode_glyph(b"\x01\x02\x41", 1, 4), [[1], [0], [0], [0]]) # 2 t, then 01 0: 1 t + 1 ink
|
||||
self.assertEqual(fonts.decode_glyph(b"\x01\xca", 1, 4), [[0], [1], [1], [0]]) # 11 001 010: 1 t + 2 ink
|
||||
self.assertEqual(fonts.decode_glyph(b"\x01\x84", 1, 7), [[1], [1], [1], [0], [0], [0], [0]]) # 10 0: 4 t + 3 ink
|
||||
|
||||
def test_font_pack_parse(self):
|
||||
f = fonts.Font("T-8", 8, 8, 1, 0x20, 0x21, [fonts.Glyph(0x20, 8, [[0] * 8 for _ in range(8)]),
|
||||
fonts.Glyph(0x21, 4, [[1, 0, 0, 1] for _ in range(8)])])
|
||||
g = fonts.Font.parse(f.pack())
|
||||
self.assertEqual((g.name, g.glyph("!").width, g.glyph("!").pixels), ("T-8", 4, f.glyphs[1].pixels))
|
||||
|
||||
def test_editor_fonts_reencode_identical(self):
|
||||
h = HMIFile.load(FIXTURE)
|
||||
for n in h.names():
|
||||
if n.endswith(".zi"):
|
||||
blob = h.read(n)
|
||||
self.assertEqual(fonts.Font.parse(blob).pack(), blob, n)
|
||||
|
||||
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
|
||||
def test_from_ttf_default_font(self):
|
||||
f = fonts.from_ttf(None, 20)
|
||||
self.assertEqual(len(f.glyphs), 95)
|
||||
self.assertTrue(any(any(r) for r in f.glyph("A").pixels))
|
||||
self.assertFalse(any(any(r) for r in f.glyph(" ").pixels))
|
||||
self.assertEqual(fonts.Font.parse(f.pack()).name, "Aileron-20ascii")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user