Skip to content

Draw an SVG referenced by an <image> in the image's viewport - #547

Merged
deeplook merged 4 commits into
deeplook:mainfrom
RavSinghChandan:image-svg-viewport
Nov 12, 2026
Merged

deeplook merged 4 commits into
deeplook:mainfrom
RavSinghChandan:image-svg-viewport

Conversation

@RavSinghChandan

Copy link
Copy Markdown
Contributor

Fixes #237.

An <image> that references an SVG document draws that document at its own size and ignores the width, height and preserveAspectRatio of the <image>. With the files attached to #237, circle.svg (199.5mm square) is drawn about 560 units wide instead of filling the 68.8 x 69.9 rectangle drawn under it, where Inkscape and browsers place it.

SVG 1.1 §5.7 says the referenced document is rendered into the viewport of the <image>: the image's width and height replace the document's, and the image's preserveAspectRatio replaces the document's own. renderSvg() already fits a nested <svg> to its viewport with preserveAspectRatio, so the new _render_svg_in_image() sets these three attributes on the referenced root <svg> while it is rendered, and restores them afterwards, because the same document can be referenced again at another size (ExternalSVG caches it). An <image> without width/height keeps the document's size, as before (tests/samples/others/svg_in_svg.svg is unchanged), and a reference to a fragment (file.svg#id) is not affected.

With the #237 files, the circles now fill the rectangle exactly (bounds 50.6, 49.8 to 119.5, 119.7, the rectangle's box).

Added test_svg_in_image_fills_the_image_viewport, which compares an <image> of an SVG file with the same content as an inline nested <svg>, for preserveAspectRatio="none", the default and xMinYMax slice, plus a second reference to the same file at another size; it fails on main. Updated tests/public_api.json with --update for the new private name, and added a ### Fixed entry in CHANGELOG.md (I used #546, I'll fix it if the PR gets another number). pytest (without the network-dependent sample downloads), ruff format --check, ruff check and mypy src tests pass.

AI-assisted (Claude Code); I reviewed and tested the change

@deeplook
deeplook merged commit 877ec1a into deeplook:main Nov 12, 2026
16 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.

Embedded SVGs aren't rendered at the right scale

2 participants