From 7a27ca361380717e7b76a93a91540d2097910d6c Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Wed, 16 Sep 2020 17:53:24 +0900 Subject: [PATCH] capi: correct interfaces. put missing const parameter and correct set/get naming. attention, these two apis are changed! tvg_gradient_color_stops() => tvg_gradient_set_color_stops() tvg_gradient_spread() = tvg_gradient_set_spread() --- inc/thorvg_capi.h | 22 ++++++++++---------- src/bindings/capi/tvgCapi.cpp | 38 ++++++++++++++++++----------------- test/testCapi.c | 26 +++++++++++++----------- 3 files changed, 45 insertions(+), 41 deletions(-) diff --git a/inc/thorvg_capi.h b/inc/thorvg_capi.h index a869639e..8ec3f99b 100644 --- a/inc/thorvg_capi.h +++ b/inc/thorvg_capi.h @@ -138,31 +138,31 @@ TVG_EXPORT Tvg_Result tvg_shape_append_path(Tvg_Paint* paint, const Tvg_Path_Com TVG_EXPORT Tvg_Result tvg_shape_get_path_coords(const Tvg_Paint* paint, const Tvg_Point** pts, uint32_t* cnt); TVG_EXPORT Tvg_Result tvg_shape_get_path_commands(const Tvg_Paint* paint, const Tvg_Path_Command** cmds, uint32_t* cnt); TVG_EXPORT Tvg_Result tvg_shape_set_stroke_width(Tvg_Paint* paint, float width); -TVG_EXPORT Tvg_Result tvg_shape_get_stroke_width(Tvg_Paint* paint, float* width); +TVG_EXPORT Tvg_Result tvg_shape_get_stroke_width(const Tvg_Paint* paint, float* width); TVG_EXPORT Tvg_Result tvg_shape_set_stroke_color(Tvg_Paint* paint, uint8_t r, uint8_t g, uint8_t b, uint8_t a); -TVG_EXPORT Tvg_Result tvg_shape_get_stroke_color(Tvg_Paint* paint, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a); +TVG_EXPORT Tvg_Result tvg_shape_get_stroke_color(const Tvg_Paint* paint, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a); TVG_EXPORT Tvg_Result tvg_shape_set_stroke_dash(Tvg_Paint* paint, const float* dashPattern, uint32_t cnt); -TVG_EXPORT Tvg_Result tvg_shape_get_stroke_dash(Tvg_Paint* paint, const float** dashPattern, uint32_t* cnt); +TVG_EXPORT Tvg_Result tvg_shape_get_stroke_dash(const Tvg_Paint* paint, const float** dashPattern, uint32_t* cnt); TVG_EXPORT Tvg_Result tvg_shape_set_stroke_cap(Tvg_Paint* paint, Tvg_Stroke_Cap cap); -TVG_EXPORT Tvg_Result tvg_shape_get_stroke_cap(Tvg_Paint* paint, Tvg_Stroke_Cap* cap); +TVG_EXPORT Tvg_Result tvg_shape_get_stroke_cap(const Tvg_Paint* paint, Tvg_Stroke_Cap* cap); TVG_EXPORT Tvg_Result tvg_shape_set_stroke_join(Tvg_Paint* paint, Tvg_Stroke_Join join); -TVG_EXPORT Tvg_Result tvg_shape_get_stroke_join(Tvg_Paint* paint, Tvg_Stroke_Join* join); +TVG_EXPORT Tvg_Result tvg_shape_get_stroke_join(const Tvg_Paint* paint, Tvg_Stroke_Join* join); TVG_EXPORT Tvg_Result tvg_shape_set_fill_color(Tvg_Paint* paint, uint8_t r, uint8_t g, uint8_t b, uint8_t a); -TVG_EXPORT Tvg_Result tvg_shape_get_fill_color(Tvg_Paint* paint, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a); +TVG_EXPORT Tvg_Result tvg_shape_get_fill_color(const Tvg_Paint* paint, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a); TVG_EXPORT Tvg_Result tvg_shape_set_linear_gradient(Tvg_Paint* paint, Tvg_Gradient* grad); TVG_EXPORT Tvg_Result tvg_shape_set_radial_gradient(Tvg_Paint* paint, Tvg_Gradient* grad); -TVG_EXPORT Tvg_Result tvg_shape_get_gradient(Tvg_Paint* paint, Tvg_Gradient** grad); +TVG_EXPORT Tvg_Result tvg_shape_get_gradient(const Tvg_Paint* paint, Tvg_Gradient** grad); /************************************************************************/ /* Gradient API */ /************************************************************************/ TVG_EXPORT Tvg_Gradient* tvg_linear_gradient_new(); TVG_EXPORT Tvg_Gradient* tvg_radial_gradient_new(); -TVG_EXPORT Tvg_Result tvg_gradient_del(Tvg_Gradient* grad); TVG_EXPORT Tvg_Result tvg_linear_gradient_set(Tvg_Gradient* grad, float x1, float y1, float x2, float y2); TVG_EXPORT Tvg_Result tvg_radial_gradient_set(Tvg_Gradient* grad, float cx, float cy, float radius); -TVG_EXPORT Tvg_Result tvg_gradient_color_stops(Tvg_Gradient* grad, const Tvg_Color_Stop* color_stop, uint32_t cnt); -TVG_EXPORT Tvg_Result tvg_gradient_spread(Tvg_Gradient* grad, const Tvg_Stroke_Fill); +TVG_EXPORT Tvg_Result tvg_gradient_set_color_stops(Tvg_Gradient* grad, const Tvg_Color_Stop* color_stop, uint32_t cnt); +TVG_EXPORT Tvg_Result tvg_gradient_set_spread(Tvg_Gradient* grad, const Tvg_Stroke_Fill); +TVG_EXPORT Tvg_Result tvg_gradient_del(Tvg_Gradient* grad); /************************************************************************/ @@ -170,7 +170,7 @@ TVG_EXPORT Tvg_Result tvg_gradient_spread(Tvg_Gradient* grad, const Tvg_Stroke_F /************************************************************************/ TVG_EXPORT Tvg_Paint* tvg_picture_new(); TVG_EXPORT Tvg_Result tvg_picture_load(Tvg_Paint* paint, const char* path); -TVG_EXPORT Tvg_Result tvg_picture_get_viewbox(Tvg_Paint* paint, float* x, float* y, float* w, float* h); +TVG_EXPORT Tvg_Result tvg_picture_get_viewbox(const Tvg_Paint* paint, float* x, float* y, float* w, float* h); #ifdef __cplusplus } diff --git a/src/bindings/capi/tvgCapi.cpp b/src/bindings/capi/tvgCapi.cpp index 0178e27e..22402235 100644 --- a/src/bindings/capi/tvgCapi.cpp +++ b/src/bindings/capi/tvgCapi.cpp @@ -26,6 +26,8 @@ using namespace std; using namespace tvg; +#define CCP(A) const_cast(A) //Const-Cast-Paint + #ifdef __cplusplus extern "C" { #endif @@ -279,10 +281,10 @@ TVG_EXPORT Tvg_Result tvg_shape_set_stroke_width(Tvg_Paint* paint, float width) } -TVG_EXPORT Tvg_Result tvg_shape_get_stroke_width(Tvg_Paint* paint, float* width) +TVG_EXPORT Tvg_Result tvg_shape_get_stroke_width(const Tvg_Paint* paint, float* width) { if (!paint || !width) return TVG_RESULT_INVALID_ARGUMENT; - *width = reinterpret_cast(paint)->strokeWidth(); + *width = reinterpret_cast(CCP(paint))->strokeWidth(); return TVG_RESULT_SUCCESS; } @@ -294,10 +296,10 @@ TVG_EXPORT Tvg_Result tvg_shape_set_stroke_color(Tvg_Paint* paint, uint8_t r, ui } -TVG_EXPORT Tvg_Result tvg_shape_get_stroke_color(Tvg_Paint* paint, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) +TVG_EXPORT Tvg_Result tvg_shape_get_stroke_color(const Tvg_Paint* paint, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) { if (!paint) return TVG_RESULT_INVALID_ARGUMENT; - return (Tvg_Result) reinterpret_cast(paint)->strokeColor(r, g, b, a); + return (Tvg_Result) reinterpret_cast(CCP(paint))->strokeColor(r, g, b, a); } @@ -308,10 +310,10 @@ TVG_EXPORT Tvg_Result tvg_shape_set_stroke_dash(Tvg_Paint* paint, const float* d } -TVG_EXPORT Tvg_Result tvg_shape_get_stroke_dash(Tvg_Paint* paint, const float** dashPattern, uint32_t* cnt) +TVG_EXPORT Tvg_Result tvg_shape_get_stroke_dash(const Tvg_Paint* paint, const float** dashPattern, uint32_t* cnt) { if (!paint || !cnt || !dashPattern) return TVG_RESULT_INVALID_ARGUMENT; - *cnt = reinterpret_cast(paint)->strokeDash(dashPattern); + *cnt = reinterpret_cast(CCP(paint))->strokeDash(dashPattern); return TVG_RESULT_SUCCESS; } @@ -323,10 +325,10 @@ TVG_EXPORT Tvg_Result tvg_shape_set_stroke_cap(Tvg_Paint* paint, Tvg_Stroke_Cap } -TVG_EXPORT Tvg_Result tvg_shape_get_stroke_cap(Tvg_Paint* paint, Tvg_Stroke_Cap* cap) +TVG_EXPORT Tvg_Result tvg_shape_get_stroke_cap(const Tvg_Paint* paint, Tvg_Stroke_Cap* cap) { if (!paint || !cap) return TVG_RESULT_INVALID_ARGUMENT; - *cap = (Tvg_Stroke_Cap) reinterpret_cast(paint)->strokeCap(); + *cap = (Tvg_Stroke_Cap) reinterpret_cast(CCP(paint))->strokeCap(); return TVG_RESULT_SUCCESS; } @@ -337,10 +339,10 @@ TVG_EXPORT Tvg_Result tvg_shape_set_stroke_join(Tvg_Paint* paint, Tvg_Stroke_Joi } -TVG_EXPORT Tvg_Result tvg_shape_get_stroke_join(Tvg_Paint* paint, Tvg_Stroke_Join* join) +TVG_EXPORT Tvg_Result tvg_shape_get_stroke_join(const Tvg_Paint* paint, Tvg_Stroke_Join* join) { if (!paint || !join) return TVG_RESULT_INVALID_ARGUMENT; - *join = (Tvg_Stroke_Join) reinterpret_cast(paint)->strokeJoin(); + *join = (Tvg_Stroke_Join) reinterpret_cast(CCP(paint))->strokeJoin(); return TVG_RESULT_SUCCESS; } @@ -352,10 +354,10 @@ TVG_EXPORT Tvg_Result tvg_shape_set_fill_color(Tvg_Paint* paint, uint8_t r, uint } -TVG_EXPORT Tvg_Result tvg_shape_get_fill_color(Tvg_Paint* paint, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) +TVG_EXPORT Tvg_Result tvg_shape_get_fill_color(const Tvg_Paint* paint, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) { if (!paint) return TVG_RESULT_INVALID_ARGUMENT; - return (Tvg_Result) reinterpret_cast(paint)->fill(r, g, b, a); + return (Tvg_Result) reinterpret_cast(CCP(paint))->fill(r, g, b, a); } @@ -373,10 +375,10 @@ TVG_EXPORT Tvg_Result tvg_shape_set_radial_gradient(Tvg_Paint* paint, Tvg_Gradie } -TVG_EXPORT Tvg_Result tvg_shape_get_gradient(Tvg_Paint* paint, Tvg_Gradient** gradient) +TVG_EXPORT Tvg_Result tvg_shape_get_gradient(const Tvg_Paint* paint, Tvg_Gradient** gradient) { if (!paint || !gradient) return TVG_RESULT_INVALID_ARGUMENT; - *gradient = (Tvg_Gradient*)(reinterpret_cast(paint)->fill()); + *gradient = (Tvg_Gradient*)(reinterpret_cast(CCP(paint))->fill()); return TVG_RESULT_SUCCESS; } @@ -398,10 +400,10 @@ TVG_EXPORT Tvg_Result tvg_picture_load(Tvg_Paint* paint, const char* path) } -TVG_EXPORT Tvg_Result tvg_picture_get_viewbox(Tvg_Paint* paint, float* x, float* y, float* w, float* h) +TVG_EXPORT Tvg_Result tvg_picture_get_viewbox(const Tvg_Paint* paint, float* x, float* y, float* w, float* h) { if (!paint) return TVG_RESULT_INVALID_ARGUMENT; - return (Tvg_Result) reinterpret_cast(paint)->viewbox(x, y, w, h); + return (Tvg_Result) reinterpret_cast(CCP(paint))->viewbox(x, y, w, h); } @@ -438,13 +440,13 @@ TVG_EXPORT Tvg_Result tvg_radial_gradient_set(Tvg_Gradient* grad, float cx, floa return (Tvg_Result) reinterpret_cast(grad)->radial(cx, cy, radius); } -TVG_EXPORT Tvg_Result tvg_gradient_color_stops(Tvg_Gradient* grad, const Tvg_Color_Stop* color_stop, uint32_t cnt) +TVG_EXPORT Tvg_Result tvg_gradient_set_color_stops(Tvg_Gradient* grad, const Tvg_Color_Stop* color_stop, uint32_t cnt) { if (!grad) return TVG_RESULT_INVALID_ARGUMENT; return (Tvg_Result) reinterpret_cast(grad)->colorStops(reinterpret_cast(color_stop), cnt); } -TVG_EXPORT Tvg_Result tvg_gradient_spread(Tvg_Gradient* grad, const Tvg_Stroke_Fill spread) +TVG_EXPORT Tvg_Result tvg_gradient_set_spread(Tvg_Gradient* grad, const Tvg_Stroke_Fill spread) { if (!grad) return TVG_RESULT_INVALID_ARGUMENT; return (Tvg_Result) reinterpret_cast(grad)->spread((FillSpread)spread); diff --git a/test/testCapi.c b/test/testCapi.c index c1fb9c9e..ab405ffe 100644 --- a/test/testCapi.c +++ b/test/testCapi.c @@ -53,7 +53,7 @@ void testCapi() {.offset=1, .r=255, .g=0, .b=0, .a=255}, }; - tvg_gradient_spread(grad2, TVG_STROKE_FILL_REPEAT); + tvg_gradient_set_spread(grad2, TVG_STROKE_FILL_REPEAT); Tvg_Paint* shape3 = tvg_shape_new(); tvg_shape_append_rect(shape3, 0, 400, 400, 800, 20, 20); @@ -65,12 +65,12 @@ void testCapi() {.offset=1, .r=0, .g=255, .b=0, .a=255}, }; - tvg_gradient_spread(grad3, TVG_STROKE_FILL_REFLECT); + tvg_gradient_set_spread(grad3, TVG_STROKE_FILL_REFLECT); - tvg_gradient_color_stops(grad, color_stops, 4); - tvg_gradient_color_stops(grad1, color_stops1, 3); - tvg_gradient_color_stops(grad2, color_stops2, 2); - tvg_gradient_color_stops(grad3, color_stops3, 2); + tvg_gradient_set_color_stops(grad, color_stops, 4); + tvg_gradient_set_color_stops(grad1, color_stops1, 3); + tvg_gradient_set_color_stops(grad2, color_stops2, 2); + tvg_gradient_set_color_stops(grad3, color_stops3, 2); tvg_shape_set_linear_gradient(shape, grad); tvg_shape_set_radial_gradient(shape1, grad1); tvg_shape_set_linear_gradient(shape2, grad2); @@ -90,7 +90,7 @@ void testCapi() {.offset=0.0, .r=0, .g=0, .b=0, .a=255}, {.offset=1, .r=0, .g=255, .b=0, .a=255}, }; - tvg_gradient_color_stops(grad4, color_stops4, 2); + tvg_gradient_set_color_stops(grad4, color_stops4, 2); tvg_shape_set_linear_gradient(shape4, grad4); Tvg_Gradient* grad5 = tvg_linear_gradient_new(); @@ -100,7 +100,7 @@ void testCapi() {.offset=0.0, .r=0, .g=0, .b=255, .a=255}, {.offset=1, .r=0, .g=255, .b=255, .a=255}, }; - tvg_gradient_color_stops(grad5, color_stops5, 2); + tvg_gradient_set_color_stops(grad5, color_stops5, 2); tvg_shape_set_linear_gradient(shape4, grad5); tvg_canvas_push(canvas, shape4); @@ -111,7 +111,7 @@ void testCapi() {.offset=0.0, .r=0, .g=125, .b=0, .a=255}, {.offset=1, .r=125, .g=0, .b=125, .a=255}, }; - tvg_gradient_color_stops(grad6, color_stops6, 2); + tvg_gradient_set_color_stops(grad6, color_stops6, 2); tvg_shape_set_radial_gradient(shape1, grad6); tvg_canvas_update(canvas); @@ -126,14 +126,16 @@ void testCapi() tvg_shape_get_path_commands(shape, &cmds, &cmdCnt); printf("---- First Shape Commands(%u) ----\n", cmdCnt); - for(int i=0; i