Skip to content

Commit

Permalink
common Paint: Add name getter/setter API
Browse files Browse the repository at this point in the history
Adds an API that can set/get string type name to tvgPaint.
This can be used when the user needs to traverse the scene tree
or find a specific instance.
+) The SVG's "id" attribute is set to name when SVG is loaded.
  • Loading branch information
JSUYA committed Nov 29, 2022
1 parent 3ba0b8a commit 9b035f0
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
22 changes: 22 additions & 0 deletions inc/thorvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,28 @@ class TVG_EXPORT Paint
*/
uint32_t identifier() const noexcept;

/**
* @brief Sets the name for the current instance.
*
* The name can be up to 127 characters long.
*
* @note The "id" attribute of the SVG format is set to name, and save/load is not yet supported in the case of the TVG format.
*
* @return Result::Success when succeed, Result::InvalidArguments in case the name length exceeded 128 characters.
*
* @BETA_API
*/
Result name(std::string name) const noexcept;

/**
* @brief Returns the name set for the current instance.
*
* @return The name set on the current instance when succeed, otherwise an empty string.
*
* @BETA_API
*/
std::string name() const noexcept;

_TVG_DECLARE_ACCESSOR();
_TVG_DECLARE_PRIVATE(Paint);
};
Expand Down
16 changes: 16 additions & 0 deletions src/lib/tvgPaint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Paint* Paint::Impl::duplicate()
}

ret->pImpl->opacity = opacity;
ret->pImpl->name = name;

if (compData) ret->pImpl->composite(ret, compData->target->duplicate(), compData->method);

Expand Down Expand Up @@ -405,3 +406,18 @@ uint32_t Paint::identifier() const noexcept
{
return pImpl->id;
}


Result Paint::name(std::string name) const noexcept
{
if (name.length() > 128) return Result::InvalidArguments;
pImpl->name = name;
return Result::Success;
}


std::string Paint::name() const noexcept
{
return pImpl->name;
}

1 change: 1 addition & 0 deletions src/lib/tvgPaint.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ namespace tvg
uint32_t ctxFlag = ContextFlag::Invalid;
uint32_t id;
uint8_t opacity = 255;
std::string name = "";

~Impl()
{
Expand Down
3 changes: 3 additions & 0 deletions src/loaders/svg/tvgSvgSceneBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ static unique_ptr<Scene> _sceneBuildHelper(const SvgNode* node, const Box& vBox,
} else if ((*child)->type == SvgNodeType::Image) {
auto image = _imageBuildHelper(*child, vBox, svgPath);
if (image) {
if ((*child)->id) image->name(std::string((*child)->id));
scene->push(move(image));
if (isMaskWhite) *isMaskWhite = false;
}
Expand All @@ -743,13 +744,15 @@ static unique_ptr<Scene> _sceneBuildHelper(const SvgNode* node, const Box& vBox,
*isMaskWhite = false;
}
}
if ((*child)->id) shape->name(std::string((*child)->id));
scene->push(move(shape));
}
}
}
_applyComposition(scene.get(), node, vBox, svgPath);
scene->opacity(node->style->opacity);
}
if (node->id) scene->name(std::string(node->id));
return scene;
}
return nullptr;
Expand Down
14 changes: 14 additions & 0 deletions test/testPaint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ TEST_CASE("Opacity", "[tvgPaint]")
REQUIRE(shape->opacity() == 0);
}

TEST_CASE("Name", "[tvgPaint]")
{
auto shape = Shape::gen();
REQUIRE(shape);

REQUIRE(shape->name().empty());

REQUIRE(shape->name("test") == Result::Success);
REQUIRE(shape->name() == "test");
}

TEST_CASE("Bounding Box", "[tvgPaint]")
{
auto shape = Shape::gen();
Expand Down Expand Up @@ -152,6 +163,7 @@ TEST_CASE("Duplication", "[tvgPaint]")
REQUIRE(shape->translate(200.0f, 100.0f) == Result::Success);
REQUIRE(shape->scale(2.2f) == Result::Success);
REQUIRE(shape->rotate(90.0f) == Result::Success);
REQUIRE(shape->name("test") == Result::Success);

auto comp = Shape::gen();
REQUIRE(comp);
Expand All @@ -164,6 +176,8 @@ TEST_CASE("Duplication", "[tvgPaint]")
//Compare properties
REQUIRE(dup->opacity() == 0);

REQUIRE(dup->name() == "test");

auto m = shape->transform();
REQUIRE(m.e11 == Approx(0.0f).margin(0.000001));
REQUIRE(m.e12 == Approx(-2.2f).margin(0.000001));
Expand Down

0 comments on commit 9b035f0

Please sign in to comment.