common fill: implement duplicate() method.
authorHermet Park <hermetpark@gmail.com>
Mon, 21 Sep 2020 10:20:51 +0000 (19:20 +0900)
committerHermet Park <chuneon.park@samsung.com>
Mon, 21 Sep 2020 10:31:04 +0000 (19:31 +0900)
Change-Id: Ia47a13d4a124d91f84bd5ed755b831ea5c35e3ff

15 files changed:
inc/thorvg.h
src/bindings/capi/tvgCapi.cpp
src/lib/meson.build
src/lib/tvgCanvasImpl.h
src/lib/tvgCommon.h
src/lib/tvgFill.cpp
src/lib/tvgFill.h [new file with mode: 0644]
src/lib/tvgLinearGradient.cpp
src/lib/tvgPaint.cpp
src/lib/tvgPaint.h
src/lib/tvgPicture.cpp
src/lib/tvgPictureImpl.h
src/lib/tvgRadialGradient.cpp
src/lib/tvgSceneImpl.h
src/lib/tvgShapeImpl.h

index 7d5062c07af7cbf6c226b37243ab0473ae06b0f2..e4f45a7c71ae654d0fc801a6135fbb0e3bc1dd20 100644 (file)
@@ -91,7 +91,7 @@ public:
     Result translate(float x, float y) noexcept;
     Result transform(const Matrix& m) noexcept;
     Result bounds(float* x, float* y, float* w, float* h) const noexcept;
-    std::unique_ptr<Paint> duplicate() const noexcept;
+    Paint* duplicate() const noexcept;
 
     _TVG_DECLARE_ACCESSOR();
     _TVG_DECLARE_PRIVATE(Paint);
@@ -122,6 +122,7 @@ public:
 
     uint32_t colorStops(const ColorStop** colorStops) const noexcept;
     FillSpread spread() const noexcept;
+    std::unique_ptr<Fill> duplicate() const noexcept;
 
     _TVG_DECALRE_IDENTIFIER();
     _TVG_DECLARE_PRIVATE(Fill);
index 03ee907325bc5a2e371796bc448f30159cda2190..b58f0ff17d193075cd8eb8f6f4fee4cb8b116962 100644 (file)
@@ -166,7 +166,7 @@ TVG_EXPORT Tvg_Result tvg_paint_transform(Tvg_Paint* paint, const Tvg_Matrix* m)
 TVG_EXPORT Tvg_Paint* tvg_paint_duplicate(Tvg_Paint* paint)
 {
     if (!paint) return NULL;
-    return (Tvg_Paint*) reinterpret_cast<Paint*>(paint)->duplicate().release();
+    return (Tvg_Paint*) reinterpret_cast<Paint*>(paint)->duplicate();
 }
 
 
