diff --git a/inc/thorvg.h b/inc/thorvg.h index bdca5a20..c8dafafa 100644 --- a/inc/thorvg.h +++ b/inc/thorvg.h @@ -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 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 gen() noexcept; _TVG_DECLARE_ACCESSOR(Canvas); diff --git a/src/lib/meson.build b/src/lib/meson.build index ad022618..64cedbd6 100644 --- a/src/lib/meson.build +++ b/src/lib/meson.build @@ -27,6 +27,7 @@ source_file = [ 'tvgInitializer.cpp', 'tvgLinearGradient.cpp', 'tvgLoaderMgr.cpp', + 'tvgPaint.cpp', 'tvgRadialGradient.cpp', 'tvgRender.cpp', 'tvgScene.cpp', diff --git a/src/lib/tvgCommon.h b/src/lib/tvgCommon.h index ac91f499..3c390b1b 100644 --- a/src/lib/tvgCommon.h +++ b/src/lib/tvgCommon.h @@ -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 index 00000000..7eeca198 --- /dev/null +++ b/src/lib/tvgPaint.cpp @@ -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()) +{ +} + + +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 index 00000000..cd0933b0 --- /dev/null +++ b/src/lib/tvgPaint.h @@ -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 + 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 diff --git a/src/lib/tvgScene.cpp b/src/lib/tvgScene.cpp index 1980d96a..78ca868b 100644 --- a/src/lib/tvgScene.cpp +++ b/src/lib/tvgScene.cpp @@ -26,6 +26,8 @@ Scene::Scene() : pImpl(make_unique()) { _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; diff --git a/src/lib/tvgSceneImpl.h b/src/lib/tvgSceneImpl.h index 9972d72f..08b42d36 100644 --- a/src/lib/tvgSceneImpl.h +++ b/src/lib/tvgSceneImpl.h @@ -231,6 +231,12 @@ struct Scene::Impl if (!loader->read()) return Result::Unknown; return Result::Success; } + + ITransformMethod* transformMethod() + { + return new TransformMethod(this); + } + }; #endif //_TVG_SCENE_IMPL_H_ \ No newline at end of file diff --git a/src/lib/tvgShape.cpp b/src/lib/tvgShape.cpp index 36aefedd..c822f942 100644 --- a/src/lib/tvgShape.cpp +++ b/src/lib/tvgShape.cpp @@ -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()) { _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(); diff --git a/src/lib/tvgShapeImpl.h b/src/lib/tvgShapeImpl.h index 54d37a3b..c38916a6 100644 --- a/src/lib/tvgShapeImpl.h +++ b/src/lib/tvgShapeImpl.h @@ -235,6 +235,12 @@ struct Shape::Impl return true; } + + + ITransformMethod* transformMethod() + { + return new TransformMethod(this); + } }; #endif //_TVG_SHAPE_IMPL_H_ \ No newline at end of file