Skip to content

Draw PDF diagrams as vectors through the shapes' own handlers - #638

Merged
QR-Madness merged 6 commits into
masterfrom
pdf-vector-diagrams
Dec 1, 2026
Merged

QR-Madness merged 6 commits into
masterfrom
pdf-vector-diagrams

Conversation

@QR-Madness

Copy link
Copy Markdown
Collaborator

What

Diagrams in PDF exports were PNG pictures. They are now drawn by the shapes' own handlers onto jsPDF, so they look like the canvas, stay sharp at any zoom, and their labels can be selected and searched. This builds on the path recorder (#635).

  • drawExport (exportUtils.ts): the PNG route's drawing, moved into a function both routes share. The PNG route makes the same calls in the same order.
  • pdfCanvasContext (pdfExport/pdfCanvas.ts): a proxy over jsPDF's context2d that fills what it draws differently from a canvas, each found in this work and pinned by a test:
    • measureText was about 3.8 times too wide in millimetres, so labels wrapped early. Widths are now the font's own; text the PDF font lacks is measured by the browser.
    • Font sizes were rounded down to whole points; the baselines were approximations; alignment was applied across the page, so rotated labels moved sideways. All three now match the canvas, with the em box as the HTML spec has it.
    • fillText's maxWidth wrapped text; it now sets an over-long UML or entity name smaller to fit, where the canvas narrows its letters.
    • The alpha of rgba() colours was recorded and never painted, so group tints came out solid. Colour alpha and globalAlpha now go through jsPDF's opacity state.
    • Dashes were set in page units; they're now scaled with the transform at each stroke.
    • arc turned neither its angles nor its radius with a rotation or an uneven scale. Every arc, arcTo, ellipse and roundRect is now drawn as cubics (replayPath's new shimArc).
    • drawImage asked for a JPEG, which loses transparency, and couldn't read SVG icons. Images are now drawn as PNGs at the export's DPI.
    • fill(path), fillRect and strokeRect no longer disturb the current path, and a path that recorded nothing is an error, not a blank.
  • Fonts (diagramVectors.ts): every family list the shapes use maps to the bundled font the PDF embeds for it. jsPDF's own fallbacks (built-in Times, Helvetica and Courier, Latin-1 only) map to the document font and the code font. registerCatalogueFont now registers a known font's missing styles; the code font came in two.
  • Founder's choice for labels: a label with characters no PDF font has (CJK, emoji, Arabic, Hebrew) is drawn as a picture of that label by the browser, in its box.
  • Safety: the drawing is clipped to its box, as the picture was. If a diagram fails part-way, everything it wrote is taken back (jsPDF's save and restore write to every page) and the diagram is placed as a picture.
  • Guard: noNativePath2D.test.ts fails on any new Path2D( outside the shape picker's on-screen previews. It caught createRoundedRectPath (group backgrounds), now a RecordedPath.
  • Docs: the guide says what prints differently (pictured labels, smaller over-long names, no shadows, gradients and patterns in their first colour). AGENTS.md describes the vector route and the RecordedPath rule, and its stale line from Print emoji in PDF exports as pictures (DSK-247) #636 (emoji were left out; they're pictures now) is corrected here because it sits in the same paragraph.

Accepted differences

  • Shadows are left out; a gradient or pattern fill prints in its first colour.
  • jsPDF's save and restore also write q/Q to every earlier page. They balance; with 100 pages before a 300-shape diagram they add about 31 KB compressed and no measurable time.
  • When a diagram fails part-way, images it already added stay in the file, unused, beside the fallback picture.

Verification

  • Tests: pdfCanvas.test.ts (31), diagramVectors.test.ts (9: labels as text on their canvas lines, each label's font, a family the PDF lacks, jsPDF's fallbacks, missing styles, the font wait, the rollback and the next drawing, library shapes with a connector and a group, the clip), RecordedPath.test.ts (+4 for the arc shim), the guard (2).
  • Mutations: 26 of 26 killed, one per fix.
  • Suites: tsc clean; 524 files and 5,686 tests before the last two fixes; the PDF export folder passes after them. The golden PDF dumps (documents without diagrams) are unchanged. The OSS leak grep is clean.
  • In the browser (local dev server, rendered with pdf.js): the same fixture through both routes matches by eye: a translucent rounded group with a dashed border, a rotated label, a dashed connector with its label gap, half opacity, a sharp icon, a CJK and emoji label as a picture, serif text. A 300-shape diagram exports in 229 ms to a 304 KB PDF with no images; the picture route alone took 1,135 ms and 1.47 MB at 2×.

Not in this PR

  • Exports don't wait for shape icons to load (pre-existing, every format); filed in the backlog.
  • Groups on a canvas page other than the open one find no children (pre-existing; Group.getBounds reads the live store), so a multi-page PDF draws them without their frame.
  • SVG export through the same handlers is the next step.

Assisted-by: Opus 5.5

Diagrams in PDF exports were pictures. They're now drawn by the shapes'
handlers onto jsPDF's context, so their text stays text and they stay
sharp at any zoom. pdfCanvasContext fills what jsPDF's context2d does
differently from a canvas: paths replayed from RecordedPaths, arcs as
cubics, colour alpha and globalAlpha, scaled dashes, exact text sizes,
widths, baselines and alignment, maxWidth, and images as PNGs at the
export's DPI. Fonts resolve to bundled ones, never jsPDF's Latin-1
built-ins. A label no PDF font can draw is a picture of that label. A
diagram that fails part-way is taken back and placed as a picture.

Assisted-by: Opus 5.5
# Conflicts:
#	docs-site/guide/export-import.md
@QR-Madness
QR-Madness merged commit 180961f into master Dec 1, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant