ThorVG  v0.8
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 Accessor; \
54  friend IteratorAccessor
55 
56 
57 namespace tvg
58 {
59 
60 class RenderMethod;
61 class IteratorAccessor;
62 class Scene;
63 class Picture;
64 class Canvas;
65 class Accessor;
66 
77 enum class TVG_EXPORT Result
78 {
79  Success = 0,
84  NonSupport,
85  Unknown
86 };
87 
94 enum class TVG_EXPORT PathCommand
95 {
96  Close = 0,
97  MoveTo,
98  LineTo,
99  CubicTo
100 };
101 
105 enum class TVG_EXPORT StrokeCap
106 {
107  Square = 0,
108  Round,
109  Butt
110 };
111 
115 enum class TVG_EXPORT StrokeJoin
116 {
117  Bevel = 0,
118  Round,
119  Miter
120 };
121 
125 enum class TVG_EXPORT FillSpread
126 {
127  Pad = 0,
128  Reflect,
129  Repeat
130 };
131 
135 enum class TVG_EXPORT FillRule
136 {
137  Winding = 0,
138  EvenOdd
139 };
140 
144 enum class TVG_EXPORT CompositeMethod
145 {
146  None = 0,
147  ClipPath,
148  AlphaMask,
149  InvAlphaMask,
150  LumaMask
151 };
152 
156 enum class TVG_EXPORT CanvasEngine
157 {
158  Sw = (1 << 1),
159  Gl = (1 << 2)
160 };
161 
162 
166 struct Point
167 {
168  float x, y;
169 };
170 
171 
179 struct Matrix
180 {
181  float e11, e12, e13;
182  float e21, e22, e23;
183  float e31, e32, e33;
184 };
185 
186 
196 class TVG_EXPORT Paint
197 {
198 public:
199  virtual ~Paint();
200 
211  Result rotate(float degree) noexcept;
212 
220  Result scale(float factor) noexcept;
221 
233  Result translate(float x, float y) noexcept;
234 
244  Result transform(const Matrix& m) noexcept;
245 
256  Matrix transform() noexcept;
257 
267  Result opacity(uint8_t o) noexcept;
268 
277  Result composite(std::unique_ptr<Paint> target, CompositeMethod method) noexcept;
278 
292  TVG_DEPRECATED Result bounds(float* x, float* y, float* w, float* h) const noexcept;
293 
309  Result bounds(float* x, float* y, float* w, float* h, bool transformed) const noexcept;
310 
318  Paint* duplicate() const noexcept;
319 
325  uint8_t opacity() const noexcept;
326 
336  CompositeMethod composite(const Paint** target) const noexcept;
337 
347  uint32_t identifier() const noexcept;
348 
349  _TVG_DECLARE_ACCESSOR();
350  _TVG_DECLARE_PRIVATE(Paint);
351 };
352 
353 
365 class TVG_EXPORT Fill
366 {
367 public:
371  struct ColorStop
372  {
373  float offset;
374  uint8_t r;
375  uint8_t g;
376  uint8_t b;
377  uint8_t a;
378  };
379 
380  virtual ~Fill();
381 
390  Result colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept;
391 
399  Result spread(FillSpread s) noexcept;
400 
410  Result transform(const Matrix& m) noexcept;
411 
419  uint32_t colorStops(const ColorStop** colorStops) const noexcept;
420 
426  FillSpread spread() const noexcept;
427 
435  Matrix transform() const noexcept;
436 
444  Fill* duplicate() const noexcept;
445 
455  uint32_t identifier() const noexcept;
456 
457  _TVG_DECLARE_PRIVATE(Fill);
458 };
459 
460 
471 class TVG_EXPORT Canvas
472 {
473 public:
474  Canvas(RenderMethod*);
475  virtual ~Canvas();
476 
487  Result reserve(uint32_t n) noexcept;
488 
506  virtual Result push(std::unique_ptr<Paint> paint) noexcept;
507 
518  virtual Result clear(bool free = true) noexcept;
519 
532  virtual Result update(Paint* paint = nullptr) noexcept;
533 
542  virtual Result draw() noexcept;
543 
553  virtual Result sync() noexcept;
554 
555  _TVG_DECLARE_PRIVATE(Canvas);
556 };
557 
558 
567 class TVG_EXPORT LinearGradient final : public Fill
568 {
569 public:
570  ~LinearGradient();
571 
588  Result linear(float x1, float y1, float x2, float y2) noexcept;
589 
604  Result linear(float* x1, float* y1, float* x2, float* y2) const noexcept;
605 
611  static std::unique_ptr<LinearGradient> gen() noexcept;
612 
622  static uint32_t identifier() noexcept;
623 
624  _TVG_DECLARE_PRIVATE(LinearGradient);
625 };
626 
627 
634 class TVG_EXPORT RadialGradient final : public Fill
635 {
636 public:
637  ~RadialGradient();
638 
650  Result radial(float cx, float cy, float radius) noexcept;
651 
663  Result radial(float* cx, float* cy, float* radius) const noexcept;
664 
670  static std::unique_ptr<RadialGradient> gen() noexcept;
671 
681  static uint32_t identifier() noexcept;
682 
683  _TVG_DECLARE_PRIVATE(RadialGradient);
684 };
685 
686 
699 class TVG_EXPORT Shape final : public Paint
700 {
701 public:
702  ~Shape();
703 
713  Result reset() noexcept;
714 
725  Result moveTo(float x, float y) noexcept;
726 
739  Result lineTo(float x, float y) noexcept;
740 
758  Result cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept;
759 
769  Result close() noexcept;
770 
795  Result appendRect(float x, float y, float w, float h, float rx, float ry) noexcept;
796 
813  Result appendCircle(float cx, float cy, float rx, float ry) noexcept;
814 
832  Result appendArc(float cx, float cy, float radius, float startAngle, float sweep, bool pie) noexcept;
833 
850  Result appendPath(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept;
851 
859  Result stroke(float width) noexcept;
860 
871  Result stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
872 
882  Result stroke(std::unique_ptr<Fill> f) noexcept;
883 
897  Result stroke(const float* dashPattern, uint32_t cnt) noexcept;
898 
906  Result stroke(StrokeCap cap) noexcept;
907 
917  Result stroke(StrokeJoin join) noexcept;
918 
933  Result fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
934 
946  Result fill(std::unique_ptr<Fill> f) noexcept;
947 
955  Result fill(FillRule r) noexcept;
956 
964  uint32_t pathCommands(const PathCommand** cmds) const noexcept;
965 
973  uint32_t pathCoords(const Point** pts) const noexcept;
974 
980  const Fill* fill() const noexcept;
981 
992  Result fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
993 
999  FillRule fillRule() const noexcept;
1000 
1006  float strokeWidth() const noexcept;
1007 
1018  Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
1019 
1025  const Fill* strokeFill() const noexcept;
1026 
1034  uint32_t strokeDash(const float** dashPattern) const noexcept;
1035 
1041  StrokeCap strokeCap() const noexcept;
1042 
1048  StrokeJoin strokeJoin() const noexcept;
1049 
1055  static std::unique_ptr<Shape> gen() noexcept;
1056 
1066  static uint32_t identifier() noexcept;
1067 
1068  _TVG_DECLARE_PRIVATE(Shape);
1069 };
1070 
1071 
1080 class TVG_EXPORT Picture final : public Paint
1081 {
1082 public:
1083  ~Picture();
1084 
1098  Result load(const std::string& path) noexcept;
1099 
1116  TVG_DEPRECATED Result load(const char* data, uint32_t size, bool copy = false) noexcept;
1117 
1135  Result load(const char* data, uint32_t size, const std::string& mimeType, bool copy = false) noexcept;
1136 
1148  Result size(float w, float h) noexcept;
1149 
1158  Result size(float* w, float* h) const noexcept;
1159 
1169  const uint32_t* data(uint32_t* w, uint32_t* h) const noexcept;
1170 
1178  Result load(uint32_t* data, uint32_t w, uint32_t h, bool copy) noexcept;
1179 
1187  Result viewbox(float* x, float* y, float* w, float* h) const noexcept;
1188 
1194  static std::unique_ptr<Picture> gen() noexcept;
1195 
1205  static uint32_t identifier() noexcept;
1206 
1207  _TVG_DECLARE_PRIVATE(Picture);
1208 };
1209 
1210 
1222 class TVG_EXPORT Scene final : public Paint
1223 {
1224 public:
1225  ~Scene();
1226 
1241  Result push(std::unique_ptr<Paint> paint) noexcept;
1242 
1253  Result reserve(uint32_t size) noexcept;
1254 
1267  Result clear(bool free = true) noexcept;
1268 
1274  static std::unique_ptr<Scene> gen() noexcept;
1275 
1285  static uint32_t identifier() noexcept;
1286 
1287  _TVG_DECLARE_PRIVATE(Scene);
1288 };
1289 
1290 
1296 class TVG_EXPORT SwCanvas final : public Canvas
1297 {
1298 public:
1299  ~SwCanvas();
1300 
1305  {
1306  ABGR8888 = 0,
1310  };
1311 
1317  {
1318  Default = 0,
1320  Individual
1321  };
1322 
1341  Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, Colorspace cs) noexcept;
1342 
1366  Result mempool(MempoolPolicy policy) noexcept;
1367 
1372  static std::unique_ptr<SwCanvas> gen() noexcept;
1373 
1374  _TVG_DECLARE_PRIVATE(SwCanvas);
1375 };
1376 
1377 
1387 class TVG_EXPORT GlCanvas final : public Canvas
1388 {
1389 public:
1390  ~GlCanvas();
1391 
1399  Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h) noexcept;
1400 
1408  static std::unique_ptr<GlCanvas> gen() noexcept;
1409 
1410  _TVG_DECLARE_PRIVATE(GlCanvas);
1411 };
1412 
1413 
1419 class TVG_EXPORT Initializer final
1420 {
1421 public:
1442  static Result init(CanvasEngine engine, uint32_t threads) noexcept;
1443 
1458  static Result term(CanvasEngine engine) noexcept;
1459 
1460  _TVG_DISABLE_CTOR(Initializer);
1461 };
1462 
1463 
1481 class TVG_EXPORT Saver final
1482 {
1483 public:
1484  ~Saver();
1485 
1508  Result save(std::unique_ptr<Paint> paint, const std::string& path, bool compress = true) noexcept;
1509 
1525  Result sync() noexcept;
1526 
1534  static std::unique_ptr<Saver> gen() noexcept;
1535 
1536  _TVG_DECLARE_PRIVATE(Saver);
1537 };
1538 
1539 
1551 class TVG_EXPORT Accessor final
1552 {
1553 public:
1554  ~Accessor();
1555 
1568  std::unique_ptr<Picture> access(std::unique_ptr<Picture> picture, bool(*func)(const Paint* paint)) noexcept;
1569 
1577  static std::unique_ptr<Accessor> gen() noexcept;
1578 
1579  _TVG_DECLARE_PRIVATE(Accessor);
1580 };
1581 
1584 } //namespace
1585 
1586 #ifdef __cplusplus
1587 }
1588 #endif
1589 
1590 #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:371
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:196
tvg::CompositeMethod::LumaMask
@ LumaMask
@BETA_API The source pixels are converted to the grayscale (luma value) and alpha blended with the ta...
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:1308
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:105
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:1304
tvg::FillRule
FillRule
Enumeration specifying the algorithm used to establish which parts of the shape are treated as the in...
Definition: thorvg.h:135
tvg::CanvasEngine::Sw
@ Sw
CPU rasterizer.
tvg::Result
Result
Enumeration specifying the result from the APIs.
Definition: thorvg.h:77
tvg::SwCanvas::ARGB8888
@ ARGB8888
The channels are joined in the order: alpha, red, green, blue. Colors are alpha-premultiplied.
Definition: thorvg.h:1307
tvg::FillSpread
FillSpread
Enumeration specifying how to fill the area outside the gradient bounds.
Definition: thorvg.h:125
tvg::Result::Success
@ Success
The value returned in case of a correct request execution.
tvg::Fill::ColorStop::r
uint8_t r
Definition: thorvg.h:374
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:373
tvg::CanvasEngine
CanvasEngine
Enumeration specifying the engine type used for the graphics backend. For multiple backends bitwise o...
Definition: thorvg.h:156
tvg::SwCanvas
A class for the rendering graphical elements with a software raster engine.
Definition: thorvg.h:1296
tvg::SwCanvas::Shareable
@ Shareable
Memory Pool is shared among the SwCanvases.
Definition: thorvg.h:1319
tvg::CompositeMethod
CompositeMethod
Enumeration indicating the method used in the composition of two objects - the target and the source.
Definition: thorvg.h:144
tvg::Point
A data structure representing a point in two-dimensional space.
Definition: thorvg.h:166
tvg::Scene
A class to composite children paints.
Definition: thorvg.h:1222
tvg::PathCommand
PathCommand
Enumeration specifying the values of the path commands accepted by TVG.
Definition: thorvg.h:94
tvg::GlCanvas
A class for the rendering graphic elements with a GL raster engine.
Definition: thorvg.h:1387
tvg::Fill::ColorStop::b
uint8_t b
Definition: thorvg.h:376
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:699
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:365
tvg::Fill::ColorStop::a
uint8_t a
Definition: thorvg.h:377
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:1419
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:1309
tvg::Matrix
A data structure representing a three-dimensional matrix.
Definition: thorvg.h:179
tvg::Accessor
The Accessor is a utility class to debug the Scene structure by traversing the scene-tree.
Definition: thorvg.h:1551
tvg::SwCanvas::MempoolPolicy
MempoolPolicy
Enumeration specifying the methods of Memory Pool behavior policy.
Definition: thorvg.h:1316
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:115
tvg::LinearGradient
A class representing the linear gradient fill of the Shape object.
Definition: thorvg.h:567
tvg::Picture
A class representing an image read in one of the supported formats: raw, svg, png,...
Definition: thorvg.h:1080
tvg::RadialGradient
A class representing the radial gradient fill of the Shape object.
Definition: thorvg.h:634
tvg::Canvas
An abstract class for drawing graphical elements.
Definition: thorvg.h:471
tvg::Fill::ColorStop::g
uint8_t g
Definition: thorvg.h:375
tvg::Saver
A class for exporting a paint object into a specified file, from which to recover the paint data late...
Definition: thorvg.h:1481
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...