Donner
C++20 SVG rendering library
Loading...
Searching...
No Matches
svg_to_png.cc

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.

To run:

bazel run --run_under="cd $PWD &&" //examples:svg_to_png -- donner_splash.svg

The output is saved to "output.png" in the current working directory.

/**
* @example svg_to_png.cc Render SVG to PNG
* @details This example demonstrates how to parse an SVG file and render it to a PNG file using the
* Skia rendering backend.
*
* To run:
*
* ```sh
* bazel run --run_under="cd $PWD &&" //examples:svg_to_png -- donner_splash.svg
* ```
*
* The output is saved to "output.png" in the current working directory.
*/
#include <filesystem>
#include <fstream>
#include <iostream>
#include <vector>
#include "donner/svg/SVG.h"
using namespace donner::base;
using namespace donner::base::parser;
using namespace donner::svg;
using namespace donner::svg::parser;
/**
* Main function, usage: svg_to_png <filename>
*/
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;
}
//! [load_file]
// Load the file and store it in a mutable std::vector<char>.
std::ifstream file(argv[1]);
if (!file) {
std::cerr << "Could not open file " << argv[1] << "\n";
std::abort();
}
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));
//! [load_file]
// Parse the SVG. Note that the lifetime of the vector must be longer than the returned
// SVGDocument, since it is referenced internally.
//! [parse]
// Allow data-name attributes without generating a warning.
options.disableUserAttributes = false;
// Allow under-development features, such as <filter>
options.enableExperimental = true;
std::vector<ParseError> warnings;
// warnings and options are optional, call ParseSVG(fileData) to use defaults and ignore warnings.
ParseResult<SVGDocument> maybeDocument = SVGParser::ParseSVG(fileData, &warnings, options);
//! [parse]
//! [handle_errors]
// ParseResult either contains an SVGDocument or an error.
if (maybeDocument.hasError()) {
std::cerr << "Parse Error: " << maybeDocument.error() << "\n";
std::abort();
}
std::cout << "Parsed successfully.\n";
if (!warnings.empty()) {
std::cout << "Warnings:\n";
for (ParseError& w : warnings) {
std::cout << " " << w << "\n";
}
}
SVGDocument document = std::move(maybeDocument.result());
//! [handle_errors]
//! [set_canvas_size]
// Setting the canvas size is equivalent to resizing a browser window. Some SVGs may scale to fit,
// other ones may only render at their base size. To auto-size, either omit this call or invoke
// useAutomaticCanvasSize().
document.setCanvasSize(800, 600);
//! [set_canvas_size]
//! [render]
// Draw the document, store the image in-memory.
RendererSkia renderer;
renderer.draw(document);
std::cout << "Final size: " << renderer.width() << "x" << renderer.height() << "\n";
// Then save it out using the save API.
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;
}
//! [render]
}
A parser result, which may contain a result of type T, or an error, or both.
Definition ParseResult.h:17
ParseError & error() &
Returns the contained error.
Definition ParseResult.h:76
bool hasError() const noexcept
Returns true if this ParseResult contains an error.
Definition ParseResult.h:105
T & result() &
Returns the contained result.
Definition ParseResult.h:46
Rendering backend using Skia, https://github.com/google/skia.
Definition RendererSkia.h:26
int height() const
Get the height of the rendered image in pixels.
Definition RendererSkia.h:119
int width() const
Get the width of the rendered image in pixels.
Definition RendererSkia.h:116
void draw(SVGDocument &document)
Draw the SVG document using the renderer.
Definition RendererSkia.cc:1181
bool save(const char *filename)
Save the rendered image to a PNG file.
Definition RendererSkia.cc:1269
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
Parsers for shared data types such as NumberParser and LengthParser.
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.
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