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 active rendering backend.
The output is saved to "output.png" in the current working directory.
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <sstream>
int main(int argc, char* argv[]) {
if (const char* bwd = std::getenv("BUILD_WORKING_DIRECTORY")) {
std::filesystem::current_path(bwd);
}
if (argc != 2) {
std::cerr << "Unexpected arg count.\n";
std::cerr << "USAGE: svg_to_png <filename>\n";
return 1;
}
const auto* fileData = std::get_if<std::string>(&fileResult);
if (fileData == nullptr) {
std::cerr << FileReadErrorMessage(std::get<FileReadError>(fileResult)) << ": "
return 1;
}
std::ostringstream diagnostic;
diagnostic << maybeDocument.
error();
return 1;
}
std::cout << "Parsed successfully.\n";
std::cout << "Warnings:\n";
std::ostringstream diagnostic;
diagnostic << w;
}
}
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
ParseDiagnostic & error() &
Returns the contained error.
Definition ParseResult.h:81
Represents a parsed SVG document containing a tree of SVGElement nodes.
Definition SVGDocument.h:58
void setCanvasSize(int width, int height)
Set the canvas (output image) size to a fixed width and height, in pixels.
Collects parse warnings during parsing.
Definition ParseWarningSink.h:29
const std::vector< ParseDiagnostic > & warnings() const
Access the collected warnings.
Definition ParseWarningSink.h:76
bool hasWarnings() const
Returns true if any warnings have been added.
Definition ParseWarningSink.h:79
Backend-agnostic renderer that resolves to the active build backend (Skia or tiny-skia).
Definition Renderer.h:82
int height() const override
Returns the rendered height in pixels.
bool save(const char *filename)
Saves the last rendered frame to a PNG file.
void draw(SVGDocument &document) override
Draws the SVG document using the active backend.
int width() const override
Returns the rendered width in pixels.
static ParseResult< SVGDocument > ParseSVG(std::string_view source, ParseWarningSink &warningSink, Options options={}, SVGDocument::Settings settings={}) noexcept
Parses an SVG XML document from a string (typically the contents of a .svg file).
static constexpr size_t kDefaultMaximumInputSize
Default maximum number of source or expanded SVG bytes accepted from untrusted input.
Definition SVGParser.h:20
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...
std::string EscapeTerminalText(std::string_view text, bool preserveNewlines=false)
Escape untrusted bytes and terminal format controls while preserving printable UTF-8.
Definition TerminalEscape.h:13
FileReadResult ReadFileBounded(const std::filesystem::path &path, size_t maximumSize)
Open and read a regular file without allocating more than maximumSize bytes.
A diagnostic message from a parser, with severity, source range, and human-readable reason.
Definition ParseDiagnostic.h:31
Options to modify the parsing behavior.
Definition SVGParser.h:37
bool disableUserAttributes
By default, the parser will ignore user-defined attributes (only presentation attributes will be pars...
Definition SVGParser.h:65