ThorVG  v0.1
ThorVG is a platform-independent portable library for drawing vector-based scene and animation. It's an open-source software that is freely used by a variety of software platforms and applications. ThorVG provides neat and easy APIs, its library has no dependencies and keeps cheap and super compact size. It serves as the vector graphics engine for Tizen OS that powers many products.
thorvg.h
1 
14 #ifndef _THORVG_H_
15 #define _THORVG_H_
16 
17 #include <memory>
18 
19 #ifdef TVG_BUILD
20  #define TVG_EXPORT __attribute__ ((visibility ("default")))
21 #else
22  #define TVG_EXPORT
23 #endif
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 
30 #define _TVG_DECLARE_PRIVATE(A) \
31 protected: \
32  struct Impl; \
33  Impl* pImpl; \
34  A(const A&) = delete; \
35  const A& operator=(const A&) = delete; \
36  A()
37 
38 #define _TVG_DISABLE_CTOR(A) \
39  A() = delete; \
40  ~A() = delete
41 
42 #define _TVG_DECLARE_ACCESSOR() \
43  friend Canvas; \
44  friend Scene; \
45  friend Picture
46 
47 #define _TVG_DECALRE_IDENTIFIER() \
48  auto id() const { return _id; } \
49 protected: \
50  unsigned _id
51 
52 namespace tvg
53 {
54 
55 class RenderMethod;
56 class Scene;
57 class Picture;
58 class Canvas;
59 
70 enum class TVG_EXPORT Result
71 {
72  Success = 0,
77  NonSupport,
78  Unknown
79 };
80 
87 enum class TVG_EXPORT PathCommand
88 {
89  Close = 0,
90  MoveTo,
91  LineTo,
92  CubicTo
93 };
94 
98 enum class TVG_EXPORT StrokeCap
99 {
100  Square = 0,
101  Round,
102  Butt
103 };
104 
108 enum class TVG_EXPORT StrokeJoin
109 {
110  Bevel = 0,
111  Round,
112  Miter
113 };
114 
118 enum class TVG_EXPORT FillSpread
119 {
120  Pad = 0,
121  Reflect,
122  Repeat
123 };
124 
128 enum class TVG_EXPORT FillRule
129 {
130  Winding = 0,
131  EvenOdd
132 };
133 
137 enum class TVG_EXPORT CompositeMethod
138 {
139  None = 0,
140  ClipPath,
141  AlphaMask,
142  InvAlphaMask
143 };
144 
148 enum class TVG_EXPORT CanvasEngine
149 {
150  Sw = (1 << 1),
151  Gl = (1 << 2)
152 };
153 
154 
158 struct Point
159 {
160  float x, y;
161 };
162 
163 
171 struct Matrix
172 {
173  float e11, e12, e13;
174  float e21, e22, e23;
175  float e31, e32, e33;
176 };
177 
178 
188 class TVG_EXPORT Paint
189 {
190 public:
191  virtual ~Paint();
192 
203  Result rotate(float degree) noexcept;
204 
212  Result scale(float factor) noexcept;
213 
225  Result translate(float x, float y) noexcept;
226 
237  Result transform(const Matrix& m) noexcept;
238 
248  Result opacity(uint8_t o) noexcept;
249 
259  Result composite(std::unique_ptr<Paint> target, CompositeMethod method) const noexcept;
260 
273  Result bounds(float* x, float* y, float* w, float* h) const noexcept;
274 
282  Paint* duplicate() const noexcept;
283 
289  uint8_t opacity() const noexcept;
290 
291  _TVG_DECLARE_ACCESSOR();
292  _TVG_DECLARE_PRIVATE(Paint);
293 };
294 
295 
307 class TVG_EXPORT Fill
308 {
309 public:
313  struct ColorStop
314  {
315  float offset;
316  uint8_t r;
317  uint8_t g;
318  uint8_t b;
319  uint8_t a;
320  };
321 
322  virtual ~Fill();
323 
332  Result colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept;
333 
341  Result spread(FillSpread s) noexcept;
342 
350  uint32_t colorStops(const ColorStop** colorStops) const noexcept;
351 
357  FillSpread spread() const noexcept;
358 
366  Fill* duplicate() const noexcept;
367 
368  _TVG_DECALRE_IDENTIFIER();
369  _TVG_DECLARE_PRIVATE(Fill);
370 };
371 
372 
383 class TVG_EXPORT Canvas
384 {
385 public:
386  Canvas(RenderMethod*);
387  virtual ~Canvas();
388 
399  Result reserve(uint32_t n) noexcept;
400 
418  virtual Result push(std::unique_ptr<Paint> paint) noexcept;
419 
430  virtual Result clear(bool free = true) noexcept;
431 
444  virtual Result update(Paint* paint) noexcept;
445 
454  virtual Result draw() noexcept;
455 
465  virtual Result sync() noexcept;
466 
467  _TVG_DECLARE_PRIVATE(Canvas);
468 };
469 
470 
479 class TVG_EXPORT LinearGradient final : public Fill
480 {
481 public:
482  ~LinearGradient();
483 
498  Result linear(float x1, float y1, float x2, float y2) noexcept;
499 
514  Result linear(float* x1, float* y1, float* x2, float* y2) const noexcept;
515 
521  static std::unique_ptr<LinearGradient> gen() noexcept;
522 
523  _TVG_DECLARE_PRIVATE(LinearGradient);
524 };
525 
526 
533 class TVG_EXPORT RadialGradient final : public Fill
534 {
535 public:
536  ~RadialGradient();
537 
549  Result radial(float cx, float cy, float radius) noexcept;
550 
562  Result radial(float* cx, float* cy, float* radius) const noexcept;
563 
569  static std::unique_ptr<RadialGradient> gen() noexcept;
570 
571  _TVG_DECLARE_PRIVATE(RadialGradient);
572 };
573 
574 
587 class TVG_EXPORT Shape final : public Paint
588 {
589 public:
590  ~Shape();
591 
601  Result reset() noexcept;
602 
613  Result moveTo(float x, float y) noexcept;
614 
627  Result lineTo(float x, float y) noexcept;
628 
646  Result cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept;
647 
657  Result close() noexcept;
658 
683  Result appendRect(float x, float y, float w, float h, float rx, float ry) noexcept;
684 
701  Result appendCircle(float cx, float cy, float rx, float ry) noexcept;
702 
720  Result appendArc(float cx, float cy, float radius, float startAngle, float sweep, bool pie) noexcept;
721 
737  Result appendPath(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept;
738 
746  Result stroke(float width) noexcept;
747 
758  Result stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
759 
769  Result stroke(std::unique_ptr<Fill> f) noexcept;
770 
784  Result stroke(const float* dashPattern, uint32_t cnt) noexcept;
785 
793  Result stroke(StrokeCap cap) noexcept;
794 
804  Result stroke(StrokeJoin join) noexcept;
805 
820  Result fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
821 
833  Result fill(std::unique_ptr<Fill> f) noexcept;
834 
842  Result fill(FillRule r) noexcept;
843 
851  uint32_t pathCommands(const PathCommand** cmds) const noexcept;
852 
860  uint32_t pathCoords(const Point** pts) const noexcept;
861 
867  const Fill* fill() const noexcept;
868 
879  Result fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
880 
886  FillRule fillRule() const noexcept;
887 
893  float strokeWidth() const noexcept;
894 
905  Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
906 
912  const Fill* strokeFill() const noexcept;
913 
921  uint32_t strokeDash(const float** dashPattern) const noexcept;
922 
928  StrokeCap strokeCap() const noexcept;
929 
935  StrokeJoin strokeJoin() const noexcept;
936 
942  static std::unique_ptr<Shape> gen() noexcept;
943 
944  _TVG_DECLARE_PRIVATE(Shape);
945 };
946 
947 
956 class TVG_EXPORT Picture final : public Paint
957 {
958 public:
959  ~Picture();
960 
974  Result load(const std::string& path) noexcept;
975 
989  Result load(const char* data, uint32_t size) noexcept;
990 
1002  Result size(float w, float h) noexcept;
1003 
1012  Result size(float* w, float* h) const noexcept;
1013 
1021  const uint32_t* data() const noexcept;
1022 
1030  Result load(uint32_t* data, uint32_t w, uint32_t h, bool copy) noexcept;
1031 
1039  Result viewbox(float* x, float* y, float* w, float* h) const noexcept;
1040 
1046  static std::unique_ptr<Picture> gen() noexcept;
1047 
1048  _TVG_DECLARE_PRIVATE(Picture);
1049 };
1050 
1051 
1063 class TVG_EXPORT Scene final : public Paint
1064 {
1065 public:
1066  ~Scene();
1067 
1082  Result push(std::unique_ptr<Paint> paint) noexcept;
1083 
1094  Result reserve(uint32_t size) noexcept;
1095 
1103  Result clear() noexcept;
1104 
1110  static std::unique_ptr<Scene> gen() noexcept;
1111 
1112  _TVG_DECLARE_PRIVATE(Scene);
1113 };
1114 
1115 
1121 class TVG_EXPORT SwCanvas final : public Canvas
1122 {
1123 public:
1124  ~SwCanvas();
1125 
1130  {
1131  ABGR8888 = 0,
1132  ARGB8888
1133  };
1134 
1141  {
1142  Default = 0,
1144  Individual
1145  };
1146 
1165  Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, Colorspace cs) noexcept;
1166 
1190  Result mempool(MempoolPolicy policy) noexcept;
1191 
1196  static std::unique_ptr<SwCanvas> gen() noexcept;
1197 
1198  _TVG_DECLARE_PRIVATE(SwCanvas);
1199 };
1200 
1201 
1211 class TVG_EXPORT GlCanvas final : public Canvas
1212 {
1213 public:
1214  ~GlCanvas();
1215 
1223  Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h) noexcept;
1224 
1232  static std::unique_ptr<GlCanvas> gen() noexcept;
1233 
1234  _TVG_DECLARE_PRIVATE(GlCanvas);
1235 };
1236 
1237 
1243 class TVG_EXPORT Initializer final
1244 {
1245 public:
1266  static Result init(CanvasEngine engine, uint32_t threads) noexcept;
1267 
1282  static Result term(CanvasEngine engine) noexcept;
1283 
1284  _TVG_DISABLE_CTOR(Initializer);
1285 };
1286 
1289 } //namespace
1290 
1291 #ifdef __cplusplus
1292 }
1293 #endif
1294 
1295 #endif //_THORVG_H_
The pixels of the source and the target are alpha blended. As a result, only the part of the source...
Draws a line from the current point to the given point and sets a new value of the current point...
The stroke is extended in both end-points of a sub-path by a half circle, with a radius equal to the ...
The value returned in all other cases.
The value returned in case of unsuccessful memory allocation.
MempoolPolicy
Enumeration specifying the methods of Memory Pool behavior policy.
Definition: thorvg.h:1140
Ends the current sub-path and connects it with its initial point. This command doesn&#39;t expect any poi...
An abstract class representing the gradient fill of the Shape object.
Definition: thorvg.h:307
uint8_t r
Definition: thorvg.h:316
Sets a new initial point of the sub-path and a new current point. This command expects 1 point: the s...
The value returned in case the request cannot be processed - e.g. asking for properties of an object...
StrokeCap
Enumeration determining the ending type of a stroke in the open sub-paths.
Definition: thorvg.h:98
The intersection of the source and the target is determined and only the resulting pixels from the so...
A class representing the radial gradient fill of the Shape object.
Definition: thorvg.h:533
FillRule
Enumeration specifying the algorithm used to establish which parts of the shape are treated as the in...
Definition: thorvg.h:128
A class to composite children paints.
Definition: thorvg.h:1063
The value returned in the event of bad memory handling - e.g. failing in pointer releasing or casting...
The gradient pattern is reflected outside the gradient area until the expected region is filled...
FillSpread
Enumeration specifying how to fill the area outside the gradient bounds.
Definition: thorvg.h:118
uint8_t a
Definition: thorvg.h:319
A data structure representing a point in two-dimensional space.
Definition: thorvg.h:158
A class representing two-dimensional figures and their properties.
Definition: thorvg.h:587
Memory Pool is shared among the SwCanvases.
Definition: thorvg.h:1143
No composition is applied.
A line from the point to a location outside the shape is drawn. The intersections of the line with th...
The pixels of the source and the complement to the target&#39;s pixels are alpha blended. As a result, only the part of the source which is not covered by the target is visible.
StrokeJoin
Enumeration determining the style used at the corners of joined stroked path segments.
Definition: thorvg.h:108
OpenGL rasterizer.
Definition: thorvg.h:52
A class representing an image read in one of the supported formats: raw, svg, png and etc...
Definition: thorvg.h:956
The remaining area is filled with the closest stop color.
CPU rasterizer.
PathCommand
Enumeration specifying the values of the path commands accepted by TVG.
Definition: thorvg.h:87
The value returned in the event of a problem with the arguments given to the API - e...
The value returned in case of a correct request execution.
A class for the rendering graphical elements with a software raster engine.
Definition: thorvg.h:1121
Draws a cubic Bezier curve from the current point to the given point using two given control points a...
The stroke ends exactly at each of the two end-points of a sub-path. For zero length sub-paths no str...
The gradient pattern is repeated continuously beyond the gradient area until the expected region is f...
Colorspace
Enumeration specifying the methods of combining the 8-bit color channels into 32-bit color...
Definition: thorvg.h:1129
A class for the rendering graphic elements with a GL raster engine.
Definition: thorvg.h:1211
The outer corner of the joined path segments is bevelled at the join point. The triangular region of ...
The stroke is extended in both end-points of a sub-path by a rectangle, with the width equal to the s...
A data structure storing the information about the color and its relative position inside the gradien...
Definition: thorvg.h:313
uint8_t b
Definition: thorvg.h:318
Result
Enumeration specifying the result from the APIs.
Definition: thorvg.h:70
CompositeMethod
Enumeration indicating the method used in the composition of two objects - the target and the source...
Definition: thorvg.h:137
An abstract class for managing graphical elements.
Definition: thorvg.h:188
The value returned in case of choosing unsupported options.
An abstract class for drawing graphical elements.
Definition: thorvg.h:383
float offset
Definition: thorvg.h:315
uint8_t g
Definition: thorvg.h:317
A line from the point to a location outside the shape is drawn and its intersections with the path se...
A data structure representing a three-dimensional matrix.
Definition: thorvg.h:171
CanvasEngine
Enumeration specifying the engine type used for the graphics backend. For multiple backeneds bitwise ...
Definition: thorvg.h:148
The outer corner of the joined path segments is spiked. The spike is created by extension beyond the ...
A class representing the linear gradient fill of the Shape object.
Definition: thorvg.h:479
A class that enables initialization and termination of the TVG engines.
Definition: thorvg.h:1243