Skip to content

Commit 5403bff

Browse files
committed
accessor: revised the usage with the modern tvg accessor style
issue: thorvg/thorvg#4217
1 parent 801d6d1 commit 5403bff

1 file changed

Lines changed: 18 additions & 17 deletions

File tree

src/Accessor.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,38 @@
2828

2929
struct UserExample : tvgexam::Example
3030
{
31+
// This example demonstrates two approaches to accessing internal SVG scene nodes.
3132
bool content(tvg::Canvas* canvas, uint32_t w, uint32_t h) override
3233
{
33-
//load the tvg file
34+
// SVG Picture
3435
auto picture = tvg::Picture::gen();
35-
auto result = picture->load(EXAMPLE_DIR"/svg/favorite_on.svg");
36-
if (!tvgexam::verify(result)) return false;
36+
37+
picture->accessible = true; // allow accessing to svg internals, it must be set before svg load call.
38+
picture->load(EXAMPLE_DIR"/svg/favorite_on.svg");
3739
picture->size(w, h);
3840

41+
/* 1. This demonstartes a traversing the internal scene tree of the SVG picture. */
3942
auto accessor = unique_ptr<tvg::Accessor>(tvg::Accessor::gen());
4043

41-
//The callback function from lambda expression.
42-
//This function will be called for every paint nodes of the picture tree.
43-
auto f = [](const tvg::Paint* paint, void* data) -> bool
44-
{
45-
if (paint->type() == tvg::Type::Shape) {
46-
auto shape = (tvg::Shape*) paint;
47-
//override color?
48-
uint8_t r, g, b;
49-
shape->fill(&r, &g, &b);
50-
if (r == 255 && g == 180 && b == 0)
51-
shape->fill(0, 0, 255);
44+
// If picture->accessible is set to true, only ID-accessible nodes are traversed,
45+
// which improves efficiency. Otherwise, all nodes are considered.
46+
auto f = [](const tvg::Paint* paint, void* data) -> bool {
47+
auto accessor = static_cast<tvg::Accessor*>(data);
5248

49+
// figure out SVG node with the unique ID "star".
50+
if (!strcmp(accessor->name(paint->id), "star")) {
51+
// override color
52+
auto shape = (tvg::Shape*) paint;
53+
shape->fill(0, 0, 255);
5354
}
5455

55-
//You can return false, to stop traversing immediately.
56+
// you can return false, to stop traversing immediately
5657
return true;
5758
};
5859

59-
if (!tvgexam::verify(accessor->set(picture, f, nullptr))) return false;
60+
if (!tvgexam::verify(accessor->set(picture, f, accessor.get()))) return false;
6061

61-
// Try to retrieve the shape that corresponds to the SVG node with the unique ID "star".
62+
/* 2. This demonstrates a direct-access to the shape that corresponds to the SVG node with the unique ID "star". */
6263
if (auto paint = picture->paint(tvg::Accessor::id("star"))) {
6364
auto shape = static_cast<tvg::Shape*>(const_cast<tvg::Paint*>(paint));
6465
shape->strokeFill(255, 255, 0);

0 commit comments

Comments
 (0)