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() \
50 #define _TVG_DECALRE_IDENTIFIER() \
51 auto id() const { return _id; } \
65 * @defgroup ThorVG ThorVG
66 * @brief ThorVG classes and enumerations providing C++ APIs.
72 * @brief Enumeration specifying the result from the APIs.
74 enum class TVG_EXPORT Result
76 Success = 0, ///< The value returned in case of a correct request execution.
77 InvalidArguments, ///< The value returned in the event of a problem with the arguments given to the API - e.g. empty paths or null pointers.
78 InsufficientCondition, ///< The value returned in case the request cannot be processed - e.g. asking for properties of an object, which does not exist.
79 FailedAllocation, ///< The value returned in case of unsuccessful memory allocation.
80 MemoryCorruption, ///< The value returned in the event of bad memory handling - e.g. failing in pointer releasing or casting
81 NonSupport, ///< The value returned in case of choosing unsupported options.
82 Unknown ///< The value returned in all other cases.
86 * @brief Enumeration specifying the values of the path commands accepted by TVG.
88 * Not to be confused with the path commands from the svg path element (like M, L, Q, H and many others).
89 * TVG interprets all of them and translates to the ones from the PathCommand values.
91 enum class TVG_EXPORT PathCommand
93 Close = 0, ///< Ends the current sub-path and connects it with its initial point. This command doesn't expect any points.
94 MoveTo, ///< Sets a new initial point of the sub-path and a new current point. This command expects 1 point: the starting position.
95 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.
96 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.
100 * @brief Enumeration determining the ending type of a stroke in the open sub-paths.
102 enum class TVG_EXPORT StrokeCap
104 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.
105 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.
106 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.
110 * @brief Enumeration determining the style used at the corners of joined stroked path segments.
112 enum class TVG_EXPORT StrokeJoin
114 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.
115 Round, ///< The outer corner of the joined path segments is rounded. The circular region is centered at the join point.
116 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.
120 * @brief Enumeration specifying how to fill the area outside the gradient bounds.
122 enum class TVG_EXPORT FillSpread
124 Pad = 0, ///< The remaining area is filled with the closest stop color.
125 Reflect, ///< The gradient pattern is reflected outside the gradient area until the expected region is filled.
126 Repeat ///< The gradient pattern is repeated continuously beyond the gradient area until the expected region is filled.
130 * @brief Enumeration specifying the algorithm used to establish which parts of the shape are treated as the inside of the shape.
132 enum class TVG_EXPORT FillRule
134 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.
135 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.
139 * @brief Enumeration indicating the method used in the composition of two objects - the target and the source.
141 enum class TVG_EXPORT CompositeMethod
143 None = 0, ///< No composition is applied.
144 ClipPath, ///< The intersection of the source and the target is determined and only the resulting pixels from the source are rendered.
145 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.
146 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.
150 * @brief Enumeration specifying the engine type used for the graphics backend. For multiple backeneds bitwise operation is allowed.
152 enum class TVG_EXPORT CanvasEngine
154 Sw = (1 << 1), ///< CPU rasterizer.
155 Gl = (1 << 2) ///< OpenGL rasterizer.
160 * @brief A data structure representing a point in two-dimensional space.
169 * @brief A data structure representing a three-dimensional matrix.
171 * The elements e11, e12, e21 and e22 represent the rotation matrix, including the scaling factor.
172 * The elements e13 and e23 determine the translation of the object along the x and y-axis, respectively.
173 * The elements e31 and e32 are set to 0, e33 is set to 1.
186 * @brief An abstract class for managing graphical elements.
188 * A graphical element in TVG is any object composed into a Canvas.
189 * Paint represents such a graphical object and its behaviors such as duplication, transformation and composition.
190 * 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.
192 class TVG_EXPORT Paint
198 * @brief Sets the angle by which the object is rotated.
200 * The angle in measured clockwise from the horizontal axis.
201 * The rotational axis passes through the point on the object with zero coordinates.
203 * @param[in] degree The value of the angle in degrees.
205 * @return Result::Success when succeed, Result::FailedAllocation otherwise.
207 Result rotate(float degree) noexcept;
210 * @brief Sets the scale value of the object.
212 * @param[in] factor The value of the scaling factor. The default value is 1.
214 * @return Result::Success when succeed, Result::FailedAllocation otherwise.
216 Result scale(float factor) noexcept;
219 * @brief Sets the values by which the object is moved in a two-dimensional space.
221 * The origin of the coordinate system is in the upper left corner of the canvas.
222 * The horizontal and vertical axes point to the right and down, respectively.
224 * @param[in] x The value of the horizontal shift.
225 * @param[in] y The value of the vertical shift.
227 * @return Result::Success when succeed, Result::FailedAllocation otherwise.
229 Result translate(float x, float y) noexcept;
232 * @brief Sets the matrix of the affine transformation for the object.
234 * The augmented matrix of the transformation is expected to be given.
236 * @param[in] m The 3x3 augmented matrix.
238 * @return Result::Success when succeed, Result::FailedAllocation otherwise.
240 Result transform(const Matrix& m) noexcept;
243 * @brief Gets the matrix of the affine transformation of the object.
245 * The values of the matrix can be set by the transform() API, as well by the translate(),
246 * scale() and rotate(). In case no transformation was applied, the identity matrix is returned.
248 * @retval The augmented transformation matrix.
252 Matrix transform() noexcept;
255 * @brief Sets the opacity of the object.
257 * @param[in] o The opacity value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque.
259 * @return Result::Success when succeed.
261 * @note Setting the opacity with this API may require multiple render pass for composition. It is recommended to avoid changing the opacity if possible.
263 Result opacity(uint8_t o) noexcept;
266 * @brief Sets the composition target object and the composition method.
268 * @param[in] target The paint of the target object.
269 * @param[in] method The method used to composite the source object with the target.
271 * @return Result::Success when succeed, Result::InvalidArguments otherwise.
273 Result composite(std::unique_ptr<Paint> target, CompositeMethod method) noexcept;
276 * @brief Gets the bounding box of the paint object before any transformation.
278 * @param[out] x The x coordinate of the upper left corner of the object.
279 * @param[out] y The y coordinate of the upper left corner of the object.
280 * @param[out] w The width of the object.
281 * @param[out] h The height of the object.
283 * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
285 * @note The bounding box doesn't indicate the final rendered region. It's the smallest rectangle that encloses the object.
287 Result bounds(float* x, float* y, float* w, float* h) const noexcept;
290 * @brief Duplicates the object.
292 * Creates a new object and sets its all properties as in the original object.
294 * @return The created object when succeed, @c nullptr otherwise.
296 Paint* duplicate() const noexcept;
299 * @brief Gets the opacity value of the object.
301 * @return The opacity value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque.
303 uint8_t opacity() const noexcept;
306 * @brief Gets the composition target object and the composition method.
308 * @param[out] target The paint of the target object.
310 * @return The method used to composite the source object with the target.
314 CompositeMethod composite(const Paint** target) const noexcept;
316 _TVG_DECLARE_ACCESSOR();
317 _TVG_DECALRE_IDENTIFIER();
318 _TVG_DECLARE_PRIVATE(Paint);
325 * @brief An abstract class representing the gradient fill of the Shape object.
327 * It contains the information about the gradient colors and their arrangement
328 * inside the gradient bounds. The gradients bounds are defined in the LinearGradient
329 * or RadialGradient class, depending on the type of the gradient to be used.
330 * It specifies the gradient behavior in case the area defined by the gradient bounds
331 * is smaller than the area to be filled.
333 class TVG_EXPORT Fill
337 * @brief A data structure storing the information about the color and its relative position inside the gradient bounds.
341 float offset; /**< The relative position of the color. */
342 uint8_t r; /**< The red color channel value in the range [0 ~ 255]. */
343 uint8_t g; /**< The green color channel value in the range [0 ~ 255]. */
344 uint8_t b; /**< The blue color channel value in the range [0 ~ 255]. */
345 uint8_t a; /**< The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. */
351 * @brief Sets the parameters of the colors of the gradient and their position.
353 * @param[in] colorStops An array of ColorStop data structure.
354 * @param[in] cnt The count of the @p colorStops array equal to the colors number used in the gradient.
356 * @return Result::Success when succeed.
358 Result colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept;
361 * @brief Sets the FillSpread value, which specifies how to fill the area outside the gradient bounds.
363 * @param[in] s The FillSpread value.
365 * @return Result::Success when succeed.
367 Result spread(FillSpread s) noexcept;
370 * @brief Gets the parameters of the colors of the gradient, their position and number.
372 * @param[in] colorStops A pointer to the memory location, where the array of the gradient's ColorStop is stored.
374 * @return The number of colors used in the gradient. This value corresponds to the length of the @p colorStops array.
376 uint32_t colorStops(const ColorStop** colorStops) const noexcept;
379 * @brief Gets the FillSpread value of the fill.
381 * @return The FillSpread value of this Fill.
383 FillSpread spread() const noexcept;
386 * @brief Creates a copy of the Fill object.
388 * Return a newly created Fill object with the properties copied from the original.
390 * @return A copied Fill object when succeed, @c nullptr otherwise.
392 Fill* duplicate() const noexcept;
394 _TVG_DECALRE_IDENTIFIER();
395 _TVG_DECLARE_PRIVATE(Fill);
402 * @brief An abstract class for drawing graphical elements.
404 * 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.
406 * @note A Canvas behavior depends on the raster engine though the final content of the buffer is expected to be identical.
407 * @warning The Paint objects belonging to one Canvas can't be shared among multiple Canvases.
409 class TVG_EXPORT Canvas
412 Canvas(RenderMethod*);
416 * @brief Sets the size of the container, where all the paints pushed into the Canvas are stored.
418 * If the number of objects pushed into the Canvas is known in advance, calling the function
419 * prevents multiple memory reallocation, thus improving the performance.
421 * @param[in] n The number of objects for which the memory is to be reserved.
423 * @return Result::Success when succeed.
425 Result reserve(uint32_t n) noexcept;
428 * @brief Passes drawing elements to the Canvas using Paint objects.
430 * Only pushed paints in the canvas will be drawing targets.
431 * They are retained by the canvas until you call Canvas::clear().
432 * If you know the number of the pushed objects in the advance, please call Canvas::reserve().
434 * @param[in] paint A Paint object to be drawn.
436 * @retval Result::Success When succeed.
437 * @retval Result::MemoryCorruption In case a @c nullptr is passed as the argument.
438 * @retval Result::InsufficientCondition An internal error.
440 * @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.
441 * @see Canvas::reserve()
442 * @see Canvas::clear()
444 virtual Result push(std::unique_ptr<Paint> paint) noexcept;
447 * @brief Sets the total number of the paints pushed into the canvas to be zero.
448 * Depending on the value of the @p free argument, the paints are freed or not.
450 * @param[in] free If @c true, the memory occupied by paints is deallocated, otherwise it is not.
452 * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
454 * @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.
456 virtual Result clear(bool free = true) noexcept;
459 * @brief Request the canvas to update the paint objects.
461 * If a @c nullptr is passed all paint objects retained by the Canvas are updated,
462 * otherwise only the paint to which the given @p paint points.
464 * @param[in] paint A pointer to the Paint object or @c nullptr.
466 * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
468 * @note The Update behavior can be asynchronous if the assigned thread number is greater than zero.
470 virtual Result update(Paint* paint) noexcept;
473 * @brief Request the canvas to draw the Paint objects.
475 * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
477 * @note Drawing can be asynchronous if the assigned thread number is greater than zero. To guarantee the drawing is done, call sync() afterwards.
478 * @see Canvas::sync()
480 virtual Result draw() noexcept;
483 * @brief Guarantees that drawing task is finished.
485 * The Canvas rendering can be performed asynchronously. To make sure that rendering is finished,
486 * the sync() must be called after the draw() regardless of threading.
488 * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
489 * @see Canvas::draw()
491 virtual Result sync() noexcept;
493 _TVG_DECLARE_PRIVATE(Canvas);
498 * @class LinearGradient
500 * @brief A class representing the linear gradient fill of the Shape object.
502 * Besides the APIs inherited from the Fill class, it enables setting and getting the linear gradient bounds.
503 * The behavior outside the gradient bounds depends on the value specified in the spread API.
505 class TVG_EXPORT LinearGradient final : public Fill
511 * @brief Sets the linear gradient bounds.
513 * The bounds of the linear gradient are defined as a surface constrained by two parallel lines crossing
514 * the given points (@p x1, @p y1) and (@p x2, @p y2), respectively. Both lines are perpendicular to the line linking
515 * (@p x1, @p y1) and (@p x2, @p y2).
517 * @param[in] x1 The horizontal coordinate of the first point used to determine the gradient bounds.
518 * @param[in] y1 The vertical coordinate of the first point used to determine the gradient bounds.
519 * @param[in] x2 The horizontal coordinate of the second point used to determine the gradient bounds.
520 * @param[in] y2 The vertical coordinate of the second point used to determine the gradient bounds.
522 * @return Result::Success when succeed, Result::InvalidArguments otherwise.
524 Result linear(float x1, float y1, float x2, float y2) noexcept;
527 * @brief Gets the linear gradient bounds.
529 * The bounds of the linear gradient are defined as a surface constrained by two parallel lines crossing
530 * the given points (@p x1, @p y1) and (@p x2, @p y2), respectively. Both lines are perpendicular to the line linking
531 * (@p x1, @p y1) and (@p x2, @p y2).
533 * @param[out] x1 The horizontal coordinate of the first point used to determine the gradient bounds.
534 * @param[out] y1 The vertical coordinate of the first point used to determine the gradient bounds.
535 * @param[out] x2 The horizontal coordinate of the second point used to determine the gradient bounds.
536 * @param[out] y2 The vertical coordinate of the second point used to determine the gradient bounds.
538 * @return Result::Success when succeed.
540 Result linear(float* x1, float* y1, float* x2, float* y2) const noexcept;
543 * @brief Creates a new LinearGradient object.
545 * @return A new LinearGradient object.
547 static std::unique_ptr<LinearGradient> gen() noexcept;
549 _TVG_DECLARE_PRIVATE(LinearGradient);
554 * @class RadialGradient
556 * @brief A class representing the radial gradient fill of the Shape object.
559 class TVG_EXPORT RadialGradient final : public Fill
565 * @brief Sets the radial gradient bounds.
567 * The radial gradient bounds are defined as a circle centered in a given point (@p cx, @p cy) of a given radius.
569 * @param[in] cx The horizontal coordinate of the center of the bounding circle.
570 * @param[in] cy The vertical coordinate of the center of the bounding circle.
571 * @param[in] radius The radius of the bounding circle.
573 * @return Result::Success when succeed, Result::InvalidArguments otherwise.
575 Result radial(float cx, float cy, float radius) noexcept;
578 * @brief Gets the radial gradient bounds.
580 * The radial gradient bounds are defined as a circle centered in a given point (@p cx, @p cy) of a given radius.
582 * @param[out] cx The horizontal coordinate of the center of the bounding circle.
583 * @param[out] cy The vertical coordinate of the center of the bounding circle.
584 * @param[out] radius The radius of the bounding circle.
586 * @return Result::Success when succeed.
588 Result radial(float* cx, float* cy, float* radius) const noexcept;
591 * @brief Creates a new RadialGradient object.
593 * @return A new RadialGradient object.
595 static std::unique_ptr<RadialGradient> gen() noexcept;
597 _TVG_DECLARE_PRIVATE(RadialGradient);
604 * @brief A class representing two-dimensional figures and their properties.
606 * A shape has three major properties: shape outline, stroking, filling. The outline in the Shape is retained as the path.
607 * Path can be composed by accumulating primitive commands such as moveTo(), lineTo(), cubicTo(), or complete shape interfaces such as appendRect(), appendCircle(), etc.
608 * Path can consists of sub-paths. One sub-path is determined by a close command.
610 * The stroke of Shape is an optional property in case the Shape needs to be represented with/without the outline borders.
611 * 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.
613 class TVG_EXPORT Shape final : public Paint
619 * @brief Resets the properties of the shape path.
621 * The color, the fill and the stroke properties are retained.
623 * @return Result::Success when succeed.
625 * @note The memory, where the path data is stored, is not deallocated at this stage for caching effect.
627 Result reset() noexcept;
630 * @brief Sets the initial point of the sub-path.
632 * The value of the current point is set to the given point.
634 * @param[in] x The horizontal coordinate of the initial point of the sub-path.
635 * @param[in] y The vertical coordinate of the initial point of the sub-path.
637 * @return Result::Success when succeed.
639 Result moveTo(float x, float y) noexcept;
642 * @brief Adds a new point to the sub-path, which results in drawing a line from the current point to the given end-point.
644 * The value of the current point is set to the given end-point.
646 * @param[in] x The horizontal coordinate of the end-point of the line.
647 * @param[in] y The vertical coordinate of the end-point of the line.
649 * @return Result::Success when succeed.
651 * @note In case this is the first command in the path, it corresponds to the moveTo() call.
653 Result lineTo(float x, float y) noexcept;
656 * @brief Adds new points to the sub-path, which results in drawing a cubic Bezier curve starting
657 * 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).
659 * The value of the current point is set to the given end-point.
661 * @param[in] cx1 The horizontal coordinate of the 1st control point.
662 * @param[in] cy1 The vertical coordinate of the 1st control point.
663 * @param[in] cx2 The horizontal coordinate of the 2nd control point.
664 * @param[in] cy2 The vertical coordinate of the 2nd control point.
665 * @param[in] x The horizontal coordinate of the end-point of the curve.
666 * @param[in] y The vertical coordinate of the end-point of the curve.
668 * @return Result::Success when succeed.
670 * @note In case this is the first command in the path, no data from the path are rendered.
672 Result cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept;
675 * @brief Closes the current sub-path by drawing a line from the current point to the initial point of the sub-path.
677 * The value of the current point is set to the initial point of the closed sub-path.
679 * @return Result::Success when succeed.
681 * @note In case the sub-path does not contain any points, this function has no effect.
683 Result close() noexcept;
686 * @brief Appends a rectangle to the path.
688 * The rectangle with rounded corners can be achieved by setting non-zero values to @p rx and @p ry arguments.
689 * The @p rx and @p ry values specify the radii of the ellipse defining the rounding of the corners.
691 * The position of the rectangle is specified by the coordinates of its upper left corner - @p x and @p y arguments.
693 * The rectangle is treated as a new sub-path - it is not connected with the previous sub-path.
695 * The value of the current point is set to (@p x + @p rx, @p y) - in case @p rx is greater
696 * than @p w/2 the current point is set to (@p x + @p w/2, @p y)
698 * @param[in] x The horizontal coordinate of the upper left corner of the rectangle.
699 * @param[in] y The vertical coordinate of the upper left corner of the rectangle.
700 * @param[in] w The width of the rectangle.
701 * @param[in] h The height of the rectangle.
702 * @param[in] rx The x-axis radius of the ellipse defining the rounded corners of the rectangle.
703 * @param[in] ry The y-axis radius of the ellipse defining the rounded corners of the rectangle.
705 * @return Result::Success when succeed.
707 * @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.
709 Result appendRect(float x, float y, float w, float h, float rx, float ry) noexcept;
712 * @brief Appends an ellipse to the path.
714 * The position of the ellipse is specified by the coordinates of its center - @p cx and @p cy arguments.
716 * The ellipse is treated as a new sub-path - it is not connected with the previous sub-path.
718 * The value of the current point is set to (@p cx, @p cy - @p ry).
720 * @param[in] cx The horizontal coordinate of the center of the ellipse.
721 * @param[in] cy The vertical coordinate of the center of the ellipse.
722 * @param[in] rx The x-axis radius of the ellipse.
723 * @param[in] ry The y-axis radius of the ellipse.
725 * @return Result::Success when succeed.
727 Result appendCircle(float cx, float cy, float rx, float ry) noexcept;
730 * @brief Appends a circular arc to the path.
732 * The arc is treated as a new sub-path - it is not connected with the previous sub-path.
733 * 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.
735 * @param[in] cx The horizontal coordinate of the center of the arc.
736 * @param[in] cy The vertical coordinate of the center of the arc.
737 * @param[in] radius The radius of the arc.
738 * @param[in] startAngle The start angle of the arc given in degrees, measured counter-clockwise from the horizontal line.
739 * @param[in] sweep The central angle of the arc given in degrees, measured counter-clockwise from @p startAngle.
740 * @param[in] pie Specifies whether to draw radii from the arc's center to both of its end-point - drawn if @c true.
742 * @return Result::Success when succeed.
744 * @note Setting @p sweep value greater than 360 degrees, is equivalent to calling appendCircle(cx, cy, radius, radius).
746 Result appendArc(float cx, float cy, float radius, float startAngle, float sweep, bool pie) noexcept;
749 * @brief Appends a given sub-path to the path.
751 * The current point value is set to the last point from the sub-path.
752 * For each command from the @p cmds array, an appropriate number of points in @p pts array should be specified.
754 * @param[in] cmds The array of the commands in the sub-path.
755 * @param[in] cmdCnt The number of the sub-path's commands.
756 * @param[in] pts The array of the two-dimensional points.
757 * @param[in] ptsCnt The number of the points in the @p pts array.
759 * @return Result::Success when succeed, Result::InvalidArguments otherwise.
761 * @note The interface is designed for optimal path setting if the caller has a completed path commands already.
763 Result appendPath(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept;
766 * @brief Sets the stroke width for all of the figures from the path.
768 * @param[in] width The width of the stroke. The default value is 0.
770 * @return Result::Success when succeed, Result::FailedAllocation otherwise.
772 Result stroke(float width) noexcept;
775 * @brief Sets the color of the stroke for all of the figures from the path.
777 * @param[in] r The red color channel value in the range [0 ~ 255]. The default value is 0.
778 * @param[in] g The green color channel value in the range [0 ~ 255]. The default value is 0.
779 * @param[in] b The blue color channel value in the range [0 ~ 255]. The default value is 0.
780 * @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.
782 * @return Result::Success when succeed, Result::FailedAllocation otherwise.
784 Result stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
787 * @brief Sets the gradient fill of the stroke for all of the figures from the path.
789 * @param[in] f The gradient fill.
791 * @retval Result::Success When succeed.
792 * @retval Result::FailedAllocation An internal error with a memory allocation for an object to be filled.
793 * @retval Result::MemoryCorruption In case a @c nullptr is passed as the argument.
795 Result stroke(std::unique_ptr<Fill> f) noexcept;
798 * @brief Sets the dash pattern of the stroke.
800 * @param[in] dashPattern The array of consecutive pair values of the dash length and the gap length.
801 * @param[in] cnt The length of the @p dashPattern array.
803 * @retval Result::Success When succeed.
804 * @retval Result::FailedAllocation An internal error with a memory allocation for an object to be dashed.
805 * @retval Result::InvalidArguments In case that either @p dashPattern is @c nullptr or @p cnt is zero.
807 * @note If any of the dash pattern values is zero, this function has no effect.
808 * @note To reset the stroke dash pattern, pass @c nullptr to @p dashPattern and zero to @p cnt.
809 * @warning @p cnt must be greater than 1 if the dash pattern is valid.
811 Result stroke(const float* dashPattern, uint32_t cnt) noexcept;
814 * @brief Sets the cap style of the stroke in the open sub-paths.
816 * @param[in] cap The cap style value. The default value is @c StrokeCap::Square.
818 * @return Result::Success when succeed, Result::FailedAllocation otherwise.
820 Result stroke(StrokeCap cap) noexcept;
823 * @brief Sets the join style for stroked path segments.
825 * The join style is used for joining the two line segment while stroking the path.
827 * @param[in] join The join style value. The default value is @c StrokeJoin::Bevel.
829 * @return Result::Success when succeed, Result::FailedAllocation otherwise.
831 Result stroke(StrokeJoin join) noexcept;
834 * @brief Sets the solid color for all of the figures from the path.
836 * The parts of the shape defined as inner are colored.
838 * @param[in] r The red color channel value in the range [0 ~ 255]. The default value is 0.
839 * @param[in] g The green color channel value in the range [0 ~ 255]. The default value is 0.
840 * @param[in] b The blue color channel value in the range [0 ~ 255]. The default value is 0.
841 * @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.
843 * @return Result::Success when succeed.
845 * @note Either a solid color or a gradient fill is applied, depending on what was set as last.
847 Result fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
850 * @brief Sets the gradient fill for all of the figures from the path.
852 * The parts of the shape defined as inner are filled.
854 * @param[in] f The unique pointer to the gradient fill.
856 * @return Result::Success when succeed, Result::MemoryCorruption otherwise.
858 * @note Either a solid color or a gradient fill is applied, depending on what was set as last.
860 Result fill(std::unique_ptr<Fill> f) noexcept;
863 * @brief Sets the fill rule for the Shape object.
865 * @param[in] r The fill rule value.
867 * @return Result::Success when succeed.
869 Result fill(FillRule r) noexcept;
872 * @brief Gets the commands data of the path.
874 * @param[out] cmds The pointer to the array of the commands from the path.
876 * @return The length of the @p cmds array when succeed, zero otherwise.
878 uint32_t pathCommands(const PathCommand** cmds) const noexcept;
881 * @brief Gets the points values of the path.
883 * @param[out] pts The pointer to the array of the two-dimensional points from the path.
885 * @return The length of the @p pts array when succeed, zero otherwise.
887 uint32_t pathCoords(const Point** pts) const noexcept;
890 * @brief Gets the pointer to the gradient fill of the shape.
892 * @return The pointer to the gradient fill of the stroke when succeed, @c nullptr in case no fill was set.
894 const Fill* fill() const noexcept;
897 * @brief Gets the solid color of the shape.
899 * @param[out] r The red color channel value in the range [0 ~ 255].
900 * @param[out] g The green color channel value in the range [0 ~ 255].
901 * @param[out] b The blue color channel value in the range [0 ~ 255].
902 * @param[out] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque.
904 * @return Result::Success when succeed.
906 Result fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
909 * @brief Gets the fill rule value.
911 * @return The fill rule value of the shape.
913 FillRule fillRule() const noexcept;
916 * @brief Gets the stroke width.
918 * @return The stroke width value when succeed, zero if no stroke was set.
920 float strokeWidth() const noexcept;
923 * @brief Gets the color of the shape's stroke.
925 * @param[out] r The red color channel value in the range [0 ~ 255].
926 * @param[out] g The green color channel value in the range [0 ~ 255].
927 * @param[out] b The blue color channel value in the range [0 ~ 255].
928 * @param[out] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque.
930 * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
932 Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
935 * @brief Gets the pointer to the gradient fill of the stroke.
937 * @return The pointer to the gradient fill of the stroke when succeed, @c nullptr otherwise.
939 const Fill* strokeFill() const noexcept;
942 * @brief Gets the dash pattern of the stroke.
944 * @param[out] dashPattern The pointer to the memory, where the dash pattern array is stored.
946 * @return The length of the @p dashPattern array.
948 uint32_t strokeDash(const float** dashPattern) const noexcept;
951 * @brief Gets the cap style used for stroking the path.
953 * @return The cap style value of the stroke.
955 StrokeCap strokeCap() const noexcept;
958 * @brief Gets the join style value used for stroking the path.
960 * @return The join style value of the stroke.
962 StrokeJoin strokeJoin() const noexcept;
965 * @brief Creates a new Shape object.
967 * @return A new Shape object.
969 static std::unique_ptr<Shape> gen() noexcept;
971 _TVG_DECLARE_PRIVATE(Shape);
978 * @brief A class representing an image read in one of the supported formats: raw, svg, png and etc.
979 * Besides the methods inherited from the Paint, it provides methods to load & draw images on the canvas.
981 * @note Supported formats are depended on the available TVG loaders.
983 class TVG_EXPORT Picture final : public Paint
989 * @brief Loads a picture data directly from a file.
991 * @param[in] path A path to the picture file.
993 * @retval Result::Success When succeed.
994 * @retval Result::InvalidArguments In case the @p path is invalid.
995 * @retval Result::NonSupport When trying to load a file with an unknown extension.
996 * @retval Result::Unknown If an error occurs at a later stage.
998 * @note The Load behavior can be asynchronous if the assigned thread number is greater than zero.
999 * @see Initializer::init()
1001 Result load(const std::string& path) noexcept;
1004 * @brief Loads a picture data from a memory block of a given size.
1006 * @param[in] data A pointer to a memory location where the content of the picture file is stored.
1007 * @param[in] size The size in bytes of the memory occupied by the @p data.
1008 * @param[in] copy Decides whether the data should be copied into the engine local buffer.
1010 * @retval Result::Success When succeed.
1011 * @retval Result::InvalidArguments In case no data are provided or the @p size is zero or less.
1012 * @retval Result::NonSupport When trying to load a file with an unknown extension.
1013 * @retval Result::Unknown If an error occurs at a later stage.
1015 * @note: This api supports only SVG format
1017 * @warning: you have responsibility to release the @p data memory if the @p copy is true
1019 Result load(const char* data, uint32_t size, bool copy) noexcept;
1020 Result load(const char* data, uint32_t size) noexcept;
1023 * @brief Resize the picture content with the given width and height.
1025 * Resize the picture content while keeping the default size aspect ratio.
1026 * The scaling factor is established for each of dimensions and the smaller value is applied to both of them.
1028 * @param[in] w A new width of the image in pixels.
1029 * @param[in] h A new height of the image in pixels.
1031 * @return Result::Success when succeed, Result::InsufficientCondition otherwise.
1033 Result size(float w, float h) noexcept;
1036 * @brief Gets the size of the image.
1038 * @param[out] w The width of the image in pixels.
1039 * @param[out] h The height of the image in pixels.
1041 * @return Result::Success when succeed.
1043 Result size(float* w, float* h) const noexcept;
1046 * @brief Gets the pixels information of the picture.
1048 * @warning Please do not use it, this API is not official one. It could be modified in the next version.
1052 const uint32_t* data() const noexcept;
1055 * @brief Set paint for the picture.
1057 * @param[in] paint A Paint object to be drawn.
1059 * @return Result::Success when succeed.
1060 * @return Result::InsufficientCondition if paint already set.
1061 * @return Result::MemoryCorruption when bad memory handling.
1063 * @warning Please do not use it, this API is not official one. It could be modified in the next version.
1067 Result paint(std::unique_ptr<Paint> paint) noexcept;
1070 * @brief Loads a raw data from a memory block with a given size.
1072 * @warning Please do not use it, this API is not official one. It could be modified in the next version.
1076 Result load(uint32_t* data, uint32_t w, uint32_t h, bool copy) noexcept;
1079 * @brief Gets the position and the size of the loaded picture.
1081 * @warning Please do not use it, this API is not official one. It could be modified in the next version.
1085 Result viewbox(float* x, float* y, float* w, float* h) const noexcept;
1088 * @brief Creates a new Picture object.
1090 * @return A new Picture object.
1092 static std::unique_ptr<Picture> gen() noexcept;
1094 _TVG_DECLARE_PRIVATE(Picture);
1101 * @brief A class to composite children paints.
1103 * As the traditional graphics rendering method, TVG also enables scene-graph mechanism.
1104 * This feature supports an array function for managing the multiple paints as one group paint.
1106 * As a group, the scene can be transformed, made translucent and composited with other target paints,
1107 * its children will be affected by the scene world.
1109 class TVG_EXPORT Scene final : public Paint
1115 * @brief Passes drawing elements to the Scene using Paint objects.
1117 * Only pushed paints in the scene will be drawing targets.
1118 * They are retained by the scene until you call Scene::clear().
1119 * If you know the number of the pushed objects in the advance, please call Scene::reserve().
1121 * @param[in] paint A Paint object to be drawn.
1123 * @return Result::Success when succeed, Result::MemoryCorruption otherwise.
1125 * @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.
1126 * @see Scene::reserve()
1128 Result push(std::unique_ptr<Paint> paint) noexcept;
1131 * @brief Sets the size of the container, where all the paints pushed into the Scene are stored.
1133 * If the number of objects pushed into the scene is known in advance, calling the function
1134 * prevents multiple memory reallocation, thus improving the performance.
1136 * @param[in] size The number of objects for which the memory is to be reserved.
1138 * @return Result::Success when succeed.
1140 Result reserve(uint32_t size) noexcept;
1143 * @brief Sets the total number of the paints pushed into the scene to be zero.
1144 * Depending on the value of the @p free argument, the paints are freed or not.
1146 * @param[in] free If @c true, the memory occupied by paints is deallocated, otherwise it is not.
1148 * @return Result::Success when succeed
1150 * @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.
1151 * @warning Please do not use it, this API is not official one. It could be modified in the next version.
1155 Result clear(bool free = true) noexcept;
1158 * @brief Creates a new Scene object.
1160 * @return A new Scene object.
1162 static std::unique_ptr<Scene> gen() noexcept;
1164 _TVG_DECLARE_PRIVATE(Scene);
1171 * @brief A class for the rendering graphical elements with a software raster engine.
1173 class TVG_EXPORT SwCanvas final : public Canvas
1179 * @brief Enumeration specifying the methods of combining the 8-bit color channels into 32-bit color.
1183 ABGR8888 = 0, ///< The channels are joined in the order: alpha, blue, green, red.
1184 ARGB8888 ///< The channels are joined in the order: alpha, red, green, blue.
1188 * @brief Enumeration specifying the methods of Memory Pool behavior policy.
1194 Default = 0, ///< Default behavior that ThorVG is designed to.
1195 Shareable, ///< Memory Pool is shared among the SwCanvases.
1196 Individual ///< Allocate designated memory pool that is only used by current instance.
1200 * @brief Sets the target buffer for the rasterization.
1202 * The buffer of a desirable size should be allocated and owned by the caller.
1204 * @param[in] buffer A pointer to a memory block of the size @p stride x @p h, where the raster data are stored.
1205 * @param[in] stride The stride of the raster image - greater than or equal to @p w.
1206 * @param[in] w The width of the raster image.
1207 * @param[in] h The height of the raster image.
1208 * @param[in] cs The value specifying the way the 32-bits colors should be read/written.
1210 * @retval Result::Success When succeed.
1211 * @retval Result::MemoryCorruption When casting in the internal function implementation failed.
1212 * @retval Result::InvalidArguments In case no valid pointer is provided or the width, or the height or the stride is zero.
1213 * @retval Result::NonSupport In case the software engine is not supported.
1215 * @warning Do not access @p buffer during Canvas::draw() - Canvas::sync(). It should not be accessed while TVG is writing on it.
1217 Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, Colorspace cs) noexcept;
1220 * @brief Set sw engine memory pool behavior policy.
1222 * Basically ThorVG draws a lot of shapes, it allocates/deallocates a few chunk of memory
1223 * while processing rendering. It internally uses one shared memory pool
1224 * which can be reused among the canvases in order to avoid memory overhead.
1226 * Thus ThorVG suggests memory pool policy to satisfy user demands,
1227 * if it needs to guarantee the thread-safety of the internal data access.
1229 * @param[in] policy Use the shared cache memory. The default value is @c true
1231 * @retval Result::Success When succeed.
1232 * @retval Result::InsufficientCondition If the canvas has any paints.
1233 * @retval Result::NonSupport In case the software engine is not supported.
1235 * @note When @c policy is set as @c MempoolPolicy::Individual, current instance of canvas uses its own individual
1236 * memory data that is not shared with others. This is necessary when the canvas is accessed on a worker-thread.
1238 * @warning It's not allowed after pushing any paints.
1242 Result mempool(MempoolPolicy policy) noexcept;
1245 * @brief Creates a new SwCanvas object.
1246 * @return A new SwCanvas object.
1248 static std::unique_ptr<SwCanvas> gen() noexcept;
1250 _TVG_DECLARE_PRIVATE(SwCanvas);
1257 * @brief A class for the rendering graphic elements with a GL raster engine.
1259 * @warning Please do not use it. This class is not fully supported yet.
1263 class TVG_EXPORT GlCanvas final : public Canvas
1269 * @brief Sets the target buffer for the rasterization.
1271 * @warning Please do not use it, this API is not official one. It could be modified in the next version.
1275 Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h) noexcept;
1278 * @brief Creates a new GlCanvas object.
1280 * @return A new GlCanvas object.
1284 static std::unique_ptr<GlCanvas> gen() noexcept;
1286 _TVG_DECLARE_PRIVATE(GlCanvas);
1291 * @class Initializer
1293 * @brief A class that enables initialization and termination of the TVG engines.
1295 class TVG_EXPORT Initializer final
1299 * @brief Initializes TVG engines.
1301 * TVG requires the running-engine environment.
1302 * TVG runs its own task-scheduler for parallelizing rendering tasks efficiently.
1303 * You can indicate the number of threads, the count of which is designated @p threads.
1304 * In the initialization step, TVG will generate/spawn the threads as set by @p threads count.
1306 * @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.
1307 * @param[in] threads The number of additional threads. Zero indicates only the main thread is to be used.
1309 * @retval Result::Success When succeed.
1310 * @retval Result::FailedAllocation An internal error possibly with memory allocation.
1311 * @retval Result::InvalidArguments If unknown engine type chosen.
1312 * @retval Result::NonSupport In case the engine type is not supported on the system.
1313 * @retval Result::Unknown Others.
1315 * @note The Initializer keeps track of the number of times it was called. Threads count is fixed at the first init() call.
1316 * @see Initializer::term()
1318 static Result init(CanvasEngine engine, uint32_t threads) noexcept;
1321 * @brief Terminates TVG engines.
1323 * @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
1325 * @retval Result::Success When succeed.
1326 * @retval Result::InsufficientCondition In case there is nothing to be terminated.
1327 * @retval Result::InvalidArguments If unknown engine type chosen.
1328 * @retval Result::NonSupport In case the engine type is not supported on the system.
1329 * @retval Result::Unknown Others.
1331 * @note Initializer does own reference counting for multiple calls.
1332 * @see Initializer::init()
1334 static Result term(CanvasEngine engine) noexcept;
1336 _TVG_DISABLE_CTOR(Initializer);
1343 * @brief A class enabling saving a paint in a binary format.
1347 class TVG_EXPORT Saver
1353 * @brief Saves all the paints from the tree in a binary format.
1355 * @param[in] paint The root paint to be saved with all its nodes.
1356 * @param[in] path A path to the file, in which the data is to be saved.
1358 * @retval Result::Success When succeed.
1359 * @retval Result::InvalidArguments the @p path is invalid.
1360 * @retval Result::FailedAllocation An internal error with a memory allocation for the Saver object.
1361 * @retval Result::MemoryCorruption When casting in the internal function implementation failed.
1362 * @retval Result::Unknown Others.
1365 Result save(std::unique_ptr<Paint> paint, const std::string& path) noexcept;
1366 Result sync() noexcept;
1368 static std::unique_ptr<Saver> gen() noexcept;
1370 _TVG_DECLARE_PRIVATE(Saver);