SVG Native viewer is a library that parses and renders SVG Native documents.
SVG Native is an upcoming specification of the SVG WG based on SVG OpenType. SVG Native will be a strict subset of SVG 1.1 and SVG 2.0.
- No stylesheet support (CSS/XSL) with the exception of the basic inheritance model and the following presentation attributes:
clip-path
clip-rule
color
display
fill
fill-opacity
fill-rule
opacity
stroke
stroke-dasharray
stroke-dashoffset
stroke-linecap
stroke-linejoin
stroke-miterlimit
stroke-opacity
stroke-width
stop-color
stop-opacity
visibility
- CSS properties do not support any default property values like
inherit
,initial
,unset
, orrevert
. - No support for scripting, interactions, events, animations, filters, masks, patterns, texts.
- No support for inner
<svg>
or<symbol>
elements. - No support for XML namespaces with the exception of the SVG namespace and the Xlink namespace.
- No support of
objectBoundingBox
ongradientUnits
orclipPathUnits
. - The
var()
CSS value function is limited to the CSS propertiesfill
,stroke
,stop-color
andcolor
. Only color values are allowed.currentColor
is supported.
A valid SVG Native document is always a valid SVG1.1/2.0 document.
- Referenced elements need to be declared first. Example: A
<linearGradient>
element must be defined in the SVG document before its first reference (fill="url(#gradient)"
). viewBox
on<svg>
element does not take translation values into account yet.preserveAspectRatio
is not supported on the<svg>
element yet.- Furthermore, there might be limitations on certain platforms. (E.g. missing spread-method support on CoreGraphics.)
SVG Native Viewer is a C++11 based project and can either be included in the source code of a client directly or linked statically or dynamically.
For rendering, SVG Native Viewer requires a rendering port. Already existing ports include:
- StringSVGRenderer for testing purposes,
- CGSVGRenderer a rendering port using CoreGraphics (Quartz 2D).
- SkiaSVGRenderer a rendering port using Skia.
New ports need to inherit from SVGRenderer and implement the virtual functions.
Here an example how to use SVG Native Viewer with the Skia SkiaSVGRenderer:
// Create the renderer object
auto renderer = std::make_shared<SVGNative::SkiaSVGRenderer>();
// Create SVGDocument object and parse the passed SVG string.
auto doc = std::unique_ptr<SVGNative::SVGDocument>(SVGNative::SVGDocument::CreateSVGDocument(svgInput.c_str(), renderer));
// Setup SkSurface for drawing
auto skRasterSurface = SkSurface::MakeRasterN32Premul(doc->Width(), doc->Height());
auto skRasterCanvas = skRasterSurface->getCanvas();
// Pass SkCanvas to renderer object
renderer->SetSkCanvas(skRasterCanvas);
// Pass drawing commands for SVG document to renderer.
doc->Render();
Refer to the examples in the example/
directory for other port examples.
This repository uses submodules. To initiate and keep submodules up-to-date run the following command:
git submodule update --init
Submodules are located in the third_party/
directory. Used submodules:
- stylesheet (optional) Needed if compiled with limited CSS style support (deprecated).
- cpp-base64 (optional) Needed by some ports for decoding base64 encoded raster image support.
Install:
- CMake Download and run the installer
- Boost Download the ZIP-package and extract the package into
C:>\Platform Files\boost\
- MS Visual Studio 2017 or up Download and install with the installer. Make sure Visual C++ gets installed. (You maybe be able to use the "Community" version for free for non-comercial/enterprise use. See the website from MS for license details.)
With Homebrew:
brew install cmake
brew install llvm
brew install boost
- Apt
sudo apt-get install build-essential libboost-system-dev
For Windows 64 bit:
cmake -Bbuild/win64 -H. -G "Visual Studio 15 2017 Win64"
For Windows 32 bit:
cmake -Bbuild/win32 -H. -G "Visual Studio 15 2017"
For macOS
cmake -Bbuild/mac -H. -G "Xcode"
For Linux
cmake -BBuild/linux -H.
On Linux you may choose to use GCC or Clang/LLVM. Add the following to the command above to choose between one and the other:
-DCMAKE_CXX_COMPILER=g++
for GCC-DCMAKE_CXX_COMPILER=clang
for Clang/LLVM
The following arguments can be passed with the -D
flag and the options ON
or OFF
:
TEXT
adds the Text port to the library. DefaultON
.CG
adds the CoreGraphics/Quartz2D port to the library. DefaultOFF
.Skia
adds the Skia port to the library. DefaultOFF
.
Each port includes an example project using the port. To disable the example projects set the following option to OFF
. Default ON
.
LIBRARY_ONLY
To enable deprecated CSS styling support:
STYLE
adds limited, deprecated support for<style>
element andstyle
attribute.
The following example creates project files for the library with the Text, CoreGraphics/Quartz2D and Skia port and the example applications. Example:
cmake -Bbuild/mac -H. -G "Xcode" -DCG=ON -DSKIA=ON
Note: For testing, build with the TEXT
option set to ON
and LIBRARY_ONLY
option set to OFF
. (The default for both.)
Replace win64
with your platform (mac
for Xcode on macOS)
cmake --build build/win64 --config Release
Only the header version of Boost is required. The following Boost features are used:
- Boost RapidXML (could be replace by RapidXML standalone)
boost::variant
to handle different SVG paint types offill
andstroke
as well as different color value types.- Boost string functions like
boost::tokenizer
,boost::trim
. (Only used by deprecated CSS<style>
element parsing.)
- Make sure your system has Python installed.
- By default, CMake creates the project files for SVGNativeViewerLib and testSVGNative. Follow the steps above to build the test app.
- Run
python script/runTest.py --tests=test/
Here the argument list of runTest.py:
--test
the folder with the test files.--program
the path to testSVGNative. If not provided uses the default, relative build path.--debug
Debug build or Release build of testSVGNative. Only relevant if--program
was not set and defaults to--debug
.
Contributions are welcomed! Read the Contributing Guide for more information.
This project is licensed under the Apache V2 License. See LICENSE for more information.