Render SVG to PNG.
Render SVG to PNG This example demonstrates how to parse an SVG file and render it to a PNG file using the Skia rendering backend.
The output is saved to "output.png" in the current working directory.
#include <filesystem>
#include <fstream>
#include <iostream>
#include <vector>
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Unexpected arg count.\n";
std::cerr << "USAGE: svg_to_png <filename>\n";
return 1;
}
std::ifstream file(argv[1]);
if (!file) {
std::cerr << "Could not open file " << argv[1] << "\n";
return 1;
}
std::string fileData;
file.seekg(0, std::ios::end);
const size_t fileLength = file.tellg();
file.seekg(0);
fileData.resize(fileLength);
file.read(fileData.data(), static_cast<std::streamsize>(fileLength));
std::vector<ParseError> warnings;
std::cerr <<
"Parse Error: " << maybeDocument.
error() <<
"\n";
return 1;
}
std::cout << "Parsed successfully.\n";
if (!warnings.empty()) {
std::cout << "Warnings:\n";
std::cout << " " << w << "\n";
}
}
std::cout <<
"Final size: " << renderer.
width() <<
"x" << renderer.
height() <<
"\n";
if (renderer.
save(
"output.png")) {
std::cout << "Saved to file: " << std::filesystem::absolute("output.png") << "\n";
return 0;
} else {
std::cerr << "Failed to save to file: " << std::filesystem::absolute("output.png") << "\n";
return 1;
}
}
A parser result, which may contain a result of type T, or an error, or both.
Definition ParseResult.h:17
bool hasError() const noexcept
Returns true if this ParseResult contains an error.
Definition ParseResult.h:110
T & result() &
Returns the contained result.
Definition ParseResult.h:51
ParseError & error() &
Returns the contained error.
Definition ParseResult.h:81
Rendering backend using Skia, https://github.com/google/skia.
Definition RendererSkia.h:29
int height() const
Get the height of the rendered image in pixels.
Definition RendererSkia.h:122
int width() const
Get the width of the rendered image in pixels.
Definition RendererSkia.h:119
void draw(SVGDocument &document)
Draw the SVG document using the renderer.
Definition RendererSkia.cc:1449
bool save(const char *filename)
Save the rendered image to a PNG file.
Definition RendererSkia.cc:1537
Represents an SVG document, which holds a collection of SVGElement as the document tree.
Definition SVGDocument.h:32
void setCanvasSize(int width, int height)
Set the canvas size to a fixed width and height, in pixels.
Definition SVGDocument.cc:44
static ParseResult< SVGDocument > ParseSVG(std::string_view source, std::vector< ParseError > *outWarnings=nullptr, Options options={}, std::unique_ptr< ResourceLoaderInterface > resourceLoader=nullptr) noexcept
Parses an SVG XML document from a string (typically the contents of a .svg file).
Definition SVGParser.cc:337
Parsers for the SVG XML format, SVGParser, as well as individual parsers for SVG components,...
Donner SVG library, which can load, manipulate and render SVG files.
Top-level Donner namespace, which is split into different sub-namespaces such as donner::svg and donn...
Error context for a failed parse, such as the error reason, line, and character offset.
Definition ParseError.h:14
Options to modify the parsing behavior.
Definition SVGParser.h:22
bool enableExperimental
Enable experimental or incomplete features.
Definition SVGParser.h:58
bool disableUserAttributes
By default, the parser will ignore user-defined attributes (only presentation attributes will be pars...
Definition SVGParser.h:50