common: revise transform interfaces. 99/239699/4
authorHermet Park <chuneon.park@samsung.com>
Wed, 29 Jul 2020 05:25:18 +0000 (14:25 +0900)
committerHermet Park <chuneon.park@samsung.com>
Wed, 29 Jul 2020 07:52:40 +0000 (16:52 +0900)
transform interfaces are getting duplicated in derived classes.

we moved to the super for smaller apis count.

Applied strategy pattern to hide the inheritance.

Change-Id: I7b0c3ff9317e9bf3c97bb0c849bf55e79ee9a591

inc/thorvg.h
src/lib/meson.build
src/lib/tvgCommon.h
src/lib/tvgPaint.cpp [new file with mode: 0644]
src/lib/tvgPaint.h [new file with mode: 0644]
src/lib/tvgScene.cpp
src/lib/tvgSceneImpl.h
src/lib/tvgShape.cpp
src/lib/tvgShapeImpl.h

index bdca5a2..c8dafaf 100644 (file)
@@ -97,9 +97,16 @@ struct Matrix
 class TVG_EXPORT Paint
 {
 public:
-    virtual ~Paint() {}
+    virtual ~Paint();
+
+    Result rotate(float degree) noexcept;
+    Result scale(float factor) noexcept;
+    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;
 
     _TVG_DECALRE_IDENTIFIER();
+    _TVG_DECLARE_PRIVATE(Paint);
 };
 
 
@@ -243,18 +250,11 @@ public:
     Result fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
     Result fill(std::unique_ptr<Fill> f) noexcept;
 
