2026-11-15 15:11:02 +01:00
|
|
|
"""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
|
|
|
|
|
|
|
|
|
|
|
2026-11-15 15:53:13 +01:00
|
|
|
def circle_octant(r: int):
|
|
|
|
|
"""First octant (x >= y >= 0) of the display's circle routine -- reproduces the Nextion `cir`
|
|
|
|
|
command and the Editor canvas exactly (verified for every radius 1..159)."""
|
|
|
|
|
pts = []
|
|
|
|
|
x, y = r, 0
|
|
|
|
|
while x >= y:
|
|
|
|
|
pts.append((x, y))
|
|
|
|
|
y += 1
|
|
|
|
|
if x * x - x + y * y + 2 * y > r * r + 1:
|
|
|
|
|
x -= 1
|
|
|
|
|
return pts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def circle_points(r: int):
|
|
|
|
|
"""All points of a ring of radius r around (0, 0) (8-way symmetric)."""
|
|
|
|
|
pts = set()
|
|
|
|
|
for x, y in circle_octant(r):
|
|
|
|
|
for a, b in ((x, y), (y, x)):
|
|
|
|
|
pts.update({(a, b), (-a, b), (a, -b), (-a, -b)})
|
|
|
|
|
return pts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def disc_rows(r: int):
|
|
|
|
|
"""{dy: half-width} of a filled circle (Nextion `cirs`): every pixel inside the ring."""
|
|
|
|
|
rows = {}
|
|
|
|
|
for a, b in circle_points(r):
|
|
|
|
|
rows[b] = max(rows.get(b, 0), abs(a))
|
|
|
|
|
return rows
|
|
|
|
|
|
|
|
|
|
|
2026-11-15 16:05:41 +01:00
|
|
|
def qsin(deg: int) -> float:
|
|
|
|
|
"""sin() as the display computes it: reduced to 0..90 degrees first, so sin(30) is a hair
|
|
|
|
|
under 0.5 and the truncated needle tip lands one pixel short on exact halves."""
|
|
|
|
|
deg %= 360
|
|
|
|
|
sign = 1
|
|
|
|
|
if deg >= 180:
|
|
|
|
|
deg, sign = deg - 180, -1
|
|
|
|
|
if deg > 90:
|
|
|
|
|
deg = 180 - deg
|
|
|
|
|
return sign * math.sin(math.radians(deg))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def needle_line(x0: int, y0: int, x1: int, y1: int):
|
|
|
|
|
"""Line from the tip (x0, y0) to the centre (x1, y1): step the major axis, minor offset
|
|
|
|
|
`(k * minor + minor - 1) // major` (the gauge needle routine)."""
|
|
|
|
|
dx, dy = abs(x1 - x0), abs(y1 - y0)
|
|
|
|
|
sx, sy = (1 if x1 >= x0 else -1), (1 if y1 >= y0 else -1)
|
|
|
|
|
if dx >= dy:
|
|
|
|
|
return [(x0 + sx * k, y0 + sy * ((k * dy + max(dy - 1, 0)) // dx if dx else 0)) for k in range(dx + 1)]
|
|
|
|
|
return [(x0 + sx * ((k * dx + max(dx - 1, 0)) // dy), y0 + sy * k) for k in range(dy + 1)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def gauge_needle(w: int, h: int, val: int, wid: int):
|
|
|
|
|
"""Pixels of a gauge needle relative to the component's top-left corner.
|
|
|
|
|
Centre (w//2, h//2); tip at length h//2 - wid; every pixel of that thin line is stamped
|
|
|
|
|
with a wid x wid square starting at -(wid//2). The needle may leave the component box."""
|
|
|
|
|
wid = max(1, wid)
|
|
|
|
|
cx, cy = w // 2, h // 2
|
|
|
|
|
r = h // 2 - wid
|
|
|
|
|
tip = (cx + int(-r * qsin(val + 90)), cy + int(-r * qsin(val)))
|
|
|
|
|
lo = -(wid // 2)
|
|
|
|
|
return {(x + ax, y + ay) for x, y in needle_line(*tip, cx, cy)
|
|
|
|
|
for ax in range(lo, lo + wid) for ay in range(lo, lo + wid)}
|
|
|
|
|
|
|
|
|
|
|
2026-11-15 16:17:23 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2026-11-15 15:11:02 +01:00
|
|
|
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:
|
2026-11-15 16:49:01 +01:00
|
|
|
part = src.crop((sx, sy, sx + w, sy + h))
|
|
|
|
|
# Intelligent pictures can carry alpha: blended over what is below (blend not yet
|
|
|
|
|
# compared with the Editor pixel by pixel)
|
|
|
|
|
img.paste(part, (dx, dy), part if part.mode == "RGBA" else None)
|
2026-11-15 15:11:02 +01:00
|
|
|
|
|
|
|
|
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):
|
2026-11-15 15:53:13 +01:00
|
|
|
"""Circle sized by the width, centred at ((w-1)//2, (w-1)//2), clipped to the box:
|
|
|
|
|
bco disc, ring (65,68,65), and when checked a pco dot of half the radius."""
|
2026-11-15 15:11:02 +01:00
|
|
|
x, y, w, h = self._box(c)
|
2026-11-15 15:53:13 +01:00
|
|
|
r = (w - 1) // 2
|
|
|
|
|
cx, cy = x + r, y + r
|
|
|
|
|
px = img.load()
|
|
|
|
|
W, H = img.size
|
|
|
|
|
|
|
|
|
|
def put(xx, yy, col):
|
|
|
|
|
if x <= xx < x + w and y <= yy < y + h and 0 <= xx < W and 0 <= yy < H:
|
|
|
|
|
px[xx, yy] = col
|
|
|
|
|
|
|
|
|
|
def fill(rad, col):
|
|
|
|
|
for dy, hw in disc_rows(rad).items():
|
|
|
|
|
for dx in range(-hw, hw + 1):
|
|
|
|
|
put(cx + dx, cy + dy, col)
|
|
|
|
|
|
|
|
|
|
fill(r, rgb(c.get("bco") or 0))
|
|
|
|
|
for dx, dy in circle_points(r):
|
|
|
|
|
put(cx + dx, cy + dy, (65, 68, 65))
|
2026-11-15 15:11:02 +01:00
|
|
|
if c.get("val"):
|
2026-11-15 15:53:13 +01:00
|
|
|
fill(r // 2, rgb(c.get("pco") or 0))
|
2026-11-15 15:11:02 +01:00
|
|
|
|
|
|
|
|
def gauge(self, img, c):
|
|
|
|
|
x, y, w, h = self._box(c)
|
|
|
|
|
self._background(img, c, x, y, w, h)
|
2026-11-15 16:05:41 +01:00
|
|
|
px, col = img.load(), rgb(c.get("pco") or 0)
|
|
|
|
|
for ax, ay in gauge_needle(w, h, c.get("val") or 0, c.get("wid") or 1):
|
|
|
|
|
if 0 <= x + ax < img.width and 0 <= y + ay < img.height:
|
|
|
|
|
px[x + ax, y + ay] = col
|
2026-11-15 15:11:02 +01:00
|
|
|
|
|
|
|
|
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)
|
2026-11-15 16:17:23 +01:00
|
|
|
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)
|