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");
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:119
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
A parser result, which may contain a result of type T, or an error, or both.
Definition ParseResult.h:17
Collects parse warnings during parsing.
Definition ParseWarningSink.h:29
static ParseWarningSink Disabled()
Construct a disabled sink that discards all warnings (no-op).
Definition ParseWarningSink.h:41
DOM object for a <circle> element.
Definition SVGCircleElement.h:71
void setR(Lengthd value)
Set the radius.
void setCx(Lengthd value)
Set the center X coordinate.
void setCy(Lengthd value)
Set the center Y coordinate.
static SVGCircleElement Create(SVGDocument &document)
Create a new <circle> element.
Definition SVGCircleElement.h:99
Represents a parsed SVG document containing a tree of SVGElement nodes.
Definition SVGDocument.h:58
SVGSVGElement svgElement() const
Get the root <svg> element of the document.
std::optional< SVGElement > querySelector(std::string_view selector)
Find the first element in the tree that matches the given CSS selector.
Derived cast()
Cast this element to its derived type.
Definition SVGElement.h:611
const PropertyRegistry & getComputedStyle() const
Get the computed CSS style of this element, after the CSS cascade has been applied.
void setStyle(std::string_view style)
Set the element style, the value of the "style" attribute.
void insertBefore(const SVGElement &newNode, std::optional< SVGElement > referenceNode)
Insert newNode as a child, before referenceNode.
std::optional< Path > computedSpline() const
Get the computed path geometry as a Path - a sequence of line segments, quadratic and cubic Bezier cu...
DOM object for a <path> element.
Definition SVGPathElement.h:96
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).
Length< double > Lengthd
Shorthand for Length<double>.
Definition Length.h:277
A diagnostic message from a parser, with severity, source range, and human-readable reason.
Definition ParseDiagnostic.h:31