capi: revise some common paint interfaces and added picture stuff.
[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 /* Paint API                                                            */
136 /************************************************************************/
137
138 TVG_EXPORT Tvg_Result tvg_paint_del(Tvg_Paint* paint)
139 {
140     delete(paint);
141     return TVG_RESULT_SUCCESS;
142 }
143
144
145 TVG_EXPORT Tvg_Result tvg_paint_scale(Tvg_Paint* paint, float factor)
146 {
147     return (Tvg_Result) reinterpret_cast<Paint*>(paint)->scale(factor);
148 }
149
150
151 TVG_EXPORT Tvg_Result tvg_paint_rotate(Tvg_Paint* paint, float degree)
152 {
153     return (Tvg_Result) reinterpret_cast<Paint*>(paint)->rotate(degree);
154 }
155
156
157 TVG_EXPORT Tvg_Result tvg_paint_translate(Tvg_Paint* paint, float x, float y)
158 {
159     return (Tvg_Result) reinterpret_cast<Paint*>(paint)->translate(x, y);
160 }
161
162
163 TVG_EXPORT Tvg_Result tvg_paint_transform(Tvg_Paint* paint, const Tvg_Matrix* m)
164 {
165     return (Tvg_Result) reinterpret_cast<Paint*>(paint)->transform(*(reinterpret_cast<const Matrix*>(m)));
166 }
167
168
169 /************************************************************************/
170 /* Shape API                                                            */
171 /************************************************************************/
172
173 TVG_EXPORT Tvg_Paint* tvg_shape_new()
174 {
175     return (Tvg_Paint*) Shape::gen().release();
176 }
177
178
179 TVG_EXPORT Tvg_Result tvg_shape_reset(Tvg_Paint* paint)
180 {
181     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->reset();
182 }
183
184
185 TVG_EXPORT Tvg_Result tvg_shape_move_to(Tvg_Paint* paint, float x, float y)
186 {
187     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->moveTo(x, y);
188 }
189
190
191 TVG_EXPORT Tvg_Result tvg_shape_line_to(Tvg_Paint* paint, float x, float y)
192 {
193     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->lineTo(x, y);
194 }
195
196
197 TVG_EXPORT Tvg_Result tvg_shape_cubic_to(Tvg_Paint* paint, float cx1, float cy1, float cx2, float cy2, float x, float y)
198 {
199     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->cubicTo(cx1, cy1, cx2, cy2, x, y);
200 }
201
202
203 TVG_EXPORT Tvg_Result tvg_shape_close(Tvg_Paint* paint)
204 {
205     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->close();
206 }
207
208
209 TVG_EXPORT Tvg_Result tvg_shape_append_rect(Tvg_Paint* paint, float x, float y, float w, float h, float rx, float ry)
210 {
211     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->appendRect(x, y, w, h, rx, ry);
212 }
213
214 TVG_EXPORT Tvg_Result tvg_shape_append_arc(Tvg_Paint* paint, float cx, float cy, float radius, float startAngle, float sweep, uint8_t pie)
215 {
216     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->appendArc(cx, cy, radius, startAngle, sweep, pie);
217 }
218
219 TVG_EXPORT Tvg_Result tvg_shape_append_circle(Tvg_Paint* paint, float cx, float cy, float rx, float ry)
220 {
221     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->appendCircle(cx, cy, rx, ry);
222 }
223
224
225 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)
226 {
227     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->appendPath((PathCommand*)cmds, cmdCnt, (Point*)pts, ptsCnt);
228 }
229
230
231 TVG_EXPORT Tvg_Result tvg_shape_set_stroke_width(Tvg_Paint* paint, float width)
232 {
233     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->stroke(width);
234 }
235
236
237 TVG_EXPORT Tvg_Result tvg_shape_set_stroke_color(Tvg_Paint* paint, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
238 {
239     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->stroke(r, g, b, a);
240 }
241
242
243 TVG_EXPORT Tvg_Result tvg_shape_set_stroke_dash(Tvg_Paint* paint, const float* dashPattern, uint32_t cnt)
244 {
245     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->stroke(dashPattern, cnt);
246 }
247
248
249 TVG_EXPORT Tvg_Result tvg_shape_set_stroke_cap(Tvg_Paint* paint, Tvg_Stroke_Cap cap)
250 {
251     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->stroke((StrokeCap)cap);
252 }
253
254
255 TVG_EXPORT Tvg_Result tvg_shape_set_stroke_join(Tvg_Paint* paint, Tvg_Stroke_Join join)
256 {
257     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->stroke((StrokeJoin)join);
258 }
259
260
261 TVG_EXPORT Tvg_Result tvg_shape_fill_color(Tvg_Paint* paint, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
262 {
263     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->fill(r, g, b, a);
264 }
265
266
267 TVG_EXPORT Tvg_Result tvg_shape_linear_gradient_set(Tvg_Paint* paint, Tvg_Gradient *gradient)
268 {
269     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->fill(unique_ptr<LinearGradient>((LinearGradient*)(gradient)));
270 }
271
272
273 TVG_EXPORT Tvg_Result tvg_shape_radial_gradient_set(Tvg_Paint* paint, Tvg_Gradient *gradient)
274 {
275     return (Tvg_Result) reinterpret_cast<Shape*>(paint)->fill(unique_ptr<RadialGradient>((RadialGradient*)(gradient)));
276 }
277
278
279 /************************************************************************/
280 /* Picture API                                                          */
281 /************************************************************************/
282
283 TVG_EXPORT Tvg_Paint* tvg_picture_new()
284 {
285     return (Tvg_Paint*) Picture::gen().release();
286 }
287
288
289 TVG_EXPORT Tvg_Result tvg_picture_load(Tvg_Paint* paint, const char* path)
290 {
291     return (Tvg_Result) reinterpret_cast<Picture*>(paint)->load(path);
292 }
293
294
295 TVG_EXPORT Tvg_Result tvg_picture_get_viewbox(Tvg_Paint* paint, float* x, float* y, float* w, float* h)
296 {
297     return (Tvg_Result) reinterpret_cast<Picture*>(paint)->viewbox(x, y, w, h);
298 }
299
300
301 /************************************************************************/
302 /* Gradient API                                                         */
303 /************************************************************************/
304 TVG_EXPORT Tvg_Gradient* tvg_linear_gradient_new()
305 {
306     return (Tvg_Gradient*)LinearGradient::gen().release();
307 }
308
309 TVG_EXPORT Tvg_Gradient* tvg_radial_gradient_new()
310 {
311     return (Tvg_Gradient*)RadialGradient::gen().release();
312
313 }
314
315 TVG_EXPORT Tvg_Result tvg_gradient_del(Tvg_Gradient* grad)
316 {
317     delete(grad);
318     return TVG_RESULT_SUCCESS;
319 }
320
321 TVG_EXPORT Tvg_Result tvg_linear_gradient_set(Tvg_Gradient* grad, float x1, float y1, float x2, float y2)
322 {
323     return (Tvg_Result) reinterpret_cast<LinearGradient*>(grad)->linear(x1, y1, x2, y2);
324 }
325
326 TVG_EXPORT Tvg_Result tvg_radial_gradient_set(Tvg_Gradient* grad, float cx, float cy, float radius)
327 {
328     return (Tvg_Result) reinterpret_cast<RadialGradient*>(grad)->radial(cx, cy, radius);
329 }
330
331 TVG_EXPORT Tvg_Result tvg_gradient_color_stops(Tvg_Gradient* grad, const Tvg_Color_Stop* color_stop, uint32_t cnt)
332 {
333     return (Tvg_Result) reinterpret_cast<Fill*>(grad)->colorStops(reinterpret_cast<const Fill::ColorStop*>(color_stop), cnt);
334 }
335
336 #ifdef __cplusplus
337 }
338 #endif