diff --git a/README.md b/README.md index d6f618b..341bb5f 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/docs/COVERAGE.md b/docs/COVERAGE.md index 0839e76..0fb963c 100644 --- a/docs/COVERAGE.md +++ b/docs/COVERAGE.md @@ -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 | diff --git a/docs/FORMAT.md b/docs/FORMAT.md index 653dd28..13061f2 100644 --- a/docs/FORMAT.md +++ b/docs/FORMAT.md @@ -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 diff --git a/src/nxhmi/fonts.py b/src/nxhmi/fonts.py index d5a5648..e4bffa4 100644 --- a/src/nxhmi/fonts.py +++ b/src/nxhmi/fonts.py @@ -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(" 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("= 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).""" diff --git a/tests/test_media.py b/tests/test_media.py index f34b78d..c36d593 100644 --- a/tests/test_media.py +++ b/tests/test_media.py @@ -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,