Magnum::Trade::ResvgImporter class new in Git master

SVG importer plugin using resvg.

Rasterizes Scalable Vector Graphics (*.svg) using the resvg library. Supports also gzipped SVG files (*.svgz).

This plugin provides the SvgImporter plugin.

Usage

This plugin depends on the Trade library and is built if MAGNUM_WITH_RESVGIMPORTER is enabled when building Magnum Plugins. To use as a dynamic plugin, load "ResvgImporter" via Corrade::PluginManager::Manager.

Additionally, if you're using Magnum as a CMake subproject, bundle the magnum-plugins repository and do the following. The resvg library is written in Rust and cannot be added as a CMake subproject so you need to provide it as a system dependency and point CMAKE_PREFIX_PATH to its installation dir if necessary. Note that in order to build against it, it has to be a library including the resvg.h C header, not just the self-contained executable alone that's provided in the official releases.

set(MAGNUM_WITH_RESVGIMPORTER ON CACHE BOOL "" FORCE)
add_subdirectory(magnum-plugins EXCLUDE_FROM_ALL)

# So the dynamically loaded plugin gets built implicitly
add_dependencies(your-app MagnumPlugins::ResvgImporter)

To use as a static plugin or as a dependency of another plugin with CMake, put FindMagnumPlugins.cmake into your modules/ directory, request the ResvgImporter component of the MagnumPlugins package in CMake and link to the MagnumPlugins::ResvgImporter target:

find_package(MagnumPlugins REQUIRED ResvgImporter)

# ...
target_link_libraries(your-app PRIVATE MagnumPlugins::ResvgImporter)

See Downloading and building plugins, Plugin usage with CMake, Loading and using plugins and File format support for more information.

Behavior and limitations

The output is always PixelFormat::RGBA8Unorm and the rasterized size by default corresponds to what's specified in <svg viewBox="...">. Use the dpi configuration option to rasterize at a different size.

The resvg library only provides very limited error reporting in case an import fails, so the plugin will almost always print a generic error message. It can only log to standard error output in a way that cannot be disabled again afterwards and thus the logging is never enabled by the plugin.

External data references

Resvg supports external <image> references, but for them to work the file has to be opened through openFile(). If the referenced file isn't found, if a SVG with an external reference is parsed via openData() or any other error occurs, the resvg library silently ignores it without giving any error or warning, and the output can also contain random pixel values in that case.

The library also claims to support text rendering, but such feature so far wasn't explicitly tested in the plugin implementation.

Plugin-specific configuration

It's possible to tune various import options through configuration(). See below for all options and their default values:

[configuration]
# DPI at which to rasterize the file. Default matches what e.g. Inkscape
# uses, and causes SVG viewBox to be interpreted as literal pixels. Setting
# to e.g. 192 makes the image twice as large, 48 half the size. Fractional
# values are accepted as well. Note that the DPI value is used during file
# opening already, changing it afterwards has no effect.
dpi=96

# Resvg takes the SVG colors, which are encoded as 8-bit sRGB values, and
# premultiplies them with the alpha linearly, i.e. not by decoding the sRGB
# value first, then premultiplying and then encoding a sRGB value back. For
# consistency with other formats that produce unpremultiplied RGBA output
# this premultiplication is undone on import. Set to `premultipliedLinear` to
# import directly the pixels produced by Resvg without any processing.
alphaMode=

See Editing plugin-specific configuration for more information and an example showing how to edit the configuration values.

Base classes

class AbstractImporter
Base for importer plugins.

Constructors, destructors, conversion operators

ResvgImporter(PluginManager::AbstractManager& manager, const Containers::StringView& plugin) explicit
Plugin manager constructor.