Pretext Lab
26Pagination — the book view
Arc 9 · Kinetic & Organic

Pagination — the book view

Most of the web is infinite scroll. A book is not. A book is a sequence of fixed frames. When you know the height of every line before painting, you can cut the text into pages by arithmetic — and then turning a page is free.

page — of —

Mechanism

prepareWithSegments runs once on the full passage. We then loop layoutNextLine(prepared, cursor, pageWidth), accumulating each line's height into a page budget. When the budget is exceeded we close the current page and open the next from the current cursor. The result is an array of page descriptors, all precomputed.

Page-turning is then a swap of which page's lines are in the DOM. Zero re-measurement during navigation.

Application

Long-form essays as turnable pages. Fixed page counts for deep reading. Scroll-free interfaces on mobile. Annotation surfaces with stable page numbers across sessions.

"Our life is frittered away by detail. Simplify, simplify."

Henry David Thoreau, Walden (1854)

Direct Claude

"paginate this passage" walk layoutNextLine, cut pages at a height budget "turn the page without reflow" swap visible line range; heights are already known "known page count" precompute pages once per width

Inspired by the community full-novel pagination demos referenced in the Hacker News Pretext discussion.

walk and cut into pages
function paginate(prepared, layoutNextLine, pageWidth, pageHeight, lineHeightPx) {
  const pages = [];
  let cursor = { segmentIndex: 0, graphemeIndex: 0 };
  while (true) {
    const lines = [];
    let budget = pageHeight;
    while (budget >= lineHeightPx) {
      const line = layoutNextLine(prepared, cursor, pageWidth);
      if (!line) break;
      lines.push(line);
      cursor = line.end;
      budget -= lineHeightPx;
    }
    if (lines.length === 0) break;
    pages.push({ lines });
  }
  return pages;
}