Donner 0.5.0
Embeddable browser-grade SVG2 engine
Loading...
Searching...
No Matches
donner::svg::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
 Callback used to parse external SVG content referenced from within a document.

Public Member Functions

 SVGDocument ()
 Constructor to create an empty SVGDocument with default settings.
 SVGDocument (Settings settings)
 Constructor to create an empty SVGDocument.
Registryregistry ()
 Get the underlying ECS Registry, which holds all data for the document, for advanced use.
const Registryregistry () 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 canvasFromDocumentTransform () const
 Returns the transform that maps points from the SVG document's viewBox coordinate space into the canvas-scaled output space.
bool operator== (const SVGDocument &other) const
 Returns true if the two SVGDocument handles reference the same underlying document.
std::optional< SVGElement > querySelector (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 donner::svg::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

Class Documentation

◆ donner::svg::SVGDocument::Settings

struct donner::svg::SVGDocument::Settings

Document settings which configure the document behavior.

Collaboration diagram for donner::svg::SVGDocument::Settings:
[legend]
Class Members
ProcessingMode processingMode = ProcessingMode::DynamicInteractive Processing mode for this document. Defaults to donner::svg::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

Initial value:
std::function<std::optional<SVGDocumentHandle>(
const std::vector<uint8_t>& svgContent, ParseWarningSink& warningSink)>
Collects parse warnings during parsing.
Definition ParseWarningSink.h:28

Callback used to parse external SVG content referenced from within a document.

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

◆ canvasFromDocumentTransform()

Transform2d donner::svg::SVGDocument::canvasFromDocumentTransform ( ) const

Returns the transform that maps points from the SVG document's viewBox coordinate space into the canvas-scaled output space.

This bakes in the preserveAspectRatio fit (letterbox offset + uniform scale) between the viewBox and the current canvas size.

Naming: per the destFromSource convention, applying this transform to a viewBox-space point yields a canvas-space point — i.e. it is canvasFromDocument, despite an earlier misnomer. Callers that need the opposite direction (canvas pixel → document viewBox coordinate, e.g. click math in editors/viewers) should invert it.

◆ 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.

◆ 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.

◆ useAutomaticCanvasSize()

void donner::svg::SVGDocument::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 file: