Donner 0.5.0
Embeddable browser-grade SVG2 engine
Loading...
Searching...
No Matches
SVGDocument Class Reference

Represents a parsed SVG document containing a tree of SVGElement nodes. More...

#include "donner/svg/SVGDocument.h"

Classes

struct  Settings
 Document settings which configure the document behavior. More...

Public Types

using SvgParseCallback

Public Member Functions

 SVGDocument ()
 Constructor to create an empty SVGDocument with default settings.
 SVGDocument (Settings settings)
 Constructor to create an empty SVGDocument.
Registry & registry ()
 Get the underlying ECS Registry, which holds all data for the document, for advanced use.
const Registry & registry () const
 Get the underlying ECS Registry, which holds all data for the document, for advanced use.
SVGDocumentHandle handle () const
 Get the internal shared document handle used by this value facade.
EntityHandle rootEntityHandle () const
 Get the root ECS Entity of the document, for advanced use.
SVGSVGElement svgElement () const
 Get the root "<svg>" element of the document.
void setCanvasSize (int width, int height)
 Set the canvas (output image) size to a fixed width and height, in pixels.
void useAutomaticCanvasSize ()
 Automatically determine the canvas size from the root <svg> element's width and height attributes.
Vector2i canvasSize () const
 Get the current canvas size, or the default size (512x512) if the canvas size has not been explicitly set.
int width () const
 Get the width of the SVG document, in pixels.
int height () const
 Get the height of the SVG document, in pixels.
Transform2d documentFromCanvasTransform () const
 Get the scale transform from the canvas to the SVG document.
bool operator== (const SVGDocument &other) const
 Returns true if the two SVGDocument handles reference the same underlying document.
std::optional< SVGElementquerySelector (std::string_view selector)
 Find the first element in the tree that matches the given CSS selector.

Static Public Member Functions

static SVGDocument CreateFromHandle (SVGDocumentHandle handle)
 Rehydrate an SVGDocument facade from an internal shared document handle.

Friends

class SVGElement
class parser::SVGParserImpl

Detailed Description

Represents a parsed SVG document containing a tree of SVGElement nodes.

To create a document, parse SVG content with donner::svg::parser::SVGParser::ParseSVG, or construct an empty document and build the tree programmatically. Access the root <svg> element with svgElement(), find elements with querySelector(), and render with Renderer.

SVGDocument and SVGElement expose a familiar DOM API for traversal and manipulation (e.g., firstChild(), appendChild(), querySelector()). Elements are lightweight value types that can be copied and passed on the stack.

SVGDocument is not thread-safe — do not access the same document from multiple threads concurrently.

Note
Internally, data is stored using an Entity Component System (ECS) for cache-friendly access during rendering. The registry() and entityHandle() accessors expose this for advanced use cases, but most users can ignore the ECS layer entirely.
See also
SVGElement
Examples
svg_filter_interaction.cc, svg_text_interaction.cc, and svg_to_png.cc.

Class Documentation

◆ SVGDocument::Settings

struct SVGDocument::Settings

Document settings which configure the document behavior.

Collaboration diagram for SVGDocument::Settings:
[legend]
Class Members
ProcessingMode processingMode = ProcessingMode::DynamicInteractive Processing mode for this document. Defaults to ProcessingMode::DynamicInteractive.
unique_ptr< ResourceLoaderInterface > resourceLoader Resource loader to use for loading external resources.
SvgParseCallback svgParseCallback Callback to parse SVG content into sub-documents.

Member Typedef Documentation

◆ SvgParseCallback

using donner::svg::SVGDocument::SvgParseCallback
Initial value:
std::function<std::optional<SVGDocumentHandle>(
const std::vector<uint8_t>& svgContent, ParseWarningSink& warningSink)>

Constructor & Destructor Documentation

◆ SVGDocument()

donner::svg::SVGDocument::SVGDocument ( Settings settings)
explicit

Constructor to create an empty SVGDocument.

To load a document from an SVG file, use donner::svg::parser::SVGParser::ParseSVG.

Parameters
settingsSettings to configure the document.

Member Function Documentation

◆ CreateFromHandle()

SVGDocument donner::svg::SVGDocument::CreateFromHandle ( SVGDocumentHandle handle)
inlinestatic

Rehydrate an SVGDocument facade from an internal shared document handle.

Parameters
handleShared document-state handle to wrap.
Returns
A new SVGDocument facade referencing the same underlying document state.

◆ handle()

SVGDocumentHandle donner::svg::SVGDocument::handle ( ) const
inline

Get the internal shared document handle used by this value facade.

Returns
Shared document-state handle backing this SVGDocument.

◆ height()

int donner::svg::SVGDocument::height ( ) const
inline

Get the height of the SVG document, in pixels.

This is the height of the canvas, which may be different from the height of the SVG content.

◆ querySelector()

std::optional< SVGElement > donner::svg::SVGDocument::querySelector ( std::string_view selector)

Find the first element in the tree that matches the given CSS selector.

auto element = document.querySelector("#elementId");

Complex selectors are supported:

auto element = document.querySelector("svg > g:nth-child(2) > rect");
Parameters
selectorCSS selector to match.
Returns
The first matching element, or std::nullopt if no element matches.
Examples
svg_filter_interaction.cc, and svg_text_interaction.cc.

◆ setCanvasSize()

void donner::svg::SVGDocument::setCanvasSize ( int width,
int height )

Set the canvas (output image) size to a fixed width and height, in pixels.

This controls the rendered output dimensions and may differ from the SVG's viewBox. If not set, defaults to 512x512 or the size specified by the root <svg> element's width/height attributes (when using useAutomaticCanvasSize()).

Parameters
widthWidth of the canvas, in pixels.
heightHeight of the canvas, in pixels.
Examples
svg_to_png.cc.

◆ useAutomaticCanvasSize()

Automatically determine the canvas size from the root <svg> element's width and height attributes.

If those attributes are not set, falls back to the default size (512x512).

◆ width()

int donner::svg::SVGDocument::width ( ) const
inline

Get the width of the SVG document, in pixels.

This is the width of the canvas, which may be different from the width of the SVG content.


The documentation for this class was generated from the following files: