Donner
C++20 SVG rendering library
Loading...
Searching...
No Matches
donner::svg::SVGDocument Class Reference

Represents an SVG document, which holds a collection of SVGElement as the document tree. More...

#include "donner/svg/SVGDocument.h"

Classes

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

Public Member Functions

 SVGDocument (Settings 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.
 
Entity rootEntity () 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 size to a fixed width and height, in pixels.
 
void useAutomaticCanvasSize ()
 Automatically determine the canvas size based on the size of the root "<svg>" element.
 
Vector2i canvasSize ()
 Get the current canvas size, or the default size (512x512) if the canvas size has not been explicitly set.
 
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.
 

Detailed Description

Represents an SVG document, which holds a collection of SVGElement as the document tree.

Each SVGElement may only belong to a single document, and each document can have only one root. SVGDocument is responsible for managing the lifetime of all elements in the document, by storing a shared pointer to the internal Registry data-store.

Data is stored using the Entity Component System (Entity Component System (ECS)) pattern, which is a data-oriented design optimized for fast data access and cache locality, particularly during rendering.

SVGDocument and SVGElement provide a facade over the ECS, and surface a familiar Document Object Model (DOM) API to traverse and manipulate the document tree, which is internally stored within Components in the ECS. This makes SVGElement a thin wrapper around an Entity, making the object lightweight and usable on the stack.

See also
SVGElement
Entity Component System (ECS)
Examples
svg_to_png.cc, and svg_tree_interaction.cc.

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
unique_ptr< ResourceLoaderInterface > resourceLoader Resource loader to use for loading external resources.

Constructor & Destructor Documentation

◆ SVGDocument()

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

Constructor to create an empty SVGDocument.

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

Parameters
settingsSettings to configure the document.

Member Function Documentation

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

◆ setCanvasSize()

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

Set the canvas size to a fixed width and height, in pixels.

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

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