SVG Text DOM Manipulation.
#include <iostream>
#ifdef DONNER_EXAMPLE_NATIVE_TEXT
#endif
int main(
int argc,
char* argv[]) {
#ifdef DONNER_EXAMPLE_NATIVE_TEXT
std::string resourceError;
std::cerr << "Packaged resources unavailable: " << resourceError << "\n";
return 1;
}
#endif
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>
)");
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";
text.
setStyle(
"font-size: 40px; font-weight: 500");
newRun.
setStyle(
"fill: #2a9; font-style: italic");
std::cout <<
"After append, text content: \"" << text.
textContent() <<
"\"\n";
#ifdef DONNER_EXAMPLE_NATIVE_TEXT
if (!(textLength > 0)) {
std::cerr << "Text layout did not produce a positive advance\n";
return 1;
}
std::cout << "Computed text length: " << textLength << "\n";
#endif
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:121
Represents a parsed SVG document containing a tree of SVGElement nodes.
Definition SVGDocument.h:106
std::optional< SVGElement > querySelector(std::string_view selector)
Find the first descendant of the root <svg> element, in document order, that matches the given CSS se...
DOM object for a <tspan> element.
Definition SVGTSpanElement.h:103
static SVGTSpanElement Create(SVGDocument &document)
Create a new <tspan> element within the specified document.
Definition SVGTSpanElement.h:132
DOM object for a <text> element.
Definition SVGTextElement.h:80
Collects parse warnings during parsing.
Definition ParseWarningSink.h:29
Backend-agnostic renderer that resolves to the active build backend (tiny-skia by default,...
Definition Renderer.h:82
int height() const override
Returns the rendered height in pixels.
bool save(const char *filename)
Saves the last rendered frame to a PNG file.
void draw(SVGDocument &document) override
Draws the SVG document using the active backend.
int width() const override
Returns the rendered width in pixels.
Represents a parsed SVG document containing a tree of SVGElement nodes.
Definition SVGDocument.h:106
Represents a single SVG element (e.g., <rect>, <circle>, <g>, <text>, etc.) within an SVGDocument.
Definition SVGElement.h:265
Derived cast()
Cast this element to its derived type.
Definition SVGElement.h:649
void setStyle(std::string_view style)
Set the element style, the value of the "style" attribute.
void appendChild(const SVGElement &child)
Append child as a child of the current node.
DOM object for a <tspan> element.
Definition SVGTSpanElement.h:103
double getComputedTextLength() const
Computes the total advance distance for all glyphs.
void appendText(std::string_view text)
Append text content from text or CDATA nodes.
RcString textContent() const
Get the raw text content concatenated from all direct child text nodes.
DOM object for a <text> element.
Definition SVGTextElement.h:80
void setY(std::optional< Lengthd > value)
Sets the y attribute list to a single value (absolute y-position).
void setX(std::optional< Lengthd > value)
Sets the x attribute list to a single value (absolute x-position).
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).
bool PreparePackagedResources(std::string *error)
Native preflight validates the entire linked set; only essential resources are retained.
Length< double > Lengthd
Shorthand for Length<double>.
Definition Length.h:279
int main()
Run the rendering example and report its result.
Definition render_test.cc:34