mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-14 12:04:29 +00:00
common: revise transform interfaces.
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
This commit is contained in:
parent
8a924cbd78
commit
9e0c4666af
9 changed files with 177 additions and 126 deletions
23
inc/thorvg.h
23
inc/thorvg.h
|
@ -97,9 +97,16 @@ struct Matrix
|
||||||
class TVG_EXPORT Paint
|
class TVG_EXPORT Paint
|
||||||
{
|
{
|
||||||
public:
|
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_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(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
|
||||||
Result fill(std::unique_ptr<Fill> f) 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
|
//Getters
|
||||||
uint32_t pathCommands(const PathCommand** cmds) const noexcept;
|
uint32_t pathCommands(const PathCommand** cmds) const noexcept;
|
||||||
uint32_t pathCoords(const Point** pts) 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;
|
Result fill(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
|
||||||
const Fill* fill() const noexcept;
|
const Fill* fill() const noexcept;
|
||||||
Result bounds(float* x, float* y, float* w, float* h) const noexcept;
|
|
||||||
|
|
||||||
float strokeWidth() const noexcept;
|
float strokeWidth() const noexcept;
|
||||||
Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) 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 reserve(uint32_t size) noexcept;
|
||||||
Result load(const std::string& path) 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;
|
static std::unique_ptr<Scene> gen() noexcept;
|
||||||
|
|
||||||
_TVG_DECLARE_ACCESSOR(Canvas);
|
_TVG_DECLARE_ACCESSOR(Canvas);
|
||||||
|
|
|
@ -27,6 +27,7 @@ source_file = [
|
||||||
'tvgInitializer.cpp',
|
'tvgInitializer.cpp',
|
||||||
'tvgLinearGradient.cpp',
|
'tvgLinearGradient.cpp',
|
||||||
'tvgLoaderMgr.cpp',
|
'tvgLoaderMgr.cpp',
|
||||||
|
'tvgPaint.cpp',
|
||||||
'tvgRadialGradient.cpp',
|
'tvgRadialGradient.cpp',
|
||||||
'tvgRender.cpp',
|
'tvgRender.cpp',
|
||||||
'tvgScene.cpp',
|
'tvgScene.cpp',
|
||||||
|
|
|
@ -43,6 +43,7 @@ using namespace tvg;
|
||||||
#include "tvgLoader.h"
|
#include "tvgLoader.h"
|
||||||
#include "tvgLoaderMgr.h"
|
#include "tvgLoaderMgr.h"
|
||||||
#include "tvgRender.h"
|
#include "tvgRender.h"
|
||||||
|
#include "tvgPaint.h"
|
||||||
#include "tvgShapePath.h"
|
#include "tvgShapePath.h"
|
||||||
#include "tvgShapeImpl.h"
|
#include "tvgShapeImpl.h"
|
||||||
#include "tvgSceneImpl.h"
|
#include "tvgSceneImpl.h"
|
||||||
|
|
74
src/lib/tvgPaint.cpp
Normal file
74
src/lib/tvgPaint.cpp
Normal file
|
@ -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_
|
77
src/lib/tvgPaint.h
Normal file
77
src/lib/tvgPaint.h
Normal file
|
@ -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_
|
|
@ -26,6 +26,8 @@
|
||||||
Scene::Scene() : pImpl(make_unique<Impl>())
|
Scene::Scene() : pImpl(make_unique<Impl>())
|
||||||
{
|
{
|
||||||
_id = PAINT_ID_SCENE;
|
_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
|
Result Scene::load(const std::string& path) noexcept
|
||||||
{
|
{
|
||||||
if (path.empty()) return Result::InvalidArguments;
|
if (path.empty()) return Result::InvalidArguments;
|
||||||
|
|
|
@ -231,6 +231,12 @@ struct Scene::Impl
|
||||||
if (!loader->read()) return Result::Unknown;
|
if (!loader->read()) return Result::Unknown;
|
||||||
return Result::Success;
|
return Result::Success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ITransformMethod* transformMethod()
|
||||||
|
{
|
||||||
|
return new TransformMethod<Scene::Impl>(this);
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //_TVG_SCENE_IMPL_H_
|
#endif //_TVG_SCENE_IMPL_H_
|
|
@ -25,7 +25,6 @@
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
constexpr auto PATH_KAPPA = 0.552284f;
|
constexpr auto PATH_KAPPA = 0.552284f;
|
||||||
|
|
||||||
|
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
/* External Class Implementation */
|
/* External Class Implementation */
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
|
@ -33,6 +32,8 @@ constexpr auto PATH_KAPPA = 0.552284f;
|
||||||
Shape :: Shape() : pImpl(make_unique<Impl>())
|
Shape :: Shape() : pImpl(make_unique<Impl>())
|
||||||
{
|
{
|
||||||
_id = PAINT_ID_SHAPE;
|
_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
|
Result Shape::stroke(float width) noexcept
|
||||||
{
|
{
|
||||||
auto impl = pImpl.get();
|
auto impl = pImpl.get();
|
||||||
|
|
|
@ -235,6 +235,12 @@ struct Shape::Impl
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ITransformMethod* transformMethod()
|
||||||
|
{
|
||||||
|
return new TransformMethod<Shape::Impl>(this);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //_TVG_SHAPE_IMPL_H_
|
#endif //_TVG_SHAPE_IMPL_H_
|
Loading…
Add table
Reference in a new issue