ThorVG  v0.6
thorvg.h
1 
14 #ifndef _THORVG_H_
15 #define _THORVG_H_
16 
17 #include <memory>
18 #include <string>
19 
20 #ifdef TVG_BUILD
21  #if defined(_MSC_VER) && !defined(__clang__)
22  #define TVG_EXPORT __declspec(dllexport)
23  #define TVG_DEPRECATED __declspec(deprecated)
24  #else
25  #define TVG_EXPORT __attribute__ ((visibility ("default")))
26  #define TVG_DEPRECATED __attribute__ ((__deprecated__))
27  #endif
28 #else
29  #define TVG_EXPORT
30  #define TVG_DEPRECATED
31 #endif
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #define _TVG_DECLARE_PRIVATE(A) \
38 protected: \
39  struct Impl; \
40  Impl* pImpl; \
41  A(const A&) = delete; \
42  const A& operator=(const A&) = delete; \
43  A()
44 
45 #define _TVG_DISABLE_CTOR(A) \
46  A() = delete; \
47  ~A() = delete
48 
49 #define _TVG_DECLARE_ACCESSOR() \
50  friend Canvas; \
51  friend Scene; \
52  friend Picture; \
53  friend IteratorAccessor
54 
55 
56 #define _TVG_DECALRE_IDENTIFIER() \
57 protected: \
58  unsigned _id
59 
60 namespace tvg
61 {
62 
63 class RenderMethod;
64 class IteratorAccessor;
65 class Scene;
66 class Picture;
67 class Canvas;
68 
79 enum class TVG_EXPORT Result
80 {
81  Success = 0,
86  NonSupport,
87  Unknown
88 };
89 
96 enum class TVG_EXPORT PathCommand
97 {
98  Close = 0,
99  MoveTo,
100  LineTo,
101  CubicTo
102 };
103 
107 enum class TVG_EXPORT StrokeCap
108 {
109  Square = 0,
110  Round,
111  Butt
112 };
113 
117 enum class TVG_EXPORT StrokeJoin
118 {
119  Bevel = 0,
120  Round,
121  Miter
122 };
123 
127 enum class TVG_EXPORT FillSpread
128 {
129  Pad = 0,
130  Reflect,
131  Repeat
132 };
133 
137 enum class TVG_EXPORT FillRule
138 {
139  Winding = 0,
140  EvenOdd
141 };
142 
146 enum class TVG_EXPORT CompositeMethod
147 {
148  None = 0,
149  ClipPath,
150  AlphaMask,
151  InvAlphaMask
152 };
153 
157 enum class TVG_EXPORT CanvasEngine
158 {
159  Sw = (1 << 1),
160  Gl = (1 << 2)
161 };
162 
163 
167 struct Point
168 {
169  float x, y;
170 };
171 
172 
180 struct Matrix
181 {
182  float e11, e12, e13;
183  float e21, e22, e23;
184  float e31, e32, e33;
185 };
186 
187 
197 class TVG_EXPORT Paint
198 {
199 public:
200  virtual ~Paint();
201 
212  Result rotate(float degree) noexcept;
213 
221  Result scale(float factor) noexcept;
222 
234  Result translate(float x, float y) noexcept;
235 
245  Result transform(const Matrix& m) noexcept;
246 
257  Matrix transform() noexcept;
258 
268  Result opacity(uint8_t o) noexcept;
269 
278  Result composite(std::unique_ptr<Paint> target, CompositeMethod method) noexcept;
279 
293  TVG_DEPRECATED Result bounds(float* x, float* y, float* w, float* h) const noexcept;
294 
312  Result bounds(float* x, float* y, float* w, float* h, bool transformed) const noexcept;
313 
321  Paint* duplicate() const noexcept;
322 
328  uint8_t opacity() const noexcept;
329 
339  CompositeMethod composite(const Paint** target) const noexcept;
340 
350  uint32_t identifier() const { return _id; }
351 
352  _TVG_DECLARE_ACCESSOR();
353  _TVG_DECALRE_IDENTIFIER();
354  _TVG_DECLARE_PRIVATE(Paint);
355 };
356 
357 
369 class TVG_EXPORT Fill
370 {
371 public:
375  struct ColorStop
376  {
377  float offset;
378  uint8_t r;
379  uint8_t g;
380  uint8_t b;
381  uint8_t a;
382  };
383 
384  virtual ~Fill();
385 
394  Result colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept;
395 
403  Result spread(FillSpread s) noexcept;
404 
416  Result transform(const Matrix& m) noexcept;
417 
425  uint32_t colorStops(const ColorStop** colorStops) const noexcept;
426 
432  FillSpread spread() const noexcept;
433 
443  Matrix transform() const noexcept;
444 
452  Fill* duplicate() const noexcept;
453 
463  uint32_t identifier() const { return _id; }
464 
465  _TVG_DECALRE_IDENTIFIER();
466  _TVG_DECLARE_PRIVATE(Fill);
467 };
468 
469 
480 class TVG_EXPORT Canvas
481 {
482 public:
483  Canvas(RenderMethod*);
484  virtual ~Canvas();
485 
496  Result reserve(uint32_t n) noexcept;
497 
515  virtual Result push(std::unique_ptr<Paint> paint) noexcept;
516 
527  virtual Result clear(bool free = true) noexcept;
528 
541  virtual Result update(Paint* paint = nullptr) noexcept;
542 
551  virtual Result draw() noexcept;
552 
562  virtual Result sync() noexcept;
563 
564  _TVG_DECLARE_PRIVATE(Canvas);
565 };
566 
567 
576 class TVG_EXPORT LinearGradient final : public Fill
577 {
578 public:
579  ~LinearGradient();
580 
597  Result linear(float x1, float y1, float x2, float y2) noexcept;
598 
613  Result linear(float* x1, float* y1, float* x2, float* y2) const noexcept;
614 
620  static std::unique_ptr<LinearGradient> gen() noexcept;
621 
631  static uint32_t identifier() noexcept;
632 
633  _TVG_DECLARE_PRIVATE(LinearGradient);
634 };
635 
636 
643 class TVG_EXPORT RadialGradient final : public Fill
644 {
645 public:
646  ~RadialGradient();
647 
659  Result radial(float cx, float cy, float radius) noexcept;
660 
672  Result radial(float* cx, float* cy, float* radius) const noexcept;
673 
679  static std::unique_ptr<RadialGradient> gen() noexcept;
680 
690  static uint32_t identifier() noexcept;
691 
692  _TVG_DECLARE_PRIVATE(RadialGradient);
693 };
694 
695 
708 class TVG_EXPORT Shape final : public Paint
709 {
710 public:
711  ~Shape();
712 
722  Result reset() noexcept;
723 
734  Result moveTo(float x, float y) noexcept;
735 
748  Result lineTo(float x, float y) noexcept;
749 
767  Result cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept;
768 
778  Result close() noexcept;
779 
804  Result appendRect(float x, float y, float w, float h, float rx, float ry) noexcept;
805 
822  Result appendCircle(float cx, float cy, float rx, float ry) noexcept;
823 
841  Result appendArc(float cx, float cy, float radius, float startAngle, float sweep, bool pie) noexcept;
842 
859  Result appendPath(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept;
860 
868  Result stroke(float width) noexcept;
869 
880  Result stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
881 
891  Result stroke(std::unique_ptr<Fill> f) noexcept;
892 
906  Result stroke(const float* dashPattern, uint32_t cnt) noexcept;
907 
915  Result stroke(StrokeCap cap) noexcept;
916 
926  Result stroke(StrokeJoin join) noexcept;
927 
942  Result fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
943 
955  Result fill(std::unique_ptr<Fill> f) noexcept;
956 
964  Result fill(FillRule r) noexcept;
965 
973  uint32_t pathCommands(const PathCommand** cmds) const noexcept;
974 
982  uint32_t pathCoords(const Point** pts) const noexcept;
983 
989  const Fill* fill() const noexcept;
990 
1001  Result fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
1002 
1008  FillRule fillRule() const noexcept;
1009 
1015  float strokeWidth() const noexcept;
1016 
1027  Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
1028 
1034  const Fill* strokeFill() const noexcept;
1035 
1043  uint32_t strokeDash(const float** dashPattern) const noexcept;
1044 
1050  StrokeCap strokeCap() const noexcept;
1051 
1057  StrokeJoin strokeJoin() const noexcept;
1058 
1064  static std::unique_ptr<Shape> gen() noexcept;
1065 
1075  static uint32_t identifier() noexcept;
1076 
1077  _TVG_DECLARE_PRIVATE(Shape);
1078 };
1079 
1080 
1089 class TVG_EXPORT Picture final : public Paint
1090 {
1091 public:
1092  ~Picture();
1093 
1107  Result load(const std::string& path) noexcept;
1108 
1125  TVG_DEPRECATED Result load(const char* data, uint32_t size, bool copy = false) noexcept;
1126 
1144  Result load(const char* data, uint32_t size, const std::string& mimeType, bool copy = false) noexcept;
1145 
1157  Result size(float w, float h) noexcept;
1158 
1167  Result size(float* w, float* h) const noexcept;
1168 
1176  const uint32_t* data(uint32_t* w, uint32_t* h) const noexcept;
1177 
1185  Result load(uint32_t* data, uint32_t w, uint32_t h, bool copy) noexcept;
1186 
1194  Result viewbox(float* x, float* y, float* w, float* h) const noexcept;
1195 
1201  static std::unique_ptr<Picture> gen() noexcept;
1202 
1212  static uint32_t identifier() noexcept;
1213 
1214  _TVG_DECLARE_PRIVATE(Picture);
1215 };
1216 
1217 
1229 class TVG_EXPORT Scene final : public Paint
1230 {
1231 public:
1232  ~Scene();
1233 
1248  Result push(std::unique_ptr<Paint> paint) noexcept;
1249 
1260  Result reserve(uint32_t size) noexcept;
1261 
1274  Result clear(bool free = true) noexcept;
1275 
1281  static std::unique_ptr<Scene> gen() noexcept;
1282 
1292  static uint32_t identifier() noexcept;
1293 
1294  _TVG_DECLARE_PRIVATE(Scene);
1295 };
1296 
1297 
1303 class TVG_EXPORT SwCanvas final : public Canvas
1304 {
1305 public:
1306  ~SwCanvas();
1307 
1312  {
1313  ABGR8888 = 0,
1317  };
1318 
1324  {
1325  Default = 0,
1327  Individual
1328  };
1329 
1348  Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, Colorspace cs) noexcept;
1349 
1373  Result mempool(MempoolPolicy policy) noexcept;
1374 
1379  static std::unique_ptr<SwCanvas> gen() noexcept;
1380 
1381  _TVG_DECLARE_PRIVATE(SwCanvas);
1382 };
1383 
1384 
1394 class TVG_EXPORT GlCanvas final : public Canvas
1395 {
1396 public:
1397  ~GlCanvas();
1398 
1406  Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h) noexcept;
1407 
1415  static std::unique_ptr<GlCanvas> gen() noexcept;
1416 
1417  _TVG_DECLARE_PRIVATE(GlCanvas);
1418 };
1419 
1420 
1426 class TVG_EXPORT Initializer final
1427 {
1428 public:
1449  static Result init(CanvasEngine engine, uint32_t threads) noexcept;
1450 
1465  static Result term(CanvasEngine engine) noexcept;
1466 
1467  _TVG_DISABLE_CTOR(Initializer);
1468 };
1469 
1470 
1488 class TVG_EXPORT Saver final
1489 {
1490 public:
1491  ~Saver();
1492 
1515  Result save(std::unique_ptr<Paint> paint, const std::string& path, bool compress = true) noexcept;
1516 
1532  Result sync() noexcept;
1533 
1541  static std::unique_ptr<Saver> gen() noexcept;
1542 
1543  _TVG_DECLARE_PRIVATE(Saver);
1544 };
1545 
1548 } //namespace
1549 
1550 #ifdef __cplusplus
1551 }
1552 #endif
1553 
1554 #endif //_THORVG_H_
tvg::Fill::ColorStop
A data structure storing the information about the color and its relative position inside the gradien...
Definition: thorvg.h:375
tvg::Result::MemoryCorruption
@ MemoryCorruption
The value returned in the event of bad memory handling - e.g. failing in pointer releasing or casting...
tvg::Result::InvalidArguments
@ InvalidArguments
The value returned in the event of a problem with the arguments given to the API - e....
tvg::Paint
An abstract class for managing graphical elements.
Definition: thorvg.h:197
tvg::SwCanvas::ABGR8888_STRAIGHT
@ ABGR8888_STRAIGHT
@BETA_API The channels are joined in the order: alpha, blue, green, red. Colors are un-alpha-premulti...
Definition: thorvg.h:1315
tvg::FillSpread::Repeat
@ Repeat
The gradient pattern is repeated continuously beyond the gradient area until the expected region is f...
tvg::StrokeCap
StrokeCap
Enumeration determining the ending type of a stroke in the open sub-paths.
Definition: thorvg.h:107
tvg::FillRule::Winding
@ Winding
A line from the point to a location outside the shape is drawn. The intersections of the line with th...
tvg::Result::NonSupport
@ NonSupport
The value returned in case of choosing unsupported options.
tvg::CanvasEngine::Gl
@ Gl
OpenGL rasterizer.
tvg::SwCanvas::Colorspace
Colorspace
Enumeration specifying the methods of combining the 8-bit color channels into 32-bit color.
Definition: thorvg.h:1311
tvg::FillRule
FillRule
Enumeration specifying the algorithm used to establish which parts of the shape are treated as the in...
Definition: thorvg.h:137
tvg::CanvasEngine::Sw
@ Sw
CPU rasterizer.
tvg::Result
Result
Enumeration specifying the result from the APIs.
Definition: thorvg.h:79
tvg::SwCanvas::ARGB8888
@ ARGB8888
The channels are joined in the order: alpha, red, green, blue. Colors are alpha-premultiplied.
Definition: thorvg.h:1314
tvg::FillSpread
FillSpread
Enumeration specifying how to fill the area outside the gradient bounds.
Definition: thorvg.h:127
tvg::Result::Success
@ Success
The value returned in case of a correct request execution.
tvg::Fill::ColorStop::r
uint8_t r
Definition: thorvg.h:378
tvg::StrokeCap::Butt
@ Butt
The stroke ends exactly at each of the two end-points of a sub-path. For zero length sub-paths no str...
tvg::PathCommand::MoveTo
@ MoveTo
Sets a new initial point of the sub-path and a new current point. This command expects 1 point: the s...
tvg::Fill::ColorStop::offset
float offset
Definition: thorvg.h:377
tvg::CanvasEngine
CanvasEngine
Enumeration specifying the engine type used for the graphics backend. For multiple backends bitwise o...
Definition: thorvg.h:157
tvg::SwCanvas
A class for the rendering graphical elements with a software raster engine.
Definition: thorvg.h:1303
tvg::SwCanvas::Shareable
@ Shareable
Memory Pool is shared among the SwCanvases.
Definition: thorvg.h:1326
tvg::CompositeMethod
CompositeMethod
Enumeration indicating the method used in the composition of two objects - the target and the source.
Definition: thorvg.h:146
tvg::Point
A data structure representing a point in two-dimensional space.
Definition: thorvg.h:167
tvg::Scene
A class to composite children paints.
Definition: thorvg.h:1229
tvg::PathCommand
PathCommand
Enumeration specifying the values of the path commands accepted by TVG.
Definition: thorvg.h:96
tvg::GlCanvas
A class for the rendering graphic elements with a GL raster engine.
Definition: thorvg.h:1394
tvg::Fill::ColorStop::b
uint8_t b
Definition: thorvg.h:380
tvg::CompositeMethod::ClipPath
@ ClipPath
The intersection of the source and the target is determined and only the resulting pixels from the so...
tvg::Shape
A class representing two-dimensional figures and their properties.
Definition: thorvg.h:708
tvg::FillSpread::Reflect
@ Reflect
The gradient pattern is reflected outside the gradient area until the expected region is filled.
tvg::Fill
An abstract class representing the gradient fill of the Shape object.
Definition: thorvg.h:369
tvg::Fill::ColorStop::a
uint8_t a
Definition: thorvg.h:381
tvg::StrokeCap::Round
@ Round
The stroke is extended in both end-points of a sub-path by a half circle, with a radius equal to the ...
tvg::CompositeMethod::None
@ None
No composition is applied.
tvg::Result::Unknown
@ Unknown
The value returned in all other cases.
tvg::PathCommand::CubicTo
@ CubicTo
Draws a cubic Bezier curve from the current point to the given point using two given control points a...
tvg::CompositeMethod::InvAlphaMask
@ InvAlphaMask
The pixels of the source and the complement to the target's pixels are alpha blended....
tvg::Initializer
A class that enables initialization and termination of the TVG engines.
Definition: thorvg.h:1426
tvg::CompositeMethod::AlphaMask
@ AlphaMask
The pixels of the source and the target are alpha blended. As a result, only the part of the source,...
tvg::StrokeJoin::Miter
@ Miter
The outer corner of the joined path segments is spiked. The spike is created by extension beyond the ...
tvg::PathCommand::LineTo
@ LineTo
Draws a line from the current point to the given point and sets a new value of the current point....
tvg::Result::FailedAllocation
@ FailedAllocation
The value returned in case of unsuccessful memory allocation.
tvg::FillRule::EvenOdd
@ EvenOdd
A line from the point to a location outside the shape is drawn and its intersections with the path se...
tvg::SwCanvas::ARGB8888_STRAIGHT
@ ARGB8888_STRAIGHT
@BETA_API The channels are joined in the order: alpha, red, green, blue. Colors are un-alpha-premulti...
Definition: thorvg.h:1316
tvg::Matrix
A data structure representing a three-dimensional matrix.
Definition: thorvg.h:180
tvg::SwCanvas::MempoolPolicy
MempoolPolicy
Enumeration specifying the methods of Memory Pool behavior policy.
Definition: thorvg.h:1323
tvg::Result::InsufficientCondition
@ InsufficientCondition
The value returned in case the request cannot be processed - e.g. asking for properties of an object,...
tvg::StrokeJoin::Bevel
@ Bevel
The outer corner of the joined path segments is bevelled at the join point. The triangular region of ...
tvg::PathCommand::Close
@ Close
Ends the current sub-path and connects it with its initial point. This command doesn't expect any poi...
tvg::FillSpread::Pad
@ Pad
The remaining area is filled with the closest stop color.
tvg::StrokeJoin
StrokeJoin
Enumeration determining the style used at the corners of joined stroked path segments.
Definition: thorvg.h:117
tvg::LinearGradient
A class representing the linear gradient fill of the Shape object.
Definition: thorvg.h:576
tvg::Picture
A class representing an image read in one of the supported formats: raw, svg, png,...
Definition: thorvg.h:1089
tvg::RadialGradient
A class representing the radial gradient fill of the Shape object.
Definition: thorvg.h:643
tvg::Canvas
An abstract class for drawing graphical elements.
Definition: thorvg.h:480
tvg::Fill::ColorStop::g
uint8_t g
Definition: thorvg.h:379
tvg::Paint::identifier
uint32_t identifier() const
Return the unique id value of the paint instance.
Definition: thorvg.h:350
tvg::Saver
A class for exporting a paint object into a specified file, from which to recover the paint data late...
Definition: thorvg.h:1488
tvg::StrokeCap::Square
@ Square
The stroke is extended in both end-points of a sub-path by a rectangle, with the width equal to the s...