diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-06 11:38:06 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-06 12:03:00 -0300 |
| commit | 0b4142e6704c851f395da2cd8b74954272dadf45 (patch) | |
| tree | 48b4980d506de14fad672eb409827fcb372c6c31 /addons/pdf | |
| parent | f05c69380c8f23ab7dadb1c9d6240af687c88bed (diff) | |
| download | pardes-0b4142e6704c851f395da2cd8b74954272dadf45.tar.gz pardes-0b4142e6704c851f395da2cd8b74954272dadf45.zip | |
kinds #19, the PDF's restored behaviours: a fling (a window's travel or more between frames) renders only the rows each page shows, on a 64-row grain, until a tick after it rests renders them whole, a partly landed page showing its paper under what it has; a wheel's amount scrolls its fraction of a row; the pointer is a hand over a link the resting pointer found; with no graphics the page's text layer is drawn, the moving keys and the wheel a page a step; pdf.script shows pictures, then the text layer
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'addons/pdf')
| -rw-r--r-- | addons/pdf/pdf.c | 130 | ||||
| -rw-r--r-- | addons/pdf/pdf.out | 60 | ||||
| -rw-r--r-- | addons/pdf/pdf.script | 5 |
3 files changed, 161 insertions, 34 deletions
diff --git a/addons/pdf/pdf.c b/addons/pdf/pdf.c index 55820785..5c156dec 100644 --- a/addons/pdf/pdf.c +++ b/addons/pdf/pdf.c @@ -33,6 +33,8 @@ #define CHUNK 512u /* a tall page's bands start on these */ #define OVERLAP 32u /* rows rendered past a band's edges */ #define HOVER_NS 32000000ull /* the pointer's rest before its word shows */ +#define FLING_NS 100000000ull /* a fling's end: this long with no turn */ +#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 @@ -49,7 +51,7 @@ 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.1: no heap */ + 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; @@ -242,6 +244,11 @@ typedef struct pdf { int hover_page, hover_link, hover_wait; uint16_t hover_col, hover_row; uint64_t hover_at; + /* A fling: the strip's travel since the last tick and since the last + * frame drawn; a window or more of it a frame renders only the rows each + * page shows, the whole page once it rests (as pdf_view.zig did). */ + double since_tick, travel; + int framed, landed, fling; uint32_t count; /* a key's count, typed before it */ int g; } pdf; @@ -515,6 +522,11 @@ static void band_of(const pdf *st, int p, uint32_t w, uint32_t h, uint32_t *y, u rows r = {0}; if (h <= cap) { *y = 0, *bh = h; + /* flinging: the rows shown alone, on the grain */ + if (st->fling && page_rows(st, p, w, h, st->vh, &r)) { + uint32_t y0 = r.y0 / FLING_GRAIN * FLING_GRAIN, y1 = (r.y1 + FLING_GRAIN - 1) / FLING_GRAIN * FLING_GRAIN; + *y = y0, *bh = (y1 < h ? y1 : h) - y0; + } return; } uint32_t y0 = 0, y1 = CHUNK; @@ -576,7 +588,7 @@ static void reconcile(pdf *st) { st->sizes_asked = ask(st, &in, sizeof in, 1) != 0; } int first, n = visible(st, &first); - if (n > 0 && first + n < st->pages) n++; /* the page ahead */ + if (n > 0 && first + n < st->pages && !st->fling) n++; /* the page ahead, at rest */ for (int i = 0; i < SLOTS; i++) if (st->s[i].page >= 0 && (st->s[i].page < first || st->s[i].page >= first + n)) { abort_job(st, st->s[i].token); @@ -713,6 +725,7 @@ static void job_done(void *v, uint32_t token, const uint8_t *bytes, uint32_t n) int sizes = st->jobs[at].sizes; st->jobs[at].token = 0; st->warm = 1; + st->landed = 1; if (sizes) { st->sizes_assumed = 0; if (res.ok && (int)res.n == st->pages && memcmp(res.sizes, st->size, res.n * sizeof *res.sizes)) { @@ -809,7 +822,19 @@ static void go_page(pdf *st, int p) { } static void scroll_by(pdf *st, double px) { - if (st->laid) set_top(st, st->top + px); + if (!st->laid) return; + double was = st->top; + set_top(st, st->top + px); + st->since_tick += fabs(st->top - was); +} + +/* 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; } + +/* 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); + else scroll_by(st, dir * px * n); } /* The search's hit `hit` in the window: centred (fit width), or panned to @@ -1026,8 +1051,16 @@ static void close(void *v) { static uint64_t tick(void *v, uint64_t now, uint32_t flags) { pdf *st = v; - (void)flags; if (!st || !st->d) return 0; + /* 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; + if (!(st->landed && st->since_tick == 0)) { + if (st->framed) st->travel = 0, st->framed = 0; + st->travel += st->since_tick; + 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)); @@ -1043,8 +1076,16 @@ 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); - reconcile(st); - return st->hover_wait == 2 ? st->hover_at : 0; + if (text_mode(st)) { + uint32_t len; + page_text(st, &len); + } else { + 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; } static void paint(void *v, const pa_window *win, pa_paint *out) { @@ -1052,6 +1093,27 @@ static void paint(void *v, const pa_window *win, pa_paint *out) { 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 */ + /* (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; + for (unsigned row = 0; row < win->rows && at < len; row++) { + unsigned col = 0; + while (at < len && t[at] != '\n') { + unsigned n = 1; + while (n < 7 && at + n < len && ((unsigned char)t[at + n] & 0xc0) == 0x80) n++; + pa_cell *c = pa_at(win, row, col); + if (c && (unsigned char)t[at] >= 0x20) { + memcpy(c->text, t + at, n), c->len = (uint8_t)n; + c->fg = c->bg = PA_DEFAULT, c->flags = PA_PAINTED; + } + col += (unsigned char)t[at] >= 0x20, at += n; + } + at++; + } + return; + } uint32_t vh = (uint32_t)win->rows * win->cell_h; if (vh > st->vh) vh = st->vh; int first, count = visible(st, &first); @@ -1062,10 +1124,27 @@ static void paint(void *v, const pa_window *win, pa_paint *out) { rows r; pa_placement *pl = &out->placements[out->n]; if (s) { - if (!page_rows(st, p, s->w, s->h, vh, &r) || r.y0 < s->band_y || r.y1 > s->band_y + s->band_h) continue; - *pl = (pa_placement){.rgba = s->rgba, .w = s->w, .h = s->band_h, .src = {r.g.sx, r.y0 - s->band_y, r.g.sw, r.y1 - r.y0}, - .dst = {0, 0, win->cols, win->rows}, .at = {r.g.dx, r.at_y, r.g.dw, r.at_h}, .key = (uint64_t)p << 32 | s->rev, - .from = s->from ? (uint64_t)p << 32 | s->from : 0, .dirty_y = s->dirty_y, .dirty_h = s->dirty_h}; + if (!page_rows(st, p, s->w, s->h, vh, &r)) continue; + uint32_t b0 = s->band_y, b1 = s->band_y + s->band_h; + if (r.y0 >= b0 && r.y1 <= b1) { + *pl = (pa_placement){.rgba = s->rgba, .w = s->w, .h = s->band_h, .src = {r.g.sx, r.y0 - b0, r.g.sw, r.y1 - r.y0}, + .dst = {0, 0, win->cols, win->rows}, .at = {r.g.dx, r.at_y, r.g.dw, r.at_h}, .key = (uint64_t)p << 32 | s->rev, + .from = s->from ? (uint64_t)p << 32 | s->from : 0, .dirty_y = s->dirty_y, .dirty_h = s->dirty_h}; + out->n++; + continue; + } + /* Its rows not all landed (a fling's band moved on): its paper, + * and over it what the band has of them. */ + *pl = (pa_placement){.rgba = st->blank, .w = 1, .h = 1, .src = {0, 0, 1, 1}, .dst = {0, 0, win->cols, win->rows}, + .at = {r.g.dx, r.at_y, r.g.dw, r.at_h}, .key = (uint64_t)p << 32}; + out->n++; + uint32_t i0 = r.y0 > b0 ? r.y0 : b0, i1 = r.y1 < b1 ? r.y1 : b1, span = r.y1 - r.y0; + if (i1 <= i0 || out->n >= out->cap) continue; + uint32_t ay = r.at_y + (uint32_t)((uint64_t)(i0 - r.y0) * r.at_h / span), ah = (uint32_t)((uint64_t)(i1 - i0) * r.at_h / span); + if (!ah) continue; + out->placements[out->n++] = (pa_placement){.rgba = s->rgba, .w = s->w, .h = s->band_h, .src = {r.g.sx, i0 - b0, r.g.sw, i1 - i0}, + .dst = {0, 0, win->cols, win->rows}, .at = {r.g.dx, ay, r.g.dw, ah}, .key = (uint64_t)p << 32 | s->rev}; + continue; } else { /* not landed yet: its paper, where it will be */ uint32_t w = (uint32_t)fmaxf(1, roundf(st->size[p].width * 8)), h = (uint32_t)fmaxf(1, roundf(st->size[p].height * 8)); @@ -1127,12 +1206,12 @@ static int key(void *v, const pa_key *k) { if (ctrl) { uint32_t half = st->vh / 2 ? st->vh / 2 : 1, n = counted(st); switch (cp) { - case 'd': scroll_by(st, (double)half * n); return 1; - case 'u': scroll_by(st, -(double)half * n); return 1; - case 'f': scroll_by(st, (double)st->vh * n); return 1; - case 'b': scroll_by(st, -(double)st->vh * n); return 1; - case 'e': scroll_by(st, cell); return 1; - case 'y': scroll_by(st, -(double)cell); return 1; + case 'd': move(st, 1, half, n); return 1; + case 'u': move(st, -1, half, n); return 1; + case 'f': move(st, 1, st->vh, n); return 1; + case 'b': move(st, -1, st->vh, n); return 1; + case 'e': move(st, 1, cell, 1); return 1; + case 'y': move(st, -1, cell, 1); return 1; default: return 0; /* the editor's: windows, jumps */ } } @@ -1140,11 +1219,10 @@ static int key(void *v, const pa_key *k) { if (st->g) { uint32_t n = st->count; st->g = 0, st->count = 0; - uint32_t cell_n = cell * (n ? n : 1); if (cp == 'g') go_page(st, n ? (int)n - 1 : 0); else if (cp == 'e') go_page(st, st->pages - 1); - else if (cp == 'j') scroll_by(st, cell_n); - else if (cp == 'k') scroll_by(st, -(double)cell_n); + else if (cp == 'j') move(st, 1, cell, n ? n : 1); + else if (cp == 'k') move(st, -1, cell, n ? n : 1); else if (cp == 'h' || cp == 's') edge(st, 0); else if (cp == 'l') edge(st, 1); return 1; @@ -1164,10 +1242,10 @@ static int key(void *v, const pa_key *k) { go_page(st, n ? (int)n - 1 : st->pages - 1); return 1; } - case 'j': case PA_KEY_DOWN: scroll_by(st, (double)cell * counted(st)); return 1; - case 'k': case PA_KEY_UP: scroll_by(st, -(double)cell * counted(st)); return 1; - case PA_KEY_PAGE_DOWN: scroll_by(st, (double)st->vh * counted(st)); return 1; - case PA_KEY_PAGE_UP: scroll_by(st, -(double)st->vh * counted(st)); return 1; + case 'j': case PA_KEY_DOWN: move(st, 1, cell, counted(st)); return 1; + case 'k': case PA_KEY_UP: move(st, -1, cell, counted(st)); return 1; + case PA_KEY_PAGE_DOWN: move(st, 1, st->vh, counted(st)); return 1; + case PA_KEY_PAGE_UP: move(st, -1, st->vh, counted(st)); return 1; case 'h': case PA_KEY_LEFT: pan_by(st, -1, st->view.cell_w * counted(st)); return 1; case 'l': case PA_KEY_RIGHT: pan_by(st, 1, st->view.cell_w * counted(st)); return 1; case '0': case '^': case PA_KEY_HOME: st->count = 0, edge(st, 0); return 1; @@ -1183,7 +1261,11 @@ static int mouse(void *v, const pa_mouse *m) { if (!st || !st->d) return 0; uint32_t cell = st->view.cell_h ? st->view.cell_h : 16; if (m->button == PA_WHEEL_UP || m->button == PA_WHEEL_DOWN) { - scroll_by(st, m->button == PA_WHEEL_UP ? -(double)cell : (double)cell); + /* a notch a row, a touchpad's fraction of one its fraction; as text a + * page a turn */ + double notches = m->amount > 0 ? m->amount : 1; + if (text_mode(st)) move(st, m->button == PA_WHEEL_UP ? -1 : 1, 0, 1); + else scroll_by(st, (m->button == PA_WHEEL_UP ? -1 : 1) * notches * cell); return 1; } if (m->button == PA_WHEEL_LEFT || m->button == PA_WHEEL_RIGHT) { diff --git a/addons/pdf/pdf.out b/addons/pdf/pdf.out index 08d24595..d598dd1e 100644 --- a/addons/pdf/pdf.out +++ b/addons/pdf/pdf.out @@ -11,6 +11,19 @@ tag [1/16] | | | +> graphics on +tag [1/16] +| +| +| +| +| +| +| +| +| +| +placement 794x1123 src 0,0 794x204 over 0,0 78x10 key 3017bc3ce49f9593 at 0,0 624x160 > size 60 20 window 58x7 tag [1/16] @@ -21,7 +34,7 @@ tag [1/16] | | | -placement 794x1123 src 0,0 794x192 over 0,0 58x7 key 3017bc3ce49f9593 at 0,0 464x112 +placement 794x1123 src 0,0 794x192 over 0,0 58x7 key 71e39225f0cec2a5 at 0,0 464x112 > key j tag [1/16] | @@ -31,7 +44,7 @@ tag [1/16] | | | -placement 794x1123 src 0,27 794x193 over 0,0 58x7 key 3017bc3ce49f9593 at 0,0 464x112 +placement 794x1123 src 0,27 794x193 over 0,0 58x7 key 71e39225f0cec2a5 at 0,0 464x112 > key 3j tag [1/16] | @@ -41,7 +54,7 @@ tag [1/16] | | | -placement 794x1123 src 0,109 794x193 over 0,0 58x7 key 3017bc3ce49f9593 at 0,0 464x112 +placement 794x1123 src 0,109 794x193 over 0,0 58x7 key 71e39225f0cec2a5 at 0,0 464x112 > key G tag [16/16] | @@ -51,7 +64,7 @@ tag [16/16] | | | -placement 794x1123 src 0,0 794x192 over 0,0 58x7 key ad6b5307dead8475 at 0,0 464x112 +placement 794x1123 src 0,0 794x192 over 0,0 58x7 key 1182c0220e1b5a25 at 0,0 464x112 > key gg tag [1/16] | @@ -61,7 +74,7 @@ tag [1/16] | | | -placement 794x1123 src 0,0 794x192 over 0,0 58x7 key 107dd9d0f0ee5746 at 0,0 464x112 +placement 794x1123 src 0,0 794x192 over 0,0 58x7 key 5400263dddff499a at 0,0 464x112 > key 3gg tag [3/16] | @@ -71,7 +84,7 @@ tag [3/16] | | | -placement 794x1123 src 0,0 794x192 over 0,0 58x7 key e6b017c42b5b77eb at 0,0 464x112 +placement 794x1123 src 0,0 794x192 over 0,0 58x7 key 1da05d9257cc481d at 0,0 464x112 > PdfFit tag [3/16] | @@ -81,7 +94,7 @@ tag [3/16] | | | -placement 794x1123 src 0,0 794x1123 over 0,0 58x7 key e6b017c42b5b77eb at 192,0 79x112 +placement 794x1123 src 0,0 794x1123 over 0,0 58x7 key 1da05d9257cc481d at 192,0 79x112 > ctl =fit:height tint:filtered > PdfTint full @@ -93,7 +106,7 @@ tag [3/16] | | | -placement 794x1123 src 0,0 794x1123 over 0,0 58x7 key e6b017c42b5b77eb at 192,0 79x112 +placement 794x1123 src 0,0 794x1123 over 0,0 58x7 key 1da05d9257cc481d at 192,0 79x112 > ctl =fit:height tint:full > PdfTint loud @@ -105,7 +118,7 @@ tag [3/16] | | | -placement 794x1123 src 0,0 794x963 over 0,1 58x6 key e6b017c42b5b77eb at 192,0 79x96 +placement 794x1123 src 0,0 794x963 over 0,1 58x6 key 1da05d9257cc481d at 192,0 79x96 > key <esc> tag [3/16] |ent: PdfTint takes disabled, filtered or full, not "loud" @@ -115,7 +128,7 @@ tag [3/16] | | | -placement 794x1123 src 0,0 794x1123 over 0,2 58x5 key 3f51689292296629 at 204,0 56x80 +placement 794x1123 src 0,0 794x1123 over 0,2 58x5 key 13a14ce76b83da1b at 204,0 56x80 > dump dump 636117 bytes tag [3/16] @@ -127,3 +140,30 @@ tag [3/16] | | | +> graphics off +tag [3/16] +|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 +|otify or a macOS kqueue behind one mark/reconcile transact +|the native macOS host watches with debounced DispatchSourc +|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] +|/// 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 +|/// user: one Ctrl-V pasting twice. +|fn isPull(comptime name: []const u8) bool { +| if (std.mem.startsWith(u8, name, "pull_")) return true +| if (std.mem.startsWith(u8, name, "push_")) { +> key 2k +tag [2/16] +|the core — src/pardes.zig +|update(ev) — one dispatch, mutates +|render(arena) → *Surface +|emit(e) → fixed ring, limits.effect_cap +|nextEffect() → perform(e) +|single-threaded by construction +|native input diff --git a/addons/pdf/pdf.script b/addons/pdf/pdf.script index dbdbea33..be76f3a3 100644 --- a/addons/pdf/pdf.script +++ b/addons/pdf/pdf.script @@ -1,5 +1,6 @@ # pardes --addon-test pdf.so addons/pdf/pdf.script | diff addons/pdf/pdf.out - open docs/design.pdf +graphics on size 60 20 key j key 3j @@ -13,3 +14,7 @@ ctl PdfTint loud key <esc> dump +# a terminal without graphics: the page's text layer, a page a step +graphics off +key j +key 2k |
