Implements stroke feature for shape pathes
authorTaehyub Kim <taehyub.kim@samsung.com>
Thu, 11 Mar 2021 02:24:01 +0000 (11:24 +0900)
committerHermet Park <hermetpark@gmail.com>
Thu, 11 Mar 2021 02:33:08 +0000 (11:33 +0900)
example/shapes.riv
src/renderer/include/thorvg_renderer.hpp
src/renderer/src/thorvg_renderer.cpp

index 091e2e4..73e0d73 100644 (file)
Binary files a/example/shapes.riv and b/example/shapes.riv differ
index fbb19b4..0c14756 100644 (file)
@@ -23,9 +23,13 @@ namespace rive
 
    struct ThorvgPaint
    {
-      int r, g, b, a;
+      int fillColor[4];
+      int strokeColor[4];
       float thickness;
-      ThorvgPaint() : r(0), g(0), b(0), a(0), thickness(0.0) {}
+      RenderPaintStyle style;
+      bool isFill;
+      bool isStroke;
+      ThorvgPaint() : isFill(false), isStroke(false) {}
    };
 
    class ThorvgRenderPath : public RenderPath
@@ -34,11 +38,13 @@ namespace rive
       tvg::Shape *m_Path;
       std::vector<ThorvgPathType> m_PathType;
       std::vector<ThorvgPoint> m_PathPoints;
-      bool pushed;
+      bool m_Pushed;
 
    public:
       ThorvgRenderPath();
       tvg::Shape* path() { return m_Path; }
+      bool getPushed() { return m_Pushed; }
+      bool setPushed(bool pushed) { m_Pushed = pushed; }
       void reset() override;
       void addRenderPath(RenderPath* path, const Mat2D& transform) override;
       void fillRule(FillRule value) override;
index a97c846..0fc8d6d 100644 (file)
@@ -4,7 +4,7 @@
 
 using namespace rive;
 
-ThorvgRenderPath::ThorvgRenderPath() : pushed(false)
+ThorvgRenderPath::ThorvgRenderPath() : m_Pushed(false)
 {
        this->m_Path = tvg::Shape::gen().release();
 }
@@ -103,14 +103,29 @@ void ThorvgRenderer::drawPath(RenderPath* path, RenderPaint* paint)
                     0, m_Transform[3], m_Transform[5],
                     0, 0, 1};
 
+   ThorvgRenderPath *renderPath = reinterpret_cast<ThorvgRenderPath*>(path);
    tvg::Shape *drawPath = reinterpret_cast<ThorvgRenderPath*>(path)->path();
-       drawPath->transform(m);
+   drawPath->transform(m);
 
    ThorvgPaint *drawPaint = reinterpret_cast<ThorvgRenderPaint*>(paint)->paint();
-   drawPath->fill(drawPaint->r, drawPaint->g, drawPaint->b, drawPaint->a);
-   drawPath->stroke(drawPaint->thickness);
+   if (drawPaint->isFill)
+     drawPath->fill(drawPaint->fillColor[0], drawPaint->fillColor[1], drawPaint->fillColor[2], drawPaint->fillColor[3]);
+   if (drawPaint->isStroke)
+   {
+     drawPath->stroke(drawPaint->strokeColor[0], drawPaint->strokeColor[1], drawPaint->strokeColor[2], drawPaint->strokeColor[3]);
+     if (drawPaint->thickness != 0)
+       drawPath->stroke(drawPaint->thickness);
+   }
 
-   m_Canvas->push(std::unique_ptr<tvg::Shape>(drawPath));
+   if (!renderPath->getPushed())
+   {
+      tvg::Result ret = m_Canvas->push(std::unique_ptr<tvg::Shape>(drawPath));
+      renderPath->setPushed(true);
+   }
+   else
+   {
+      tvg::Result ret = m_Canvas->update(drawPath);
+   }
 }
 
 void ThorvgRenderer::clipPath(RenderPath* path)
@@ -128,17 +143,36 @@ void ThorvgRenderPaint::style(RenderPaintStyle style)
        switch (style)
        {
                case RenderPaintStyle::fill:
+         m_Paint.style = RenderPaintStyle::fill;
+         m_Paint.isFill = true;
                        break;
                case RenderPaintStyle::stroke:
+         m_Paint.style = RenderPaintStyle::stroke;
+         m_Paint.isStroke = true;
                        break;
        }
 }
 void ThorvgRenderPaint::color(unsigned int value)
 {
-   m_Paint.r = value >> 0 & 255;
-   m_Paint.g = value >> 8 & 255;
-   m_Paint.b = value >> 16 & 255;
-   m_Paint.a = value >> 24 & 255;
+   int b = value >> 0 & 255;
+   int g = value >> 8 & 255;
+   int r = value >> 16 & 255;
+   int a = value >> 24 & 255;
+
+   if (m_Paint.style == RenderPaintStyle::fill)
+   {
+      m_Paint.fillColor[0] = r;
+      m_Paint.fillColor[1] = g;
+      m_Paint.fillColor[2] = b;
+      m_Paint.fillColor[3] = a;
+   }
+   else if (m_Paint.style == RenderPaintStyle::stroke)
+   {
+      m_Paint.strokeColor[0] = r;
+      m_Paint.strokeColor[1] = g;
+      m_Paint.strokeColor[2] = b;
+      m_Paint.strokeColor[3] = a;
+   }
 }
 
 void ThorvgRenderPaint::thickness(float value)