sammycage / lunasvg

SVG rendering and manipulation library in C++
MIT License
866 stars 124 forks source link

Is there a way to change the text of a <text> or <tspan> node? #180

Closed bitgenerator closed 1 week ago

bitgenerator commented 1 week ago

Is there a way to change the text of a text or tspan node, like the attributes?

sammycage commented 1 week ago

Currently, no.

sammycage commented 1 week ago

@bitgenerator Text nodes manipulation has been added in the latest commit. Here's an example:

auto data = R"SVG(<svg xmlns="http://www.w3.org/2000/svg">
<text id='a' y='120' font-size='120'> Hello World</text>
</svg>)SVG";

auto document = Document::loadFromData(data);

auto text_element = document->getElementById("a");
auto text_node = text_element.children().front().toTextNode();
text_node.setData("I love SVG!!!");

document->updateLayout();

This should help you modify the text content as needed.

bitgenerator commented 1 week ago

Thanks a lot! I've written a similar function but it's good to have a official change. ;-)