Skip to content

feat: PageSVG, an SVG device behind the device seam - #140

Merged
tannevaled merged 6 commits into
mainfrom
svg-device
Nov 22, 2026
Merged

tannevaled merged 6 commits into
mainfrom
svg-device

Conversation

@tannevaled

@tannevaled tannevaled commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Second of the "PDF to SVG" series, stacked on #139 (base device-seam; retarget to main once #139 merges).

Refs #139, #137, #138

What

render.PageSVG(d, i, w, SVGOptions{MaxBytes, RasterScale, MaxDuration}), written as a second implementation of the device interface. Paths, clips, images, faded groups and text (outlines, one definition per glyph, a use per letter) become SVG elements. Tiling and shading patterns, shadings, soft masks and groups under a mask are drawn by the rasteriser on a white and a black paper and embedded as an image where they fall.

The device is adapted to the seam as it stands in #139 (rebased onto 2edc683): open groups are values on a stack in the device, no *gstate is kept past its run, and clipPath ignores the rectangle the seam carries.

Security

  • Only numbers, generated ids and base64 are written; nothing from the PDF is copied as text. NaN and infinities are never printed.
  • href is #id or data:image/(png|jpeg);base64,; no script, foreignObject, style or on* attribute.
  • The allowlist is checked in production: verifySVG (svgverify.go) reads the finished buffer, element by element and attribute by attribute (shape of every value, no text, matching tags, unique ids, every reference defined) before it is written, and PageSVG refuses with nothing written if anything is off it. It is a hand-written scanner over the bytes: 221 allocations for a 35 KB document with 200 ids and 200 references (one per id, none per element or byte; asserted by a test). The independent encoding/xml checker stays in the tests and every document the suite draws is held to both. FuzzPageSVG does the same for any content stream.
  • Bounds: MaxBytes (default 64 MiB) gives ErrSVGTooLarge; embedded pictures are capped at 16 Mpixel; RasterScale defaults to 2; MaxDuration defaults to 10 s (ErrTimedOut); clips nested deeper than 64 give ErrSVGTooDeep.
  • No error leaves a byte in w (except a failing writer): the document is built in memory, bounded by MaxBytes, and written in one call.

Review round (cce1b2b to b34281a)

  • Every fallback picture (shading, pattern, soft mask, group) is bounded by the clip box: the SVG device now narrows the rasteriser's clip too, reads back, hashes and embeds only that box, in one reused buffer instead of a page-size NRGBA per picture. The sh operator itself painted a coverage grid of the whole page per call (wholeClip, pattern.go); it is now the clip box. Raster pixels unchanged (965 of 965 corpus pages identical to e54313c, rerun on this head).
  • Equal pictures (sha256 of the pixels) are defined once and used where they fall.
  • A rectangle clip inside a rectangle clip is their intersection, one clip; any other clip nests, capped at 64.
  • Reproductions, default options, 2000 x 2000 pt page:
