Skip to content

Milestone 4: export to SVG, PNG and PDF - #4

Merged
y0sif merged 13 commits into
mainfrom
feat/m4-export
Oct 25, 2026
Merged

y0sif merged 13 commits into
mainfrom
feat/m4-export

Conversation

@y0sif

@y0sif y0sif commented Oct 25, 2026

Copy link
Copy Markdown
Owner

What

  • Export (lawh-render): Renderer::export writes a board to SVG, PNG or PDF, framed to its objects plus a 1-unit margin, with no grid dots or overlay. Every formula is compiled in the worker and waited for; one that fails is drawn in red, as on screen, and counted in the message.
    • PNG: 80 px a unit by default, sRGB chunk, transparent with exportbg=none.
    • SVG: written by hand. Glyphs are outlines defined once and reused, with no <text>, no board text and no reference outside the file.
    • PDF: krilla. Board text is real text (searchable, copyable, fonts subset), formulas are paths, 30 pt a unit on one page, sRGB, the same bytes on every run.
  • One draw list, one palette: the window and all three formats draw each object from scene.rs, and every colour comes from Palette. A test fails on a colour made anywhere else, so the visual pass restyles exports too.
  • Engine (lawh-core): :exp[ort] {file} (the format comes from the extension), only the selection from Visual, :set pngscale and exportbg, and :w x.png refuses and names :export. :te, :ma and :sym on an object now place the block below it.
  • App: a status line saying "exporting" is shown before the export runs. Keys typed behind an export wait for it. An export onto the board's own file is refused. lawh export [--scale N] [--transparent] <board.lawh> <out.ext> works without a window and exits 0, 1 or 2.
  • Caps: one work budget charged by all three formats (PNG encoding included), a 10 s wait for an export's formulas, pixel, page and byte caps (a board too big shrinks to fit, with the scale in the message), one export a key, and the per-key work budget. New limits are listed in CLAUDE.md.
  • Worker: the math worker is killed and reaped when the engine is dropped, so lawh export leaves none behind.
  • Docs: docs/keys.md, the architecture, testing, tech-stack and security docs, and M4 measurements.

Result

lawh export takes 18 ms for examples/area.lawh as SVG, 21 ms as PDF and 31 ms as PNG (release, worker start included). The board still takes 62 keys to build.

Checks

  • Locally: fmt, clippy -D warnings, 1,002 tests, the Rust 1.95 check and cargo-deny all pass. lawh-core's dependency tree is unchanged.
  • Exports checked against the screen: resvg's render of the SVG and hayro's of the PDF match the PNG.
  • Tested by hand on Linux (Hyprland, Wayland), with the files opened in Chromium and the desktop's viewers. Not tried on macOS or Windows.

🤖 Generated with Claude Code

y0sif and others added 13 commits October 25, 2026 00:29
Every colour moves into one Palette (Palette::LIGHT) so a later theme can
restyle the window and exports alike. scene.rs builds each object's items in
board units, independent of the view, and Renderer::export draws them into a
PNG: whole board or a selection, plus a margin, waiting for every formula in
the worker, with caps on drawing work, math wait, pixels and bytes. The
window's drawing is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each shape kind has one builder, which maps into board units for exports or
into the view for the window, so the screen and every export draw the same
objects with the same geometry. The window and PNG export share one set of
vello calls. Screen output is byte-identical to before (298 dumped frames and
images) and frame times are unchanged within noise. render_png moves its
image out and writes the sRGB chunk.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SVG export from the shared scene, written by hand: every glyph is an outline
defined once and placed with <use>, shapes are paths with round caps and
joins, colours come from the palette. No board text, no external reference,
same bytes every run, and the 64 MiB cap is checked while writing.

MathEngine::layout_until stops waiting at a deadline without caching a
failure, so an export's 10 s math wait no longer runs seconds past its cap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One page at 30 pt a unit, shrunk to fit PDF's 14,400 pt limit. Board text is
real text through krilla (subset fonts, ToUnicode), so it can be searched
and copied; math glyphs are drawn as reusable forms, each outline stored
once. ICC sRGB, no date or metadata, same bytes every run. Every glyph id is
checked before krilla sees it. hayro (dev) reads the file back and renders
it against the PNG export.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ow objects

:export {file} (:exp) picks the format from the extension and exports the
whole board, or the selection from Visual. :w with an .svg, .png or .pdf file
is refused and points to :export. pngscale and exportbg set PNG sharpness and
the background. One export per key, each also a write slot, charged to the
key's work budget. export_done reports without naming or saving the board.

:te, :ma and :sym with the cursor on an object now put the new block below it
by the place rule instead of on top of it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d line

:export resolves its path like :w, refuses the board's own file, shows
"exporting" and presents that frame before the export runs, then writes
atomically and reports. Effects and queued keys keep the engine's order.

lawh export [--scale N] [--transparent] <board.lawh> <out.ext> exports
without a window: math in the worker, no lawhrc, exit 0, 1 or 2. A board
file named export opens with lawh ./export or lawh -- export.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From the cost review: PNG encoding ran outside every budget (one crafted
board took 7 s to encode) and the byte cap was checked only after the whole
image was compressed in memory. The encode is now charged to the export
budget up front (zlib up to 2 Mpx, fdeflate above, both weights measured on
their worst content) and streams through the byte cap.

Dropping a worker-mode MathEngine kills and reaps the worker, so lawh export
leaves no process behind. SVG width and height are capped like PNG sides.
The PDF form weight is refitted on a board of distinct colours. A test now
fails on any colour made outside palette.rs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From review round 1: a click, a close request or a failed present ran at
most one waiting export, so a second one was dropped and keys queued behind
it ran after the click moved the cursor. They now drain the whole queue in
the engine's order first.

Text and failed-formula colours are picked in one place (scene::text_paint)
for the window and exports. A formula that fails only when fetched again to
draw stops the export instead of drawing cropped. The math-wait CLI test no
longer depends on the machine's speed. Doc comments brought up to date.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From review round 2: a click that first ran a waiting export landed through
the view the drained keys left; it now lands through the view the user
clicked on. The report of an export run by a click or close is no longer
cleared or hidden (Engine::restore_message). PNG takes fdeflate's charge
before drawing and zlib's extra only if it fits, so a smaller image is never
refused where a larger one is written.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
keys.md lists :export, the :w refusal, the two settings, lawh export and
blocks placed below an object. Architecture gains an Export section and its
caps; tech-stack moves krilla to in use and adds the test readers; testing,
SECURITY and CLAUDE.md follow. M4 numbers, re-measured in release, are in
docs/research/m4-measurements.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CLI exit codes and stdout stated exactly, claims about Ghostscript and rsvg
re-measured and sourced, derived numbers marked, the SVG writer's chunk size
added to the limits, <file> notation made consistent, and code comments
brought in line with the re-measured numbers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Typst caches what it evaluated for the whole process. When the deadline
test compiled the slow formula first, the timeout test got it back at
once and failed (seen on CI; reproduced with --test-threads=1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@y0sif
y0sif merged commit 80cd686 into main Oct 25, 2026
4 checks passed
@y0sif
y0sif deleted the feat/m4-export branch October 25, 2026 17:00
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