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
+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)."""
|
||||
|
||||
Reference in new issue
Block a user