page input before (cce1b2b, from the review) after
50 x sh under 0 0 1 1 re W n 1044 B 22.9 MB out, 40 GiB allocated, 2 min 13 s 3012 B out, 184 MiB allocated, 0.09 s
100 pattern-filled 2x2 rects 2294 B 6.3 GiB allocated 193 MiB allocated, 5771 B out
20000 x 0 0 100 100 re W n 380 KB 1.96 MB, rsvg stack overflow 1 clip, 329 B out (480 MiB allocated, in the rasteriser's own clip, not touched here)
a form with a gradient drawn 500 times 14.9 KB not measured 122 KB out, 199 MiB allocated
  • TestASmallFileCannotAskForMuchMemoryOrOutput asserts, for these four, allocated <= 16 B per page pixel + 2000 B per input byte, and written <= 4 KiB + 16 B per input byte. About 185 MiB of the allocation is the floor of a 16 Mpixel page (the rasteriser's image, painted grid, white and black papers).
  • Coverage 100 %. mutsweep over svg.go, svgdevice.go and svgverify.go: all caught by assertion, after removing one guard that duplicated another and testing the other. The survivors listed in pattern.go are on lines this PR does not touch (only wholeClip changed there, caught).
  • Timing against mutool draw -F svg: DEFERRED, 1-minute load was about 25-30 (limit 8). CI has not run on the base device-seam.

Seam changes

userPath.glyph, gstate.svgClip, renderPageWith (device chosen after the renderer exists), soft-mask forms are always drawn by the rasteriser, and wholeClip is the clip box.

Not done, referenced

Text render modes 4-7 (#137) and blend modes (#138) are not handled, as in the rasteriser. Knockout groups are not read. See the README section for the full list.

🤖 Generated with Claude Code

tannevaled and others added 5 commits November 22, 2026 17:52
PageSVG(d, i, w, SVGOptions) writes a page as SVG through a second
implementation of the device interface: paths, clips, images, groups and
text (as outlines, one <path> per glyph, <use> per letter). What SVG 1.1
cannot say (tiling and shading patterns, shadings, soft masks, groups under
a mask) is drawn by the rasteriser on a white and a black paper and embedded
as an <image> where it falls.

The document holds only numbers, generated ids and base64: href is #id or a
data:image/(png|jpeg);base64, URI, never script, foreignObject, style or on*
attributes, never NaN or Inf. Every document the suite draws is also written
as SVG and held to that allowlist; FuzzPageSVG does the same for any
content stream. Output is bounded by MaxBytes (ErrSVGTooLarge, nothing
written), pictures by a pixel cap, time by MaxDuration.

Raster output is unchanged: hashes of ten pages are identical to e54313c.
Known render bugs are referenced, not fixed (#137, #138).

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The four guards mutsweep could only see as a panic are asserted by tests that sit first in the package and turn a crash into a failure.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…ny error

The SVG device keeps its open groups as values on a stack and never a
*gstate past its run. clipPath ignores the rectangle the seam now carries.

PageSVG builds the document in memory, bounded by MaxBytes, and writes it in
one call: ErrSVGTooLarge, ErrTimedOut and every other error leave w empty.
A test counts the bytes and the calls on each error path.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…ns, check the allowlist in production

- The rasteriser's clip is narrowed by the SVG device too, so a fallback
  picture (shading, pattern, soft mask, group) is read back, hashed and
  embedded over the clip box only, in one reused buffer.
- The sh operator painted a grid of the whole page per call; it is now
  the clip box (pattern.go). Pixels are unchanged: 965 of 965 corpus
  pages identical to e54313c.
- Equal pictures are defined once and used where they fall (sha256 of
  the pixels).
- A rectangle clip inside a rectangle clip is their intersection; other
  clips nest at most 64 deep (ErrSVGTooDeep, nothing written).
- verifySVG reads the finished buffer against the allowlist before it
  is written, with no allocation but the ids.
- MaxDuration defaults to ten seconds.
- TestASmallFileCannotAskForMuchMemoryOrOutput asserts allocated and
  written bytes against stated constants, under default options.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…mutsweep

The unterminated-value guard was the same refusal as the one at the end of
the element and is removed; the quote guard gets the case that tells it
from the code after it.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…ts off the wall clock

overrun() read the clock only once every 256 operators and never when a
page ended, so a page with fewer operators was never measured: a 200-
operator page under a 1 ns MaxDuration returned nil on Windows. The page
now reads the clock once when it ends (late()), and the deadline and the
check go through a replaceable clock, so the test uses a fake clock
instead of wall time.

TestANestOfRectanglesIsOneClip and the twenty-thousand-clips case test
clip collapsing and output bounds, not time; under qemu they ran into the
new 10 s default. They now pass MaxDuration: time.Hour. Their clip and
allocation assertions are unchanged.

Refs #142 (the stride still lets a few expensive operators overrun).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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