Debian Codegraph @

thirdparty/thorvg/inc/thorvg.h

raw symbols SCIP
1 #ifndef _THORVG_H_
2 #define _THORVG_H_
3
4 #include <cstdint>
5 #include <functional>
6 #include <memory>
7 #include <string>
8 #include <list>
9
10 #ifdef TVG_API
11 #undef TVG_API
12 #endif
13
14 #ifndef TVG_STATIC
15 #ifdef _WIN32
16 #if TVG_BUILD
17 #define TVG_API __declspec(dllexport)
18 #else
19 #define TVG_API __declspec(dllimport)
20 #endif
21 #elif (defined(__SUNPRO_C) || defined(__SUNPRO_CC))
22 #define TVG_API __global
23 #else
24 #if (defined(__GNUC__) && __GNUC__ >= 4) || defined(__INTEL_COMPILER)
25 #define TVG_API __attribute__ ((visibility("default")))
26 #else
27 #define TVG_API
28 #endif
29 #endif
30 #else
31 #define TVG_API
32 #endif
33
34 #ifdef TVG_DEPRECATED
35 #undef TVG_DEPRECATED
36 #endif
37
38 #ifdef _WIN32
39 #define TVG_DEPRECATED __declspec(deprecated)
40 #elif __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
41 #define TVG_DEPRECATED __attribute__ ((__deprecated__))
42 #else
43 #define TVG_DEPRECATED
44 #endif
45
46 #define _TVG_DECLARE_PRIVATE(A) \
47 struct Impl; \
48 Impl* pImpl; \
49 protected: \
50 A(const A&) = delete; \
51 const A& operator=(const A&) = delete; \
52 A()
53
54 #define _TVG_DISABLE_CTOR(A) \
55 A() = delete; \
56 ~A() = delete
57
58 #define _TVG_DECLARE_ACCESSOR(A) \
59 friend A
60
61 namespace tvg
62 {
63
64 class RenderMethod;
65 class Animation;
66
67 /**
68 * @defgroup ThorVG ThorVG
69 * @brief ThorVG classes and enumerations providing C++ APIs.
70 */
71
72 /**@{*/
73
74 /**
75 * @brief Enumeration specifying the result from the APIs.
76 *
77 * All ThorVG APIs could potentially return one of the values in the list.
78 * Please note that some APIs may additionally specify the reasons that trigger their return values.
79 *
80 */
81 enum class Result
82 {
83 Success = 0, ///< The value returned in case of a correct request execution.
84 InvalidArguments, ///< The value returned in the event of a problem with the arguments given to the API - e.g. empty paths or null pointers.
85 InsufficientCondition, ///< The value returned in case the request cannot be processed - e.g. asking for properties of an object, which does not exist.
86 FailedAllocation, ///< The value returned in case of unsuccessful memory allocation.
87 MemoryCorruption, ///< The value returned in the event of bad memory handling - e.g. failing in pointer releasing or casting
88 NonSupport, ///< The value returned in case of choosing unsupported engine features(options).
89 Unknown ///< The value returned in all other cases.
90 };
91
92
93 /**
94 * @brief Enumeration specifying the values of the path commands accepted by TVG.
95 *
96 * Not to be confused with the path commands from the svg path element (like M, L, Q, H and many others).
97 * TVG interprets all of them and translates to the ones from the PathCommand values.
98 */
99 enum class PathCommand
100 {
101 Close = 0, ///< Ends the current sub-path and connects it with its initial point. This command doesn't expect any points.
102 MoveTo, ///< Sets a new initial point of the sub-path and a new current point. This command expects 1 point: the starting position.
103 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.
104 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.
105 };
106
107
108 /**
109 * @brief Enumeration determining the ending type of a stroke in the open sub-paths.
110 */
111 enum class StrokeCap
112 {
113 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.
114 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.
115 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.
116 };
117
118
119 /**
120 * @brief Enumeration determining the style used at the corners of joined stroked path segments.
121 */
122 enum class StrokeJoin
123 {
124 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.
125 Round, ///< The outer corner of the joined path segments is rounded. The circular region is centered at the join point.
126 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.
127 };
128
129
130 /**
131 * @brief Enumeration specifying how to fill the area outside the gradient bounds.
132 */
133 enum class FillSpread
134 {
135 Pad = 0, ///< The remaining area is filled with the closest stop color.
136 Reflect, ///< The gradient pattern is reflected outside the gradient area until the expected region is filled.
137 Repeat ///< The gradient pattern is repeated continuously beyond the gradient area until the expected region is filled.
138 };
139
140
141 /**
142 * @brief Enumeration specifying the algorithm used to establish which parts of the shape are treated as the inside of the shape.
143 */
144 enum class FillRule
145 {
146 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.
147 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.
148 };
149
150
151 /**
152 * @brief Enumeration indicating the method used in the composition of two objects - the target and the source.
153 *
154 * Notation: S(Source), T(Target), SA(Source Alpha), TA(Target Alpha)
155 *
156 * @see Paint::composite()
157 */
158 enum class CompositeMethod
159 {
160 None = 0, ///< No composition is applied.
161 ClipPath, ///< The intersection of the source and the target is determined and only the resulting pixels from the source are rendered. Note that ClipPath only supports the Shape type. @deprecated Use Paint::clip() instead.
162 AlphaMask, ///< Alpha Masking using the compositing target's pixels as an alpha value.
163 InvAlphaMask, ///< Alpha Masking using the complement to the compositing target's pixels as an alpha value.
164 LumaMask, ///< Alpha Masking using the grayscale (0.2125R + 0.7154G + 0.0721*B) of the compositing target's pixels. @since 0.9
165 InvLumaMask, ///< Alpha Masking using the grayscale (0.2125R + 0.7154G + 0.0721*B) of the complement to the compositing target's pixels. @since 0.11
166 AddMask, ///< Combines the target and source objects pixels using target alpha. (T * TA) + (S * (255 - TA)) (Experimental API)
167 SubtractMask, ///< Subtracts the source color from the target color while considering their respective target alpha. (T * TA) - (S * (255 - TA)) (Experimental API)
168 IntersectMask, ///< Computes the result by taking the minimum value between the target alpha and the source alpha and multiplies it with the target color. (T * min(TA, SA)) (Experimental API)
169 DifferenceMask, ///< Calculates the absolute difference between the target color and the source color multiplied by the complement of the target alpha. abs(T - S * (255 - TA)) (Experimental API)
170 LightenMask, ///< Where multiple masks intersect, the highest transparency value is used. (Experimental API)
171 DarkenMask ///< Where multiple masks intersect, the lowest transparency value is used. (Experimental API)
172 };
173
174
175 /**
176 * @brief Enumeration indicates the method used for blending paint. Please refer to the respective formulas for each method.
177 *
178 * Notation: S(source paint as the top layer), D(destination as the bottom layer), Sa(source paint alpha), Da(destination alpha)
179 *
180 * @see Paint::blend()
181 *
182 * @since 0.15
183 */
184 enum class BlendMethod : uint8_t
185 {
186 Normal = 0, ///< Perform the alpha blending(default). S if (Sa == 255), otherwise (Sa * S) + (255 - Sa) * D
187 Multiply, ///< Takes the RGB channel values from 0 to 255 of each pixel in the top layer and multiples them with the values for the corresponding pixel from the bottom layer. (S * D)
188 Screen, ///< The values of the pixels in the two layers are inverted, multiplied, and then inverted again. (S + D) - (S * D)
189 Overlay, ///< Combines Multiply and Screen blend modes. (2 * S * D) if (2 * D < Da), otherwise (Sa * Da) - 2 * (Da - S) * (Sa - D)
190 Darken, ///< Creates a pixel that retains the smallest components of the top and bottom layer pixels. min(S, D)
191 Lighten, ///< Only has the opposite action of Darken Only. max(S, D)
192 ColorDodge, ///< Divides the bottom layer by the inverted top layer. D / (255 - S)
193 ColorBurn, ///< Divides the inverted bottom layer by the top layer, and then inverts the result. 255 - (255 - D) / S
194 HardLight, ///< The same as Overlay but with the color roles reversed. (2 * S * D) if (S < Sa), otherwise (Sa * Da) - 2 * (Da - S) * (Sa - D)
195 SoftLight, ///< The same as Overlay but with applying pure black or white does not result in pure black or white. (1 - 2 * S) * (D ^ 2) + (2 * S * D)
196 Difference, ///< Subtracts the bottom layer from the top layer or the other way around, to always get a non-negative value. (S - D) if (S > D), otherwise (D - S)
197 Exclusion, ///< The result is twice the product of the top and bottom layers, subtracted from their sum. s + d - (2 * s * d)
198 Hue, ///< Reserved. Not supported.
199 Saturation, ///< Reserved. Not supported.
200 Color, ///< Reserved. Not supported.
201 Luminosity, ///< Reserved. Not supported.
202 Add, ///< Simply adds pixel values of one layer with the other. (S + D)
203 HardMix ///< Reserved. Not supported.
204 };
205
206
207 /**
208 * @brief Enumeration that defines methods used for Scene Effects.
209 *
210 * This enum provides options to apply various post-processing effects to a scene.
211 * Scene effects are typically applied to modify the final appearance of a rendered scene, such as blurring.
212 *
213 * @see Scene::push(SceneEffect effect, ...)
214 *
215 * @note Experimental API
216 */
217 enum class SceneEffect : uint8_t
218 {
219 ClearAll = 0, ///< Reset all previously applied scene effects, restoring the scene to its original state.
220 GaussianBlur, ///< Apply a blur effect with a Gaussian filter. Param(3) = {sigma(float)[> 0], direction(int)[both: 0 / horizontal: 1 / vertical: 2], border(int)[duplicate: 0 / wrap: 1], quality(int)[0 - 100]}
221 DropShadow, ///< Apply a drop shadow effect with a Gaussian Blur filter. Param(8) = {color_R(int)[0 - 255], color_G(int)[0 - 255], color_B(int)[0 - 255], opacity(int)[0 - 255], angle(float)[0 - 360], distance(float), blur_sigma(float)[> 0], quality(int)[0 - 100]}
222 Fill, ///< Override the scene content color with a given fill information (Experimental API). Param(5) = {color_R(int)[0 - 255], color_G(int)[0 - 255], color_B(int)[0 - 255], opacity(int)[0 - 255]}
223 Tint, ///< Tinting the current scene color with a given black, white color paramters (Experimental API). Param(7) = {black_R(int)[0 - 255], black_G(int)[0 - 255], black_B(int)[0 - 255], white_R(int)[0 - 255], white_G(int)[0 - 255], white_B(int)[0 - 255], intensity(float)[0 - 100]}
224 Tritone ///< Apply a tritone color effect to the scene using three color parameters for shadows, midtones, and highlights (Experimental API). Param(9) = {Shadow_R(int)[0 - 255], Shadow_G(int)[0 - 255], Shadow_B(int)[0 - 255], Midtone_R(int)[0 - 255], Midtone_G(int)[0 - 255], Midtone_B(int)[0 - 255], Highlight_R(int)[0 - 255], Highlight_G(int)[0 - 255], Highlight_B(int)[0 - 255]}
225 };
226
227
228 /**
229 * @brief Enumeration specifying the engine type used for the graphics backend. For multiple backends bitwise operation is allowed.
230 */
231 enum class CanvasEngine
232 {
233 Sw = (1 << 1), ///< CPU rasterizer.
234 Gl = (1 << 2), ///< OpenGL rasterizer.
235 Wg = (1 << 3), ///< WebGPU rasterizer. @since 0.15
236 };
237
238
239 /**
240 * @brief Enumeration specifying the ThorVG class type value.
241 *
242 * ThorVG's drawing objects can return class type values, allowing you to identify the specific class of each object.
243 *
244 * @see Paint::type()
245 * @see Fill::type()
246 *
247 * @note Experimental API
248 */
249 enum class Type : uint8_t
250 {
251 Undefined = 0, ///< Unkown class
252 Shape, ///< Shape class
253 Scene, ///< Scene class
254 Picture, ///< Picture class
255 Text, ///< Text class
256 LinearGradient = 10, ///< LinearGradient class
257 RadialGradient ///< RadialGradient class
258 };
259
260
261 /**
262 * @brief A data structure representing a point in two-dimensional space.
263 *
264 * This structure defines a single point using Cartesian coordinates.
265 * It is typically used for specifying positions or coordinates in 2D graphics.
266 */
267 struct Point
268 {
269 float x; ///< The x-coordinate of the point.
270 float y; ///< The y-coordinate of the point.
271 };
272
273
274 /**
275 * @brief A data structure representing a three-dimensional matrix.
276 *
277 * The elements e11, e12, e21 and e22 represent the rotation matrix, including the scaling factor.
278 * The elements e13 and e23 determine the translation of the object along the x and y-axis, respectively.
279 * The elements e31 and e32 are set to 0, e33 is set to 1.
280 */
281 struct Matrix
282 {
283 float e11, e12, e13;
284 float e21, e22, e23;
285 float e31, e32, e33;
286 };
287
288
289 /**
290 * @class Paint
291 *
292 * @brief An abstract class for managing graphical elements.
293 *
294 * A graphical element in TVG is any object composed into a Canvas.
295 * Paint represents such a graphical object and its behaviors such as duplication, transformation and composition.
296 * 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.
297 */
298 class TVG_API Paint
299 {
300 public:
301 virtual ~Paint();
302
303 /**
304 * @brief Sets the angle by which the object is rotated.
305 *
306 * The angle in measured clockwise from the horizontal axis.
307 * The rotational axis passes through the point on the object with zero coordinates.
308 *
309 * @param[in] degree The value of the angle in degrees.
310 *
311 * @retval Result::InsufficientCondition in case a custom transform is applied.
312 * @see Paint::transform()
313 */
314 Result rotate(float degree) noexcept;
315
316 /**
317 * @brief Sets the scale value of the object.
318 *
319 * @param[in] factor The value of the scaling factor. The default value is 1.
320 *
321 * @retval Result::InsufficientCondition in case a custom transform is applied.
322 * @see Paint::transform()
323 */
324 Result scale(float factor) noexcept;
325
326 /**
327 * @brief Sets the values by which the object is moved in a two-dimensional space.
328 *
329 * The origin of the coordinate system is in the upper-left corner of the canvas.
330 * The horizontal and vertical axes point to the right and down, respectively.
331 *
332 * @param[in] x The value of the horizontal shift.
333 * @param[in] y The value of the vertical shift.
334 *
335 * @retval Result::InsufficientCondition in case a custom transform is applied.
336 * @see Paint::transform()
337 */
338 Result translate(float x, float y) noexcept;
339
340 /**
341 * @brief Sets the matrix of the affine transformation for the object.
342 *
343 * The augmented matrix of the transformation is expected to be given.
344 *
345 * @param[in] m The 3x3 augmented matrix.
346 */
347 Result transform(const Matrix& m) noexcept;
348
349 /**
350 * @brief Gets the matrix of the affine transformation of the object.
351 *
352 * The values of the matrix can be set by the transform() API, as well by the translate(),
353 * scale() and rotate(). In case no transformation was applied, the identity matrix is returned.
354 *
355 * @return The augmented transformation matrix.
356 *
357 * @since 0.4
358 */
359 Matrix transform() noexcept;
360
361 /**
362 * @brief Sets the opacity of the object.
363 *
364 * @param[in] o The opacity value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque.
365 *
366 * @note Setting the opacity with this API may require multiple render pass for composition. It is recommended to avoid changing the opacity if possible.
367 */
368 Result opacity(uint8_t o) noexcept;
369
370 /**
371 * @brief Sets the composition target object and the composition method.
372 *
373 * @param[in] target The paint of the target object.
374 * @param[in] method The method used to composite the source object with the target.
375 */
376 Result composite(std::unique_ptr<Paint> target, CompositeMethod method) noexcept;
377
378 /**
379 * @brief Clip the drawing region of the paint object.
380 *
381 * This function restricts the drawing area of the paint object to the specified shape's paths.
382 *
383 * @param[in] clipper The shape object as the clipper.
384 *
385 * @retval Result::NonSupport If the @p clipper type is not Shape.
386 *
387 * @note @p clipper only supports the Shape type.
388 * @note Experimental API
389 */
390 Result clip(std::unique_ptr<Paint> clipper) noexcept;
391
392 /**
393 * @brief Sets the blending method for the paint object.
394 *
395 * The blending feature allows you to combine colors to create visually appealing effects, including transparency, lighting, shading, and color mixing, among others.
396 * its process involves the combination of colors or images from the source paint object with the destination (the lower layer image) using blending operations.
397 * The blending operation is determined by the chosen @p BlendMethod, which specifies how the colors or images are combined.
398 *
399 * @param[in] method The blending method to be set.
400 *
401 * @note Experimental API
402 */
403 Result blend(BlendMethod method) noexcept;
404
405 /**
406 * @deprecated Use bounds(float* x, float* y, float* w, float* h, bool transformed) instead
407 */
408 TVG_DEPRECATED Result bounds(float* x, float* y, float* w, float* h) const noexcept;
409
410 /**
411 * @brief Gets the axis-aligned bounding box of the paint object.
412 *
413 * @param[out] x The x-coordinate of the upper-left corner of the object.
414 * @param[out] y The y-coordinate of the upper-left corner of the object.
415 * @param[out] w The width of the object.
416 * @param[out] h The height of the object.
417 * @param[in] transformed If @c true, the paint's transformations are taken into account in the scene it belongs to. Otherwise they aren't.
418 *
419 * @note This is useful when you need to figure out the bounding box of the paint in the canvas space.
420 * @note The bounding box doesn't indicate the actual drawing region. It's the smallest rectangle that encloses the object.
421 * @note If @p transformed is @c true, the paint needs to be pushed into a canvas and updated before this api is called.
422 * @see Canvas::update()
423 */
424 Result bounds(float* x, float* y, float* w, float* h, bool transformed) const noexcept;
425
426 /**
427 * @brief Duplicates the object.
428 *
429 * Creates a new object and sets its all properties as in the original object.
430 *
431 * @return The created object when succeed, @c nullptr otherwise.
432 */
433 Paint* duplicate() const noexcept;
434
435 /**
436 * @brief Gets the opacity value of the object.
437 *
438 * @return The opacity value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque.
439 */
440 uint8_t opacity() const noexcept;
441
442 /**
443 * @brief Gets the composition target object and the composition method.
444 *
445 * @param[out] target The paint of the target object.
446 *
447 * @return The method used to composite the source object with the target.
448 *
449 * @since 0.5
450 */
451 CompositeMethod composite(const Paint** target) const noexcept;
452
453 /**
454 * @brief Returns the ID value of this class.
455 *
456 * This method can be used to check the current concrete instance type.
457 *
458 * @return The class type ID of the Paint instance.
459 *
460 * @note Experimental API
461 */
462 virtual Type type() const noexcept = 0;
463
464 /**
465 * @brief Unique ID of this instance.
466 *
467 * This is reserved to specify an paint instance in a scene.
468 *
469 * @note Experimental API
470 */
471 uint32_t id = 0;
472
473 /**
474 * @see Paint::type()
475 */
476 TVG_DEPRECATED uint32_t identifier() const noexcept;
477
478 _TVG_DECLARE_PRIVATE(Paint);
479 };
480
481
482 /**
483 * @class Fill
484 *
485 * @brief An abstract class representing the gradient fill of the Shape object.
486 *
487 * It contains the information about the gradient colors and their arrangement
488 * inside the gradient bounds. The gradients bounds are defined in the LinearGradient
489 * or RadialGradient class, depending on the type of the gradient to be used.
490 * It specifies the gradient behavior in case the area defined by the gradient bounds
491 * is smaller than the area to be filled.
492 */
493 class TVG_API Fill
494 {
495 public:
496 /**
497 * @brief A data structure storing the information about the color and its relative position inside the gradient bounds.
498 */
499 struct ColorStop
500 {
501 float offset; /**< The relative position of the color. */
502 uint8_t r; /**< The red color channel value in the range [0 ~ 255]. */
503 uint8_t g; /**< The green color channel value in the range [0 ~ 255]. */
504 uint8_t b; /**< The blue color channel value in the range [0 ~ 255]. */
505 uint8_t a; /**< The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. */
506 };
507
508 virtual ~Fill();
509
510 /**
511 * @brief Sets the parameters of the colors of the gradient and their position.
512 *
513 * @param[in] colorStops An array of ColorStop data structure.
514 * @param[in] cnt The count of the @p colorStops array equal to the colors number used in the gradient.
515 */
516 Result colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept;
517
518 /**
519 * @brief Sets the FillSpread value, which specifies how to fill the area outside the gradient bounds.
520 *
521 * @param[in] s The FillSpread value.
522 */
523 Result spread(FillSpread s) noexcept;
524
525 /**
526 * @brief Sets the matrix of the affine transformation for the gradient fill.
527 *
528 * The augmented matrix of the transformation is expected to be given.
529 *
530 * @param[in] m The 3x3 augmented matrix.
531 */
532 Result transform(const Matrix& m) noexcept;
533
534 /**
535 * @brief Gets the parameters of the colors of the gradient, their position and number.
536 *
537 * @param[out] colorStops A pointer to the memory location, where the array of the gradient's ColorStop is stored.
538 *
539 * @return The number of colors used in the gradient. This value corresponds to the length of the @p colorStops array.
540 */
541 uint32_t colorStops(const ColorStop** colorStops) const noexcept;
542
543 /**
544 * @brief Gets the FillSpread value of the fill.
545 *
546 * @return The FillSpread value of this Fill.
547 */
548 FillSpread spread() const noexcept;
549
550 /**
551 * @brief Gets the matrix of the affine transformation of the gradient fill.
552 *
553 * In case no transformation was applied, the identity matrix is returned.
554 *
555 * @return The augmented transformation matrix.
556 */
557 Matrix transform() const noexcept;
558
559 /**
560 * @brief Creates a copy of the Fill object.
561 *
562 * Return a newly created Fill object with the properties copied from the original.
563 *
564 * @return A copied Fill object when succeed, @c nullptr otherwise.
565 */
566 Fill* duplicate() const noexcept;
567
568 /**
569 * @brief Returns the ID value of this class.
570 *
571 * This method can be used to check the current concrete instance type.
572 *
573 * @return The class type ID of the Fill instance.
574 *
575 * @note Experimental API
576 */
577 virtual Type type() const noexcept = 0;
578
579 /**
580 * @see Fill::type()
581 */
582 TVG_DEPRECATED uint32_t identifier() const noexcept;
583
584 _TVG_DECLARE_PRIVATE(Fill);
585 };
586
587
588 /**
589 * @class Canvas
590 *
591 * @brief An abstract class for drawing graphical elements.
592 *
593 * 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.
594 *
595 * @note A Canvas behavior depends on the raster engine though the final content of the buffer is expected to be identical.
596 * @warning The Paint objects belonging to one Canvas can't be shared among multiple Canvases.
597 */
598 class TVG_API Canvas
599 {
600 public:
601 Canvas(RenderMethod*);
602 virtual ~Canvas();
603
604 TVG_DEPRECATED Result reserve(uint32_t n) noexcept;
605
606 /**
607 * @brief Returns the list of the paints that currently held by the Canvas.
608 *
609 * This function provides the list of paint nodes, allowing users a direct opportunity to modify the scene tree.
610 *
611 * @warning Please avoid accessing the paints during Canvas update/draw. You can access them after calling sync().
612 * @see Canvas::sync()
613 *
614 * @note Experimental API
615 */
616 std::list<Paint*>& paints() noexcept;
617
618 /**
619 * @brief Passes drawing elements to the Canvas using Paint objects.
620 *
621 * Only pushed paints in the canvas will be drawing targets.
622 * They are retained by the canvas until you call Canvas::clear().
623 *
624 * @param[in] paint A Paint object to be drawn.
625 *
626 * @retval Result::MemoryCorruption In case a @c nullptr is passed as the argument.
627 *
628 * @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.
629 * @see Canvas::paints()
630 * @see Canvas::clear()
631 */
632 virtual Result push(std::unique_ptr<Paint> paint) noexcept;
633
634 /**
635 * @brief Clear the internal canvas resources that used for the drawing.
636 *
637 * This API sets the total number of paints pushed into the canvas to zero.
638 * Depending on the value of the @p free argument, the paints are either freed or retained.
639 * So if you need to update paint properties while maintaining the existing scene structure, you can set @p free = false.
640 *
641 * @param[in] free If @c true, the memory occupied by paints is deallocated, otherwise it is not.
642 *
643 *
644 * @see Canvas::push()
645 * @see Canvas::paints()
646 */
647 virtual Result clear(bool free = true) noexcept;
648
649 /**
650 * @brief Request the canvas to update the paint objects.
651 *
652 * If a @c nullptr is passed all paint objects retained by the Canvas are updated,
653 * otherwise only the paint to which the given @p paint points.
654 *
655 * @param[in] paint A pointer to the Paint object or @c nullptr.
656 *
657 * @note The Update behavior can be asynchronous if the assigned thread number is greater than zero.
658 */
659 virtual Result update(Paint* paint = nullptr) noexcept;
660
661 /**
662 * @brief Requests the canvas to draw the Paint objects.
663 *
664 * @note Drawing can be asynchronous if the assigned thread number is greater than zero. To guarantee the drawing is done, call sync() afterwards.
665 * @see Canvas::sync()
666 */
667 virtual Result draw() noexcept;
668
669 /**
670 * @brief Sets the drawing region in the canvas.
671 *
672 * This function defines the rectangular area of the canvas that will be used for drawing operations.
673 * The specified viewport is used to clip the rendering output to the boundaries of the rectangle.
674 *
675 * @param[in] x The x-coordinate of the upper-left corner of the rectangle.
676 * @param[in] y The y-coordinate of the upper-left corner of the rectangle.
677 * @param[in] w The width of the rectangle.
678 * @param[in] h The height of the rectangle.
679 *
680 * @see SwCanvas::target()
681 * @see GlCanvas::target()
682 * @see WgCanvas::target()
683 *
684 * @warning It's not allowed to change the viewport during Canvas::push() - Canvas::sync() or Canvas::update() - Canvas::sync().
685 *
686 * @note When resetting the target, the viewport will also be reset to the target size.
687 * @since 0.15
688 */
689 virtual Result viewport(int32_t x, int32_t y, int32_t w, int32_t h) noexcept;
690
691 /**
692 * @brief Guarantees that drawing task is finished.
693 *
694 * The Canvas rendering can be performed asynchronously. To make sure that rendering is finished,
695 * the sync() must be called after the draw() regardless of threading.
696 *
697 * @retval Result::InsufficientCondition: The canvas is either already in sync condition or in a damaged condition (a draw is required before syncing).
698 *
699 * @see Canvas::draw()
700 */
701 virtual Result sync() noexcept;
702
703 _TVG_DECLARE_PRIVATE(Canvas);
704 };
705
706
707 /**
708 * @class LinearGradient
709 *
710 * @brief A class representing the linear gradient fill of the Shape object.
711 *
712 * Besides the APIs inherited from the Fill class, it enables setting and getting the linear gradient bounds.
713 * The behavior outside the gradient bounds depends on the value specified in the spread API.
714 */
715 class TVG_API LinearGradient final : public Fill
716 {
717 public:
718 ~LinearGradient();
719
720 /**
721 * @brief Sets the linear gradient bounds.
722 *
723 * The bounds of the linear gradient are defined as a surface constrained by two parallel lines crossing
724 * the given points (@p x1, @p y1) and (@p x2, @p y2), respectively. Both lines are perpendicular to the line linking
725 * (@p x1, @p y1) and (@p x2, @p y2).
726 *
727 * @param[in] x1 The horizontal coordinate of the first point used to determine the gradient bounds.
728 * @param[in] y1 The vertical coordinate of the first point used to determine the gradient bounds.
729 * @param[in] x2 The horizontal coordinate of the second point used to determine the gradient bounds.
730 * @param[in] y2 The vertical coordinate of the second point used to determine the gradient bounds.
731 *
732 * @note In case the first and the second points are equal, an object is filled with a single color using the last color specified in the colorStops().
733 * @see Fill::colorStops()
734 */
735 Result linear(float x1, float y1, float x2, float y2) noexcept;
736
737 /**
738 * @brief Gets the linear gradient bounds.
739 *
740 * The bounds of the linear gradient are defined as a surface constrained by two parallel lines crossing
741 * the given points (@p x1, @p y1) and (@p x2, @p y2), respectively. Both lines are perpendicular to the line linking
742 * (@p x1, @p y1) and (@p x2, @p y2).
743 *
744 * @param[out] x1 The horizontal coordinate of the first point used to determine the gradient bounds.
745 * @param[out] y1 The vertical coordinate of the first point used to determine the gradient bounds.
746 * @param[out] x2 The horizontal coordinate of the second point used to determine the gradient bounds.
747 * @param[out] y2 The vertical coordinate of the second point used to determine the gradient bounds.
748 */
749 Result linear(float* x1, float* y1, float* x2, float* y2) const noexcept;
750
751 /**
752 * @brief Creates a new LinearGradient object.
753 *
754 * @return A new LinearGradient object.
755 */
756 static std::unique_ptr<LinearGradient> gen() noexcept;
757
758 /**
759 * @brief Returns the ID value of this class.
760 *
761 * This method can be used to check the current concrete instance type.
762 *
763 * @return The class type ID of the LinearGradient instance.
764 *
765 * @note Experimental API
766 */
767 Type type() const noexcept override;
768
769 /**
770 * @see LinearGradient::type()
771 */
772 TVG_DEPRECATED static uint32_t identifier() noexcept;
773
774 _TVG_DECLARE_PRIVATE(LinearGradient);
775 };
776
777
778 /**
779 * @class RadialGradient
780 *
781 * @brief A class representing the radial gradient fill of the Shape object.
782 *
783 */
784 class TVG_API RadialGradient final : public Fill
785 {
786 public:
787 ~RadialGradient();
788
789 /**
790 * @brief Sets the radial gradient bounds.
791 *
792 * The radial gradient bounds are defined as a circle centered in a given point (@p cx, @p cy) of a given radius.
793 *
794 * @param[in] cx The horizontal coordinate of the center of the bounding circle.
795 * @param[in] cy The vertical coordinate of the center of the bounding circle.
796 * @param[in] radius The radius of the bounding circle.
797 *
798 * @retval Result::InvalidArguments in case the @p radius value is zero or less.
799 *
800 * @note In case the @p radius is zero, an object is filled with a single color using the last color specified in the colorStops().
801 */
802 Result radial(float cx, float cy, float radius) noexcept;
803
804 /**
805 * @brief Gets the radial gradient bounds.
806 *
807 * The radial gradient bounds are defined as a circle centered in a given point (@p cx, @p cy) of a given radius.
808 *
809 * @param[out] cx The horizontal coordinate of the center of the bounding circle.
810 * @param[out] cy The vertical coordinate of the center of the bounding circle.
811 * @param[out] radius The radius of the bounding circle.
812 *
813 */
814 Result radial(float* cx, float* cy, float* radius) const noexcept;
815
816 /**
817 * @brief Creates a new RadialGradient object.
818 *
819 * @return A new RadialGradient object.
820 */
821 static std::unique_ptr<RadialGradient> gen() noexcept;
822
823 /**
824 * @brief Returns the ID value of this class.
825 *
826 * This method can be used to check the current concrete instance type.
827 *
828 * @return The class type ID of the LinearGradient instance.
829 *
830 * @note Experimental API
831 */
832 Type type() const noexcept override;
833
834 /**
835 * @see RadialGradient::type()
836 */
837 TVG_DEPRECATED static uint32_t identifier() noexcept;
838
839 _TVG_DECLARE_PRIVATE(RadialGradient);
840 };
841
842
843 /**
844 * @class Shape
845 *
846 * @brief A class representing two-dimensional figures and their properties.
847 *
848 * A shape has three major properties: shape outline, stroking, filling. The outline in the Shape is retained as the path.
849 * Path can be composed by accumulating primitive commands such as moveTo(), lineTo(), cubicTo(), or complete shape interfaces such as appendRect(), appendCircle(), etc.
850 * Path can consists of sub-paths. One sub-path is determined by a close command.
851 *
852 * The stroke of Shape is an optional property in case the Shape needs to be represented with/without the outline borders.
853 * 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.
854 */
855 class TVG_API Shape final : public Paint
856 {
857 public:
858 ~Shape();
859
860 /**
861 * @brief Resets the shape path.
862 *
863 * The transformation matrix, color, fill, and stroke properties are retained.
864 *
865 * @note The memory where the path data is stored is not deallocated at this stage to allow for caching.
866 */
867 Result reset() noexcept;
868
869 /**
870 * @brief Sets the initial point of the sub-path.
871 *
872 * The value of the current point is set to the given point.
873 *
874 * @param[in] x The horizontal coordinate of the initial point of the sub-path.
875 * @param[in] y The vertical coordinate of the initial point of the sub-path.
876 */
877 Result moveTo(float x, float y) noexcept;
878
879 /**
880 * @brief Adds a new point to the sub-path, which results in drawing a line from the current point to the given end-point.
881 *
882 * The value of the current point is set to the given end-point.
883 *
884 * @param[in] x The horizontal coordinate of the end-point of the line.
885 * @param[in] y The vertical coordinate of the end-point of the line.
886 *
887 * @note In case this is the first command in the path, it corresponds to the moveTo() call.
888 */
889 Result lineTo(float x, float y) noexcept;
890
891 /**
892 * @brief Adds new points to the sub-path, which results in drawing a cubic Bezier curve starting
893 * 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).
894 *
895 * The value of the current point is set to the given end-point.
896 *
897 * @param[in] cx1 The horizontal coordinate of the 1st control point.
898 * @param[in] cy1 The vertical coordinate of the 1st control point.
899 * @param[in] cx2 The horizontal coordinate of the 2nd control point.
900 * @param[in] cy2 The vertical coordinate of the 2nd control point.
901 * @param[in] x The horizontal coordinate of the end-point of the curve.
902 * @param[in] y The vertical coordinate of the end-point of the curve.
903 *
904 * @note In case this is the first command in the path, no data from the path are rendered.
905 */
906 Result cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept;
907
908 /**
909 * @brief Closes the current sub-path by drawing a line from the current point to the initial point of the sub-path.
910 *
911 * The value of the current point is set to the initial point of the closed sub-path.
912 *
913 * @note In case the sub-path does not contain any points, this function has no effect.
914 */
915 Result close() noexcept;
916
917 /**
918 * @brief Appends a rectangle to the path.
919 *
920 * The rectangle with rounded corners can be achieved by setting non-zero values to @p rx and @p ry arguments.
921 * The @p rx and @p ry values specify the radii of the ellipse defining the rounding of the corners.
922 *
923 * The position of the rectangle is specified by the coordinates of its upper-left corner - @p x and @p y arguments.
924 *
925 * The rectangle is treated as a new sub-path - it is not connected with the previous sub-path.
926 *
927 * The value of the current point is set to (@p x + @p rx, @p y) - in case @p rx is greater
928 * than @p w/2 the current point is set to (@p x + @p w/2, @p y)
929 *
930 * @param[in] x The horizontal coordinate of the upper-left corner of the rectangle.
931 * @param[in] y The vertical coordinate of the upper-left corner of the rectangle.
932 * @param[in] w The width of the rectangle.
933 * @param[in] h The height of the rectangle.
934 * @param[in] rx The x-axis radius of the ellipse defining the rounded corners of the rectangle.
935 * @param[in] ry The y-axis radius of the ellipse defining the rounded corners of the rectangle.
936 *
937 * @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.
938 */
939 Result appendRect(float x, float y, float w, float h, float rx = 0, float ry = 0) noexcept;
940
941 /**
942 * @brief Appends an ellipse to the path.
943 *
944 * The position of the ellipse is specified by the coordinates of its center - @p cx and @p cy arguments.
945 *
946 * The ellipse is treated as a new sub-path - it is not connected with the previous sub-path.
947 *
948 * The value of the current point is set to (@p cx, @p cy - @p ry).
949 *
950 * @param[in] cx The horizontal coordinate of the center of the ellipse.
951 * @param[in] cy The vertical coordinate of the center of the ellipse.
952 * @param[in] rx The x-axis radius of the ellipse.
953 * @param[in] ry The y-axis radius of the ellipse.
954 *
955 */
956 Result appendCircle(float cx, float cy, float rx, float ry) noexcept;
957
958 /**
959 * @brief Appends a circular arc to the path.
960 *
961 * The arc is treated as a new sub-path - it is not connected with the previous sub-path.
962 * 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.
963 *
964 * @param[in] cx The horizontal coordinate of the center of the arc.
965 * @param[in] cy The vertical coordinate of the center of the arc.
966 * @param[in] radius The radius of the arc.
967 * @param[in] startAngle The start angle of the arc given in degrees, measured counter-clockwise from the horizontal line.
968 * @param[in] sweep The central angle of the arc given in degrees, measured counter-clockwise from @p startAngle.
969 * @param[in] pie Specifies whether to draw radii from the arc's center to both of its end-point - drawn if @c true.
970 *
971 * @note Setting @p sweep value greater than 360 degrees, is equivalent to calling appendCircle(cx, cy, radius, radius).
972 */
973 TVG_DEPRECATED Result appendArc(float cx, float cy, float radius, float startAngle, float sweep, bool pie) noexcept;
974
975 /**
976 * @brief Appends a given sub-path to the path.
977 *
978 * The current point value is set to the last point from the sub-path.
979 * For each command from the @p cmds array, an appropriate number of points in @p pts array should be specified.
980 * If the number of points in the @p pts array is different than the number required by the @p cmds array, the shape with this sub-path will not be displayed on the screen.
981 *
982 * @param[in] cmds The array of the commands in the sub-path.
983 * @param[in] cmdCnt The number of the sub-path's commands.
984 * @param[in] pts The array of the two-dimensional points.
985 * @param[in] ptsCnt The number of the points in the @p pts array.
986 *
987 * @note The interface is designed for optimal path setting if the caller has a completed path commands already.
988 */
989 Result appendPath(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept;
990
991 /**
992 * @brief Sets the stroke width for all of the figures from the path.
993 *
994 * @param[in] width The width of the stroke. The default value is 0.
995 *
996 */
997 Result stroke(float width) noexcept;
998
999 /**
1000 * @brief Sets the color of the stroke for all of the figures from the path.
1001 *
1002 * @param[in] r The red color channel value in the range [0 ~ 255]. The default value is 0.
1003 * @param[in] g The green color channel value in the range [0 ~ 255]. The default value is 0.
1004 * @param[in] b The blue color channel value in the range [0 ~ 255]. The default value is 0.
1005 * @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.
1006 *
1007 */
1008 Result stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) noexcept;
1009
1010 /**
1011 * @brief Sets the gradient fill of the stroke for all of the figures from the path.
1012 *
1013 * @param[in] f The gradient fill.
1014 *
1015 * @retval Result::MemoryCorruption In case a @c nullptr is passed as the argument.
1016 */
1017 Result stroke(std::unique_ptr<Fill> f) noexcept;
1018
1019 /**
1020 * @brief Sets the dash pattern of the stroke.
1021 *
1022 * @param[in] dashPattern The array of consecutive pair values of the dash length and the gap length.
1023 * @param[in] cnt The length of the @p dashPattern array.
1024 *
1025 * @retval Result::InvalidArguments In case @p dashPattern is @c nullptr and @p cnt > 0, @p cnt is zero, any of the dash pattern values is zero or less.
1026 *
1027 * @note To reset the stroke dash pattern, pass @c nullptr to @p dashPattern and zero to @p cnt.
1028 * @warning @p cnt must be greater than 1 if the dash pattern is valid.
1029 */
1030 Result stroke(const float* dashPattern, uint32_t cnt) noexcept;
1031
1032 /**
1033 * @brief Sets the cap style of the stroke in the open sub-paths.
1034 *
1035 * @param[in] cap The cap style value. The default value is @c StrokeCap::Square.
1036 *
1037 */
1038 Result stroke(StrokeCap cap) noexcept;
1039
1040 /**
1041 * @brief Sets the join style for stroked path segments.
1042 *
1043 * The join style is used for joining the two line segment while stroking the path.
1044 *
1045 * @param[in] join The join style value. The default value is @c StrokeJoin::Bevel.
1046 *
1047 */
1048 Result stroke(StrokeJoin join) noexcept;
1049
1050 /**
1051 * @brief Sets the stroke miterlimit.
1052 *
1053 * @param[in] miterlimit The miterlimit imposes a limit on the extent of the stroke join, when the @c StrokeJoin::Miter join style is set. The default value is 4.
1054 *
1055 * @retval Result::InvalidArgument for @p miterlimit values less than zero.
1056 *
1057 * @since 0.11
1058 */
1059 Result strokeMiterlimit(float miterlimit) noexcept;
1060
1061 /**
1062 * @brief Sets the trim of the stroke along the defined path segment, allowing control over which part of the stroke is visible.
1063 *
1064 * If the values of the arguments @p begin and @p end exceed the 0-1 range, they are wrapped around in a manner similar to angle wrapping, effectively treating the range as circular.
1065 *
1066 * @param[in] begin Specifies the start of the segment to display along the path.
1067 * @param[in] end Specifies the end of the segment to display along the path.
1068 * @param[in] simultaneous Determines how to trim multiple paths within a single shape. If set to @c true (default), trimming is applied simultaneously to all paths;
1069 * Otherwise, all paths are treated as a single entity with a combined length equal to the sum of their individual lengths and are trimmed as such.
1070 *
1071 * @note Experimental API
1072 */
1073 Result strokeTrim(float begin, float end, bool simultaneous = true) noexcept;
1074
1075 /**
1076 * @brief Sets the solid color for all of the figures from the path.
1077 *
1078 * The parts of the shape defined as inner are colored.
1079 *
1080 * @param[in] r The red color channel value in the range [0 ~ 255]. The default value is 0.
1081 * @param[in] g The green color channel value in the range [0 ~ 255]. The default value is 0.
1082 * @param[in] b The blue color channel value in the range [0 ~ 255]. The default value is 0.
1083 * @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.
1084 *
1085 * @note Either a solid color or a gradient fill is applied, depending on what was set as last.
1086 */
1087 Result fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) noexcept;
1088
1089 /**
1090 * @brief Sets the gradient fill for all of the figures from the path.
1091 *
1092 * The parts of the shape defined as inner are filled.
1093 *
1094 * @param[in] f The unique pointer to the gradient fill.
1095 *
1096 * @note Either a solid color or a gradient fill is applied, depending on what was set as last.
1097 */
1098 Result fill(std::unique_ptr<Fill> f) noexcept;
1099
1100 /**
1101 * @brief Sets the fill rule for the Shape object.
1102 *
1103 * @param[in] r The fill rule value. The default value is @c FillRule::Winding.
1104 */
1105 Result fill(FillRule r) noexcept;
1106
1107 /**
1108 * @brief Sets the rendering order of the stroke and the fill.
1109 *
1110 * @param[in] strokeFirst If @c true the stroke is rendered before the fill, otherwise the stroke is rendered as the second one (the default option).
1111 *
1112 * @since 0.10
1113 */
1114 Result order(bool strokeFirst) noexcept;
1115
1116 /**
1117 * @brief Gets the commands data of the path.
1118 *
1119 * @param[out] cmds The pointer to the array of the commands from the path.
1120 *
1121 * @return The length of the @p cmds array when succeed, zero otherwise.
1122 */
1123 uint32_t pathCommands(const PathCommand** cmds) const noexcept;
1124
1125 /**
1126 * @brief Gets the points values of the path.
1127 *
1128 * @param[out] pts The pointer to the array of the two-dimensional points from the path.
1129 *
1130 * @return The length of the @p pts array when succeed, zero otherwise.
1131 */
1132 uint32_t pathCoords(const Point** pts) const noexcept;
1133
1134 /**
1135 * @brief Gets the pointer to the gradient fill of the shape.
1136 *
1137 * @return The pointer to the gradient fill of the stroke when succeed, @c nullptr in case no fill was set.
1138 */
1139 const Fill* fill() const noexcept;
1140
1141 /**
1142 * @brief Gets the solid color of the shape.
1143 *
1144 * @param[out] r The red color channel value in the range [0 ~ 255].
1145 * @param[out] g The green color channel value in the range [0 ~ 255].
1146 * @param[out] b The blue color channel value in the range [0 ~ 255].
1147 * @param[out] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque.
1148 *
1149 */
1150 Result fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a = nullptr) const noexcept;
1151
1152 /**
1153 * @brief Gets the fill rule value.
1154 *
1155 * @return The fill rule value of the shape.
1156 */
1157 FillRule fillRule() const noexcept;
1158
1159 /**
1160 * @brief Gets the stroke width.
1161 *
1162 * @return The stroke width value when succeed, zero if no stroke was set.
1163 */
1164 float strokeWidth() const noexcept;
1165
1166 /**
1167 * @brief Gets the color of the shape's stroke.
1168 *
1169 * @param[out] r The red color channel value in the range [0 ~ 255].
1170 * @param[out] g The green color channel value in the range [0 ~ 255].
1171 * @param[out] b The blue color channel value in the range [0 ~ 255].
1172 * @param[out] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque.
1173 *
1174 */
1175 Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a = nullptr) const noexcept;
1176
1177 /**
1178 * @brief Gets the pointer to the gradient fill of the stroke.
1179 *
1180 * @return The pointer to the gradient fill of the stroke when succeed, @c nullptr otherwise.
1181 */
1182 const Fill* strokeFill() const noexcept;
1183
1184 /**
1185 * @brief Gets the dash pattern of the stroke.
1186 *
1187 * @param[out] dashPattern The pointer to the memory, where the dash pattern array is stored.
1188 *
1189 * @return The length of the @p dashPattern array.
1190 */
1191 uint32_t strokeDash(const float** dashPattern) const noexcept;
1192
1193 /**
1194 * @brief Gets the cap style used for stroking the path.
1195 *
1196 * @return The cap style value of the stroke.
1197 */
1198 StrokeCap strokeCap() const noexcept;
1199
1200 /**
1201 * @brief Gets the join style value used for stroking the path.
1202 *
1203 * @return The join style value of the stroke.
1204 */
1205 StrokeJoin strokeJoin() const noexcept;
1206
1207 /**
1208 * @brief Gets the stroke miterlimit.
1209 *
1210 * @return The stroke miterlimit value when succeed, 4 if no stroke was set.
1211 *
1212 * @since 0.11
1213 */
1214 float strokeMiterlimit() const noexcept;
1215
1216 /**
1217 * @brief Creates a new Shape object.
1218 *
1219 * @return A new Shape object.
1220 */
1221 static std::unique_ptr<Shape> gen() noexcept;
1222
1223 /**
1224 * @brief Returns the ID value of this class.
1225 *
1226 * This method can be used to check the current concrete instance type.
1227 *
1228 * @return The class type ID of the Shape instance.
1229 *
1230 * @note Experimental API
1231 */
1232 Type type() const noexcept override;
1233
1234 /**
1235 * @see Shape::type()
1236 */
1237 TVG_DEPRECATED static uint32_t identifier() noexcept;
1238
1239 _TVG_DECLARE_PRIVATE(Shape);
1240 };
1241
1242
1243 /**
1244 * @class Picture
1245 *
1246 * @brief A class representing an image read in one of the supported formats: raw, svg, png, jpg, lottie(json) and etc.
1247 * Besides the methods inherited from the Paint, it provides methods to load & draw images on the canvas.
1248 *
1249 * @note Supported formats are depended on the available TVG loaders.
1250 * @note See Animation class if the picture data is animatable.
1251 */
1252 class TVG_API Picture final : public Paint
1253 {
1254 public:
1255 ~Picture();
1256
1257 /**
1258 * @brief Loads a picture data directly from a file.
1259 *
1260 * ThorVG efficiently caches the loaded data using the specified @p path as a key.
1261 * This means that loading the same file again will not result in duplicate operations;
1262 * instead, ThorVG will reuse the previously loaded picture data.
1263 *
1264 * @param[in] path A path to the picture file.
1265 *
1266 * @retval Result::InvalidArguments In case the @p path is invalid.
1267 * @retval Result::NonSupport When trying to load a file with an unknown extension.
1268 *
1269 * @note The Load behavior can be asynchronous if the assigned thread number is greater than zero.
1270 * @see Initializer::init()
1271 */
1272 Result load(const std::string& path) noexcept;
1273
1274 /**
1275 * @deprecated Use load(const char* data, uint32_t size, const std::string& mimeType, bool copy) instead.
1276 */
1277 TVG_DEPRECATED Result load(const char* data, uint32_t size, bool copy = false) noexcept;
1278
1279 /**
1280 * @brief Loads a picture data from a memory block of a given size.
1281 *
1282 * ThorVG efficiently caches the loaded data using the specified @p data address as a key
1283 * when the @p copy has @c false. This means that loading the same data again will not result in duplicate operations
1284 * for the sharable @p data. Instead, ThorVG will reuse the previously loaded picture data.
1285 *
1286 * @param[in] data A pointer to a memory location where the content of the picture file is stored. A null-terminated string is expected for non-binary data if @p copy is @c false.
1287 * @param[in] size The size in bytes of the memory occupied by the @p data.
1288 * @param[in] mimeType Mimetype or extension of data such as "jpg", "jpeg", "lottie", "svg", "svg+xml", "png", etc. In case an empty string or an unknown type is provided, the loaders will be tried one by one.
1289 * @param[in] copy If @c true the data are copied into the engine local buffer, otherwise they are not.
1290 *
1291 * @retval Result::InvalidArguments In case no data are provided or the @p size is zero or less.
1292 * @retval Result::NonSupport When trying to load a file with an unknown extension.
1293 *
1294 * @warning It's the user responsibility to release the @p data memory.
1295 *
1296 * @note If you are unsure about the MIME type, you can provide an empty value like @c "", and thorvg will attempt to figure it out.
1297 * @since 0.5
1298 */
1299 Result load(const char* data, uint32_t size, const std::string& mimeType, bool copy = false) noexcept;
1300
1301 /**
1302 * @brief Resizes the picture content to the given width and height.
1303 *
1304 * The picture content is resized while keeping the default size aspect ratio.
1305 * The scaling factor is established for each of dimensions and the smaller value is applied to both of them.
1306 *
1307 * @param[in] w A new width of the image in pixels.
1308 * @param[in] h A new height of the image in pixels.
1309 *
1310 */
1311 Result size(float w, float h) noexcept;
1312
1313 /**
1314 * @brief Gets the size of the image.
1315 *
1316 * @param[out] w The width of the image in pixels.
1317 * @param[out] h The height of the image in pixels.
1318 *
1319 */
1320 Result size(float* w, float* h) const noexcept;
1321
1322 /**
1323 * @brief Loads raw data in ARGB8888 format from a memory block of the given size.
1324 *
1325 * ThorVG efficiently caches the loaded data using the specified @p data address as a key
1326 * when the @p copy has @c false. This means that loading the same data again will not result in duplicate operations
1327 * for the sharable @p data. Instead, ThorVG will reuse the previously loaded picture data.
1328 *
1329 * @param[in] data A pointer to a memory location where the content of the picture raw data is stored.
1330 * @param[in] w The width of the image @p data in pixels.
1331 * @param[in] h The height of the image @p data in pixels.
1332 * @param[in] premultiplied If @c true, the given image data is alpha-premultiplied.
1333 * @param[in] copy If @c true the data are copied into the engine local buffer, otherwise they are not.
1334 *
1335 * @since 0.9
1336 */
1337 Result load(uint32_t* data, uint32_t w, uint32_t h, bool copy) noexcept;
1338
1339 /**
1340 * @brief Retrieve a paint object from the Picture scene by its Unique ID.
1341 *
1342 * This function searches for a paint object within the Picture scene that matches the provided @p id.
1343 *
1344 * @param[in] id The Unique ID of the paint object.
1345 *
1346 * @return A pointer to the paint object that matches the given identifier, or @c nullptr if no matching paint object is found.
1347 *
1348 * @see Accessor::id()
1349 *
1350 * @note Experimental API
1351 */
1352 const Paint* paint(uint32_t id) noexcept;
1353
1354 /**
1355 * @brief Creates a new Picture object.
1356 *
1357 * @return A new Picture object.
1358 */
1359 static std::unique_ptr<Picture> gen() noexcept;
1360
1361 /**
1362 * @brief Returns the ID value of this class.
1363 *
1364 * This method can be used to check the current concrete instance type.
1365 *
1366 * @return The class type ID of the Picture instance.
1367 *
1368 * @note Experimental API
1369 */
1370 Type type() const noexcept override;
1371
1372 /**
1373 * @see Picture::type()
1374 */
1375 TVG_DEPRECATED static uint32_t identifier() noexcept;
1376
1377 _TVG_DECLARE_ACCESSOR(Animation);
1378 _TVG_DECLARE_PRIVATE(Picture);
1379 };
1380
1381
1382 /**
1383 * @class Scene
1384 *
1385 * @brief A class to composite children paints.
1386 *
1387 * As the traditional graphics rendering method, TVG also enables scene-graph mechanism.
1388 * This feature supports an array function for managing the multiple paints as one group paint.
1389 *
1390 * As a group, the scene can be transformed, made translucent and composited with other target paints,
1391 * its children will be affected by the scene world.
1392 */
1393 class TVG_API Scene final : public Paint
1394 {
1395 public:
1396 ~Scene();
1397
1398 /**
1399 * @brief Passes drawing elements to the Scene using Paint objects.
1400 *
1401 * Only the paints pushed into the scene will be the drawn targets.
1402 * The paints are retained by the scene until Scene::clear() is called.
1403 *
1404 * @param[in] paint A Paint object to be drawn.
1405 *
1406 * @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.
1407 * @see Scene::paints()
1408 * @see Scene::clear()
1409 */
1410 Result push(std::unique_ptr<Paint> paint) noexcept;
1411
1412 TVG_DEPRECATED Result reserve(uint32_t size) noexcept;
1413
1414 /**
1415 * @brief Returns the list of the paints that currently held by the Scene.
1416 *
1417 * This function provides the list of paint nodes, allowing users a direct opportunity to modify the scene tree.
1418 *
1419 * @warning Please avoid accessing the paints during Scene update/draw. You can access them after calling Canvas::sync().
1420 * @see Canvas::sync()
1421 * @see Scene::push(std::unique_ptr<Paint> paint)
1422 * @see Scene::clear()
1423 *
1424 * @note Experimental API
1425 */
1426 std::list<Paint*>& paints() noexcept;
1427
1428 /**
1429 * @brief Sets the total number of the paints pushed into the scene to be zero.
1430 * Depending on the value of the @p free argument, the paints are freed or not.
1431 *
1432 * @param[in] free If @c true, the memory occupied by paints is deallocated, otherwise it is not.
1433 *
1434 * @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.
1435 *
1436 * @since 0.2
1437 */
1438 Result clear(bool free = true) noexcept;
1439
1440 /**
1441 * @brief Apply a post-processing effect to the scene.
1442 *
1443 * This function adds a specified scene effect, such as clearing all effects or applying a Gaussian blur,
1444 * to the scene after it has been rendered. Multiple effects can be applied in sequence.
1445 *
1446 * @param[in] effect The scene effect to apply. Options are defined in the SceneEffect enum.
1447 * For example, use SceneEffect::GaussianBlur to apply a blur with specific parameters.
1448 * @param[in] ... Additional variadic parameters required for certain effects (e.g., sigma and direction for GaussianBlur).
1449 *
1450 * @note Experimental API
1451 */
1452 Result push(SceneEffect effect, ...) noexcept;
1453
1454 /**
1455 * @brief Creates a new Scene object.
1456 *
1457 * @return A new Scene object.
1458 */
1459 static std::unique_ptr<Scene> gen() noexcept;
1460
1461 /**
1462 * @brief Returns the ID value of this class.
1463 *
1464 * This method can be used to check the current concrete instance type.
1465 *
1466 * @return The class type ID of the Scene instance.
1467 *
1468 * @note Experimental API
1469 */
1470 Type type() const noexcept override;
1471
1472 /**
1473 * @see Scene::type()
1474 */
1475 TVG_DEPRECATED static uint32_t identifier() noexcept;
1476
1477 _TVG_DECLARE_PRIVATE(Scene);
1478 };
1479
1480
1481 /**
1482 * @class Text
1483 *
1484 * @brief A class to represent text objects in a graphical context, allowing for rendering and manipulation of unicode text.
1485 *
1486 * @since 0.15
1487 */
1488 class TVG_API Text final : public Paint
1489 {
1490 public:
1491 ~Text();
1492
1493 /**
1494 * @brief Sets the font properties for the text.
1495 *
1496 * This function allows you to define the font characteristics used for text rendering.
1497 * It sets the font name, size and optionally the style.
1498 *
1499 * @param[in] name The name of the font. This should correspond to a font available in the canvas.
1500 * If set to @c nullptr, ThorVG will attempt to select a fallback font available on the system.
1501 * @param[in] size The size of the font in points. This determines how large the text will appear.
1502 * @param[in] style The style of the font. It can be used to set the font to 'italic'.
1503 * If not specified, the default style is used. Only 'italic' style is supported currently.
1504 *
1505 * @retval Result::InsufficientCondition when the specified @p name cannot be found.
1506 *
1507 * @note If the @p name is not specified, ThorVG will select any available font candidate.
1508 * @since 1.0
1509 *
1510 * @code
1511 * // Tip for fallback support to use any available font.
1512 * if (text->font("Arial", 24) != tvg::Result::Success) {
1513 * text->font(nullptr, 24);
1514 * }
1515 * @endcode
1516 */
1517 Result font(const char* name, float size, const char* style = nullptr) noexcept;
1518
1519 /**
1520 * @brief Assigns the given unicode text to be rendered.
1521 *
1522 * This function sets the unicode string that will be displayed by the rendering system.
1523 * The text is set according to the specified UTF encoding method, which defaults to UTF-8.
1524 *
1525 * @param[in] text The multi-byte text encoded with utf8 string to be rendered.
1526 *
1527 * @note Experimental API
1528 */
1529 Result text(const char* text) noexcept;
1530
1531 /**
1532 * @brief Sets the text color.
1533 *
1534 * @param[in] r The red color channel value in the range [0 ~ 255]. The default value is 0.
1535 * @param[in] g The green color channel value in the range [0 ~ 255]. The default value is 0.
1536 * @param[in] b The blue color channel value in the range [0 ~ 255]. The default value is 0.
1537 *
1538 * @see Text::font()
1539 *
1540 * @since 0.15
1541 */
1542 Result fill(uint8_t r, uint8_t g, uint8_t b) noexcept;
1543
1544 /**
1545 * @brief Sets the gradient fill for all of the figures from the text.
1546 *
1547 * The parts of the text defined as inner are filled.
1548 *
1549 * @param[in] f The unique pointer to the gradient fill.
1550 *
1551 * @note Either a solid color or a gradient fill is applied, depending on what was set as last.
1552 * @see Text::font()
1553 *
1554 * @since 0.15
1555 */
1556 Result fill(std::unique_ptr<Fill> f) noexcept;
1557
1558 /**
1559 * @brief Loads a scalable font data (ttf) from a file.
1560 *
1561 * ThorVG efficiently caches the loaded data using the specified @p path as a key.
1562 * This means that loading the same file again will not result in duplicate operations;
1563 * instead, ThorVG will reuse the previously loaded font data.
1564 *
1565 * @param[in] path The path to the font file.
1566 *
1567 * @retval Result::InvalidArguments In case the @p path is invalid.
1568 * @retval Result::NonSupport When trying to load a file with an unknown extension.
1569 *
1570 * @see Text::unload(const std::string& path)
1571 *
1572 * @since 0.15
1573 */
1574 static Result load(const std::string& path) noexcept;
1575
1576 /**
1577 * @brief Loads a scalable font data (ttf) from a memory block of a given size.
1578 *
1579 * ThorVG efficiently caches the loaded font data using the specified @p name as a key.
1580 * This means that loading the same fonts again will not result in duplicate operations.
1581 * Instead, ThorVG will reuse the previously loaded font data.
1582 *
1583 * @param[in] name The name under which the font will be stored and accessible (e.x. in a @p font() API).
1584 * @param[in] data A pointer to a memory location where the content of the font data is stored.
1585 * @param[in] size The size in bytes of the memory occupied by the @p data.
1586 * @param[in] mimeType Mimetype or extension of font data. In case an empty string is provided the loader will be determined automatically.
1587 * @param[in] copy If @c true the data are copied into the engine local buffer, otherwise they are not (default).
1588 *
1589 * @retval Result::InvalidArguments If no name is provided or if @p size is zero while @p data points to a valid memory location.
1590 * @retval Result::NonSupport When trying to load a file with an unsupported extension.
1591 * @retval Result::InsufficientCondition If attempting to unload the font data that has not been previously loaded.
1592 *
1593 * @warning It's the user responsibility to release the @p data memory.
1594 *
1595 * @note To unload the font data loaded using this API, pass the proper @p name and @c nullptr as @p data.
1596 * @note If you are unsure about the MIME type, you can provide an empty value like @c "", and thorvg will attempt to figure it out.
1597 * @see Text::font(const char* name, float size, const char* style)
1598 *
1599 * @note 0.15
1600 */
1601 static Result load(const char* name, const char* data, uint32_t size, const std::string& mimeType = "ttf", bool copy = false) noexcept;
1602
1603 /**
1604 * @brief Unloads the specified scalable font data (TTF) that was previously loaded.
1605 *
1606 * This function is used to release resources associated with a font file that has been loaded into memory.
1607 *
1608 * @param[in] path The file path of the loaded font.
1609 *
1610 * @retval Result::InsufficientCondition Fails if the loader is not initialized.
1611 *
1612 * @note If the font data is currently in use, it will not be immediately unloaded.
1613 * @see Text::load(const std::string& path)
1614 *
1615 * @since 0.15
1616 */
1617 static Result unload(const std::string& path) noexcept;
1618
1619 /**
1620 * @brief Creates a new Text object.
1621 *
1622 * @return A new Text object.
1623 *
1624 * @since 0.15
1625 */
1626 static std::unique_ptr<Text> gen() noexcept;
1627
1628 /**
1629 * @brief Returns the ID value of this class.
1630 *
1631 * This method can be used to check the current concrete instance type.
1632 *
1633 * @return The class type ID of the Text instance.
1634 *
1635 * @note Experimental API
1636 */
1637 Type type() const noexcept override;
1638
1639 _TVG_DECLARE_PRIVATE(Text);
1640 };
1641
1642
1643 /**
1644 * @class SwCanvas
1645 *
1646 * @brief A class for the rendering graphical elements with a software raster engine.
1647 */
1648 class TVG_API SwCanvas final : public Canvas
1649 {
1650 public:
1651 ~SwCanvas();
1652
1653 /**
1654 * @brief Enumeration specifying the methods of combining the 8-bit color channels into 32-bit color.
1655 */
1656 enum Colorspace
1657 {
1658 ABGR8888 = 0, ///< The channels are joined in the order: alpha, blue, green, red. Colors are alpha-premultiplied. (a << 24 | b << 16 | g << 8 | r)
1659 ARGB8888, ///< The channels are joined in the order: alpha, red, green, blue. Colors are alpha-premultiplied. (a << 24 | r << 16 | g << 8 | b)
1660 ABGR8888S, ///< The channels are joined in the order: alpha, blue, green, red. Colors are un-alpha-premultiplied. @since 0.12
1661 ARGB8888S, ///< The channels are joined in the order: alpha, red, green, blue. Colors are un-alpha-premultiplied. @since 0.12
1662 };
1663
1664 /**
1665 * @brief Enumeration specifying the methods of Memory Pool behavior policy.
1666 * @since 0.4
1667 */
1668 enum MempoolPolicy
1669 {
1670 Default = 0, ///< Default behavior that ThorVG is designed to.
1671 Shareable, ///< Memory Pool is shared among the SwCanvases.
1672 Individual ///< Allocate designated memory pool that is only used by current instance.
1673 };
1674
1675 /**
1676 * @brief Sets the drawing target for the rasterization.
1677 *
1678 * The buffer of a desirable size should be allocated and owned by the caller.
1679 *
1680 * @param[in] buffer A pointer to a memory block of the size @p stride x @p h, where the raster data are stored.
1681 * @param[in] stride The stride of the raster image - greater than or equal to @p w.
1682 * @param[in] w The width of the raster image.
1683 * @param[in] h The height of the raster image.
1684 * @param[in] cs The value specifying the way the 32-bits colors should be read/written.
1685 *
1686 * @retval Result::InvalidArguments In case no valid pointer is provided or the width, or the height or the stride is zero.
1687 * @retval Result::InsufficientCondition if the canvas is performing rendering. Please ensure the canvas is synced.
1688 * @retval Result::NonSupport In case the software engine is not supported.
1689 *
1690 * @warning Do not access @p buffer during Canvas::push() - Canvas::sync(). It should not be accessed while the engine is writing on it.
1691 *
1692 * @see Canvas::viewport()
1693 * @see Canvas::sync()
1694 */
1695 Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, Colorspace cs) noexcept;
1696
1697 /**
1698 * @brief Set sw engine memory pool behavior policy.
1699 *
1700 * Basically ThorVG draws a lot of shapes, it allocates/deallocates a few chunk of memory
1701 * while processing rendering. It internally uses one shared memory pool
1702 * which can be reused among the canvases in order to avoid memory overhead.
1703 *
1704 * Thus ThorVG suggests using a memory pool policy to satisfy user demands,
1705 * if it needs to guarantee the thread-safety of the internal data access.
1706 *
1707 * @param[in] policy The method specifying the Memory Pool behavior. The default value is @c MempoolPolicy::Default.
1708 *
1709 * @retval Result::InsufficientCondition If the canvas contains some paints already.
1710 * @retval Result::NonSupport In case the software engine is not supported.
1711 *
1712 * @note When @c policy is set as @c MempoolPolicy::Individual, the current instance of canvas uses its own individual
1713 * memory data, which is not shared with others. This is necessary when the canvas is accessed on a worker-thread.
1714 *
1715 * @warning It's not allowed after pushing any paints.
1716 *
1717 * @since 0.4
1718 */
1719 Result mempool(MempoolPolicy policy) noexcept;
1720
1721 /**
1722 * @brief Creates a new SwCanvas object.
1723 * @return A new SwCanvas object.
1724 */
1725 static std::unique_ptr<SwCanvas> gen() noexcept;
1726
1727 _TVG_DECLARE_PRIVATE(SwCanvas);
1728 };
1729
1730
1731 /**
1732 * @class GlCanvas
1733 *
1734 * @brief A class for the rendering graphic elements with a GL raster engine.
1735 *
1736 * @since 0.14
1737 */
1738 class TVG_API GlCanvas final : public Canvas
1739 {
1740 public:
1741 ~GlCanvas();
1742
1743 /**
1744 * @brief Sets the drawing target for rasterization.
1745 *
1746 * This function specifies the drawing target where the rasterization will occur. It can target
1747 * a specific framebuffer object (FBO) or the main surface.
1748 *
1749 * @param[in] id The GL target ID, usually indicating the FBO ID. A value of @c 0 specifies the main surface.
1750 * @param[in] w The width (in pixels) of the raster image.
1751 * @param[in] h The height (in pixels) of the raster image.
1752 *
1753 * @retval Result::InsufficientCondition if the canvas is performing rendering. Please ensure the canvas is synced.
1754 * @retval Result::NonSupport In case the gl engine is not supported.
1755 *
1756 * @see Canvas::viewport()
1757 * @see Canvas::sync()
1758 *
1759 * @note Currently, this only allows the GL_RGBA8 color space format.
1760 * @note Experimental API
1761 */
1762 Result target(int32_t id, uint32_t w, uint32_t h) noexcept;
1763
1764 /**
1765 * @brief Creates a new GlCanvas object.
1766 *
1767 * @return A new GlCanvas object.
1768 *
1769 * @since 0.14
1770 */
1771 static std::unique_ptr<GlCanvas> gen() noexcept;
1772
1773 _TVG_DECLARE_PRIVATE(GlCanvas);
1774 };
1775
1776
1777 /**
1778 * @class WgCanvas
1779 *
1780 * @brief A class for the rendering graphic elements with a WebGPU raster engine.
1781 *
1782 * @warning Please do not use it. This class is not fully supported yet.
1783 *
1784 * @since 0.15
1785 */
1786 class TVG_API WgCanvas final : public Canvas
1787 {
1788 public:
1789 ~WgCanvas();
1790
1791 /**
1792 * @brief Sets the drawing target for the rasterization.
1793 *
1794 * @param[in] instance WGPUInstance, context for all other wgpu objects.
1795 * @param[in] surface WGPUSurface, handle to a presentable surface.
1796 * @param[in] w The width of the surface.
1797 * @param[in] h The height of the surface.
1798 * @param[in] device WGPUDevice, a desired handle for the wgpu device. If it is @c nullptr, ThorVG will assign an appropriate device internally.
1799 *
1800 * @retval Result::InsufficientCondition if the canvas is performing rendering. Please ensure the canvas is synced.
1801 * @retval Result::NonSupport In case the wg engine is not supported.
1802 *
1803 * @note Experimental API
1804 *
1805 * @see Canvas::viewport()
1806 * @see Canvas::sync()
1807 */
1808 Result target(void* instance, void* surface, uint32_t w, uint32_t h, void* device = nullptr) noexcept;
1809
1810 /**
1811 * @brief Creates a new WgCanvas object.
1812 *
1813 * @return A new WgCanvas object.
1814 *
1815 * @since 0.15
1816 */
1817 static std::unique_ptr<WgCanvas> gen() noexcept;
1818
1819 _TVG_DECLARE_PRIVATE(WgCanvas);
1820 };
1821
1822
1823 /**
1824 * @class Initializer
1825 *
1826 * @brief A class that enables initialization and termination of the TVG engines.
1827 */
1828 class TVG_API Initializer final
1829 {
1830 public:
1831 /**
1832 * @brief Initializes TVG engines.
1833 *
1834 * TVG requires the running-engine environment.
1835 * TVG runs its own task-scheduler for parallelizing rendering tasks efficiently.
1836 * You can indicate the number of threads, the count of which is designated @p threads.
1837 * In the initialization step, TVG will generate/spawn the threads as set by @p threads count.
1838 *
1839 * @param[in] engine The engine types to initialize. This is relative to the Canvas types, in which it will be used. For multiple backends bitwise operation is allowed.
1840 * @param[in] threads The number of additional threads. Zero indicates only the main thread is to be used.
1841 *
1842 * @retval Result::NonSupport In case the engine type is not supported on the system.
1843 *
1844 * @note The Initializer keeps track of the number of times it was called. Threads count is fixed at the first init() call.
1845 * @see Initializer::term()
1846 */
1847 static Result init(CanvasEngine engine, uint32_t threads) noexcept;
1848
1849 /**
1850 * @brief Terminates TVG engines.
1851 *
1852 * @param[in] engine The engine types to terminate. This is relative to the Canvas types, in which it will be used. For multiple backends bitwise operation is allowed
1853 *
1854 * @retval Result::InsufficientCondition In case there is nothing to be terminated.
1855 * @retval Result::NonSupport In case the engine type is not supported on the system.
1856 *
1857 * @note Initializer does own reference counting for multiple calls.
1858 * @see Initializer::init()
1859 */
1860 static Result term(CanvasEngine engine) noexcept;
1861
1862 /**
1863 * @brief Retrieves the version of the TVG engine.
1864 *
1865 * @param[out] major A major version number.
1866 * @param[out] minor A minor version number.
1867 * @param[out] micro A micro version number.
1868 *
1869 * @return The version of the engine in the format major.minor.micro, or a @p nullptr in case of an internal error.
1870 *
1871 * @since 0.15
1872 */
1873 static const char* version(uint32_t* major, uint32_t* minor, uint32_t* micro) noexcept;
1874
1875 _TVG_DISABLE_CTOR(Initializer);
1876 };
1877
1878
1879 /**
1880 * @class Animation
1881 *
1882 * @brief The Animation class enables manipulation of animatable images.
1883 *
1884 * This class supports the display and control of animation frames.
1885 *
1886 * @since 0.13
1887 */
1888
1889 class TVG_API Animation
1890 {
1891 public:
1892 ~Animation();
1893
1894 /**
1895 * @brief Specifies the current frame in the animation.
1896 *
1897 * @param[in] no The index of the animation frame to be displayed. The index should be less than the totalFrame().
1898 *
1899 * @retval Result::InsufficientCondition if the given @p no is the same as the current frame value.
1900 * @retval Result::NonSupport The current Picture data does not support animations.
1901 *
1902 * @note For efficiency, ThorVG ignores updates to the new frame value if the difference from the current frame value
1903 * is less than 0.001. In such cases, it returns @c Result::InsufficientCondition.
1904 * Values less than 0.001 may be disregarded and may not be accurately retained by the Animation.
1905 *
1906 * @see totalFrame()
1907 *
1908 */
1909 Result frame(float no) noexcept;
1910
1911 /**
1912 * @brief Retrieves a picture instance associated with this animation instance.
1913 *
1914 * This function provides access to the picture instance that can be used to load animation formats, such as Lottie(json).
1915 * After setting up the picture, it can be pushed to the designated canvas, enabling control over animation frames
1916 * with this Animation instance.
1917 *
1918 * @return A picture instance that is tied to this animation.
1919 *
1920 * @warning The picture instance is owned by Animation. It should not be deleted manually.
1921 *
1922 */
1923 Picture* picture() const noexcept;
1924
1925 /**
1926 * @brief Retrieves the current frame number of the animation.
1927 *
1928 * @return The current frame number of the animation, between 0 and totalFrame() - 1.
1929 *
1930 * @note If the Picture is not properly configured, this function will return 0.
1931 *
1932 * @see Animation::frame(float no)
1933 * @see Animation::totalFrame()
1934 *
1935 */
1936 float curFrame() const noexcept;
1937
1938 /**
1939 * @brief Retrieves the total number of frames in the animation.
1940 *
1941 * @return The total number of frames in the animation.
1942 *
1943 * @note Frame numbering starts from 0.
1944 * @note If the Picture is not properly configured, this function will return 0.
1945 *
1946 */
1947 float totalFrame() const noexcept;
1948
1949 /**
1950 * @brief Retrieves the duration of the animation in seconds.
1951 *
1952 * @return The duration of the animation in seconds.
1953 *
1954 * @note If the Picture is not properly configured, this function will return 0.
1955 *
1956 */
1957 float duration() const noexcept;
1958
1959 /**
1960 * @brief Specifies the playback segment of the animation.
1961 *
1962 * The set segment is designated as the play area of the animation.
1963 * This is useful for playing a specific segment within the entire animation.
1964 * After setting, the number of animation frames and the playback time are calculated
1965 * by mapping the playback segment as the entire range.
1966 *
1967 * @param[in] begin segment start.
1968 * @param[in] end segment end.
1969 *
1970 * @retval Result::InsufficientCondition In case the animation is not loaded.
1971 * @retval Result::NonSupport When it's not animatable.
1972 *
1973 * @note Animation allows a range from 0.0 to 1.0. @p end should not be higher than @p begin.
1974 * @note If a marker has been specified, its range will be disregarded.
1975 * @see LottieAnimation::segment(const char* marker)
1976 *
1977 * @note Experimental API
1978 */
1979 Result segment(float begin, float end) noexcept;
1980
1981 /**
1982 * @brief Gets the current segment.
1983 *
1984 * @param[out] begin segment start.
1985 * @param[out] end segment end.
1986 *
1987 * @retval Result::InsufficientCondition In case the animation is not loaded.
1988 * @retval Result::NonSupport When it's not animatable.
1989 *
1990 * @note Experimental API
1991 */
1992 Result segment(float* begin, float* end = nullptr) noexcept;
1993
1994 /**
1995 * @brief Creates a new Animation object.
1996 *
1997 * @return A new Animation object.
1998 *
1999 */
2000 static std::unique_ptr<Animation> gen() noexcept;
2001
2002 _TVG_DECLARE_PRIVATE(Animation);
2003 };
2004
2005
2006 /**
2007 * @class Saver
2008 *
2009 * @brief A class for exporting a paint object into a specified file, from which to recover the paint data later.
2010 *
2011 * ThorVG provides a feature for exporting & importing paint data. The Saver role is to export the paint data to a file.
2012 * It's useful when you need to save the composed scene or image from a paint object and recreate it later.
2013 *
2014 * The file format is decided by the extension name(i.e. "*.tvg") while the supported formats depend on the TVG packaging environment.
2015 * If it doesn't support the file format, the save() method returns the @c Result::NonSupport result.
2016 *
2017 * Once you export a paint to the file successfully, you can recreate it using the Picture class.
2018 *
2019 * @see Picture::load()
2020 *
2021 * @since 0.5
2022 */
2023 class TVG_API Saver final
2024 {
2025 public:
2026 ~Saver();
2027
2028 /**
2029 * @brief Sets the base background content for the saved image.
2030 *
2031 * @param[in] paint The paint to be drawn as the background image for the saving paint.
2032 *
2033 * @note Experimental API
2034 */
2035 Result background(std::unique_ptr<Paint> paint) noexcept;
2036
2037 /**
2038 * @brief Exports the given @p paint data to the given @p path
2039 *
2040 * If the saver module supports any compression mechanism, it will optimize the data size.
2041 * This might affect the encoding/decoding time in some cases. You can turn off the compression
2042 * if you wish to optimize for speed.
2043 *
2044 * @param[in] paint The paint to be saved with all its associated properties.
2045 * @param[in] path A path to the file, in which the paint data is to be saved.
2046 * @param[in] compress If @c true then compress data if possible.
2047 *
2048 * @retval Result::InsufficientCondition If currently saving other resources.
2049 * @retval Result::NonSupport When trying to save a file with an unknown extension or in an unsupported format.
2050 * @retval Result::Unknown In case an empty paint is to be saved.
2051 *
2052 * @note Saving can be asynchronous if the assigned thread number is greater than zero. To guarantee the saving is done, call sync() afterwards.
2053 * @see Saver::sync()
2054 *
2055 * @since 0.5
2056 */
2057 Result save(std::unique_ptr<Paint> paint, const std::string& path, bool compress = true) noexcept;
2058
2059 /**
2060 * @brief Export the provided animation data to the specified file path.
2061 *
2062 * This function exports the given animation data to the provided file path. You can also specify the desired frame rate in frames per second (FPS) by providing the fps parameter.
2063 *
2064 * @param[in] animation The animation to be saved, including all associated properties.
2065 * @param[in] path The path to the file where the animation will be saved.
2066 * @param[in] quality The encoded quality level. @c 0 is the minimum, @c 100 is the maximum value(recommended).
2067 * @param[in] fps The desired frames per second (FPS). For example, to encode data at 60 FPS, pass 60. Pass 0 to keep the original frame data.
2068 *
2069 * @retval Result::InsufficientCondition if there are ongoing resource-saving operations.
2070 * @retval Result::NonSupport if an attempt is made to save the file with an unknown extension or in an unsupported format.
2071 * @retval Result::Unknown if attempting to save an empty paint.
2072 *
2073 * @note A higher frames per second (FPS) would result in a larger file size. It is recommended to use the default value.
2074 * @note Saving can be asynchronous if the assigned thread number is greater than zero. To guarantee the saving is done, call sync() afterwards.
2075 *
2076 * @see Saver::sync()
2077 *
2078 * @note Experimental API
2079 */
2080 Result save(std::unique_ptr<Animation> animation, const std::string& path, uint32_t quality = 100, uint32_t fps = 0) noexcept;
2081
2082 /**
2083 * @brief Guarantees that the saving task is finished.
2084 *
2085 * The behavior of the Saver works on a sync/async basis, depending on the threading setting of the Initializer.
2086 * Thus, if you wish to have a benefit of it, you must call sync() after the save() in the proper delayed time.
2087 * Otherwise, you can call sync() immediately.
2088 *
2089 * @note The asynchronous tasking is dependent on the Saver module implementation.
2090 * @see Saver::save()
2091 *
2092 * @since 0.5
2093 */
2094 Result sync() noexcept;
2095
2096 /**
2097 * @brief Creates a new Saver object.
2098 *
2099 * @return A new Saver object.
2100 *
2101 * @since 0.5
2102 */
2103 static std::unique_ptr<Saver> gen() noexcept;
2104
2105 _TVG_DECLARE_PRIVATE(Saver);
2106 };
2107
2108
2109 /**
2110 * @class Accessor
2111 *
2112 * @brief The Accessor is a utility class to debug the Scene structure by traversing the scene-tree.
2113 *
2114 * The Accessor helps you search specific nodes to read the property information, figure out the structure of the scene tree and its size.
2115 *
2116 * @warning We strongly warn you not to change the paints of a scene unless you really know the design-structure.
2117 *
2118 * @since 0.10
2119 */
2120 class TVG_API Accessor final
2121 {
2122 public:
2123 ~Accessor();
2124
2125 TVG_DEPRECATED std::unique_ptr<Picture> set(std::unique_ptr<Picture> picture, std::function<bool(const Paint* paint)> func) noexcept;
2126
2127 /**
2128 * @brief Set the access function for traversing the Picture scene tree nodes.
2129 *
2130 * @param[in] paint The paint node to traverse the internal scene-tree.
2131 * @param[in] func The callback function calling for every paint nodes of the Picture.
2132 * @param[in] data Data passed to the @p func as its argument.
2133 *
2134 * @note The bitmap based picture might not have the scene-tree.
2135 *
2136 * @note Experimental API
2137 */
2138 Result set(Paint* paint, std::function<bool(const Paint* paint, void* data)> func, void* data) noexcept;
2139
2140 /**
2141 * @brief Generate a unique ID (hash key) from a given name.
2142 *
2143 * This function computes a unique identifier value based on the provided string.
2144 * You can use this to assign a unique ID to the Paint object.
2145 *
2146 * @param[in] name The input string to generate the unique identifier from.
2147 *
2148 * @return The generated unique identifier value.
2149 *
2150 * @see Paint::id
2151 *
2152 * @note Experimental API
2153 */
2154 static uint32_t id(const char* name) noexcept;
2155
2156 /**
2157 * @brief Creates a new Accessor object.
2158 *
2159 * @return A new Accessor object.
2160 */
2161 static std::unique_ptr<Accessor> gen() noexcept;
2162
2163 _TVG_DECLARE_PRIVATE(Accessor);
2164 };
2165
2166
2167 /**
2168 * @brief The cast() function is a utility function used to cast a 'Paint' to type 'T'.
2169 * @since 0.11
2170 */
2171 template<typename T = tvg::Paint>
2172 std::unique_ptr<T> cast(Paint* paint)
2173 {
2174 return std::unique_ptr<T>(static_cast<T*>(paint));
2175 }
2176
2177
2178 /**
2179 * @brief The cast() function is a utility function used to cast a 'Fill' to type 'T'.
2180 * @since 0.11
2181 */
2182 template<typename T = tvg::Fill>
2183 std::unique_ptr<T> cast(Fill* fill)
2184 {
2185 return std::unique_ptr<T>(static_cast<T*>(fill));
2186 }
2187
2188
2189 /** @}*/
2190
2191 } //namespace
2192
2193 #endif //_THORVG_H_