Pack complete notices losslessly and specialize the renderer to generated single-phase fonts

This commit is contained in:
JUAN JIANT committed 2026-11-05 22:40:42 -03:00
1 parent 42aeff02da
commit 818d9796e2
6 files changed
+104 -7

No files matched your search

+4 -2
View File
@@ -7,7 +7,7 @@ static const char *const TG_ROLE[] = {"BASS","CHORDS","LEAD","ARP"};
static int trackgen_ok(const track_t *t)
{return eng_ok(eng_idx(t->eng_req))&&eng_idx(t->eng_req)!=ENGI_INPUT;}
static __attribute__((noinline)) void trackgen_knob(uint32_t k,int32_t s)
BTAPP_FN static void trackgen_knob_bt(uint32_t k,int32_t s)
{
tg_config_t *c=&tg_config[song.sel%NTRK];
if(!k) c->genre=gen_genre_next(c->genre,s);
@@ -17,7 +17,7 @@ static __attribute__((noinline)) void trackgen_knob(uint32_t k,int32_t s)
*p=(uint8_t)clamp((int32_t)*p+s*(drum_track(TSEL)?5:1),0,drum_track(TSEL)?100:3);
} else c->energy=(uint8_t)clamp((int32_t)c->energy+s*5,0,100);
}
static __attribute__((noinline)) void trackgen_cards(void)
BTAPP_FN static void trackgen_cards_bt(void)
{
const tg_config_t *c=&tg_config[song.sel%NTRK];char b[12];
hot_text_column(0,"GENRE",c->genre==GEN_EXPLORE?"EXPLORE":c->genre?GENRE[(c->genre-1u)%NGENRE].name:"RANDOM");
@@ -25,6 +25,8 @@ static __attribute__((noinline)) void trackgen_cards(void)
fmt_int(b,c->complex);hot_text_column(2,drum_track(TSEL)?"COMPLEX":"ROLE",drum_track(TSEL)?b:TG_ROLE[c->role%4u]);
fmt_int(b,c->energy);hot_unit_column(3,"ENERGY",b,"%");
}
static void trackgen_knob(uint32_t k,int32_t s) {if(bt_hw_seg()) trackgen_knob_bt(k,s);}
static void trackgen_cards(void) {if(bt_hw_seg()) trackgen_cards_bt();}
static __attribute__((noinline)) void trackgen_make(void)
{
track_t *t=TSEL;uint32_t k=trk_index(t),i,j,n,len,f;
+15 -1
View File
@@ -27,6 +27,8 @@ typedef struct { const char *name; uint16_t bg, surf, text, theme, accent; } ui_
typedef struct { uint16_t off; uint8_t w; const char *label; } kc_t;
#include "ui_fonts.h" /* AF_S 12 px / 400, AF_M 15 px / 500, AF_L 28 px / 600 */
#include "ui_font_x.h" /* (0.8.5) AF_X: the 5 x 7 mono pixel face (tools/gen_px_font.py) */
#define FONT_COMPACT (AF_S_COMPACT && AF_M_COMPACT && AF_L_COMPACT && AF_X_COMPACT)
#define FONT_PHASES (AF_S_PHASES > 1 || AF_M_PHASES > 1 || AF_L_PHASES > 1 || AF_X_PHASES > 1)
#include "ui_palettes.h"
#include "ui_keycaps.h" /* KC_*: the keycaps / badges (tools/gen_aa_keycaps.py), KNOB_* arcs */
#define ELLIPSIS '\x85' /* the ellipsis glyph of AF_S and AF_M */
@@ -760,17 +762,22 @@ static uint32_t glyph(const aafont_t *f, uint32_t ch)
if ((k = glyph_at(f, ch)) < 0 && (ch != (uint8_t)ELLIPSIS || (k = glyph_at(f, '.')) < 0) &&
(k = glyph_at(f, '?')) < 0)
k = 0;
#if FONT_PHASES
return (uint32_t)k << f->psh;
#else
return (uint32_t)k;
#endif
}
/* table entry i (glyph i >> psh at phase i & (phases - 1)), unpacked */
static aag_t ag(const aafont_t *f, uint32_t i)
{
if(!f->gp) {
if(FONT_COMPACT || !f->gp) {
const uint8_t *p=(const uint8_t *)f->gh+5u*i;
aag_t g={(uint16_t)(p[0]|(p[1]&15u)<<8), (uint16_t)((p[1]>>4|p[2]<<4)&511u),
(int8_t)((p[2]>>5)-1),p[3]&31u,(p[3]>>5|p[4]<<3)&31u,(p[4]>>2)&31u};
return g;
}
#if !FONT_COMPACT
const aagh_t *h = &f->gh[i >> f->psh];
uint32_t v = f->gp[i];
aag_t g;
@@ -781,10 +788,13 @@ static aag_t ag(const aafont_t *f, uint32_t i)
g.bw = (uint8_t)((v >> 19) & 127u);
g.bh = (uint8_t)(v >> 26);
return g;
#endif
}
static uint32_t font_advance(const aafont_t *f,uint32_t i)
{
#if !FONT_COMPACT
if(f->gp) return f->gh[i>>f->psh].adv;
#endif
const uint8_t *p=(const uint8_t *)f->gh+5u*i;
return (p[1]>>4|p[2]<<4)&511u;
}
@@ -838,6 +848,7 @@ static int32_t text_run(int32_t x, int32_t y, const aafont_t *f, const char *s,
if (prev)
pen += kern(f, prev, c);
gi = glyph(f, c);
#if FONT_PHASES
if (f->psh && (f->gp[gi + 1u] & 1023u) != (f->gp[gi] & 1023u)) { /* (phase 1's bitmap is not phase 0's) */
pos = pen - (pen & (step - 1)); /* the phase position at or left of the pen */
d0 = pos - pen - err; /* .. and the one right of it: which keeps the gap truer */
@@ -846,8 +857,11 @@ static int32_t text_run(int32_t x, int32_t y, const aafont_t *f, const char *s,
pos += step;
gi += ((uint32_t)pos >> (4u - f->psh)) & ((1u << f->psh) - 1u);
} else {
#endif
pos = (pen + 8) & ~15; /* one phase (figures, ., blanks): the rounded pen */
#if FONT_PHASES
}
#endif
gv = ag(f, gi);
err = pos - pen;
if (g->bw) {
+19 -3
View File
@@ -35,6 +35,7 @@ static void draw_about_qr(int32_t x, int32_t y)
* text, 1 theme, 2 mid) + 3 x the gap before it / 4 px), its text, a zero; a zero ends the list. The URLs without
* "https://" (as they were shown) */
#define MENU_DOC_H 196 /* the document: screen rows 24..220 */
#ifdef ABOUT_NOTICE_TEXT
static const char ABOUT_PROJECT[] BTAPP_RO =
"\002THE PROJECT\0"
"\001Made in Argentina by Juan Jiant, visual artist and musician, a dad of two: an independent project, free for everyone.\0"
@@ -89,6 +90,16 @@ static const char ABOUT_CREDITS[] BTAPP_RO =
"\001NO WARRANTY\0"
"\007Full notices in source:\0"
"\003github.com/juanjiant-bit/JIANT\0";
#endif
#include "about_notices.h"
/* Generation proves acyclic pairs, depth <=12 and the document size. */
static char *about_byte(char *d,char *end,uint32_t c)
{
if(c<128u) {if(d<end) *d++=(char)c;}
else {d=about_byte(d,end,ABOUT_PAIRS[c-128u][0]);d=about_byte(d,end,ABOUT_PAIRS[c-128u][1]);}
return d;
}
static int32_t menu_text(const char *s, int32_t y, uint16_t col, int draw)
{
@@ -121,10 +132,15 @@ static int32_t menu_text(const char *s, int32_t y, uint16_t col, int draw)
return y;
}
static int32_t menu_lines(const char *s, int32_t y, int draw) /* (V10.13) ABOUT_PROJECT, ABOUT_CREDITS */
static int32_t menu_lines(const uint8_t *packed, int32_t y, int draw)
{
char text[ABOUT_MAX],*d=text;
if (!bt_hw_seg())
return y;
uint32_t n=packed[0]|(uint32_t)packed[1]<<8;
for(uint32_t i=0;i<n;i++) d=about_byte(d,text+sizeof text-1u,packed[2u+i]);
*d=0;
const char *s=text;
for (; *s; s += str_len(s) + 1u) {
uint32_t k = (uint8_t)*s - 1u, c = k % 3u;
y = menu_text(s + 1, y + 4 * (int32_t)(k / 3u), c == 1u ? T_THEME : c == 2u ? T_MID : T_TEXT, draw);
@@ -236,11 +252,11 @@ static int32_t menu_document(int32_t y, int draw)
cv_text(6, y + 174, &AF_S, "/JIANT", T_MID);
draw_about_qr(158, y + 122);
}
y = menu_lines(ABOUT_PROJECT, y + 216, draw);
y = menu_lines(ABOUT_PROJECT_PACK, y + 216, draw);
if (bt_hw_seg()) /* (V10.13: in the segment) */
y = about_load(y, draw);
y = about_cpu(y, draw); /* (p1: out of line, menu_document's room is pinned) */
y = menu_lines(ABOUT_CREDITS, y + 8, draw);
y = menu_lines(ABOUT_CREDITS_PACK, y + 8, draw);
return y + 4;
}
static int32_t menu_scroll_max(void)
+2 -1
View File
@@ -187,7 +187,7 @@ def glyph_tables(name, table, phases):
value = off | adv << 12 | (bx+1) << 21 | by << 24 | bw << 29 | bh << 34
data.extend(value.to_bytes(5, 'little'))
text = c_array(f"AF_{name}_PACK", "uint8_t", data, 20)
text += f"\n#define AF_{name}_GH ((const aagh_t *)AF_{name}_PACK)\n#define AF_{name}_GP 0"
text += f"\n#define AF_{name}_GH ((const aagh_t *)AF_{name}_PACK)\n#define AF_{name}_GP 0\n#define AF_{name}_COMPACT 1\n#define AF_{name}_PHASES {phases}"
return text, len(data)
gh, gp = [], []
for i in range(0, len(table), phases):
@@ -205,6 +205,7 @@ def glyph_tables(name, table, phases):
lines.append(" " + " ".join("{%d, %d}," % e for e in gh[k:k + 8]))
lines.append("};")
lines.append(c_array(f"AF_{name}_GP", "uint32_t", gp, 8, "0x{:08x}"))
lines += [f"#define AF_{name}_COMPACT 0", f"#define AF_{name}_PHASES {phases}"]
return "\n".join(lines), 4 * len(gh) + 4 * len(gp)
+1
View File
@@ -102,6 +102,7 @@ def generate():
tools = SRC / "tools"
cmds = [[tools / "gen_aa_font.py", GEN / "ui_fonts.h", "--preset", "jiant-lab", "--kern-min", "4"], # (snd7: pairs under 1/4 px dropped) # (gen_aa_font.UI_PRESET),
[tools / "gen_px_font.py", GEN / "ui_font_x.h"],
[tools / "gen_about.py", GEN / "about_notices.h", FW / "src" / "ui_menu.c"],
[tools / "gen_aa_icons.py", GEN / "ui_icons.h"],
[tools / "gen_aa_keycaps.py", GEN / "ui_keycaps.h"],
[tools / "gen_ui_palettes.py", GEN / "ui_palettes.h"],
+63
View File
@@ -0,0 +1,63 @@
#!/usr/bin/env python3
"""Losslessly pack the original on-device notices. No text is removed."""
import ast
from collections import Counter
from pathlib import Path
import re
import sys
def notices(path):
source = Path(path).read_text()
out = []
for name in ("PROJECT", "CREDITS"):
body = re.search(r"ABOUT_"+name+r"\[\] BTAPP_RO =(.*?);\n", source, re.S).group(1)
body = re.sub(r"/\*.*?\*/", "", body, flags=re.S)
text = "".join(ast.literal_eval(s) for s in re.findall(r'"(?:[^"\\]|\\.)*"', body))
out.append((text+"\0").encode("ascii"))
return out
def pack(raw):
rows = [list(r) for r in raw]
pairs = []
best = (sum(map(len, rows)), [], rows)
for code in range(128, 256):
counts = Counter(p for row in rows for p in zip(row, row[1:]))
pair, count = counts.most_common(1)[0]
if count < 3: break
changed = []
for row in rows:
new, i = [], 0
while i < len(row):
if tuple(row[i:i+2]) == pair: new.append(code); i += 2
else: new.append(row[i]); i += 1
changed.append(new)
rows = changed
pairs = pairs + [pair]
size = 2*len(pairs)+sum(map(len, rows))
if size < best[0]: best = size, pairs, rows
return best[1:]
def unpack(pairs, row):
def expand(c):
return bytes([c]) if c < 128 else b"".join(expand(v) for v in pairs[c-128])
return b"".join(expand(c) for c in row)
def main(out, source):
raw = notices(source); pairs, rows = pack(raw)
assert all(unpack(pairs, row) == original for row, original in zip(rows, raw))
depths = [0]*128
for a,b in pairs: depths.append(1+max(depths[a],depths[b]))
assert max(depths) <= 12 and max(map(len,raw)) <= 1536
lines = ["/* Generated losslessly from ui_menu.c; readable source notices remain there. */",
f"#define ABOUT_MAX {max(map(len,raw))}u",
f"static const uint8_t ABOUT_PAIRS[{len(pairs)}][2] BTAPP_RO = {{"]
lines += [f" {{{a},{b}}}," for a,b in pairs]
lines.append("};")
for name, row in zip(("PROJECT", "CREDITS"), rows):
data = [len(row)&255, len(row)>>8]+row
lines += [f"static const uint8_t ABOUT_{name}_PACK[{len(data)}] BTAPP_RO = {{",
",".join(map(str,data)), "};"]
Path(out).write_text("\n".join(lines)+"\n")
print(f"about: {sum(map(len,raw))} -> {sum(map(len,rows))+2*len(pairs)+4} bytes; notices preserved; depth {max(depths)}")
if __name__ == "__main__": main(*sys.argv[1:])