common stroke: implement stroke interfaces.

Change-Id: I28fe5d5df4cde6640b143e67e241c6afc9c6b1fe
This commit is contained in:
Hermet Park 2020-05-22 16:18:32 +09:00
parent a5f15a588c
commit 19999e7abd
4 changed files with 145 additions and 1 deletions

View file

@ -136,6 +136,10 @@ public:
int appendCircle(float cx, float cy, float radiusW, float radiusH) noexcept;
int appendPath(const PathCommand* cmds, size_t cmdCnt, const Point* pts, size_t ptsCnt) noexcept;
int stroke(size_t width) noexcept;
int stroke(size_t r, size_t g, size_t b, size_t a) noexcept;
int stroke(const size_t* dashPattern, size_t cnt) noexcept;
int fill(size_t r, size_t g, size_t b, size_t a) noexcept;
int rotate(float degree) noexcept override;
@ -145,6 +149,10 @@ public:
size_t pathCommands(const PathCommand** cmds) const noexcept;
size_t pathCoords(const Point** pts) const noexcept;
int fill(size_t* r, size_t* g, size_t* b, size_t* a) const noexcept;
size_t stroke() const noexcept;
int stroke(size_t* r, size_t* g, size_t* b, size_t* a) const noexcept;
size_t stroke(const size_t** dashPattern) const noexcept;
int bounds(float&x, float& y, float& w, float& h) const noexcept override;
static std::unique_ptr<Shape> gen() noexcept;

View file

@ -28,7 +28,7 @@ struct Surface
size_t w, h;
};
enum RenderUpdateFlag {None = 0, Path = 1, Fill = 2, Transform = 4, All = 8};
enum RenderUpdateFlag {None = 0, Path = 1, Fill = 2, Stroke = 4, Transform = 8, All = 16};
struct RenderTransform
{

View file

@ -280,4 +280,79 @@ int Shape::bounds(float& x, float& y, float& w, float& h) const noexcept
}
int Shape::stroke(size_t width) noexcept
{
auto impl = pImpl.get();
assert(impl);
if (!impl->strokeWidth(width)) return -1;
return 0;
}
size_t Shape::stroke() const noexcept
{
auto impl = pImpl.get();
assert(impl);
if (!impl->stroke) return -1;
return impl->stroke->width;
}
int Shape::stroke(size_t r, size_t g, size_t b, size_t a) noexcept
{
auto impl = pImpl.get();
assert(impl);
if (!impl->strokeColor(r, g, b, a)) return -1;
return 0;
}
int Shape::stroke(size_t* r, size_t* g, size_t* b, size_t* a) const noexcept
{
auto impl = pImpl.get();
assert(impl);
if (!impl->stroke) return -1;
if (r) *r = impl->stroke->color[0];
if (g) *g = impl->stroke->color[1];
if (b) *b = impl->stroke->color[2];
if (a) *a = impl->stroke->color[3];
return 0;
}
int Shape::stroke(const size_t* dashPattern, size_t cnt) noexcept
{
if (cnt < 2 || !dashPattern) return -1;
auto impl = pImpl.get();
assert(impl);
if (!impl->strokeDash(dashPattern, cnt)) return -1;
return 0;
}
size_t Shape::stroke(const size_t** dashPattern) const noexcept
{
assert(dashPattern);
auto impl = pImpl.get();
assert(impl);
if (!impl->stroke) return 0;
*dashPattern = impl->stroke->dashPattern;
return impl->stroke->dashCnt;
}
#endif //_TVG_SHAPE_CPP_

View file

@ -30,6 +30,15 @@ struct ShapeFill
struct ShapeStroke
{
size_t width = 0;
size_t color[4] = {0, 0, 0, 0};
size_t* dashPattern = nullptr;
size_t dashCnt = 0;
~ShapeStroke()
{
if (dashPattern) free(dashPattern);
}
};
@ -141,6 +150,58 @@ struct Shape::Impl
return 0;
}
bool strokeWidth(size_t width)
{
//TODO: Size Exception?
if (!stroke) stroke = new ShapeStroke();
assert(stroke);
stroke->width = width;
flag |= RenderUpdateFlag::Stroke;
return 0;
}
bool strokeColor(size_t r, size_t g, size_t b, size_t a)
{
if (!stroke) stroke = new ShapeStroke();
assert(stroke);
stroke->color[0] = r;
stroke->color[1] = g;
stroke->color[2] = b;
stroke->color[3] = a;
flag |= RenderUpdateFlag::Stroke;
return 0;
}
bool strokeDash(const size_t* pattern, size_t cnt)
{
assert(pattern);
if (!stroke) stroke = new ShapeStroke();
assert(stroke);
if (stroke->dashCnt != cnt) {
if (stroke->dashPattern) free(stroke->dashPattern);
stroke->dashPattern = nullptr;
}
if (!stroke->dashPattern) stroke->dashPattern = static_cast<size_t*>(malloc(sizeof(size_t) * cnt));
assert(stroke->dashPattern);
memcpy(stroke->dashPattern, pattern, cnt);
stroke->dashCnt = cnt;
flag |= RenderUpdateFlag::Stroke;
return 0;
}
};
#endif //_TVG_SHAPE_IMPL_H_