Skip to content

Rasterize SVG into Image with lunasvg - #7678

Open
Grantim wants to merge 13 commits into
masterfrom
svg-rasterize-lunasvg
Open

Grantim wants to merge 13 commits into
masterfrom
svg-rasterize-lunasvg

Conversation

@Grantim

@Grantim Grantim commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Adds ImageLoad::fromSvg( file | data, resolution ) in the new MRIOExtras/MRSvgImage.h: renders an SVG into an Image of the given size (up to 32767 per side and fewer than 2^29 pixels, the limits of plutovg), scaled uniformly to fit and centered. It replaces the FreeType rasterizer of #7652 with lunasvg, which covers nearly all of static SVG (gradients, clip paths, masks, CSS, symbols, units).

New dependency: lunasvg 3.5.0 (MIT) with plutovg (MIT; its rasterizer and stroker are FreeType-derived, FTL). No fonts are loaded, so text is not drawn. An <image> is drawn from a data: URI or read from its file path, relative to the current directory. Two lunasvg 3.5.0 gaps are worked around: it does not skip a UTF-8 BOM, so fromSvg strips it; and it renders the missing side of an SVG with only width or only height as 100% of the viewBox, so fromSvg sets both to the size lunasvg reports.

  • vcpkg (Windows, Rocky, vcpkg Linux/macOS): registry ports lunasvg 3.5.0 and plutovg 1.3.3;
  • macOS Homebrew and Emscripten: submodule thirdparty/lunasvg at v3.5.0, with its bundled plutovg 1.3.1;
  • Ubuntu: CPM stage 1 at v3.5.0 (Ubuntu has no lunasvg package).

MRIOEXTRAS_NO_LUNASVG turns it off. Credits: ThirdpartyList.md, THIRD-PARTY-NOTICES.txt, manifest.json, SBOM sources.

