mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-08 13:43:43 +00:00
common: code refactoring.
revise duplicate() approach with stategy pattern.
This commit is contained in:
parent
c8a03494a1
commit
87fbff63cb
11 changed files with 83 additions and 57 deletions
|
@ -92,7 +92,7 @@ public:
|
|||
Result transform(const Matrix& m) noexcept;
|
||||
Result bounds(float* x, float* y, float* w, float* h) const noexcept;
|
||||
|
||||
virtual std::unique_ptr<Paint> duplicate() const noexcept = 0;
|
||||
std::unique_ptr<Paint> duplicate() const noexcept;
|
||||
|
||||
_TVG_DECLARE_ACCESSOR();
|
||||
_TVG_DECLARE_PRIVATE(Paint);
|
||||
|
@ -250,7 +250,6 @@ public:
|
|||
StrokeJoin strokeJoin() const noexcept;
|
||||
|
||||
static std::unique_ptr<Shape> gen() noexcept;
|
||||
std::unique_ptr<Paint> duplicate() const noexcept override;
|
||||
|
||||
_TVG_DECLARE_PRIVATE(Shape);
|
||||
};
|
||||
|
@ -274,7 +273,6 @@ public:
|
|||
Result viewbox(float* x, float* y, float* w, float* h) const noexcept;
|
||||
|
||||
static std::unique_ptr<Picture> gen() noexcept;
|
||||
std::unique_ptr<Paint> duplicate() const noexcept override;
|
||||
|
||||
_TVG_DECLARE_PRIVATE(Picture);
|
||||
};
|
||||
|
@ -297,7 +295,6 @@ public:
|
|||
Result reserve(uint32_t size) noexcept;
|
||||
|
||||
static std::unique_ptr<Scene> gen() noexcept;
|
||||
std::unique_ptr<Paint> duplicate() const noexcept override;
|
||||
|
||||
_TVG_DECLARE_PRIVATE(Scene);
|
||||
};
|
||||
|
|
|
@ -30,7 +30,6 @@ struct LinearGradient::Impl
|
|||
float x1, y1, x2, y2;
|
||||
};
|
||||
|
||||
|
||||
/************************************************************************/
|
||||
/* External Class Implementation */
|
||||
/************************************************************************/
|
||||
|
|
|
@ -72,3 +72,8 @@ Result Paint::bounds(float* x, float* y, float* w, float* h) const noexcept
|
|||
if (IMPL->bounds(x, y, w, h)) return Result::Success;
|
||||
return Result::InsufficientCondition;
|
||||
}
|
||||
|
||||
unique_ptr<Paint> Paint::duplicate() const noexcept
|
||||
{
|
||||
return IMPL->duplicate();
|
||||
}
|
|
@ -32,6 +32,7 @@ namespace tvg
|
|||
virtual bool update(RenderMethod& renderer, const RenderTransform* transform, RenderUpdateFlag pFlag) = 0;
|
||||
virtual bool render(RenderMethod& renderer) = 0;
|
||||
virtual bool bounds(float* x, float* y, float* w, float* h) const = 0;
|
||||
virtual unique_ptr<Paint> duplicate() = 0;
|
||||
};
|
||||
|
||||
struct Paint::Impl
|
||||
|
@ -144,6 +145,11 @@ namespace tvg
|
|||
{
|
||||
return smethod->render(renderer);
|
||||
}
|
||||
|
||||
unique_ptr<Paint> duplicate()
|
||||
{
|
||||
return smethod->duplicate();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
@ -174,6 +180,11 @@ namespace tvg
|
|||
{
|
||||
return inst->render(renderer);
|
||||
}
|
||||
|
||||
unique_ptr<Paint> duplicate() override
|
||||
{
|
||||
return inst->duplicate();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -63,10 +63,3 @@ Result Picture::viewbox(float* x, float* y, float* w, float* h) const noexcept
|
|||
if (IMPL->viewbox(x, y, w, h)) return Result::Success;
|
||||
return Result::InsufficientCondition;
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<Paint> Picture::duplicate() const noexcept
|
||||
{
|
||||
//TODO: implement
|
||||
return nullptr;
|
||||
}
|
||||
|
|
|
@ -104,6 +104,12 @@ struct Picture::Impl
|
|||
if (!loader->read()) return Result::Unknown;
|
||||
return Result::Success;
|
||||
}
|
||||
|
||||
unique_ptr<Paint> duplicate()
|
||||
{
|
||||
//TODO:
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //_TVG_PICTURE_IMPL_H_
|
|
@ -58,10 +58,3 @@ Result Scene::reserve(uint32_t size) noexcept
|
|||
|
||||
return Result::Success;
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<Paint> Scene::duplicate() const noexcept
|
||||
{
|
||||
//TODO: implement
|
||||
return nullptr;
|
||||
}
|
||||
|
|
|
@ -88,6 +88,12 @@ struct Scene::Impl
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
unique_ptr<Paint> duplicate()
|
||||
{
|
||||
//TODO:
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //_TVG_SCENE_IMPL_H_
|
|
@ -49,14 +49,6 @@ unique_ptr<Shape> Shape::gen() noexcept
|
|||
}
|
||||
|
||||
|
||||
unique_ptr<Paint> Shape::duplicate() const noexcept
|
||||
{
|
||||
auto shape = Shape::gen();
|
||||
if (!shape->pImpl->duplicate(IMPL)) return nullptr;
|
||||
return shape;
|
||||
}
|
||||
|
||||
|
||||
Result Shape::reset() noexcept
|
||||
{
|
||||
IMPL->path->reset();
|
||||
|
|
|
@ -38,6 +38,19 @@ struct ShapeStroke
|
|||
StrokeCap cap = StrokeCap::Square;
|
||||
StrokeJoin join = StrokeJoin::Bevel;
|
||||
|
||||
ShapeStroke() {}
|
||||
|
||||
ShapeStroke(const ShapeStroke* src)
|
||||
{
|
||||
width = src->width;
|
||||
dashCnt = src->dashCnt;
|
||||
cap = src->cap;
|
||||
join = src->join;
|
||||
memcpy(color, src->color, sizeof(color));
|
||||
dashPattern = static_cast<float*>(malloc(sizeof(float) * dashCnt));
|
||||
memcpy(dashPattern, src->dashPattern, sizeof(float) * dashCnt);
|
||||
}
|
||||
|
||||
~ShapeStroke()
|
||||
{
|
||||
if (dashPattern) free(dashPattern);
|
||||
|
@ -163,42 +176,32 @@ struct Shape::Impl
|
|||
return true;
|
||||
}
|
||||
|
||||
bool duplicate(Shape::Impl *src)
|
||||
unique_ptr<Paint> duplicate()
|
||||
{
|
||||
auto ret = Shape::gen();
|
||||
if (!ret) return nullptr;
|
||||
|
||||
auto dup = ret.get()->pImpl.get();
|
||||
|
||||
//Color
|
||||
memcpy(color, src->color, sizeof(color));
|
||||
flag = RenderUpdateFlag::Color;
|
||||
memcpy(dup->color, color, sizeof(color));
|
||||
dup->flag = RenderUpdateFlag::Color;
|
||||
|
||||
//Copy Path
|
||||
if (src->path) {
|
||||
path = new ShapePath();
|
||||
if (!path) return false;
|
||||
*path = *src->path;
|
||||
|
||||
path->cmds = static_cast<PathCommand*>(malloc(sizeof(PathCommand) * path->reservedCmdCnt));
|
||||
if (!path->cmds) return false;
|
||||
memcpy(path->cmds, src->path->cmds, sizeof(PathCommand) * path->cmdCnt);
|
||||
|
||||
path->pts = static_cast<Point*>(malloc(sizeof(Point) * path->reservedPtsCnt));
|
||||
if (!path->pts) return false;
|
||||
memcpy(path->pts, src->path->pts, sizeof(Point) * path->ptsCnt);
|
||||
|
||||
flag |= RenderUpdateFlag::Path;
|
||||
//Path
|
||||
if (path) {
|
||||
dup->path = new ShapePath(path);
|
||||
dup->flag |= RenderUpdateFlag::Path;
|
||||
}
|
||||
|
||||
//Copy Stroke
|
||||
if (src->stroke) {
|
||||
stroke = new ShapeStroke();
|
||||
if (!stroke) return false;
|
||||
*stroke = *src->stroke;
|
||||
stroke->dashPattern = static_cast<float*>(malloc(sizeof(float) * stroke->dashCnt));
|
||||
memcpy(stroke->dashPattern, src->stroke->dashPattern, sizeof(float) * stroke->dashCnt);
|
||||
flag |= RenderUpdateFlag::Stroke;
|
||||
//Stroke
|
||||
if (stroke) {
|
||||
dup->stroke = new ShapeStroke(stroke);
|
||||
dup->flag |= RenderUpdateFlag::Stroke;
|
||||
}
|
||||
|
||||
//TODO: Copy Fill
|
||||
//TODO: Fill
|
||||
|
||||
return true;
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -44,6 +44,27 @@ struct ShapePath
|
|||
if (pts) free(pts);
|
||||
}
|
||||
|
||||
ShapePath() {}
|
||||
|
||||
ShapePath(const ShapePath* src)
|
||||
{
|
||||
cmdCnt = src->cmdCnt;
|
||||
reservedCmdCnt = src->reservedCmdCnt;
|
||||
ptsCnt = src->ptsCnt;
|
||||
reservedPtsCnt = src->reservedPtsCnt;
|
||||
|
||||
cmds = static_cast<PathCommand*>(malloc(sizeof(PathCommand) * reservedCmdCnt));
|
||||
if (!cmds) return;
|
||||
memcpy(cmds, src->cmds, sizeof(PathCommand) * cmdCnt);
|
||||
|
||||
pts = static_cast<Point*>(malloc(sizeof(Point) * reservedPtsCnt));
|
||||
if (!pts) {
|
||||
free(cmds);
|
||||
return;
|
||||
}
|
||||
memcpy(pts, src->pts, sizeof(Point) * ptsCnt);
|
||||
}
|
||||
|
||||
void reserveCmd(uint32_t cmdCnt)
|
||||
{
|
||||
if (cmdCnt <= reservedCmdCnt) return;
|
||||
|
|
Loading…
Add table
Reference in a new issue