Files
skiff/tests/text_ref_test.c
T
illuminus5 32828c4aa5 Skiff 2.0: NOTES and FEEL, chains, sounds on song entries, drum lanes panned, your own drum kits
- The step pages, four become two: NOTES (the piano roll, a selection edited as one, notes that overlap with lengths
  of their own, copy, paste and delete) and FEEL (chance and conditions, ratchets, nudge, accent and slide).
- SONG: chains (entries on CHAIN played and edited as one long pattern), NEW, COPY+, copies numbered beside their
  originals, SONG ONCE, the pool's EMPTY and UNUSED, and a sound on any entry, loaded as the track comes onto it.
- DRUM and DRUM-X: LANE MIX, each lane's level and pan. DRUM's KIT on to USR1-3: a sample slot as the kit, built in
  the web editor's Samples tab, a WAV a lane.
- One mute in the GLO, FX and PERFORM layers, one RETURN, one MOD page, DIST through INSERT's shapers.
- A user sound stays the track's over a save, a load and power-on; RESTORE LAST writes 2 s after the last change.
- New save formats (projects, settings, user presets, parameters by stored id) with a converter in the installer and
  the editor's Restore (Felucca 1.2+ projects and presets too); about 30 KB freed on the FM-1; release builds without
  the serial console.
- The SMK-37 Pro: the sustain pedal holds the keys; its loaders start the flash at the app's speed.
- Fixes: REC on an armed track disarms every track, RANDOM on a lengthened line, PD BRASS's clicks (#41), settings
  from Felucca 1.4+ or JIANT.
2026-10-24 18:56:17 -05:00

172 lines
8.1 KiB
C

