Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/actions/install-macos-thirdparty/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ runs:
thirdparty/tinygltf \
thirdparty/laz-perf \
thirdparty/draco \
thirdparty/lunasvg \
thirdparty/clip \
thirdparty/fastmcpp
shasum -a 256 \
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions doxygen/general_pages/ThirdpartyList.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ Below is a summary of all modules and their dependencies, so you can see what ea
|<a rel="nofollow" href="https://github.com/google/draco">Draco</a> |library for compressing and decompressing 3D meshes and point clouds |Apache 2.0 |
|<a rel="nofollow" href="https://github.com/asmaloney/libE57Format">libe57format</a> |library for reading & writing the E57 file format |Boost Software License |
|<a rel="nofollow" href="https://leethomason.github.io/tinyxml2/">tinyxml2</a> |an efficient, C++ XML parser |zlib |
|<a rel="nofollow" href="https://github.com/sammycage/lunasvg">lunasvg</a> |SVG rendering library in C++ |MIT |
|<a rel="nofollow" href="https://github.com/sammycage/plutovg">plutovg</a> |tiny 2D vector graphics library in C, used by lunasvg |MIT, FTL (FreeType License) |
|<a rel="nofollow" href="https://github.com/nlohmann/json">nlohmann-json</a> |JSON for Modern C++ — used by the glTF reader/writer |MIT |

### MRSymbolMesh
Expand Down
1 change: 1 addition & 0 deletions requirements/vcpkg-linux.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ libe57format
libharu
libjpeg-turbo
libzip
lunasvg
mimalloc
opencascade-minimal
openctm
Expand Down
1 change: 1 addition & 0 deletions requirements/windows.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ laz-perf
libe57format
libharu
libzip
lunasvg
mimalloc[override]
opencascade-minimal
openctm
Expand Down
1 change: 1 addition & 0 deletions scripts/clone_submodules_emscripten_thirdparty.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions scripts/clone_submodules_macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ if [[ $1 != --vcpkg ]]; then
thirdparty/glad
thirdparty/laz-perf
thirdparty/libE57Format
thirdparty/lunasvg
thirdparty/OpenCTM-git
thirdparty/tinygltf
)
Expand Down
1 change: 1 addition & 0 deletions scripts/sbom/vcpkg-sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"Draco": ["draco"],
"libE57Format": ["libe57format"],
"tinyxml2": ["tinyxml2"],
"lunasvg": ["lunasvg"],
"nlohmann-json": ["nlohmann-json"],
"OpenVDB": ["openvdb"],
"GDCM": ["gdcm"],
Expand Down
14 changes: 14 additions & 0 deletions source/MRIOExtras/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -74,6 +75,19 @@ IF(NOT MRIOEXTRAS_NO_LAS)
target_link_libraries(${PROJECT_NAME} PRIVATE LAZPERF::lazperf)
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}"
)
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()
Expand Down
2 changes: 2 additions & 0 deletions source/MRIOExtras/MRIOExtras.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<ClCompile Include="MRStep.cpp" />
<ClCompile Include="MRTiff.cpp" />
<ClCompile Include="MRSvg.cpp" />
<ClCompile Include="MRSvgImage.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="config.h" />
Expand All @@ -65,6 +66,7 @@
<ClInclude Include="MRZlib.h" />
<ClInclude Include="MRTiff.h" />
<ClInclude Include="MRSvg.h" />
<ClInclude Include="MRSvgImage.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
Expand Down
2 changes: 2 additions & 0 deletions source/MRIOExtras/MRIOExtrasConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ 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()

include("${CMAKE_CURRENT_LIST_DIR}/MRIOExtrasTargets.cmake")
65 changes: 65 additions & 0 deletions source/MRIOExtras/MRSvgImage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "MRSvgImage.h"
#ifndef MRIOEXTRAS_NO_LUNASVG
#include <MRMesh/MRIOParsing.h>
#include <MRMesh/MRStringConvert.h>
#include <MRPch/MRFmt.h>

#include <lunasvg/lunasvg.h>

#include <algorithm>
#include <fstream>

namespace MR::ImageLoad
{

Expected<Image> 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<char>&& buf )
{
return fromSvg( buf.data(), buf.size(), resolution );
} ), file );
}

Expected<Image> 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" );
const float width = doc->width();
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
|| size_t( resolution.x ) * size_t( resolution.y ) >= ( size_t( 1 ) << 29 ) )
return unexpected( "Invalid image resolution" );

Image image{ .pixels = std::vector<Color>( 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();

// bitmap rows go from top to bottom, image rows from bottom to top
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;
}

} // namespace MR::ImageLoad
#endif
22 changes: 22 additions & 0 deletions source/MRIOExtras/MRSvgImage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "config.h"
#ifndef MRIOEXTRAS_NO_LUNASVG
#include "exports.h"

#include <MRMesh/MRExpected.h>
#include <MRMesh/MRImage.h>

#include <filesystem>

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 (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<Image> fromSvg( const std::filesystem::path& file, const Vector2i& resolution );
MRIOEXTRAS_API Expected<Image> fromSvg( const char* data, size_t size, const Vector2i& resolution );

} // namespace MR::ImageLoad
#endif
1 change: 1 addition & 0 deletions source/MRIOExtras/config_cmake.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
Loading