From bc5d90c1574aa3dfbbbd4346e922af186778818b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Drago=C8=99=20Tiselice?= Date: Tue, 25 Mar 2025 15:18:02 +0200 Subject: [PATCH] capi: exposed Accessor apis This patch adds basic Accessor API to the C bindings --- src/bindings/capi/thorvg_capi.h | 55 ++++++++++++++++++++++++++++++--- src/bindings/capi/tvgCapi.cpp | 22 +++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/src/bindings/capi/thorvg_capi.h b/src/bindings/capi/thorvg_capi.h index fe3e798f..712b0bbe 100644 --- a/src/bindings/capi/thorvg_capi.h +++ b/src/bindings/capi/thorvg_capi.h @@ -101,6 +101,11 @@ typedef struct _Tvg_Saver Tvg_Saver; */ typedef struct _Tvg_Animation Tvg_Animation; +/** +* @brief A structure representing an object that enables iterating through a scene's descendents. +*/ +typedef struct _Tvg_Accessor Tvg_Accessor; + /** * @brief Enumeration specifying the engine type used for the graphics backend. For multiple backends bitwise operation is allowed. @@ -666,7 +671,7 @@ TVG_API Tvg_Result tvg_canvas_update_paint(Tvg_Canvas* canvas, Tvg_Paint* paint) * @return Tvg_Result enumeration. * @retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Canvas pointer. * -* @note Clearing the buffer is unnecessary if the canvas will be fully covered +* @note Clearing the buffer is unnecessary if the canvas will be fully covered * with opaque content, which can improve performance. * @note Drawing may be asynchronous if the thread count is greater than zero. * To ensure drawing is complete, call tvg_canvas_sync() afterwards. @@ -925,7 +930,7 @@ TVG_API Tvg_Paint* tvg_paint_duplicate(Tvg_Paint* paint); * @return Tvg_Result enumeration. * @retval TVG_RESULT_INVALID_ARGUMENT An invalid @p paint. * @retval TVG_RESULT_INSUFFICIENT_CONDITION If it failed to compute the bounding box (mostly due to invalid path information). - * + * * @see tvg_paint_get_obb() * @see tvg_canvas_update_paint() */ @@ -934,7 +939,7 @@ TVG_API Tvg_Result tvg_paint_get_aabb(const Tvg_Paint* paint, float* x, float* y /** * @brief Retrieves the object-oriented bounding box (OBB) of the paint object in canvas space. - * + * * This function returns the bounding box of the paint, as an oriented bounding box (OBB) after transformations are applied. * * @param[in] paint The Tvg_Paint object of which to get the bounds. @@ -943,7 +948,7 @@ TVG_API Tvg_Result tvg_paint_get_aabb(const Tvg_Paint* paint, float* x, float* y * @return Tvg_Result enumeration. * @retval TVG_RESULT_INVALID_ARGUMENT @p paint or @p pt4 is invalid. * @retval TVG_RESULT_INSUFFICIENT_CONDITION If it failed to compute the bounding box (mostly due to invalid path information). - * + * * @see tvg_paint_get_aabb() * @see tvg_canvas_update_paint() * @@ -2448,6 +2453,48 @@ TVG_API Tvg_Result tvg_animation_del(Tvg_Animation* animation); /************************************************************************/ /* Accessor API */ /************************************************************************/ +/*! +* @brief Creates a new accessor object. +* +* @return A new accessor object. +* +* @note Experimental API +*/ +TVG_API Tvg_Accessor* tvg_accessor_new(); + + +/*! +* @brief Deletes the given accessor object. +* +* @param[in] accessor The accessor object to be deleted. +* +* @return Tvg_Result enumeration. +* @retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Accessor pointer. +* +* @note Experimental API +*/ +TVG_API Tvg_Result tvg_accessor_del(Tvg_Accessor* accessor); + + +/*! +* @brief Sets the paint of the accessor then iterates through its descendents. +* +* Iterates through all descendents of the scene passed through the paint argument +* while calling func on each and passing the data pointer to this function. When +* func returns false iteration stops and the function returns. +* +* @param[in] accessor A Tvg_Accessor pointer to the accessor object. +* @param[in] paint A Tvg_Paint pointer to the scene object. +* @param[in] func A function pointer to the function that will be execute for each child. +* @param[in] data A void pointer to data that will be passed to the func. +* +* @return Tvg_Result enumeration. +* @retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Accessor, Tvg_Paint, or function pointer. +* +* @note Experimental API +*/ +TVG_API Tvg_Result tvg_accessor_set_paint(Tvg_Accessor* accessor, Tvg_Paint* paint, bool (*func)(Tvg_Paint* paint, void* data), void* data); + /*! * @brief Generate a unique ID (hash key) from a given name. diff --git a/src/bindings/capi/tvgCapi.cpp b/src/bindings/capi/tvgCapi.cpp index a927c34b..cd1c7905 100644 --- a/src/bindings/capi/tvgCapi.cpp +++ b/src/bindings/capi/tvgCapi.cpp @@ -903,6 +903,28 @@ TVG_API Tvg_Result tvg_animation_del(Tvg_Animation* animation) /* Accessor API */ /************************************************************************/ +TVG_API Tvg_Accessor* tvg_accessor_new() +{ + return (Tvg_Accessor*) Accessor::gen(); +} + + +TVG_API Tvg_Result tvg_accessor_del(Tvg_Accessor* accessor) +{ + if (!accessor) return TVG_RESULT_INVALID_ARGUMENT; + delete(reinterpret_cast(accessor)); + return TVG_RESULT_SUCCESS; +} + + +TVG_API Tvg_Result tvg_accessor_set(Tvg_Accessor* accessor, Tvg_Paint* paint, bool (*func)(Tvg_Paint* paint, void* data), void* data) +{ + if (!accessor) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(accessor)->set(static_cast(reinterpret_cast(paint)), + [func](const Paint* paint, void* data) { return func((Tvg_Paint*) paint, data); }, data); +} + + TVG_API uint32_t tvg_accessor_generate_id(const char* name) { return Accessor::id(name);