SVG Tree Interaction.
SVG Tree Interaction Demonstrates how to interact with the SVG DOM. This example loads an SVG file, gets the SVGPathElement for a path in the image, then prints metadata and modifies it.
#include <iostream>
int main(int argc, char* argv[]) {
const std::string_view svgContents(R"(
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" viewBox="0 0 10 10">
<path d="M 1 1 L 4 5" stroke="blue" />
</svg>
)");
std::cerr <<
"Parse Error " << maybeResult.
error() <<
"\n";
std::abort();
}
std::optional<donner::svg::SVGElement> maybePath = document.
querySelector(
"path");
if (std::optional<donner::svg::PathSpline> spline = path.
computedSpline()) {
std::cout << "Path: " << *spline << "\n";
std::cout << "Length: " << spline->pathLength() << " userspace units\n";
} else {
std::cout << "Path is empty\n";
}
return 0;
}
#define UTILS_RELEASE_ASSERT_MSG(x, msg)
An assert that evaluates on both release and debug builds and errors with the provided msg.
Definition Utils.h:102
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
ParseError & error() &
Returns the contained error.
Definition ParseResult.h:81
A parser result, which may contain a result of type T, or an error, or both.
Definition ParseResult.h:17
DOM object for a "<circle>" element.
Definition SVGCircleElement.h:61
void setR(Lengthd value)
Set the radius.
Definition SVGCircleElement.cc:31
void setCx(Lengthd value)
Set the center X coordinate.
Definition SVGCircleElement.cc:17
void setCy(Lengthd value)
Set the center Y coordinate.
Definition SVGCircleElement.cc:24
static SVGCircleElement Create(SVGDocument &document)
Create a new "<circle>" element.
Definition SVGCircleElement.h:89
Represents an SVG document, which holds a collection of SVGElement as the document tree.
Definition SVGDocument.h:32
SVGSVGElement svgElement() const
Get the root "<svg>" element of the document.
Definition SVGDocument.cc:40
std::optional< SVGElement > querySelector(std::string_view selector)
Find the first element in the tree that matches the given CSS selector.
Definition SVGDocument.cc:69
Derived cast()
Cast this element to its derived type.
Definition SVGElement.h:329
const PropertyRegistry & getComputedStyle() const
Get the computed CSS style of this element, after the CSS cascade.
Definition SVGElement.cc:306
void setStyle(std::string_view style)
Set the element style, the value of the "style" attribute.
Definition SVGElement.cc:107
void insertBefore(const SVGElement &newNode, std::optional< SVGElement > referenceNode)
Insert newNode as a child, before referenceNode.
Definition SVGElement.cc:271
std::optional< PathSpline > computedSpline() const
Get the path spline, computed by converting this shape's path to a spline.
Definition SVGGeometryElement.cc:34
DOM object for a "<path>" element.
Definition SVGPathElement.h:85
static ParseResult< SVGDocument > ParseSVG(std::string_view source, std::vector< ParseError > *outWarnings=nullptr, Options options={}, std::unique_ptr< ResourceLoaderInterface > resourceLoader=nullptr) noexcept
Parses an SVG XML document from a string (typically the contents of a .svg file).
Definition SVGParser.cc:337
Length< double > Lengthd
Shorthand for Length<double>.
Definition Length.h:214
Error context for a failed parse, such as the error reason, line, and character offset.
Definition ParseError.h:14