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,362 @@
|
||||
"""Render pages to images, the way Nextion Editor draws them on its canvas (needs Pillow).
|
||||
|
||||
Usage::
|
||||
|
||||
from nxhmi.render import Renderer
|
||||
r = Renderer(HMIFile.load("project.HMI"))
|
||||
r.render("main").save("main.png")
|
||||
|
||||
Covered: page background (colour / picture), picture, crop, text, scrolling text (static), number,
|
||||
xfloat, button, dual-state button, slider, progress bar, checkbox, radio, gauge, waveform (frame +
|
||||
grid), QR code (placeholder pattern). Invisible components (timer, variable, hotspot, touch cap)
|
||||
draw nothing. See docs/RENDER.md for how close each one is to the Editor.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
from .container import HMIFile
|
||||
from .fonts import Font
|
||||
from .page import Component, Page
|
||||
from . import pictures
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
class Renderer:
|
||||
def __init__(self, hmi: HMIFile):
|
||||
from PIL import Image # noqa: F401 (fail early if Pillow is missing)
|
||||
self.h = hmi
|
||||
self._pics = {}
|
||||
self._fonts = {}
|
||||
self.pages = {}
|
||||
for e in hmi.live():
|
||||
if e.name.endswith(".pa"):
|
||||
p = Page.parse(hmi.read(e.name))
|
||||
self.pages[p.name] = p
|
||||
self.pages[int(e.name[:-3])] = p
|
||||
|
||||
# ------------------------------------------------------------------ resources
|
||||
def pic(self, n: int):
|
||||
if n not in self._pics:
|
||||
try:
|
||||
self._pics[n] = pictures.to_image(self.h.read("%d.i" % n))
|
||||
except (KeyError, NotImplementedError):
|
||||
self._pics[n] = None
|
||||
return self._pics[n]
|
||||
|
||||
def font(self, n: int):
|
||||
if n not in self._fonts:
|
||||
try:
|
||||
self._fonts[n] = Font.parse(self.h.read("%d.zi" % n))
|
||||
except (KeyError, ValueError):
|
||||
self._fonts[n] = None
|
||||
return self._fonts[n]
|
||||
|
||||
# ------------------------------------------------------------------ page
|
||||
def render(self, page) -> "Image":
|
||||
from PIL import Image
|
||||
p = self.pages[page]
|
||||
pc = p.components[0]
|
||||
w, h = pc.get("w") or 480, pc.get("h") or 320
|
||||
img = Image.new("RGB", (w, h), (255, 255, 255))
|
||||
sta = pc.get("sta")
|
||||
if sta == 1:
|
||||
img.paste(rgb(pc.get("bco")), (0, 0, w, h))
|
||||
elif sta == 2:
|
||||
pic = self.pic(pc.get("pic"))
|
||||
if pic:
|
||||
self._paste(img, pic, 0, 0, w, h, 0, 0)
|
||||
for c in p.components[1:]:
|
||||
self.draw(img, c)
|
||||
return img
|
||||
|
||||
# ------------------------------------------------------------------ components
|
||||
def draw(self, img, c: Component):
|
||||
t = c.get("type")
|
||||
fn = {116: self.text, 55: self.text, 98: self.text, 53: self.dual, 54: self.number, 59: self.xfloat,
|
||||
112: self.picture, 113: self.crop, 1: self.slider, 106: self.progress, 56: self.checkbox,
|
||||
57: self.radio, 122: self.gauge, 0: self.waveform, 58: self.qrcode}.get(t)
|
||||
if fn:
|
||||
fn(img, c)
|
||||
|
||||
@staticmethod
|
||||
def _paste(img, src, sx, sy, w, h, dx, dy):
|
||||
"""Paste src[sx:sx+w, sy:sy+h] at (dx, dy), clipped to src's real size (no black padding)."""
|
||||
w = min(w, src.width - sx)
|
||||
h = min(h, src.height - sy)
|
||||
if w > 0 and h > 0:
|
||||
img.paste(src.crop((sx, sy, sx + w, sy + h)), (dx, dy))
|
||||
|
||||
def _box(self, c):
|
||||
x, y, w, h = c.get("x"), c.get("y"), c.get("w"), c.get("h")
|
||||
return x, y, w, h
|
||||
|
||||
def _background(self, img, c, x, y, w, h):
|
||||
sta = c.get("sta")
|
||||
if sta == 0: # crop image: same area of picture picc
|
||||
src = self.pic(c.get("picc"))
|
||||
if src:
|
||||
self._paste(img, src, x, y, w, h, x, y)
|
||||
elif sta == 1:
|
||||
img.paste(rgb(c.get("bco")), (x, y, x + w, y + h))
|
||||
elif sta == 2:
|
||||
src = self.pic(c.get("pic"))
|
||||
if src:
|
||||
self._paste(img, src, 0, 0, w, h, x, y)
|
||||
|
||||
def _border(self, img, c, x, y, w, h, override=None):
|
||||
from PIL import ImageDraw
|
||||
d = ImageDraw.Draw(img)
|
||||
style = c.get("style") if override is None else override
|
||||
if c.get("sta") in (0, 2) and c.get("type") in (98, 53):
|
||||
return # picture-skinned buttons have no frame
|
||||
if style == 1: # border
|
||||
bw, bc = c.get("borderw") or 0, rgb(c.get("borderc") or 0)
|
||||
for i in range(bw):
|
||||
d.rectangle((x + i, y + i, x + w - 1 - i, y + h - 1 - i), outline=bc)
|
||||
elif style in (2, 3, 4): # 3D down / up / auto(=up when released)
|
||||
light, dark = (230, 226, 230), (65, 68, 65) # measured in Editor 1.68
|
||||
if style == 2:
|
||||
light, dark = dark, light
|
||||
d.line((x, y, x + w - 1, y), fill=light); d.line((x, y, x, y + h - 1), fill=light)
|
||||
d.line((x, y + h - 1, x + w - 1, y + h - 1), fill=dark); d.line((x + w - 1, y, x + w - 1, y + h - 1), fill=dark)
|
||||
|
||||
def _text(self, img, c, s, x, y, w, h, color):
|
||||
font = self.font(c.get("font") or 0)
|
||||
if font is None or not s:
|
||||
return
|
||||
spax, spay = c.get("spax") or 0, c.get("spay") or 0
|
||||
xcen, ycen = c.get("xcen"), c.get("ycen")
|
||||
wrap = c.get("isbr")
|
||||
glyphs = {g.code: g for g in font.glyphs}
|
||||
|
||||
def gw(ch):
|
||||
g = glyphs.get(ord(ch))
|
||||
return (g.width if g else font.width) + spax
|
||||
|
||||
lines = []
|
||||
for para in s.replace("\r\n", "\n").split("\n"):
|
||||
cur = ""
|
||||
for ch in para:
|
||||
if wrap and cur and sum(gw(k) for k in cur + ch) - spax > w:
|
||||
lines.append(cur); cur = ""
|
||||
cur += ch
|
||||
lines.append(cur)
|
||||
line_h = font.height + spay
|
||||
total_h = len(lines) * line_h - spay
|
||||
if ycen == 1:
|
||||
ty = y + max(0, (h - total_h) // 2) # never starts above the box
|
||||
elif ycen == 2:
|
||||
ty = y + max(0, h - total_h)
|
||||
else:
|
||||
ty = y
|
||||
px = img.load()
|
||||
W, H = img.size
|
||||
# the Editor aligns the text *block* (widest line); lines are left-aligned inside it
|
||||
block_w = max((sum(gw(k) for k in ln) - (spax if ln else 0)) for ln in lines)
|
||||
for ln in lines:
|
||||
lw = block_w
|
||||
if xcen == 1:
|
||||
tx = x + max(0, (w - lw) // 2)
|
||||
elif xcen == 2:
|
||||
tx = x + max(0, w - lw)
|
||||
else:
|
||||
tx = x
|
||||
for ch in ln:
|
||||
g = glyphs.get(ord(ch))
|
||||
if g:
|
||||
for gy, row in enumerate(g.pixels):
|
||||
yy = ty + gy
|
||||
if yy < y or yy >= y + h or yy >= H:
|
||||
continue
|
||||
for gx, v in enumerate(row):
|
||||
xx = tx + gx
|
||||
if v and x <= xx < x + w and 0 <= xx < W and yy >= 0:
|
||||
px[xx, yy] = color
|
||||
tx += gw(ch)
|
||||
ty += line_h
|
||||
|
||||
def dual(self, img, c):
|
||||
"""Dual-state button: val=1 shows the second skin (bco2 / pic2 / picc2) and pco2."""
|
||||
x, y, w, h = self._box(c)
|
||||
on = bool(c.get("val"))
|
||||
sta = c.get("sta")
|
||||
if sta == 1:
|
||||
img.paste(rgb((c.get("bco2") if on else c.get("bco")) or 0), (x, y, x + w, y + h))
|
||||
elif sta == 2:
|
||||
src = self.pic(c.get("pic2") if on else c.get("pic"))
|
||||
if src:
|
||||
self._paste(img, src, 0, 0, w, h, x, y)
|
||||
elif sta == 0:
|
||||
src = self.pic(c.get("picc2") if on else c.get("picc"))
|
||||
if src:
|
||||
self._paste(img, src, x, y, w, h, x, y)
|
||||
if on and c.get("style") == 4 and sta == 1:
|
||||
self._border(img, c, x, y, w, h, override=2) # "on" looks pressed: 3D down
|
||||
else:
|
||||
self._border(img, c, x, y, w, h)
|
||||
self._text(img, c, c.get("txt") or "", *self._inner(c, x, y, w, h),
|
||||
rgb((c.get("pco2") if on else c.get("pco")) or 0))
|
||||
|
||||
def _inner(self, c, x, y, w, h):
|
||||
"""Text area inside the border."""
|
||||
style = c.get("style")
|
||||
b = (c.get("borderw") or 0) if style == 1 else (1 if style in (2, 3, 4) else 0)
|
||||
return x + b, y + b, w - 2 * b, h - 2 * b
|
||||
|
||||
def text(self, img, c):
|
||||
x, y, w, h = self._box(c)
|
||||
self._background(img, c, x, y, w, h)
|
||||
self._border(img, c, x, y, w, h)
|
||||
self._text(img, c, c.get("txt") or "", *self._inner(c, x, y, w, h), rgb(c.get("pco") or 0))
|
||||
|
||||
def number(self, img, c):
|
||||
v = c.get("val") or 0
|
||||
if v >= 0x80000000:
|
||||
v -= 1 << 32
|
||||
fmt, length = c.get("format"), c.get("lenth") or 0
|
||||
s = ("%X" % (v & 0xFFFFFFFF)) if fmt == 2 else ("{:,}".format(v) if fmt == 1 else str(v))
|
||||
if length and fmt != 1:
|
||||
s = s.zfill(length)[-length:]
|
||||
x, y, w, h = self._box(c)
|
||||
self._background(img, c, x, y, w, h)
|
||||
self._border(img, c, x, y, w, h)
|
||||
self._text(img, c, s, *self._inner(c, x, y, w, h), rgb(c.get("pco") or 0))
|
||||
|
||||
def xfloat(self, img, c):
|
||||
v = c.get("val") or 0
|
||||
if v >= 0x80000000:
|
||||
v -= 1 << 32
|
||||
ints, decs = c.get("vvs0") or 0, c.get("vvs1") or 0
|
||||
s = str(abs(v)).zfill(decs + 1)
|
||||
s = (s[:-decs] + "." + s[-decs:]) if decs else s
|
||||
if ints:
|
||||
ip, _, dp = s.partition(".")
|
||||
s = ip.zfill(ints)[-ints:] + ("." + dp if dp else "")
|
||||
s = ("-" if v < 0 else "") + s
|
||||
x, y, w, h = self._box(c)
|
||||
self._background(img, c, x, y, w, h)
|
||||
self._border(img, c, x, y, w, h)
|
||||
self._text(img, c, s, *self._inner(c, x, y, w, h), rgb(c.get("pco") or 0))
|
||||
|
||||
def picture(self, img, c):
|
||||
src = self.pic(c.get("pic"))
|
||||
if src:
|
||||
x, y, w, h = self._box(c)
|
||||
self._paste(img, src, 0, 0, w, h, x, y)
|
||||
|
||||
def crop(self, img, c):
|
||||
src = self.pic(c.get("picc"))
|
||||
if src:
|
||||
x, y, w, h = self._box(c)
|
||||
self._paste(img, src, x, y, w, h, x, y)
|
||||
|
||||
def slider(self, img, c):
|
||||
from PIL import ImageDraw
|
||||
x, y, w, h = self._box(c)
|
||||
self._background(img, c, x, y, w, h)
|
||||
d = ImageDraw.Draw(img)
|
||||
vmin, vmax, val = c.get("minval") or 0, c.get("maxval") or 100, c.get("val") or 0
|
||||
frac = 0 if vmax == vmin else (val - vmin) / (vmax - vmin)
|
||||
vertical = c.get("mode") == 1
|
||||
cw = c.get("wid"); ch = c.get("hig")
|
||||
if c.get("psta") == 1: # picture cursor (pic2) at its own size
|
||||
knob = self.pic(c.get("pic2"))
|
||||
if knob:
|
||||
kw, kh = knob.size
|
||||
if vertical:
|
||||
ky = y + h - kh - int(frac * (h - kh))
|
||||
img.paste(knob, (x + (w - kw) // 2, ky))
|
||||
else:
|
||||
kx = x + int(frac * (w - kw))
|
||||
img.paste(knob, (kx, y + (h - kh) // 2))
|
||||
return
|
||||
# "auto" (255) cursor = a square as thick as the slider
|
||||
if vertical:
|
||||
cw = w if cw in (None, 255) else cw
|
||||
ch = w if ch in (None, 255) else ch
|
||||
cy = y + h - ch - int(frac * (h - ch)) # truncates, like the Editor
|
||||
d.rectangle((x + (w - cw) // 2, cy, x + (w - cw) // 2 + cw - 1, cy + ch - 1), fill=rgb(c.get("pco") or 0))
|
||||
else:
|
||||
ch = h if ch in (None, 255) else ch
|
||||
cw = h if cw in (None, 255) else cw
|
||||
cx = x + int(frac * (w - cw)) # truncates, like the Editor
|
||||
d.rectangle((cx, y + (h - ch) // 2, cx + cw - 1, y + (h - ch) // 2 + ch - 1), fill=rgb(c.get("pco") or 0))
|
||||
|
||||
def progress(self, img, c):
|
||||
from PIL import ImageDraw
|
||||
x, y, w, h = self._box(c)
|
||||
d = ImageDraw.Draw(img)
|
||||
val = max(0, min(100, c.get("val") or 0))
|
||||
if c.get("sta") == 1: # picture mode: bpic background, ppic foreground
|
||||
bg, fg = self.pic(c.get("bpic")), self.pic(c.get("ppic"))
|
||||
if bg:
|
||||
self._paste(img, bg, 0, 0, w, h, x, y)
|
||||
if fg:
|
||||
if c.get("dez") == 1:
|
||||
fh = h * val // 100
|
||||
if fh:
|
||||
self._paste(img, fg, 0, h - fh, w, fh, x, y + h - fh)
|
||||
else:
|
||||
fw = w * val // 100
|
||||
if fw:
|
||||
self._paste(img, fg, 0, 0, fw, h, x, y)
|
||||
return
|
||||
d.rectangle((x, y, x + w - 1, y + h - 1), fill=rgb(c.get("bco") or 0))
|
||||
if c.get("dez") == 1:
|
||||
fh = h * val // 100
|
||||
if fh:
|
||||
d.rectangle((x, y + h - fh, x + w - 1, y + h - 1), fill=rgb(c.get("pco") or 0))
|
||||
else:
|
||||
fw = w * val // 100
|
||||
if fw:
|
||||
d.rectangle((x, y, x + fw - 1, y + h - 1), fill=rgb(c.get("pco") or 0))
|
||||
|
||||
def checkbox(self, img, c):
|
||||
from PIL import ImageDraw
|
||||
x, y, w, h = self._box(c)
|
||||
d = ImageDraw.Draw(img)
|
||||
d.rectangle((x, y, x + w - 1, y + h - 1), fill=rgb(c.get("bco") or 0))
|
||||
self._border(img, c, x, y, w, h)
|
||||
if c.get("val"): # filled square, half the size, centred
|
||||
d.rectangle((x + w // 4, y + h // 4, x + 3 * w // 4 - 1, y + 3 * h // 4 - 1), fill=rgb(c.get("pco") or 0))
|
||||
|
||||
def radio(self, img, c):
|
||||
from PIL import ImageDraw
|
||||
x, y, w, h = self._box(c)
|
||||
d = ImageDraw.Draw(img)
|
||||
d.ellipse((x, y, x + w - 2, y + h - 2), fill=rgb(c.get("bco") or 0), outline=(65, 68, 65))
|
||||
if c.get("val"):
|
||||
d.ellipse((x + w // 4, y + h // 4, x + 3 * w // 4 - 2, y + 3 * h // 4 - 2), fill=rgb(c.get("pco") or 0))
|
||||
|
||||
def gauge(self, img, c):
|
||||
from PIL import ImageDraw
|
||||
x, y, w, h = self._box(c)
|
||||
self._background(img, c, x, y, w, h)
|
||||
d = ImageDraw.Draw(img)
|
||||
r = min(w, h) // 2 - 1
|
||||
cx, cy = x + w // 2, y + h // 2
|
||||
a = math.radians(c.get("val") or 0)
|
||||
d.line((cx, cy, cx - int(r * math.cos(a)), cy - int(r * math.sin(a))), fill=rgb(c.get("pco") or 0),
|
||||
width=max(1, c.get("wid") or 1))
|
||||
|
||||
def waveform(self, img, c):
|
||||
from PIL import ImageDraw
|
||||
x, y, w, h = self._box(c)
|
||||
self._background(img, c, x, y, w, h)
|
||||
d = ImageDraw.Draw(img)
|
||||
gw, gh, gc = c.get("gdw") or 0, c.get("gdh") or 0, rgb(c.get("gdc") or 0)
|
||||
if gw:
|
||||
for xx in range(x, x + w, gw):
|
||||
d.line((xx, y, xx, y + h - 1), fill=gc)
|
||||
if gh: # horizontal lines count from the bottom
|
||||
for yy in range(y + h - 1, y - 1, -gh):
|
||||
d.line((x, yy, x + w - 1, yy), fill=gc)
|
||||
|
||||
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))
|
||||
Reference in new issue
Block a user