From 3965af8c6cd124f58ef5fa31f4a535bab1f40d5f Mon Sep 17 00:00:00 2001 From: Grant Karapetyan Date: Fri, 4 Dec 2026 13:46:10 +0400 Subject: [PATCH 01/13] Rasterize SVG into Image with lunasvg 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 --- .../install-macos-thirdparty/action.yml | 1 + .gitmodules | 3 + doxygen/general_pages/ThirdpartyList.md | 2 + requirements/vcpkg-linux.txt | 1 + requirements/windows.txt | 1 + .../clone_submodules_emscripten_thirdparty.sh | 1 + scripts/clone_submodules_macos.sh | 1 + scripts/sbom/vcpkg-sources.json | 1 + source/MRIOExtras/CMakeLists.txt | 8 + source/MRIOExtras/MRIOExtras.vcxproj | 2 + source/MRIOExtras/MRIOExtrasConfig.cmake | 1 + source/MRIOExtras/MRSvgImage.cpp | 53 ++++ source/MRIOExtras/MRSvgImage.h | 21 ++ source/MRIOExtras/config_cmake.h.in | 1 + source/MRTest/MRSvgImageTests.cpp | 243 ++++++++++++++++ source/MRTest/MRTest.vcxproj | 1 + thirdparty/CMakeLists.txt | 6 + thirdparty/cpm/package-lock.cmake | 5 + thirdparty/cpm/stage1/CMakeLists.txt | 9 + thirdparty/licenses/THIRD-PARTY-NOTICES.txt | 269 ++++++++++++++++++ thirdparty/licenses/manifest.json | 30 ++ thirdparty/lunasvg | 1 + thirdparty/vcpkg/ports/lunasvg/portfile.cmake | 31 ++ thirdparty/vcpkg/ports/lunasvg/vcpkg.json | 19 ++ thirdparty/vcpkg/vcpkg.json | 1 + 25 files changed, 712 insertions(+) create mode 100644 source/MRIOExtras/MRSvgImage.cpp create mode 100644 source/MRIOExtras/MRSvgImage.h create mode 100644 source/MRTest/MRSvgImageTests.cpp create mode 160000 thirdparty/lunasvg create mode 100644 thirdparty/vcpkg/ports/lunasvg/portfile.cmake create mode 100644 thirdparty/vcpkg/ports/lunasvg/vcpkg.json diff --git a/.github/actions/install-macos-thirdparty/action.yml b/.github/actions/install-macos-thirdparty/action.yml index 4b32f14bc0e0..10608b7a7175 100644 --- a/.github/actions/install-macos-thirdparty/action.yml +++ b/.github/actions/install-macos-thirdparty/action.yml @@ -90,6 +90,7 @@ runs: thirdparty/tinygltf \ thirdparty/laz-perf \ thirdparty/draco \ + thirdparty/lunasvg \ thirdparty/clip \ thirdparty/fastmcpp shasum -a 256 \ diff --git a/.gitmodules b/.gitmodules index 83e508a0b61f..9e640971ec7a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -90,3 +90,6 @@ [submodule "thirdparty/draco"] path = thirdparty/draco url = https://github.com/google/draco +[submodule "thirdparty/lunasvg"] + path = thirdparty/lunasvg + url = https://github.com/sammycage/lunasvg.git diff --git a/doxygen/general_pages/ThirdpartyList.md b/doxygen/general_pages/ThirdpartyList.md index dbb12bdae1d8..9a1f042e3782 100644 --- a/doxygen/general_pages/ThirdpartyList.md +++ b/doxygen/general_pages/ThirdpartyList.md @@ -55,6 +55,8 @@ Below is a summary of all modules and their dependencies, so you can see what ea |Draco |library for compressing and decompressing 3D meshes and point clouds |Apache 2.0 | |libe57format |library for reading & writing the E57 file format |Boost Software License | |tinyxml2 |an efficient, C++ XML parser |zlib | +|lunasvg |SVG rendering library in C++ |MIT | +|plutovg |tiny 2D vector graphics library in C, used by lunasvg |MIT, FTL (FreeType License) | |nlohmann-json |JSON for Modern C++ — used by the glTF reader/writer |MIT | ### MRSymbolMesh diff --git a/requirements/vcpkg-linux.txt b/requirements/vcpkg-linux.txt index aa601ea42c13..ca15edd59ffb 100644 --- a/requirements/vcpkg-linux.txt +++ b/requirements/vcpkg-linux.txt @@ -23,6 +23,7 @@ libe57format libharu libjpeg-turbo libzip +lunasvg mimalloc opencascade-minimal openctm diff --git a/requirements/windows.txt b/requirements/windows.txt index 9f2a3db802c9..06c8e118c1cb 100644 --- a/requirements/windows.txt +++ b/requirements/windows.txt @@ -19,6 +19,7 @@ laz-perf libe57format libharu libzip +lunasvg mimalloc[override] opencascade-minimal openctm diff --git a/scripts/clone_submodules_emscripten_thirdparty.sh b/scripts/clone_submodules_emscripten_thirdparty.sh index 0448816f9f43..eedc66273d38 100755 --- a/scripts/clone_submodules_emscripten_thirdparty.sh +++ b/scripts/clone_submodules_emscripten_thirdparty.sh @@ -18,6 +18,7 @@ SCRIPT_DIR="$(dirname "$BASH_SOURCE")" thirdparty/libE57Format \ thirdparty/libjpeg-turbo \ thirdparty/libzip \ + thirdparty/lunasvg \ thirdparty/mbedtls \ thirdparty/onetbb \ thirdparty/OpenCTM-git \ diff --git a/scripts/clone_submodules_macos.sh b/scripts/clone_submodules_macos.sh index 8a6937cef026..4ac0f5139519 100755 --- a/scripts/clone_submodules_macos.sh +++ b/scripts/clone_submodules_macos.sh @@ -16,6 +16,7 @@ if [[ $1 != --vcpkg ]]; then thirdparty/glad thirdparty/laz-perf thirdparty/libE57Format + thirdparty/lunasvg thirdparty/OpenCTM-git thirdparty/tinygltf ) diff --git a/scripts/sbom/vcpkg-sources.json b/scripts/sbom/vcpkg-sources.json index b6cd0ce64df2..f930268e9f65 100644 --- a/scripts/sbom/vcpkg-sources.json +++ b/scripts/sbom/vcpkg-sources.json @@ -17,6 +17,7 @@ "Draco": ["draco"], "libE57Format": ["libe57format"], "tinyxml2": ["tinyxml2"], + "lunasvg": ["lunasvg"], "nlohmann-json": ["nlohmann-json"], "OpenVDB": ["openvdb"], "GDCM": ["gdcm"], diff --git a/source/MRIOExtras/CMakeLists.txt b/source/MRIOExtras/CMakeLists.txt index f5cc823523bc..d63399210886 100644 --- a/source/MRIOExtras/CMakeLists.txt +++ b/source/MRIOExtras/CMakeLists.txt @@ -6,6 +6,7 @@ option(MRIOEXTRAS_NO_E57 "Disable E57 support" OFF) option(MRIOEXTRAS_NO_GLTF "Disable glTF support" OFF) option(MRIOEXTRAS_NO_JPEG "Disable JPEG support" OFF) option(MRIOEXTRAS_NO_LAS "Disable LAS/LAZ support" OFF) +option(MRIOEXTRAS_NO_LUNASVG "Disable SVG rasterization" OFF) option(MRIOEXTRAS_NO_PDF "Disable PDF support" OFF) option(MRIOEXTRAS_NO_PNG "Disable PNG support" OFF) option(MRIOEXTRAS_NO_STEP "Disable STEP support" OFF) @@ -74,6 +75,13 @@ IF(NOT MRIOEXTRAS_NO_LAS) target_link_libraries(${PROJECT_NAME} PRIVATE LAZPERF::lazperf) ENDIF() +IF(NOT MRIOEXTRAS_NO_LUNASVG) + find_package(lunasvg CONFIG REQUIRED + HINTS "${MESHLIB_THIRDPARTY_ROOT_DIR}" + ) + target_link_libraries(${PROJECT_NAME} PRIVATE lunasvg::lunasvg) +ENDIF() + IF(NOT MRIOEXTRAS_NO_PDF) target_link_libraries(${PROJECT_NAME} PRIVATE hpdf) ENDIF() diff --git a/source/MRIOExtras/MRIOExtras.vcxproj b/source/MRIOExtras/MRIOExtras.vcxproj index 6b2c4f1c664e..94d0dd035b8a 100644 --- a/source/MRIOExtras/MRIOExtras.vcxproj +++ b/source/MRIOExtras/MRIOExtras.vcxproj @@ -45,6 +45,7 @@ + @@ -65,6 +66,7 @@ + 15.0 diff --git a/source/MRIOExtras/MRIOExtrasConfig.cmake b/source/MRIOExtras/MRIOExtrasConfig.cmake index 6288fa1ba3d0..5739261016b4 100644 --- a/source/MRIOExtras/MRIOExtrasConfig.cmake +++ b/source/MRIOExtras/MRIOExtrasConfig.cmake @@ -5,6 +5,7 @@ if(EMSCRIPTEN) find_dependency(OpenCTM) find_dependency(draco) find_dependency(LAZPERF) + find_dependency(lunasvg) endif() include("${CMAKE_CURRENT_LIST_DIR}/MRIOExtrasTargets.cmake") diff --git a/source/MRIOExtras/MRSvgImage.cpp b/source/MRIOExtras/MRSvgImage.cpp new file mode 100644 index 000000000000..52448fe08af7 --- /dev/null +++ b/source/MRIOExtras/MRSvgImage.cpp @@ -0,0 +1,53 @@ +#include "MRSvgImage.h" +#ifndef MRIOEXTRAS_NO_LUNASVG +#include +#include +#include + +#include + +#include +#include +#include + +namespace MR::ImageLoad +{ + +Expected fromSvg( const std::filesystem::path& file, const Vector2i& resolution ) +{ + std::ifstream in( file, std::ifstream::binary ); + if ( !in ) + return unexpected( "Cannot open file for reading " + utf8string( file ) ); + + return addFileNameInError( readCharBuffer( in ).and_then( [&] ( Buffer&& buf ) + { + return fromSvg( buf.data(), buf.size(), resolution ); + } ), file ); +} + +Expected fromSvg( const char* data, size_t size, const Vector2i& resolution ) +{ + const auto doc = lunasvg::Document::loadFromData( data, size ); + if ( !doc ) + return unexpected( "Failed to parse SVG" ); + const float width = doc->width(); + const float height = doc->height(); + if ( !( width > 0 && height > 0 ) ) + return unexpected( "SVG has empty size" ); + + lunasvg::Bitmap bitmap( resolution.x, resolution.y ); + if ( bitmap.isNull() ) + return unexpected( "Invalid image resolution" ); + const float scale = std::min( resolution.x / width, resolution.y / height ); + doc->render( bitmap, lunasvg::Matrix( scale, 0, 0, scale, ( resolution.x - scale * width ) / 2, ( resolution.y - scale * height ) / 2 ) ); + bitmap.convertToRGBA(); + + Image image{ .pixels = std::vector( size_t( resolution.x ) * resolution.y ), .resolution = resolution }; + // bitmap rows go from top to bottom, image rows from bottom to top + for ( int y = 0; y < resolution.y; ++y ) + std::memcpy( &image.pixels[size_t( resolution.y - 1 - y ) * resolution.x], bitmap.data() + size_t( y ) * bitmap.stride(), resolution.x * sizeof( Color ) ); + return image; +} + +} // namespace MR::ImageLoad +#endif diff --git a/source/MRIOExtras/MRSvgImage.h b/source/MRIOExtras/MRSvgImage.h new file mode 100644 index 000000000000..9dd49c5d4a1b --- /dev/null +++ b/source/MRIOExtras/MRSvgImage.h @@ -0,0 +1,21 @@ +#pragma once + +#include "config.h" +#ifndef MRIOEXTRAS_NO_LUNASVG +#include "exports.h" + +#include +#include + +#include + +namespace MR::ImageLoad +{ + +/// rasterizes .SVG file into an image of given resolution (from 1 to 32767 in each dimension), the SVG is scaled uniformly to fit the image and centered; +/// unsupported SVG features are skipped, images are drawn only from data: URIs, and text is not drawn +MRIOEXTRAS_API Expected fromSvg( const std::filesystem::path& file, const Vector2i& resolution ); +MRIOEXTRAS_API Expected fromSvg( const char* data, size_t size, const Vector2i& resolution ); + +} // namespace MR::ImageLoad +#endif diff --git a/source/MRIOExtras/config_cmake.h.in b/source/MRIOExtras/config_cmake.h.in index 4159ba200920..dd78c7c87100 100644 --- a/source/MRIOExtras/config_cmake.h.in +++ b/source/MRIOExtras/config_cmake.h.in @@ -6,6 +6,7 @@ #cmakedefine MRIOEXTRAS_NO_GLTF #cmakedefine MRIOEXTRAS_NO_JPEG #cmakedefine MRIOEXTRAS_NO_LAS +#cmakedefine MRIOEXTRAS_NO_LUNASVG #cmakedefine MRIOEXTRAS_NO_PDF #cmakedefine MRIOEXTRAS_NO_PNG #cmakedefine MRIOEXTRAS_NO_STEP diff --git a/source/MRTest/MRSvgImageTests.cpp b/source/MRTest/MRSvgImageTests.cpp new file mode 100644 index 000000000000..aed61b44dffc --- /dev/null +++ b/source/MRTest/MRSvgImageTests.cpp @@ -0,0 +1,243 @@ +#include +#ifndef MRIOEXTRAS_NO_LUNASVG +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace MR +{ + +static Expected rasterize( const std::string& svg, const Vector2i& res ) +{ + return ImageLoad::fromSvg( svg.data(), svg.size(), res ); +} + +// a file with symbols outside of any single code page in its name must be opened on Windows as well +TEST( MRIOExtras, SvgImageUnicodeFileName ) +{ + UniqueTemporaryFolder folder; + ASSERT_TRUE( bool( folder ) ); + const auto path = folder / std::filesystem::path( u8"тест 文件 é.svg" ); + std::ofstream( path, std::ofstream::binary ) << R"()"; + + const auto image = ImageLoad::fromSvg( path, { 2, 1 } ); + ASSERT_TRUE( image ) << image.error(); + EXPECT_EQ( ( *image )[Vector2i( 0, 0 )], Color::black() ); + EXPECT_EQ( ( *image )[Vector2i( 1, 0 )], Color::transparent() ); + + const auto absent = ImageLoad::fromSvg( folder / "absent.svg", { 2, 1 } ); + ASSERT_FALSE( absent ); + EXPECT_TRUE( absent.error().starts_with( "Cannot open file for reading" ) ) << absent.error(); +} + +TEST( MRIOExtras, SvgImageRasterize ) +{ + // red top-left quadrant, and blue horizontal line 1 wide at y = 3 + const auto image = rasterize( R"svg( + + +)svg", { 8, 8 } ); + ASSERT_TRUE( image ) << image.error(); + ASSERT_EQ( image->resolution, Vector2i( 8, 8 ) ); + // image rows go from bottom to top + EXPECT_EQ( ( *image )[Vector2i( 1, 6 )], Color::red() ); + EXPECT_EQ( ( *image )[Vector2i( 6, 6 )], Color::transparent() ); + EXPECT_EQ( ( *image )[Vector2i( 5, 3 )], Color::transparent() ); + EXPECT_EQ( ( *image )[Vector2i( 5, 2 )], Color::blue() ); + EXPECT_EQ( ( *image )[Vector2i( 5, 1 )], Color::blue() ); + EXPECT_EQ( ( *image )[Vector2i( 5, 0 )], Color::transparent() ); + + // the stroke width is scaled by the group transform, so the line at y = 4 is 2 pixels wide + const auto scaled = rasterize( R"svg()svg", { 8, 8 } ); + ASSERT_TRUE( scaled ) << scaled.error(); + for ( int y = 2; y <= 5; ++y ) + EXPECT_EQ( ( *scaled )[Vector2i( 4, y )], y == 3 || y == 4 ? Color::black() : Color::transparent() ) << y; +} + +TEST( MRIOExtras, SvgImageFillRuleOpacity ) +{ + const auto ring = [] ( const std::string& rule ) + { + return rasterize( R"()", { 4, 4 } ); + }; + const auto evenOdd = ring( "evenodd" ); + ASSERT_TRUE( evenOdd ) << evenOdd.error(); + EXPECT_EQ( ( *evenOdd )[Vector2i( 0, 0 )], Color::black() ); + EXPECT_EQ( ( *evenOdd )[Vector2i( 1, 1 )], Color::transparent() ); + const auto nonZero = ring( "nonzero" ); + ASSERT_TRUE( nonZero ) << nonZero.error(); + EXPECT_EQ( ( *nonZero )[Vector2i( 1, 1 )], Color::black() ); + + // group opacity applies to the group as a whole, so the overlap of its rectangles is not darker + const auto image = rasterize( R"()", { 2, 1 } ); + ASSERT_TRUE( image ) << image.error(); + EXPECT_EQ( ( *image )[Vector2i( 0, 0 )], Color( 255, 255, 255, 128 ) ); + EXPECT_EQ( ( *image )[Vector2i( 1, 0 )], Color( 255, 255, 255, 128 ) ); +} + +// use draws the referenced element moved by x, y and its transform, which inherits the style of use but keeps its own, also through a group or another use; +// opacity of use applies to the drawn element, the elements in defs are not drawn by themselves, and a reference to a missing element draws nothing +TEST( MRIOExtras, SvgImageUse ) +{ + const auto image = rasterize( R"svg()svg" + R"svg()svg" + R"svg()svg" + R"svg()svg" + R"svg()svg", { 7, 1 } ); + ASSERT_TRUE( image ) << image.error(); + const Color expected[] = { Color::red(), Color::blue(), Color::green(), Color::red(), Color::blue(), Color( 0, 0, 0, 128 ), Color::transparent() }; + for ( int x = 0; x < 7; ++x ) + EXPECT_EQ( ( *image )[Vector2i( x, 0 )], expected[x] ) << x; +} + +// the SVG is fitted uniformly in the center of a non-square image +TEST( MRIOExtras, SvgImageAspectRatio ) +{ + const std::string svg = R"()"; + for ( const auto& res : { Vector2i( 4, 2 ), Vector2i( 2, 4 ) } ) + { + const auto image = rasterize( svg, res ); + ASSERT_TRUE( image ) << image.error(); + // columns or rows 0 and 3 are empty + for ( int y = 0; y < res.y; ++y ) + for ( int x = 0; x < res.x; ++x ) + EXPECT_EQ( ( *image )[Vector2i( x, y )], ( res.x > res.y ? x : y ) % 3 == 0 ? Color::transparent() : Color::black() ) << x << ' ' << y; + } +} + +// currentColor is the color property of the drawn element even if it is inherited or set after it +TEST( MRIOExtras, SvgImageCurrentColor ) +{ + const auto image = rasterize( R"()" + R"()" + R"()", { 9, 3 } ); + ASSERT_TRUE( image ) << image.error(); + for ( int x : { 1, 4, 7 } ) + EXPECT_EQ( ( *image )[Vector2i( x, 1 )], Color::red() ) << x; +} + +TEST( MRIOExtras, SvgImageDashes ) +{ + // a stroke through the only row of pixels, dash ends are on pixel borders; alpha of at least hi is '#', at most lo is '.', otherwise '?' + const auto row = [] ( const std::string& stroke, int lo = 0, int hi = 255 ) + { + const auto image = rasterize( R"svg(", { 10, 1 } ); + if ( !image ) + return image.error(); + std::string res; + for ( const auto& c : image->pixels ) + res += c.a >= hi ? '#' : c.a <= lo ? '.' : '?'; + return res; + }; + EXPECT_EQ( row( R"(d="M0 0.5H8" stroke-dasharray="2")" ), "##..##...." ); + EXPECT_EQ( row( R"(d="M0 0.5H8" stroke-dasharray="2 2" stroke-dashoffset="1")" ), "#..##..#.." ); + EXPECT_EQ( row( R"(d="M0 0.5H8" stroke-dasharray="2,2" stroke-dashoffset="-1")" ), ".##..##..." ); + EXPECT_EQ( row( R"(d="M0 0.5H8" style="stroke-dasharray: 1, 2, 3")" ), "#..###.#.." ); + EXPECT_EQ( row( R"(d="M0 0.5H8" stroke-dasharray="0 0")" ), "########.." ); +} + +// features beyond plain shapes: gradients, style sheets, symbols, clip paths as exported from Figma +TEST( MRIOExtras, SvgImageFeatures ) +{ + const auto image = rasterize( R"svg()svg" + R"svg()svg" + R"svg()svg" + R"svg()svg" + R"svg()svg", { 8, 2 } ); + ASSERT_TRUE( image ) << image.error(); + const Color expected[] = { Color::red(), Color::red(), Color::blue(), Color::blue(), Color::green(), Color::green(), Color::black(), Color::transparent() }; + for ( int x = 0; x < 8; ++x ) + EXPECT_EQ( ( *image )[Vector2i( x, 0 )], expected[x] ) << x; +} + +// an image is drawn from a data: URI, but never read from a file +TEST( MRIOExtras, SvgImageExternalImage ) +{ + UniqueTemporaryFolder folder; + ASSERT_TRUE( bool( folder ) ); + std::ostringstream png; + ASSERT_TRUE( ImageSave::toPng( Image{ .pixels = { Color::red() }, .resolution = { 1, 1 } }, png ) ); + const auto bytes = png.str(); + const auto path = folder / "red.png"; + std::ofstream( path, std::ofstream::binary ) << bytes; + + const auto draw = [] ( const std::string& href ) + { + return rasterize( R"()", { 1, 1 } ); + }; + const auto embedded = draw( "data:image/png;base64," + encode64( ( const std::uint8_t* )bytes.data(), bytes.size() ) ); + ASSERT_TRUE( embedded ) << embedded.error(); + EXPECT_EQ( ( *embedded )[Vector2i( 0, 0 )], Color::red() ); + const auto file = draw( utf8string( path ) ); + ASSERT_TRUE( file ) << file.error(); + EXPECT_EQ( ( *file )[Vector2i( 0, 0 )], Color::transparent() ); +} + +TEST( MRIOExtras, SvgImageErrors ) +{ + const std::pair errors[] = { + { "", "Failed to parse SVG" }, + { "", "Failed to parse SVG" }, + { "", "SVG has empty size" }, + { R"()", "SVG has empty size" }, + }; + for ( const auto& [svg, error] : errors ) + { + const auto image = rasterize( svg, { 1, 1 } ); + ASSERT_FALSE( image ) << svg; + EXPECT_EQ( image.error(), error ) << svg; + } + for ( const Vector2i& res : { Vector2i( 0, 1 ), Vector2i( -1, 10 ), Vector2i( 32768, 1 ) } ) + { + const auto invalid = rasterize( R"()", res ); + ASSERT_FALSE( invalid ) << res.x << " " << res.y; + EXPECT_EQ( invalid.error(), "Invalid image resolution" ); + } +} + +// inputs that made other rasterizers fail or loop: huge and invalid coordinates, deep and cyclic references, references multiplying the drawn elements +TEST( MRIOExtras, SvgImageHardInputs ) +{ + const auto chain = [] ( int length ) + { + std::string shapes = R"()"; + for ( int i = 1; i <= length; ++i ) + shapes += ""; + return shapes + ""; + }; + std::string fanOut = R"()"; + for ( int i = 1; i <= 6; ++i ) + { + fanOut += ""; + for ( int j = 0; j < 10; ++j ) + fanOut += ""; + fanOut += ""; + } + fanOut += R"()"; + const std::string shapes[] = { + chain( 200 ), + fanOut, + R"svg()svg", + R"svg()svg", + R"()", + R"()", + R"()", + R"()", + R"()", + R"()", + }; + for ( const auto& s : shapes ) + { + const auto image = rasterize( R"()" + s + "", { 2, 1 } ); + EXPECT_TRUE( image ) << s.substr( 0, 100 ) << ": " << image.error(); + } +} + +} // namespace MR +#endif diff --git a/source/MRTest/MRTest.vcxproj b/source/MRTest/MRTest.vcxproj index c2cd58e31829..ca275a5e5b6f 100644 --- a/source/MRTest/MRTest.vcxproj +++ b/source/MRTest/MRTest.vcxproj @@ -74,6 +74,7 @@ + diff --git a/thirdparty/CMakeLists.txt b/thirdparty/CMakeLists.txt index ea7f3d1ca9c7..222278223f06 100644 --- a/thirdparty/CMakeLists.txt +++ b/thirdparty/CMakeLists.txt @@ -201,3 +201,9 @@ IF(NOT draco_FOUND) ENDIF() add_subdirectory(./draco) ENDIF() + +# always built from the submodule: packaged lunasvg loads the files an SVG refers to +set(LUNASVG_BUILD_EXAMPLES OFF) +set(LUNASVG_DISABLE_EXTERNAL_RESOURCES ON) +set(LUNASVG_DISABLE_LOAD_SYSTEM_FONTS ON) +add_subdirectory(./lunasvg) diff --git a/thirdparty/cpm/package-lock.cmake b/thirdparty/cpm/package-lock.cmake index ccbfe41541c2..2db70bd1480b 100644 --- a/thirdparty/cpm/package-lock.cmake +++ b/thirdparty/cpm/package-lock.cmake @@ -124,6 +124,11 @@ set(MESHLIB_PACKAGE_libzip GIT_TAG v1.11.4 ) +set(MESHLIB_PACKAGE_lunasvg + GIT_REPOSITORY https://github.com/sammycage/lunasvg.git + GIT_TAG 470e78e2953dcf0d4b46322d64eed6ca84156fec +) + set(MESHLIB_PACKAGE_mbedtls GIT_REPOSITORY https://github.com/Mbed-TLS/mbedtls GIT_TAG v3.5.0 diff --git a/thirdparty/cpm/stage1/CMakeLists.txt b/thirdparty/cpm/stage1/CMakeLists.txt index bf1e63abe628..0b66a3557754 100644 --- a/thirdparty/cpm/stage1/CMakeLists.txt +++ b/thirdparty/cpm/stage1/CMakeLists.txt @@ -207,6 +207,15 @@ if(NOT draco_FOUND) add_draco() endif() +# always built: packaged lunasvg loads the files an SVG refers to +function(add_lunasvg) + set(LUNASVG_BUILD_EXAMPLES OFF) + set(LUNASVG_DISABLE_EXTERNAL_RESOURCES ON) + set(LUNASVG_DISABLE_LOAD_SYSTEM_FONTS ON) + meshlib_add_package(lunasvg) +endfunction() +add_lunasvg() + if(EMSCRIPTEN) find_package(libjpeg-turbo) if(NOT libjpeg-turbo_FOUND) diff --git a/thirdparty/licenses/THIRD-PARTY-NOTICES.txt b/thirdparty/licenses/THIRD-PARTY-NOTICES.txt index 14203fb424be..acda31bebaa8 100644 --- a/thirdparty/licenses/THIRD-PARTY-NOTICES.txt +++ b/thirdparty/licenses/THIRD-PARTY-NOTICES.txt @@ -2916,6 +2916,275 @@ must not be misrepresented as being the original software. distribution. +################################################################################ +lunasvg -- MIT +https://github.com/sammycage/lunasvg +################################################################################ + +MIT License + +Copyright (c) 2020-2026 Samuel Ugochukwu + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + +################################################################################ +plutovg -- MIT AND FTL +https://github.com/sammycage/plutovg +################################################################################ + +----- LICENSE ----- + +MIT License + +Copyright (c) 2020-2026 Samuel Ugochukwu + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +----- source/FTL.TXT (plutovg-ft-math, plutovg-ft-raster, plutovg-ft-stroker) ----- + + The FreeType Project LICENSE + ---------------------------- + + 2006-Jan-27 + + Copyright 1996-2002, 2006 by + David Turner, Robert Wilhelm, and Werner Lemberg + + + +Introduction +============ + + The FreeType Project is distributed in several archive packages; + some of them may contain, in addition to the FreeType font engine, + various tools and contributions which rely on, or relate to, the + FreeType Project. + + This license applies to all files found in such packages, and + which do not fall under their own explicit license. The license + affects thus the FreeType font engine, the test programs, + documentation and makefiles, at the very least. + + This license was inspired by the BSD, Artistic, and IJG + (Independent JPEG Group) licenses, which all encourage inclusion + and use of free software in commercial and freeware products + alike. As a consequence, its main points are that: + + o We don't promise that this software works. However, we will be + interested in any kind of bug reports. (`as is' distribution) + + o You can use this software for whatever you want, in parts or + full form, without having to pay us. (`royalty-free' usage) + + o You may not pretend that you wrote this software. If you use + it, or only parts of it, in a program, you must acknowledge + somewhere in your documentation that you have used the + FreeType code. (`credits') + + We specifically permit and encourage the inclusion of this + software, with or without modifications, in commercial products. + We disclaim all warranties covering The FreeType Project and + assume no liability related to The FreeType Project. + + + Finally, many people asked us for a preferred form for a + credit/disclaimer to use in compliance with this license. We thus + encourage you to use the following text: + + """ + Portions of this software are copyright � The FreeType + Project (www.freetype.org). All rights reserved. + """ + + Please replace with the value from the FreeType version you + actually use. + + +Legal Terms +=========== + +0. Definitions +-------------- + + Throughout this license, the terms `package', `FreeType Project', + and `FreeType archive' refer to the set of files originally + distributed by the authors (David Turner, Robert Wilhelm, and + Werner Lemberg) as the `FreeType Project', be they named as alpha, + beta or final release. + + `You' refers to the licensee, or person using the project, where + `using' is a generic term including compiling the project's source + code as well as linking it to form a `program' or `executable'. + This program is referred to as `a program using the FreeType + engine'. + + This license applies to all files distributed in the original + FreeType Project, including all source code, binaries and + documentation, unless otherwise stated in the file in its + original, unmodified form as distributed in the original archive. + If you are unsure whether or not a particular file is covered by + this license, you must contact us to verify this. + + The FreeType Project is copyright (C) 1996-2000 by David Turner, + Robert Wilhelm, and Werner Lemberg. All rights reserved except as + specified below. + +1. No Warranty +-------------- + + THE FREETYPE PROJECT IS PROVIDED `AS IS' WITHOUT WARRANTY OF ANY + KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + PURPOSE. IN NO EVENT WILL ANY OF THE AUTHORS OR COPYRIGHT HOLDERS + BE LIABLE FOR ANY DAMAGES CAUSED BY THE USE OR THE INABILITY TO + USE, OF THE FREETYPE PROJECT. + +2. Redistribution +----------------- + + This license grants a worldwide, royalty-free, perpetual and + irrevocable right and license to use, execute, perform, compile, + display, copy, create derivative works of, distribute and + sublicense the FreeType Project (in both source and object code + forms) and derivative works thereof for any purpose; and to + authorize others to exercise some or all of the rights granted + herein, subject to the following conditions: + + o Redistribution of source code must retain this license file + (`FTL.TXT') unaltered; any additions, deletions or changes to + the original files must be clearly indicated in accompanying + documentation. The copyright notices of the unaltered, + original files must be preserved in all copies of source + files. + + o Redistribution in binary form must provide a disclaimer that + states that the software is based in part of the work of the + FreeType Team, in the distribution documentation. We also + encourage you to put an URL to the FreeType web page in your + documentation, though this isn't mandatory. + + These conditions apply to any software derived from or based on + the FreeType Project, not just the unmodified files. If you use + our work, you must acknowledge us. However, no fee need be paid + to us. + +3. Advertising +-------------- + + Neither the FreeType authors and contributors nor you shall use + the name of the other for commercial, advertising, or promotional + purposes without specific prior written permission. + + We suggest, but do not require, that you use one or more of the + following phrases to refer to this software in your documentation + or advertising materials: `FreeType Project', `FreeType Engine', + `FreeType library', or `FreeType Distribution'. + + As you have not signed this license, you are not required to + accept it. However, as the FreeType Project is copyrighted + material, only this license, or another one contracted with the + authors, grants you the right to use, distribute, and modify it. + Therefore, by using, distributing, or modifying the FreeType + Project, you indicate that you understand and accept all the terms + of this license. + +4. Contacts +----------- + + There are two mailing lists related to FreeType: + + o freetype@nongnu.org + + Discusses general use and applications of FreeType, as well as + future and wanted additions to the library and distribution. + If you are looking for support, start in this list if you + haven't found anything to help you in the documentation. + + o freetype-devel@nongnu.org + + Discusses bugs, as well as engine internals, design issues, + specific licenses, porting, etc. + + Our home page can be found at + + http://www.freetype.org + +----- stb_image, stb_image_write, stb_truetype ----- + +------------------------------------------------------------------------------ +This software is available under 2 licenses -- choose whichever you prefer. +------------------------------------------------------------------------------ +ALTERNATIVE A - MIT License +Copyright (c) 2017 Sean Barrett +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +------------------------------------------------------------------------------ +ALTERNATIVE B - Public Domain (www.unlicense.org) +This is free and unencumbered software released into the public domain. +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to +this software under copyright law. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +------------------------------------------------------------------------------ + + ################################################################################ nlohmann-json -- MIT https://github.com/nlohmann/json diff --git a/thirdparty/licenses/manifest.json b/thirdparty/licenses/manifest.json index 226b23924542..cd5bd580e969 100644 --- a/thirdparty/licenses/manifest.json +++ b/thirdparty/licenses/manifest.json @@ -378,6 +378,36 @@ }, "version": "321ea883b7190d4e85cae5512a12e5eaa8f8731f" }, + { + "id": "lunasvg", + "modules": [ + "MRIOExtras" + ], + "license": "MIT", + "upstream": "https://github.com/sammycage/lunasvg", + "source": { + "type": "submodule", + "path": "thirdparty/lunasvg" + }, + "version": "470e78e2953dcf0d4b46322d64eed6ca84156fec" + }, + { + "id": "plutovg", + "modules": [ + "MRIOExtras" + ], + "license": "MIT AND FTL", + "upstream": "https://github.com/sammycage/plutovg", + "source": { + "type": "vcpkg-registry", + "ports": [ + "plutovg" + ] + }, + "version": { + "plutovg": "1.3.3#0" + } + }, { "id": "nlohmann-json", "modules": [ diff --git a/thirdparty/lunasvg b/thirdparty/lunasvg new file mode 160000 index 000000000000..470e78e2953d --- /dev/null +++ b/thirdparty/lunasvg @@ -0,0 +1 @@ +Subproject commit 470e78e2953dcf0d4b46322d64eed6ca84156fec diff --git a/thirdparty/vcpkg/ports/lunasvg/portfile.cmake b/thirdparty/vcpkg/ports/lunasvg/portfile.cmake new file mode 100644 index 000000000000..c6f7a3b8a837 --- /dev/null +++ b/thirdparty/vcpkg/ports/lunasvg/portfile.cmake @@ -0,0 +1,31 @@ +vcpkg_from_github( + OUT_SOURCE_PATH SOURCE_PATH + REPO sammycage/lunasvg + # v3.5.0 has no LUNASVG_DISABLE_EXTERNAL_RESOURCES yet + REF 470e78e2953dcf0d4b46322d64eed6ca84156fec + SHA512 f258ebeb17876f22786b4df25b2f01314e04917dad7061a5413e43bc5e9668904ed4527c138350bc4755e3f30cda1fb802391c67cc6fff2e2cbe775224b11808 + HEAD_REF master +) + +vcpkg_cmake_configure( + SOURCE_PATH "${SOURCE_PATH}" + OPTIONS + -DCMAKE_REQUIRE_FIND_PACKAGE_plutovg=1 + -DUSE_SYSTEM_PLUTOVG=ON + -DLUNASVG_BUILD_EXAMPLES=OFF + -DLUNASVG_DISABLE_EXTERNAL_RESOURCES=ON + -DLUNASVG_DISABLE_LOAD_SYSTEM_FONTS=ON +) + +vcpkg_cmake_install() +vcpkg_copy_pdbs() +vcpkg_cmake_config_fixup(CONFIG_PATH lib/cmake/lunasvg) +vcpkg_fixup_pkgconfig() + +if(VCPKG_LIBRARY_LINKAGE STREQUAL "static") + vcpkg_replace_string("${CURRENT_PACKAGES_DIR}/include/lunasvg/lunasvg.h" "defined(LUNASVG_BUILD_STATIC)" "1") +endif() + +file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include") + +vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE") diff --git a/thirdparty/vcpkg/ports/lunasvg/vcpkg.json b/thirdparty/vcpkg/ports/lunasvg/vcpkg.json new file mode 100644 index 000000000000..14c3065c3307 --- /dev/null +++ b/thirdparty/vcpkg/ports/lunasvg/vcpkg.json @@ -0,0 +1,19 @@ +{ + "name": "lunasvg", + "version": "3.5.0", + "port-version": 1, + "description": "lunasvg is a standalone SVG rendering library in C++", + "homepage": "https://github.com/sammycage/lunasvg", + "license": "MIT", + "dependencies": [ + "plutovg", + { + "name": "vcpkg-cmake", + "host": true + }, + { + "name": "vcpkg-cmake-config", + "host": true + } + ] +} diff --git a/thirdparty/vcpkg/vcpkg.json b/thirdparty/vcpkg/vcpkg.json index 09c75526773c..9489cc869f17 100644 --- a/thirdparty/vcpkg/vcpkg.json +++ b/thirdparty/vcpkg/vcpkg.json @@ -28,6 +28,7 @@ "libharu", "libjpeg-turbo", "libzip", + "lunasvg", "opencascade-minimal", "openctm", "openvdb", From 7356b4af1b7a5b227d1aebef02e860cad584a341 Mon Sep 17 00:00:00 2001 From: Grant Karapetyan Date: Fri, 4 Dec 2026 15:13:20 +0400 Subject: [PATCH 02/13] Match SVG image tests to lunasvg output 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 --- source/MRTest/MRSvgImageTests.cpp | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/source/MRTest/MRSvgImageTests.cpp b/source/MRTest/MRSvgImageTests.cpp index aed61b44dffc..5e079799c422 100644 --- a/source/MRTest/MRSvgImageTests.cpp +++ b/source/MRTest/MRSvgImageTests.cpp @@ -77,8 +77,8 @@ TEST( MRIOExtras, SvgImageFillRuleOpacity ) // group opacity applies to the group as a whole, so the overlap of its rectangles is not darker const auto image = rasterize( R"()", { 2, 1 } ); ASSERT_TRUE( image ) << image.error(); - EXPECT_EQ( ( *image )[Vector2i( 0, 0 )], Color( 255, 255, 255, 128 ) ); - EXPECT_EQ( ( *image )[Vector2i( 1, 0 )], Color( 255, 255, 255, 128 ) ); + EXPECT_EQ( ( *image )[Vector2i( 0, 0 )], Color( 255, 255, 255, 127 ) ); + EXPECT_EQ( ( *image )[Vector2i( 1, 0 )], Color( 255, 255, 255, 127 ) ); } // use draws the referenced element moved by x, y and its transform, which inherits the style of use but keeps its own, also through a group or another use; @@ -91,7 +91,7 @@ TEST( MRIOExtras, SvgImageUse ) R"svg()svg" R"svg()svg", { 7, 1 } ); ASSERT_TRUE( image ) << image.error(); - const Color expected[] = { Color::red(), Color::blue(), Color::green(), Color::red(), Color::blue(), Color( 0, 0, 0, 128 ), Color::transparent() }; + const Color expected[] = { Color::red(), Color::blue(), Color::green(), Color::red(), Color::blue(), Color( 0, 0, 0, 127 ), Color::transparent() }; for ( int x = 0; x < 7; ++x ) EXPECT_EQ( ( *image )[Vector2i( x, 0 )], expected[x] ) << x; } @@ -111,14 +111,13 @@ TEST( MRIOExtras, SvgImageAspectRatio ) } } -// currentColor is the color property of the drawn element even if it is inherited or set after it +// currentColor is the color property of the element, even if it is inherited or set after it TEST( MRIOExtras, SvgImageCurrentColor ) { - const auto image = rasterize( R"()" - R"()" - R"()", { 9, 3 } ); + const auto image = rasterize( R"()" + R"()", { 6, 3 } ); ASSERT_TRUE( image ) << image.error(); - for ( int x : { 1, 4, 7 } ) + for ( int x : { 1, 4 } ) EXPECT_EQ( ( *image )[Vector2i( x, 1 )], Color::red() ) << x; } @@ -201,7 +200,7 @@ TEST( MRIOExtras, SvgImageErrors ) } } -// inputs that made other rasterizers fail or loop: huge and invalid coordinates, deep and cyclic references, references multiplying the drawn elements +// inputs that made other rasterizers fail or loop: huge and invalid coordinates, deep and cyclic references, too many dashes TEST( MRIOExtras, SvgImageHardInputs ) { const auto chain = [] ( int length ) @@ -211,18 +210,8 @@ TEST( MRIOExtras, SvgImageHardInputs ) shapes += ""; return shapes + ""; }; - std::string fanOut = R"()"; - for ( int i = 1; i <= 6; ++i ) - { - fanOut += ""; - for ( int j = 0; j < 10; ++j ) - fanOut += ""; - fanOut += ""; - } - fanOut += R"()"; const std::string shapes[] = { chain( 200 ), - fanOut, R"svg()svg", R"svg()svg", R"()", @@ -230,7 +219,7 @@ TEST( MRIOExtras, SvgImageHardInputs ) R"()", R"()", R"()", - R"()", + R"()", }; for ( const auto& s : shapes ) { From ea153a83f1d5cd5a341d7dab4c67e43f3bada701 Mon Sep 17 00:00:00 2001 From: Grant Karapetyan Date: Fri, 4 Dec 2026 15:18:20 +0400 Subject: [PATCH 03/13] Find plutovg with the thirdparty hints before lunasvg 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 --- source/MRIOExtras/CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/MRIOExtras/CMakeLists.txt b/source/MRIOExtras/CMakeLists.txt index d63399210886..d29a0834e336 100644 --- a/source/MRIOExtras/CMakeLists.txt +++ b/source/MRIOExtras/CMakeLists.txt @@ -76,6 +76,10 @@ IF(NOT MRIOEXTRAS_NO_LAS) ENDIF() IF(NOT MRIOEXTRAS_NO_LUNASVG) + # found first with the hints, since lunasvg's config looks for it without them + find_package(plutovg CONFIG REQUIRED + HINTS "${MESHLIB_THIRDPARTY_ROOT_DIR}" + ) find_package(lunasvg CONFIG REQUIRED HINTS "${MESHLIB_THIRDPARTY_ROOT_DIR}" ) From ec93100e32d4ea4ab27e8cfb3770a82df139ec0a Mon Sep 17 00:00:00 2001 From: Grant Karapetyan Date: Fri, 4 Dec 2026 16:25:52 +0400 Subject: [PATCH 04/13] Render SVG straight into the image and limit its size 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 --- source/MRIOExtras/MRSvgImage.cpp | 15 +++++++++------ source/MRIOExtras/MRSvgImage.h | 2 +- source/MRTest/MRSvgImageTests.cpp | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/source/MRIOExtras/MRSvgImage.cpp b/source/MRIOExtras/MRSvgImage.cpp index 52448fe08af7..4f08a7787b2a 100644 --- a/source/MRIOExtras/MRSvgImage.cpp +++ b/source/MRIOExtras/MRSvgImage.cpp @@ -7,7 +7,6 @@ #include #include -#include #include namespace MR::ImageLoad @@ -35,17 +34,21 @@ Expected fromSvg( const char* data, size_t size, const Vector2i& resoluti if ( !( width > 0 && height > 0 ) ) return unexpected( "SVG has empty size" ); - lunasvg::Bitmap bitmap( resolution.x, resolution.y ); - if ( bitmap.isNull() ) + // plutovg needs each side below 2^15 and computes byte sizes of the image in int + if ( resolution.x <= 0 || resolution.y <= 0 || resolution.x >= 32768 || resolution.y >= 32768 + || size_t( resolution.x ) * size_t( resolution.y ) >= ( size_t( 1 ) << 29 ) ) return unexpected( "Invalid image resolution" ); + + Image image{ .pixels = std::vector( size_t( resolution.x ) * resolution.y, Color::transparent() ), .resolution = resolution }; + lunasvg::Bitmap bitmap( ( uint8_t* )image.pixels.data(), resolution.x, resolution.y, resolution.x * 4 ); const float scale = std::min( resolution.x / width, resolution.y / height ); doc->render( bitmap, lunasvg::Matrix( scale, 0, 0, scale, ( resolution.x - scale * width ) / 2, ( resolution.y - scale * height ) / 2 ) ); bitmap.convertToRGBA(); - Image image{ .pixels = std::vector( size_t( resolution.x ) * resolution.y ), .resolution = resolution }; // bitmap rows go from top to bottom, image rows from bottom to top - for ( int y = 0; y < resolution.y; ++y ) - std::memcpy( &image.pixels[size_t( resolution.y - 1 - y ) * resolution.x], bitmap.data() + size_t( y ) * bitmap.stride(), resolution.x * sizeof( Color ) ); + const auto row = [&] ( int y ) { return image.pixels.begin() + size_t( y ) * resolution.x; }; + for ( int y = 0; y < resolution.y / 2; ++y ) + std::swap_ranges( row( y ), row( y + 1 ), row( resolution.y - 1 - y ) ); return image; } diff --git a/source/MRIOExtras/MRSvgImage.h b/source/MRIOExtras/MRSvgImage.h index 9dd49c5d4a1b..5c0c5073e853 100644 --- a/source/MRIOExtras/MRSvgImage.h +++ b/source/MRIOExtras/MRSvgImage.h @@ -12,7 +12,7 @@ namespace MR::ImageLoad { -/// rasterizes .SVG file into an image of given resolution (from 1 to 32767 in each dimension), the SVG is scaled uniformly to fit the image and centered; +/// rasterizes .SVG file into an image of given resolution (from 1 to 32767 in each dimension, fewer than 2^29 pixels in total), the SVG is scaled uniformly to fit the image and centered; /// unsupported SVG features are skipped, images are drawn only from data: URIs, and text is not drawn MRIOEXTRAS_API Expected fromSvg( const std::filesystem::path& file, const Vector2i& resolution ); MRIOEXTRAS_API Expected fromSvg( const char* data, size_t size, const Vector2i& resolution ); diff --git a/source/MRTest/MRSvgImageTests.cpp b/source/MRTest/MRSvgImageTests.cpp index 5e079799c422..cc3e325bba0a 100644 --- a/source/MRTest/MRSvgImageTests.cpp +++ b/source/MRTest/MRSvgImageTests.cpp @@ -192,7 +192,7 @@ TEST( MRIOExtras, SvgImageErrors ) ASSERT_FALSE( image ) << svg; EXPECT_EQ( image.error(), error ) << svg; } - for ( const Vector2i& res : { Vector2i( 0, 1 ), Vector2i( -1, 10 ), Vector2i( 32768, 1 ) } ) + for ( const Vector2i& res : { Vector2i( 0, 1 ), Vector2i( -1, 10 ), Vector2i( 32768, 1 ), Vector2i( 23200, 23200 ) } ) { const auto invalid = rasterize( R"()", res ); ASSERT_FALSE( invalid ) << res.x << " " << res.y; From c4841aa7ff306666dc068923a8191b3fd361db86 Mon Sep 17 00:00:00 2001 From: Grant Karapetyan Date: Fri, 4 Dec 2026 16:26:00 +0400 Subject: [PATCH 05/13] Document that fromSvg needs a trusted SVG 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 --- source/MRIOExtras/MRSvgImage.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/MRIOExtras/MRSvgImage.h b/source/MRIOExtras/MRSvgImage.h index 5c0c5073e853..ddd308b9d3d3 100644 --- a/source/MRIOExtras/MRSvgImage.h +++ b/source/MRIOExtras/MRSvgImage.h @@ -13,7 +13,8 @@ namespace MR::ImageLoad { /// rasterizes .SVG file into an image of given resolution (from 1 to 32767 in each dimension, fewer than 2^29 pixels in total), the SVG is scaled uniformly to fit the image and centered; -/// unsupported SVG features are skipped, images are drawn only from data: URIs, and text is not drawn +/// unsupported SVG features are skipped, images are drawn only from data: URIs, and text is not drawn; +/// the SVG must come from a trusted source, since some inputs take unbounded time or memory MRIOEXTRAS_API Expected fromSvg( const std::filesystem::path& file, const Vector2i& resolution ); MRIOEXTRAS_API Expected fromSvg( const char* data, size_t size, const Vector2i& resolution ); From 32ddab2ee9c89ff3da0fdb9b6d219d75fe3c672b Mon Sep 17 00:00:00 2001 From: Grant Karapetyan Date: Fri, 4 Dec 2026 16:27:28 +0400 Subject: [PATCH 06/13] Test SVG viewBox fitting and drop PNG encoding from tests 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 --- source/MRTest/MRSvgImageTests.cpp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/source/MRTest/MRSvgImageTests.cpp b/source/MRTest/MRSvgImageTests.cpp index cc3e325bba0a..5e128bcf2868 100644 --- a/source/MRTest/MRSvgImageTests.cpp +++ b/source/MRTest/MRSvgImageTests.cpp @@ -1,6 +1,5 @@ #include #ifndef MRIOEXTRAS_NO_LUNASVG -#include #include #include #include @@ -8,7 +7,6 @@ #include #include -#include namespace MR { @@ -111,6 +109,19 @@ TEST( MRIOExtras, SvgImageAspectRatio ) } } +// the viewBox, whose origin is not at zero as in icons with viewBox="0 -960 960 960", is fitted in the center of width and height +TEST( MRIOExtras, SvgImageViewBox ) +{ + // red square at the top left of the viewBox, blue one at the bottom right + const auto image = rasterize( R"()" + R"()", { 12, 6 } ); + ASSERT_TRUE( image ) << image.error(); + // 2 pixels per viewBox unit, columns 0, 1, 10, 11 and rows 0, 5 are empty + for ( int y = 0; y < 6; ++y ) + for ( int x = 0; x < 12; ++x ) + EXPECT_EQ( ( *image )[Vector2i( x, y )], x >= 2 && x < 4 && y >= 3 && y < 5 ? Color::red() : x >= 8 && x < 10 && y >= 1 && y < 3 ? Color::blue() : Color::transparent() ) << x << ' ' << y; +} + // currentColor is the color property of the element, even if it is inherited or set after it TEST( MRIOExtras, SvgImageCurrentColor ) { @@ -160,17 +171,17 @@ TEST( MRIOExtras, SvgImageExternalImage ) { UniqueTemporaryFolder folder; ASSERT_TRUE( bool( folder ) ); - std::ostringstream png; - ASSERT_TRUE( ImageSave::toPng( Image{ .pixels = { Color::red() }, .resolution = { 1, 1 } }, png ) ); - const auto bytes = png.str(); + // 1x1 red PNG + const std::string png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR42mP4z8AAAAMBAQD3A0FDAAAAAElFTkSuQmCC"; + const auto bytes = decode64( png ); const auto path = folder / "red.png"; - std::ofstream( path, std::ofstream::binary ) << bytes; + std::ofstream( path, std::ofstream::binary ).write( ( const char* )bytes.data(), std::streamsize( bytes.size() ) ); const auto draw = [] ( const std::string& href ) { return rasterize( R"()", { 1, 1 } ); }; - const auto embedded = draw( "data:image/png;base64," + encode64( ( const std::uint8_t* )bytes.data(), bytes.size() ) ); + const auto embedded = draw( "data:image/png;base64," + png ); ASSERT_TRUE( embedded ) << embedded.error(); EXPECT_EQ( ( *embedded )[Vector2i( 0, 0 )], Color::red() ); const auto file = draw( utf8string( path ) ); From 9c4c53304173fb21b5bd05c636d5aebd9fcdec81 Mon Sep 17 00:00:00 2001 From: Grant Karapetyan Date: Fri, 4 Dec 2026 16:27:28 +0400 Subject: [PATCH 07/13] Keep lunasvg and plutovg headers out of vcpkg packages Both are private dependencies of MRIOExtras, not reachable from MeshLib's public headers. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 292225921203..14666c6c4539 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -232,6 +232,7 @@ if(MESHLIB_USE_VCPKG) opencascade xercesc openssl curl cpr gdcm[^/]* socketxx openjph openjpeg-[^/]* E57Format hpdf[^/]* tiff[^/]* tinyxml2\\.h tiny_?gltf[^/]* stb_[^/]* + lunasvg plutovg ) list(JOIN VCPKG_PRIVATE_HEADERS "|" VCPKG_PRIVATE_HEADERS) install( From e0bff4ed538591245051c7557e2083880cc66501 Mon Sep 17 00:00:00 2001 From: Grant Karapetyan Date: Fri, 4 Dec 2026 16:30:49 +0400 Subject: [PATCH 08/13] Note that lunasvg must be built by MeshLib Co-Authored-By: Claude Opus 5.5 --- source/MRIOExtras/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/MRIOExtras/CMakeLists.txt b/source/MRIOExtras/CMakeLists.txt index d29a0834e336..606b897b0aee 100644 --- a/source/MRIOExtras/CMakeLists.txt +++ b/source/MRIOExtras/CMakeLists.txt @@ -80,6 +80,8 @@ IF(NOT MRIOEXTRAS_NO_LUNASVG) find_package(plutovg CONFIG REQUIRED HINTS "${MESHLIB_THIRDPARTY_ROOT_DIR}" ) + # must be the lunasvg MeshLib builds (vcpkg overlay port, thirdparty submodule or CPM): a stock one reads the files an SVG refers to, + # and its LUNASVG_DISABLE_EXTERNAL_RESOURCES is a PRIVATE define that cannot be checked here find_package(lunasvg CONFIG REQUIRED HINTS "${MESHLIB_THIRDPARTY_ROOT_DIR}" ) From 719540b5aa17315f0c84d476759a17ca00ed48ec Mon Sep 17 00:00:00 2001 From: Grant Karapetyan Date: Fri, 4 Dec 2026 17:33:39 +0400 Subject: [PATCH 09/13] Build lunasvg 3.5.0 without disabling external resources The SVG must be trusted anyway, so an 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 --- source/MRIOExtras/CMakeLists.txt | 2 -- source/MRIOExtras/MRSvgImage.h | 2 +- source/MRTest/MRSvgImageTests.cpp | 27 ++++------------ thirdparty/CMakeLists.txt | 5 ++- thirdparty/cpm/package-lock.cmake | 2 +- thirdparty/cpm/stage1/CMakeLists.txt | 5 ++- thirdparty/licenses/THIRD-PARTY-NOTICES.txt | 2 +- thirdparty/licenses/manifest.json | 2 +- thirdparty/lunasvg | 2 +- thirdparty/vcpkg/ports/lunasvg/portfile.cmake | 31 ------------------- thirdparty/vcpkg/ports/lunasvg/vcpkg.json | 19 ------------ 11 files changed, 15 insertions(+), 84 deletions(-) delete mode 100644 thirdparty/vcpkg/ports/lunasvg/portfile.cmake delete mode 100644 thirdparty/vcpkg/ports/lunasvg/vcpkg.json diff --git a/source/MRIOExtras/CMakeLists.txt b/source/MRIOExtras/CMakeLists.txt index 606b897b0aee..d29a0834e336 100644 --- a/source/MRIOExtras/CMakeLists.txt +++ b/source/MRIOExtras/CMakeLists.txt @@ -80,8 +80,6 @@ IF(NOT MRIOEXTRAS_NO_LUNASVG) find_package(plutovg CONFIG REQUIRED HINTS "${MESHLIB_THIRDPARTY_ROOT_DIR}" ) - # must be the lunasvg MeshLib builds (vcpkg overlay port, thirdparty submodule or CPM): a stock one reads the files an SVG refers to, - # and its LUNASVG_DISABLE_EXTERNAL_RESOURCES is a PRIVATE define that cannot be checked here find_package(lunasvg CONFIG REQUIRED HINTS "${MESHLIB_THIRDPARTY_ROOT_DIR}" ) diff --git a/source/MRIOExtras/MRSvgImage.h b/source/MRIOExtras/MRSvgImage.h index ddd308b9d3d3..f41a5a9fea3c 100644 --- a/source/MRIOExtras/MRSvgImage.h +++ b/source/MRIOExtras/MRSvgImage.h @@ -13,7 +13,7 @@ namespace MR::ImageLoad { /// rasterizes .SVG file into an image of given resolution (from 1 to 32767 in each dimension, fewer than 2^29 pixels in total), the SVG is scaled uniformly to fit the image and centered; -/// unsupported SVG features are skipped, images are drawn only from data: URIs, and text is not drawn; +/// unsupported SVG features are skipped, text is not drawn, and an image given by a file path is read from that path; /// the SVG must come from a trusted source, since some inputs take unbounded time or memory MRIOEXTRAS_API Expected fromSvg( const std::filesystem::path& file, const Vector2i& resolution ); MRIOEXTRAS_API Expected fromSvg( const char* data, size_t size, const Vector2i& resolution ); diff --git a/source/MRTest/MRSvgImageTests.cpp b/source/MRTest/MRSvgImageTests.cpp index 5e128bcf2868..3d13fcfeee58 100644 --- a/source/MRTest/MRSvgImageTests.cpp +++ b/source/MRTest/MRSvgImageTests.cpp @@ -1,7 +1,5 @@ #include #ifndef MRIOEXTRAS_NO_LUNASVG -#include -#include #include #include @@ -166,27 +164,14 @@ TEST( MRIOExtras, SvgImageFeatures ) EXPECT_EQ( ( *image )[Vector2i( x, 0 )], expected[x] ) << x; } -// an image is drawn from a data: URI, but never read from a file -TEST( MRIOExtras, SvgImageExternalImage ) +// an image is drawn from a data: URI +TEST( MRIOExtras, SvgImageEmbeddedImage ) { - UniqueTemporaryFolder folder; - ASSERT_TRUE( bool( folder ) ); // 1x1 red PNG - const std::string png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR42mP4z8AAAAMBAQD3A0FDAAAAAElFTkSuQmCC"; - const auto bytes = decode64( png ); - const auto path = folder / "red.png"; - std::ofstream( path, std::ofstream::binary ).write( ( const char* )bytes.data(), std::streamsize( bytes.size() ) ); - - const auto draw = [] ( const std::string& href ) - { - return rasterize( R"()", { 1, 1 } ); - }; - const auto embedded = draw( "data:image/png;base64," + png ); - ASSERT_TRUE( embedded ) << embedded.error(); - EXPECT_EQ( ( *embedded )[Vector2i( 0, 0 )], Color::red() ); - const auto file = draw( utf8string( path ) ); - ASSERT_TRUE( file ) << file.error(); - EXPECT_EQ( ( *file )[Vector2i( 0, 0 )], Color::transparent() ); + const auto image = rasterize( R"()", { 1, 1 } ); + ASSERT_TRUE( image ) << image.error(); + EXPECT_EQ( ( *image )[Vector2i( 0, 0 )], Color::red() ); } TEST( MRIOExtras, SvgImageErrors ) diff --git a/thirdparty/CMakeLists.txt b/thirdparty/CMakeLists.txt index 222278223f06..afcb966f3339 100644 --- a/thirdparty/CMakeLists.txt +++ b/thirdparty/CMakeLists.txt @@ -202,8 +202,7 @@ IF(NOT draco_FOUND) add_subdirectory(./draco) ENDIF() -# always built from the submodule: packaged lunasvg loads the files an SVG refers to set(LUNASVG_BUILD_EXAMPLES OFF) -set(LUNASVG_DISABLE_EXTERNAL_RESOURCES ON) -set(LUNASVG_DISABLE_LOAD_SYSTEM_FONTS ON) +# loads no fonts, as vcpkg's plutovg by default +set(PLUTOVG_DISABLE_FONT_FACE_CACHE_LOAD ON) add_subdirectory(./lunasvg) diff --git a/thirdparty/cpm/package-lock.cmake b/thirdparty/cpm/package-lock.cmake index 2db70bd1480b..36645843b0c7 100644 --- a/thirdparty/cpm/package-lock.cmake +++ b/thirdparty/cpm/package-lock.cmake @@ -126,7 +126,7 @@ set(MESHLIB_PACKAGE_libzip set(MESHLIB_PACKAGE_lunasvg GIT_REPOSITORY https://github.com/sammycage/lunasvg.git - GIT_TAG 470e78e2953dcf0d4b46322d64eed6ca84156fec + GIT_TAG 83c58df8103dc7dca423dfd824992af94d49bed6 ) set(MESHLIB_PACKAGE_mbedtls diff --git a/thirdparty/cpm/stage1/CMakeLists.txt b/thirdparty/cpm/stage1/CMakeLists.txt index 0b66a3557754..e8e943ec9c3a 100644 --- a/thirdparty/cpm/stage1/CMakeLists.txt +++ b/thirdparty/cpm/stage1/CMakeLists.txt @@ -207,11 +207,10 @@ if(NOT draco_FOUND) add_draco() endif() -# always built: packaged lunasvg loads the files an SVG refers to function(add_lunasvg) set(LUNASVG_BUILD_EXAMPLES OFF) - set(LUNASVG_DISABLE_EXTERNAL_RESOURCES ON) - set(LUNASVG_DISABLE_LOAD_SYSTEM_FONTS ON) + # loads no fonts, as vcpkg's plutovg by default + set(PLUTOVG_DISABLE_FONT_FACE_CACHE_LOAD ON) meshlib_add_package(lunasvg) endfunction() add_lunasvg() diff --git a/thirdparty/licenses/THIRD-PARTY-NOTICES.txt b/thirdparty/licenses/THIRD-PARTY-NOTICES.txt index acda31bebaa8..7111c8058f32 100644 --- a/thirdparty/licenses/THIRD-PARTY-NOTICES.txt +++ b/thirdparty/licenses/THIRD-PARTY-NOTICES.txt @@ -2923,7 +2923,7 @@ https://github.com/sammycage/lunasvg MIT License -Copyright (c) 2020-2026 Samuel Ugochukwu +Copyright (c) 2020-2025 Samuel Ugochukwu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/thirdparty/licenses/manifest.json b/thirdparty/licenses/manifest.json index cd5bd580e969..30e009098786 100644 --- a/thirdparty/licenses/manifest.json +++ b/thirdparty/licenses/manifest.json @@ -389,7 +389,7 @@ "type": "submodule", "path": "thirdparty/lunasvg" }, - "version": "470e78e2953dcf0d4b46322d64eed6ca84156fec" + "version": "83c58df8103dc7dca423dfd824992af94d49bed6" }, { "id": "plutovg", diff --git a/thirdparty/lunasvg b/thirdparty/lunasvg index 470e78e2953d..83c58df8103d 160000 --- a/thirdparty/lunasvg +++ b/thirdparty/lunasvg @@ -1 +1 @@ -Subproject commit 470e78e2953dcf0d4b46322d64eed6ca84156fec +Subproject commit 83c58df8103dc7dca423dfd824992af94d49bed6 diff --git a/thirdparty/vcpkg/ports/lunasvg/portfile.cmake b/thirdparty/vcpkg/ports/lunasvg/portfile.cmake deleted file mode 100644 index c6f7a3b8a837..000000000000 --- a/thirdparty/vcpkg/ports/lunasvg/portfile.cmake +++ /dev/null @@ -1,31 +0,0 @@ -vcpkg_from_github( - OUT_SOURCE_PATH SOURCE_PATH - REPO sammycage/lunasvg - # v3.5.0 has no LUNASVG_DISABLE_EXTERNAL_RESOURCES yet - REF 470e78e2953dcf0d4b46322d64eed6ca84156fec - SHA512 f258ebeb17876f22786b4df25b2f01314e04917dad7061a5413e43bc5e9668904ed4527c138350bc4755e3f30cda1fb802391c67cc6fff2e2cbe775224b11808 - HEAD_REF master -) - -vcpkg_cmake_configure( - SOURCE_PATH "${SOURCE_PATH}" - OPTIONS - -DCMAKE_REQUIRE_FIND_PACKAGE_plutovg=1 - -DUSE_SYSTEM_PLUTOVG=ON - -DLUNASVG_BUILD_EXAMPLES=OFF - -DLUNASVG_DISABLE_EXTERNAL_RESOURCES=ON - -DLUNASVG_DISABLE_LOAD_SYSTEM_FONTS=ON -) - -vcpkg_cmake_install() -vcpkg_copy_pdbs() -vcpkg_cmake_config_fixup(CONFIG_PATH lib/cmake/lunasvg) -vcpkg_fixup_pkgconfig() - -if(VCPKG_LIBRARY_LINKAGE STREQUAL "static") - vcpkg_replace_string("${CURRENT_PACKAGES_DIR}/include/lunasvg/lunasvg.h" "defined(LUNASVG_BUILD_STATIC)" "1") -endif() - -file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include") - -vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE") diff --git a/thirdparty/vcpkg/ports/lunasvg/vcpkg.json b/thirdparty/vcpkg/ports/lunasvg/vcpkg.json deleted file mode 100644 index 14c3065c3307..000000000000 --- a/thirdparty/vcpkg/ports/lunasvg/vcpkg.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "name": "lunasvg", - "version": "3.5.0", - "port-version": 1, - "description": "lunasvg is a standalone SVG rendering library in C++", - "homepage": "https://github.com/sammycage/lunasvg", - "license": "MIT", - "dependencies": [ - "plutovg", - { - "name": "vcpkg-cmake", - "host": true - }, - { - "name": "vcpkg-cmake-config", - "host": true - } - ] -} From 957d2ebcb029639bc27762758b5d987eee3a479b Mon Sep 17 00:00:00 2001 From: Grant Karapetyan Date: Fri, 4 Dec 2026 18:26:31 +0400 Subject: [PATCH 10/13] Strip a BOM before lunasvg 3.5.0 and find Threads for its plutovg 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 --- source/MRIOExtras/CMakeLists.txt | 2 ++ source/MRIOExtras/MRIOExtrasConfig.cmake | 1 + source/MRIOExtras/MRSvgImage.cpp | 6 ++++++ source/MRIOExtras/MRSvgImage.h | 4 ++-- source/MRTest/MRSvgImageTests.cpp | 18 +++++++++++++++--- 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/source/MRIOExtras/CMakeLists.txt b/source/MRIOExtras/CMakeLists.txt index d29a0834e336..d79b889cbc7b 100644 --- a/source/MRIOExtras/CMakeLists.txt +++ b/source/MRIOExtras/CMakeLists.txt @@ -76,6 +76,8 @@ IF(NOT MRIOEXTRAS_NO_LAS) ENDIF() IF(NOT MRIOEXTRAS_NO_LUNASVG) + # static plutovg 1.3.1 links Threads::Threads, but its config does not find it + find_package(Threads) # found first with the hints, since lunasvg's config looks for it without them find_package(plutovg CONFIG REQUIRED HINTS "${MESHLIB_THIRDPARTY_ROOT_DIR}" diff --git a/source/MRIOExtras/MRIOExtrasConfig.cmake b/source/MRIOExtras/MRIOExtrasConfig.cmake index 5739261016b4..ca40cc48339c 100644 --- a/source/MRIOExtras/MRIOExtrasConfig.cmake +++ b/source/MRIOExtras/MRIOExtrasConfig.cmake @@ -5,6 +5,7 @@ if(EMSCRIPTEN) find_dependency(OpenCTM) find_dependency(draco) find_dependency(LAZPERF) + find_dependency(Threads) # plutovg 1.3.1's config does not find it find_dependency(lunasvg) endif() diff --git a/source/MRIOExtras/MRSvgImage.cpp b/source/MRIOExtras/MRSvgImage.cpp index 4f08a7787b2a..0b86be99ab21 100644 --- a/source/MRIOExtras/MRSvgImage.cpp +++ b/source/MRIOExtras/MRSvgImage.cpp @@ -26,6 +26,12 @@ Expected fromSvg( const std::filesystem::path& file, const Vector2i& reso Expected fromSvg( const char* data, size_t size, const Vector2i& resolution ) { + // lunasvg 3.5.0 does not skip a UTF-8 BOM + if ( hasBom( { data, size } ) ) + { + data += 3; + size -= 3; + } const auto doc = lunasvg::Document::loadFromData( data, size ); if ( !doc ) return unexpected( "Failed to parse SVG" ); diff --git a/source/MRIOExtras/MRSvgImage.h b/source/MRIOExtras/MRSvgImage.h index f41a5a9fea3c..e6470a9093d9 100644 --- a/source/MRIOExtras/MRSvgImage.h +++ b/source/MRIOExtras/MRSvgImage.h @@ -13,8 +13,8 @@ namespace MR::ImageLoad { /// rasterizes .SVG file into an image of given resolution (from 1 to 32767 in each dimension, fewer than 2^29 pixels in total), the SVG is scaled uniformly to fit the image and centered; -/// unsupported SVG features are skipped, text is not drawn, and an image given by a file path is read from that path; -/// the SVG must come from a trusted source, since some inputs take unbounded time or memory +/// unsupported SVG features are skipped, text is not drawn, and an image given by a file path is read from that path (relative to the current directory); +/// the SVG must come from a trusted source, since some inputs take unbounded time or memory, or crash MRIOEXTRAS_API Expected fromSvg( const std::filesystem::path& file, const Vector2i& resolution ); MRIOEXTRAS_API Expected fromSvg( const char* data, size_t size, const Vector2i& resolution ); diff --git a/source/MRTest/MRSvgImageTests.cpp b/source/MRTest/MRSvgImageTests.cpp index 3d13fcfeee58..4a4a986512c7 100644 --- a/source/MRTest/MRSvgImageTests.cpp +++ b/source/MRTest/MRSvgImageTests.cpp @@ -132,15 +132,15 @@ TEST( MRIOExtras, SvgImageCurrentColor ) TEST( MRIOExtras, SvgImageDashes ) { - // a stroke through the only row of pixels, dash ends are on pixel borders; alpha of at least hi is '#', at most lo is '.', otherwise '?' - const auto row = [] ( const std::string& stroke, int lo = 0, int hi = 255 ) + // a stroke through the only row of pixels, dash ends are on pixel borders; opaque is '#', transparent is '.', otherwise '?' + const auto row = [] ( const std::string& stroke ) { const auto image = rasterize( R"svg(", { 10, 1 } ); if ( !image ) return image.error(); std::string res; for ( const auto& c : image->pixels ) - res += c.a >= hi ? '#' : c.a <= lo ? '.' : '?'; + res += c.a == 255 ? '#' : c.a == 0 ? '.' : '?'; return res; }; EXPECT_EQ( row( R"(d="M0 0.5H8" stroke-dasharray="2")" ), "##..##...." ); @@ -164,6 +164,18 @@ TEST( MRIOExtras, SvgImageFeatures ) EXPECT_EQ( ( *image )[Vector2i( x, 0 )], expected[x] ) << x; } +// a file as saved by editors: BOM, XML declaration, comment, DOCTYPE; text is not drawn, since no fonts are loaded +TEST( MRIOExtras, SvgImageProlog ) +{ + const auto image = rasterize( "\xEF\xBB\xBF" R"()" + R"()" + R"(W)", { 20, 10 } ); + ASSERT_TRUE( image ) << image.error(); + for ( int y = 0; y < 10; ++y ) + for ( int x = 0; x < 20; ++x ) + EXPECT_EQ( ( *image )[Vector2i( x, y )], x < 10 ? Color::red() : Color::transparent() ) << x << ' ' << y; +} + // an image is drawn from a data: URI TEST( MRIOExtras, SvgImageEmbeddedImage ) { From 7c7f5a3b13e03cbd117513914afd25dc514e344e Mon Sep 17 00:00:00 2001 From: Grant Karapetyan Date: Sat, 5 Dec 2026 15:32:55 +0400 Subject: [PATCH 11/13] Render an SVG with only width or height at its reported size 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 --- source/MRIOExtras/MRSvgImage.cpp | 4 ++++ source/MRTest/MRSvgImageTests.cpp | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/source/MRIOExtras/MRSvgImage.cpp b/source/MRIOExtras/MRSvgImage.cpp index 0b86be99ab21..07f5d1d1e570 100644 --- a/source/MRIOExtras/MRSvgImage.cpp +++ b/source/MRIOExtras/MRSvgImage.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include @@ -39,6 +40,9 @@ Expected fromSvg( const char* data, size_t size, const Vector2i& resoluti const float height = doc->height(); if ( !( width > 0 && height > 0 ) ) return unexpected( "SVG has empty size" ); + // lunasvg 3.5.0 renders a missing width or height as 100% of the viewBox, not as the reported size + doc->documentElement().setAttribute( "width", fmt::format( "{}", width ) ); + doc->documentElement().setAttribute( "height", fmt::format( "{}", height ) ); // plutovg needs each side below 2^15 and computes byte sizes of the image in int if ( resolution.x <= 0 || resolution.y <= 0 || resolution.x >= 32768 || resolution.y >= 32768 diff --git a/source/MRTest/MRSvgImageTests.cpp b/source/MRTest/MRSvgImageTests.cpp index 4a4a986512c7..471ca7a40f88 100644 --- a/source/MRTest/MRSvgImageTests.cpp +++ b/source/MRTest/MRSvgImageTests.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -118,6 +119,14 @@ TEST( MRIOExtras, SvgImageViewBox ) for ( int y = 0; y < 6; ++y ) for ( int x = 0; x < 12; ++x ) EXPECT_EQ( ( *image )[Vector2i( x, y )], x >= 2 && x < 4 && y >= 3 && y < 5 ? Color::red() : x >= 8 && x < 10 && y >= 1 && y < 3 ? Color::blue() : Color::transparent() ) << x << ' ' << y; + + // with only width or only height, the other side follows the aspect ratio of the viewBox + for ( const std::string size : { R"(width="4")", R"(height="2")" } ) + { + const auto filled = rasterize( ")", { 4, 2 } ); + ASSERT_TRUE( filled ) << filled.error(); + EXPECT_EQ( std::ranges::count( filled->pixels, Color::red() ), 8 ) << size; + } } // currentColor is the color property of the element, even if it is inherited or set after it From d8e246a83c2cd2f3314ac102269458af2714e9b8 Mon Sep 17 00:00:00 2001 From: Grant Karapetyan Date: Sat, 5 Dec 2026 15:34:24 +0400 Subject: [PATCH 12/13] Test the largest SVG image side and drop overflowing inputs 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 --- source/MRIOExtras/MRSvgImage.cpp | 1 - source/MRTest/MRSvgImageTests.cpp | 11 +++++------ source/MRTest/MRTest.vcxproj.filters | 3 +++ thirdparty/licenses/THIRD-PARTY-NOTICES.txt | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/source/MRIOExtras/MRSvgImage.cpp b/source/MRIOExtras/MRSvgImage.cpp index 07f5d1d1e570..506e84ecd23c 100644 --- a/source/MRIOExtras/MRSvgImage.cpp +++ b/source/MRIOExtras/MRSvgImage.cpp @@ -1,6 +1,5 @@ #include "MRSvgImage.h" #ifndef MRIOEXTRAS_NO_LUNASVG -#include #include #include #include diff --git a/source/MRTest/MRSvgImageTests.cpp b/source/MRTest/MRSvgImageTests.cpp index 471ca7a40f88..8f7f0e9a0b19 100644 --- a/source/MRTest/MRSvgImageTests.cpp +++ b/source/MRTest/MRSvgImageTests.cpp @@ -215,9 +215,13 @@ TEST( MRIOExtras, SvgImageErrors ) ASSERT_FALSE( invalid ) << res.x << " " << res.y; EXPECT_EQ( invalid.error(), "Invalid image resolution" ); } + // the largest side is accepted + const auto widest = rasterize( R"()", { 32767, 1 } ); + ASSERT_TRUE( widest ) << widest.error(); + EXPECT_TRUE( std::ranges::all_of( widest->pixels, [] ( const Color& c ) { return c == Color::red(); } ) ); } -// inputs that made other rasterizers fail or loop: huge and invalid coordinates, deep and cyclic references, too many dashes +// inputs that made other rasterizers fail or loop: deep and cyclic references, too many dashes TEST( MRIOExtras, SvgImageHardInputs ) { const auto chain = [] ( int length ) @@ -231,11 +235,6 @@ TEST( MRIOExtras, SvgImageHardInputs ) chain( 200 ), R"svg()svg", R"svg()svg", - R"()", - R"()", - R"()", - R"()", - R"()", R"()", }; for ( const auto& s : shapes ) diff --git a/source/MRTest/MRTest.vcxproj.filters b/source/MRTest/MRTest.vcxproj.filters index 0c423b5f851a..98509af12096 100644 --- a/source/MRTest/MRTest.vcxproj.filters +++ b/source/MRTest/MRTest.vcxproj.filters @@ -76,6 +76,9 @@ Source Files + + Source Files + Source Files diff --git a/thirdparty/licenses/THIRD-PARTY-NOTICES.txt b/thirdparty/licenses/THIRD-PARTY-NOTICES.txt index 7111c8058f32..01f06f9c3697 100644 --- a/thirdparty/licenses/THIRD-PARTY-NOTICES.txt +++ b/thirdparty/licenses/THIRD-PARTY-NOTICES.txt @@ -2973,7 +2973,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------ source/FTL.TXT (plutovg-ft-math, plutovg-ft-raster, plutovg-ft-stroker) ----- +----- source/FTL.TXT (plutovg-ft-math, plutovg-ft-raster, plutovg-ft-stroker, plutovg-ft-types) ----- The FreeType Project LICENSE ---------------------------- From 8238eaa2e72c56a0304a5eee6586edffdbeba8c0 Mon Sep 17 00:00:00 2001 From: Grant Karapetyan Date: Sat, 5 Dec 2026 15:37:05 +0400 Subject: [PATCH 13/13] Count pixels in SVG tests without std::ranges 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 --- source/MRTest/MRSvgImageTests.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/MRTest/MRSvgImageTests.cpp b/source/MRTest/MRSvgImageTests.cpp index 8f7f0e9a0b19..fcb91a469368 100644 --- a/source/MRTest/MRSvgImageTests.cpp +++ b/source/MRTest/MRSvgImageTests.cpp @@ -125,7 +125,7 @@ TEST( MRIOExtras, SvgImageViewBox ) { const auto filled = rasterize( ")", { 4, 2 } ); ASSERT_TRUE( filled ) << filled.error(); - EXPECT_EQ( std::ranges::count( filled->pixels, Color::red() ), 8 ) << size; + EXPECT_EQ( std::count( filled->pixels.begin(), filled->pixels.end(), Color::red() ), 8 ) << size; } } @@ -218,7 +218,7 @@ TEST( MRIOExtras, SvgImageErrors ) // the largest side is accepted const auto widest = rasterize( R"()", { 32767, 1 } ); ASSERT_TRUE( widest ) << widest.error(); - EXPECT_TRUE( std::ranges::all_of( widest->pixels, [] ( const Color& c ) { return c == Color::red(); } ) ); + EXPECT_TRUE( std::all_of( widest->pixels.begin(), widest->pixels.end(), [] ( const Color& c ) { return c == Color::red(); } ) ); } // inputs that made other rasterizers fail or loop: deep and cyclic references, too many dashes