9b01facf3516fa1292e6ea7ea058af8df9bac406
[platform/core/uifw/rive-tizen.git] / src / renderer / tvg_renderer.hpp
1 #ifndef _RIVE_THORVG_RENDERER_HPP_
2 #define _RIVE_THORVG_RENDERER_HPP_
3
4 #include <thorvg.h>
5 #include <vector>
6 #include "renderer.hpp"
7
8 using namespace tvg;
9 using namespace std;
10
11 namespace rive
12 {
13    struct TvgPaint
14    {
15       uint8_t color[4];
16       float thickness;
17       tvg::Fill *gradientFill;
18       tvg::StrokeJoin join = tvg::StrokeJoin::Bevel;
19       tvg::StrokeCap  cap = tvg::StrokeCap::Butt;
20       RenderPaintStyle style;
21       bool isGradient;
22       bool gradientApplied;
23       TvgPaint() : isGradient(false), gradientApplied(false) {}
24    };
25
26    class TvgRenderPath : public RenderPath
27    {
28    private:
29       Shape *m_Shape;
30       vector<PathCommand> m_PathType;
31       vector<Vec2D> m_PathPoints;
32
33    public:
34       TvgRenderPath();
35       ~TvgRenderPath();
36       Shape* shape() { return m_Shape; }
37       void buildShape();
38       void reset() override;
39       void addRenderPath(RenderPath* path, const Mat2D& transform) override;
40       void fillRule(FillRule value) override;
41       void moveTo(float x, float y) override;
42       void lineTo(float x, float y) override;
43       void cubicTo(float ox, float oy, float ix, float iy, float x, float y) override;
44       virtual void close() override;
45    };
46
47    struct GradientStop
48    {
49       unsigned int color;
50       float stop;
51       GradientStop(unsigned int color, float stop) : color(color), stop(stop)
52       {
53       }
54    };
55
56    class TvgGradientBuilder
57    {
58    public:
59       std::vector<GradientStop> stops;
60       float sx, sy, ex, ey;
61       virtual ~TvgGradientBuilder() {}
62       TvgGradientBuilder(float sx, float sy, float ex, float ey) :
63           sx(sx), sy(sy), ex(ex), ey(ey)
64       {
65       }
66
67       virtual void make(TvgPaint* paint) = 0;
68    };
69
70    class TvgRadialGradientBuilder : public TvgGradientBuilder
71    {
72    public:
73       TvgRadialGradientBuilder(float sx, float sy, float ex, float ey) :
74           TvgGradientBuilder(sx, sy, ex, ey)
75       {
76       }
77       void make(TvgPaint* paint) override;
78    };
79
80    class TvgLinearGradientBuilder : public TvgGradientBuilder
81    {
82    public:
83       TvgLinearGradientBuilder(float sx, float sy, float ex, float ey) :
84           TvgGradientBuilder(sx, sy, ex, ey)
85       {
86       }
87       void make(TvgPaint* paint) override;
88    };
89
90    class TvgRenderPaint : public RenderPaint
91    {
92    private:
93       TvgPaint m_Paint;
94       TvgGradientBuilder* m_GradientBuilder;
95
96    public:
97       TvgPaint* paint() { return &m_Paint; }
98       TvgRenderPaint();
99       void style(RenderPaintStyle style) override;
100       void color(unsigned int value) override;
101       void thickness(float value) override;
102       void join(StrokeJoin value) override;
103       void cap(StrokeCap value) override;
104       void blendMode(BlendMode value) override;
105
106       void linearGradient(float sx, float sy, float ex, float ey) override;
107       void radialGradient(float sx, float sy, float ex, float ey) override;
108       void addStop(unsigned int color, float stop) override;
109       void completeGradient() override;
110    };
111
112    class TvgRenderer : public Renderer
113    {
114    private:
115       Canvas* m_Canvas;
116       Shape* m_ClipPath;
117       Mat2D m_Transform;
118       Mat2D m_SaveTransform;
119
120    public:
121       TvgRenderer(Canvas* canvas) : m_Canvas(canvas) {}
122       void save() override;
123       void restore() override;
124       void transform(const Mat2D& transform) override;
125       void drawPath(RenderPath* path, RenderPaint* paint) override;
126       void clipPath(RenderPath* path) override;
127    };
128 } // namespace rive
129 #endif