common path: code refactoring.

revise duplicate() method for clean & neat code.
This commit is contained in:
Hermet Park 2020-09-17 11:28:05 +09:00 committed by GitHub
parent 538db6e881
commit 8d5e4e883d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 27 deletions

View file

@ -52,7 +52,7 @@ unique_ptr<Shape> Shape::gen() noexcept
unique_ptr<Paint> Shape::duplicate() const noexcept unique_ptr<Paint> Shape::duplicate() const noexcept
{ {
auto shape = Shape::gen(); auto shape = Shape::gen();
shape->pImpl->duplicate(IMPL); if (!shape->pImpl->duplicate(IMPL)) return nullptr;
return shape; return shape;
} }

View file

@ -163,44 +163,42 @@ struct Shape::Impl
return true; return true;
} }
void duplicate(Shape::Impl *s) bool duplicate(Shape::Impl *src)
{ {
if (memcmp(color, s->color, sizeof(color))) { //Color
memcpy(color, s->color, sizeof(color)); *color = *src->color;
flag = RenderUpdateFlag::Color; flag = RenderUpdateFlag::Color;
}
if (s->path) { //Copy Path
if (src->path) {
path = new ShapePath(); path = new ShapePath();
if (!path) return false;
path->cmdCnt = s->path->cmdCnt; *path = *src->path;
path->ptsCnt = s->path->ptsCnt;
path->reservedCmdCnt = s->path->reservedCmdCnt;
path->reservedPtsCnt = s->path->reservedPtsCnt;
path->cmds = static_cast<PathCommand*>(malloc(sizeof(PathCommand) * path->reservedCmdCnt)); 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)); 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);
memcpy(path->cmds, s->path->cmds, sizeof(PathCommand) * s->path->cmdCnt); flag |= RenderUpdateFlag::Path;
memcpy(path->pts, s->path->pts, sizeof(Point) * s->path->ptsCnt);
flag = RenderUpdateFlag::Path;
} }
if (s->stroke) { //Copy Stroke
if (src->stroke) {
stroke = new ShapeStroke(); stroke = new ShapeStroke();
strokeCap(s->stroke->cap); if (!stroke) return false;
strokeColor(s->stroke->color[0], s->stroke->color[1], *stroke = *src->stroke;
s->stroke->color[2], s->stroke->color[3]); stroke->dashPattern = static_cast<float*>(malloc(sizeof(float) * stroke->dashCnt));
strokeJoin(s->stroke->join); memcpy(stroke->dashPattern, src->stroke->dashPattern, sizeof(float) * stroke->dashCnt);
strokeWidth(s->stroke->width); flag |= RenderUpdateFlag::Stroke;
if (s->stroke->dashPattern && s->stroke->dashCnt > 0) {
strokeDash(s->stroke->dashPattern, s->stroke->dashCnt);
}
} }
//TODO: add fill //TODO: Copy Fill
return true;
} }
}; };