From e9f9f65ace7650a02cee6bf2b2eb466522485834 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 15:58:05 -0300 Subject: kinds #36, the five kinds on ABI 3: fake.c with every function (view painting each view's size, search through found, FakeJob cancel) and PA_PICTURES, snake, life and image over the module's prefix and open(path, bytes), image without a record (a Restore reads its file), and pdf.c ported mechanically (tick into pa_tick with its [P/N] prefix, a table of the views it is told, its strip laid out for the view its last key or click came from, the text layer painted for a window of glyphs and read on the tick while some view shows text, a body read keeping nothing, search through found, the record its fit, tint and place, PA_PICTURES); the goldens moved once, said in the report: the art words from the first frame, fake's view line, a pane ticked when opened or loaded, a view told after a frame re-wrapped its tag Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- addons/pdf/pdf.c | 121 +++++++++++++++++++++++++++++++---------------------- addons/pdf/pdf.out | 65 ++++++++++++++-------------- 2 files changed, 103 insertions(+), 83 deletions(-) (limited to 'addons/pdf') diff --git a/addons/pdf/pdf.c b/addons/pdf/pdf.c index 5c156dec..5a0448b1 100644 --- a/addons/pdf/pdf.c +++ b/addons/pdf/pdf.c @@ -13,7 +13,8 @@ * rows, repainted on the loop and said as rows (`from`, `dirty_*`). The * body is the text of the page shown; `/` lists hits as `file.pdf:P:H`; * PdfSections lists the outline; a click on a link follows it. The record - * in a dump is the file's bytes, with its fit, tint and place. */ + * in a dump is its fit, tint and place. The strip is laid out for one view + * at a time: the one its last key or click came from, else the first told. */ #include #include #include @@ -37,6 +38,7 @@ #define FLING_GRAIN 64u /* a fling's band starts and ends on these rows */ #define MARKS 1024 /* highlights on a page */ #define RECORD_VERSION 1u +#define VIEWS 8 /* views a pane is told of, by id */ static const pa_api *api; @@ -51,7 +53,6 @@ static void *bridge_realloc(void *user, void *p, size_t n) { (void)user; return static void bridge_free(void *user, void *p) { (void)user; unheap(p); } static int init(const pa_api *a) { - if (a->size < sizeof(pa_api)) return 1; /* a core before 2.2: no heap, no amount */ api = a; static const pardes_pdf_allocator hooks = {0, bridge_alloc, bridge_realloc, bridge_free}; return pardes_pdf_set_allocator(&hooks) == PARDES_PDF_OK ? PA_OK : 1; @@ -213,7 +214,7 @@ typedef struct pdf { uint64_t height; /* the strip's */ uint32_t vw, vh; /* the window the strip is laid out for, in pixels */ int laid, laid_fit, fit; - pa_view view; + pa_view view, views[VIEWS]; /* the one laid out for, and each told */ double top; /* the strip's pixel at the window's top */ int want_page; /* -1, else laid out next at this page, want_frac down it */ double want_frac, want_x, want_y; /* or an outline's place there (NAN: none) */ @@ -831,6 +832,13 @@ static void scroll_by(pdf *st, double px) { /* A terminal without graphics: the page's text layer, a page a step. */ static int text_mode(const pdf *st) { return st->view.glyphs != 0; } +/* Some view told shows it as text: the page's text is read for it. */ +static int any_text(const pdf *st) { + for (int i = 0; i < VIEWS; i++) + if (st->views[i].cols && st->views[i].glyphs) return 1; + return text_mode(st); +} + /* Down (dir 1) or up `n` steps of `px` pixels, or of a page as text. */ static void move(pdf *st, int dir, double px, uint32_t n) { if (text_mode(st)) go_page(st, st->page + dir * (int)n); @@ -1049,9 +1057,11 @@ static void close(void *v) { st->d = 0; } -static uint64_t tick(void *v, uint64_t now, uint32_t flags) { +static void tick(void *v, pa_tick *t) { pdf *st = v; - if (!st || !st->d) return 0; + uint64_t now = t->now; + uint32_t flags = t->flags; + if (!st || !st->d) return; /* A fling is a window's travel or more between frames; a tick a page's * landing made (no turn since) neither starts nor ends one. */ if (flags & PA_FRAMED) st->framed = 1; @@ -1061,7 +1071,6 @@ static uint64_t tick(void *v, uint64_t now, uint32_t flags) { st->fling = st->vh && st->travel >= st->vh; } st->landed = 0, st->since_tick = 0; - api->view(&st->view); layout(st); to_page(st, page_at(st, st->top)); /* white tinted is the theme's paper */ @@ -1076,25 +1085,37 @@ static uint64_t tick(void *v, uint64_t now, uint32_t flags) { } if (st->query_len && st->hits_page != st->page) search_page(st, st->page, 1); if (st->reveal) reveal_hit(st); - if (text_mode(st)) { - uint32_t len; - page_text(st, &len); - } else { - reconcile(st); - } + uint32_t len; + if (any_text(st)) page_text(st, &len); + if (!text_mode(st)) reconcile(st); uint64_t next = st->hover_wait == 2 ? st->hover_at : 0; /* flinging: a tick when it rests, for the whole pages */ if (st->fling && (!next || now + FLING_NS < next)) next = now + FLING_NS; - return next; + t->next = next; + int n = snprintf(t->prefix, sizeof t->prefix, "[%d/%d]", st->page + 1, st->pages); + t->prefix_len = n > 0 ? (uint32_t)n : 0; +} + +/* A view told: kept by its id, laid out for when it is the one (or none is). */ +static void view(void *v, const pa_view *pv) { + pdf *st = v; + if (!st || pv->id < 1 || pv->id > VIEWS) return; + st->views[pv->id - 1] = *pv; + if (pv->id == st->view.id || !st->view.cols) st->view = *pv; + for (int i = 0; i < VIEWS && !st->view.cols; i++) + if (st->views[i].cols) st->view = st->views[i]; +} + +/* An input from view `id`: the strip is laid out for it from now on. */ +static void adopt(pdf *st, uint8_t id) { + if (id >= 1 && id <= VIEWS && st->views[id - 1].cols) st->view = st->views[id - 1]; } static void paint(void *v, const pa_window *win, pa_paint *out) { const pdf *st = v; - int n = st && st->d ? snprintf(out->prefix, sizeof out->prefix, "[%d/%d]", st->page + 1, st->pages) : 0; - out->prefix_len = n > 0 ? (uint32_t)n : 0; if (!st || !st->laid) return; out->pointer = st->hover_page >= 0 && st->hover_link ? PA_POINTER_LINK : PA_POINTER_DEFAULT; - if (text_mode(st)) { /* the page's text layer, its lines from the top */ + if (win->glyphs) { /* the page's text layer, its lines from the top */ /* (read on the tick: paint changes nothing) */ uint32_t len = st->text_page == st->page ? st->text_len : 0, at = 0; const char *t = st->text; @@ -1154,7 +1175,7 @@ static void paint(void *v, const pa_window *win, pa_paint *out) { } out->n++; } - if (st->height) { /* the rail's thumb; init refused a core before 2.1 */ + if (st->height) { /* the rail's thumb */ out->at = (float)(st->top / (double)st->height); out->span = (float)((double)st->vh / (double)st->height); } @@ -1199,6 +1220,7 @@ static int cancel(pdf *st) { static int key(void *v, const pa_key *k) { pdf *st = v; if (!st || !st->d) return 0; + adopt(st, k->view); uint32_t cp = k->cp, cell = st->view.cell_h ? st->view.cell_h : 16; int ctrl = k->ctrl; if (cp == 0x1b) return k->shift ? 0 : cancel(st); /* Shift-Esc: the editor's, leaving at once */ @@ -1259,6 +1281,7 @@ static int key(void *v, const pa_key *k) { static int mouse(void *v, const pa_mouse *m) { pdf *st = v; if (!st || !st->d) return 0; + adopt(st, m->view); uint32_t cell = st->view.cell_h ? st->view.cell_h : 16; if (m->button == PA_WHEEL_UP || m->button == PA_WHEEL_DOWN) { /* a notch a row, a touchpad's fraction of one its fraction; as text a @@ -1341,19 +1364,21 @@ static int mouse(void *v, const pa_mouse *m) { /* ---- the body, places, search ---- */ -static uint64_t body_len(void *v) { - uint32_t n = 0; - if (v && ((pdf *)v)->d) page_text(v, &n); - return n; -} - +/* The page's text: what a tick read for a view's paint, else MuPDF's, + * not kept (a read changes nothing a frame shows). */ static uint32_t body(void *v, uint64_t off, char *buf, uint32_t n) { - uint32_t len = 0; - if (!v || !((pdf *)v)->d) return 0; - const char *t = page_text(v, &len); - return pa_copy(t, len, off, buf, n); + pdf *st = v; + if (!st || !st->d) return 0; + if (st->text_page == st->page) return buf ? pa_copy(st->text, st->text_len, off, buf, n) : st->text_len; + pardes_pdf_text t; + if (pardes_pdf_page_text(st->d->base, st->page, &t) != PARDES_PDF_OK) return 0; + uint32_t k = buf ? pa_copy(t.data, t.len, off, buf, n) : (uint32_t)t.len; + pardes_pdf_drop_text(st->d->base, t.handle); + return k; } +static uint64_t body_len(void *v) { return body(v, 0, 0, 0); } + /* `text` as a number from `*i`, stopped at `end`: 0 when none is there. */ static int number(const char *t, uint32_t *i, uint32_t end, uint64_t *out) { uint32_t from = *i; @@ -1570,26 +1595,29 @@ static uint32_t where(void *v, char *buf, uint32_t n) { return k; } -/* `/`: every hit, `file.pdf:P:H pattern` a row, at most MAX_HITS. */ -static uint32_t search(void *v, const char *pat, uint32_t n, char *buf, uint32_t cap) { +/* `/`: every hit, `file.pdf:P:H pattern` a row, at most MAX_HITS, found + * at once (on the loop: 7c moves it to a job). */ +static void search(void *v, const char *pat, uint32_t n) { pdf *st = v; - if (!st || !st->d || n == 0 || n > 255) return 0; + if (!st || !st->d || n == 0 || n > 255) return; if (st->query_len != n || memcmp(st->query, pat, n)) { memcpy(st->query, pat, n), st->query_len = n; drop_hits(st); st->hit = 0, st->reveal = 1; } - uint32_t shown = n > 200 ? 200 : n, len = 0, rows = 0; + uint32_t shown = n > 200 ? 200 : n, len = 0, rows = 0, cap = MAX_HITS * 512; while (shown > 0 && shown < n && ((unsigned char)pat[shown] & 0xc0) == 0x80) shown--; + char *buf = heap(cap); + if (!buf) return; for (int p = 0; p < st->pages && rows < MAX_HITS; p++) { uint32_t hits = search_page(st, p, 0); for (uint32_t h = 0; h < hits && rows < MAX_HITS; h++, rows++) { int k = snprintf(buf + len, cap - len, "%s:%d:%u %.*s\n", st->name, p + 1, h + 1, (int)shown, pat); - if (k < 0 || (uint32_t)k >= cap - len) return len; - len += (uint32_t)k; + if (k > 0 && (uint32_t)k < cap - len) len += (uint32_t)k; } } - return len; + api->found(buf, len); + unheap(buf); } /* ---- words ---- */ @@ -1636,7 +1664,7 @@ static uint32_t ctl_read(void *v, char *buf, uint32_t n) { return k > 0 && (uint32_t)k < n ? (uint32_t)k : 0; } -/* ---- the record: its fit, tint and place, then the file's bytes ---- */ +/* ---- the record: its fit, tint and place ---- */ typedef struct record { uint32_t version, fit, tint, page; @@ -1645,30 +1673,23 @@ typedef struct record { static uint32_t dump(void *v, uint8_t *buf, uint32_t n) { pdf *st = v; - if (!st || !st->d) return 0; - size_t len = 0; - const unsigned char *bytes = pardes_pdf_bytes(st->d->base, &len); - if (!bytes || len > 0xffffffffu - sizeof(record)) return 0; - uint32_t total = (uint32_t)(sizeof(record) + len); - if (total > n) return total; + if (!st || !st->d || n < sizeof(record)) return 0; record r = {RECORD_VERSION, (uint32_t)st->fit, (uint32_t)st->tint, (uint32_t)st->page, 0}; if (st->laid && st->ph[st->page]) r.frac = (st->top - (double)st->y[st->page]) / st->ph[st->page]; memcpy(buf, &r, sizeof r); - memcpy(buf + sizeof r, bytes, len); - return total; + return sizeof r; } -/* On a state open made from its file (a Restore's, from nothing): the - * document from the record. On one open made from the file as it is now - * (it changed on disk): only its fit, tint and place. */ +/* On a state open made from its file as it is (a Restore's, a change on + * disk): its fit, tint and place. A record of before 3 has the file's + * bytes after them, unread. */ static int load(void *v, const uint8_t *buf, uint32_t n, uint32_t version) { pdf *st = v; record r; (void)version; - if (!st || n < sizeof r) return 1; + if (!st || !st->d || n < sizeof r) return 1; memcpy(&r, buf, sizeof r); if (r.version != RECORD_VERSION || r.fit > 1 || r.tint > 2) return 1; - if (!st->d && !reopen_bytes(st, buf + sizeof r, n - sizeof r)) return 1; st->fit = (int)r.fit, st->tint = (int)r.tint; st->page = (int)(r.page < (uint32_t)st->pages ? r.page : (uint32_t)st->pages - 1); st->want_page = st->page, st->want_frac = r.frac; @@ -1682,8 +1703,8 @@ static const pa_word words[] = { }; const pa_ops pardes_addon = { - .head = PA_ADDON("pdf", 1, ".pdf", words, 3), + .head = PA_ADDON("pdf", 1, PA_PICTURES, ".pdf", words, 3), .init = init, .open = open, .close = close, .tick = tick, .paint = paint, .key = key, .mouse = mouse, .body_len = body_len, .body = body, .address = address, .where = where, .ctl = ctl, .ctl_read = ctl_read, - .job_run = job_run, .job_done = job_done, .dump = dump, .load = load, .search = search, + .job_run = job_run, .job_done = job_done, .dump = dump, .load = load, .search = search, .view = view, }; diff --git a/addons/pdf/pdf.out b/addons/pdf/pdf.out index 23849db2..a6ea68b3 100644 --- a/addons/pdf/pdf.out +++ b/addons/pdf/pdf.out @@ -1,18 +1,17 @@ > open docs/design.pdf -window 78x10 -tag [1/16] -| -| -| -| -| -| -| -| -| -| +window 78x9 +tag [1/16] petscii:on palette:commodore ascii:on docs/design.pdf +|Pardes: A Text Environment +|Architecture and implementation of the rewrite +|Abstract. Pardes is a text environment in the acme tradition: columns of panes +|body a live terminal, a file, an image or a PDF. It is one program with five t +|SDL3 window, a browser tab driven by vanilla JavaScript, an AppKit application +|— where the prototype had three parallel implementations of one editor. Two se +|is a state machine over plain values: Event in, Surface and a queue of Effect +|said in those types exists in pardes. Downward, host.VTable is twenty-one opti +|one answered in-process, so the zero-method host is both the test harness and > graphics on -tag [1/16] +tag [1/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -25,7 +24,7 @@ tag [1/16] placement 794x1123 src 0,0 794x184 over 0,0 78x9 key 3017bc3ce49f9593 at 0,0 624x144 > size 60 20 window 58x7 -tag [1/16] +tag [1/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -35,7 +34,7 @@ tag [1/16] | placement 794x1123 src 0,0 794x192 over 0,0 58x7 key 71e39225f0cec2a5 at 0,0 464x112 > key j -tag [1/16] +tag [1/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -45,7 +44,7 @@ tag [1/16] | placement 794x1123 src 0,27 794x193 over 0,0 58x7 key 71e39225f0cec2a5 at 0,0 464x112 > key 3j -tag [1/16] +tag [1/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -55,7 +54,7 @@ tag [1/16] | placement 794x1123 src 0,109 794x193 over 0,0 58x7 key 71e39225f0cec2a5 at 0,0 464x112 > key G -tag [16/16] +tag [16/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -65,7 +64,7 @@ tag [16/16] | placement 794x1123 src 0,0 794x192 over 0,0 58x7 key 1182c0220e1b5a25 at 0,0 464x112 > key gg -tag [1/16] +tag [1/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -75,7 +74,7 @@ tag [1/16] | placement 794x1123 src 0,0 794x192 over 0,0 58x7 key 5400263dddff499a at 0,0 464x112 > key 3gg -tag [3/16] +tag [3/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -85,7 +84,7 @@ tag [3/16] | placement 794x1123 src 0,0 794x192 over 0,0 58x7 key 1da05d9257cc481d at 0,0 464x112 > PdfFit -tag [3/16] +tag [3/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -93,11 +92,11 @@ tag [3/16] | | | -placement 794x1123 src 0,0 794x1123 over 0,0 58x7 key 1da05d9257cc481d at 192,0 79x112 +placement 794x1123 src 0,0 794x1123 over 0,0 58x7 key acaee8c0ff4f63a6 at 192,0 79x112 > ctl =fit:height tint:filtered > PdfTint full -tag [3/16] +tag [3/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -105,11 +104,11 @@ tag [3/16] | | | -placement 794x1123 src 0,0 794x1123 over 0,0 58x7 key 1da05d9257cc481d at 192,0 79x112 +placement 794x1123 src 0,0 794x1123 over 0,0 58x7 key 11acd08483d3011b at 192,0 79x112 > ctl =fit:height tint:full > PdfTint loud -tag [3/16] +tag [3/16] petscii:off palette:commodore ascii:on docs/design.pdf |ent: PdfTint takes disabled, filtered or full, not "loud" | | @@ -117,9 +116,9 @@ tag [3/16] | | | -placement 794x1123 src 0,0 794x963 over 0,1 58x6 key 1da05d9257cc481d at 192,0 79x96 +placement 794x1123 src 0,0 794x1123 over 0,1 58x6 key a790cfe920570b8b at 198,0 67x96 > key -tag [3/16] +tag [3/16] petscii:off palette:commodore ascii:on docs/design.pdf |ent: PdfTint takes disabled, filtered or full, not "loud" | Last | @@ -127,11 +126,10 @@ tag [3/16] | | | -placement 794x1123 src 0,0 794x1123 over 0,2 58x5 key 13a14ce76b83da1b at 204,0 56x80 +placement 794x1123 src 0,0 794x1123 over 0,2 58x5 key b7d9755f4d1c3b06 at 204,0 56x80 > dump -dump 636117 bytes -tag [3/16] -| +dump 24 bytes +tag [3/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -139,8 +137,9 @@ tag [3/16] | | | +placement 794x1123 src 0,0 794x1123 over 0,0 58x7 key 4a2b3987bae4cbef at 192,0 79x112 > graphics off -tag [3/16] +tag [3/16] petscii:on palette:commodore ascii:on docs/design.pdf |The watch path, in full, because it is the ask with the mo |machinery behind it. The tty and SDL hosts and the detache |daemon implement it in src/file_watch.zig, with Linux in­ @@ -149,7 +148,7 @@ tag [3/16] |instead, then restats the exact path under a pane-generati |guard. All three macOS watchers mark the same two things f > key j -tag [4/16] +tag [4/16] petscii:on palette:commodore ascii:on docs/design.pdf |/// How to route a method, read off its own name. A method |/// is a COMPILE ERROR rather than a silent push, because |/// forgotten pull is invisible in every unit test and obv @@ -158,7 +157,7 @@ tag [4/16] | if (std.mem.startsWith(u8, name, "pull_")) return true | if (std.mem.startsWith(u8, name, "push_")) { > key 2k -tag [2/16] +tag [2/16] petscii:on palette:commodore ascii:on docs/design.pdf |the core — src/pardes.zig |update(ev) — one dispatch, mutates |render(arena) → *Surface -- cgit v1.3