index bc0d959280ba2fb2ae4ac5dc18fd43536daa86af..0b1b90aa2ca37206dc4ae7101ffb657573148e09 100644 (file)
@@ -14,6 +14,7 @@ source_file = [
    'tvgCanvasImpl.h',
    'tvgCommon.h',
    'tvgBezier.h',
+   'tvgFill.h',
    'tvgLoader.h',
    'tvgLoaderMgr.h',
    'tvgPictureImpl.h',
index ebac74db5cffa98cc50399050e77edb2f160dc00..81312849bbfe8334806e04b6d2eb33ea07d1f696 100644 (file)
@@ -22,7 +22,7 @@
 #ifndef _TVG_CANVAS_IMPL_H_
 #define _TVG_CANVAS_IMPL_H_
 
-#include "tvgCommon.h"
+#include "tvgPaint.h"
 
 /************************************************************************/
 /* Internal Class Implementation                                        */
index 3ed4544d636465e6525bad4a9aad7b0fbf69ee2c..ecacc828659291b80e5ccbf81172b0d07460dc72 100644 (file)
@@ -46,7 +46,6 @@ using namespace tvg;
 #include "tvgLoader.h"
 #include "tvgLoaderMgr.h"
 #include "tvgRender.h"
-#include "tvgPaint.h"
 #include "tvgTaskScheduler.h"
 
 #endif //_TVG_COMMON_H_
index 4594e846b5ab1de83085de706f0d4528d54463f5..c9d3f1c72361cb31f16d0c1c45285aa2370bdc57 100644 (file)
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-#include "tvgCommon.h"
-
+#include "tvgFill.h"
 
 /************************************************************************/
 /* Internal Class Implementation                                        */
 /************************************************************************/
 
-struct Fill::Impl
-{
-    ColorStop* colorStops = nullptr;
-    uint32_t cnt = 0;
-    FillSpread spread;
-
-    ~Impl()
-    {
-        if (colorStops) free(colorStops);
-    }
-};
-
 
 /************************************************************************/
 /* External Class Implementation                                        */
@@ -95,4 +82,10 @@ Result Fill::spread(FillSpread s) noexcept
 FillSpread Fill::spread() const noexcept
 {
     return pImpl->spread;
-}
\ No newline at end of file
+}
+
+
+unique_ptr<Fill> Fill::duplicate() const noexcept
+{
+    return pImpl->duplicate();
+}
diff --git a/src/lib/tvgFill.h b/src/lib/tvgFill.h
new file mode 100644 (file)
index 0000000..07af49c
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
+
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#ifndef _TVG_FILL_H_
+#define _TVG_FILL_H_
+
+#include "tvgCommon.h"
+
+template<typename T>
+struct DuplicateMethod
+{
+    virtual ~DuplicateMethod(){}
+    virtual unique_ptr<T> duplicate() = 0;
+};
+
+template<class T>
+struct FillDup : DuplicateMethod<Fill>
+{
+    T* inst = nullptr;
+
+    FillDup(T* _inst) : inst(_inst) {}
+    ~FillDup(){}
+
+    unique_ptr<Fill> duplicate() override
+    {
+        return inst->duplicate();
+    }
+};
+
+struct Fill::Impl
+{
+    ColorStop* colorStops = nullptr;
+    uint32_t cnt = 0;
+    FillSpread spread;
+    DuplicateMethod<Fill>* dup = nullptr;
+
+    ~Impl()
+    {
+        if (dup) delete(dup);
+        if (colorStops) free(colorStops);
+    }
+
+    void method(DuplicateMethod<Fill>* dup)
+    {
+        this->dup = dup;
+    }
+
+    unique_ptr<Fill> duplicate()
+    {
+        auto ret = dup->duplicate();
+        if (!ret) return nullptr;
+
+        ret->pImpl->cnt = cnt;
+        ret->pImpl->spread = spread;
+        ret->pImpl->colorStops = static_cast<ColorStop*>(malloc(sizeof(ColorStop) * cnt));
+        memcpy(ret->pImpl->colorStops, colorStops, sizeof(ColorStop) * cnt);
+
+        return ret;
+    }    
+};
+
+#endif  //_TVG_FILL_H_
\ No newline at end of file
index 1d67c17d632824c08fea8377e2ecd8709cc260e1..5258b335ddc4aa44ddb7dd4fa0ac5945d8d731e4 100644 (file)
@@ -19,7 +19,7 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-#include "tvgCommon.h"
+#include "tvgFill.h"
 
 /************************************************************************/
 /* Internal Class Implementation                                        */
@@ -31,6 +31,19 @@ struct LinearGradient::Impl
     float y1 = 0;
     float x2 = 0;
     float y2 = 0;
+
+    unique_ptr<Fill> duplicate()
+    {
+        auto ret = LinearGradient::gen();
+        if (!ret) return nullptr;
+
+        ret->pImpl->x1 = x1;
+        ret->pImpl->y1 = y1;
+        ret->pImpl->x2 = x2;
+        ret->pImpl->y2 = y2;
+
+        return ret;
+    }
 };
 
 /************************************************************************/
@@ -40,6 +53,7 @@ struct LinearGradient::Impl
 LinearGradient::LinearGradient():pImpl(new Impl())
 {
     _id = FILL_ID_LINEAR;
+    Fill::pImpl->method(new FillDup<LinearGradient::Impl>(pImpl));
 }
 
 
index 55e0d754233a2944a23dbd1bc6514cdd2808090d..7c490babb6517c28bf8c9cedbe68ef21604a9474 100644 (file)
@@ -19,7 +19,7 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-#include "tvgCommon.h"
+#include "tvgPaint.h"
 
 /************************************************************************/
 /* Internal Class Implementation                                        */