-    //Transform
-    Result rotate(float degree) noexcept;
-    Result scale(float factor) noexcept;
-    Result translate(float x, float y) noexcept;
-    Result transform(const Matrix& m) noexcept;
-
     //Getters
     uint32_t pathCommands(const PathCommand** cmds) const noexcept;
     uint32_t pathCoords(const Point** pts) const noexcept;
     Result fill(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
     const Fill* fill() const noexcept;
-    Result bounds(float* x, float* y, float* w, float* h) const noexcept;
 
     float strokeWidth() const noexcept;
     Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
@@ -287,13 +287,6 @@ public:
     Result reserve(uint32_t size) noexcept;
     Result load(const std::string& path) noexcept;
 
-    Result rotate(float degree) noexcept;
-    Result scale(float factor) noexcept;
-    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;
-
     static std::unique_ptr<Scene> gen() noexcept;
 
     _TVG_DECLARE_ACCESSOR(Canvas);
index ad02261..64cedbd 100644 (file)
@@ -27,6 +27,7 @@ source_file = [
    'tvgInitializer.cpp',
    'tvgLinearGradient.cpp',
    'tvgLoaderMgr.cpp',
+   'tvgPaint.cpp',
    'tvgRadialGradient.cpp',
    'tvgRender.cpp',
    'tvgScene.cpp',
index ac91f49..3c390b1 100644 (file)
@@ -43,6 +43,7 @@ using namespace tvg;
 #include "tvgLoader.h"
 #include "tvgLoaderMgr.h"
 #include "tvgRender.h"
+#include "tvgPaint.h"
 #include "tvgShapePath.h"
 #include "tvgShapeImpl.h"
 #include "tvgSceneImpl.h"
diff --git a/src/lib/tvgPaint.cpp b/src/lib/tvgPaint.cpp
new file mode 100644 (file)
index 0000000..7eeca19
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *               http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+#ifndef _TVG_PAINT_CPP_
+#define _TVG_PAINT_CPP_
+
+#include "tvgCommon.h"
+
+/************************************************************************/
+/* Internal Class Implementation                                        */
+/************************************************************************/
+
+Paint :: Paint() : pImpl(make_unique<Impl>())
+{
+}
+
+
+Paint :: ~Paint()
+{
+}
+
+
+/************************************************************************/
+/* External Class Implementation                                        */
+/************************************************************************/
+
+Result Paint::rotate(float degree) noexcept
+{
+    if (pImpl.get()->ts->rotate(degree)) return Result::Success;
+    return Result::FailedAllocation;
+}
+
+
+Result Paint::scale(float factor) noexcept
+{
+    if (pImpl.get()->ts->scale(factor)) return Result::Success;
+    return Result::FailedAllocation;
+}
+
+
+Result Paint::translate(float x, float y) noexcept
+{
+    if (pImpl.get()->ts->translate(x, y)) return Result::Success;
+    return Result::FailedAllocation;
+}
+
+
+Result Paint::transform(const Matrix& m) noexcept
+{
+    if (pImpl.get()->ts->transform(m)) return Result::Success;
+    return Result::FailedAllocation;
+}
+
+
+Result Paint::bounds(float* x, float* y, float* w, float* h) const noexcept
+{
+    if (pImpl.get()->ts->bounds(x, y, w, h)) return Result::Success;
+    return Result::InsufficientCondition;
+}
+
+#endif //_TVG_PAINT_CPP_
\ No newline at end of file
diff --git a/src/lib/tvgPaint.h b/src/lib/tvgPaint.h
new file mode 100644 (file)
index 0000000..cd0933b
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *               http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+#ifndef _TVG_PAINT_H_
+#define _TVG_PAINT_H_
+
+namespace tvg
+{
+    struct ITransformMethod
+    {
+        virtual ~ITransformMethod(){}
+        virtual bool rotate(float degree) = 0;
+        virtual bool scale(float factor) = 0;
+        virtual bool translate(float x, float y) = 0;
+        virtual bool transform(const Matrix& m) = 0;
+        virtual bool bounds(float* x, float* y, float* w, float* h) const = 0;
+    };
+
+    struct Paint::Impl
+    {
+        ITransformMethod* ts = nullptr;
+
+        ~Impl() {
+            if (ts) delete(ts);
+        }
+    };
+
+
+    template<class T>
+    struct TransformMethod : ITransformMethod
+    {
+        T* _inst = nullptr;
+
+        TransformMethod(T* inst) : _inst(inst) {}
+        ~TransformMethod(){}
+
+        bool rotate(float degree) override
+        {
+            return _inst->rotate(degree);
+        }
+
+        bool scale(float factor) override
+        {
+            return _inst->scale(factor);
+        }
+
+        bool translate(float x, float y) override
+        {
+            return _inst->translate(x, y);
+        }
+
+        bool transform(const Matrix& m) override
+        {
+            return _inst->transform(m);
+        }
+
+        bool bounds(float* x, float* y, float* w, float* h) const override
+        {
+            return _inst->bounds(x, y, w, h);
+        }
+    };
+}
+
+#endif //_TVG_PAINT_H_
\ No newline at end of file
index 1980d96..78ca868 100644 (file)
@@ -26,6 +26,8 @@
 Scene::Scene() : pImpl(make_unique<Impl>())
 {
     _id = PAINT_ID_SCENE;
+
+    Paint::pImpl.get()->ts = pImpl.get()->transformMethod();
 }
 
 
@@ -64,61 +66,6 @@ Result Scene::reserve(uint32_t size) noexcept
 }
 
 
-Result Scene::scale(float factor) noexcept
-{
-    auto impl = pImpl.get();
-    if (!impl) return Result::MemoryCorruption;
-
-    if (!impl->scale(factor)) return Result::FailedAllocation;
-
-    return Result::Success;
-}
-
-
-Result Scene::rotate(float degree) noexcept
-{
-    auto impl = pImpl.get();
-    if (!impl) return Result::MemoryCorruption;
-
-    if (!impl->rotate(degree)) return Result::FailedAllocation;
-
-    return Result::Success;
-}
-
-
-Result Scene::translate(float x, float y) noexcept
-{
-    auto impl = pImpl.get();
-    if (!impl) return Result::MemoryCorruption;
-
-    if (!impl->translate(x, y)) return Result::FailedAllocation;
-
-    return Result::Success;
-}
-
-
-Result Scene::transform(const Matrix& m) noexcept
-{
-    auto impl = pImpl.get();
-    if (!impl) return Result::MemoryCorruption;
-
-    if (!impl->transform(m)) return Result::FailedAllocation;
-
-    return Result::Success;
-}
-
-
-Result Scene::bounds(float* x, float* y, float* w, float* h) const noexcept
-{
-    auto impl = pImpl.get();
-    if (!impl) return Result::MemoryCorruption;
-
-    if (!impl->bounds(x, y, w, h)) return Result::InsufficientCondition;
-
-    return Result::Success;
-}
-
-
 Result Scene::load(const std::string& path) noexcept
 {
     if (path.empty()) return Result::InvalidArguments;
index 9972d72..08b42d3 100644 (file)
@@ -231,6 +231,12 @@ struct Scene::Impl
         if (!loader->read()) return Result::Unknown;
         return Result::Success;
     }
+
+    ITransformMethod* transformMethod()
+    {
+        return new TransformMethod<Scene::Impl>(this);
+    }
+
 };
 
 #endif //_TVG_SCENE_IMPL_H_
