ThorVG  v0.7
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 namespace tvg
57 {
58 
59 class RenderMethod;
60 class IteratorAccessor;
61 class Scene;
62 class Picture;
63 class Canvas;
64 
75 enum class TVG_EXPORT Result
76 {
77  Success = 0,
82  NonSupport,
83  Unknown
84 };
85 
92 enum class TVG_EXPORT PathCommand
93 {
94  Close = 0,
95  MoveTo,
96  LineTo,
97  CubicTo
98 };
99 
103 enum class TVG_EXPORT StrokeCap
104 {
105  Square = 0,
106  Round,
107  Butt
108 };
109 
113 enum class TVG_EXPORT StrokeJoin
114 {
115  Bevel = 0,
116  Round,
117  Miter
118 };
119 
123 enum class TVG_EXPORT FillSpread
124 {
125  Pad = 0,
126  Reflect,
127  Repeat
128 };
129 
133 enum class TVG_EXPORT FillRule
134 {
135  Winding = 0,
136  EvenOdd
137 };
138 
142 enum class TVG_EXPORT CompositeMethod
143 {
144  None = 0,
145  ClipPath,
146  AlphaMask,
147  InvAlphaMask
148 };
149 
153 enum class TVG_EXPORT CanvasEngine
154 {
155  Sw = (1 << 1),
156  Gl = (1 << 2)
157 };
158 
159 
163 struct Point
164 {
165  float x, y;
166 };
167 
168 
176 struct Matrix
177 {
178  float e11, e12, e13;
179  float e21, e22, e23;
180  float e31, e32, e33;
181 };
182 
183 
193 class TVG_EXPORT Paint
194 {
195 public:
196  virtual ~Paint();
197 
208  Result rotate(float degree) noexcept;
209 
217  Result scale(float factor) noexcept;
218 
230  Result translate(float x, float y) noexcept;
231 
241  Result transform(const Matrix& m) noexcept;
242 
253  Matrix transform() noexcept;
254 
264  Result opacity(uint8_t o) noexcept;
265 
274  Result composite(std::unique_ptr<Paint> target, CompositeMethod method) noexcept;
275 
289  TVG_DEPRECATED Result bounds(float* x, float* y, float* w, float* h) const noexcept;
290 
306  Result bounds(float* x, float* y, float* w, float* h, bool transformed) const noexcept;
307 
315  Paint* duplicate() const noexcept;
316 
322  uint8_t opacity() const noexcept;
323 
333  CompositeMethod composite(const Paint** target) const noexcept;
334 
344  uint32_t identifier() const noexcept;
345 
346  _TVG_DECLARE_ACCESSOR();
347  _TVG_DECLARE_PRIVATE(Paint);
348 };
349 
350 
362 class TVG_EXPORT Fill
363 {
364 public:
368  struct ColorStop
369  {
370  float offset;
371  uint8_t r;
372  uint8_t g;
373  uint8_t b;
374  uint8_t a;
375  };
376 
377  virtual ~Fill();
378 
387  Result colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept;
388 
396  Result spread(FillSpread s) noexcept;
397 
407  Result transform(const Matrix& m) noexcept;
408 
416  uint32_t colorStops(const ColorStop** colorStops) const noexcept;
417 
423  FillSpread spread() const noexcept;
424 
432  Matrix transform() const noexcept;
433 
441  Fill* duplicate() const noexcept;
442 
452  uint32_t identifier() const noexcept;
453 
454  _TVG_DECLARE_PRIVATE(Fill);
455 };
456 
457 
468 class TVG_EXPORT Canvas
469 {
470 public:
471  Canvas(RenderMethod*);
472  virtual ~Canvas();
473 
484  Result reserve(uint32_t n) noexcept;
485 
503  virtual Result push(std::unique_ptr<Paint> paint) noexcept;
504 
515  virtual Result clear(bool free = true) noexcept;
516 
529  virtual Result update(Paint* paint = nullptr) noexcept;
530 
539  virtual Result draw() noexcept;
540 
550  virtual Result sync() noexcept;
551 
552  _TVG_DECLARE_PRIVATE(Canvas);
553 };
554 
555 
564 class TVG_EXPORT LinearGradient final : public Fill
565 {
566 public:
567  ~LinearGradient();
568 
585  Result linear(float x1, float y1, float x2, float y2) noexcept;
586 
601  Result linear(float* x1, float* y1, float* x2, float* y2) const noexcept;
602 
608  static std::unique_ptr<LinearGradient> gen() noexcept;
609 
619  static uint32_t identifier() noexcept;
620 
621  _TVG_DECLARE_PRIVATE(LinearGradient);
622 };
623 
624 
631 class TVG_EXPORT RadialGradient final : public Fill
632 {
633 public:
634  ~RadialGradient();
635 
647  Result radial(float cx, float cy, float radius) noexcept;
648 
660  Result radial(float* cx, float* cy, float* radius) const noexcept;
661 
667  static std::unique_ptr<RadialGradient> gen() noexcept;
668 
678  static uint32_t identifier() noexcept;
679 
680  _TVG_DECLARE_PRIVATE(RadialGradient);
681 };
682 
683 
696 class TVG_EXPORT Shape final : public Paint
697 {
698 public:
699  ~Shape();
700 
710  Result reset() noexcept;
711 
722  Result moveTo(float x, float y) noexcept;
723 
736  Result lineTo(float x, float y) noexcept;
737 
755  Result cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept;
756 
766  Result close() noexcept;
767 
792  Result appendRect(float x, float y, float w, float h, float rx, float ry) noexcept;
793 
810  Result appendCircle(float cx, float cy, float rx, float ry) noexcept;
811 
829  Result appendArc(float cx, float cy, float radius, float startAngle, float sweep, bool pie) noexcept;
830 
847  Result appendPath(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept;
848 
856  Result stroke(float width) noexcept;
857 
868  Result stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
869 
879  Result stroke(std::unique_ptr<Fill> f) noexcept;
880 
894  Result stroke(const float* dashPattern, uint32_t cnt) noexcept;
895 
903  Result stroke(StrokeCap cap) noexcept;
904 
914  Result stroke(StrokeJoin join) noexcept;
915 
930  Result fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
931 
943  Result fill(std::unique_ptr<Fill> f) noexcept;
944 
952  Result fill(FillRule r) noexcept;
953 
961  uint32_t pathCommands(const PathCommand** cmds) const noexcept;
962 
970  uint32_t pathCoords(const Point** pts) const noexcept;
971 
977  const Fill* fill() const noexcept;
978 
989  Result fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
990 
996  FillRule fillRule() const noexcept;
997 
1003  float strokeWidth() const noexcept;
1004 
1015  Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
1016 
1022  const Fill* strokeFill() const noexcept;
1023 
1031  uint32_t strokeDash(const float** dashPattern) const noexcept;
1032 
1038  StrokeCap strokeCap() const noexcept;
1039 
1045  StrokeJoin strokeJoin() const noexcept;
1046 
1052  static std::unique_ptr<Shape> gen() noexcept;
1053 
1063  static uint32_t identifier() noexcept;
1064 
1065  _TVG_DECLARE_PRIVATE(Shape);
1066 };
1067 
1068 
1077 class TVG_EXPORT Picture final : public Paint
1078 {
1079 public:
1080  ~Picture();
1081 
1095  Result load(const std::string& path) noexcept;
1096 
1113  TVG_DEPRECATED Result load(const char* data, uint32_t size, bool copy = false) noexcept;
1114 
1132  Result load(const char* data, uint32_t size, const std::string& mimeType, bool copy = false) noexcept;
1133 
1145  Result size(float w, float h) noexcept;
1146 
1155  Result size(float* w, float* h) const noexcept;
1156 
1166  const uint32_t* data(uint32_t* w, uint32_t* h) const noexcept;
1167 
1175  Result load(uint32_t* data, uint32_t w, uint32_t h, bool copy) noexcept;
1176 
1184  Result viewbox(float* x, float* y, float* w, float* h) const noexcept;
1185 
1191  static std::unique_ptr<Picture> gen() noexcept;
1192 
1202  static uint32_t identifier() noexcept;
1203 
1204  _TVG_DECLARE_PRIVATE(Picture);
1205 };
1206 
1207 
1219 class TVG_EXPORT Scene final : public Paint
1220 {
1221 public:
1222  ~Scene();
1223 
1238  Result push(std::unique_ptr<Paint> paint) noexcept;
1239 
1250  Result reserve(uint32_t size) noexcept;
1251 
1264  Result clear(bool free = true) noexcept;
1265 
1271  static std::unique_ptr<Scene> gen() noexcept;
1272 
1282  static uint32_t identifier() noexcept;
1283 
1284  _TVG_DECLARE_PRIVATE(Scene);
1285 };
1286 
1287 
1293 class TVG_EXPORT SwCanvas final : public Canvas
1294 {
1295 public:
1296  ~SwCanvas();
1297 
1302  {
1303  ABGR8888 = 0,
1307  };
1308 
1314  {
1315  Default = 0,
1317  Individual
1318  };
1319 
1338  Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, Colorspace cs) noexcept;
1339 
1363  Result mempool(MempoolPolicy policy) noexcept;
1364 
1369  static std::unique_ptr<SwCanvas> gen() noexcept;
1370 
1371  _TVG_DECLARE_PRIVATE(SwCanvas);
1372 };
1373 
1374 
1384 class TVG_EXPORT GlCanvas final : public Canvas
1385 {
1386 public:
1387  ~GlCanvas();
1388 
1396  Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h) noexcept;
1397 
1405  static std::unique_ptr<GlCanvas> gen() noexcept;
1406 
1407  _TVG_DECLARE_PRIVATE(GlCanvas);
1408 };
1409 
1410 
1416 class TVG_EXPORT Initializer final
1417 {
1418 public:
1439  static Result init(CanvasEngine engine, uint32_t threads) noexcept;
1440 
1455  static Result term(CanvasEngine engine) noexcept;
1456 
1457  _TVG_DISABLE_CTOR(Initializer);
1458 };
1459 
1460 
1478 class TVG_EXPORT Saver final
1479 {
1480 public:
1481  ~Saver();
1482 
1505  Result save(std::unique_ptr<Paint> paint, const std::string& path, bool compress = true) noexcept;
1506 
1522  Result sync() noexcept;
1523 
1531  static std::unique_ptr<Saver> gen() noexcept;
1532 
1533  _TVG_DECLARE_PRIVATE(Saver);
1534 };
1535 
1536 
1537 class TVG_EXPORT Iteratorv final
1538 {
1539 public:
1540  static std::unique_ptr<Picture> iterate(std::unique_ptr<Picture> picture, int(*func)(const Paint* paint, const Paint* parent, bool hasChildren)) noexcept;
1541 };
1542 
1543 
1546 } //namespace
1547 
1548 #ifdef __cplusplus
1549 }
1550 #endif
1551 
1552 #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:368
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:193
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:1305
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:103
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:1301
tvg::FillRule
FillRule
Enumeration specifying the algorithm used to establish which parts of the shape are treated as the in...
Definition: thorvg.h:133
tvg::CanvasEngine::Sw
@ Sw
CPU rasterizer.
tvg::Result
Result
Enumeration specifying the result from the APIs.
Definition: thorvg.h:75
tvg::SwCanvas::ARGB8888
@ ARGB8888
The channels are joined in the order: alpha, red, green, blue. Colors are alpha-premultiplied.
Definition: thorvg.h:1304
tvg::FillSpread
FillSpread
Enumeration specifying how to fill the area outside the gradient bounds.
Definition: thorvg.h:123
tvg::Result::Success
@ Success
The value returned in case of a correct request execution.
tvg::Fill::ColorStop::r
uint8_t r
Definition: thorvg.h:371
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:370
tvg::CanvasEngine
CanvasEngine
Enumeration specifying the engine type used for the graphics backend. For multiple backends bitwise o...
Definition: thorvg.h:153
tvg::SwCanvas
A class for the rendering graphical elements with a software raster engine.
Definition: thorvg.h:1293
tvg::SwCanvas::Shareable
@ Shareable
Memory Pool is shared among the SwCanvases.
Definition: thorvg.h:1316
tvg::CompositeMethod
CompositeMethod
Enumeration indicating the method used in the composition of two objects - the target and the source.
Definition: thorvg.h:142
tvg::Point
A data structure representing a point in two-dimensional space.
Definition: thorvg.h:163
tvg::Scene
A class to composite children paints.
Definition: thorvg.h:1219
tvg::PathCommand
PathCommand
Enumeration specifying the values of the path commands accepted by TVG.
Definition: thorvg.h:92
tvg::GlCanvas
A class for the rendering graphic elements with a GL raster engine.
Definition: thorvg.h:1384
tvg::Fill::ColorStop::b
uint8_t b
Definition: thorvg.h:373
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:696
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:362
tvg::Fill::ColorStop::a
uint8_t a
Definition: thorvg.h:374
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:1416
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:1306
tvg::Matrix
A data structure representing a three-dimensional matrix.
Definition: thorvg.h:176
tvg::SwCanvas::MempoolPolicy
MempoolPolicy
Enumeration specifying the methods of Memory Pool behavior policy.
Definition: thorvg.h:1313
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:113
tvg::LinearGradient
A class representing the linear gradient fill of the Shape object.
Definition: thorvg.h:564
tvg::Picture
A class representing an image read in one of the supported formats: raw, svg, png,...
Definition: thorvg.h:1077
tvg::RadialGradient
A class representing the radial gradient fill of the Shape object.
Definition: thorvg.h:631
tvg::Canvas
An abstract class for drawing graphical elements.
Definition: thorvg.h:468
tvg::Fill::ColorStop::g
uint8_t g
Definition: thorvg.h:372
tvg::Saver
A class for exporting a paint object into a specified file, from which to recover the paint data late...
Definition: thorvg.h:1478
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...