common shape: added arc implementation
[platform/core/graphics/tizenvg.git] / inc / thorvg.h
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 #ifndef _THORVG_H_
18 #define _THORVG_H_
19
20 #include <memory>
21
22 #ifdef TVG_BUILD
23     #define TVG_EXPORT __attribute__ ((visibility ("default")))
24 #else
25     #define TVG_EXPORT
26 #endif
27
28 #ifdef  LOG_TAG
29 #undef  LOG_TAG
30 #endif
31 #define LOG_TAG "TVG"
32
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38
39 #define _TVG_DECLARE_PRIVATE(A) \
40 protected: \
41     struct Impl; \
42     std::unique_ptr<Impl> pImpl; \
43     A(const A&) = delete; \
44     const A& operator=(const A&) = delete; \
45     A()
46
47 #define _TVG_DISABLE_CTOR(A) \
48     A() = delete; \
49     ~A() = delete
50
51 #define _TVG_DECLARE_ACCESSOR() \
52     friend Canvas; \
53     friend Scene; \
54     friend Picture
55
56 #define _TVG_DECALRE_IDENTIFIER() \
57     auto id() const { return _id; } \
58 protected: \
59     unsigned _id
60
61 namespace tvg
62 {
63
64 class RenderMethod;
65 class Scene;
66 class Picture;
67 class Canvas;
68
69
70 enum class TVG_EXPORT Result { Success = 0, InvalidArguments, InsufficientCondition, FailedAllocation, MemoryCorruption, NonSupport, Unknown };
71 enum class TVG_EXPORT PathCommand { Close = 0, MoveTo, LineTo, CubicTo };
72 enum class TVG_EXPORT StrokeCap { Square = 0, Round, Butt };
73 enum class TVG_EXPORT StrokeJoin { Bevel = 0, Round, Miter };
74 enum class TVG_EXPORT FillSpread { Pad = 0, Reflect, Repeat };
75 enum class TVG_EXPORT CanvasEngine { Sw = 0, Gl };
76
77
78 struct Point
79 {
80     float x, y;
81 };
82
83
84 struct Matrix
85 {
86     float e11, e12, e13;
87     float e21, e22, e23;
88     float e31, e32, e33;
89 };
90
91
92 /**
93  * @class Paint
94  *
95  * @ingroup ThorVG
96  *
97  * @brief description...
98  *
99  */
100 class TVG_EXPORT Paint
101 {
102 public:
103     virtual ~Paint();
104
105     Result rotate(float degree) noexcept;
106     Result scale(float factor) noexcept;
107     Result translate(float x, float y) noexcept;
108     Result transform(const Matrix& m) noexcept;
109     Result bounds(float* x, float* y, float* w, float* h) const noexcept;
110
111     _TVG_DECLARE_ACCESSOR();
112     _TVG_DECLARE_PRIVATE(Paint);
113 };
114
115
116 /**
117  * @class Fill
118  *
119  * @ingroup ThorVG
120  *
121  * @brief description...
122  *
123  */
124 class TVG_EXPORT Fill
125 {
126 public:
127     struct ColorStop
128     {
129         float offset;
130         uint8_t r, g, b, a;
131     };
132
133     virtual ~Fill();
134
135     Result colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept;
136     Result spread(FillSpread s) noexcept;
137
138     uint32_t colorStops(const ColorStop** colorStops) const noexcept;
139     FillSpread spread() const noexcept;
140
141     _TVG_DECALRE_IDENTIFIER();
142     _TVG_DECLARE_PRIVATE(Fill);
143 };
144
145
146 /**
147  * @class Canvas
148  *
149  * @ingroup ThorVG
150  *
151  * @brief description...
152  *
153  */
154 class TVG_EXPORT Canvas
155 {
156 public:
157     Canvas(RenderMethod*);
158     virtual ~Canvas();
159
160     Result reserve(uint32_t n) noexcept;
161     virtual Result push(std::unique_ptr<Paint> paint) noexcept;
162     virtual Result clear() noexcept;
163     virtual Result update(Paint* paint) noexcept;
164     virtual Result draw() noexcept;
165     virtual Result sync() noexcept;
166
167     _TVG_DECLARE_PRIVATE(Canvas);
168 };
169
170
171 /**
172  * @class LinearGradient
173  *
174  * @ingroup ThorVG
175  *
176  * @brief description...
177  *
178  */
179 class TVG_EXPORT LinearGradient final : public Fill
180 {
181 public:
182     ~LinearGradient();
183
184     Result linear(float x1, float y1, float x2, float y2) noexcept;
185     Result linear(float* x1, float* y1, float* x2, float* y2) const noexcept;
186
187     static std::unique_ptr<LinearGradient> gen() noexcept;
188
189     _TVG_DECLARE_PRIVATE(LinearGradient);
190 };
191
192
193 /**
194  * @class RadialGradient
195  *
196  * @ingroup ThorVG
197  *
198  * @brief description...
199  *
200  */
201 class TVG_EXPORT RadialGradient final : public Fill
202 {
203 public:
204     ~RadialGradient();
205
206     Result radial(float cx, float cy, float radius) noexcept;
207     Result radial(float* cx, float* cy, float* radius) const noexcept;
208
209     static std::unique_ptr<RadialGradient> gen() noexcept;
210
211     _TVG_DECLARE_PRIVATE(RadialGradient);
212 };
213
214
215
216 /**
217  * @class Shape
218  *
219  * @ingroup ThorVG
220  *
221  * @brief description...
222  *
223  */
224 class TVG_EXPORT Shape final : public Paint
225 {
226 public:
227     ~Shape();
228
229     Result reset() noexcept;
230
231     //Path
232     Result moveTo(float x, float y) noexcept;
233     Result lineTo(float x, float y) noexcept;
234     Result cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept;
235     Result close() noexcept;
236
237     //Shape
238     Result appendRect(float x, float y, float w, float h, float rx, float ry) noexcept;
239     Result appendCircle(float cx, float cy, float rx, float ry) noexcept;
240     Result appendArc(float x, float y, float w, float h, float startAngle, float sweep) noexcept;
241     Result appendPath(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept;
242
243     //Stroke
244     Result stroke(float width) noexcept;
245     Result stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
246     Result stroke(const float* dashPattern, uint32_t cnt) noexcept;
247     Result stroke(StrokeCap cap) noexcept;
248     Result stroke(StrokeJoin join) noexcept;
249
250     //Fill
251     Result fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
252     Result fill(std::unique_ptr<Fill> f) noexcept;
253
254     //Getters
255     uint32_t pathCommands(const PathCommand** cmds) const noexcept;
256     uint32_t pathCoords(const Point** pts) const noexcept;
257     Result fill(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
258     const Fill* fill() const noexcept;
259
260     float strokeWidth() const noexcept;
261     Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
262     uint32_t strokeDash(const float** dashPattern) const noexcept;
263     StrokeCap strokeCap() const noexcept;
264     StrokeJoin strokeJoin() const noexcept;
265
266     static std::unique_ptr<Shape> gen() noexcept;
267
268     _TVG_DECLARE_PRIVATE(Shape);
269 };
270
271
272 /**
273  * @class Picture
274  *
275  * @ingroup ThorVG
276  *
277  * @brief description...
278  *
279  */
280 class TVG_EXPORT Picture final : public Paint
281 {
282 public:
283     ~Picture();
284
285     Result load(const std::string& path) noexcept;
286     Result viewbox(float* x, float* y, float* w, float* h) const noexcept;
287
288     static std::unique_ptr<Picture> gen() noexcept;
289
290     _TVG_DECLARE_PRIVATE(Picture);
291 };
292
293
294 /**
295  * @class Scene
296  *
297  * @ingroup ThorVG
298  *
299  * @brief description...
300  *
301  */
302 class TVG_EXPORT Scene final : public Paint
303 {
304 public:
305     ~Scene();
306
307     Result push(std::unique_ptr<Paint> paint) noexcept;
308     Result reserve(uint32_t size) noexcept;
309
310     static std::unique_ptr<Scene> gen() noexcept;
311
312     _TVG_DECLARE_PRIVATE(Scene);
313 };
314
315
316 /**
317  * @class SwCanvas
318  *
319  * @ingroup ThorVG
320  *
321   @brief description...
322  *
323  */
324 class TVG_EXPORT SwCanvas final : public Canvas
325 {
326 public:
327     ~SwCanvas();
328
329     Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h) noexcept;
330
331     static std::unique_ptr<SwCanvas> gen() noexcept;
332
333     _TVG_DECLARE_PRIVATE(SwCanvas);
334 };
335
336
337 /**
338  * @class GlCanvas
339  *
340  * @ingroup ThorVG
341  *
342  * @brief description...
343  *
344  */
345 class TVG_EXPORT GlCanvas final : public Canvas
346 {
347 public:
348     ~GlCanvas();
349
350     //TODO: Gl Specific methods. Need gl backend configuration methods as well.
351     Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h) noexcept;
352
353     static std::unique_ptr<GlCanvas> gen() noexcept;
354
355     _TVG_DECLARE_PRIVATE(GlCanvas);
356 };
357
358
359 /**
360  * @class Engine
361  *
362  * @ingroup ThorVG
363  *
364  * @brief description...
365  *
366  */
367 class TVG_EXPORT Initializer final
368 {
369 public:
370
371     /**
372      * @brief ...
373      *
374      * @param[in] arg ...
375      *
376      * @note ...
377      *
378      * @return ...
379      *
380      * @see ...
381      */
382     static Result init(CanvasEngine engine) noexcept;
383     static Result term(CanvasEngine engine) noexcept;
384
385     _TVG_DISABLE_CTOR(Initializer);
386 };
387
388 } //namespace
389
390 #ifdef __cplusplus
391 }
392 #endif
393
394 #endif //_THORVG_H_