SPB Git

Repository Public

export.ts 18.0 KB 261 lines typescript

1/**2 * Export rendering (worker only).3 *  - SVG: the block is rendered to a standalone vector document by the same components that draw it on screen.4 *  - PNG / PDF: a headless Chromium opens the internal render page of the stored document and captures it, so5 *    fonts, equations and colours are exactly those of the site. PDF output is vector with embedded fonts.6 * Interactive blocks are captured in their initial state and labelled as such by the print mode of the renderer.7 */8import { readFile } from "node:fs/promises";9import path from "node:path";10import { chromium, type Browser, type Page } from "playwright-core";11import { sql } from "@/lib/db";12import { env } from "@/lib/env";13import { log } from "@/lib/log";14import { allBlocks, FORMAT_SIZES, mustParseSheet, slugify, type Sheet } from "@/schema";15import { renderBlockSvg } from "@/renderer/export-svg";16import { createExportAsset } from "@/server/assets";17import { renderToken } from "@/server/render-token";1819let browser: Browser | null = null, idleTimer: NodeJS.Timeout | null = null;20async function getBrowser(): Promise<Browser> {21  if (idleTimer) clearTimeout(idleTimer);22  if (!browser || !browser.isConnected()) browser = await chromium.launch({ executablePath: env.chromiumPath, args: ["--font-render-hinting=none"] });23  return browser;24}25function releaseBrowser() {26  if (idleTimer) clearTimeout(idleTimer);27  idleTimer = setTimeout(() => { void browser?.close().catch(() => {}); browser = null; }, 120_000);28  idleTimer.unref();29}30export async function closeBrowser() { if (idleTimer) clearTimeout(idleTimer); await browser?.close().catch(() => {}); browser = null; }3132/** Whether a Chromium binary is installed for this worker (reported on the dashboard). */33export async function chromiumAvailable(): Promise<{ ok: boolean; detail: string }> {34  // The only honest test is to start it: which binary Playwright picks (full build or headless shell) is its business.35  try { const b = await chromium.launch({ executablePath: env.chromiumPath }); const v = b.version(); await b.close(); return { ok: true, detail: `Chromium ${v}` }; }36  catch (e) { return { ok: false, detail: `Chromium could not start (${(e as Error).message.split("\n")[0].slice(0, 160)}). Run: node_modules/.bin/playwright-core install chromium-headless-shell` }; }37}3839let fontCss: string | null = null;40/** @font-face rules with the fonts inlined, so an exported SVG looks the same on a machine without them. */41async function embeddedFonts(): Promise<string> {42  if (fontCss !== null) return fontCss;43  const face = async (family: string, pkg: string, file: string) => {44    const data = await readFile(path.join(process.cwd(), "node_modules", pkg, "files", file));45    return `@font-face{font-family:"${family}";font-style:normal;font-weight:100 900;src:url(data:font/woff2;base64,${data.toString("base64")}) format("woff2");}`;46  };47  try {48    fontCss = (await Promise.all([49      face("Inter Variable", "@fontsource-variable/inter", "inter-latin-wght-normal.woff2"),50      face("JetBrains Mono Variable", "@fontsource-variable/jetbrains-mono", "jetbrains-mono-latin-wght-normal.woff2"),51    ])).join("");52  } catch (e) { log("warn", "export.fonts_missing", { detail: (e as Error).message }); fontCss = ""; }53  return fontCss;54}5556/**57 * PDF of the page currently loaded.58 *59 * Article sheets become a quick sheet: A4 landscape, three columns of compact panels (the styles are under60 * "Quick sheet" in src/renderer/sheet.css), printed at a reduced scale so that a column is as wide as a phone screen —61 * the width every block is already designed for. Fixed formats are printed at their exact size, one page.62 * Before printing, what only makes sense on a screen is settled: closed disclosures are opened (a reader of the PDF63 * cannot click), and a formula wider than its box is reduced until it fits (paper cannot scroll).64 */65export const PDF_SCALE = 0.62;66const PAPER = { width: 297, height: 210, top: 8, side: 8, bottom: 12 }; // A4 landscape, millimetres67export async function printPdf(page: Page, size: { width: number; height: number | null }): Promise<Buffer> {68  await page.emulateMedia({ media: "print" });69  if (size.height) return page.pdf({ width: `${size.width}px`, height: `${size.height}px`, printBackground: true, margin: { top: "0", right: "0", bottom: "0", left: "0" }, pageRanges: "1" });70  // Lay the page out at the width it will have on paper, so that the measurements below are the real ones.71  const px = (mm: number) => (mm / 25.4) * 96 / PDF_SCALE;72  await page.setViewportSize({ width: Math.round(px(PAPER.width - 2 * PAPER.side)), height: 1400 });73  await page.evaluate(() => document.fonts.ready.then(() => undefined));74  // tsx wraps named functions with a helper that does not exist in the page: define it there.75  await page.evaluate("globalThis.__name = (f) => f");76  await page.evaluate((pageHeight) => {77    for (const d of document.querySelectorAll<HTMLDetailsElement>(".sheet details")) d.open = true;78    const grid = document.querySelector<HTMLElement>(".sh-qs");79    if (!grid) return;80    // ── Quick-sheet imposition ──────────────────────────────────────────────────────────────────81    // Three columns per page, filled in reading order (down a column, then the next one). A wide panel takes two82    // columns. A section band never ends a column, a panel is never cut, and one that is taller than a page is83    // reduced to fit. Every panel stays live DOM: the PDF remains vector, with selectable text.84    const COLS = 3, GAP = 15, ROW_GAP = 11;85    const W = grid.getBoundingClientRect().width, colW = (W - GAP * (COLS - 1)) / COLS;86    const band = document.querySelector<HTMLElement>("[data-print-masthead]");87    const bandH = band ? band.getBoundingClientRect().height + parseFloat(getComputedStyle(band).marginBottom) : 0;88    const H = Math.floor(pageHeight) - 6;89    const cells = [...grid.children].filter((c): c is HTMLElement => c instanceof HTMLElement && getComputedStyle(c).display !== "none");90    grid.style.columns = "auto";91    for (const c of cells) { c.style.margin = "0"; c.style.width = `${c.classList.contains("sh-qs-full") ? W : c.classList.contains("sh-qs-wide") ? colW * 2 + GAP : colW}px`; }92    // Paper cannot scroll: a formula wider than its box is reduced until it fits (measured at the final width).93    for (const box of document.querySelectorAll<HTMLElement>(".sh-equation-math, .sh-vars-symbol")) {94      for (let i = 0; i < 8 && box.scrollWidth > box.clientWidth + 1; i++) box.style.fontSize = `${(parseFloat(getComputedStyle(box).fontSize) * Math.max(0.6, (box.clientWidth / box.scrollWidth) * 0.98)).toFixed(2)}px`;95    }96    const items = cells.map((el) => {97      let h = el.getBoundingClientRect().height;98      const max = H - bandH - 4; // must fit even on the first page99      if (h > max) { const z = Math.max(0.5, max / h); el.style.zoom = String(Math.floor(z * 1000) / 1000); h = el.getBoundingClientRect().height; }100      return { el, h, wide: el.classList.contains("sh-qs-wide"), full: el.classList.contains("sh-qs-full"), heading: el.dataset.blockType === "heading", refs: el.dataset.blockType === "references", placed: false };101    });102    interface Slot { el: HTMLElement; page: number; x: number; y: number }103    const slots: Slot[] = [];104    let pageNo = 0, col = 0, fill = [0, 0, 0];105    const room = () => (pageNo === 0 ? H - bandH : H);106    const nextPage = () => { pageNo++; col = 0; fill = [0, 0, 0]; };107    const nextCol = () => { if (col + 1 >= COLS) nextPage(); else col++; };108    let n = 0;109    for (const it of items) { const h2 = it.el.querySelector<HTMLElement>(".sh-heading--2"); if (h2) h2.dataset.n = String(++n).padStart(2, "0"); }110    const place = (el: HTMLElement, c: number, y: number) => { slots.push({ el, page: pageNo, x: c * (colW + GAP), y }); };111    // A column that the flow leaves with room to spare is topped up with later panels of the same section that fit112    // there (the scan stops at the next heading: nothing is shown ahead of the section it belongs to; never a wide one).113    const topUp = (from: number) => {114      for (let j = from; j < items.length && !items[j].heading; j++) {115        const cand = items[j], left = room() - fill[col];116        if (left < 70) return;117        if (cand.placed || cand.wide || cand.full || cand.refs || cand.h > left) continue;118        place(cand.el, col, fill[col]); fill[col] += cand.h + ROW_GAP; cand.placed = true;119      }120    };121    // The list of references is the one panel that may be cut: between two entries, continuing in the next column.122    const flowReferences = (cell: HTMLElement) => {123      const section = cell.querySelector<HTMLElement>(".sh-references"), list = cell.querySelector<HTMLElement>(".sh-references-list");124      const entries = list ? [...list.children].filter((x): x is HTMLElement => x instanceof HTMLElement) : [];125      if (!section || !list || entries.length < 2) return false;126      const title = section.querySelector<HTMLElement>(".sh-references-title");127      let done = 0;128      for (let guard = 0; done < entries.length && guard < 40; guard++) {129        const part = cell.cloneNode(false) as HTMLElement, sec = section.cloneNode(false) as HTMLElement, ol = list.cloneNode(false) as HTMLElement;130        part.removeAttribute("id");131        if (done === 0 && title) sec.appendChild(title);132        ol.style.counterReset = `sh-ref ${done}`;133        sec.appendChild(ol); part.appendChild(sec); grid.appendChild(part);134        const left = room() - fill[col];135        let taken = 0;136        while (done + taken < entries.length) {137          ol.appendChild(entries[done + taken]);138          if (part.getBoundingClientRect().height > left && (taken > 0 || fill[col] > 0)) { ol.removeChild(entries[done + taken]); break; }139          taken++;140        }141        if (taken === 0) { part.remove(); nextCol(); continue; }142        place(part, col, fill[col]);143        fill[col] += part.getBoundingClientRect().height + ROW_GAP;144        done += taken;145        if (done < entries.length) nextCol();146      }147      cell.remove();148      return true;149    };150    for (let i = 0; i < items.length; i++) {151      const it = items[i], next = items[i + 1];152      if (it.placed) continue;153      if (it.heading && next?.wide) continue; // placed together with the wide panel it introduces154      if (it.full) {155        // Across the whole page, under everything already placed; on a new page if it does not fit there.156        let top = Math.max(...fill);157        if (top > 0 && top + it.h > room()) { nextPage(); top = 0; }158        place(it.el, 0, top);159        fill = fill.map(() => top + it.h + ROW_GAP); col = 0;160        continue;161      }162      if (it.wide) {163        const head = i > 0 && items[i - 1].heading ? items[i - 1] : null;164        const lead = head ? head.h + ROW_GAP : 0;165        // Every pair of columns from the current one is tried; the one where the panel is drawn largest wins (the pair166        // to the right is often free while the current column is half full, typically beside the introduction).167        // A panel that would have to be reduced below 80 % starts a new page instead, after the columns of this168        // page have been topped up with what follows in the section.169        let c = Math.min(col, COLS - 2), top = Math.max(fill[c], fill[c + 1]);170        const zoomAt = (t: number) => (room() - t - lead - 2) / it.h;171        for (let k = c + 1; k <= COLS - 2 && zoomAt(top) < 1; k++) { const t = Math.max(fill[k], fill[k + 1]); if (zoomAt(t) > zoomAt(top)) { c = k; top = t; } }172        const z = zoomAt(top);173        if (z < 1) {174          if (z >= 0.8) { it.el.style.zoom = String(Math.floor(z * (parseFloat(it.el.style.zoom) || 1) * 1000) / 1000); it.h = it.h * z; }175          else { const keep = col; for (col = 0; col < COLS; col++) topUp(i + 1); col = keep; nextPage(); c = 0; top = 0; }176        }177        if (head) { head.el.style.width = `${colW * 2 + GAP}px`; place(head.el, c, top); }178        place(it.el, c, top + lead);179        fill[c] = fill[c + 1] = top + lead + it.h + ROW_GAP;180        if (c < col) col = c; // otherwise the current column keeps filling, then the flow continues under the panel181        continue;182      }183      if (it.refs && fill[col] + it.h > room() && flowReferences(it.el)) continue;184      // A heading travels with the block that follows it.185      const tail = it.heading && next ? Math.min(next.h, room() * 0.45) + ROW_GAP : 0;186      for (let guard = 0; guard < 8 && fill[col] > 0 && fill[col] + it.h + tail > room(); guard++) { if (!it.heading) topUp(i + 1); nextCol(); }187      place(it.el, col, fill[col]);188      fill[col] += it.h + ROW_GAP;189    }190    document.body.dataset.qs = JSON.stringify({ H, bandH: Math.round(bandH), colW: Math.round(colW), items: items.map((it) => [it.el.dataset.blockType, Math.round(it.h), it.wide ? "wide" : "", it.el.style.zoom]) });191    for (let p = 0; p <= pageNo; p++) {192      const sheet = document.createElement("div");193      sheet.className = "sh-qs-page";194      sheet.style.cssText = `position:relative;height:${p === 0 ? H - bandH : H}px;${p < pageNo ? "break-after:page;" : ""}`;195      for (const s of slots.filter((x) => x.page === p)) {196        // Offsets of a zoomed element are expressed in its own (zoomed) units.197        const z = parseFloat(s.el.style.zoom) || 1;198        s.el.style.position = "absolute"; s.el.style.left = `${s.x / z}px`; s.el.style.top = `${s.y / z}px`; sheet.appendChild(s.el);199      }200      grid.appendChild(sheet);201    }202  }, px(PAPER.height - PAPER.top - PAPER.bottom));203  const footer = `<div style="width:100%;font:7px Helvetica,Arial,sans-serif;color:#667085;padding:0 ${PAPER.side}mm;display:flex;justify-content:space-between;align-items:center"><span><b style="color:#101828">Sheet</b> — by Simon-Pierre Boucher</span><span><span class="pageNumber"></span> / <span class="totalPages"></span></span></div>`;204  return page.pdf({ format: "A4", landscape: true, scale: PDF_SCALE, printBackground: true, margin: { top: `${PAPER.top}mm`, right: `${PAPER.side}mm`, bottom: `${PAPER.bottom}mm`, left: `${PAPER.side}mm` }, displayHeaderFooter: true, headerTemplate: "<span></span>", footerTemplate: footer });205}206207interface ExportJob { id: string; sheet_id: string; revision_id: string | null; publication_id: string | null; format: "svg" | "png" | "pdf" | "json"; options: { scale?: number; blockId?: string | null }; is_public: boolean }208209async function loadDoc(job: ExportJob): Promise<{ doc: Sheet; slug: string }> {210  const [row] = job.publication_id211    ? await sql<{ doc: unknown; slug: string }[]>`SELECT p.doc, p.slug FROM sheet_publications p WHERE p.id = ${job.publication_id}`212    : await sql<{ doc: unknown; slug: string }[]>`SELECT r.doc, s.slug FROM sheet_revisions r JOIN sheets s ON s.id = r.sheet_id WHERE r.id = ${job.revision_id}`;213  if (!row) throw new Error("The document of this export no longer exists");214  return { doc: mustParseSheet(row.doc), slug: row.slug };215}216217export async function runExport(job: ExportJob): Promise<string> {218  const { doc, slug } = await loadDoc(job);219  const blockId = job.options.blockId ?? undefined;220  const base = slugify(slug + (blockId ? `-${blockId}` : ""));221222  if (job.format === "svg") {223    const block = allBlocks(doc).find((b) => b.id === blockId);224    if (!block) throw new Error("Block not found");225    const svg = renderBlockSvg(block as never, { theme: doc.theme, locale: doc.metadata.locale, fontCss: await embeddedFonts() });226    if (!svg) throw new Error(`A “${block.type}” block has no vector export; use PNG or PDF`);227    return createExportAsset({ filename: `${base}.svg`, mime: "image/svg+xml", data: Buffer.from(svg, "utf8"), isPublic: false });228  }229230  const format = doc.layout.format, size = FORMAT_SIZES[format];231  const scale = job.format === "png" ? Math.min(4, Math.max(1, job.options.scale ?? 2)) : 1;232  const token = renderToken({ kind: job.publication_id ? "publication" : "revision", id: (job.publication_id ?? job.revision_id)!, blockId }, 600);233  const b = await getBrowser();234  const context = await b.newContext({ viewport: { width: size.width + (format === "article" ? 96 : 0), height: size.height ?? 1200 }, deviceScaleFactor: scale, reducedMotion: "reduce", javaScriptEnabled: true });235  try {236    const page = await context.newPage();237    // The render page is same-origin and self-contained; nothing else may be fetched during an export.238    const allowed = new URL(env.internalUrl).origin;239    await page.route("**/*", (route) => { let ok = false; try { ok = new URL(route.request().url()).origin === allowed; } catch { /* data: and blob: URLs have no origin to compare */ } return ok ? route.continue() : route.abort(); });240    const res = await page.goto(`${env.internalUrl}/render/${token}`, { waitUntil: "networkidle", timeout: 60_000 });241    if (!res || res.status() !== 200) throw new Error(`Render page answered HTTP ${res?.status() ?? "?"}`);242    await page.evaluate(() => document.fonts.ready.then(() => undefined));243    const target = page.locator(blockId ? `[data-block-id="${blockId}"]` : ".sheet").first();244    await target.waitFor({ state: "visible", timeout: 15_000 });245    // A capture of a collapsed layout would be a valid file with unreadable content: refuse it instead.246    const sheetBox = await page.locator(".sheet").first().boundingBox();247    if (!sheetBox || sheetBox.width < 280) throw new Error("The sheet did not lay out at a readable width; export aborted");248249    if (job.format === "png") {250      const box = await target.boundingBox();251      if (!box) throw new Error("Nothing to capture");252      if (box.height * scale > 16_000 || box.width * scale > 16_000) throw new Error(`The image would be ${Math.round(box.width * scale)} × ${Math.round(box.height * scale)} px, beyond what a PNG capture supports; use a lower scale or the PDF export`);253      const data = await target.screenshot({ type: "png", animations: "disabled", scale: "device" });254      return createExportAsset({ filename: `${base}@${scale}x.png`, mime: "image/png", data, isPublic: false });255    }256257    const data = await printPdf(page, size);258    return createExportAsset({ filename: `${base}.pdf`, mime: "application/pdf", data, isPublic: job.is_public });259  } finally { await context.close(); releaseBrowser(); }260}261
Files main
Menu

Theme