The SVG must come from a trusted source (documented in the header), since some inputs take unbounded time or memory or crash: use references that multiply to ~10^6 drawn elements do not finish in reasonable time (#7652 returned an error there); a dash much shorter than its path (stroke-dasharray="0.001" on a path 10^5 long) never finishes, since plutovg sums dash lengths in a float; a CSS :first-of-type or :last-of-type selector loops forever when the adjacent sibling is of another type; a mask or clip path whose element covers more than about 23000 px per side overflows plutovg. The last two are fixed upstream after 3.5.0.

Other known lunasvg 3.5.0 behavior: an inherited fill="currentColor" resolves at the ancestor; opacity 0.5 gives alpha 127.

Test plan:

  • New MRIOExtras.SvgImage* tests (Unicode file name, orientation, fill rule, opacity, use, aspect ratio, viewBox fitting with only width or height, prolog with BOM / XML declaration / DOCTYPE, no text, currentColor, dashes, gradients / CSS / symbol / clip path, data: image, errors and resolution limits, hard inputs), Release and Debug, Windows MSBuild with vcpkg lunasvg 3.5.0
  • Full MRTest suite, Release, Windows MSBuild
  • 813 local icon SVGs render without errors, identical to lunasvg 470e78e
  • scripts/check_third_party_licenses.py, scripts/check_vcxproj.py
  • CI on all platforms (the only check of the CPM and submodule builds with plutovg 1.3.1; on Emscripten its static config needs find_package(Threads) from MRIOExtras)

Generated with Claude Code

Grantim and others added 3 commits December 4, 2026 13:46
ImageLoad::fromSvg renders an SVG at a given resolution, fitted uniformly in the center. lunasvg is built from a pinned commit on every platform with external resources and system fonts disabled, so an SVG never reads files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Opacity 0.5 gives alpha 127, and lunasvg resolves an inherited fill="currentColor" at the ancestor. use fan-out to 10^6 elements does not finish, so it is no longer in the test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lunasvg's package config calls find_dependency(plutovg) without hints, which misses a thirdparty root that is not on CMAKE_PREFIX_PATH (macOS Homebrew builds).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Grantim Grantim added the full-ci run all steps label Dec 4, 2026

@Fedr Fedr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code itself looks right: convertToRGBA un-premultiplies, the rows are flipped into Image order, and the resolution limit in the header matches plutovg's kMaxSize check (Bitmap::isNull also catches a non-positive size). Delegating to lunasvg is a good trade for icons with gradients, clip paths and CSS. A few things to settle before merging:

  1. Untrusted input. The PR body says use fan-out can stall the call, but the header does not. Since this is a public function, please add a line there saying the SVG must come from a trusted source, as icons do.

  2. The no-file-reading guarantee depends on the build. LUNASVG_DISABLE_EXTERNAL_RESOURCES is a PRIVATE compile definition of lunasvg, so a build that picks up a stock lunasvg reads the files an SVG refers to, with no sign of it in MeshLib. Examples are the official vcpkg port without the overlay, or a lunasvg found through CMAKE_PREFIX_PATH before the HINTS. The data: vs file test catches this in our CI only, so it is worth a comment next to find_package( lunasvg ).

  3. (nit) thirdparty/CMakeLists.txt builds lunasvg even when MRIOEXTRAS_NO_LUNASVG is ON.

  4. (nit) MRIOExtrasConfig.cmake has find_dependency( lunasvg ) for Emscripten, but lunasvg's config looks for plutovg without hints, which is the problem the third commit fixes for MeshLib itself. A find_dependency( plutovg ) before it would spare downstream Emscripten consumers the same failure.

@oitel oitel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is a local copy of the lunasvg's vcpkg port used?

Grantim and others added 5 commits December 4, 2026 16:25
plutovg computes byte sizes in int, so 2^29 pixels or more overflowed and crashed; such resolutions now return the resolution error. The image buffer is wrapped as the lunasvg bitmap instead of copying from a second one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Very fine dashes on a long path never finish in plutovg, and use references multiplying to about 10^6 elements stall lunasvg.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A viewBox with a non-zero origin is fitted in width and height of another aspect ratio. The embedded image is a fixed PNG literal, so the tests compile with MRIOEXTRAS_NO_PNG.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Both are private dependencies of MRIOExtras, not reachable from MeshLib's public headers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Grantim

Grantim commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review.

  1. Done in c4841aa: the header now says the SVG must come from a trusted source. Besides the use fan-out, a dash much shorter than its path never finishes either (plutovg sums dash lengths in a float). The resolution limit was not fully right: plutovg computes byte sizes in int, so e.g. 23200x23200 overflowed. ec93100 checks it explicitly (each side from 1 to 32767, fewer than 2^29 pixels) and documents it.
  2. Done in e0bff4e: a comment next to find_package(lunasvg).
  3. The thirdparty build is a separate CMake project that doesn't see the MRIOEXTRAS_* options; OpenCTM, draco and libE57Format are built there unconditionally as well.
  4. Downstream, find_dependency(lunasvg) has no hints, so the search path that finds lunasvg finds plutovg too, which is installed into the same prefix. MeshLib's own build needed the extra find_package only because it finds lunasvg via HINTS, which lunasvg's config doesn't pass on to plutovg.

@Grantim

Grantim commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

The registry port builds the v3.5.0 tag, which doesn't have LUNASVG_DISABLE_EXTERNAL_RESOURCES yet (it was added after the tag), and it passes neither that option nor LUNASVG_DISABLE_LOAD_SYSTEM_FONTS. Without them lunasvg loads the files an SVG refers to (<image href="file">). The overlay pins 470e78e, the first commit with the option, and turns both on. Even after a release with the option reaches the registry, an overlay is still needed to pass the options, unless the registry port gains a feature for them.

Grantim and others added 2 commits December 4, 2026 17:33
The SVG must be trusted anyway, so an <image> file path may be read; this drops the overlay port and the pin past 3.5.0. Fonts stay unloaded on every build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lunasvg 3.5.0 does not parse an SVG starting with a UTF-8 BOM, and the static config of its bundled plutovg 1.3.1 links Threads::Threads without finding it. Tests cover an editor prolog and that text is not drawn.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Grantim

Grantim commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up: lunasvg is now built without LUNASVG_DISABLE_EXTERNAL_RESOURCES. The SVG must come from a trusted source anyway, so reading an <image> file path is acceptable, and that option was the only reason for the overlay port and for pinning a commit past 3.5.0. vcpkg builds now use the registry ports lunasvg 3.5.0 and plutovg 1.3.3, and the submodule and CPM are at the v3.5.0 tag. So the overlay port that oitel asked about is gone, and the find_package(lunasvg) comment from Fedr's point 2 is removed as no longer true. The header says that file paths are read, relative to the current directory.

Grantim and others added 3 commits December 5, 2026 15:32
lunasvg 3.5.0 reports the missing side from the viewBox aspect ratio but renders it as 100% of the viewBox, so such an SVG was drawn smaller and off-centre. The reported size is now written back to the root element before rendering.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The removed hard inputs overflow float-to-long casts inside plutovg, so their result depends on the platform. Also lists the test file in the filters and plutovg-ft-types under FTL in the notices, and drops a redundant include.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
No other MeshLib source uses std::ranges algorithms, so the tests use the iterator forms that every CI toolchain has.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci run all steps

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants