4 * The main APIs enabling the TVG initialization, preparation of the canvas and provisioning of its content:
5 * - drawing shapes such as line, curve, arc, rectangle, circle or user-defined
6 * - drawing pictures - SVG, PNG, RAW
7 * - solid or gradient filling
8 * - continuous and dashed stroking
9 * - clipping and masking
10 * and finally drawing the canvas and TVG termination.
21 #define TVG_EXPORT __attribute__ ((visibility ("default")))
31 #define _TVG_DECLARE_PRIVATE(A) \
35 A(const A&) = delete; \
36 const A& operator=(const A&) = delete; \
39 #define _TVG_DISABLE_CTOR(A) \
43 #define _TVG_DECLARE_ACCESSOR() \
48 #define _TVG_DECALRE_IDENTIFIER() \
49 auto id() const { return _id; } \
62 * @defgroup ThorVG ThorVG
63 * @brief ThorVG classes and enumerations providing C++ APIs.
69 * @brief Enumeration specifying the result from the APIs.
71 enum class TVG_EXPORT Result
73 Success = 0, ///< The value returned in case of a correct request execution.
74 InvalidArguments, ///< The value returned in the event of a problem with the arguments given to the API - e.g. empty paths or null pointers.
75 InsufficientCondition, ///< The value returned in case the request cannot be processed - e.g. asking for properties of an object, which does not exist.
76 FailedAllocation, ///< The value returned in case of unsuccessful memory allocation.
77 MemoryCorruption, ///< The value returned in the event of bad memory handling - e.g. failing in pointer releasing or casting
78 NonSupport, ///< The value returned in case of choosing unsupported options.
79 Unknown ///< The value returned in all other cases.
83 * @brief Enumeration specifying the values of the path commands accepted by TVG.
85 * Not to be confused with the path commands from the svg path element (like M, L, Q, H and many others).
86 * TVG interprets all of them and translates to the ones from the PathCommand values.
88 enum class TVG_EXPORT PathCommand
90 Close = 0, ///< Ends the current sub-path and connects it with its initial point. This command doesn't expect any points.
91 MoveTo, ///< Sets a new initial point of the sub-path and a new current point. This command expects 1 point: the starting position.
92 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.
93 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.
97 * @brief Enumeration determining the ending type of a stroke in the open sub-paths.
99 enum class TVG_EXPORT StrokeCap
101 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.
102 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.
103 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.
107 * @brief Enumeration determining the style used at the corners of joined stroked path segments.
109 enum class TVG_EXPORT StrokeJoin
111 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.
112 Round, ///< The outer corner of the joined path segments is rounded. The circular region is centered at the join point.
113 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.
117 * @brief Enumeration specifying how to fill the area outside the gradient bounds.
119 enum class TVG_EXPORT FillSpread
121 Pad = 0, ///< The remaining area is filled with the closest stop color.
122 Reflect, ///< The gradient pattern is reflected outside the gradient area until the expected region is filled.
123 Repeat ///< The gradient pattern is repeated continuously beyond the gradient area until the expected region is filled.
127 * @brief Enumeration specifying the algorithm used to establish which parts of the shape are treated as the inside of the shape.
129 enum class TVG_EXPORT FillRule
131 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.
132 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.
136 * @brief Enumeration indicating the method used in the composition of two objects - the target and the source.
138 enum class TVG_EXPORT CompositeMethod
140 None = 0, ///< No composition is applied.
141 ClipPath, ///< The intersection of the source and the target is determined and only the resulting pixels from the source are rendered.
142 AlphaMask, ///< The pixels of the source and the target are alpha blended. As a result, only the part of the source, which intersects with the target is visible.
143 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 is not covered by the target is visible.
147 * @brief Enumeration specifying the engine type used for the graphics backend. For multiple backeneds bitwise operation is allowed.
149 enum class TVG_EXPORT CanvasEngine
151 Sw = (1 << 1), ///< CPU rasterizer.
152 Gl = (1 << 2) ///< OpenGL rasterizer.
157 * @brief A data structure representing a point in two-dimensional space.
166 * @brief A data structure representing a three-dimensional matrix.
168 * The elements e11, e12, e21 and e22 represent the rotation matrix, including the scaling factor.
169 * The elements e13 and e23 determine the translation of the object along the x and y-axis, respectively.
170 * The elements e31 and e32 are set to 0, e33 is set to 1.
183 * @brief An abstract class for managing graphical elements.
185 * A graphical element in TVG is any object composed into a Canvas.
186 * Paint represents such a graphical object and its behaviors such as duplication, transformation and composition.
187 * 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.
189 class TVG_EXPORT Paint
195 * @brief Sets the angle by which the object is rotated.
197 * The angle in measured clockwise from the horizontal axis.
198 * The rotational axis passes through the point on the object with zero coordinates.
200 * @param[in] degree The value of the angle in degrees.
202 * @return Result::Success when succeed, Result::FailedAllocation otherwise.
204 Result rotate(float degree) noexcept;
207 * @brief Sets the scale value of the object.
209 * @param[in] factor The value of the scaling factor. The default value is 1.
211 * @return Result::Success when succeed, Result::FailedAllocation otherwise.
213 Result scale(float factor) noexcept;
216 * @brief Sets the values by which the object is moved in a two-dimensional space.
218 * The origin of the coordinate system is in the upper left corner of the canvas.
219 * The horizontal and vertical axes point to the right and down, respectively.
221 * @param[in] x The value of the horizontal shift.
222 * @param[in] y The value of the vertical shift.
224 * @return Result::Success when succeed, Result::FailedAllocation otherwise.
226 Result translate(float x, float y) noexcept;
229 * @brief Sets the matrix of the affine transformation for the object.
231 * The augmented matrix of the transformation is expected to be given.
233 * @param[in] m The 3x3 augmented matrix.
235 * @return Result::Success when succeed, Result::FailedAllocation otherwise.
237 Result transform(const Matrix& m) noexcept;
240 * @brief Gets the matrix of the affine transformation of the object.
242 * The values of the matrix can be set by the transform() API, as well by the translate(),
243 * scale() and rotate(). In case no transformation was applied, the identity matrix is returned.
245 * @retval The augmented transformation matrix.
249 Matrix transform() noexcept;
252 * @brief Sets the opacity of the object.
254 * @param[in] o The opacity value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque.
256 * @return Result::Success when succeed.
258 * @note Setting the opacity with this API may require multiple render pass for composition. It is recommended to avoid changing the opacity if possible.
260 Result opacity(uint8_t o) noexcept;
263 * @brief Sets the composition target object and the composition method.
265 * @param[in] target The paint of the target object.
266 * @param[in] method The method used to composite the source object with the target.
268 * @return Result::Success when succeed, Result::InvalidArguments otherwise.
270 Result composite(std::unique_ptr<Paint> target, CompositeMethod method) noexcept;
273 * @brief Gets the bounding box of the paint object before any transformation.
275 * @param[out] x The x coordinate of the upper left corner of the object.
276 * @param[out] y The y coordinate of the upper left corner of the object.
277 * @param[out] w The width of the object.
278 * @param[out] h The height of the object.
280 * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
282 * @note The bounding box doesn't indicate the final rendered region. It's the smallest rectangle that encloses the object.
284 Result bounds(float* x, float* y, float* w, float* h) const noexcept;
287 * @brief Duplicates the object.
289 * Creates a new object and sets its all properties as in the original object.
291 * @return The created object when succeed, @c nullptr otherwise.
293 Paint* duplicate() const noexcept;
296 * @brief Gets the opacity value of the object.
298 * @return The opacity value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque.
300 uint8_t opacity() const noexcept;
303 * @brief Const forward iterator-like class enabling the iteration over the children nodes of the given paint.
305 * For the Scene-type Paint the children nodes represent the paints pushed into the Scene - the order of the children nodes is the same as the order as they were pushed. For the Picture-type Paint the child node is the read image in one of the supported formats. The Shape-type Paint doesn't have any child nodes.
315 Iterator (Paint* p = nullptr, Paint* c = nullptr);
316 const Paint& operator*() const;
317 Iterator& operator++();
318 Iterator operator++(int);
319 friend bool operator!=(const Iterator& it1, const Iterator& it2)
321 return it1.child != it2.child;
323 friend bool operator==(const Iterator& it1, const Iterator& it2)
325 return it1.child == it2.child;
330 * @brief Gets the iterator to the first child node.
332 * @return The iterator pointing to the first element of the children nodes of the given paint object.
336 Iterator begin() const noexcept;
339 * @brief Gets the iterator to the past-the end child node.
341 * @return The iterator referring to the past-the-end element of the children nodes of the given paint object.
345 Iterator end() const noexcept;
348 * @brief Gets the composition target object and the composition method.
350 * @param[out] target The paint of the target object.
352 * @return The method used to composite the source object with the target.
356 CompositeMethod composite(const Paint** target) const noexcept;
358 _TVG_DECLARE_ACCESSOR();
359 _TVG_DECALRE_IDENTIFIER();
360 _TVG_DECLARE_PRIVATE(Paint);
367 * @brief An abstract class representing the gradient fill of the Shape object.
369 * It contains the information about the gradient colors and their arrangement
370 * inside the gradient bounds. The gradients bounds are defined in the LinearGradient
371 * or RadialGradient class, depending on the type of the gradient to be used.
372 * It specifies the gradient behavior in case the area defined by the gradient bounds
373 * is smaller than the area to be filled.
375 class TVG_EXPORT Fill
379 * @brief A data structure storing the information about the color and its relative position inside the gradient bounds.
383 float offset; /**< The relative position of the color. */
384 uint8_t r; /**< The red color channel value in the range [0 ~ 255]. */
385 uint8_t g; /**< The green color channel value in the range [0 ~ 255]. */
386 uint8_t b; /**< The blue color channel value in the range [0 ~ 255]. */
387 uint8_t a; /**< The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. */
393 * @brief Sets the parameters of the colors of the gradient and their position.
395 * @param[in] colorStops An array of ColorStop data structure.
396 * @param[in] cnt The count of the @p colorStops array equal to the colors number used in the gradient.
398 * @return Result::Success when succeed.
400 Result colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept;
403 * @brief Sets the FillSpread value, which specifies how to fill the area outside the gradient bounds.
405 * @param[in] s The FillSpread value.
407 * @return Result::Success when succeed.
409 Result spread(FillSpread s) noexcept;
412 * @brief Gets the parameters of the colors of the gradient, their position and number.
414 * @param[in] colorStops A pointer to the memory location, where the array of the gradient's ColorStop is stored.
416 * @return The number of colors used in the gradient. This value corresponds to the length of the @p colorStops array.
418 uint32_t colorStops(const ColorStop** colorStops) const noexcept;
421 * @brief Gets the FillSpread value of the fill.
423 * @return The FillSpread value of this Fill.
425 FillSpread spread() const noexcept;
428 * @brief Creates a copy of the Fill object.
430 * Return a newly created Fill object with the properties copied from the original.
432 * @return A copied Fill object when succeed, @c nullptr otherwise.
434 Fill* duplicate() const noexcept;
436 _TVG_DECALRE_IDENTIFIER();
437 _TVG_DECLARE_PRIVATE(Fill);
444 * @brief An abstract class for drawing graphical elements.
446 * 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.
448 * @note A Canvas behavior depends on the raster engine though the final content of the buffer is expected to be identical.
449 * @warning The Paint objects belonging to one Canvas can't be shared among multiple Canvases.
451 class TVG_EXPORT Canvas
454 Canvas(RenderMethod*);
458 * @brief Sets the size of the container, where all the paints pushed into the Canvas are stored.
460 * If the number of objects pushed into the Canvas is known in advance, calling the function
461 * prevents multiple memory reallocation, thus improving the performance.
463 * @param[in] n The number of objects for which the memory is to be reserved.
465 * @return Result::Success when succeed.
467 Result reserve(uint32_t n) noexcept;
470 * @brief Passes drawing elements to the Canvas using Paint objects.
472 * Only pushed paints in the canvas will be drawing targets.
473 * They are retained by the canvas until you call Canvas::clear().
474 * If you know the number of the pushed objects in the advance, please call Canvas::reserve().
476 * @param[in] paint A Paint object to be drawn.
478 * @retval Result::Success When succeed.
479 * @retval Result::MemoryCorruption In case a @c nullptr is passed as the argument.
480 * @retval Result::InsufficientCondition An internal error.
482 * @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.
483 * @see Canvas::reserve()
484 * @see Canvas::clear()
486 virtual Result push(std::unique_ptr<Paint> paint) noexcept;
489 * @brief Sets the total number of the paints pushed into the canvas to be zero.
490 * Depending on the value of the @p free argument, the paints are freed or not.
492 * @param[in] free If @c true, the memory occupied by paints is deallocated, otherwise it is not.
494 * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
496 * @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.
498 virtual Result clear(bool free = true) noexcept;
501 * @brief Request the canvas to update the paint objects.
503 * If a @c nullptr is passed all paint objects retained by the Canvas are updated,
504 * otherwise only the paint to which the given @p paint points.
506 * @param[in] paint A pointer to the Paint object or @c nullptr.
508 * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
510 * @note The Update behavior can be asynchronous if the assigned thread number is greater than zero.
512 virtual Result update(Paint* paint) noexcept;
515 * @brief Request the canvas to draw the Paint objects.
517 * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
519 * @note Drawing can be asynchronous if the assigned thread number is greater than zero. To guarantee the drawing is done, call sync() afterwards.
520 * @see Canvas::sync()
522 virtual Result draw() noexcept;
525 * @brief Guarantees that drawing task is finished.
527 * The Canvas rendering can be performed asynchronously. To make sure that rendering is finished,
528 * the sync() must be called after the draw() regardless of threading.
530 * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
531 * @see Canvas::draw()
533 virtual Result sync() noexcept;
535 _TVG_DECLARE_PRIVATE(Canvas);
540 * @class LinearGradient
542 * @brief A class representing the linear gradient fill of the Shape object.
544 * Besides the APIs inherited from the Fill class, it enables setting and getting the linear gradient bounds.
545 * The behavior outside the gradient bounds depends on the value specified in the spread API.
547 class TVG_EXPORT LinearGradient final : public Fill
553 * @brief Sets the linear gradient bounds.
555 * The bounds of the linear gradient are defined as a surface constrained by two parallel lines crossing
556 * the given points (@p x1, @p y1) and (@p x2, @p y2), respectively. Both lines are perpendicular to the line linking
557 * (@p x1, @p y1) and (@p x2, @p y2).
559 * @param[in] x1 The horizontal coordinate of the first point used to determine the gradient bounds.
560 * @param[in] y1 The vertical coordinate of the first point used to determine the gradient bounds.
561 * @param[in] x2 The horizontal coordinate of the second point used to determine the gradient bounds.
562 * @param[in] y2 The vertical coordinate of the second point used to determine the gradient bounds.
564 * @return Result::Success when succeed, Result::InvalidArguments otherwise.
566 Result linear(float x1, float y1, float x2, float y2) noexcept;
569 * @brief Gets the linear gradient bounds.
571 * The bounds of the linear gradient are defined as a surface constrained by two parallel lines crossing
572 * the given points (@p x1, @p y1) and (@p x2, @p y2), respectively. Both lines are perpendicular to the line linking
573 * (@p x1, @p y1) and (@p x2, @p y2).
575 * @param[out] x1 The horizontal coordinate of the first point used to determine the gradient bounds.
576 * @param[out] y1 The vertical coordinate of the first point used to determine the gradient bounds.
577 * @param[out] x2 The horizontal coordinate of the second point used to determine the gradient bounds.
578 * @param[out] y2 The vertical coordinate of the second point used to determine the gradient bounds.
580 * @return Result::Success when succeed.
582 Result linear(float* x1, float* y1, float* x2, float* y2) const noexcept;
585 * @brief Creates a new LinearGradient object.
587 * @return A new LinearGradient object.
589 static std::unique_ptr<LinearGradient> gen() noexcept;
591 _TVG_DECLARE_PRIVATE(LinearGradient);
596 * @class RadialGradient
598 * @brief A class representing the radial gradient fill of the Shape object.
601 class TVG_EXPORT RadialGradient final : public Fill
607 * @brief Sets the radial gradient bounds.
609 * The radial gradient bounds are defined as a circle centered in a given point (@p cx, @p cy) of a given radius.
611 * @param[in] cx The horizontal coordinate of the center of the bounding circle.
612 * @param[in] cy The vertical coordinate of the center of the bounding circle.
613 * @param[in] radius The radius of the bounding circle.
615 * @return Result::Success when succeed, Result::InvalidArguments otherwise.
617 Result radial(float cx, float cy, float radius) noexcept;
620 * @brief Gets the radial gradient bounds.
622 * The radial gradient bounds are defined as a circle centered in a given point (@p cx, @p cy) of a given radius.
624 * @param[out] cx The horizontal coordinate of the center of the bounding circle.
625 * @param[out] cy The vertical coordinate of the center of the bounding circle.
626 * @param[out] radius The radius of the bounding circle.
628 * @return Result::Success when succeed.
630 Result radial(float* cx, float* cy, float* radius) const noexcept;
633 * @brief Creates a new RadialGradient object.
635 * @return A new RadialGradient object.
637 static std::unique_ptr<RadialGradient> gen() noexcept;
639 _TVG_DECLARE_PRIVATE(RadialGradient);
646 * @brief A class representing two-dimensional figures and their properties.
648 * A shape has three major properties: shape outline, stroking, filling. The outline in the Shape is retained as the path.
649 * Path can be composed by accumulating primitive commands such as moveTo(), lineTo(), cubicTo(), or complete shape interfaces such as appendRect(), appendCircle(), etc.
650 * Path can consists of sub-paths. One sub-path is determined by a close command.
652 * The stroke of Shape is an optional property in case the Shape needs to be represented with/without the outline borders.
653 * 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.
655 class TVG_EXPORT Shape final : public Paint
661 * @brief Resets the properties of the shape path.
663 * The color, the fill and the stroke properties are retained.
665 * @return Result::Success when succeed.
667 * @note The memory, where the path data is stored, is not deallocated at this stage for caching effect.
669 Result reset() noexcept;
672 * @brief Sets the initial point of the sub-path.
674 * The value of the current point is set to the given point.
676 * @param[in] x The horizontal coordinate of the initial point of the sub-path.
677 * @param[in] y The vertical coordinate of the initial point of the sub-path.
679 * @return Result::Success when succeed.
681 Result moveTo(float x, float y) noexcept;
684 * @brief Adds a new point to the sub-path, which results in drawing a line from the current point to the given end-point.
686 * The value of the current point is set to the given end-point.
688 * @param[in] x The horizontal coordinate of the end-point of the line.
689 * @param[in] y The vertical coordinate of the end-point of the line.
691 * @return Result::Success when succeed.
693 * @note In case this is the first command in the path, it corresponds to the moveTo() call.
695 Result lineTo(float x, float y) noexcept;
698 * @brief Adds new points to the sub-path, which results in drawing a cubic Bezier curve starting
699 * 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).
701 * The value of the current point is set to the given end-point.
703 * @param[in] cx1 The horizontal coordinate of the 1st control point.
704 * @param[in] cy1 The vertical coordinate of the 1st control point.
705 * @param[in] cx2 The horizontal coordinate of the 2nd control point.
706 * @param[in] cy2 The vertical coordinate of the 2nd control point.
707 * @param[in] x The horizontal coordinate of the end-point of the curve.
708 * @param[in] y The vertical coordinate of the end-point of the curve.
710 * @return Result::Success when succeed.
712 * @note In case this is the first command in the path, no data from the path are rendered.
714 Result cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept;
717 * @brief Closes the current sub-path by drawing a line from the current point to the initial point of the sub-path.
719 * The value of the current point is set to the initial point of the closed sub-path.
721 * @return Result::Success when succeed.
723 * @note In case the sub-path does not contain any points, this function has no effect.
725 Result close() noexcept;
728 * @brief Appends a rectangle to the path.
730 * The rectangle with rounded corners can be achieved by setting non-zero values to @p rx and @p ry arguments.
731 * The @p rx and @p ry values specify the radii of the ellipse defining the rounding of the corners.
733 * The position of the rectangle is specified by the coordinates of its upper left corner - @p x and @p y arguments.
735 * The rectangle is treated as a new sub-path - it is not connected with the previous sub-path.
737 * The value of the current point is set to (@p x + @p rx, @p y) - in case @p rx is greater
738 * than @p w/2 the current point is set to (@p x + @p w/2, @p y)
740 * @param[in] x The horizontal coordinate of the upper left corner of the rectangle.
741 * @param[in] y The vertical coordinate of the upper left corner of the rectangle.
742 * @param[in] w The width of the rectangle.
743 * @param[in] h The height of the rectangle.
744 * @param[in] rx The x-axis radius of the ellipse defining the rounded corners of the rectangle.
745 * @param[in] ry The y-axis radius of the ellipse defining the rounded corners of the rectangle.
747 * @return Result::Success when succeed.
749 * @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.
751 Result appendRect(float x, float y, float w, float h, float rx, float ry) noexcept;
754 * @brief Appends an ellipse to the path.
756 * The position of the ellipse is specified by the coordinates of its center - @p cx and @p cy arguments.
758 * The ellipse is treated as a new sub-path - it is not connected with the previous sub-path.
760 * The value of the current point is set to (@p cx, @p cy - @p ry).
762 * @param[in] cx The horizontal coordinate of the center of the ellipse.
763 * @param[in] cy The vertical coordinate of the center of the ellipse.
764 * @param[in] rx The x-axis radius of the ellipse.
765 * @param[in] ry The y-axis radius of the ellipse.
767 * @return Result::Success when succeed.
769 Result appendCircle(float cx, float cy, float rx, float ry) noexcept;
772 * @brief Appends a circular arc to the path.
774 * The arc is treated as a new sub-path - it is not connected with the previous sub-path.
775 * 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.
777 * @param[in] cx The horizontal coordinate of the center of the arc.
778 * @param[in] cy The vertical coordinate of the center of the arc.
779 * @param[in] radius The radius of the arc.
780 * @param[in] startAngle The start angle of the arc given in degrees, measured counter-clockwise from the horizontal line.
781 * @param[in] sweep The central angle of the arc given in degrees, measured counter-clockwise from @p startAngle.
782 * @param[in] pie Specifies whether to draw radii from the arc's center to both of its end-point - drawn if @c true.
784 * @return Result::Success when succeed.
786 * @note Setting @p sweep value greater than 360 degrees, is equivalent to calling appendCircle(cx, cy, radius, radius).
788 Result appendArc(float cx, float cy, float radius, float startAngle, float sweep, bool pie) noexcept;
791 * @brief Appends a given sub-path to the path.
793 * The current point value is set to the last point from the sub-path.
794 * For each command from the @p cmds array, an appropriate number of points in @p pts array should be specified.
796 * @param[in] cmds The array of the commands in the sub-path.
797 * @param[in] cmdCnt The number of the sub-path's commands.
798 * @param[in] pts The array of the two-dimensional points.
799 * @param[in] ptsCnt The number of the points in the @p pts array.
801 * @return Result::Success when succeed, Result::InvalidArguments otherwise.
803 * @note The interface is designed for optimal path setting if the caller has a completed path commands already.
805 Result appendPath(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept;
808 * @brief Sets the stroke width for all of the figures from the path.
810 * @param[in] width The width of the stroke. The default value is 0.
812 * @return Result::Success when succeed, Result::FailedAllocation otherwise.
814 Result stroke(float width) noexcept;
817 * @brief Sets the color of the stroke for all of the figures from the path.
819 * @param[in] r The red color channel value in the range [0 ~ 255]. The default value is 0.
820 * @param[in] g The green color channel value in the range [0 ~ 255]. The default value is 0.
821 * @param[in] b The blue color channel value in the range [0 ~ 255]. The default value is 0.
822 * @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.
824 * @return Result::Success when succeed, Result::FailedAllocation otherwise.
826 Result stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
829 * @brief Sets the gradient fill of the stroke for all of the figures from the path.
831 * @param[in] f The gradient fill.
833 * @retval Result::Success When succeed.
834 * @retval Result::FailedAllocation An internal error with a memory allocation for an object to be filled.
835 * @retval Result::MemoryCorruption In case a @c nullptr is passed as the argument.
837 Result stroke(std::unique_ptr<Fill> f) noexcept;
840 * @brief Sets the dash pattern of the stroke.
842 * @param[in] dashPattern The array of consecutive pair values of the dash length and the gap length.
843 * @param[in] cnt The length of the @p dashPattern array.
845 * @retval Result::Success When succeed.
846 * @retval Result::FailedAllocation An internal error with a memory allocation for an object to be dashed.
847 * @retval Result::InvalidArguments In case that either @p dashPattern is @c nullptr or @p cnt is zero.
849 * @note If any of the dash pattern values is zero, this function has no effect.
850 * @note To reset the stroke dash pattern, pass @c nullptr to @p dashPattern and zero to @p cnt.
851 * @warning @p cnt must be greater than 1 if the dash pattern is valid.
853 Result stroke(const float* dashPattern, uint32_t cnt) noexcept;
856 * @brief Sets the cap style of the stroke in the open sub-paths.
858 * @param[in] cap The cap style value. The default value is @c StrokeCap::Square.
860 * @return Result::Success when succeed, Result::FailedAllocation otherwise.
862 Result stroke(StrokeCap cap) noexcept;
865 * @brief Sets the join style for stroked path segments.
867 * The join style is used for joining the two line segment while stroking the path.
869 * @param[in] join The join style value. The default value is @c StrokeJoin::Bevel.
871 * @return Result::Success when succeed, Result::FailedAllocation otherwise.
873 Result stroke(StrokeJoin join) noexcept;
876 * @brief Sets the solid color for all of the figures from the path.
878 * The parts of the shape defined as inner are colored.
880 * @param[in] r The red color channel value in the range [0 ~ 255]. The default value is 0.
881 * @param[in] g The green color channel value in the range [0 ~ 255]. The default value is 0.
882 * @param[in] b The blue color channel value in the range [0 ~ 255]. The default value is 0.
883 * @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.
885 * @return Result::Success when succeed.
887 * @note Either a solid color or a gradient fill is applied, depending on what was set as last.
889 Result fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
892 * @brief Sets the gradient fill for all of the figures from the path.
894 * The parts of the shape defined as inner are filled.
896 * @param[in] f The unique pointer to the gradient fill.
898 * @return Result::Success when succeed, Result::MemoryCorruption otherwise.
900 * @note Either a solid color or a gradient fill is applied, depending on what was set as last.
902 Result fill(std::unique_ptr<Fill> f) noexcept;
905 * @brief Sets the fill rule for the Shape object.
907 * @param[in] r The fill rule value.
909 * @return Result::Success when succeed.
911 Result fill(FillRule r) noexcept;
914 * @brief Gets the commands data of the path.
916 * @param[out] cmds The pointer to the array of the commands from the path.
918 * @return The length of the @p cmds array when succeed, zero otherwise.
920 uint32_t pathCommands(const PathCommand** cmds) const noexcept;
923 * @brief Gets the points values of the path.
925 * @param[out] pts The pointer to the array of the two-dimensional points from the path.
927 * @return The length of the @p pts array when succeed, zero otherwise.
929 uint32_t pathCoords(const Point** pts) const noexcept;
932 * @brief Gets the pointer to the gradient fill of the shape.
934 * @return The pointer to the gradient fill of the stroke when succeed, @c nullptr in case no fill was set.
936 const Fill* fill() const noexcept;
939 * @brief Gets the solid color of the shape.
941 * @param[out] r The red color channel value in the range [0 ~ 255].
942 * @param[out] g The green color channel value in the range [0 ~ 255].
943 * @param[out] b The blue color channel value in the range [0 ~ 255].
944 * @param[out] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque.
946 * @return Result::Success when succeed.
948 Result fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
951 * @brief Gets the fill rule value.
953 * @return The fill rule value of the shape.
955 FillRule fillRule() const noexcept;
958 * @brief Gets the stroke width.
960 * @return The stroke width value when succeed, zero if no stroke was set.
962 float strokeWidth() const noexcept;
965 * @brief Gets the color of the shape's stroke.
967 * @param[out] r The red color channel value in the range [0 ~ 255].
968 * @param[out] g The green color channel value in the range [0 ~ 255].
969 * @param[out] b The blue color channel value in the range [0 ~ 255].
970 * @param[out] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque.
972 * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
974 Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
977 * @brief Gets the pointer to the gradient fill of the stroke.
979 * @return The pointer to the gradient fill of the stroke when succeed, @c nullptr otherwise.
981 const Fill* strokeFill() const noexcept;
984 * @brief Gets the dash pattern of the stroke.
986 * @param[out] dashPattern The pointer to the memory, where the dash pattern array is stored.
988 * @return The length of the @p dashPattern array.
990 uint32_t strokeDash(const float** dashPattern) const noexcept;
993 * @brief Gets the cap style used for stroking the path.
995 * @return The cap style value of the stroke.
997 StrokeCap strokeCap() const noexcept;
1000 * @brief Gets the join style value used for stroking the path.
1002 * @return The join style value of the stroke.
1004 StrokeJoin strokeJoin() const noexcept;
1007 * @brief Creates a new Shape object.
1009 * @return A new Shape object.
1011 static std::unique_ptr<Shape> gen() noexcept;
1013 _TVG_DECLARE_PRIVATE(Shape);
1020 * @brief A class representing an image read in one of the supported formats: raw, svg, png and etc.
1021 * Besides the methods inherited from the Paint, it provides methods to load & draw images on the canvas.
1023 * @note Supported formats are depended on the available TVG loaders.
1025 class TVG_EXPORT Picture final : public Paint
1031 * @brief Loads a picture data directly from a file.
1033 * @param[in] path A path to the picture file.
1035 * @retval Result::Success When succeed.
1036 * @retval Result::InvalidArguments In case the @p path is invalid.
1037 * @retval Result::NonSupport When trying to load a file with an unknown extension.
1038 * @retval Result::Unknown If an error occurs at a later stage.
1040 * @note The Load behavior can be asynchronous if the assigned thread number is greater than zero.
1041 * @see Initializer::init()
1043 Result load(const std::string& path) noexcept;
1046 * @brief Loads a picture data from a memory block of a given size.
1048 * @param[in] data A pointer to a memory location where the content of the picture file is stored.
1049 * @param[in] size The size in bytes of the memory occupied by the @p data.
1050 * @param[in] copy Decides whether the data should be copied into the engine local buffer.
1052 * @retval Result::Success When succeed.
1053 * @retval Result::InvalidArguments In case no data are provided or the @p size is zero or less.
1054 * @retval Result::NonSupport When trying to load a file with an unknown extension.
1055 * @retval Result::Unknown If an error occurs at a later stage.
1057 * @note: This api supports only SVG format
1059 * @warning: you have responsibility to release the @p data memory if the @p copy is true
1061 Result load(const char* data, uint32_t size, bool copy) noexcept;
1062 Result load(const char* data, uint32_t size) noexcept;
1065 * @brief Resize the picture content with the given width and height.
1067 * Resize the picture content while keeping the default size aspect ratio.
1068 * The scaling factor is established for each of dimensions and the smaller value is applied to both of them.
1070 * @param[in] w A new width of the image in pixels.
1071 * @param[in] h A new height of the image in pixels.
1073 * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
1075 Result size(float w, float h) noexcept;
1078 * @brief Gets the size of the image.
1080 * @param[out] w The width of the image in pixels.
1081 * @param[out] h The height of the image in pixels.
1083 * @return Result::Success when succeed.
1085 Result size(float* w, float* h) const noexcept;
1088 * @brief Gets the pixels information of the picture.
1090 * @warning Please do not use it, this API is not official one. It could be modified in the next version.
1094 const uint32_t* data() const noexcept;
1097 * @brief Set paint for the picture.
1099 * @param[in] paint A Paint object to be drawn.
1101 * @return Result::Success when succeed.
1102 * @return Result::InsufficientCondition if paint already set.
1103 * @return Result::MemoryCorruption when bad memory handling.
1105 * @warning Please do not use it, this API is not official one. It could be modified in the next version.
1109 Result paint(std::unique_ptr<Paint> paint) noexcept;
1112 * @brief Loads a raw data from a memory block with a given size.
1114 * @warning Please do not use it, this API is not official one. It could be modified in the next version.
1118 Result load(uint32_t* data, uint32_t w, uint32_t h, bool copy) noexcept;
1121 * @brief Gets the position and the size of the loaded picture.
1123 * @warning Please do not use it, this API is not official one. It could be modified in the next version.
1127 Result viewbox(float* x, float* y, float* w, float* h) const noexcept;
1130 * @brief Creates a new Picture object.
1132 * @return A new Picture object.
1134 static std::unique_ptr<Picture> gen() noexcept;
1136 _TVG_DECLARE_PRIVATE(Picture);
1143 * @brief A class to composite children paints.
1145 * As the traditional graphics rendering method, TVG also enables scene-graph mechanism.
1146 * This feature supports an array function for managing the multiple paints as one group paint.
1148 * As a group, the scene can be transformed, made translucent and composited with other target paints,
1149 * its children will be affected by the scene world.
1151 class TVG_EXPORT Scene final : public Paint
1157 * @brief Passes drawing elements to the Scene using Paint objects.
1159 * Only pushed paints in the scene will be drawing targets.
1160 * They are retained by the scene until you call Scene::clear().
1161 * If you know the number of the pushed objects in the advance, please call Scene::reserve().
1163 * @param[in] paint A Paint object to be drawn.
1165 * @return Result::Success when succeed, Result::MemoryCorruption otherwise.
1167 * @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.
1168 * @see Scene::reserve()
1170 Result push(std::unique_ptr<Paint> paint) noexcept;
1173 * @brief Sets the size of the container, where all the paints pushed into the Scene are stored.
1175 * If the number of objects pushed into the scene is known in advance, calling the function
1176 * prevents multiple memory reallocation, thus improving the performance.
1178 * @param[in] size The number of objects for which the memory is to be reserved.
1180 * @return Result::Success when succeed.
1182 Result reserve(uint32_t size) noexcept;
1185 * @brief Sets the total number of the paints pushed into the scene to be zero.
1186 * Depending on the value of the @p free argument, the paints are freed or not.
1188 * @param[in] free If @c true, the memory occupied by paints is deallocated, otherwise it is not.
1190 * @return Result::Success when succeed
1192 * @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.
1193 * @warning Please do not use it, this API is not official one. It could be modified in the next version.
1197 Result clear(bool free = true) noexcept;
1200 * @brief Creates a new Scene object.
1202 * @return A new Scene object.
1204 static std::unique_ptr<Scene> gen() noexcept;
1206 _TVG_DECLARE_PRIVATE(Scene);
1213 * @brief A class for the rendering graphical elements with a software raster engine.
1215 class TVG_EXPORT SwCanvas final : public Canvas
1221 * @brief Enumeration specifying the methods of combining the 8-bit color channels into 32-bit color.
1225 ABGR8888 = 0, ///< The channels are joined in the order: alpha, blue, green, red.
1226 ARGB8888 ///< The channels are joined in the order: alpha, red, green, blue.
1230 * @brief Enumeration specifying the methods of Memory Pool behavior policy.
1236 Default = 0, ///< Default behavior that ThorVG is designed to.
1237 Shareable, ///< Memory Pool is shared among the SwCanvases.
1238 Individual ///< Allocate designated memory pool that is only used by current instance.
1242 * @brief Sets the target buffer for the rasterization.
1244 * The buffer of a desirable size should be allocated and owned by the caller.
1246 * @param[in] buffer A pointer to a memory block of the size @p stride x @p h, where the raster data are stored.
1247 * @param[in] stride The stride of the raster image - greater than or equal to @p w.
1248 * @param[in] w The width of the raster image.
1249 * @param[in] h The height of the raster image.
1250 * @param[in] cs The value specifying the way the 32-bits colors should be read/written.
1252 * @retval Result::Success When succeed.
1253 * @retval Result::MemoryCorruption When casting in the internal function implementation failed.
1254 * @retval Result::InvalidArguments In case no valid pointer is provided or the width, or the height or the stride is zero.
1255 * @retval Result::NonSupport In case the software engine is not supported.
1257 * @warning Do not access @p buffer during Canvas::draw() - Canvas::sync(). It should not be accessed while TVG is writing on it.
1259 Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, Colorspace cs) noexcept;
1262 * @brief Set sw engine memory pool behavior policy.
1264 * Basically ThorVG draws a lot of shapes, it allocates/deallocates a few chunk of memory
1265 * while processing rendering. It internally uses one shared memory pool
1266 * which can be reused among the canvases in order to avoid memory overhead.
1268 * Thus ThorVG suggests memory pool policy to satisfy user demands,
1269 * if it needs to guarantee the thread-safety of the internal data access.
1271 * @param[in] policy Use the shared cache memory. The default value is @c true
1273 * @retval Result::Success When succeed.
1274 * @retval Result::InsufficientCondition If the canvas has any paints.
1275 * @retval Result::NonSupport In case the software engine is not supported.
1277 * @note When @c policy is set as @c MempoolPolicy::Individual, current instance of canvas uses its own individual
1278 * memory data that is not shared with others. This is necessary when the canvas is accessed on a worker-thread.
1280 * @warning It's not allowed after pushing any paints.
1284 Result mempool(MempoolPolicy policy) noexcept;
1287 * @brief Creates a new SwCanvas object.
1288 * @return A new SwCanvas object.
1290 static std::unique_ptr<SwCanvas> gen() noexcept;
1292 _TVG_DECLARE_PRIVATE(SwCanvas);
1299 * @brief A class for the rendering graphic elements with a GL raster engine.
1301 * @warning Please do not use it. This class is not fully supported yet.
1305 class TVG_EXPORT GlCanvas final : public Canvas
1311 * @brief Sets the target buffer for the rasterization.
1313 * @warning Please do not use it, this API is not official one. It could be modified in the next version.
1317 Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h) noexcept;
1320 * @brief Creates a new GlCanvas object.
1322 * @return A new GlCanvas object.
1326 static std::unique_ptr<GlCanvas> gen() noexcept;
1328 _TVG_DECLARE_PRIVATE(GlCanvas);
1333 * @class Initializer
1335 * @brief A class that enables initialization and termination of the TVG engines.
1337 class TVG_EXPORT Initializer final
1341 * @brief Initializes TVG engines.
1343 * TVG requires the running-engine environment.
1344 * TVG runs its own task-scheduler for parallelizing rendering tasks efficiently.
1345 * You can indicate the number of threads, the count of which is designated @p threads.
1346 * In the initialization step, TVG will generate/spawn the threads as set by @p threads count.
1348 * @param[in] engine The engine types to initialize. This is relative to the Canvas types, in which it will be used. For multiple backeneds bitwise operation is allowed.
1349 * @param[in] threads The number of additional threads. Zero indicates only the main thread is to be used.
1351 * @retval Result::Success When succeed.
1352 * @retval Result::FailedAllocation An internal error possibly with memory allocation.
1353 * @retval Result::InvalidArguments If unknown engine type chosen.
1354 * @retval Result::NonSupport In case the engine type is not supported on the system.
1355 * @retval Result::Unknown Others.
1357 * @note The Initializer keeps track of the number of times it was called. Threads count is fixed at the first init() call.
1358 * @see Initializer::term()
1360 static Result init(CanvasEngine engine, uint32_t threads) noexcept;
1363 * @brief Terminates TVG engines.
1365 * @param[in] engine The engine types to terminate. This is relative to the Canvas types, in which it will be used. For multiple backeneds bitwise operation is allowed
1367 * @retval Result::Success When succeed.
1368 * @retval Result::InsufficientCondition In case there is nothing to be terminated.
1369 * @retval Result::InvalidArguments If unknown engine type chosen.
1370 * @retval Result::NonSupport In case the engine type is not supported on the system.
1371 * @retval Result::Unknown Others.
1373 * @note Initializer does own reference counting for multiple calls.
1374 * @see Initializer::init()
1376 static Result term(CanvasEngine engine) noexcept;
1378 _TVG_DISABLE_CTOR(Initializer);
1385 * @brief A class enabling saving a paint in a binary format.
1389 class TVG_EXPORT Saver
1395 * @brief Saves all the paints from the tree in a binary format.
1397 * @param[in] paint The root paint to be saved with all its nodes.
1398 * @param[in] path A path to the file, in which the data is to be saved.
1400 * @retval Result::Success When succeed.
1401 * @retval Result::InvalidArguments the @p path is invalid.
1402 * @retval Result::FailedAllocation An internal error with a memory allocation for the Saver object.
1403 * @retval Result::MemoryCorruption When casting in the internal function implementation failed.
1404 * @retval Result::Unknown Others.
1407 Result save(std::unique_ptr<Paint> paint, const std::string& path) noexcept;
1408 Result sync() noexcept;
1410 static std::unique_ptr<Saver> gen() noexcept;
1412 _TVG_DECLARE_PRIVATE(Saver);