Fonts: anti-aliasing flag, overhanging glyphs, Font Creator byte 18
The Font Creator's anti-aliased fonts are 1-bit glyphs with header flag 0x1E (the simulator draws them with two colours). Map bytes 3/4 are extra columns left/right of the advance width; glyphs are drawn starting 'left' px early. Editor-generated fonts now round-trip byte for byte. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6616b47cb4
commit
55c2f9d6b6
6 files changed
+49
-12
No files matched your search
@@ -28,7 +28,7 @@ Every percentage is a count with a stated basis, so it can be checked (details i
|
||||
| Component types (read, write, create) | **100 %** (31/31) | all types in the Editor's toolbox, incl. 11 Intelligent-only |
|
||||
| Display series | **40 %** (2/5) | Basic (T) and Intelligent (P) confirmed; Discovery, Enhanced, Edge not sampled |
|
||||
| Picture formats | **100 %** (2/2) | raw RGB565 (Basic) and run-length + alpha (Intelligent), byte-identical to the Editor, every display direction |
|
||||
| Font modes | **50 %** (1/2) | black/white done (incl. TrueType import); anti-aliased not yet |
|
||||
| Font modes | **100 %** (2/2) | black/white and "anti-aliased" (a header flag; glyphs stay 1-bit, verified in the simulator); Editor fonts with overhanging glyphs round-trip byte-identical |
|
||||
| Readable attribute names in JSON | **100 %** (31/31 types) | every value list the Editor documents, incl. the Intelligent-only types and load effects |
|
||||
| Rendering, Basic components | **100 %** (20/20 pixel-exact) | incl. gauges and QR codes (own encoder, no dependencies) |
|
||||
| Rendering, all components | **94 %** (29/31 pixel-exact) | Intelligent ComboBox and Switch are drawn, but their anti-aliased edges are approximate (~99 % / ~92 % of pixels exact) |
|
||||
|
||||
+2
-2
@@ -14,7 +14,7 @@ byte for byte, or a file written by nxhmi was opened in the Editor and showed th
|
||||
| Components | **31 / 31 types** (20 Basic + 11 Intelligent-only) read/write/create |
|
||||
| Readable JSON (enum names, colours) | done for every value list the Editor documents |
|
||||
| Pictures | **complete**: format 0 (Basic) and format 4 (Intelligent, with alpha), all display directions |
|
||||
| Fonts | black/white **complete** incl. generation from TrueType; anti-aliased **missing** |
|
||||
| Fonts | **complete** for single-byte encodings: black/white and the anti-aliasing flag, overhanging glyphs, all display directions |
|
||||
| Rendering pages like the Editor | **pixel-identical** for every Basic component and 9 of 11 Intelligent-only ones; ComboBox and Switch approximate at anti-aliased edges |
|
||||
| New projects from scratch | `project.new_project(model)` for 12 Basic/Intelligent models; the generated demo is byte-identical to the Editor's re-save |
|
||||
| Compiling `.tft` firmware | **research only** -- not possible yet |
|
||||
@@ -94,7 +94,7 @@ byte for byte, or a file written by nxhmi was opened in the Editor and showed th
|
||||
| Item | Notes |
|
||||
|---|---|
|
||||
| Picture quality below 100 (Editor picture list "Q:") | not studied |
|
||||
| Anti-aliased fonts, multi-byte encodings (GB2312, Big5, ...) | only B/W single-byte fonts seen |
|
||||
| Multi-byte encodings (GB2312, Big5, ...) | only single-byte fonts studied |
|
||||
| Other display series | Discovery / Enhanced / Edge not sampled; only Basic + Intelligent templates |
|
||||
| Display direction | byte 12 decoded and written (`new_project(portrait=...)`); picture order verified for all four (format 4); font glyph order for 90° only |
|
||||
| Some header fields | main.HMI bytes 8, 13, 36, 0x41; page header 21/23; entry bytes 25-27 |
|
||||
|
||||
+9
-3
@@ -247,7 +247,14 @@ multi-byte range (2 bytes), code range first/last, u32 character count, u8 versi
|
||||
u8 name length (twice) + 0, u32 length of name+map+glyphs, u32 name offset = 0x2C, 16 unknown
|
||||
bytes. Then the name, the character map, the glyph data.
|
||||
|
||||
Character map: 10 bytes per character: u16 code, u8 glyph width, 2 unknown bytes,
|
||||
Header byte 0x1E = 1 when the Font Creator's "Anti-aliasing of fonts" box was ticked. Such fonts
|
||||
still hold 1-bit glyphs (Windows' thinner anti-aliased outline, thresholded); the Intelligent
|
||||
simulator draws them with two colours only. Byte 18 (second name length) is 0 in fresh Font
|
||||
Creator output and the name length after a re-save; both load.
|
||||
|
||||
Character map: 10 bytes per character: u16 code, u8 glyph advance width, u8 extra columns on the
|
||||
left, u8 extra columns on the right (glyphs that overhang, e.g. `j`, `f`, `W`; the stored glyph is
|
||||
advance + left + right wide and drawn starting `left` px before the pen position),
|
||||
u24 glyph offset (relative to the map start), u16 glyph length. In Intelligent projects the glyph
|
||||
data starts 4-byte aligned (zero padding after the map); Basic fonts have no padding.
|
||||
|
||||
@@ -269,8 +276,7 @@ converts a 90
|
||||
The Editor encodes greedily (transparent run + 1..4 ink if possible, else the 3+3-bit form, else a
|
||||
plain run); our encoder reproduces a 224-glyph Editor font byte for byte. The v3/v5 write-ups in
|
||||
github.com/hagronnestad/nextion-font-editor were a useful starting point; v6 differs (scan order,
|
||||
B/W codes `10`/`11`). **OPEN**: anti-aliased glyphs (other mode bytes), multi-byte encodings,
|
||||
the 16 unknown header bytes.
|
||||
B/W codes `10`/`11`). **OPEN**: multi-byte encodings, the remaining unknown header bytes.
|
||||
|
||||
## 6. Not yet covered
|
||||
|
||||
|
||||
+24
-5
@@ -50,8 +50,16 @@ class Glyph:
|
||||
code: int
|
||||
width: int
|
||||
pixels: list # rows top->bottom of 0/1 (len = height, each len = width)
|
||||
map_extra: bytes = b"\0\0" # map bytes 3..4
|
||||
mode: int = 1
|
||||
map_extra: bytes = b"\0\0" # map bytes 3..4: extra pixel columns beside the advance width
|
||||
mode: int = 1 # (overhanging glyphs such as 'j', 'f'); `pixels` include them
|
||||
|
||||
@property
|
||||
def left(self) -> int:
|
||||
return self.map_extra[0] if self.map_extra else 0
|
||||
|
||||
@property
|
||||
def data_width(self) -> int:
|
||||
return self.width + sum(self.map_extra[:2]) if self.map_extra else self.width
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -67,6 +75,13 @@ class Font:
|
||||
direction: int = 1 # display direction the glyphs are stored for (header byte 3 = 0x0A + d)
|
||||
align: bool = False # Intelligent: glyph data starts 4-byte aligned (zero padding after the map)
|
||||
|
||||
@property
|
||||
def antialias(self) -> bool:
|
||||
"""Header byte 0x1E: set when the Font Creator's "Anti-aliasing of fonts" box was ticked.
|
||||
The glyphs are still 1-bit (a thinner rasterisation); the simulator draws them without
|
||||
grey levels, so rendering needs nothing special."""
|
||||
return len(self.header) > 0x1E and self.header[0x1E] == 1
|
||||
|
||||
# ------------------------------------------------------------------ decode
|
||||
@classmethod
|
||||
def parse(cls, blob: bytes) -> "Font":
|
||||
@@ -90,13 +105,14 @@ class Font:
|
||||
off = e[5] | e[6] << 8 | e[7] << 16
|
||||
ln, = struct.unpack_from("<H", e, 8)
|
||||
data = blob[m + off: m + off + ln]
|
||||
f.glyphs.append(Glyph(code, gw, decode_glyph(data, gw, h, d), bytes(e[3:5]), data[0]))
|
||||
dw = gw + e[3] + e[4]
|
||||
f.glyphs.append(Glyph(code, gw, decode_glyph(data, dw, h, d), bytes(e[3:5]), data[0]))
|
||||
return f
|
||||
|
||||
# ------------------------------------------------------------------ encode
|
||||
def pack(self) -> bytes:
|
||||
name = self.name.encode("latin-1")
|
||||
bodies = [bytes([g.mode]) + encode_glyph(g.pixels, g.width, self.height, self.direction)
|
||||
bodies = [bytes([g.mode]) + encode_glyph(g.pixels, g.data_width, self.height, self.direction)
|
||||
for g in self.glyphs]
|
||||
mp = bytearray()
|
||||
off = MAP_ENTRY * len(self.glyphs)
|
||||
@@ -112,7 +128,9 @@ class Font:
|
||||
struct.pack_into("<H", hdr, 4, self.encoding)
|
||||
hdr[6], hdr[7], hdr[10], hdr[11] = self.width, self.height, self.first, self.last
|
||||
struct.pack_into("<I", hdr, 12, len(self.glyphs))
|
||||
hdr[16], hdr[17], hdr[18] = 6, len(name), len(name)
|
||||
hdr[16], hdr[17] = 6, len(name)
|
||||
if not self.header or hdr[18]: # Font Creator output leaves byte 18 at 0
|
||||
hdr[18] = len(name)
|
||||
struct.pack_into("<II", hdr, 20, len(payload), HEADER_SIZE)
|
||||
return bytes(hdr) + payload
|
||||
|
||||
@@ -217,5 +235,6 @@ def from_ttf(path: str | None, height: int, encoding: str = "ascii", fixed_width
|
||||
hdr[0:4] = b"\x04\xff\x00\x0b"
|
||||
hdr[0x1C] = last
|
||||
hdr[0x20] = 0x0B
|
||||
hdr[18] = len(base[:255].encode("latin-1")) # name length twice, like the Editor re-saves it
|
||||
struct.pack_into("<I", hdr, 0x24, len(glyphs))
|
||||
return Font(base[:255], width, height, enc, first, last, glyphs, bytes(hdr))
|
||||
+3
-1
@@ -293,13 +293,14 @@ class Renderer:
|
||||
for ch in ln:
|
||||
g = glyphs.get(ord(ch))
|
||||
if g:
|
||||
ox = tx - g.left # columns overhanging on the left
|
||||
for gy, row in enumerate(g.pixels):
|
||||
yy = ty + gy
|
||||
if yy < max(y, cy0) or yy >= min(y + h, cy1) or yy >= H:
|
||||
continue
|
||||
col = shade(yy) if shade else color
|
||||
for gx, v in enumerate(row):
|
||||
xx = tx + gx
|
||||
xx = ox + gx
|
||||
if v and x <= xx < x + w and 0 <= xx < W and yy >= 0:
|
||||
px[xx, yy] = col
|
||||
tx += gw(ch)
|
||||
@@ -671,6 +672,7 @@ class Renderer:
|
||||
self.rgb(c.get("pco3") or 0), sdf)
|
||||
|
||||
|
||||
|
||||
def _radius(c, h):
|
||||
"""Corner radius of switch / combobox: dis % of h/2, whole pixels; below 3 px the simulator
|
||||
draws square corners (no anti-aliased edge either)."""
|
||||
|
||||
@@ -95,6 +95,16 @@ class Format4(unittest.TestCase):
|
||||
self.assertEqual(img.getpixel((1, 0))[3], 226) # 227 * 127 // 255 = 113 -> 226
|
||||
self.assertEqual(img.getpixel((2, 0))[3], 2)
|
||||
|
||||
def test_overhang_columns(self):
|
||||
# Font Creator fonts: map bytes 3/4 = extra columns left/right of the advance width
|
||||
rows = [[1, 0, 0, 1, 1]] * 2
|
||||
g = fonts.Glyph(ord("j"), 4, rows, bytes([1, 0]))
|
||||
f = fonts.Font("t", 4, 2, 1, 0x6A, 0x6A, [g])
|
||||
back = fonts.Font.parse(f.pack())
|
||||
self.assertEqual((back.glyphs[0].width, back.glyphs[0].data_width, back.glyphs[0].left), (4, 5, 1))
|
||||
self.assertEqual(back.glyphs[0].pixels, rows)
|
||||
self.assertFalse(back.antialias)
|
||||
|
||||
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
|
||||
def test_font_follows_project_direction(self):
|
||||
# an Intelligent landscape project (0 degrees): glyphs row by row, header byte 3 = 0x0A,
|
||||
|
||||
Reference in new issue
Block a user