\ No newline at end of file
index 36aefed..c822f94 100644 (file)
@@ -25,7 +25,6 @@
 /************************************************************************/
 constexpr auto PATH_KAPPA = 0.552284f;
 
-
 /************************************************************************/
 /* External Class Implementation                                        */
 /************************************************************************/
@@ -33,6 +32,8 @@ constexpr auto PATH_KAPPA = 0.552284f;
 Shape :: Shape() : pImpl(make_unique<Impl>())
 {
     _id = PAINT_ID_SHAPE;
+
+    Paint::pImpl.get()->ts = pImpl.get()->transformMethod();
 }
 
 
@@ -280,61 +281,6 @@ const Fill* Shape::fill() const noexcept
 }
 
 
-Result Shape::scale(float factor) noexcept
-{
-    auto impl = pImpl.get();
-    if (!impl) return Result::MemoryCorruption;
-
-    if (!impl->scale(factor)) return Result::FailedAllocation;
-
-    return Result::Success;
-}
-
-
-Result Shape::rotate(float degree) noexcept
-{
-    auto impl = pImpl.get();
-    if (!impl) return Result::MemoryCorruption;
-
-    if (!impl->rotate(degree)) return Result::FailedAllocation;
-
-    return Result::Success;
-}
-
-
-Result Shape::translate(float x, float y) noexcept
-{
-    auto impl = pImpl.get();
-    if (!impl) return Result::MemoryCorruption;
-
-    if (!impl->translate(x, y)) return Result::FailedAllocation;
-
-    return Result::Success;
-}
-
-
-Result Shape::transform(const Matrix& m) noexcept
-{
-    auto impl = pImpl.get();
-    if (!impl) return Result::MemoryCorruption;
-
-    if (!impl->transform(m)) return Result::FailedAllocation;
-
-    return Result::Success;
-}
-
-
-Result Shape::bounds(float* x, float* y, float* w, float* h) const noexcept
-{
-    auto impl = pImpl.get();
-    if (!impl) return Result::MemoryCorruption;
-
-    if (!impl->bounds(x, y, w, h)) return Result::InsufficientCondition;
-
-    return Result::Success;
-}
-
-
 Result Shape::stroke(float width) noexcept
 {
     auto impl = pImpl.get();
index 54d37a3..c38916a 100644 (file)
@@ -235,6 +235,12 @@ struct Shape::Impl
 
         return true;
     }
+
+
+    ITransformMethod* transformMethod()
+    {
+        return new TransformMethod<Shape::Impl>(this);
+    }
 };
 
 #endif //_TVG_SHAPE_IMPL_H_
\ No newline at end of file