renderer: optimize path manipulation.
[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
31    public:
32       TvgRenderPath();
33       ~TvgRenderPath();
34       Shape* shape() { return m_Shape; }
35       void buildShape();
36       void reset() override;
37       void addRenderPath(RenderPath* path, const Mat2D& transform) override;
38       void fillRule(FillRule value) override;
39       void moveTo(float x, float y) override;
40       void lineTo(float x, float y) override;
41       void cubicTo(float ox, float oy, float ix, float iy, float x, float y) override;
42       virtual void close() override;
43    };
44
45    struct GradientStop
46    {
47       unsigned int color;
48       float stop;
49       GradientStop(unsigned int color, float stop) : color(color), stop(stop)
50       {
51       }
52    };
53
54    class TvgGradientBuilder
55    {
56    public:
57       std::vector<GradientStop> stops;
58       float sx, sy, ex, ey;
59       virtual ~TvgGradientBuilder() {}
60       TvgGradientBuilder(float sx, float sy, float ex, float ey) :
61           sx(sx), sy(sy), ex(ex), ey(ey)
62       {
63       }
64
65       virtual void make(TvgPaint* paint) = 0;
66    };
67
68    class TvgRadialGradientBuilder : public TvgGradientBuilder
69    {
70    public:
71       TvgRadialGradientBuilder(float sx, float sy, float ex, float ey) :
72           TvgGradientBuilder(sx, sy, ex, ey)
73       {
74       }
75       void make(TvgPaint* paint) override;
76    };
77
78    class TvgLinearGradientBuilder : public TvgGradientBuilder
79    {
80    public:
81       TvgLinearGradientBuilder(float sx, float sy, float ex, float ey) :
82           TvgGradientBuilder(sx, sy, ex, ey)
83       {
84       }
85       void make(TvgPaint* paint) override;
86    };
87
88    class TvgRenderPaint : public RenderPaint
89    {
90    private:
91       TvgPaint m_Paint;
92       TvgGradientBuilder* m_GradientBuilder;
93
94    public:
95       TvgPaint* paint() { return &m_Paint; }
96       TvgRenderPaint();
97       void style(RenderPaintStyle style) override;
98       void color(unsigned int value) override;
99       void thickness(float value) override;
100       void join(StrokeJoin value) override;
101       void cap(StrokeCap value) override;
102       void blendMode(BlendMode value) override;
103
104       void linearGradient(float sx, float sy, float ex, float ey) override;
105       void radialGradient(float sx, float sy, float ex, float ey) override;
106       void addStop(unsigned int color, float stop) override;
107       void completeGradient() override;
108    };
109
110    class TvgRenderer : public Renderer
111    {
112    private:
113       Canvas* m_Canvas;
114       Shape* m_ClipPath;
115       Mat2D m_Transform;
116       Mat2D m_SaveTransform;
117
118    public:
119       TvgRenderer(Canvas* canvas) : m_Canvas(canvas) {}
120       void save() override;
121       void restore() override;
122       void transform(const Mat2D& transform) override;
123       void drawPath(RenderPath* path, RenderPaint* paint) override;
124       void clipPath(RenderPath* path) override;
125    };
126 } // namespace rive
127 #endif