Skip to content

fix(svg): export svg files without the #508 layout zoom (#552) - #553

Merged
tinchox5 merged 1 commit into
zumerlab:mainfrom
j8seangel:fix/552-svg-file-layout-zoom
Nov 3, 2026
Merged

tinchox5 merged 1 commit into
zumerlab:mainfrom
j8seangel:fix/552-svg-file-layout-zoom

Conversation

@j8seangel

Copy link
Copy Markdown
Contributor

Fixes #552.

Follows the approach from the issue thread: the layout zoom only pays off when the browser itself rasterizes the SVG, so SVG output that leaves as a file is serialized without the zoom wrapper, the same wrapper Safari and 1x screens already get. Raster exports keep it. No new option, and dpr is not involved.

Cause

On a real hi-DPI screen in Chromium and Firefox, composeAndSerialize lays the clone out on the device grid (#508): zoom: <dpr> on the foreignObject's container, scale(1/dpr) on a <g> around it, and foreignObject geometry multiplied by the zoom. Browsers cancel the two out. macOS Preview and Quick Look do not: inline <svg> children of the foreignObject draw at about 2x while the HTML text around them stays the right size. The zoom came only from liveLayoutZoom(view), so there was no way to get a downloadable SVG that Preview displays correctly.

Fix

  • src/engines/svg.js: when layoutZoom !== 1, composeAndSerialize also records the 1x wrapper next to the zoomed one in options.__fileSvg: the svg header with the foreignObject open tag, the container open tag, and the footer. Only the wrapper differs between the two (positions inside are the same CSS pixels), so the clone markup is not serialized twice. The 1x wrapper keeps the clip translate() and drops scale(), the container zoom and the zoomed fo geometry. At zoom 1 the field is reset to null, so a reused options bag never carries a previous capture's tags.
  • src/utils/helpers.js: fileSvgText(svgText, fileSvg) swaps the zoomed wrapper for the flat one. If the markup does not have the expected shape (no zoom, or a plugin rewrote it after render), it returns the input unchanged.
  • File exports go through it: toBlob({ format: 'svg' }) (and so download({ format: 'svg' })) in src/exporters/toBlob.js, and svgString() for plugins in src/api/snapdom.js.
  • Unchanged: result.url, toImg, toCanvas and every raster export keep the zoomed SVG, so the Layout becomes misaligned on mobile browsers #508 match with the page stays.

Cost: nothing at zoom 1. At zoom ≠ 1, two shallow-clone tag serializations per capture, and one string splice per file export.

Tests

New describe('svg files drop the layout zoom (#552)') in __tests__/engine.svg.layoutZoom.test.js, using stubHiDPI:

  • toBlob svg has no zoom and no scale(, its foreignObject is in CSS pixels, and url still has zoom: 2.625.
  • The static snapdom.toBlob shortcut gets the same file.
  • svgString() seen by a plugin is the 1x wrapper.
  • A clipped capture's file, drawn back to a canvas, puts the content in the same box as the 1x raster (pins the swapped wrapper's clip translate, fo x/y and padding).
  • A 1x capture's svg file is byte-identical to url.

Results:

  • Against main, the new zoomed cases fail:
  • npm test (chromium):
  • npm run test:full:
  • Downloaded SVG from Chrome on a Retina display opens correctly in macOS Preview / Quick Look:

This PR was prepared with AI assistance.

@tinchox5

tinchox5 commented Nov 3, 2026

Copy link
Copy Markdown
Member

Great thank you and welcome !!

@tinchox5
tinchox5 merged commit 071122d into zumerlab:main Nov 3, 2026
@j8seangel
j8seangel deleted the fix/552-svg-file-layout-zoom branch November 3, 2026 10:36
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.

SVG export from hi-DPI Chrome draws nested <svg> at 2× in macOS Preview

2 participants