capi: Added C wrappers for gradient func
[platform/core/graphics/tizenvg.git] / src / bindings / capi / tvgCapi.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *               http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  */
17
18 #include <thorvg.h>
19 #include "thorvg_capi.h"
20
21 using namespace std;
22 using namespace tvg;
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 struct _Tvg_Canvas
29 {
30     //Dummy for Direct Casting
31 };
32
33 struct _Tvg_Paint
34 {
35     //Dummy for Direct Casting
36 };
37
38 struct _Tvg_Gradient
39 {
40     //Dummy for Direct Casting
41 };
42
43
44 /************************************************************************/
45 /* Engine API                                                           */
46 /************************************************************************/
47
48 TVG_EXPORT Tvg_Result tvg_engine_init(unsigned engine_method) {
49     Result ret = Result::Success;
50
51     if (engine_method & TVG_ENGINE_SW) ret = tvg::Initializer::init(tvg::CanvasEngine::Sw);
52     if (ret != Result::Success) return (Tvg_Result) ret;
53
54     if (engine_method & TVG_ENGINE_GL) ret = tvg::Initializer::init(tvg::CanvasEngine::Gl);
55     return (Tvg_Result) ret;
56 }
57
58
59 TVG_EXPORT Tvg_Result tvg_engine_term(unsigned engine_method) {
60     Result ret = Result::Success;
61
62     if (engine_method & TVG_ENGINE_SW) ret = tvg::Initializer::init(tvg::CanvasEngine::Sw);
63     if (ret != Result::Success) return (Tvg_Result) ret;
64
65     if (engine_method & TVG_ENGINE_GL) ret = tvg::Initializer::init(tvg::CanvasEngine::Gl);
66     return (Tvg_Result) ret;
67 }
68
69 /************************************************************************/
70 /* Canvas API                                                           */
71 /************************************************************************/
72
73 TVG_EXPORT Tvg_Canvas* tvg_swcanvas_create()
74 {
75     return (Tvg_Canvas*) SwCanvas::gen().release();
76 }
77
78
79 TVG_EXPORT Tvg_Result tvg_canvas_destroy(Tvg_Canvas* canvas)
80 {
81     delete(canvas);
82     return TVG_RESULT_SUCCESS;
83 }
84
85
86 TVG_EXPORT Tvg_Result tvg_swcanvas_set_target(Tvg_Canvas* canvas, uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h)
87 {
88     return (Tvg_Result) reinterpret_cast<SwCanvas*>(canvas)->target(buffer, stride, w, h);
89 }
90
91
92 TVG_EXPORT Tvg_Result tvg_canvas_push(Tvg_Canvas* canvas, Tvg_Paint* paint)
93 {
94     return (Tvg_Result) reinterpret_cast<Canvas*>(canvas)->push(unique_ptr<Paint>((Paint*)paint));
95 }
96
97
98 TVG_EXPORT Tvg_Result tvg_canvas_reserve(Tvg_Canvas* canvas, uint32_t n)
99 {
100     return (Tvg_Result) reinterpret_cast<Canvas*>(canvas)->reserve(n);
101 }
102
103
104 TVG_EXPORT Tvg_Result tvg_canvas_clear(Tvg_Canvas* canvas)
105 {
106     return (Tvg_Result) reinterpret_cast<Canvas*>(canvas)->clear();
107 }
108
109
110 TVG_EXPORT Tvg_Result tvg_canvas_update(Tvg_Canvas* canvas)
111 {
112      return (Tvg_Result) reinterpret_cast<Canvas*>(canvas)->update(nullptr);
113 }
114
115
116 TVG_EXPORT Tvg_Result tvg_canvas_update_paint(Tvg_Canvas* canvas, Tvg_Paint* paint)
117 {
118     return (Tvg_Result) reinterpret_cast<Canvas*>(canvas)->update((Paint*) paint);
119 }
120
121
122 TVG_EXPORT Tvg_Result tvg_canvas_draw(Tvg_Canvas* canvas)
123 {
124     return (Tvg_Result) reinterpret_cast<Canvas*>(canvas)->draw();
125 }
126
127
128 TVG_EXPORT Tvg_Result tvg_canvas_sync(Tvg_Canvas* canvas)
129 {
130     return (Tvg_Result) reinterpret_cast<Canvas*>(canvas)->sync();
131 }
132
133
134 /************************************************************************/
135 /* Shape API                                                            */
136 /************************************************************************/
137
138 TVG_EXPORT Tvg_Paint* tvg_shape_new()
139 {
140     return (Tvg_Paint*) Shape::gen().release();
141 }
142
143
144 TVG_EXPORT Tvg_Result tvg_shape_del(Tvg_Paint* paint)
145 {
146     delete(paint);
147     return TVG_RESULT_SUCCESS;
148 }
149
150
151 TVG_EXPORT Tvg_Result tvg_shape_reset(Tvg_Paint* paint)
152 {
153     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->reset();
154 }
155
156
157 TVG_EXPORT Tvg_Result tvg_shape_move_to(Tvg_Paint* paint, float x, float y)
158 {
159     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->moveTo(x, y);
160 }
161
162
163 TVG_EXPORT Tvg_Result tvg_shape_line_to(Tvg_Paint* paint, float x, float y)
164 {
165     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->lineTo(x, y);
166 }
167
168
169 TVG_EXPORT Tvg_Result tvg_shape_cubic_to(Tvg_Paint* paint, float cx1, float cy1, float cx2, float cy2, float x, float y)
170 {
171     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->cubicTo(cx1, cy1, cx2, cy2, x, y);
172 }
173
174
175 TVG_EXPORT Tvg_Result tvg_shape_close(Tvg_Paint* paint)
176 {
177     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->close();
178 }
179
180
181 TVG_EXPORT Tvg_Result tvg_shape_append_rect(Tvg_Paint* paint, float x, float y, float w, float h, float rx, float ry)
182 {
183     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->appendRect(x, y, w, h, rx, ry);
184 }
185
186
187 TVG_EXPORT Tvg_Result tvg_shape_append_circle(Tvg_Paint* paint, float cx, float cy, float rx, float ry)
188 {
189     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->appendCircle(cx, cy, rx, ry);
190 }
191
192
193 TVG_EXPORT Tvg_Result tvg_shape_append_path(Tvg_Paint* paint, const Tvg_Path_Command* cmds, uint32_t cmdCnt, const Tvg_Point* pts, uint32_t ptsCnt)
194 {
195     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->appendPath((PathCommand*)cmds, cmdCnt, (Point*)pts, ptsCnt);
196 }
197
198
199 TVG_EXPORT Tvg_Result tvg_shape_set_stroke_width(Tvg_Paint* paint, float width)
200 {
201     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->stroke(width);
202 }
203
204
205 TVG_EXPORT Tvg_Result tvg_shape_set_stroke_color(Tvg_Paint* paint, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
206 {
207     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->stroke(r, g, b, a);
208 }
209
210
211 TVG_EXPORT Tvg_Result tvg_shape_set_stroke_dash(Tvg_Paint* paint, const float* dashPattern, uint32_t cnt)
212 {
213     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->stroke(dashPattern, cnt);
214 }
215
216
217 TVG_EXPORT Tvg_Result tvg_shape_set_stroke_cap(Tvg_Paint* paint, Tvg_Stroke_Cap cap)
218 {
219     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->stroke((StrokeCap)cap);
220 }
221
222
223 TVG_EXPORT Tvg_Result tvg_shape_set_stroke_join(Tvg_Paint* paint, Tvg_Stroke_Join join)
224 {
225     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->stroke((StrokeJoin)join);
226 }
227
228
229 TVG_EXPORT Tvg_Result tvg_shape_fill_color(Tvg_Paint* paint, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
230 {
231     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->fill(r, g, b, a);
232 }
233
234 TVG_EXPORT Tvg_Result tvg_shape_scale(Tvg_Paint* paint, float factor)
235 {
236     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->scale(factor);
237 }
238
239 TVG_EXPORT Tvg_Result tvg_shape_rotate(Tvg_Paint* paint, float degree)
240 {
241     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->rotate(degree);
242 }
243
244 TVG_EXPORT Tvg_Result tvg_shape_translate(Tvg_Paint* paint, float x, float y)
245 {
246     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->translate(x, y);
247 }
248
249 TVG_EXPORT Tvg_Result tvg_shape_transform(Tvg_Paint* paint, const Tvg_Matrix* m)
250 {
251     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->transform(*(reinterpret_cast<const Matrix*>(m)));
252 }
253
254 TVG_EXPORT Tvg_Result tvg_shape_linear_gradient_set(Tvg_Paint* paint, Tvg_Gradient *grad)
255 {
256     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->fill(unique_ptr<LinearGradient>((LinearGradient*)(grad)));
257 }
258
259 TVG_EXPORT Tvg_Result tvg_shape_radial_gradient_set(Tvg_Paint* paint, Tvg_Gradient *grad)
260 {
261     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->fill(unique_ptr<RadialGradient>((RadialGradient*)(grad)));
262 }
263
264
265 /************************************************************************/
266 /* Gradient API                                                         */
267 /************************************************************************/
268 TVG_EXPORT Tvg_Gradient* tvg_linear_gradient_new()
269 {
270     return (Tvg_Gradient*)LinearGradient::gen().release();
271 }
272
273 TVG_EXPORT Tvg_Gradient* tvg_radial_gradient_new()
274 {
275     return (Tvg_Gradient*)RadialGradient::gen().release();
276
277 }
278
279 TVG_EXPORT Tvg_Result tvg_gradient_del(Tvg_Gradient* grad)
280 {
281     delete(grad);
282     return TVG_RESULT_SUCCESS;
283 }
284
285 TVG_EXPORT Tvg_Result tvg_linear_gradient_set(Tvg_Gradient* grad, float x1, float y1, float x2, float y2)
286 {
287     return (Tvg_Result) reinterpret_cast<LinearGradient*>(grad)->linear(x1, y1, x2, y2);
288 }
289
290 TVG_EXPORT Tvg_Result tvg_radial_gradient_set(Tvg_Gradient* grad, float cx, float cy, float radius)
291 {
292     return (Tvg_Result) reinterpret_cast<RadialGradient*>(grad)->radial(cx, cy, radius);
293 }
294
295 TVG_EXPORT Tvg_Result tvg_gradient_color_stops(Tvg_Gradient* grad, const Tvg_Color_Stop* color_stop, uint32_t cnt)
296 {
297     return (Tvg_Result) reinterpret_cast<Fill*>(grad)->colorStops(reinterpret_cast<const Fill::ColorStop*>(color_stop), cnt);
298 }
299
300 #ifdef __cplusplus
301 }
302 #endif