SVG Text DOM Manipulation.
#include <iostream>
int main(int argc, char* argv[]) {
const std::string_view kSvgSource(R"(
<svg xmlns="http://www.w3.org/2000/svg" width="400" height="120" viewBox="0 0 400 120">
<text x="20" y="70" font-family="sans-serif" font-size="32" fill="#1f5a8a">
Hello, <tspan fill="#c33" font-weight="bold">Donner</tspan>!
</text>
</svg>
)");
auto maybeDocument =
if (maybeDocument.hasError()) {
std::cerr << "Parse error: " << maybeDocument.error() << "\n";
return 1;
}
SVGDocument document = std::move(maybeDocument.result());
std::optional<SVGElement> maybeText = document.
querySelector(
"text");
std::cout <<
"Current text content: \"" << text.
textContent() <<
"\"\n";
std::cout <<
"After append, text content: \"" << text.
textContent() <<
"\"\n";
const std::string output = argc >= 2 ? argv[1] : "text_interaction.png";
if (!renderer.
save(output.c_str())) {
std::cerr << "Failed to save " << output << "\n";
return 1;
}
std::cout <<
"Rendered to " << output <<
" (" << renderer.
width() <<
"x" << renderer.
height()
<< ")\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
Represents a parsed SVG document containing a tree of SVGElement nodes.
Definition SVGDocument.h:39
std::optional< SVGElement > querySelector(std::string_view selector)
Find the first element in the tree that matches the given CSS selector.
Definition SVGDocument.cc:75
DOM object for a "<tspan>" element.
Definition SVGTSpanElement.h:98
static SVGTSpanElement Create(SVGDocument &document)
Create a new "<tspan>" element within the specified document.
Definition SVGTSpanElement.h:127
DOM object for a "<text>" element.
Definition SVGTextElement.h:79
Collects parse warnings during parsing.
Definition ParseWarningSink.h:28
Backend-agnostic renderer that resolves to the active build backend (Skia or tiny-skia).
Definition Renderer.h:27
int height() const override
Returns the rendered height in pixels.
Definition Renderer.cc:131
bool save(const char *filename)
Saves the last rendered frame to a PNG file.
Definition Renderer.cc:116
void draw(SVGDocument &document) override
Draws the SVG document using the active backend.
Definition Renderer.cc:18
int width() const override
Returns the rendered width in pixels.
Definition Renderer.cc:127
Represents a parsed SVG document containing a tree of SVGElement nodes.
Definition SVGDocument.h:39
Represents a single SVG element (e.g., <rect>, <circle>, <g>, <text>, etc.) within an SVGDocument.
Definition SVGElement.h:52
Derived cast()
Cast this element to its derived type.
Definition SVGElement.h:330
void setStyle(std::string_view style)
Set the element style, the value of the "style" attribute.
Definition SVGElement.cc:199
void appendChild(const SVGElement &child)
Append child as a child of the current node.
Definition SVGElement.cc:413
DOM object for a "<tspan>" element.
Definition SVGTSpanElement.h:98
void appendText(std::string_view text)
Append text content from text or CDATA nodes.
Definition SVGTextContentElement.cc:167
RcString textContent() const
Get the raw text content concatenated from all child text nodes.
Definition SVGTextContentElement.cc:196
DOM object for a "<text>" element.
Definition SVGTextElement.h:79
void setY(std::optional< Lengthd > value)
Sets the y attribute list to a single value (absolute y-position).
Definition SVGTextPositioningElement.cc:40
void setX(std::optional< Lengthd > value)
Sets the x attribute list to a single value (absolute x-position).
Definition SVGTextPositioningElement.cc:12
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).
Definition SVGParser.cc:377
Length< double > Lengthd
Shorthand for Length<double>.
Definition Length.h:228