Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

common Paint: Add name getter/setter API #1285

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions inc/thorvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,28 @@ class TVG_API 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_PRIVATE(Paint);
};

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 @@ -796,6 +796,7 @@ static unique_ptr<Scene> _sceneBuildHelper(SvgLoaderData& loaderData, const SvgN
} else if ((*child)->type == SvgNodeType::Image) {
auto image = _imageBuildHelper(loaderData, *child, vBox, svgPath);
if (image) {
if ((*child)->id) image->name(std::string((*child)->id));
scene->push(std::move(image));
if (isMaskWhite) *isMaskWhite = false;
}
Expand All @@ -810,13 +811,15 @@ static unique_ptr<Scene> _sceneBuildHelper(SvgLoaderData& loaderData, const SvgN
*isMaskWhite = false;
}
}
if ((*child)->id) shape->name(std::string((*child)->id));
scene->push(std::move(shape));
}
}
}
_applyComposition(loaderData, 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
17 changes: 16 additions & 1 deletion src/renderer/tvgPaint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,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 @@ -458,12 +459,26 @@ Result Paint::blend(BlendMethod method) const noexcept
pImpl->blendMethod = method;
pImpl->renderFlag |= RenderUpdateFlag::Blend;
}
return Result::Success;
}


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


BlendMethod Paint::blend() const noexcept
{
return pImpl->blendMethod;
}
}


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

3 changes: 2 additions & 1 deletion src/renderer/tvgPaint.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace tvg
uint8_t id;
uint8_t opacity = 255;
uint8_t refCnt = 0; //reference count
std::string name = "";

Impl(Paint* pnt) : paint(pnt) {}

Expand Down Expand Up @@ -143,4 +144,4 @@ namespace tvg
};
}

#endif //_TVG_PAINT_H_
#endif //_TVG_PAINT_H_
14 changes: 14 additions & 0 deletions test/testPaint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,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 @@ -166,6 +177,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 @@ -178,6 +190,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