common paint: keep clean apis and small size.
[platform/core/graphics/tizenvg.git] / inc / thorvg.h
1 /*!
2  * @file thorvg.h
3  *
4  * The main APIs enabling the TVG initialization, preparation of the canvas and provisioning of its content:
5  * - drawing shapes: line, arc, curve, path, polygon...
6  * - drawing pictures: tvg, svg, png, jpg, bitmap...
7  * - drawing fillings: solid, linear and radial gradient...
8  * - drawing stroking: continuous stroking with arbitrary width, join, cap, dash styles.
9  * - drawing composition: blending, masking, path clipping...
10  * - drawing scene graph & affine transformation (translation, rotation, scale, ...)
11  * and finally drawing the canvas and TVG termination.
12  */
13
14
15 #ifndef _THORVG_H_
16 #define _THORVG_H_
17
18 #include <functional>
19 #include <memory>
20 #include <string>
21
22 #ifdef TVG_BUILD
23     #if defined(_WIN32) && !defined(__clang__)
24         #define TVG_EXPORT __declspec(dllexport)
25         #define TVG_DEPRECATED __declspec(deprecated)
26     #else
27         #define TVG_EXPORT __attribute__ ((visibility ("default")))
28         #define TVG_DEPRECATED __attribute__ ((__deprecated__))
29     #endif
30 #else
31     #define TVG_EXPORT
32     #define TVG_DEPRECATED
33 #endif
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 #define _TVG_DECLARE_PRIVATE(A) \
40 protected: \
41     struct Impl; \
42     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     friend Accessor; \
56     friend IteratorAccessor
57
58
59 namespace tvg
60 {
61
62 class RenderMethod;
63 class IteratorAccessor;
64 class Scene;
65 class Picture;
66 class Canvas;
67 class Accessor;
68
69 /**
70  * @defgroup ThorVG ThorVG
71  * @brief ThorVG classes and enumerations providing C++ APIs.
72  */
73
74 /**@{*/
75
76 /**
77  * @brief Enumeration specifying the result from the APIs.
78  */
79 enum class Result
80 {
81     Success = 0,           ///< The value returned in case of a correct request execution.
82     InvalidArguments,      ///< The value returned in the event of a problem with the arguments given to the API - e.g. empty paths or null pointers.
83     InsufficientCondition, ///< The value returned in case the request cannot be processed - e.g. asking for properties of an object, which does not exist.
84     FailedAllocation,      ///< The value returned in case of unsuccessful memory allocation.
85     MemoryCorruption,      ///< The value returned in the event of bad memory handling - e.g. failing in pointer releasing or casting
86     NonSupport,            ///< The value returned in case of choosing unsupported options.
87     Unknown                ///< The value returned in all other cases.
88 };
89
90 /**
91  * @brief Enumeration specifying the values of the path commands accepted by TVG.
92  *
93  * Not to be confused with the path commands from the svg path element (like M, L, Q, H and many others).
94  * TVG interprets all of them and translates to the ones from the PathCommand values.
95  */
96 enum class PathCommand
97 {
98     Close = 0, ///< Ends the current sub-path and connects it with its initial point. This command doesn't expect any points.
99     MoveTo,    ///< Sets a new initial point of the sub-path and a new current point. This command expects 1 point: the starting position.
100     LineTo,    ///< Draws a line from the current point to the given point and sets a new value of the current point. This command expects 1 point: the end-position of the line.
101     CubicTo    ///< Draws a cubic Bezier curve from the current point to the given point using two given control points and sets a new value of the current point. This command expects 3 points: the 1st control-point, the 2nd control-point, the end-point of the curve.
102 };
103
104 /**
105  * @brief Enumeration determining the ending type of a stroke in the open sub-paths.
106  */
107 enum class StrokeCap
108 {
109     Square = 0, ///< The stroke is extended in both end-points of a sub-path by a rectangle, with the width equal to the stroke width and the length equal to the half of the stroke width. For zero length sub-paths the square is rendered with the size of the stroke width.
110     Round,      ///< The stroke is extended in both end-points of a sub-path by a half circle, with a radius equal to the half of a stroke width. For zero length sub-paths a full circle is rendered.
111     Butt        ///< The stroke ends exactly at each of the two end-points of a sub-path. For zero length sub-paths no stroke is rendered.
112 };
113
114 /**
115  * @brief Enumeration determining the style used at the corners of joined stroked path segments.
116  */
117 enum class StrokeJoin
118 {
119     Bevel = 0, ///< The outer corner of the joined path segments is bevelled at the join point. The triangular region of the corner is enclosed by a straight line between the outer corners of each stroke.
120     Round,     ///< The outer corner of the joined path segments is rounded. The circular region is centered at the join point.
121     Miter      ///< The outer corner of the joined path segments is spiked. The spike is created by extension beyond the join point of the outer edges of the stroke until they intersect. In case the extension goes beyond the limit, the join style is converted to the Bevel style.
122 };
123
124 /**
125  * @brief Enumeration specifying how to fill the area outside the gradient bounds.
126  */
127 enum class FillSpread
128 {
129     Pad = 0, ///< The remaining area is filled with the closest stop color.
130     Reflect, ///< The gradient pattern is reflected outside the gradient area until the expected region is filled.
131     Repeat   ///< The gradient pattern is repeated continuously beyond the gradient area until the expected region is filled.
132 };
133
134 /**
135  * @brief Enumeration specifying the algorithm used to establish which parts of the shape are treated as the inside of the shape.
136  */
137 enum class FillRule
138 {
139     Winding = 0, ///< A line from the point to a location outside the shape is drawn. The intersections of the line with the path segment of the shape are counted. Starting from zero, if the path segment of the shape crosses the line clockwise, one is added, otherwise one is subtracted. If the resulting sum is non zero, the point is inside the shape.
140     EvenOdd      ///< A line from the point to a location outside the shape is drawn and its intersections with the path segments of the shape are counted. If the number of intersections is an odd number, the point is inside the shape.
141 };
142
143 /**
144  * @brief Enumeration indicating the method used in the composition of two objects - the target and the source.
145  */
146 enum class CompositeMethod
147 {
148     None = 0,     ///< No composition is applied.
149     ClipPath,     ///< The intersection of the source and the target is determined and only the resulting pixels from the source are rendered.
150     AlphaMask,    ///< The pixels of the source and the target are alpha blended. As a result, only the part of the source, which alpha intersects with the target is visible.
151     InvAlphaMask, ///< The pixels of the source and the complement to the target's pixels are alpha blended. As a result, only the part of the source which alpha is not covered by the target is visible.
152     LumaMask      ///< @BETA_API The source pixels are converted to the grayscale (luma value) and alpha blended with the target. As a result, only the part of the source, which intersects with the target is visible.
153 };
154
155 /**
156  * @brief Enumeration specifying the engine type used for the graphics backend. For multiple backends bitwise operation is allowed.
157  */
158 enum class CanvasEngine
159 {
160     Sw = (1 << 1), ///< CPU rasterizer.
161     Gl = (1 << 2)  ///< OpenGL rasterizer.
162 };
163
164
165 /**
166  * @brief A data structure representing a point in two-dimensional space.
167  */
168 struct Point
169 {
170     float x, y;
171 };
172
173
174 /**
175  * @brief A data structure representing a three-dimensional matrix.
176  *
177  * The elements e11, e12, e21 and e22 represent the rotation matrix, including the scaling factor.
178  * The elements e13 and e23 determine the translation of the object along the x and y-axis, respectively.
179  * The elements e31 and e32 are set to 0, e33 is set to 1.
180  */
181 struct Matrix
182 {
183     float e11, e12, e13;
184     float e21, e22, e23;
185     float e31, e32, e33;
186 };
187
188 /**
189  * @brief A data structure representing a texture mesh vertex
190  *
191  * @param pt The vertex coordinate
192  * @param uv The normalized texture coordinate in the range (0.0..1.0, 0.0..1.0)
193  *
194  * @BETA_API
195  */
196 struct Vertex
197 {
198    Point pt;
199    Point uv;
200 };
201
202
203 /**
204  * @brief A data structure representing a triange in a texture mesh
205  *
206  * @param vertex The three vertices that make up the polygon
207  *
208  * @BETA_API
209  */
210 struct Polygon
211 {
212    Vertex vertex[3];
213 };
214
215
216 /**
217  * @class Paint
218  *
219  * @brief An abstract class for managing graphical elements.
220  *
221  * A graphical element in TVG is any object composed into a Canvas.
222  * Paint represents such a graphical object and its behaviors such as duplication, transformation and composition.
223  * TVG recommends the user to regard a paint as a set of volatile commands. They can prepare a Paint and then request a Canvas to run them.
224  */
225 class TVG_EXPORT Paint
226 {
227 public:
228     virtual ~Paint();
229
230     /**
231      * @brief Sets the angle by which the object is rotated.
232      *
233      * The angle in measured clockwise from the horizontal axis.
234      * The rotational axis passes through the point on the object with zero coordinates.
235      *
236      * @param[in] degree The value of the angle in degrees.
237      *
238      * @return Result::Success when succeed, Result::FailedAllocation otherwise.
239      */
240     Result rotate(float degree) noexcept;
241
242     /**
243      * @brief Sets the scale value of the object.
244      *
245      * @param[in] factor The value of the scaling factor. The default value is 1.
246      *
247      * @return Result::Success when succeed, Result::FailedAllocation otherwise.
248      */
249     Result scale(float factor) noexcept;
250
251     /**
252      * @brief Sets the values by which the object is moved in a two-dimensional space.
253      *
254      * The origin of the coordinate system is in the upper left corner of the canvas.
255      * The horizontal and vertical axes point to the right and down, respectively.
256      *
257      * @param[in] x The value of the horizontal shift.
258      * @param[in] y The value of the vertical shift.
259      *
260      * @return Result::Success when succeed, Result::FailedAllocation otherwise.
261      */
262     Result translate(float x, float y) noexcept;
263
264     /**
265      * @brief Sets the matrix of the affine transformation for the object.
266      *
267      * The augmented matrix of the transformation is expected to be given.
268      *
269      * @param[in] m The 3x3 augmented matrix.
270      *
271      * @return Result::Success when succeed, Result::FailedAllocation otherwise.
272      */
273     Result transform(const Matrix& m) noexcept;
274
275     /**
276      * @brief Gets the matrix of the affine transformation of the object.
277      *
278      * The values of the matrix can be set by the transform() API, as well by the translate(),
279      * scale() and rotate(). In case no transformation was applied, the identity matrix is returned.
280      *
281      * @retval The augmented transformation matrix.
282      *
283      * @since 0.4
284      */
285     Matrix transform() noexcept;
286
287     /**
288      * @brief Sets the opacity of the object.
289      *
290      * @param[in] o The opacity value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque.
291      *
292      * @return Result::Success when succeed.
293      *
294      * @note Setting the opacity with this API may require multiple render pass for composition. It is recommended to avoid changing the opacity if possible.
295      * @note ClipPath won't use the opacity value. (see: enum class CompositeMethod::ClipPath)
296      */
297     Result opacity(uint8_t o) noexcept;
298
299     /**
300      * @brief Sets the composition target object and the composition method.
301      *
302      * @param[in] target The paint of the target object.
303      * @param[in] method The method used to composite the source object with the target.
304      *
305      * @return Result::Success when succeed, Result::InvalidArguments otherwise.
306      */
307     Result composite(std::unique_ptr<Paint> target, CompositeMethod method) noexcept;
308
309     /**
310      * @brief Gets the bounding box of the paint object before any transformation.
311      *
312      * @param[out] x The x coordinate of the upper left corner of the object.
313      * @param[out] y The y coordinate of the upper left corner of the object.
314      * @param[out] w The width of the object.
315      * @param[out] h The height of the object.
316      *
317      * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
318      *
319      * @note The bounding box doesn't indicate the final rendered region. It's the smallest rectangle that encloses the object.
320      * @see Paint::bounds(float* x, float* y, float* w, float* h, bool transformed);
321      */
322     TVG_DEPRECATED Result bounds(float* x, float* y, float* w, float* h) const noexcept;
323
324     /**
325      * @brief Gets the axis-aligned bounding box of the paint object.
326      *
327      * In case @p transform is @c true, all object's transformations are applied first, and then the bounding box is established. Otherwise, the bounding box is determined before any transformations.
328      *
329      * @param[out] x The x coordinate of the upper left corner of the object.
330      * @param[out] y The y coordinate of the upper left corner of the object.
331      * @param[out] w The width of the object.
332      * @param[out] h The height of the object.
333      * @param[in] transformed If @c true, the paint's transformations are taken into account, otherwise they aren't.
334      *
335      * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
336      *
337      * @note The bounding box doesn't indicate the actual drawing region. It's the smallest rectangle that encloses the object.
338      */
339     Result bounds(float* x, float* y, float* w, float* h, bool transformed) const noexcept;
340
341     /**
342      * @brief Duplicates the object.
343      *
344      * Creates a new object and sets its all properties as in the original object.
345      *
346      * @return The created object when succeed, @c nullptr otherwise.
347      */
348     Paint* duplicate() const noexcept;
349
350     /**
351      * @brief Gets the opacity value of the object.
352      *
353      * @return The opacity value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque.
354      */
355     uint8_t opacity() const noexcept;
356
357     /**
358      * @brief Gets the composition target object and the composition method.
359      *
360      * @param[out] target The paint of the target object.
361      *
362      * @return The method used to composite the source object with the target.
363      *
364      * @since 0.5
365      */
366     CompositeMethod composite(const Paint** target) const noexcept;
367
368     /**
369      * @brief Return the unique id value of the paint instance.
370      *
371      * This method can be called for checking the current concrete instance type.
372      *
373      * @return The type id of the Paint instance.
374      */
375     uint32_t identifier() const noexcept;
376
377     _TVG_DECLARE_ACCESSOR();
378     _TVG_DECLARE_PRIVATE(Paint);
379 };
380
381
382 /**
383  * @class Fill
384  *
385  * @brief An abstract class representing the gradient fill of the Shape object.
386  *
387  * It contains the information about the gradient colors and their arrangement
388  * inside the gradient bounds. The gradients bounds are defined in the LinearGradient
389  * or RadialGradient class, depending on the type of the gradient to be used.
390  * It specifies the gradient behavior in case the area defined by the gradient bounds
391  * is smaller than the area to be filled.
392  */
393 class TVG_EXPORT Fill
394 {
395 public:
396     /**
397      * @brief A data structure storing the information about the color and its relative position inside the gradient bounds.
398      */
399     struct ColorStop
400     {
401         float offset; /**< The relative position of the color. */
402         uint8_t r;    /**< The red color channel value in the range [0 ~ 255]. */
403         uint8_t g;    /**< The green color channel value in the range [0 ~ 255]. */
404         uint8_t b;    /**< The blue color channel value in the range [0 ~ 255]. */
405         uint8_t a;    /**< The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. */
406     };
407
408     virtual ~Fill();
409
410     /**
411      * @brief Sets the parameters of the colors of the gradient and their position.
412      *
413      * @param[in] colorStops An array of ColorStop data structure.
414      * @param[in] cnt The count of the @p colorStops array equal to the colors number used in the gradient.
415      *
416      * @return Result::Success when succeed.
417      */
418     Result colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept;
419
420     /**
421      * @brief Sets the FillSpread value, which specifies how to fill the area outside the gradient bounds.
422      *
423      * @param[in] s The FillSpread value.
424      *
425      * @return Result::Success when succeed.
426      */
427     Result spread(FillSpread s) noexcept;
428
429     /**
430      * @brief Sets the matrix of the affine transformation for the gradient fill.
431      *
432      * The augmented matrix of the transformation is expected to be given.
433      *
434      * @param[in] m The 3x3 augmented matrix.
435      *
436      * @return Result::Success when succeed, Result::FailedAllocation otherwise.
437      */
438     Result transform(const Matrix& m) noexcept;
439
440     /**
441      * @brief Gets the parameters of the colors of the gradient, their position and number.
442      *
443      * @param[out] colorStops A pointer to the memory location, where the array of the gradient's ColorStop is stored.
444      *
445      * @return The number of colors used in the gradient. This value corresponds to the length of the @p colorStops array.
446      */
447     uint32_t colorStops(const ColorStop** colorStops) const noexcept;
448
449     /**
450      * @brief Gets the FillSpread value of the fill.
451      *
452      * @return The FillSpread value of this Fill.
453      */
454     FillSpread spread() const noexcept;
455
456     /**
457      * @brief Gets the matrix of the affine transformation of the gradient fill.
458      *
459      * In case no transformation was applied, the identity matrix is returned.
460      *
461      * @retval The augmented transformation matrix.
462      */
463     Matrix transform() const noexcept;
464
465     /**
466      * @brief Creates a copy of the Fill object.
467      *
468      * Return a newly created Fill object with the properties copied from the original.
469      *
470      * @return A copied Fill object when succeed, @c nullptr otherwise.
471      */
472     Fill* duplicate() const noexcept;
473
474     /**
475      * @brief Return the unique id value of the Fill instance.
476      *
477      * This method can be called for checking the current concrete instance type.
478      *
479      * @return The type id of the Fill instance.
480      */
481     uint32_t identifier() const noexcept;
482
483     _TVG_DECLARE_PRIVATE(Fill);
484 };
485
486
487 /**
488  * @class Canvas
489  *
490  * @brief An abstract class for drawing graphical elements.
491  *
492  * A canvas is an entity responsible for drawing the target. It sets up the drawing engine and the buffer, which can be drawn on the screen. It also manages given Paint objects.
493  *
494  * @note A Canvas behavior depends on the raster engine though the final content of the buffer is expected to be identical.
495  * @warning The Paint objects belonging to one Canvas can't be shared among multiple Canvases.
496  */
497 class TVG_EXPORT Canvas
498 {
499 public:
500     Canvas(RenderMethod*);
501     virtual ~Canvas();
502
503     /**
504      * @brief Sets the size of the container, where all the paints pushed into the Canvas are stored.
505      *
506      * If the number of objects pushed into the Canvas is known in advance, calling the function
507      * prevents multiple memory reallocation, thus improving the performance.
508      *
509      * @param[in] n The number of objects for which the memory is to be reserved.
510      *
511      * @return Result::Success when succeed.
512      */
513     Result reserve(uint32_t n) noexcept;
514
515     /**
516      * @brief Passes drawing elements to the Canvas using Paint objects.
517      *
518      * Only pushed paints in the canvas will be drawing targets.
519      * They are retained by the canvas until you call Canvas::clear().
520      * If you know the number of the pushed objects in advance, please call Canvas::reserve().
521      *
522      * @param[in] paint A Paint object to be drawn.
523      *
524      * @retval Result::Success When succeed.
525      * @retval Result::MemoryCorruption In case a @c nullptr is passed as the argument.
526      * @retval Result::InsufficientCondition An internal error.
527      *
528      * @note The rendering order of the paints is the same as the order as they were pushed into the canvas. Consider sorting the paints before pushing them if you intend to use layering.
529      * @see Canvas::reserve()
530      * @see Canvas::clear()
531      */
532     virtual Result push(std::unique_ptr<Paint> paint) noexcept;
533
534     /**
535      * @brief Sets the total number of the paints pushed into the canvas to be zero.
536      * Depending on the value of the @p free argument, the paints are freed or not.
537      *
538      * @param[in] free If @c true, the memory occupied by paints is deallocated, otherwise it is not.
539      *
540      * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
541      *
542      * @warning If you don't free the paints they become dangled. They are supposed to be reused, otherwise you are responsible for their lives. Thus please use the @p free argument only when you know how it works, otherwise it's not recommended.
543      */
544     virtual Result clear(bool free = true) noexcept;
545
546     /**
547      * @brief Request the canvas to update the paint objects.
548      *
549      * If a @c nullptr is passed all paint objects retained by the Canvas are updated,
550      * otherwise only the paint to which the given @p paint points.
551      *
552      * @param[in] paint A pointer to the Paint object or @c nullptr.
553      *
554      * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
555      *
556      * @note The Update behavior can be asynchronous if the assigned thread number is greater than zero.
557      */
558     virtual Result update(Paint* paint = nullptr) noexcept;
559
560     /**
561      * @brief Requests the canvas to draw the Paint objects.
562      *
563      * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
564      *
565      * @note Drawing can be asynchronous if the assigned thread number is greater than zero. To guarantee the drawing is done, call sync() afterwards.
566      * @see Canvas::sync()
567      */
568     virtual Result draw() noexcept;
569
570     /**
571      * @brief Guarantees that drawing task is finished.
572      *
573      * The Canvas rendering can be performed asynchronously. To make sure that rendering is finished,
574      * the sync() must be called after the draw() regardless of threading.
575      *
576      * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
577      * @see Canvas::draw()
578      */
579     virtual Result sync() noexcept;
580
581     _TVG_DECLARE_PRIVATE(Canvas);
582 };
583
584
585 /**
586  * @class LinearGradient
587  *
588  * @brief A class representing the linear gradient fill of the Shape object.
589  *
590  * Besides the APIs inherited from the Fill class, it enables setting and getting the linear gradient bounds.
591  * The behavior outside the gradient bounds depends on the value specified in the spread API.
592  */
593 class TVG_EXPORT LinearGradient final : public Fill
594 {
595 public:
596     ~LinearGradient();
597
598     /**
599      * @brief Sets the linear gradient bounds.
600      *
601      * The bounds of the linear gradient are defined as a surface constrained by two parallel lines crossing
602      * the given points (@p x1, @p y1) and (@p x2, @p y2), respectively. Both lines are perpendicular to the line linking
603      * (@p x1, @p y1) and (@p x2, @p y2).
604      *
605      * @param[in] x1 The horizontal coordinate of the first point used to determine the gradient bounds.
606      * @param[in] y1 The vertical coordinate of the first point used to determine the gradient bounds.
607      * @param[in] x2 The horizontal coordinate of the second point used to determine the gradient bounds.
608      * @param[in] y2 The vertical coordinate of the second point used to determine the gradient bounds.
609      *
610      * @return Result::Success when succeed.
611      *
612      * @note In case the first and the second points are equal, an object filled with such a gradient fill is not rendered.
613      */
614     Result linear(float x1, float y1, float x2, float y2) noexcept;
615
616     /**
617      * @brief Gets the linear gradient bounds.
618      *
619      * The bounds of the linear gradient are defined as a surface constrained by two parallel lines crossing
620      * the given points (@p x1, @p y1) and (@p x2, @p y2), respectively. Both lines are perpendicular to the line linking
621      * (@p x1, @p y1) and (@p x2, @p y2).
622      *
623      * @param[out] x1 The horizontal coordinate of the first point used to determine the gradient bounds.
624      * @param[out] y1 The vertical coordinate of the first point used to determine the gradient bounds.
625      * @param[out] x2 The horizontal coordinate of the second point used to determine the gradient bounds.
626      * @param[out] y2 The vertical coordinate of the second point used to determine the gradient bounds.
627      *
628      * @return Result::Success when succeed.
629      */
630     Result linear(float* x1, float* y1, float* x2, float* y2) const noexcept;
631
632     /**
633      * @brief Creates a new LinearGradient object.
634      *
635      * @return A new LinearGradient object.
636      */
637     static std::unique_ptr<LinearGradient> gen() noexcept;
638
639     /**
640      * @brief Return the unique id value of this class.
641      *
642      * This method can be referred for identifying the LinearGradient class type.
643      *
644      * @return The type id of the LinearGradient class.
645      */
646     static uint32_t identifier() noexcept;
647
648     _TVG_DECLARE_PRIVATE(LinearGradient);
649 };
650
651
652 /**
653  * @class RadialGradient
654  *
655  * @brief A class representing the radial gradient fill of the Shape object.
656  *
657  */
658 class TVG_EXPORT RadialGradient final : public Fill
659 {
660 public:
661     ~RadialGradient();
662
663     /**
664      * @brief Sets the radial gradient bounds.
665      *
666      * The radial gradient bounds are defined as a circle centered in a given point (@p cx, @p cy) of a given radius.
667      *
668      * @param[in] cx The horizontal coordinate of the center of the bounding circle.
669      * @param[in] cy The vertical coordinate of the center of the bounding circle.
670      * @param[in] radius The radius of the bounding circle.
671      *
672      * @return Result::Success when succeed, Result::InvalidArguments in case the @p radius value is zero or less.
673      */
674     Result radial(float cx, float cy, float radius) noexcept;
675
676     /**
677      * @brief Gets the radial gradient bounds.
678      *
679      * The radial gradient bounds are defined as a circle centered in a given point (@p cx, @p cy) of a given radius.
680      *
681      * @param[out] cx The horizontal coordinate of the center of the bounding circle.
682      * @param[out] cy The vertical coordinate of the center of the bounding circle.
683      * @param[out] radius The radius of the bounding circle.
684      *
685      * @return Result::Success when succeed.
686      */
687     Result radial(float* cx, float* cy, float* radius) const noexcept;
688
689     /**
690      * @brief Creates a new RadialGradient object.
691      *
692      * @return A new RadialGradient object.
693      */
694     static std::unique_ptr<RadialGradient> gen() noexcept;
695
696     /**
697      * @brief Return the unique id value of this class.
698      *
699      * This method can be referred for identifying the RadialGradient class type.
700      *
701      * @return The type id of the RadialGradient class.
702      */
703     static uint32_t identifier() noexcept;
704
705     _TVG_DECLARE_PRIVATE(RadialGradient);
706 };
707
708
709 /**
710  * @class Shape
711  *
712  * @brief A class representing two-dimensional figures and their properties.
713  *
714  * A shape has three major properties: shape outline, stroking, filling. The outline in the Shape is retained as the path.
715  * Path can be composed by accumulating primitive commands such as moveTo(), lineTo(), cubicTo(), or complete shape interfaces such as appendRect(), appendCircle(), etc.
716  * Path can consists of sub-paths. One sub-path is determined by a close command.
717  *
718  * The stroke of Shape is an optional property in case the Shape needs to be represented with/without the outline borders.
719  * It's efficient since the shape path and the stroking path can be shared with each other. It's also convenient when controlling both in one context.
720  */
721 class TVG_EXPORT Shape final : public Paint
722 {
723 public:
724     ~Shape();
725
726     /**
727      * @brief Resets the properties of the shape path.
728      *
729      * The color, the fill and the stroke properties are retained.
730      *
731      * @return Result::Success when succeed.
732      *
733      * @note The memory, where the path data is stored, is not deallocated at this stage for caching effect.
734      */
735     Result reset() noexcept;
736
737     /**
738      * @brief Sets the initial point of the sub-path.
739      *
740      * The value of the current point is set to the given point.
741      *
742      * @param[in] x The horizontal coordinate of the initial point of the sub-path.
743      * @param[in] y The vertical coordinate of the initial point of the sub-path.
744      *
745      * @return Result::Success when succeed.
746      */
747     Result moveTo(float x, float y) noexcept;
748
749     /**
750      * @brief Adds a new point to the sub-path, which results in drawing a line from the current point to the given end-point.
751      *
752      * The value of the current point is set to the given end-point.
753      *
754      * @param[in] x The horizontal coordinate of the end-point of the line.
755      * @param[in] y The vertical coordinate of the end-point of the line.
756      *
757      * @return Result::Success when succeed.
758      *
759      * @note In case this is the first command in the path, it corresponds to the moveTo() call.
760      */
761     Result lineTo(float x, float y) noexcept;
762
763     /**
764      * @brief Adds new points to the sub-path, which results in drawing a cubic Bezier curve starting
765      * at the current point and ending at the given end-point (@p x, @p y) using the control points (@p cx1, @p cy1) and (@p cx2, @p cy2).
766      *
767      * The value of the current point is set to the given end-point.
768      *
769      * @param[in] cx1 The horizontal coordinate of the 1st control point.
770      * @param[in] cy1 The vertical coordinate of the 1st control point.
771      * @param[in] cx2 The horizontal coordinate of the 2nd control point.
772      * @param[in] cy2 The vertical coordinate of the 2nd control point.
773      * @param[in] x The horizontal coordinate of the end-point of the curve.
774      * @param[in] y The vertical coordinate of the end-point of the curve.
775      *
776      * @return Result::Success when succeed.
777      *
778      * @note In case this is the first command in the path, no data from the path are rendered.
779      */
780     Result cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept;
781
782     /**
783      * @brief Closes the current sub-path by drawing a line from the current point to the initial point of the sub-path.
784      *
785      * The value of the current point is set to the initial point of the closed sub-path.
786      *
787      * @return Result::Success when succeed.
788      *
789      * @note In case the sub-path does not contain any points, this function has no effect.
790      */
791     Result close() noexcept;
792
793     /**
794      * @brief Appends a rectangle to the path.
795      *
796      * The rectangle with rounded corners can be achieved by setting non-zero values to @p rx and @p ry arguments.
797      * The @p rx and @p ry values specify the radii of the ellipse defining the rounding of the corners.
798      *
799      * The position of the rectangle is specified by the coordinates of its upper left corner - @p x and @p y arguments.
800      *
801      * The rectangle is treated as a new sub-path - it is not connected with the previous sub-path.
802      *
803      * The value of the current point is set to (@p x + @p rx, @p y) - in case @p rx is greater
804      * than @p w/2 the current point is set to (@p x + @p w/2, @p y)
805      *
806      * @param[in] x The horizontal coordinate of the upper left corner of the rectangle.
807      * @param[in] y The vertical coordinate of the upper left corner of the rectangle.
808      * @param[in] w The width of the rectangle.
809      * @param[in] h The height of the rectangle.
810      * @param[in] rx The x-axis radius of the ellipse defining the rounded corners of the rectangle.
811      * @param[in] ry The y-axis radius of the ellipse defining the rounded corners of the rectangle.
812      *
813      * @return Result::Success when succeed.
814      *
815      * @note For @p rx and @p ry greater than or equal to the half of @p w and the half of @p h, respectively, the shape become an ellipse.
816      */
817     Result appendRect(float x, float y, float w, float h, float rx, float ry) noexcept;
818
819     /**
820      * @brief Appends an ellipse to the path.
821      *
822      * The position of the ellipse is specified by the coordinates of its center - @p cx and @p cy arguments.
823      *
824      * The ellipse is treated as a new sub-path - it is not connected with the previous sub-path.
825      *
826      * The value of the current point is set to (@p cx, @p cy - @p ry).
827      *
828      * @param[in] cx The horizontal coordinate of the center of the ellipse.
829      * @param[in] cy The vertical coordinate of the center of the ellipse.
830      * @param[in] rx The x-axis radius of the ellipse.
831      * @param[in] ry The y-axis radius of the ellipse.
832      *
833      * @return Result::Success when succeed.
834      */
835     Result appendCircle(float cx, float cy, float rx, float ry) noexcept;
836
837     /**
838      * @brief Appends a circular arc to the path.
839      *
840      * The arc is treated as a new sub-path - it is not connected with the previous sub-path.
841      * The current point value is set to the end-point of the arc in case @p pie is @c false, and to the center of the arc otherwise.
842      *
843      * @param[in] cx The horizontal coordinate of the center of the arc.
844      * @param[in] cy The vertical coordinate of the center of the arc.
845      * @param[in] radius The radius of the arc.
846      * @param[in] startAngle The start angle of the arc given in degrees, measured counter-clockwise from the horizontal line.
847      * @param[in] sweep The central angle of the arc given in degrees, measured counter-clockwise from @p startAngle.
848      * @param[in] pie Specifies whether to draw radii from the arc's center to both of its end-point - drawn if @c true.
849      *
850      * @return Result::Success when succeed.
851      *
852      * @note Setting @p sweep value greater than 360 degrees, is equivalent to calling appendCircle(cx, cy, radius, radius).
853      */
854     Result appendArc(float cx, float cy, float radius, float startAngle, float sweep, bool pie) noexcept;
855
856     /**
857      * @brief Appends a given sub-path to the path.
858      *
859      * The current point value is set to the last point from the sub-path.
860      * For each command from the @p cmds array, an appropriate number of points in @p pts array should be specified.
861      * If the number of points in the @p pts array is different than the number required by the @p cmds array, the shape with this sub-path will not be displayed on the screen.
862      *
863      * @param[in] cmds The array of the commands in the sub-path.
864      * @param[in] cmdCnt The number of the sub-path's commands.
865      * @param[in] pts The array of the two-dimensional points.
866      * @param[in] ptsCnt The number of the points in the @p pts array.
867      *
868      * @return Result::Success when succeed, Result::InvalidArguments otherwise.
869      *
870      * @note The interface is designed for optimal path setting if the caller has a completed path commands already.
871      */
872     Result appendPath(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept;
873
874     /**
875      * @brief Sets the stroke width for all of the figures from the path.
876      *
877      * @param[in] width The width of the stroke. The default value is 0.
878      *
879      * @return Result::Success when succeed, Result::FailedAllocation otherwise.
880      */
881     Result stroke(float width) noexcept;
882
883     /**
884      * @brief Sets the color of the stroke for all of the figures from the path.
885      *
886      * @param[in] r The red color channel value in the range [0 ~ 255]. The default value is 0.
887      * @param[in] g The green color channel value in the range [0 ~ 255]. The default value is 0.
888      * @param[in] b The blue color channel value in the range [0 ~ 255]. The default value is 0.
889      * @param[in] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. The default value is 0.
890      *
891      * @return Result::Success when succeed, Result::FailedAllocation otherwise.
892      */
893     Result stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
894
895     /**
896      * @brief Sets the gradient fill of the stroke for all of the figures from the path.
897      *
898      * @param[in] f The gradient fill.
899      *
900      * @retval Result::Success When succeed.
901      * @retval Result::FailedAllocation An internal error with a memory allocation for an object to be filled.
902      * @retval Result::MemoryCorruption In case a @c nullptr is passed as the argument.
903      */
904     Result stroke(std::unique_ptr<Fill> f) noexcept;
905
906     /**
907      * @brief Sets the dash pattern of the stroke.
908      *
909      * @param[in] dashPattern The array of consecutive pair values of the dash length and the gap length.
910      * @param[in] cnt The length of the @p dashPattern array.
911      *
912      * @retval Result::Success When succeed.
913      * @retval Result::FailedAllocation An internal error with a memory allocation for an object to be dashed.
914      * @retval Result::InvalidArguments In case @p dashPattern is @c nullptr and @p cnt > 0, @p cnt is zero, any of the dash pattern values is zero or less.
915      *
916      * @note To reset the stroke dash pattern, pass @c nullptr to @p dashPattern and zero to @p cnt.
917      * @warning @p cnt must be greater than 1 if the dash pattern is valid.
918      */
919     Result stroke(const float* dashPattern, uint32_t cnt) noexcept;
920
921     /**
922      * @brief Sets the cap style of the stroke in the open sub-paths.
923      *
924      * @param[in] cap The cap style value. The default value is @c StrokeCap::Square.
925      *
926      * @return Result::Success when succeed, Result::FailedAllocation otherwise.
927      */
928     Result stroke(StrokeCap cap) noexcept;
929
930     /**
931      * @brief Sets the join style for stroked path segments.
932      *
933      * The join style is used for joining the two line segment while stroking the path.
934      *
935      * @param[in] join The join style value. The default value is @c StrokeJoin::Bevel.
936      *
937      * @return Result::Success when succeed, Result::FailedAllocation otherwise.
938      */
939     Result stroke(StrokeJoin join) noexcept;
940
941     /**
942      * @brief Sets the solid color for all of the figures from the path.
943      *
944      * The parts of the shape defined as inner are colored.
945      *
946      * @param[in] r The red color channel value in the range [0 ~ 255]. The default value is 0.
947      * @param[in] g The green color channel value in the range [0 ~ 255]. The default value is 0.
948      * @param[in] b The blue color channel value in the range [0 ~ 255]. The default value is 0.
949      * @param[in] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. The default value is 0.
950      *
951      * @return Result::Success when succeed.
952      *
953      * @note Either a solid color or a gradient fill is applied, depending on what was set as last.
954      * @note ClipPath won't use the fill values. (see: enum class CompositeMethod::ClipPath)
955      */
956     Result fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
957
958     /**
959      * @brief Sets the gradient fill for all of the figures from the path.
960      *
961      * The parts of the shape defined as inner are filled.
962      *
963      * @param[in] f The unique pointer to the gradient fill.
964      *
965      * @return Result::Success when succeed, Result::MemoryCorruption otherwise.
966      *
967      * @note Either a solid color or a gradient fill is applied, depending on what was set as last.
968      */
969     Result fill(std::unique_ptr<Fill> f) noexcept;
970
971     /**
972      * @brief Sets the fill rule for the Shape object.
973      *
974      * @param[in] r The fill rule value. The default value is @c FillRule::Winding.
975      *
976      * @return Result::Success when succeed.
977      */
978     Result fill(FillRule r) noexcept;
979
980     /**
981      * @brief Gets the commands data of the path.
982      *
983      * @param[out] cmds The pointer to the array of the commands from the path.
984      *
985      * @return The length of the @p cmds array when succeed, zero otherwise.
986      */
987     uint32_t pathCommands(const PathCommand** cmds) const noexcept;
988
989     /**
990      * @brief Gets the points values of the path.
991      *
992      * @param[out] pts The pointer to the array of the two-dimensional points from the path.
993      *
994      * @return The length of the @p pts array when succeed, zero otherwise.
995      */
996     uint32_t pathCoords(const Point** pts) const noexcept;
997
998     /**
999      * @brief Gets the pointer to the gradient fill of the shape.
1000      *
1001      * @return The pointer to the gradient fill of the stroke when succeed, @c nullptr in case no fill was set.
1002      */
1003     const Fill* fill() const noexcept;
1004
1005     /**
1006      * @brief Gets the solid color of the shape.
1007      *
1008      * @param[out] r The red color channel value in the range [0 ~ 255].
1009      * @param[out] g The green color channel value in the range [0 ~ 255].
1010      * @param[out] b The blue color channel value in the range [0 ~ 255].
1011      * @param[out] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque.
1012      *
1013      * @return Result::Success when succeed.
1014      */
1015     Result fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
1016
1017     /**
1018      * @brief Gets the fill rule value.
1019      *
1020      * @return The fill rule value of the shape.
1021      */
1022     FillRule fillRule() const noexcept;
1023
1024     /**
1025      * @brief Gets the stroke width.
1026      *
1027      * @return The stroke width value when succeed, zero if no stroke was set.
1028      */
1029     float strokeWidth() const noexcept;
1030
1031     /**
1032      * @brief Gets the color of the shape's stroke.
1033      *
1034      * @param[out] r The red color channel value in the range [0 ~ 255].
1035      * @param[out] g The green color channel value in the range [0 ~ 255].
1036      * @param[out] b The blue color channel value in the range [0 ~ 255].
1037      * @param[out] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque.
1038      *
1039      * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
1040      */
1041     Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
1042
1043     /**
1044      * @brief Gets the pointer to the gradient fill of the stroke.
1045      *
1046      * @return The pointer to the gradient fill of the stroke when succeed, @c nullptr otherwise.
1047      */
1048     const Fill* strokeFill() const noexcept;
1049
1050     /**
1051      * @brief Gets the dash pattern of the stroke.
1052      *
1053      * @param[out] dashPattern The pointer to the memory, where the dash pattern array is stored.
1054      *
1055      * @return The length of the @p dashPattern array.
1056      */
1057     uint32_t strokeDash(const float** dashPattern) const noexcept;
1058
1059     /**
1060      * @brief Gets the cap style used for stroking the path.
1061      *
1062      * @return The cap style value of the stroke.
1063      */
1064     StrokeCap strokeCap() const noexcept;
1065
1066     /**
1067      * @brief Gets the join style value used for stroking the path.
1068      *
1069      * @return The join style value of the stroke.
1070      */
1071     StrokeJoin strokeJoin() const noexcept;
1072
1073     /**
1074      * @brief Creates a new Shape object.
1075      *
1076      * @return A new Shape object.
1077      */
1078     static std::unique_ptr<Shape> gen() noexcept;
1079
1080     /**
1081      * @brief Return the unique id value of this class.
1082      *
1083      * This method can be referred for identifying the Shape class type.
1084      *
1085      * @return The type id of the Shape class.
1086      */
1087     static uint32_t identifier() noexcept;
1088
1089     _TVG_DECLARE_PRIVATE(Shape);
1090 };
1091
1092
1093 /**
1094  * @class Picture
1095  *
1096  * @brief A class representing an image read in one of the supported formats: raw, svg, png, jpg and etc.
1097  * Besides the methods inherited from the Paint, it provides methods to load & draw images on the canvas.
1098  *
1099  * @note Supported formats are depended on the available TVG loaders.
1100  */
1101 class TVG_EXPORT Picture final : public Paint
1102 {
1103 public:
1104     ~Picture();
1105
1106     /**
1107      * @brief Loads a picture data directly from a file.
1108      *
1109      * @param[in] path A path to the picture file.
1110      *
1111      * @retval Result::Success When succeed.
1112      * @retval Result::InvalidArguments In case the @p path is invalid.
1113      * @retval Result::NonSupport When trying to load a file with an unknown extension.
1114      * @retval Result::Unknown If an error occurs at a later stage.
1115      *
1116      * @note The Load behavior can be asynchronous if the assigned thread number is greater than zero.
1117      * @see Initializer::init()
1118      */
1119     Result load(const std::string& path) noexcept;
1120
1121     /**
1122      * @brief Loads a picture data from a memory block of a given size.
1123      *
1124      * @param[in] data A pointer to a memory location where the content of the picture file is stored.
1125      * @param[in] size The size in bytes of the memory occupied by the @p data.
1126      * @param[in] copy Decides whether the data should be copied into the engine local buffer.
1127      *
1128      * @retval Result::Success When succeed.
1129      * @retval Result::InvalidArguments In case no data are provided or the @p size is zero or less.
1130      * @retval Result::NonSupport When trying to load a file with an unknown extension.
1131      * @retval Result::Unknown If an error occurs at a later stage.
1132      *
1133      * @warning: you have responsibility to release the @p data memory if the @p copy is true
1134      * @deprecated Use load(const char* data, uint32_t size, const std::string& mimeType, bool copy) instead.
1135      * @see Result load(const char* data, uint32_t size, const std::string& mimeType, bool copy = false) noexcept
1136      */
1137     TVG_DEPRECATED Result load(const char* data, uint32_t size, bool copy = false) noexcept;
1138
1139     /**
1140      * @brief Loads a picture data from a memory block of a given size.
1141      *
1142      * @param[in] data A pointer to a memory location where the content of the picture file is stored.
1143      * @param[in] size The size in bytes of the memory occupied by the @p data.
1144      * @param[in] mimeType Mimetype or extension of data such as "jpg", "jpeg", "svg", "svg+xml", "png", etc. In case an empty string or an unknown type is provided, the loaders will be tried one by one.
1145      * @param[in] copy If @c true the data are copied into the engine local buffer, otherwise they are not.
1146      *
1147      * @retval Result::Success When succeed.
1148      * @retval Result::InvalidArguments In case no data are provided or the @p size is zero or less.
1149      * @retval Result::NonSupport When trying to load a file with an unknown extension.
1150      * @retval Result::Unknown If an error occurs at a later stage.
1151      *
1152      * @warning: It's the user responsibility to release the @p data memory if the @p copy is @c true.
1153      *
1154      * @since 0.5
1155      */
1156     Result load(const char* data, uint32_t size, const std::string& mimeType, bool copy = false) noexcept;
1157
1158     /**
1159      * @brief Resizes the picture content to the given width and height.
1160      *
1161      * The picture content is resized while keeping the default size aspect ratio.
1162      * The scaling factor is established for each of dimensions and the smaller value is applied to both of them.
1163      *
1164      * @param[in] w A new width of the image in pixels.
1165      * @param[in] h A new height of the image in pixels.
1166      *
1167      * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
1168      */
1169     Result size(float w, float h) noexcept;
1170
1171     /**
1172      * @brief Gets the size of the image.
1173      *
1174      * @param[out] w The width of the image in pixels.
1175      * @param[out] h The height of the image in pixels.
1176      *
1177      * @return Result::Success when succeed.
1178      */
1179     Result size(float* w, float* h) const noexcept;
1180
1181     /**
1182      * @brief Gets the pixels information of the picture.
1183      *
1184      * @note The data must be pre-multiplied by the alpha channels.
1185      *
1186      * @warning Please do not use it, this API is not official one. It could be modified in the next version.
1187      *
1188      * @BETA_API
1189      */
1190     const uint32_t* data(uint32_t* w, uint32_t* h) const noexcept;
1191
1192     /**
1193      * @brief Loads a raw data from a memory block with a given size.
1194      *
1195      * @warning Please do not use it, this API is not official one. It could be modified in the next version.
1196      *
1197      * @BETA_API
1198      */
1199     Result load(uint32_t* data, uint32_t w, uint32_t h, bool copy) noexcept;
1200
1201     /**
1202      * @brief Sets or removes the triangle mesh to deform the image.
1203      *
1204      * If a mesh is provided, the transform property of the Picture will apply to the triangle mesh, and the
1205      * image data will be used as the texture.
1206      *
1207      * If @p triangles is @c nullptr, or @p triangleCnt is 0, the mesh will be removed.
1208      *
1209      * Only raster image types are supported at this time (png, jpg). Vector types like svg and tvg do not support.
1210      * mesh deformation. However, if required you should be able to render a vector image to a raster image and then apply a mesh.
1211      *
1212      * @param[in] triangles An array of Polygons(triangles) that make up the mesh, or null to remove the mesh.
1213      * @param[in] triangleCnt The number of Polygons(triangles) provided, or 0 to remove the mesh.
1214      *
1215      * @retval Result::Success When succeed.
1216      * @retval Result::Unknown If fails
1217      *
1218      * @note The Polygons are copied internally, so modifying them after calling Mesh::mesh has no affect.
1219      * @warning Please do not use it, this API is not official one. It could be modified in the next version.
1220      *
1221      * @BETA_API
1222      */
1223     Result mesh(const Polygon* triangles, const uint32_t triangleCnt) noexcept;
1224
1225     /**
1226      * @brief Return the number of triangles in the mesh, and optionally get a pointer to the array of triangles in the mesh.
1227      *
1228      * @param[out] triangles Optional. A pointer to the array of Polygons used by this mesh.
1229      *
1230      * @return uint32_t The number of polygons in the array.
1231      *
1232      * @note Modifying the triangles returned by this method will modify them directly within the mesh.
1233      * @warning Please do not use it, this API is not official one. It could be modified in the next version.
1234      *
1235      * @BETA_API
1236      */
1237     uint32_t mesh(const Polygon** triangles) const noexcept;
1238
1239     /**
1240      * @brief Gets the position and the size of the loaded SVG picture.
1241      *
1242      * @warning Please do not use it, this API is not official one. It could be modified in the next version.
1243      *
1244      * @BETA_API
1245      */
1246     Result viewbox(float* x, float* y, float* w, float* h) const noexcept;
1247
1248     /**
1249      * @brief Creates a new Picture object.
1250      *
1251      * @return A new Picture object.
1252      */
1253     static std::unique_ptr<Picture> gen() noexcept;
1254
1255     /**
1256      * @brief Return the unique id value of this class.
1257      *
1258      * This method can be referred for identifying the Picture class type.
1259      *
1260      * @return The type id of the Picture class.
1261      */
1262     static uint32_t identifier() noexcept;
1263
1264     _TVG_DECLARE_PRIVATE(Picture);
1265 };
1266
1267
1268 /**
1269  * @class Scene
1270  *
1271  * @brief A class to composite children paints.
1272  *
1273  * As the traditional graphics rendering method, TVG also enables scene-graph mechanism.
1274  * This feature supports an array function for managing the multiple paints as one group paint.
1275  *
1276  * As a group, the scene can be transformed, made translucent and composited with other target paints,
1277  * its children will be affected by the scene world.
1278  */
1279 class TVG_EXPORT Scene final : public Paint
1280 {
1281 public:
1282     ~Scene();
1283
1284     /**
1285      * @brief Passes drawing elements to the Scene using Paint objects.
1286      *
1287      * Only the paints pushed into the scene will be the drawn targets.
1288      * The paints are retained by the scene until Scene::clear() is called.
1289      * If you know the number of the pushed objects in advance, please call Scene::reserve().
1290      *
1291      * @param[in] paint A Paint object to be drawn.
1292      *
1293      * @return Result::Success when succeed, Result::MemoryCorruption otherwise.
1294      *
1295      * @note The rendering order of the paints is the same as the order as they were pushed. Consider sorting the paints before pushing them if you intend to use layering.
1296      * @see Scene::reserve()
1297      */
1298     Result push(std::unique_ptr<Paint> paint) noexcept;
1299
1300     /**
1301      * @brief Sets the size of the container, where all the paints pushed into the Scene are stored.
1302      *
1303      * If the number of objects pushed into the scene is known in advance, calling the function
1304      * prevents multiple memory reallocation, thus improving the performance.
1305      *
1306      * @param[in] size The number of objects for which the memory is to be reserved.
1307      *
1308      * @return Result::Success when succeed, Result::FailedAllocation otherwise.
1309      */
1310     Result reserve(uint32_t size) noexcept;
1311
1312     /**
1313      * @brief Sets the total number of the paints pushed into the scene to be zero.
1314      * Depending on the value of the @p free argument, the paints are freed or not.
1315      *
1316      * @param[in] free If @c true, the memory occupied by paints is deallocated, otherwise it is not.
1317      *
1318      * @return Result::Success when succeed
1319      *
1320      * @warning If you don't free the paints they become dangled. They are supposed to be reused, otherwise you are responsible for their lives. Thus please use the @p free argument only when you know how it works, otherwise it's not recommended.
1321      *
1322      * @since 0.2
1323      */
1324     Result clear(bool free = true) noexcept;
1325
1326     /**
1327      * @brief Creates a new Scene object.
1328      *
1329      * @return A new Scene object.
1330      */
1331     static std::unique_ptr<Scene> gen() noexcept;
1332
1333     /**
1334      * @brief Return the unique id value of this class.
1335      *
1336      * This method can be referred for identifying the Scene class type.
1337      *
1338      * @return The type id of the Scene class.
1339      */
1340     static uint32_t identifier() noexcept;
1341
1342     _TVG_DECLARE_PRIVATE(Scene);
1343 };
1344
1345
1346 /**
1347  * @class SwCanvas
1348  *
1349  * @brief A class for the rendering graphical elements with a software raster engine.
1350  */
1351 class TVG_EXPORT SwCanvas final : public Canvas
1352 {
1353 public:
1354     ~SwCanvas();
1355
1356     /**
1357      * @brief Enumeration specifying the methods of combining the 8-bit color channels into 32-bit color.
1358      */
1359     enum Colorspace
1360     {
1361         ABGR8888 = 0,      ///< The channels are joined in the order: alpha, blue, green, red. Colors are alpha-premultiplied.
1362         ARGB8888,          ///< The channels are joined in the order: alpha, red, green, blue. Colors are alpha-premultiplied.
1363         ABGR8888_STRAIGHT, ///< @BETA_API The channels are joined in the order: alpha, blue, green, red. Colors are un-alpha-premultiplied.
1364         ARGB8888_STRAIGHT, ///< @BETA_API The channels are joined in the order: alpha, red, green, blue. Colors are un-alpha-premultiplied.
1365     };
1366
1367     /**
1368      * @brief Enumeration specifying the methods of Memory Pool behavior policy.
1369      * @since 0.4
1370      */
1371     enum MempoolPolicy
1372     {
1373         Default = 0, ///< Default behavior that ThorVG is designed to.
1374         Shareable,   ///< Memory Pool is shared among the SwCanvases.
1375         Individual   ///< Allocate designated memory pool that is only used by current instance.
1376     };
1377
1378     /**
1379      * @brief Sets the target buffer for the rasterization.
1380      *
1381      * The buffer of a desirable size should be allocated and owned by the caller.
1382      *
1383      * @param[in] buffer A pointer to a memory block of the size @p stride x @p h, where the raster data are stored.
1384      * @param[in] stride The stride of the raster image - greater than or equal to @p w.
1385      * @param[in] w The width of the raster image.
1386      * @param[in] h The height of the raster image.
1387      * @param[in] cs The value specifying the way the 32-bits colors should be read/written.
1388      *
1389      * @retval Result::Success When succeed.
1390      * @retval Result::MemoryCorruption When casting in the internal function implementation failed.
1391      * @retval Result::InvalidArguments In case no valid pointer is provided or the width, or the height or the stride is zero.
1392      * @retval Result::NonSupport In case the software engine is not supported.
1393      *
1394      * @warning Do not access @p buffer during Canvas::draw() - Canvas::sync(). It should not be accessed while TVG is writing on it.
1395     */
1396     Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, Colorspace cs) noexcept;
1397
1398     /**
1399      * @brief Set sw engine memory pool behavior policy.
1400      *
1401      * Basically ThorVG draws a lot of shapes, it allocates/deallocates a few chunk of memory
1402      * while processing rendering. It internally uses one shared memory pool
1403      * which can be reused among the canvases in order to avoid memory overhead.
1404      *
1405      * Thus ThorVG suggests using a memory pool policy to satisfy user demands,
1406      * if it needs to guarantee the thread-safety of the internal data access.
1407      *
1408      * @param[in] policy The method specifying the Memory Pool behavior. The default value is @c MempoolPolicy::Default.
1409      *
1410      * @retval Result::Success When succeed.
1411      * @retval Result::InsufficientCondition If the canvas contains some paints already.
1412      * @retval Result::NonSupport In case the software engine is not supported.
1413      *
1414      * @note When @c policy is set as @c MempoolPolicy::Individual, the current instance of canvas uses its own individual
1415      *       memory data, which is not shared with others. This is necessary when the canvas is accessed on a worker-thread.
1416      *
1417      * @warning It's not allowed after pushing any paints.
1418      *
1419      * @since 0.4
1420     */
1421     Result mempool(MempoolPolicy policy) noexcept;
1422
1423     /**
1424      * @brief Creates a new SwCanvas object.
1425      * @return A new SwCanvas object.
1426      */
1427     static std::unique_ptr<SwCanvas> gen() noexcept;
1428
1429     _TVG_DECLARE_PRIVATE(SwCanvas);
1430 };
1431
1432
1433 /**
1434  * @class GlCanvas
1435  *
1436  * @brief A class for the rendering graphic elements with a GL raster engine.
1437  *
1438  * @warning Please do not use it. This class is not fully supported yet.
1439  *
1440  * @BETA_API
1441  */
1442 class TVG_EXPORT GlCanvas final : public Canvas
1443 {
1444 public:
1445     ~GlCanvas();
1446
1447     /**
1448      * @brief Sets the target buffer for the rasterization.
1449      *
1450      * @warning Please do not use it, this API is not official one. It could be modified in the next version.
1451      *
1452      * @BETA_API
1453      */
1454     Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h) noexcept;
1455
1456     /**
1457      * @brief Creates a new GlCanvas object.
1458      *
1459      * @return A new GlCanvas object.
1460      *
1461      * @BETA_API
1462      */
1463     static std::unique_ptr<GlCanvas> gen() noexcept;
1464
1465     _TVG_DECLARE_PRIVATE(GlCanvas);
1466 };
1467
1468
1469 /**
1470  * @class Initializer
1471  *
1472  * @brief A class that enables initialization and termination of the TVG engines.
1473  */
1474 class TVG_EXPORT Initializer final
1475 {
1476 public:
1477     /**
1478      * @brief Initializes TVG engines.
1479      *
1480      * TVG requires the running-engine environment.
1481      * TVG runs its own task-scheduler for parallelizing rendering tasks efficiently.
1482      * You can indicate the number of threads, the count of which is designated @p threads.
1483      * In the initialization step, TVG will generate/spawn the threads as set by @p threads count.
1484      *
1485      * @param[in] engine The engine types to initialize. This is relative to the Canvas types, in which it will be used. For multiple backends bitwise operation is allowed.
1486      * @param[in] threads The number of additional threads. Zero indicates only the main thread is to be used.
1487      *
1488      * @retval Result::Success When succeed.
1489      * @retval Result::FailedAllocation An internal error possibly with memory allocation.
1490      * @retval Result::InvalidArguments If unknown engine type chosen.
1491      * @retval Result::NonSupport In case the engine type is not supported on the system.
1492      * @retval Result::Unknown Others.
1493      *
1494      * @note The Initializer keeps track of the number of times it was called. Threads count is fixed at the first init() call.
1495      * @see Initializer::term()
1496      */
1497     static Result init(CanvasEngine engine, uint32_t threads) noexcept;
1498
1499     /**
1500      * @brief Terminates TVG engines.
1501      *
1502      * @param[in] engine The engine types to terminate. This is relative to the Canvas types, in which it will be used. For multiple backends bitwise operation is allowed
1503      *
1504      * @retval Result::Success When succeed.
1505      * @retval Result::InsufficientCondition In case there is nothing to be terminated.
1506      * @retval Result::InvalidArguments If unknown engine type chosen.
1507      * @retval Result::NonSupport In case the engine type is not supported on the system.
1508      * @retval Result::Unknown Others.
1509      *
1510      * @note Initializer does own reference counting for multiple calls.
1511      * @see Initializer::init()
1512      */
1513     static Result term(CanvasEngine engine) noexcept;
1514
1515     _TVG_DISABLE_CTOR(Initializer);
1516 };
1517
1518
1519 /**
1520  * @class Saver
1521  *
1522  * @brief A class for exporting a paint object into a specified file, from which to recover the paint data later.
1523  *
1524  * ThorVG provides a feature for exporting & importing paint data. The Saver role is to export the paint data to a file.
1525  * It's useful when you need to save the composed scene or image from a paint object and recreate it later.
1526  *
1527  * The file format is decided by the extension name(i.e. "*.tvg") while the supported formats depend on the TVG packaging environment.
1528  * If it doesn't support the file format, the save() method returns the @c Result::NonSuppport result.
1529  *
1530  * Once you export a paint to the file successfully, you can recreate it using the Picture class.
1531  *
1532  * @see Picture::load()
1533  *
1534  * @since 0.5
1535  */
1536 class TVG_EXPORT Saver final
1537 {
1538 public:
1539     ~Saver();
1540
1541     /**
1542      * @brief Exports the given @p paint data to the given @p path
1543      *
1544      * If the saver module supports any compression mechanism, it will optimize the data size.
1545      * This might affect the encoding/decoding time in some cases. You can turn off the compression
1546      * if you wish to optimize for speed.
1547      *
1548      * @param[in] paint The paint to be saved with all its associated properties.
1549      * @param[in] path A path to the file, in which the paint data is to be saved.
1550      * @param[in] compress If @c true then compress data if possible.
1551      *
1552      * @retval Result::Success When succeed.
1553      * @retval Result::InsufficientCondition If currently saving other resources.
1554      * @retval Result::NonSupport When trying to save a file with an unknown extension or in an unsupported format.
1555      * @retval Result::MemoryCorruption An internal error.
1556      * @retval Result::Unknown In case an empty paint is to be saved.
1557      *
1558      * @note Saving can be asynchronous if the assigned thread number is greater than zero. To guarantee the saving is done, call sync() afterwards.
1559      * @see Saver::sync()
1560      *
1561      * @since 0.5
1562      */
1563     Result save(std::unique_ptr<Paint> paint, const std::string& path, bool compress = true) noexcept;
1564
1565     /**
1566      * @brief Guarantees that the saving task is finished.
1567      *
1568      * The behavior of the Saver works on a sync/async basis, depending on the threading setting of the Initializer.
1569      * Thus, if you wish to have a benefit of it, you must call sync() after the save() in the proper delayed time.
1570      * Otherwise, you can call sync() immediately.
1571      *
1572      * @retval Result::Success when succeed.
1573      * @retval Result::InsufficientCondition otherwise.
1574      *
1575      * @note The asynchronous tasking is dependent on the Saver module implementation.
1576      * @see Saver::save()
1577      *
1578      * @since 0.5
1579      */
1580     Result sync() noexcept;
1581
1582     /**
1583      * @brief Creates a new Saver object.
1584      *
1585      * @return A new Saver object.
1586      *
1587      * @since 0.5
1588      */
1589     static std::unique_ptr<Saver> gen() noexcept;
1590
1591     _TVG_DECLARE_PRIVATE(Saver);
1592 };
1593
1594
1595 /**
1596  * @class Accessor
1597  *
1598  * @brief The Accessor is a utility class to debug the Scene structure by traversing the scene-tree.
1599  *
1600  * The Accessor helps you search specific nodes to read the property information, figure out the structure of the scene tree and its size.
1601  *
1602  * @warning We strongly warn you not to change the paints of a scene unless you really know the design-structure.
1603  *
1604  * @BETA_API
1605  */
1606 class TVG_EXPORT Accessor final
1607 {
1608 public:
1609     ~Accessor();
1610
1611     /**
1612      * @brief Set the access function for traversing the Picture scene tree nodes.
1613      *
1614      * @param[in] picture The picture node to traverse the internal scene-tree.
1615      * @param[in] func The callback function calling for every paint nodes of the Picture.
1616      *
1617      * @return Return the given @p picture instance.
1618      *
1619      * @note The bitmap based picture might not have the scene-tree.
1620      *
1621      * @BETA_API
1622      */
1623     std::unique_ptr<Picture> set(std::unique_ptr<Picture> picture, std::function<bool(const Paint* paint)> func) noexcept;
1624
1625     /**
1626      * @brief Creates a new Accessor object.
1627      *
1628      * @return A new Accessor object.
1629      *
1630      * @BETA_API
1631      */
1632     static std::unique_ptr<Accessor> gen() noexcept;
1633
1634     _TVG_DECLARE_PRIVATE(Accessor);
1635 };
1636
1637 /** @}*/
1638
1639 } //namespace
1640
1641 #ifdef __cplusplus
1642 }
1643 #endif
1644
1645 #endif //_THORVG_H_