ThorVG  v0.5
thorvg.h
1 
14 #ifndef _THORVG_H_
15 #define _THORVG_H_
16 
17 #include <memory>
18 #include <string>
19 
20 #ifdef TVG_BUILD
21  #ifdef _MSC_VER
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 IteratorModule; \
54 
55 
56 #define _TVG_DECALRE_IDENTIFIER() \
57  auto id() const { return _id; } \
58 protected: \
59  unsigned _id
60 
61 namespace tvg
62 {
63 
64 class RenderMethod;
65 class IteratorModule;
66 class Scene;
67 class Picture;
68 class Canvas;
69 
80 enum class TVG_EXPORT Result
81 {
82  Success = 0,
87  NonSupport,
88  Unknown
89 };
90 
97 enum class TVG_EXPORT PathCommand
98 {
99  Close = 0,
100  MoveTo,
101  LineTo,
102  CubicTo
103 };
104 
108 enum class TVG_EXPORT StrokeCap
109 {
110  Square = 0,
111  Round,
112  Butt
113 };
114 
118 enum class TVG_EXPORT StrokeJoin
119 {
120  Bevel = 0,
121  Round,
122  Miter
123 };
124 
128 enum class TVG_EXPORT FillSpread
129 {
130  Pad = 0,
131  Reflect,
132  Repeat
133 };
134 
138 enum class TVG_EXPORT FillRule
139 {
140  Winding = 0,
141  EvenOdd
142 };
143 
147 enum class TVG_EXPORT CompositeMethod
148 {
149  None = 0,
150  ClipPath,
151  AlphaMask,
152  InvAlphaMask
153 };
154 
158 enum class TVG_EXPORT CanvasEngine
159 {
160  Sw = (1 << 1),
161  Gl = (1 << 2)
162 };
163 
164 
168 struct Point
169 {
170  float x, y;
171 };
172 
173 
181 struct Matrix
182 {
183  float e11, e12, e13;
184  float e21, e22, e23;
185  float e31, e32, e33;
186 };
187 
188 
198 class TVG_EXPORT Paint
199 {
200 public:
201  virtual ~Paint();
202 
213  Result rotate(float degree) noexcept;
214 
222  Result scale(float factor) noexcept;
223 
235  Result translate(float x, float y) noexcept;
236 
246  Result transform(const Matrix& m) noexcept;
247 
258  Matrix transform() noexcept;
259 
269  Result opacity(uint8_t o) noexcept;
270 
279  Result composite(std::unique_ptr<Paint> target, CompositeMethod method) noexcept;
280 
294  TVG_DEPRECATED Result bounds(float* x, float* y, float* w, float* h) const noexcept;
295 
296 
312  Result bounds(float* x, float* y, float* w, float* h, bool transformed) const noexcept;
313 
314 
322  Paint* duplicate() const noexcept;
323 
329  uint8_t opacity() const noexcept;
330 
340  CompositeMethod composite(const Paint** target) const noexcept;
341 
342  _TVG_DECLARE_ACCESSOR();
343  _TVG_DECALRE_IDENTIFIER();
344  _TVG_DECLARE_PRIVATE(Paint);
345 };
346 
347 
359 class TVG_EXPORT Fill
360 {
361 public:
365  struct ColorStop
366  {
367  float offset;
368  uint8_t r;
369  uint8_t g;
370  uint8_t b;
371  uint8_t a;
372  };
373 
374  virtual ~Fill();
375 
384  Result colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept;
385 
393  Result spread(FillSpread s) noexcept;
394 
402  uint32_t colorStops(const ColorStop** colorStops) const noexcept;
403 
409  FillSpread spread() const noexcept;
410 
418  Fill* duplicate() const noexcept;
419 
420  _TVG_DECALRE_IDENTIFIER();
421  _TVG_DECLARE_PRIVATE(Fill);
422 };
423 
424 
435 class TVG_EXPORT Canvas
436 {
437 public:
438  Canvas(RenderMethod*);
439  virtual ~Canvas();
440 
451  Result reserve(uint32_t n) noexcept;
452 
470  virtual Result push(std::unique_ptr<Paint> paint) noexcept;
471 
482  virtual Result clear(bool free = true) noexcept;
483 
496  virtual Result update(Paint* paint = nullptr) noexcept;
497 
506  virtual Result draw() noexcept;
507 
517  virtual Result sync() noexcept;
518 
519  _TVG_DECLARE_PRIVATE(Canvas);
520 };
521 
522 
531 class TVG_EXPORT LinearGradient final : public Fill
532 {
533 public:
534  ~LinearGradient();
535 
550  Result linear(float x1, float y1, float x2, float y2) noexcept;
551 
566  Result linear(float* x1, float* y1, float* x2, float* y2) const noexcept;
567 
573  static std::unique_ptr<LinearGradient> gen() noexcept;
574 
575  _TVG_DECLARE_PRIVATE(LinearGradient);
576 };
577 
578 
585 class TVG_EXPORT RadialGradient final : public Fill
586 {
587 public:
588  ~RadialGradient();
589 
601  Result radial(float cx, float cy, float radius) noexcept;
602 
614  Result radial(float* cx, float* cy, float* radius) const noexcept;
615 
621  static std::unique_ptr<RadialGradient> gen() noexcept;
622 
623  _TVG_DECLARE_PRIVATE(RadialGradient);
624 };
625 
626 
639 class TVG_EXPORT Shape final : public Paint
640 {
641 public:
642  ~Shape();
643 
653  Result reset() noexcept;
654 
665  Result moveTo(float x, float y) noexcept;
666 
679  Result lineTo(float x, float y) noexcept;
680 
698  Result cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept;
699 
709  Result close() noexcept;
710 
735  Result appendRect(float x, float y, float w, float h, float rx, float ry) noexcept;
736 
753  Result appendCircle(float cx, float cy, float rx, float ry) noexcept;
754 
772  Result appendArc(float cx, float cy, float radius, float startAngle, float sweep, bool pie) noexcept;
773 
790  Result appendPath(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept;
791 
799  Result stroke(float width) noexcept;
800 
811  Result stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
812 
822  Result stroke(std::unique_ptr<Fill> f) noexcept;
823 
838  Result stroke(const float* dashPattern, uint32_t cnt) noexcept;
839 
847  Result stroke(StrokeCap cap) noexcept;
848 
858  Result stroke(StrokeJoin join) noexcept;
859 
874  Result fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
875 
887  Result fill(std::unique_ptr<Fill> f) noexcept;
888 
896  Result fill(FillRule r) noexcept;
897 
905  uint32_t pathCommands(const PathCommand** cmds) const noexcept;
906 
914  uint32_t pathCoords(const Point** pts) const noexcept;
915 
921  const Fill* fill() const noexcept;
922 
933  Result fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
934 
940  FillRule fillRule() const noexcept;
941 
947  float strokeWidth() const noexcept;
948 
959  Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
960 
966  const Fill* strokeFill() const noexcept;
967 
975  uint32_t strokeDash(const float** dashPattern) const noexcept;
976 
982  StrokeCap strokeCap() const noexcept;
983 
989  StrokeJoin strokeJoin() const noexcept;
990 
996  static std::unique_ptr<Shape> gen() noexcept;
997 
998  _TVG_DECLARE_PRIVATE(Shape);
999 };
1000 
1001 
1010 class TVG_EXPORT Picture final : public Paint
1011 {
1012 public:
1013  ~Picture();
1014 
1028  Result load(const std::string& path) noexcept;
1029 
1046  TVG_DEPRECATED Result load(const char* data, uint32_t size, bool copy = false) noexcept;
1047 
1065  Result load(const char* data, uint32_t size, const std::string& mimeType, bool copy = false) noexcept;
1066 
1078  Result size(float w, float h) noexcept;
1079 
1088  Result size(float* w, float* h) const noexcept;
1089 
1097  const uint32_t* data(uint32_t* w, uint32_t* h) const noexcept;
1098 
1106  Result load(uint32_t* data, uint32_t w, uint32_t h, bool copy) noexcept;
1107 
1115  Result viewbox(float* x, float* y, float* w, float* h) const noexcept;
1116 
1122  static std::unique_ptr<Picture> gen() noexcept;
1123 
1124  _TVG_DECLARE_PRIVATE(Picture);
1125 };
1126 
1127 
1139 class TVG_EXPORT Scene final : public Paint
1140 {
1141 public:
1142  ~Scene();
1143 
1158  Result push(std::unique_ptr<Paint> paint) noexcept;
1159 
1170  Result reserve(uint32_t size) noexcept;
1171 
1185  Result clear(bool free = true) noexcept;
1186 
1192  static std::unique_ptr<Scene> gen() noexcept;
1193 
1194  _TVG_DECLARE_PRIVATE(Scene);
1195 };
1196 
1197 
1203 class TVG_EXPORT SwCanvas final : public Canvas
1204 {
1205 public:
1206  ~SwCanvas();
1207 
1212  {
1213  ABGR8888 = 0,
1214  ARGB8888
1215  };
1216 
1222  {
1223  Default = 0,
1225  Individual
1226  };
1227 
1246  Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, Colorspace cs) noexcept;
1247 
1271  Result mempool(MempoolPolicy policy) noexcept;
1272 
1277  static std::unique_ptr<SwCanvas> gen() noexcept;
1278 
1279  _TVG_DECLARE_PRIVATE(SwCanvas);
1280 };
1281 
1282 
1292 class TVG_EXPORT GlCanvas final : public Canvas
1293 {
1294 public:
1295  ~GlCanvas();
1296 
1304  Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h) noexcept;
1305 
1313  static std::unique_ptr<GlCanvas> gen() noexcept;
1314 
1315  _TVG_DECLARE_PRIVATE(GlCanvas);
1316 };
1317 
1318 
1324 class TVG_EXPORT Initializer final
1325 {
1326 public:
1347  static Result init(CanvasEngine engine, uint32_t threads) noexcept;
1348 
1363  static Result term(CanvasEngine engine) noexcept;
1364 
1365  _TVG_DISABLE_CTOR(Initializer);
1366 };
1367 
1368 
1386 class TVG_EXPORT Saver
1387 {
1388 public:
1389  ~Saver();
1390 
1411  Result save(std::unique_ptr<Paint> paint, const std::string& path, bool compress = true) noexcept;
1412 
1428  Result sync() noexcept;
1429 
1437  static std::unique_ptr<Saver> gen() noexcept;
1438 
1439  _TVG_DECLARE_PRIVATE(Saver);
1440 };
1441 
1444 } //namespace
1445 
1446 #ifdef __cplusplus
1447 }
1448 #endif
1449 
1450 #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:365
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:198
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:108
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:1211
tvg::FillRule
FillRule
Enumeration specifying the algorithm used to establish which parts of the shape are treated as the in...
Definition: thorvg.h:138
tvg::CanvasEngine::Sw
@ Sw
CPU rasterizer.
tvg::Result
Result
Enumeration specifying the result from the APIs.
Definition: thorvg.h:80
tvg::FillSpread
FillSpread
Enumeration specifying how to fill the area outside the gradient bounds.
Definition: thorvg.h:128
tvg::Result::Success
@ Success
The value returned in case of a correct request execution.
tvg::Fill::ColorStop::r
uint8_t r
Definition: thorvg.h:368
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:367
tvg::CanvasEngine
CanvasEngine
Enumeration specifying the engine type used for the graphics backend. For multiple backeneds bitwise ...
Definition: thorvg.h:158
tvg::SwCanvas
A class for the rendering graphical elements with a software raster engine.
Definition: thorvg.h:1203
tvg::SwCanvas::Shareable
@ Shareable
Memory Pool is shared among the SwCanvases.
Definition: thorvg.h:1224
tvg::CompositeMethod
CompositeMethod
Enumeration indicating the method used in the composition of two objects - the target and the source.
Definition: thorvg.h:147
tvg::Point
A data structure representing a point in two-dimensional space.
Definition: thorvg.h:168
tvg::Scene
A class to composite children paints.
Definition: thorvg.h:1139
tvg::PathCommand
PathCommand
Enumeration specifying the values of the path commands accepted by TVG.
Definition: thorvg.h:97
tvg::GlCanvas
A class for the rendering graphic elements with a GL raster engine.
Definition: thorvg.h:1292
tvg::Fill::ColorStop::b
uint8_t b
Definition: thorvg.h:370
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:639
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:359
tvg::Fill::ColorStop::a
uint8_t a
Definition: thorvg.h:371
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:1324
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::Matrix
A data structure representing a three-dimensional matrix.
Definition: thorvg.h:181
tvg::SwCanvas::MempoolPolicy
MempoolPolicy
Enumeration specifying the methods of Memory Pool behavior policy.
Definition: thorvg.h:1221
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:118
tvg::LinearGradient
A class representing the linear gradient fill of the Shape object.
Definition: thorvg.h:531
tvg::Picture
A class representing an image read in one of the supported formats: raw, svg, png and etc....
Definition: thorvg.h:1010
tvg::RadialGradient
A class representing the radial gradient fill of the Shape object.
Definition: thorvg.h:585
tvg::Canvas
An abstract class for drawing graphical elements.
Definition: thorvg.h:435
tvg::Fill::ColorStop::g
uint8_t g
Definition: thorvg.h:369
tvg::Saver
A class for exporting a paint object into a specified file, from which to recover the paint data late...
Definition: thorvg.h:1386
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...