/* SPDX-License-Identifier: GPL-3.0-only
* Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments */
/* The firmware's text (src/gfx.c cv_text) against the reference text path of the UI-redesign prototype that
* drew the UI design screens (its aa_text / aa_blit4 / ramp_make,
* restated below): pen in 1/16 px accumulated over the string, pair kerning by binary search, no extra tracking,
* the 16-entry ramp with the light / dark coverage curve. Glyph placement has since moved on from the prototype's
* (the glyph at the rounded pen) to horizontal phases (tools/aa_raster.py): of the two phase positions around
* the pen, the one whose offset is nearer the previous glyph's, plus that phase's ink offset; a glyph with one
* phase (figures, .) at the rounded pen as before; restated here too.
* How close that lands to the font's own spacing is tests/text_spacing_test.py's question. Every string below, in S M L, THEME on SURF and TEXT on BG, in every palette: the end pen and every
* pixel must be identical (GREY expands its gray with G = 2 R, the gray rule; the prototype's G = 2 R + R / 16
* differed by one green LSB there, and that is the only difference to the mock renders). */
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#define __attribute__(x)
static void lcd_sync(void) {}
static void lcd_power(uint32_t s) { (void)s; } /* (MENU > SCREEN OFF: lcd.c) */
static void lcd_wake_now(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 const char *const STR[] = {
"CUTOFF", "RESO", "ATK", "DEC", "LEVEL", "PAN", "REV", "MUTE", "STEP", "LANE", "HIT", "ACC", "No.",
"124", "1234.5", "-12", "+3", "0.87", "100 %", "/32", "C#4", "ANALOG", "DIGITAL", "OFF", "ON",
"OVERWRITE PROJECT A?", "OCT- NO", "OCT+ YES", "PRESETS", "MOVE", "BACK",
"AV AW LT TA YO To Vo", "KEYS = STEPS", "PAGE 1/2", "HARDWARE CALIBRATION", "Multi-engine synthesizer",
"H\xFCgelton Instruments", "FELUCCA", "MENU", "HOME", "ENV DEST 2/2", "ANLG", "SUPER SAW", "WIRE",
"oeo coco eco nono", "minimum hello level", "decay release sound", "Pitch Cutoff", "bdpq doob", "-1 +2.5",
};
#define NSTR (sizeof STR / sizeof STR[0])
#define W 240
#define H 40
static uint16_t ref[W * H];
/* --- the prototype's text path (gfx_aa.h), on a plain frame buffer --- */
static uint16_t rv[16];
static void ref_ramp(uint16_t fg, uint16_t bg)
{
static const uint8_t SH[3] = {11, 5, 0}, MK[3] = {31, 63, 31};
const uint8_t *cv = ux.light ? CURVE_LIGHT : CURVE_DARK;
for (uint32_t a = 0; a < 16u; a++) {
uint32_t out = 0;
for (uint32_t k = 0; k < 3u; k++) {
int32_t x = (bg >> SH[k]) & MK[k], y = (fg >> SH[k]) & MK[k];
out |= (uint32_t)(x + (((y - x) * (int32_t)cv[a] + 128) >> 8)) << SH[k];
}
rv[a] = (uint16_t)out;
}
}
static int32_t ref_at(const aafont_t *f, uint32_t ch) /* the glyph's number in the face, -1 = none */
{
if (ch >= f->first && ch <= f->last) return (int32_t)(ch - f->first);
for (uint32_t i = 0; i < f->nex; i++)
if (f->ex[i] == ch) return (int32_t)(f->last - f->first + 1 + i);
return -1;
}
static uint32_t ref_gi(const aafont_t *f, uint32_t ch) /* the glyph's number (its phases: g[n << psh] ..) */
{
int32_t k;
if (ch >= 'a' && ch <= 'z' && f->last < 'a') ch -= 32u;
k = ref_at(f, ch);
if (k < 0 && ch == 0x85u) k = ref_at(f, '.'); /* the ellipsis */
if (k < 0) k = ref_at(f, '?');
return k < 0 ? 0u : (uint32_t)k; /* a sparse face (L) without '?': its first glyph */
}
static int32_t ref_kern(const aafont_t *f, uint32_t a, uint32_t b)
{
uint32_t key = (a << 8) | b, lo = 0, hi = f->nk;
while (lo < hi) {
uint32_t m = (lo + hi) / 2u;
if (f->kkey[m] == key) return f->kd[m];
if (f->kkey[m] < key) lo = m + 1u; else hi = m;
}
return 0;
}
/* a glyph's alpha values: 2 px per byte, or (f->hc) the Huffman bit stream of tools/gen_aa_font.py, decoded
* here by matching each (length, code) against the canonical code table built from hc's length counts */
static uint8_t alpha[64 * 64];
static void ref_unpack(const aafont_t *f, const aag_t *g)
{
uint32_t n = (uint32_t)g->bw * g->bh, k = 0, pos = 0;
const uint8_t *d = f->data + g->off;
if (f->sc) { /* (Skiff 2.0) a pixel face: S's pixels, scaled, PX_BOLD */
uint32_t s = f->sc & 3u, b = f->sc >> 7, sw = (g->bw - b) / s;
for (uint32_t gy = 0; gy < g->bh; gy++)
for (uint32_t gx = 0; gx < g->bw; gx++, k++) {
uint32_t i = gy / s * sw + gx / s;
alpha[k] = (uint8_t)((gx / s < sw ? (d[i >> 1] >> ((i & 1u) ? 0 : 4)) & 15u : 0u) |
(b && gx ? (d[(i - 1u) >> 1] >> (((i - 1u) & 1u) ? 0 : 4)) & 15u : 0u));
}
return;
}
if (!f->hc) {
for (k = 0; k < n; k++) alpha[k] = (d[k >> 1] >> ((k & 1u) ? 0 : 4)) & 15u;
return;
}
while (k < n) {
uint32_t code = 0, len, s = 0, found = 0;
for (len = 1; len <= 15u && !found; len++) {
uint32_t c = 0, sym = 15, l;
code = code << 1 | ((d[pos >> 3] >> (7u - (pos & 7u))) & 1u);
pos++;
for (l = 1; l <= 15u && !found; l++, c <<= 1) /* canonical: codes of length l, in order */
for (uint32_t i = 0; i < f->hc[l - 1u]; i++, c++, sym++)
if (l == len && c == code) { s = f->hc[sym]; found = 1; break; }
}
for (uint32_t r = 0; r <= (s >> 4) && k < n; r++) alpha[k++] = (uint8_t)(s & 15u);
}
}
static int32_t ref_text(int32_t x, int32_t y, const aafont_t *f, const char *s, uint16_t fg, uint16_t bg)
{
int32_t pen = 0, err = 0, n = 1 << f->psh, step = 16 / n;
uint32_t prev = 0;
ref_ramp(fg, bg);
for (; *s; s++) {
uint32_t c = (uint8_t)*s, k = 0;
const aag_t *g;
int32_t lo, hi, pos;
if (c >= 'a' && c <= 'z' && f->last < 'a') c -= 32u;
if (prev) pen += ref_kern(f, prev, c);
g = &f->g[ref_gi(f, c) * (uint32_t)n];
if (n > 1 && g[1].off != g[0].off) {
lo = step * (int32_t)floor((double)pen / step); /* the phase positions around the pen */
hi = lo + step;
pos = fabs((double)(lo - pen - err)) <= fabs((double)(hi - pen - err)) ? lo : hi;
g += ((pos / step) % n + n) % n;
} else {
pos = 16 * (int32_t)floor((pen + 8) / 16.0); /* one phase: the rounded pen */
}
err = pos - pen;
ref_unpack(f, g);
for (uint32_t gy = 0; gy < g->bh; gy++)
for (uint32_t gx = 0; gx < g->bw; gx++, k++) {
uint32_t v = alpha[k];
int32_t px = x + (int32_t)floor(pos / 16.0) + g->bx + (int32_t)gx, py = y + g->by + (int32_t)gy;
if (v && (uint32_t)px < W && (uint32_t)py < H) ref[py * W + px] = v == 15u ? fg : rv[v];
}
pen += g->adv;
prev = c;
}
return x + ((pen + 8) >> 4);
}
int main(void)
{
const aafont_t *F[3] = {&AF_S, &AF_M, &AF_L};
uint32_t n = 0, bad_px = 0, bad_pen = 0;
for (uint32_t p = 0; p < NPALETTES; p++) {
palette_set(p);
for (uint32_t k = 0; k < 2u; k++)
for (uint32_t f = 0; f < 3u; f++)
for (uint32_t s = 0; s < NSTR; s++) {
uint16_t fg = k ? T_TEXT : T_THEME, bg = k ? T_BG : T_SURF;
int32_t e0, e1;
for (uint32_t i = 0; i < W * H; i++) ref[i] = bg;
e0 = ref_text(3, 2, F[f], STR[s], fg, bg);
cv_begin(W, H, bg);
e1 = cv_text(3, 2, F[f], STR[s], fg);
bad_pen += e0 != e1;
for (uint32_t i = 0; i < W * H; i++) bad_px += swap16(cv_px[i]) != ref[i];
n++;
}
}
printf("text: %u strings x fonts x palettes against the mock renderer: %u end pens and %u pixels differ %s\n",
n, bad_pen, bad_px, bad_pen || bad_px ? "FAIL" : "ok");
return bad_pen || bad_px ? 1 : 0;
}