| ThorVG v0.4

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 SaveModule; \
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 SaveModule;
66 class Scene;
67 class Picture;
68 class Canvas;
69
80 enum class TVG_EXPORT Result
81 {
82 Success = 0,
83 InvalidArguments,
84 InsufficientCondition,
85 FailedAllocation,
86 MemoryCorruption,
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
293 Result bounds(float* x, float* y, float* w, float* h) const noexcept;
294
302 Paint* duplicate() const noexcept;
303
309 uint8_t opacity() const noexcept;
310
320 CompositeMethod composite(const Paint** target) const noexcept;
321
322 _TVG_DECLARE_ACCESSOR();
323 _TVG_DECALRE_IDENTIFIER();
324 _TVG_DECLARE_PRIVATE(Paint);
325 };
326
327
339 class TVG_EXPORT Fill
340 {
341 public:
345 struct ColorStop
346 {
347 float offset;
348 uint8_t r;
349 uint8_t g;
350 uint8_t b;
351 uint8_t a;
352 };
353
354 virtual ~Fill();
355
364 Result colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept;
365
373 Result spread(FillSpread s) noexcept;
374
382 uint32_t colorStops(const ColorStop** colorStops) const noexcept;
383
389 FillSpread spread() const noexcept;
390
398 Fill* duplicate() const noexcept;
399
400 _TVG_DECALRE_IDENTIFIER();
401 _TVG_DECLARE_PRIVATE(Fill);
402 };
403
404
415 class TVG_EXPORT Canvas
416 {
417 public:
418 Canvas(RenderMethod*);
419 virtual ~Canvas();
420
431 Result reserve(uint32_t n) noexcept;
432
450 virtual Result push(std::unique_ptr<Paint> paint) noexcept;
451
462 virtual Result clear(bool free = true) noexcept;
463
476 virtual Result update(Paint* paint) noexcept;
477
486 virtual Result draw() noexcept;
487
497 virtual Result sync() noexcept;
498
499 _TVG_DECLARE_PRIVATE(Canvas);
500 };
501
502
511 class TVG_EXPORT LinearGradient final : public Fill
512 {
513 public:
514 ~LinearGradient();
515
530 Result linear(float x1, float y1, float x2, float y2) noexcept;
531
546 Result linear(float* x1, float* y1, float* x2, float* y2) const noexcept;
547
553 static std::unique_ptr<LinearGradient> gen() noexcept;
554
555 _TVG_DECLARE_PRIVATE(LinearGradient);
556 };
557
558
565 class TVG_EXPORT RadialGradient final : public Fill
566 {
567 public:
568 ~RadialGradient();
569
581 Result radial(float cx, float cy, float radius) noexcept;
582
594 Result radial(float* cx, float* cy, float* radius) const noexcept;
595
601 static std::unique_ptr<RadialGradient> gen() noexcept;
602
603 _TVG_DECLARE_PRIVATE(RadialGradient);
604 };
605
606
619 class TVG_EXPORT Shape final : public Paint
620 {
621 public:
622 ~Shape();
623
633 Result reset() noexcept;
634
645 Result moveTo(float x, float y) noexcept;
646
659 Result lineTo(float x, float y) noexcept;
660
678 Result cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept;
679
689 Result close() noexcept;
690
715 Result appendRect(float x, float y, float w, float h, float rx, float ry) noexcept;
716
733 Result appendCircle(float cx, float cy, float rx, float ry) noexcept;
734
752 Result appendArc(float cx, float cy, float radius, float startAngle, float sweep, bool pie) noexcept;
753
769 Result appendPath(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept;
770
778 Result stroke(float width) noexcept;
779
790 Result stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
791
801 Result stroke(std::unique_ptr<Fill> f) noexcept;
802
817 Result stroke(const float* dashPattern, uint32_t cnt) noexcept;
818
826 Result stroke(StrokeCap cap) noexcept;
827
837 Result stroke(StrokeJoin join) noexcept;
838
853 Result fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
854
866 Result fill(std::unique_ptr<Fill> f) noexcept;
867
875 Result fill(FillRule r) noexcept;
876
884 uint32_t pathCommands(const PathCommand** cmds) const noexcept;
885
893 uint32_t pathCoords(const Point** pts) const noexcept;
894
900 const Fill* fill() const noexcept;
901
912 Result fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
913
919 FillRule fillRule() const noexcept;
920
926 float strokeWidth() const noexcept;
927
938 Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
939
945 const Fill* strokeFill() const noexcept;
946
954 uint32_t strokeDash(const float** dashPattern) const noexcept;
955
961 StrokeCap strokeCap() const noexcept;
962
968 StrokeJoin strokeJoin() const noexcept;
969
975 static std::unique_ptr<Shape> gen() noexcept;
976
977 _TVG_DECLARE_PRIVATE(Shape);
978 };
979
980
989 class TVG_EXPORT Picture final : public Paint
990 {
991 public:
992 ~Picture();
993
1007 Result load(const std::string& path) noexcept;
1008
1025 TVG_DEPRECATED Result load(const char* data, uint32_t size, bool copy = false) noexcept;
1026
1044 Result load(const char* data, uint32_t size, const std::string& mimeType, bool copy = false) noexcept;
1045
1057 Result size(float w, float h) noexcept;
1058
1067 Result size(float* w, float* h) const noexcept;
1068
1076 const uint32_t* data(uint32_t* w, uint32_t* h) const noexcept;
1077
1082 Result paint(std::unique_ptr<Paint> paint) noexcept;
1083
1091 Result load(uint32_t* data, uint32_t w, uint32_t h, bool copy) noexcept;
1092
1100 Result viewbox(float* x, float* y, float* w, float* h) const noexcept;
1101
1107 static std::unique_ptr<Picture> gen() noexcept;
1108
1109 _TVG_DECLARE_PRIVATE(Picture);
1110 };
1111
1112
1124 class TVG_EXPORT Scene final : public Paint
1125 {
1126 public:
1127 ~Scene();
1128
1143 Result push(std::unique_ptr<Paint> paint) noexcept;
1144
1155 Result reserve(uint32_t size) noexcept;
1156
1170 Result clear(bool free = true) noexcept;
1171
1177 static std::unique_ptr<Scene> gen() noexcept;
1178
1179 _TVG_DECLARE_PRIVATE(Scene);
1180 };
1181
1182
1188 class TVG_EXPORT SwCanvas final : public Canvas
1189 {
1190 public:
1191 ~SwCanvas();
1192
1196 enum Colorspace
1197 {
1198 ABGR8888 = 0,
1199 ARGB8888
1200 };
1201
1206 enum MempoolPolicy
1207 {
1208 Default = 0,
1209 Shareable,
1210 Individual
1211 };
1212
1231 Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, Colorspace cs) noexcept;
1232
1256 Result mempool(MempoolPolicy policy) noexcept;
1257
1262 static std::unique_ptr<SwCanvas> gen() noexcept;
1263
1264 _TVG_DECLARE_PRIVATE(SwCanvas);
1265 };
1266
1267
1277 class TVG_EXPORT GlCanvas final : public Canvas
1278 {
1279 public:
1280 ~GlCanvas();
1281
1289 Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h) noexcept;
1290
1298 static std::unique_ptr<GlCanvas> gen() noexcept;
1299
1300 _TVG_DECLARE_PRIVATE(GlCanvas);
1301 };
1302
1303
1309 class TVG_EXPORT Initializer final
1310 {
1311 public:
1332 static Result init(CanvasEngine engine, uint32_t threads) noexcept;
1333
1348 static Result term(CanvasEngine engine) noexcept;
1349
1350 _TVG_DISABLE_CTOR(Initializer);
1351 };
1352
1353
1371 class TVG_EXPORT Saver
1372 {
1373 public:
1374 ~Saver();
1375
1391 Result save(std::unique_ptr<Paint> paint, const std::string& path) noexcept;
1392
1408 Result sync() noexcept;
1409
1417 static std::unique_ptr<Saver> gen() noexcept;
1418
1419 _TVG_DECLARE_PRIVATE(Saver);
1420 };
1421
1424 } //namespace
1425
1426 #ifdef __cplusplus
1427 }
1428 #endif
1429
1430 #endif //_THORVG_H_
tvg::CompositeMethod::AlphaMask
The pixels of the source and the target are alpha blended. As a result, only the part of the source...
tvg::PathCommand::LineTo
Draws a line from the current point to the given point and sets a new value of the current point...
tvg::StrokeCap::Round
The stroke is extended in both end-points of a sub-path by a half circle, with a radius equal to the ...
tvg::Result::Unknown
The value returned in all other cases.
tvg::Result::FailedAllocation
The value returned in case of unsuccessful memory allocation.
tvg::SwCanvas::MempoolPolicy
MempoolPolicy
Enumeration specifying the methods of Memory Pool behavior policy.
Definition: thorvg.h:1206
tvg::PathCommand::Close
Ends the current sub-path and connects it with its initial point. This command doesn&#39;t expect any poi...
tvg::Saver
A class for exporting a paint object into a specified file, from which to recover the paint data late...
Definition: thorvg.h:1371
tvg::Fill
An abstract class representing the gradient fill of the Shape object.
Definition: thorvg.h:339
tvg::Fill::ColorStop::r
uint8_t r
Definition: thorvg.h:348
tvg::PathCommand::MoveTo
Sets a new initial point of the sub-path and a new current point. This command expects 1 point: the s...
tvg::Result::InsufficientCondition
The value returned in case the request cannot be processed - e.g. asking for properties of an object...
tvg::StrokeCap
StrokeCap
Enumeration determining the ending type of a stroke in the open sub-paths.
Definition: thorvg.h:108
tvg::CompositeMethod::ClipPath
The intersection of the source and the target is determined and only the resulting pixels from the so...
tvg::RadialGradient
A class representing the radial gradient fill of the Shape object.
Definition: thorvg.h:565
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::Scene
A class to composite children paints.
Definition: thorvg.h:1124
tvg::Result::MemoryCorruption
The value returned in the event of bad memory handling - e.g. failing in pointer releasing or casting...
tvg::FillSpread::Reflect
The gradient pattern is reflected outside the gradient area until the expected region is filled...
tvg::FillSpread
FillSpread
Enumeration specifying how to fill the area outside the gradient bounds.
Definition: thorvg.h:128
tvg::Fill::ColorStop::a
uint8_t a
Definition: thorvg.h:351
tvg::Point
A data structure representing a point in two-dimensional space.
Definition: thorvg.h:168
tvg::Shape
A class representing two-dimensional figures and their properties.
Definition: thorvg.h:619
tvg::SwCanvas::Shareable
Memory Pool is shared among the SwCanvases.
Definition: thorvg.h:1209
tvg::CompositeMethod::None
No composition is applied.
tvg::FillRule::Winding
A line from the point to a location outside the shape is drawn. The intersections of the line with th...
tvg::CompositeMethod::InvAlphaMask
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.
tvg::StrokeJoin
StrokeJoin
Enumeration determining the style used at the corners of joined stroked path segments.
Definition: thorvg.h:118
tvg::CanvasEngine::Gl
OpenGL rasterizer.
tvg
Definition: thorvg.h:61
tvg::Picture
A class representing an image read in one of the supported formats: raw, svg, png and etc...
Definition: thorvg.h:989
tvg::FillSpread::Pad
The remaining area is filled with the closest stop color.
tvg::CanvasEngine::Sw
CPU rasterizer.
tvg::PathCommand
PathCommand
Enumeration specifying the values of the path commands accepted by TVG.
Definition: thorvg.h:97
tvg::Result::InvalidArguments
The value returned in the event of a problem with the arguments given to the API - e...
tvg::Result::Success
The value returned in case of a correct request execution.
tvg::SwCanvas
A class for the rendering graphical elements with a software raster engine.
Definition: thorvg.h:1188
tvg::PathCommand::CubicTo
Draws a cubic Bezier curve from the current point to the given point using two given control points a...
tvg::StrokeCap::Butt
The stroke ends exactly at each of the two end-points of a sub-path. For zero length sub-paths no str...
tvg::FillSpread::Repeat
The gradient pattern is repeated continuously beyond the gradient area until the expected region is f...
tvg::SwCanvas::Colorspace
Colorspace
Enumeration specifying the methods of combining the 8-bit color channels into 32-bit color...
Definition: thorvg.h:1196
tvg::GlCanvas
A class for the rendering graphic elements with a GL raster engine.
Definition: thorvg.h:1277
tvg::StrokeJoin::Bevel
The outer corner of the joined path segments is bevelled at the join point. The triangular region of ...
tvg::StrokeCap::Square
The stroke is extended in both end-points of a sub-path by a rectangle, with the width equal to the s...
tvg::Fill::ColorStop
A data structure storing the information about the color and its relative position inside the gradien...
Definition: thorvg.h:345
tvg::Fill::ColorStop::b
uint8_t b
Definition: thorvg.h:350
tvg::Result
Result
Enumeration specifying the result from the APIs.
Definition: thorvg.h:80
tvg::CompositeMethod
CompositeMethod
Enumeration indicating the method used in the composition of two objects - the target and the source...
Definition: thorvg.h:147
tvg::Paint
An abstract class for managing graphical elements.
Definition: thorvg.h:198
tvg::Result::NonSupport
The value returned in case of choosing unsupported options.
tvg::Canvas
An abstract class for drawing graphical elements.
Definition: thorvg.h:415
tvg::Fill::ColorStop::offset
float offset
Definition: thorvg.h:347
tvg::Fill::ColorStop::g
uint8_t g
Definition: thorvg.h:349
tvg::FillRule::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::CanvasEngine
CanvasEngine
Enumeration specifying the engine type used for the graphics backend. For multiple backeneds bitwise ...
Definition: thorvg.h:158
tvg::StrokeJoin::Miter
The outer corner of the joined path segments is spiked. The spike is created by extension beyond the ...
tvg::LinearGradient
A class representing the linear gradient fill of the Shape object.
Definition: thorvg.h:511
tvg::Initializer
A class that enables initialization and termination of the TVG engines.
Definition: thorvg.h:1309