mirror of
https://github.com/isod89/clipper-fm1.git
synced 2026-12-05 12:24:32 +01:00
Screen font at 1 bit a pixel (5376 bytes instead of 21504), same pixels (tests/font_test.c)
This commit is contained in:
5 files changed
+126
-9
No files matched your search
+14
-1
@@ -12,6 +12,8 @@ typedef struct { /* proportional, see tools/gen_font.py */
|
||||
const uint8_t *bw; /* bitmap width per glyph (starts FONT_PAD left of the pen) */
|
||||
const uint16_t *off; /* byte offset of each glyph */
|
||||
const uint8_t *data;
|
||||
uint8_t bits; /* 1 = 1 bit a pixel (a pixel font: glyph rows one after the other, high
|
||||
* bit first, each glyph from a whole byte); 0 = 2 pixels a byte, alpha 0..15 */
|
||||
} felucca_font_t;
|
||||
#include "felucca_font.h"
|
||||
|
||||
@@ -143,8 +145,19 @@ static int32_t cv_text(int32_t x, int32_t y, const felucca_font_t *f, const char
|
||||
uint32_t gi = glyph(f, (uint8_t)*s), gx, gy, w, bpr;
|
||||
const uint8_t *gd;
|
||||
w = f->bw[gi];
|
||||
bpr = ((w >> f->sh) + 1u) / 2u;
|
||||
gd = f->data + f->off[gi];
|
||||
if (f->bits == 1u) { /* on / off pixels: full colour where a bit is set */
|
||||
uint32_t sw = w >> f->sh;
|
||||
for (gy = 0; gy < f->h; gy++)
|
||||
for (gx = 0; gx < w; gx++) {
|
||||
uint32_t i = (gy >> f->sh) * sw + (gx >> f->sh);
|
||||
if ((gd[i >> 3] >> (7u - (i & 7u))) & 1u)
|
||||
cv_pset(x - f->pad + (int32_t)gx, y + (int32_t)gy, ramp[15]);
|
||||
}
|
||||
x += f->adv[gi];
|
||||
continue;
|
||||
}
|
||||
bpr = ((w >> f->sh) + 1u) / 2u;
|
||||
for (gy = 0; gy < f->h; gy++)
|
||||
for (gx = 0; gx < w; gx++) {
|
||||
uint32_t sx = gx >> f->sh, v = gd[(gy >> f->sh) * bpr + sx / 2u];
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-only */
|
||||
/* The screen font (gfx.c cv_text, tools/gen_font.py): every glyph of FONT_S (1x, Latin-1) and FONT_L (2x) drawn in
|
||||
* several colours, at several offsets (clipped at every edge of the canvas, and with a graph y offset), hashed.
|
||||
* The firmware stores the Terminus bitmaps at 1 bit a pixel (5376 bytes instead of 21504): the expected hashes are
|
||||
* those of the 4-bit renderer before, so the screen shows exactly the same pixels. Also checked: the advance and
|
||||
* bitmap widths (text_w, the layout), and the data size. Exit status: the number of failed checks. */
|
||||
#include <stdint.h>
|
||||
#define FONT_S_HASH 0xca4fe04eu /* the renderer before (4-bit data) */
|
||||
#define FONT_L_HASH 0xd0230e39u
|
||||
#define FONT_W_HASH 0x6840d800u
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
static void lcd_sync(void) {}
|
||||
static void lcd_blit(uint32_t x, uint32_t y, uint32_t w, uint32_t h, const uint16_t *p) { (void)x; (void)y; (void)w; (void)h; (void)p; }
|
||||
#include "../firmware/src/gfx.c"
|
||||
static int bad;
|
||||
static void check(int ok, const char *what) { printf("font: %-70s %s\n", what, ok ? "ok" : "FAIL"); bad += !ok; }
|
||||
static uint32_t hash_cv(uint32_t h)
|
||||
{
|
||||
uint32_t i;
|
||||
for (i = 0; i < cv_w * cv_h; i++)
|
||||
h = (h ^ cv_px[i]) * 16777619u;
|
||||
return h;
|
||||
}
|
||||
static uint32_t render_all(const felucca_font_t *f, uint32_t ch_hi)
|
||||
{
|
||||
static const uint16_t COL[4] = {0xFFFFu, 0x07E0u, 0xF81Fu, 0x39E7u};
|
||||
static const int32_t XY[6][3] = {{0, 0, 0}, {5, 3, 0}, {-3, 0, 0}, {50, -5, 0}, {52, 30, 0}, {10, 4, -9}};
|
||||
uint32_t h = 0x811C9DC5u, c, k, i;
|
||||
char s[2] = {0, 0};
|
||||
for (c = f->first; c <= ch_hi; c++)
|
||||
for (k = 0; k < 4u; k++)
|
||||
for (i = 0; i < 6u; i++) {
|
||||
cv_begin(64, 48, 0);
|
||||
cv_oy = XY[i][2];
|
||||
s[0] = (char)c;
|
||||
cv_text(XY[i][0], XY[i][1], f, s, COL[k]);
|
||||
cv_oy = 0;
|
||||
h = hash_cv(h);
|
||||
}
|
||||
cv_begin(240, 40, 0); /* a line of text, as the pages draw it */
|
||||
cv_text(2, 2, &FONT_S, "SLOOP 2.5 HUGELTON BPM 96 -12.5 dB", 0xFFFFu);
|
||||
cv_text(2, 18, &FONT_S, "\xdc\xe9\xb0\xb1 kick snare", 0x07E0u);
|
||||
return hash_cv(h);
|
||||
}
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
uint32_t hs = render_all(&FONT_S, 255u), hl = render_all(&FONT_L, 95u), wsum = 0, c;
|
||||
for (c = 0; c < 224u; c++) wsum = wsum * 31u + FONT_S.adv[c] * 7u + FONT_S.bw[c];
|
||||
for (c = 0; c < 64u; c++) wsum = wsum * 31u + FONT_L.adv[c] * 7u + FONT_L.bw[c];
|
||||
if (argc > 1) {
|
||||
printf("%08x %08x %08x %u\n", hs, hl, wsum, (unsigned)sizeof FONT_S_DATA);
|
||||
return 0;
|
||||
}
|
||||
check(hs == FONT_S_HASH, "FONT_S: every glyph, 4 colours, 6 places (clipped): the same pixels as before");
|
||||
check(hl == FONT_L_HASH, "FONT_L (2x): every glyph, 4 colours, 6 places (clipped): the same pixels as before");
|
||||
check(wsum == FONT_W_HASH, "advance and bitmap widths unchanged (the layout)");
|
||||
{ /* the 4-bit path (anti-aliased fonts) still draws: FONT_S unpacked to 2 pixels a byte, the same pixels */
|
||||
static uint8_t d4[21504];
|
||||
static uint16_t off4[224];
|
||||
felucca_font_t s4 = FONT_S, l4 = FONT_L;
|
||||
uint32_t g, n = 0, y, xx;
|
||||
for (g = 0; g < 224u; g++) {
|
||||
uint32_t w = FONT_S.bw[g], bpr = (w + 1u) / 2u;
|
||||
off4[g] = (uint16_t)n;
|
||||
for (y = 0; y < FONT_S.h; y++)
|
||||
for (xx = 0; xx < w; xx++) {
|
||||
uint32_t i = y * w + xx, on = (FONT_S_DATA[FONT_S_OFF[g] + i / 8u] >> (7u - i % 8u)) & 1u;
|
||||
if (on)
|
||||
d4[n + y * bpr + xx / 2u] |= (uint8_t)(xx & 1u ? 15u : 15u << 4);
|
||||
}
|
||||
n += FONT_S.h * bpr;
|
||||
}
|
||||
s4.data = l4.data = d4; s4.off = l4.off = off4; s4.bits = l4.bits = 0;
|
||||
check(n == 21504u && render_all(&s4, 255u) == FONT_S_HASH && render_all(&l4, 95u) == FONT_L_HASH,
|
||||
"the 4-bit format (anti-aliased fonts) still draws the same pixels");
|
||||
}
|
||||
check(sizeof FONT_S_DATA == 5376u && FONT_S.bits == 1u, "the Terminus bitmaps at 1 bit a pixel: 5376 bytes (21504 at 4 bits)");
|
||||
printf("%s\n", bad ? "FONT TEST FAILED" : "font test passed");
|
||||
return bad;
|
||||
}
|
||||
@@ -88,6 +88,8 @@ run "no divide by zero (UBSan): UI fuzz, sequencer, projects, a minute of live u
|
||||
|
||||
$CC -O2 -w -Ibuild/gen -Ifirmware/src -o "$OUT/soak_test" tests/soak_test.c -lm
|
||||
run "soak: ${SOAK_MIN:-10} minutes of random live use (bounded, no hanging voices, idle after stop)" "$OUT/soak_test" "${SOAK_MIN:-10}"
|
||||
$CC -O2 -w -Ibuild/gen -Ifirmware/src -o "$OUT/font_test" tests/font_test.c
|
||||
run "the screen font at 1 bit a pixel: every glyph, the same pixels as before" "$OUT/font_test"
|
||||
$CC -O2 -w -Ibuild/gen -Ifirmware/src -Ifirmware/hal -o "$OUT/stress_test" tests/stress_test.c -lm
|
||||
run "stress (2.4): FONT_L = FONT_S at 2x, hard random use of every 2.4 addition, clean stop" "$OUT/stress_test" "${STRESS_FRAMES:-40000}"
|
||||
# the same under AddressSanitizer + UBSan: any read or write out of bounds stops it (left shifts of negative values
|
||||
|
||||
+9
-2
@@ -40,8 +40,14 @@ static void font_ref_build(void)
|
||||
big_off[gi] = (uint16_t)o;
|
||||
for (y = 0; y < 32u; y++)
|
||||
for (x = 0; x < wb; x++) {
|
||||
uint32_t sx = x / 2u, v = s[(y / 2u) * bs + sx / 2u];
|
||||
v = (sx & 1u) ? (v & 15u) : (v >> 4);
|
||||
uint32_t sx = x / 2u, v;
|
||||
if (FONT_S.bits == 1u) { /* (the font at 1 bit a pixel) */
|
||||
uint32_t i = (y / 2u) * ws + sx;
|
||||
v = (s[i >> 3] >> (7u - (i & 7u))) & 1u ? 15u : 0u;
|
||||
} else {
|
||||
v = s[(y / 2u) * bs + sx / 2u];
|
||||
v = (sx & 1u) ? (v & 15u) : (v >> 4);
|
||||
}
|
||||
if (x & 1u) big_data[o + y * bb + x / 2u] |= (uint8_t)v;
|
||||
else big_data[o + y * bb + x / 2u] = (uint8_t)(v << 4);
|
||||
}
|
||||
@@ -50,6 +56,7 @@ static void font_ref_build(void)
|
||||
}
|
||||
FONT_L2X = FONT_L;
|
||||
FONT_L2X.sh = 0;
|
||||
FONT_L2X.bits = 0; /* (the reference: 4-bit, stored at 2x) */
|
||||
FONT_L2X.off = big_off;
|
||||
FONT_L2X.data = big_data;
|
||||
}
|
||||
|
||||
+20
-6
@@ -9,8 +9,11 @@ Terminus 8x16 (BDF, SIL OFL 1.1). TTF fonts also work through render()
|
||||
|
||||
Glyph format: per glyph an advance width, a bitmap width and an offset; the
|
||||
bitmap starts FONT_PAD pixels left of the pen position (room for side
|
||||
bearings); rows top to bottom, 2 pixels per byte (high nibble first),
|
||||
alpha 0..15.
|
||||
bearings); rows top to bottom. A font whose pixels are all on or off (the
|
||||
Terminus pixel font) is stored at 1 bit a pixel (5376 bytes
|
||||
instead of 21504), the glyph's rows one after the other, high bit first, each
|
||||
glyph from a whole byte, and marked bits = 1; an anti-aliased one (TTF) stays at
|
||||
2 pixels per byte (high nibble first), alpha 0..15, bits = 0 (gfx.c cv_text).
|
||||
"""
|
||||
import sys
|
||||
from pathlib import Path
|
||||
@@ -126,7 +129,7 @@ def main(out):
|
||||
if scale > 1 and pixel and base is not None:
|
||||
# SLOOP 2.4: a pixel font at 2x is the 1x font's own bitmaps, enlarged while drawing (gfx.c
|
||||
# cv_text): no second copy in flash (24 KB). Same glyphs, same pixels: checked here.
|
||||
bname, bfirst, bh, bglyphs = base
|
||||
bname, bfirst, bh, bglyphs, bone = base
|
||||
assert scale == 2 and first == bfirst and h == bh * 2, "L must be S at 2x, same first glyph"
|
||||
for (adv, bw, g), (badv, bbw, bg) in zip(glyphs, bglyphs):
|
||||
assert adv == badv * 2 and bw == bbw * 2 and g == upscale(bg, bbw, bh, 2)
|
||||
@@ -135,14 +138,25 @@ def main(out):
|
||||
lines.append(f"static const uint8_t FONT_{name}_BW[{len(glyphs)}] = {{" +
|
||||
", ".join(str(b) for _, b, _ in glyphs) + "};")
|
||||
lines.append(f"static const felucca_font_t FONT_{name} = {{ {h}, {PAD * scale}, {first}, {last}, 1, "
|
||||
f"FONT_{name}_ADV, FONT_{name}_BW, FONT_{bname}_OFF, FONT_{bname}_DATA }};")
|
||||
f"FONT_{name}_ADV, FONT_{name}_BW, FONT_{bname}_OFF, FONT_{bname}_DATA, {1 if bone else 0} }};")
|
||||
lines.append("")
|
||||
print(f"font {name}: {ttf} {px}px h {h}, digit adv {glyphs[ord('0') - first][0]}, "
|
||||
f"the {bname} bitmaps at {scale}x (0 B)")
|
||||
continue
|
||||
data, offs = [], []
|
||||
one = all(v in (0, 15) for _, _, g in glyphs for v in g) # on / off only: 1 bit a pixel
|
||||
for adv, bw, g in glyphs:
|
||||
offs.append(len(data))
|
||||
if one:
|
||||
acc = 0
|
||||
for i, v in enumerate(g + [0] * (-len(g) % 8)):
|
||||
acc = (acc << 1) | (v != 0)
|
||||
if i % 8 == 7:
|
||||
data.append(acc)
|
||||
acc = 0
|
||||
unp = [15 if data[offs[-1] + i // 8] >> (7 - i % 8) & 1 else 0 for i in range(len(g))]
|
||||
assert unp == g, "1-bit packing must give the glyph back"
|
||||
continue
|
||||
for y in range(h):
|
||||
row = g[y * bw:(y + 1) * bw] + [0]
|
||||
for x in range(0, bw, 2):
|
||||
@@ -158,10 +172,10 @@ def main(out):
|
||||
lines.append(f"static const uint8_t FONT_{name}_BW[{len(glyphs)}] = {{" +
|
||||
", ".join(str(b) for _, b, _ in glyphs) + "};")
|
||||
lines.append(f"static const felucca_font_t FONT_{name} = {{ {h}, {PAD * scale}, {first}, {last}, 0, "
|
||||
f"FONT_{name}_ADV, FONT_{name}_BW, FONT_{name}_OFF, FONT_{name}_DATA }};")
|
||||
f"FONT_{name}_ADV, FONT_{name}_BW, FONT_{name}_OFF, FONT_{name}_DATA, {1 if one else 0} }};")
|
||||
lines.append("")
|
||||
if scale == 1:
|
||||
base = (name, first, h, glyphs)
|
||||
base = (name, first, h, glyphs, one)
|
||||
print(f"font {name}: {ttf} {px}px h {h}, digit adv {glyphs[ord('0') - first][0]}, {len(data)} B")
|
||||
Path(out).write_text("\n".join(lines))
|
||||
|
||||
|
||||
Reference in new issue
Block a user