From ee3293401b8077957c7cf412aa72637db8d019ae Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Tue, 30 Jul 2024 11:30:02 +0900 Subject: [PATCH] renderer/picture: added a method to search for a paint object This utility method allows access to a specific paint instance by its unique identifier. Experimental API: - const Paint* Picture::paint(uint32_t id) - const Tvg_Paint* tvg_picture_get_paint(Tvg_Paint* paint, uint32_t id) --- inc/thorvg.h | 15 +++++++++++++++ src/bindings/capi/thorvg_capi.h | 16 ++++++++++++++++ src/bindings/capi/tvgCapi.cpp | 7 +++++++ src/loaders/lottie/thorvg_lottie.h | 2 +- src/renderer/tvgPicture.cpp | 23 +++++++++++++++++++++++ 5 files changed, 62 insertions(+), 1 deletion(-) diff --git a/inc/thorvg.h b/inc/thorvg.h index 3e1bfe9d..c76dfe94 100644 --- a/inc/thorvg.h +++ b/inc/thorvg.h @@ -1339,6 +1339,21 @@ public: */ Result load(uint32_t* data, uint32_t w, uint32_t h, bool premultiplied, bool copy = false) noexcept; + /** + * @brief Retrieve a paint object from the Picture scene by its Unique ID. + * + * This function searches for a paint object within the Picture scene that matches the provided @p id. + * + * @param[in] id The Unique ID of the paint object. + * + * @return A pointer to the paint object that matches the given identifier, or @c nullptr if no matching paint object is found. + * + * @see Accessor::id() + * + * @note Experimental API + */ + const Paint* paint(uint32_t id) noexcept; + /** * @brief Sets or removes the triangle mesh to deform the image. * diff --git a/src/bindings/capi/thorvg_capi.h b/src/bindings/capi/thorvg_capi.h index 945c8d79..c07fa333 100644 --- a/src/bindings/capi/thorvg_capi.h +++ b/src/bindings/capi/thorvg_capi.h @@ -2071,6 +2071,22 @@ TVG_API Tvg_Result tvg_picture_set_size(Tvg_Paint* paint, float w, float h); TVG_API Tvg_Result tvg_picture_get_size(const Tvg_Paint* paint, float* w, float* h); +/*! +* \brief Retrieve a paint object from the Picture scene by its Unique ID. +* +* This function searches for a paint object within the Picture scene that matches the provided @p id. +* +* \param[in] paint A Tvg_Paint pointer to the picture object. +* \param[in] id The Unique ID of the paint object. + +* \return A pointer to the paint object that matches the given identifier, or @c nullptr if no matching paint object is found. +* +* \see tvg_accessor_generate_id() +* \note experimental API +*/ +TVG_API const Tvg_Paint* tvg_picture_get_paint(Tvg_Paint* paint, uint32_t id); + + /** \} */ // end defgroup ThorVGCapi_Picture diff --git a/src/bindings/capi/tvgCapi.cpp b/src/bindings/capi/tvgCapi.cpp index 91e7b9c7..8adf987c 100644 --- a/src/bindings/capi/tvgCapi.cpp +++ b/src/bindings/capi/tvgCapi.cpp @@ -582,6 +582,13 @@ TVG_API Tvg_Result tvg_picture_get_size(const Tvg_Paint* paint, float* w, float* } +TVG_API const Tvg_Paint* tvg_picture_get_paint(Tvg_Paint* paint, uint32_t id) +{ + if (!paint) return nullptr; + return (Tvg_Paint*) reinterpret_cast(paint)->paint(id); +} + + /************************************************************************/ /* Gradient API */ /************************************************************************/ diff --git a/src/loaders/lottie/thorvg_lottie.h b/src/loaders/lottie/thorvg_lottie.h index 3af73e95..cced6591 100644 --- a/src/loaders/lottie/thorvg_lottie.h +++ b/src/loaders/lottie/thorvg_lottie.h @@ -78,7 +78,7 @@ public: * @note Experimental API */ const char* marker(uint32_t idx) noexcept; - + /** * @brief Creates a new LottieAnimation object. * diff --git a/src/renderer/tvgPicture.cpp b/src/renderer/tvgPicture.cpp index 4b846dbe..c4820638 100644 --- a/src/renderer/tvgPicture.cpp +++ b/src/renderer/tvgPicture.cpp @@ -214,3 +214,26 @@ uint32_t Picture::mesh(const Polygon** triangles) const noexcept if (triangles) *triangles = pImpl->rm.triangles; return pImpl->rm.triangleCnt; } + + +const Paint* Picture::paint(uint32_t id) noexcept +{ + struct Value + { + uint32_t id; + const Paint* ret; + } value = {id, nullptr}; + + auto cb = [](const tvg::Paint* paint, void* data) -> bool + { + auto p = static_cast(data); + if (p->id == paint->id) { + p->ret = paint; + return false; + } + return true; + }; + + tvg::Accessor::gen()->set(this, cb, &value); + return value.ret; +} \ No newline at end of file