ThorVG  v0.9
thorvg.h
1 
15 #ifndef _THORVG_H_
16 #define _THORVG_H_
17 
18 #include <functional>
19 #include <memory>
20 #include <string>
21 
22 #ifdef TVG_API
23  #undef TVG_API
24 #endif
25 
26 #if defined(_WIN32) && !defined(__clang__)
27  #if TVG_BUILD
28  #if TVG_EXPORT
29  #define TVG_API __declspec(dllexport)
30  #else
31  #define TVG_API
32  #endif
33  #else
34  #define TVG_API __declspec(dllimport)
35  #endif
36  #define TVG_DEPRECATED __declspec(deprecated)
37 #else
38  #if TVG_BUILD
39  #if TVG_EXPORT
40  #define TVG_API __attribute__ ((visibility ("default")))
41  #else
42  #define TVG_API
43  #endif
44  #else
45  #define TVG_API
46  #endif
47  #define TVG_DEPRECATED __attribute__ ((__deprecated__))
48 #endif
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 #define _TVG_DECLARE_PRIVATE(A) \
55 protected: \
56  struct Impl; \
57  Impl* pImpl; \
58  A(const A&) = delete; \
59  const A& operator=(const A&) = delete; \
60  A()
61 
62 #define _TVG_DISABLE_CTOR(A) \
63  A() = delete; \
64  ~A() = delete
65 
66 #define _TVG_DECLARE_ACCESSOR() \
67  friend Canvas; \
68  friend Scene; \
69  friend Picture; \
70  friend Accessor; \
71  friend IteratorAccessor
72 
73 
74 namespace tvg
75 {
76 
77 class RenderMethod;
78 class IteratorAccessor;
79 class Scene;
80 class Picture;
81 class Canvas;
82 class Accessor;
83 
94 enum class Result
95 {
96  Success = 0,
101  NonSupport,
102  Unknown
103 };
104 
111 enum class PathCommand
112 {
113  Close = 0,
114  MoveTo,
115  LineTo,
116  CubicTo
117 };
118 
122 enum class StrokeCap
123 {
124  Square = 0,
125  Round,
126  Butt
127 };
128 
132 enum class StrokeJoin
133 {
134  Bevel = 0,
135  Round,
136  Miter
137 };
138 
142 enum class FillSpread
143 {
144  Pad = 0,
145  Reflect,
146  Repeat
147 };
148 
152 enum class FillRule
153 {
154  Winding = 0,
155  EvenOdd
156 };
157 
161 enum class CompositeMethod
162 {
163  None = 0,
164  ClipPath,
165  AlphaMask,
166  InvAlphaMask,
167  LumaMask
168 };
169 
173 enum class CanvasEngine
174 {
175  Sw = (1 << 1),
176  Gl = (1 << 2)
177 };
178 
179 
183 struct Point
184 {
185  float x, y;
186 };
187 
188 
196 struct Matrix
197 {
198  float e11, e12, e13;
199  float e21, e22, e23;
200  float e31, e32, e33;
201 };
202 
211 struct Vertex
212 {
213  Point pt;
214  Point uv;
215 };
216 
217 
225 struct Polygon
226 {
227  Vertex vertex[3];
228 };
229 
230 
240 class TVG_API Paint
241 {
242 public:
243  virtual ~Paint();
244 
255  Result rotate(float degree) noexcept;
256 
264  Result scale(float factor) noexcept;
265 
277  Result translate(float x, float y) noexcept;
278 
288  Result transform(const Matrix& m) noexcept;
289 
300  Matrix transform() noexcept;
301 
312  Result opacity(uint8_t o) noexcept;
313 
322  Result composite(std::unique_ptr<Paint> target, CompositeMethod method) noexcept;
323 
337  TVG_DEPRECATED Result bounds(float* x, float* y, float* w, float* h) const noexcept;
338 
354  Result bounds(float* x, float* y, float* w, float* h, bool transformed) const noexcept;
355 
363  Paint* duplicate() const noexcept;
364 
370  uint8_t opacity() const noexcept;
371 
381  CompositeMethod composite(const Paint** target) const noexcept;
382 
390  uint32_t identifier() const noexcept;
391 
392  _TVG_DECLARE_ACCESSOR();
393  _TVG_DECLARE_PRIVATE(Paint);
394 };
395 
396 
408 class TVG_API Fill
409 {
410 public:
414  struct ColorStop
415  {
416  float offset;
417  uint8_t r;
418  uint8_t g;
419  uint8_t b;
420  uint8_t a;
421  };
422 
423  virtual ~Fill();
424 
433  Result colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept;
434 
442  Result spread(FillSpread s) noexcept;
443 
453  Result transform(const Matrix& m) noexcept;
454 
462  uint32_t colorStops(const ColorStop** colorStops) const noexcept;
463 
469  FillSpread spread() const noexcept;
470 
478  Matrix transform() const noexcept;
479 
487  Fill* duplicate() const noexcept;
488 
496  uint32_t identifier() const noexcept;
497 
498  _TVG_DECLARE_PRIVATE(Fill);
499 };
500 
501 
512 class TVG_API Canvas
513 {
514 public:
515  Canvas(RenderMethod*);
516  virtual ~Canvas();
517 
528  Result reserve(uint32_t n) noexcept;
529 
547  virtual Result push(std::unique_ptr<Paint> paint) noexcept;
548 
559  virtual Result clear(bool free = true) noexcept;
560 
573  virtual Result update(Paint* paint = nullptr) noexcept;
574 
583  virtual Result draw() noexcept;
584 
594  virtual Result sync() noexcept;
595 
596  _TVG_DECLARE_PRIVATE(Canvas);
597 };
598 
599 
608 class TVG_API LinearGradient final : public Fill
609 {
610 public:
611  ~LinearGradient();
612 
629  Result linear(float x1, float y1, float x2, float y2) noexcept;
630 
645  Result linear(float* x1, float* y1, float* x2, float* y2) const noexcept;
646 
652  static std::unique_ptr<LinearGradient> gen() noexcept;
653 
661  static uint32_t identifier() noexcept;
662 
663  _TVG_DECLARE_PRIVATE(LinearGradient);
664 };
665 
666 
673 class TVG_API RadialGradient final : public Fill
674 {
675 public:
676  ~RadialGradient();
677 
689  Result radial(float cx, float cy, float radius) noexcept;
690 
702  Result radial(float* cx, float* cy, float* radius) const noexcept;
703 
709  static std::unique_ptr<RadialGradient> gen() noexcept;
710 
718  static uint32_t identifier() noexcept;
719 
720  _TVG_DECLARE_PRIVATE(RadialGradient);
721 };
722 
723 
736 class TVG_API Shape final : public Paint
737 {
738 public:
739  ~Shape();
740 
750  Result reset() noexcept;
751 
762  Result moveTo(float x, float y) noexcept;
763 
776  Result lineTo(float x, float y) noexcept;
777 
795  Result cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept;
796 
806  Result close() noexcept;
807 
832  Result appendRect(float x, float y, float w, float h, float rx, float ry) noexcept;
833 
850  Result appendCircle(float cx, float cy, float rx, float ry) noexcept;
851 
869  Result appendArc(float cx, float cy, float radius, float startAngle, float sweep, bool pie) noexcept;
870 
887  Result appendPath(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept;
888 
896  Result stroke(float width) noexcept;
897 
908  Result stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
909 
919  Result stroke(std::unique_ptr<Fill> f) noexcept;
920 
934  Result stroke(const float* dashPattern, uint32_t cnt) noexcept;
935 
943  Result stroke(StrokeCap cap) noexcept;
944 
954  Result stroke(StrokeJoin join) noexcept;
955 
971  Result fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
972 
984  Result fill(std::unique_ptr<Fill> f) noexcept;
985 
993  Result fill(FillRule r) noexcept;
994 
995 
1004  Result order(bool strokeFirst) noexcept;
1005 
1006 
1014  uint32_t pathCommands(const PathCommand** cmds) const noexcept;
1015 
1023  uint32_t pathCoords(const Point** pts) const noexcept;
1024 
1030  const Fill* fill() const noexcept;
1031 
1042  Result fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
1043 
1049  FillRule fillRule() const noexcept;
1050 
1056  float strokeWidth() const noexcept;
1057 
1068  Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
1069 
1075  const Fill* strokeFill() const noexcept;
1076 
1084  uint32_t strokeDash(const float** dashPattern) const noexcept;
1085 
1091  StrokeCap strokeCap() const noexcept;
1092 
1098  StrokeJoin strokeJoin() const noexcept;
1099 
1105  static std::unique_ptr<Shape> gen() noexcept;
1106 
1114  static uint32_t identifier() noexcept;
1115 
1116  _TVG_DECLARE_PRIVATE(Shape);
1117 };
1118 
1119 
1128 class TVG_API Picture final : public Paint
1129 {
1130 public:
1131  ~Picture();
1132 
1146  Result load(const std::string& path) noexcept;
1147 
1164  TVG_DEPRECATED Result load(const char* data, uint32_t size, bool copy = false) noexcept;
1165 
1183  Result load(const char* data, uint32_t size, const std::string& mimeType, bool copy = false) noexcept;
1184 
1196  Result size(float w, float h) noexcept;
1197 
1206  Result size(float* w, float* h) const noexcept;
1207 
1217  const uint32_t* data(uint32_t* w, uint32_t* h) const noexcept;
1218 
1227  Result load(uint32_t* data, uint32_t w, uint32_t h, bool copy) noexcept;
1228 
1251  Result mesh(const Polygon* triangles, uint32_t triangleCnt) noexcept;
1252 
1265  uint32_t mesh(const Polygon** triangles) const noexcept;
1266 
1274  Result viewbox(float* x, float* y, float* w, float* h) const noexcept;
1275 
1281  static std::unique_ptr<Picture> gen() noexcept;
1282 
1290  static uint32_t identifier() noexcept;
1291 
1292  _TVG_DECLARE_PRIVATE(Picture);
1293 };
1294 
1295 
1307 class TVG_API Scene final : public Paint
1308 {
1309 public:
1310  ~Scene();
1311 
1326  Result push(std::unique_ptr<Paint> paint) noexcept;
1327 
1338  Result reserve(uint32_t size) noexcept;
1339 
1352  Result clear(bool free = true) noexcept;
1353 
1359  static std::unique_ptr<Scene> gen() noexcept;
1360 
1368  static uint32_t identifier() noexcept;
1369 
1370  _TVG_DECLARE_PRIVATE(Scene);
1371 };
1372 
1373 
1379 class TVG_API SwCanvas final : public Canvas
1380 {
1381 public:
1382  ~SwCanvas();
1383 
1388  {
1389  ABGR8888 = 0,
1393  };
1394 
1400  {
1401  Default = 0,
1403  Individual
1404  };
1405 
1424  Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, Colorspace cs) noexcept;
1425 
1449  Result mempool(MempoolPolicy policy) noexcept;
1450 
1455  static std::unique_ptr<SwCanvas> gen() noexcept;
1456 
1457  _TVG_DECLARE_PRIVATE(SwCanvas);
1458 };
1459 
1460 
1470 class TVG_API GlCanvas final : public Canvas
1471 {
1472 public:
1473  ~GlCanvas();
1474 
1482  Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h) noexcept;
1483 
1491  static std::unique_ptr<GlCanvas> gen() noexcept;
1492 
1493  _TVG_DECLARE_PRIVATE(GlCanvas);
1494 };
1495 
1496 
1502 class TVG_API Initializer final
1503 {
1504 public:
1525  static Result init(CanvasEngine engine, uint32_t threads) noexcept;
1526 
1541  static Result term(CanvasEngine engine) noexcept;
1542 
1543  _TVG_DISABLE_CTOR(Initializer);
1544 };
1545 
1546 
1564 class TVG_API Saver final
1565 {
1566 public:
1567  ~Saver();
1568 
1591  Result save(std::unique_ptr<Paint> paint, const std::string& path, bool compress = true) noexcept;
1592 
1608  Result sync() noexcept;
1609 
1617  static std::unique_ptr<Saver> gen() noexcept;
1618 
1619  _TVG_DECLARE_PRIVATE(Saver);
1620 };
1621 
1622 
1634 class TVG_API Accessor final
1635 {
1636 public:
1637  ~Accessor();
1638 
1651  std::unique_ptr<Picture> set(std::unique_ptr<Picture> picture, std::function<bool(const Paint* paint)> func) noexcept;
1652 
1660  static std::unique_ptr<Accessor> gen() noexcept;
1661 
1662  _TVG_DECLARE_PRIVATE(Accessor);
1663 };
1664 
1667 } //namespace
1668 
1669 #ifdef __cplusplus
1670 }
1671 #endif
1672 
1673 #endif //_THORVG_H_
The Accessor is a utility class to debug the Scene structure by traversing the scene-tree.
Definition: thorvg.h:1635
std::unique_ptr< Picture > set(std::unique_ptr< Picture > picture, std::function< bool(const Paint *paint)> func) noexcept
Set the access function for traversing the Picture scene tree nodes.
static std::unique_ptr< Accessor > gen() noexcept
Creates a new Accessor object.
An abstract class for drawing graphical elements.
Definition: thorvg.h:513
Result reserve(uint32_t n) noexcept
Sets the size of the container, where all the paints pushed into the Canvas are stored.
virtual Result push(std::unique_ptr< Paint > paint) noexcept
Passes drawing elements to the Canvas using Paint objects.
virtual Result clear(bool free=true) noexcept
Sets the total number of the paints pushed into the canvas to be zero. Depending on the value of the ...
An abstract class representing the gradient fill of the Shape object.
Definition: thorvg.h:409
FillSpread spread() const noexcept
Gets the FillSpread value of the fill.
Result colorStops(const ColorStop *colorStops, uint32_t cnt) noexcept
Sets the parameters of the colors of the gradient and their position.
Result transform(const Matrix &m) noexcept
Sets the matrix of the affine transformation for the gradient fill.
uint32_t colorStops(const ColorStop **colorStops) const noexcept
Gets the parameters of the colors of the gradient, their position and number.
Result spread(FillSpread s) noexcept
Sets the FillSpread value, which specifies how to fill the area outside the gradient bounds.
A class for the rendering graphic elements with a GL raster engine.
Definition: thorvg.h:1471
Result target(uint32_t *buffer, uint32_t stride, uint32_t w, uint32_t h) noexcept
Sets the target buffer for the rasterization.
static std::unique_ptr< GlCanvas > gen() noexcept
Creates a new GlCanvas object.
A class that enables initialization and termination of the TVG engines.
Definition: thorvg.h:1503
static Result term(CanvasEngine engine) noexcept
Terminates TVG engines.
static Result init(CanvasEngine engine, uint32_t threads) noexcept
Initializes TVG engines.
A class representing the linear gradient fill of the Shape object.
Definition: thorvg.h:609
static std::unique_ptr< LinearGradient > gen() noexcept
Creates a new LinearGradient object.
Result linear(float *x1, float *y1, float *x2, float *y2) const noexcept
Gets the linear gradient bounds.
Result linear(float x1, float y1, float x2, float y2) noexcept
Sets the linear gradient bounds.
An abstract class for managing graphical elements.
Definition: thorvg.h:241
Result scale(float factor) noexcept
Sets the scale value of the object.
Result rotate(float degree) noexcept
Sets the angle by which the object is rotated.
Result transform(const Matrix &m) noexcept
Sets the matrix of the affine transformation for the object.
Matrix transform() noexcept
Gets the matrix of the affine transformation of the object.
Result translate(float x, float y) noexcept
Sets the values by which the object is moved in a two-dimensional space.
A class representing an image read in one of the supported formats: raw, svg, png,...
Definition: thorvg.h:1129
TVG_DEPRECATED Result load(const char *data, uint32_t size, bool copy=false) noexcept
Loads a picture data from a memory block of a given size.
Result load(const std::string &path) noexcept
Loads a picture data directly from a file.
A class representing the radial gradient fill of the Shape object.
Definition: thorvg.h:674
Result radial(float cx, float cy, float radius) noexcept
Sets the radial gradient bounds.
Result radial(float *cx, float *cy, float *radius) const noexcept
Gets the radial gradient bounds.
static std::unique_ptr< RadialGradient > gen() noexcept
Creates a new RadialGradient object.
A class for exporting a paint object into a specified file, from which to recover the paint data late...
Definition: thorvg.h:1565
Result save(std::unique_ptr< Paint > paint, const std::string &path, bool compress=true) noexcept
Exports the given paint data to the given path.
A class to composite children paints.
Definition: thorvg.h:1308
Result push(std::unique_ptr< Paint > paint) noexcept
Passes drawing elements to the Scene using Paint objects.
Result reserve(uint32_t size) noexcept
Sets the size of the container, where all the paints pushed into the Scene are stored.
Result clear(bool free=true) noexcept
Sets the total number of the paints pushed into the scene to be zero. Depending on the value of the f...
A class representing two-dimensional figures and their properties.
Definition: thorvg.h:737
Result reset() noexcept
Resets the properties of the shape path.
A class for the rendering graphical elements with a software raster engine.
Definition: thorvg.h:1380
Result target(uint32_t *buffer, uint32_t stride, uint32_t w, uint32_t h, Colorspace cs) noexcept
Sets the target buffer for the rasterization.
Result mempool(MempoolPolicy policy) noexcept
Set sw engine memory pool behavior policy.
Colorspace
Enumeration specifying the methods of combining the 8-bit color channels into 32-bit color.
Definition: thorvg.h:1388
@ ARGB8888_STRAIGHT
@BETA_API The channels are joined in the order: alpha, red, green, blue. Colors are un-alpha-premulti...
Definition: thorvg.h:1392
@ ABGR8888_STRAIGHT
@BETA_API The channels are joined in the order: alpha, blue, green, red. Colors are un-alpha-premulti...
Definition: thorvg.h:1391
@ ARGB8888
The channels are joined in the order: alpha, red, green, blue. Colors are alpha-premultiplied.
Definition: thorvg.h:1390
MempoolPolicy
Enumeration specifying the methods of Memory Pool behavior policy.
Definition: thorvg.h:1400
@ Shareable
Memory Pool is shared among the SwCanvases.
Definition: thorvg.h:1402
static std::unique_ptr< SwCanvas > gen() noexcept
Creates a new SwCanvas object.
FillSpread
Enumeration specifying how to fill the area outside the gradient bounds.
Definition: thorvg.h:143
Result
Enumeration specifying the result from the APIs.
Definition: thorvg.h:95
CanvasEngine
Enumeration specifying the engine type used for the graphics backend. For multiple backends bitwise o...
Definition: thorvg.h:174
StrokeCap
Enumeration determining the ending type of a stroke in the open sub-paths.
Definition: thorvg.h:123
PathCommand
Enumeration specifying the values of the path commands accepted by TVG.
Definition: thorvg.h:112
FillRule
Enumeration specifying the algorithm used to establish which parts of the shape are treated as the in...
Definition: thorvg.h:153
CompositeMethod
Enumeration indicating the method used in the composition of two objects - the target and the source.
Definition: thorvg.h:162
StrokeJoin
Enumeration determining the style used at the corners of joined stroked path segments.
Definition: thorvg.h:133
@ Repeat
The gradient pattern is repeated continuously beyond the gradient area until the expected region is f...
@ Reflect
The gradient pattern is reflected outside the gradient area until the expected region is filled.
@ Pad
The remaining area is filled with the closest stop color.
@ InsufficientCondition
The value returned in case the request cannot be processed - e.g. asking for properties of an object,...
@ Success
The value returned in case of a correct request execution.
@ Unknown
The value returned in all other cases.
@ NonSupport
The value returned in case of choosing unsupported options.
@ FailedAllocation
The value returned in case of unsuccessful memory allocation.
@ InvalidArguments
The value returned in the event of a problem with the arguments given to the API - e....
@ MemoryCorruption
The value returned in the event of bad memory handling - e.g. failing in pointer releasing or casting...
@ Gl
OpenGL rasterizer.
@ Sw
CPU rasterizer.
@ Butt
The stroke ends exactly at each of the two end-points of a sub-path. For zero length sub-paths no str...
@ Round
The stroke is extended in both end-points of a sub-path by a half circle, with a radius equal to the ...
@ Square
The stroke is extended in both end-points of a sub-path by a rectangle, with the width equal to the s...
@ LineTo
Draws a line from the current point to the given point and sets a new value of the current point....
@ CubicTo
Draws a cubic Bezier curve from the current point to the given point using two given control points a...
@ Close
Ends the current sub-path and connects it with its initial point. This command doesn't expect any poi...
@ MoveTo
Sets a new initial point of the sub-path and a new current point. This command expects 1 point: the s...
@ Winding
A line from the point to a location outside the shape is drawn. The intersections of the line with th...
@ EvenOdd
A line from the point to a location outside the shape is drawn and its intersections with the path se...
@ LumaMask
The source pixels are converted to the grayscale (luma value) and alpha blended with the target....
@ InvAlphaMask
The pixels of the source and the complement to the target's pixels are alpha blended....
@ ClipPath
The intersection of the source and the target is determined and only the resulting pixels from the so...
@ None
No composition is applied.
@ AlphaMask
The pixels of the source and the target are alpha blended. As a result, only the part of the source,...
@ Bevel
The outer corner of the joined path segments is bevelled at the join point. The triangular region of ...
@ Round
The outer corner of the joined path segments is rounded. The circular region is centered at the join ...
@ Miter
The outer corner of the joined path segments is spiked. The spike is created by extension beyond the ...
A data structure storing the information about the color and its relative position inside the gradien...
Definition: thorvg.h:415
uint8_t g
Definition: thorvg.h:418
float offset
Definition: thorvg.h:416
uint8_t b
Definition: thorvg.h:419
uint8_t r
Definition: thorvg.h:417
uint8_t a
Definition: thorvg.h:420
A data structure representing a three-dimensional matrix.
Definition: thorvg.h:197
A data structure representing a point in two-dimensional space.
Definition: thorvg.h:184
A data structure representing a triange in a texture mesh.
Definition: thorvg.h:226
A data structure representing a texture mesh vertex.
Definition: thorvg.h:212