@@ -74,7 +74,7 @@ Result Paint::bounds(float* x, float* y, float* w, float* h) const noexcept
     return Result::InsufficientCondition;
 }
 
-unique_ptr<Paint> Paint::duplicate() const noexcept
+Paint* Paint::duplicate() const noexcept
 {
     return pImpl->duplicate();
 }
\ No newline at end of file
index 0fd3a7997d712b2b20270f5f918508a08a2b1dda..63c084991900456cb888c20190b5eaae578be484 100644 (file)
@@ -22,6 +22,8 @@
 #ifndef _TVG_PAINT_H_
 #define _TVG_PAINT_H_
 
+#include "tvgCommon.h"
+
 namespace tvg
 {
     struct StrategyMethod
@@ -146,9 +148,9 @@ namespace tvg
             return smethod->render(renderer);
         }
 
-        unique_ptr<Paint> duplicate()
+        Paint* duplicate()
         {
-            return smethod->duplicate();
+            return smethod->duplicate().release();
         }
     };
 
index a4ac3ee56f1af198cba4595d59b03f2e521484c6..0860ff477d0dea34d8ef6cf7e4184d10367ea201 100644 (file)
@@ -19,6 +19,7 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
+
 #include "tvgPictureImpl.h"
 
 /************************************************************************/
index e96384d7a21df27207cdf545e720e30fda83fd36..b1e4fb19a112d5166a9a262cb854f24876d793de 100644 (file)
@@ -22,7 +22,7 @@
 #ifndef _TVG_PICTURE_IMPL_H_
 #define _TVG_PICTURE_IMPL_H_
 
-#include "tvgCommon.h"
+#include "tvgPaint.h"
 
 /************************************************************************/
 /* Internal Class Implementation                                        */
index d91ebba314794e1ac39b3309db075756b23d0f3b..d02d2e081af09b3d25a03234e7d9dbb53fdb3dce 100644 (file)
@@ -19,7 +19,7 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-#include "tvgCommon.h"
+#include "tvgFill.h"
 
 /************************************************************************/
 /* Internal Class Implementation                                        */
@@ -30,6 +30,18 @@ struct RadialGradient::Impl
     float cx = 0;
     float cy = 0;
     float radius = 0;
+
+    unique_ptr<Fill> duplicate()
+    {
+        auto ret = RadialGradient::gen();
+        if (!ret) return nullptr;
+
+        ret->pImpl->cx = cx;
+        ret->pImpl->cy = cy;
+        ret->pImpl->radius = radius;
+
+        return ret;
+    }
 };
 
 
@@ -40,6 +52,7 @@ struct RadialGradient::Impl
 RadialGradient::RadialGradient():pImpl(new Impl())
 {
     _id = FILL_ID_RADIAL;
+    Fill::pImpl->method(new FillDup<RadialGradient::Impl>(pImpl));
 }
 
 
index b1b289810ab4d6ae219297a7e2debbbd016809d3..00f2ccce1334cb44ddf93d5c21335e69daaf722a 100644 (file)
@@ -22,7 +22,7 @@
 #ifndef _TVG_SCENE_IMPL_H_
 #define _TVG_SCENE_IMPL_H_
 
-#include "tvgCommon.h"
+#include "tvgPaint.h"
 
 /************************************************************************/
 /* Internal Class Implementation                                        */
index d9481a272a2eb5cfc3868b744470474bcd96d875..5186003e60f1ab6291e40f000d090b8534d33c65 100644 (file)
@@ -22,7 +22,7 @@
 #ifndef _TVG_SHAPE_IMPL_H_
 #define _TVG_SHAPE_IMPL_H_
 
-#include "tvgCommon.h"
+#include "tvgPaint.h"
 #include "tvgShapePath.h"
 
 /************************************************************************/
@@ -199,7 +199,10 @@ struct Shape::Impl
             dup->flag |= RenderUpdateFlag::Stroke;
         }
 
-        //TODO: Fill
+        if (fill) {
+            dup->fill = fill->duplicate().release();
+            dup->flag |= RenderUpdateFlag::Gradient;
+        }
 
         return ret;
     }