Phase 6 fix: 14/16-segment maps + geometry verbatim from C
The draft segment code used its own segment ids (h/i centre
verticals, j/k/l/m diagonals) and drew diagonals as full-length
round-capped strokes, so I/K/M/V/W were wrong and every glyph was
swamped by a dark X blob ("HELLO" unreadable in the showcase
screenshot). This mirrors the C widgets exactly: segment_map()
strings from InstSeg14/16Display5.h, C ids (i/l centre verticals,
h/j/k/m diagonals), C draw_segment geometry (inset bars, hh/hv
verticals) and C draw_diag polygons that stop short of the centre.
Adds per-segment probe tests: every 14-seg glyph vs C map, 16-seg
M/W clean forms, 7-seg 8 vs 1.
This commit is contained in:
1 parent
11c380df76
commit
6d2dae20a1
2 files changed
+254
-99
No files matched your search
+172
-99
@@ -1146,129 +1146,202 @@ def _draw_lamp_test_button(cr, widget):
|
||||
|
||||
# --- segment displays ----------------------------------------------------------
|
||||
|
||||
# Segment maps are verbatim from the C widgets
|
||||
# (InstSeg14Display5.h / InstSeg16Display5.h segment_map(),
|
||||
# InstDigitalReadout.h draw_digit seg[10][7]). C ids: a top,
|
||||
# b upper-right, c lower-right, d bottom, e lower-left,
|
||||
# f upper-left, g1/g2 middle halves, i upper centre vertical,
|
||||
# l lower centre vertical, h/j/k/m diagonals (top-left, top-right,
|
||||
# bottom-left, bottom-right). 16-segment splits a -> a1/a2 and
|
||||
# d -> d1/d2; its M ("b c e f h i j") and W ("b c e f k l m") are
|
||||
# the clean 16-seg forms. (An earlier draft used different ids for
|
||||
# the centre verticals/diagonals, which made I/K/M/V/W wrong and
|
||||
# drew the diagonals as full-length round-capped strokes -- a dark
|
||||
# X blob over every glyph. This block mirrors C exactly.)
|
||||
_SEVEN_MAP = {
|
||||
"0": "abcdef", "1": "bc", "2": "abged", "3": "abgcd",
|
||||
"4": "fgbc", "5": "afgcd", "6": "afgedc", "7": "abc",
|
||||
"0": "abcdef", "1": "bc", "2": "abdeg", "3": "abcdg",
|
||||
"4": "bcfg", "5": "acdfg", "6": "acdefg", "7": "abc",
|
||||
"8": "abcdefg", "9": "abcdfg", "-": "g", "_": "d", " ": "",
|
||||
}
|
||||
|
||||
# 14-segment: a top, b upper-right, c lower-right, d bottom,
|
||||
# e lower-left, f upper-left, g1/g2 middle halves, h/i centre
|
||||
# verticals, j/k/l/m diagonals (top-left, top-right, bottom-left,
|
||||
# bottom-right). 16-segment splits a -> a1/a2 and d -> d1/d2.
|
||||
_FOURTEEN_MAP = {
|
||||
"0": "abcdef", "1": "bc", "2": "abg1g2ed", "3": "abg1g2cd",
|
||||
"4": "fg1g2bc", "5": "afg1g2cd", "6": "afg1g2ecd",
|
||||
"7": "abc", "8": "abcdefg1g2", "9": "abcdfg1g2",
|
||||
"A": "abcfg1g2", "B": "abcdfg1g2hi", "C": "afed",
|
||||
"D": "abcdhi", "E": "afg1g2ed", "F": "afg1g2e",
|
||||
"G": "afg1g2cde", "H": "fg1g2bce", "I": "adhi",
|
||||
"J": "bcde", "K": "fg1hkl", "L": "fed", "M": "febchi",
|
||||
"N": "febcjm", "O": "abcdef", "P": "abfg1g2e",
|
||||
"Q": "abcdefm", "R": "abfg1g2em", "S": "afg1g2cd",
|
||||
"T": "ahi", "U": "febcd", "V": "lm", "W": "febci",
|
||||
"X": "jklm", "Y": "fg1g2bi", "Z": "akld",
|
||||
"-": "g1g2", "_": "d", "=": "g1g2d", "+": "g1g2hi",
|
||||
"/": "kl", " ": "",
|
||||
" ": "",
|
||||
'"': "h j",
|
||||
"'": "j",
|
||||
"+": "g1 g2 i l",
|
||||
"-": "g1 g2",
|
||||
"/": "j k",
|
||||
"0": "a b c d e f",
|
||||
"1": "b c",
|
||||
"2": "a b d e g1 g2",
|
||||
"3": "a b c d g1 g2",
|
||||
"4": "b c f g1 g2",
|
||||
"5": "a c d f g1 g2",
|
||||
"6": "a c d e f g1 g2",
|
||||
"7": "a b c",
|
||||
"8": "a b c d e f g1 g2",
|
||||
"9": "a b c d f g1 g2",
|
||||
"=": "d g1 g2",
|
||||
"A": "a b c e f g1 g2",
|
||||
"B": "a b c d g1 g2 i l",
|
||||
"C": "a d e f",
|
||||
"D": "a b c d i l",
|
||||
"E": "a d e f g1 g2",
|
||||
"F": "a e f g1 g2",
|
||||
"G": "a c d e f g1 g2",
|
||||
"H": "b c e f g1 g2",
|
||||
"I": "i l",
|
||||
"J": "b c d e",
|
||||
"K": "e f g1 j k",
|
||||
"L": "d e f",
|
||||
"M": "b c e f h j",
|
||||
"N": "b c e f h m",
|
||||
"O": "a b c d e f",
|
||||
"P": "a b e f g1 g2",
|
||||
"Q": "a b c d e f m",
|
||||
"R": "a b e f g1 g2 m",
|
||||
"S": "a c d f g1 g2",
|
||||
"T": "a i l",
|
||||
"U": "b c d e",
|
||||
"V": "c e j k",
|
||||
"W": "b c e f k m",
|
||||
"X": "h j k m",
|
||||
"Y": "b c f g1 g2 l",
|
||||
"Z": "a d j k",
|
||||
"_": "d",
|
||||
}
|
||||
_SIXTEEN_MAP = dict(_FOURTEEN_MAP)
|
||||
_SIXTEEN_MAP.update({"M": "febchi", "W": "febchi"})
|
||||
_SIXTEEN_MAP.update({
|
||||
"0": "a1 a2 b c d1 d2 e f",
|
||||
"2": "a1 a2 b d1 d2 e g1 g2",
|
||||
"3": "a1 a2 b c d1 d2 g1 g2",
|
||||
"5": "a1 a2 c d1 d2 f g1 g2",
|
||||
"6": "a1 a2 c d1 d2 e f g1 g2",
|
||||
"7": "a1 a2 b c",
|
||||
"8": "a1 a2 b c d1 d2 e f g1 g2",
|
||||
"9": "a1 a2 b c d1 d2 f g1 g2",
|
||||
"=": "d1 d2 g1 g2",
|
||||
"A": "a1 a2 b c e f g1 g2",
|
||||
"B": "a1 a2 b c d1 d2 g1 g2 i l",
|
||||
"C": "a1 a2 d1 d2 e f",
|
||||
"D": "a1 a2 b c d1 d2 i l",
|
||||
"E": "a1 a2 d1 d2 e f g1 g2",
|
||||
"F": "a1 a2 e f g1 g2",
|
||||
"G": "a1 a2 c d1 d2 e f g1 g2",
|
||||
"J": "b c d1 d2 e",
|
||||
"L": "d1 d2 e f",
|
||||
"M": "b c e f h i j",
|
||||
"O": "a1 a2 b c d1 d2 e f",
|
||||
"P": "a1 a2 b e f g1 g2",
|
||||
"Q": "a1 a2 b c d1 d2 e f m",
|
||||
"R": "a1 a2 b e f g1 g2 m",
|
||||
"S": "a1 a2 c d1 d2 f g1 g2",
|
||||
"T": "a1 a2 i l",
|
||||
"U": "b c d1 d2 e",
|
||||
"W": "b c e f k l m",
|
||||
"Z": "a1 a2 d1 d2 j k",
|
||||
"_": "d1 d2",
|
||||
})
|
||||
|
||||
|
||||
def _seg_thickness(w, h):
|
||||
return max(2.0, min(w, h) / 7.0)
|
||||
def _seg_thickness(w):
|
||||
"""C: t = max(2, cell_w / 6) (Seg14 draw); the 7-seg readout
|
||||
uses a fixed t = 8 at its own cell size, the same scale."""
|
||||
return max(2.0, w / 6.0)
|
||||
|
||||
|
||||
def _seg_rects(x, y, w, h, sixteen=False):
|
||||
"""Segment name -> rect (or line endpoints for diagonals)."""
|
||||
t = _seg_thickness(w, h)
|
||||
mid = y + h / 2
|
||||
cx = x + w / 2
|
||||
def _seg_rects(x, y, w, h, segs):
|
||||
"""Segment name -> rect, C draw_segment() geometry verbatim
|
||||
(InstSeg14Display5.h; DigitalReadout.h for the 7-seg set).
|
||||
Diagonals are returned separately as endpoint pairs: C draws
|
||||
them as polygons (draw_diag), not stroked lines, so they stop
|
||||
short of the centre instead of bulging into a blob."""
|
||||
t = _seg_thickness(w)
|
||||
midx = x + w / 2
|
||||
midy = y + h / 2
|
||||
if segs == "seven":
|
||||
hv = max(1.0, h / 2 - 2 * t)
|
||||
return {
|
||||
"a": (x + t, y, w - 2 * t, t),
|
||||
"b": (x + w - t, y + t, t, hv),
|
||||
"c": (x + w - t, y + h / 2 + t, t, hv),
|
||||
"d": (x + t, y + h - t, w - 2 * t, t),
|
||||
"e": (x, y + h / 2 + t, t, hv),
|
||||
"f": (x, y + t, t, hv),
|
||||
"g": (x + t, y + h / 2 - t / 2, w - 2 * t, t),
|
||||
}, {}
|
||||
hh = max(1.0, h / 2 - t)
|
||||
hw = max(1.0, w / 2 - t)
|
||||
cv = max(1.0, h / 2 - 2 * t)
|
||||
rects = {
|
||||
"a": (x + 2, y, w - 4, t),
|
||||
"b": (x + w - t, y + 2, t, h / 2 - 4),
|
||||
"c": (x + w - t, mid + 2, t, h / 2 - 4),
|
||||
"d": (x + 2, y + h - t, w - 4, t),
|
||||
"e": (x, mid + 2, t, h / 2 - 4),
|
||||
"f": (x, y + 2, t, h / 2 - 4),
|
||||
"g": (x + 2, mid - t / 2, w - 4, t),
|
||||
"g1": (x + 2, mid - t / 2, w / 2 - 3, t),
|
||||
"g2": (cx + 1, mid - t / 2, w / 2 - 3, t),
|
||||
"h": (cx - t / 2, y + 2, t, h / 2 - 4),
|
||||
"i": (cx - t / 2, mid + 2, t, h / 2 - 4),
|
||||
"a": (x + t, y, w - 2 * t, t),
|
||||
"b": (x + w - t, y + t, t, hh),
|
||||
"c": (x + w - t, midy, t, hh),
|
||||
"d": (x + t, y + h - t, w - 2 * t, t),
|
||||
"e": (x, midy, t, hh),
|
||||
"f": (x, y + t, t, hh),
|
||||
"g1": (x + t, midy - t / 2, hw, t),
|
||||
"g2": (midx, midy - t / 2, hw, t),
|
||||
"i": (midx - t / 2, y + t, t, cv),
|
||||
"l": (midx - t / 2, midy + t, t, cv),
|
||||
}
|
||||
if sixteen:
|
||||
rects["a1"] = (x + 2, y, w / 2 - 3, t)
|
||||
rects["a2"] = (cx + 1, y, w / 2 - 3, t)
|
||||
rects["d1"] = (x + 2, y + h - t, w / 2 - 3, t)
|
||||
rects["d2"] = (cx + 1, y + h - t, w / 2 - 3, t)
|
||||
if segs == "sixteen":
|
||||
rects["a1"] = (x + t, y, hw, t)
|
||||
rects["a2"] = (midx, y, hw, t)
|
||||
rects["d1"] = (x + t, y + h - t, hw, t)
|
||||
rects["d2"] = (midx, y + h - t, hw, t)
|
||||
lines = {
|
||||
"j": (x + 2, y + 2, cx, mid),
|
||||
"k": (x + w - 2, y + 2, cx, mid),
|
||||
"l": (x + 2, y + h - 2, cx, mid),
|
||||
"m": (x + w - 2, y + h - 2, cx, mid),
|
||||
"h": (x + t, y + t, midx, midy - t),
|
||||
"j": (x + w - t, y + t, midx, midy - t),
|
||||
"k": (x + t, y + h - t, midx, midy + t),
|
||||
"m": (x + w - t, y + h - t, midx, midy + t),
|
||||
}
|
||||
return rects, lines
|
||||
|
||||
|
||||
def _draw_seg_digit(cr, x, y, w, h, ch, on_rgb, off_rgb, sixteen=False):
|
||||
"""One 7/14/16-segment digit; off segments stay faintly visible."""
|
||||
if sixteen:
|
||||
raw = _SIXTEEN_MAP.get(ch.upper(), "")
|
||||
lit = set()
|
||||
i = 0
|
||||
while i < len(raw):
|
||||
if raw[i:i + 2] in ("g1", "g2"):
|
||||
lit.add(raw[i:i + 2])
|
||||
i += 2
|
||||
elif raw[i] == "a":
|
||||
lit.update(("a1", "a2"))
|
||||
i += 1
|
||||
elif raw[i] == "d":
|
||||
lit.update(("d1", "d2"))
|
||||
i += 1
|
||||
else:
|
||||
lit.add(raw[i])
|
||||
i += 1
|
||||
universe = ("a1", "a2", "b", "c", "d1", "d2", "e", "f", "g1",
|
||||
"g2", "h", "i", "j", "k", "l", "m")
|
||||
def _draw_diag(cr, x0, y0, x1, y1, t):
|
||||
"""C draw_diag: a t-wide parallelogram, not a stroked line."""
|
||||
dx, dy = x1 - x0, y1 - y0
|
||||
length = math.hypot(dx, dy)
|
||||
if length < 1.0:
|
||||
return
|
||||
px, py = -dy / length * t / 2.0, dx / length * t / 2.0
|
||||
cr.move_to(x0 + px, y0 + py)
|
||||
cr.line_to(x1 + px, y1 + py)
|
||||
cr.line_to(x1 - px, y1 - py)
|
||||
cr.line_to(x0 - px, y0 - py)
|
||||
cr.close_path()
|
||||
cr.fill()
|
||||
|
||||
|
||||
def _draw_seg_digit(cr, x, y, w, h, ch, on_rgb, off_rgb,
|
||||
segs="fourteen"):
|
||||
"""One 7/14/16-segment digit; off segments stay faintly visible.
|
||||
Maps and geometry are verbatim from the C widgets."""
|
||||
if segs == "seven":
|
||||
lit = set(_SEVEN_MAP.get(ch, ""))
|
||||
universe = ("a", "b", "c", "d", "e", "f", "g")
|
||||
elif segs == "sixteen":
|
||||
lit = set(_SIXTEEN_MAP.get(ch.upper(), "").split())
|
||||
universe = ("a1", "a2", "b", "c", "d1", "d2", "e", "f",
|
||||
"g1", "g2", "h", "i", "j", "k", "l", "m")
|
||||
else:
|
||||
raw = _FOURTEEN_MAP.get(ch.upper(), _SEVEN_MAP.get(ch, ""))
|
||||
lit = set()
|
||||
i = 0
|
||||
while i < len(raw):
|
||||
if raw[i:i + 2] in ("g1", "g2"):
|
||||
lit.add(raw[i:i + 2])
|
||||
i += 2
|
||||
else:
|
||||
lit.add(raw[i])
|
||||
i += 1
|
||||
if ch in _SEVEN_MAP and ch not in _FOURTEEN_MAP:
|
||||
lit = set(_SEVEN_MAP[ch])
|
||||
universe = ("a", "b", "c", "d", "e", "f", "g")
|
||||
else:
|
||||
universe = ("a", "b", "c", "d", "e", "f", "g1", "g2", "h",
|
||||
"i", "j", "k", "l", "m")
|
||||
rects, lines = _seg_rects(x, y, w, h, sixteen=sixteen)
|
||||
t = _seg_thickness(w, h)
|
||||
lit = set(_FOURTEEN_MAP.get(ch.upper(), "").split())
|
||||
universe = ("a", "b", "c", "d", "e", "f", "g1", "g2",
|
||||
"h", "i", "j", "k", "l", "m")
|
||||
rects, lines = _seg_rects(x, y, w, h, segs)
|
||||
t = _seg_thickness(w)
|
||||
for seg in universe:
|
||||
colour = on_rgb if seg in lit else off_rgb
|
||||
cr.save()
|
||||
_rgb(cr, colour)
|
||||
_rgb(cr, on_rgb if seg in lit else off_rgb)
|
||||
if seg in rects:
|
||||
rx, ry, rw, rh = rects[seg]
|
||||
cr.rectangle(rx, ry, rw, rh)
|
||||
cr.rectangle(rx, ry, max(1.0, rw), max(1.0, rh))
|
||||
cr.fill()
|
||||
else:
|
||||
x0, y0, x1, y1 = lines[seg]
|
||||
cr.set_line_width(t)
|
||||
cr.set_line_cap(1)
|
||||
cr.move_to(x0, y0)
|
||||
cr.line_to(x1, y1)
|
||||
cr.stroke()
|
||||
_draw_diag(cr, x0, y0, x1, y1, t)
|
||||
cr.restore()
|
||||
|
||||
|
||||
def _led_digits_text(value, digits, dp):
|
||||
"""Zero-padded digit string + decimal-point index (C shown_digits
|
||||
/ decimal_point_after). Returns (digits_str, point_after_index)
|
||||
@@ -1306,7 +1379,7 @@ def _draw_led_digits(cr, widget):
|
||||
for i, ch in enumerate(text):
|
||||
x = pad + i * (dw + gap)
|
||||
_draw_seg_digit(cr, x, 10, dw, dh, ch, lit, dim,
|
||||
sixteen=False)
|
||||
segs="seven")
|
||||
if i == point_after:
|
||||
cr.save()
|
||||
_rgb(cr, lit)
|
||||
@@ -1335,7 +1408,7 @@ def _draw_digital_readout(cr, widget):
|
||||
return
|
||||
for i, ch in enumerate(text):
|
||||
x = pad + i * (dw + gap)
|
||||
_draw_seg_digit(cr, x, 10, dw, dh, ch, on, off, sixteen=False)
|
||||
_draw_seg_digit(cr, x, 10, dw, dh, ch, on, off, segs="seven")
|
||||
if i == point_after:
|
||||
cr.save()
|
||||
_rgb(cr, on)
|
||||
@@ -1379,7 +1452,7 @@ def _draw_seg_display(cr, widget):
|
||||
for i, ch in enumerate(chars):
|
||||
x = pad + i * (dw + gap)
|
||||
_draw_seg_digit(cr, x, 10, dw, dh, ch, lit, dim,
|
||||
sixteen=sixteen)
|
||||
segs="sixteen" if sixteen else "fourteen")
|
||||
if i == point_after:
|
||||
cr.save()
|
||||
_rgb(cr, lit)
|
||||
|
||||
@@ -285,6 +285,88 @@ class Phase6DrawTests(unittest.TestCase):
|
||||
high = _count_colour(_draw(widget), (40, 120, 200))
|
||||
self.assertGreater(high, low * 3)
|
||||
|
||||
def test_seg14_glyphs_match_c_map(self):
|
||||
"""Each 14-seg glyph lights exactly C's segment set
|
||||
(InstSeg14Display5.h segment_map): probe every segment of
|
||||
a single digit and compare lit/dim against the map. This is
|
||||
the test that catches the wrong-ids / diagonal-blob class:
|
||||
I/K/M/V/W render differently from the draft's maps."""
|
||||
from lightgui.xframe.instruments import (
|
||||
_FOURTEEN_MAP, _seg_rects)
|
||||
universe = ("a", "b", "c", "d", "e", "f", "g1", "g2",
|
||||
"h", "i", "j", "k", "l", "m")
|
||||
for ch in "HELLOIMVWX\"'":
|
||||
widget = core.create("InstSeg14Display1",
|
||||
"Seg14_001", 0, 0, 90, 110)
|
||||
widget.set_prop("value", ch)
|
||||
surface = _draw(widget)
|
||||
rects, lines = _seg_rects(10, 10, 70, 90, "fourteen")
|
||||
expected = set(_FOURTEEN_MAP[ch].split())
|
||||
for seg in universe:
|
||||
if seg in rects:
|
||||
rx, ry, rw, rh = rects[seg]
|
||||
px, py = int(rx + rw / 2), int(ry + rh / 2)
|
||||
else:
|
||||
x0, y0, x1, y1 = lines[seg]
|
||||
px, py = int((x0 + x1) / 2), int((y0 + y1) / 2)
|
||||
r, g, b = _pixel(surface, px, py)
|
||||
lit = (abs(r - 255) <= 40 and abs(g - 176) <= 40
|
||||
and b <= 40)
|
||||
self.assertEqual(lit, seg in expected,
|
||||
"%r seg %s at (%d,%d) = (%d,%d,%d)"
|
||||
% (ch, seg, px, py, r, g, b))
|
||||
|
||||
def test_seg16_mw_clean(self):
|
||||
"""16-seg M/W use the split forms (C InstSeg16Display5.h):
|
||||
M lights the top diagonals + centre vertical, W the bottom
|
||||
diagonals + lower centre vertical."""
|
||||
from lightgui.xframe.instruments import (
|
||||
_SIXTEEN_MAP, _seg_rects)
|
||||
universe = ("a1", "a2", "b", "c", "d1", "d2", "e", "f",
|
||||
"g1", "g2", "h", "i", "j", "k", "l", "m")
|
||||
for ch, must_have in (("M", {"h", "i", "j"}),
|
||||
("W", {"k", "l", "m"})):
|
||||
widget = core.create("InstSeg16Display1",
|
||||
"Seg16_001", 0, 0, 90, 110)
|
||||
widget.set_prop("value", ch)
|
||||
surface = _draw(widget)
|
||||
rects, lines = _seg_rects(10, 10, 70, 90, "sixteen")
|
||||
expected = set(_SIXTEEN_MAP[ch].split())
|
||||
self.assertTrue(must_have <= expected, (ch, expected))
|
||||
for seg in must_have:
|
||||
if seg in rects:
|
||||
rx, ry, rw, rh = rects[seg]
|
||||
px, py = int(rx + rw / 2), int(ry + rh / 2)
|
||||
else:
|
||||
x0, y0, x1, y1 = lines[seg]
|
||||
px, py = int((x0 + x1) / 2), int((y0 + y1) / 2)
|
||||
r, g, b = _pixel(surface, px, py)
|
||||
self.assertTrue(
|
||||
abs(r - 255) <= 40 and abs(g - 176) <= 40
|
||||
and b <= 40,
|
||||
"%r seg %s should be lit, got (%d,%d,%d)"
|
||||
% (ch, seg, r, g, b))
|
||||
|
||||
def test_seven_seg_8_vs_1(self):
|
||||
"""7-seg: 8 lights all segments, 1 only b+c (C
|
||||
DigitalReadout seg[10][7])."""
|
||||
from lightgui.xframe.instruments import _seg_rects
|
||||
for ch, lit_segs in (("8", set("abcdefg")), ("1", {"b", "c"})):
|
||||
widget = core.create("InstLedDigits1",
|
||||
"Led1_001", 0, 0, 90, 110)
|
||||
widget.set_prop("value", ch)
|
||||
surface = _draw(widget)
|
||||
rects, _lines = _seg_rects(10, 10, 70, 90, "seven")
|
||||
for seg in "abcdefg":
|
||||
rx, ry, rw, rh = rects[seg]
|
||||
px, py = int(rx + rw / 2), int(ry + rh / 2)
|
||||
r, g, b = _pixel(surface, px, py)
|
||||
lit = (abs(r - 255) <= 40 and abs(g - 60) <= 40
|
||||
and abs(b - 35) <= 40)
|
||||
self.assertEqual(lit, seg in lit_segs,
|
||||
"%r seg %s = (%d,%d,%d)"
|
||||
% (ch, seg, r, g, b))
|
||||
|
||||
def test_gauge_lamp_segments_render(self):
|
||||
widget = core.create("InstAnalogGauge", "InstAnalogGauge_001",
|
||||
0, 0, 200, 200)
|
||||
|
||||
Reference in new issue
Block a user