2 * Copyright (c) 2020-2021 Samsung Electronics Co., Ltd. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 #ifndef _TVG_SHAPE_IMPL_H_
23 #define _TVG_SHAPE_IMPL_H_
28 /************************************************************************/
29 /* Internal Class Implementation */
30 /************************************************************************/
35 uint8_t color[4] = {0, 0, 0, 0};
37 float* dashPattern = nullptr;
39 StrokeCap cap = StrokeCap::Square;
40 StrokeJoin join = StrokeJoin::Bevel;
44 ShapeStroke(const ShapeStroke* src)
46 dashCnt(src->dashCnt),
50 memcpy(color, src->color, sizeof(color));
51 dashPattern = static_cast<float*>(malloc(sizeof(float) * dashCnt));
52 memcpy(dashPattern, src->dashPattern, sizeof(float) * dashCnt);
54 fill = src->fill->duplicate();
59 if (dashPattern) free(dashPattern);
60 if (fill) delete(fill);
67 PathCommand* cmds = nullptr;
69 uint32_t reservedCmdCnt = 0;
73 uint32_t reservedPtsCnt = 0;
85 void duplicate(const ShapePath* src)
88 reservedCmdCnt = src->reservedCmdCnt;
90 reservedPtsCnt = src->reservedPtsCnt;
92 cmds = static_cast<PathCommand*>(malloc(sizeof(PathCommand) * reservedCmdCnt));
94 memcpy(cmds, src->cmds, sizeof(PathCommand) * cmdCnt);
96 pts = static_cast<Point*>(malloc(sizeof(Point) * reservedPtsCnt));
101 memcpy(pts, src->pts, sizeof(Point) * ptsCnt);
104 void reserveCmd(uint32_t cmdCnt)
106 if (cmdCnt <= reservedCmdCnt) return;
107 reservedCmdCnt = cmdCnt;
108 cmds = static_cast<PathCommand*>(realloc(cmds, sizeof(PathCommand) * reservedCmdCnt));
111 void reservePts(uint32_t ptsCnt)
113 if (ptsCnt <= reservedPtsCnt) return;
114 reservedPtsCnt = ptsCnt;
115 pts = static_cast<Point*>(realloc(pts, sizeof(Point) * reservedPtsCnt));
118 void grow(uint32_t cmdCnt, uint32_t ptsCnt)
120 reserveCmd(this->cmdCnt + cmdCnt);
121 reservePts(this->ptsCnt + ptsCnt);
130 void append(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt)
132 memcpy(this->cmds + this->cmdCnt, cmds, sizeof(PathCommand) * cmdCnt);
133 memcpy(this->pts + this->ptsCnt, pts, sizeof(Point) * ptsCnt);
134 this->cmdCnt += cmdCnt;
135 this->ptsCnt += ptsCnt;
138 void moveTo(float x, float y)
140 if (cmdCnt + 1 > reservedCmdCnt) reserveCmd((cmdCnt + 1) * 2);
141 if (ptsCnt + 2 > reservedPtsCnt) reservePts((ptsCnt + 2) * 2);
143 cmds[cmdCnt++] = PathCommand::MoveTo;
144 pts[ptsCnt++] = {x, y};
147 void lineTo(float x, float y)
149 if (cmdCnt + 1 > reservedCmdCnt) reserveCmd((cmdCnt + 1) * 2);
150 if (ptsCnt + 2 > reservedPtsCnt) reservePts((ptsCnt + 2) * 2);
152 cmds[cmdCnt++] = PathCommand::LineTo;
153 pts[ptsCnt++] = {x, y};
156 void cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y)
158 if (cmdCnt + 1 > reservedCmdCnt) reserveCmd((cmdCnt + 1) * 2);
159 if (ptsCnt + 3 > reservedPtsCnt) reservePts((ptsCnt + 3) * 2);
161 cmds[cmdCnt++] = PathCommand::CubicTo;
162 pts[ptsCnt++] = {cx1, cy1};
163 pts[ptsCnt++] = {cx2, cy2};
164 pts[ptsCnt++] = {x, y};
169 if (cmdCnt > 0 && cmds[cmdCnt - 1] == PathCommand::Close) return;
171 if (cmdCnt + 1 > reservedCmdCnt) reserveCmd((cmdCnt + 1) * 2);
172 cmds[cmdCnt++] = PathCommand::Close;
175 bool bounds(float* x, float* y, float* w, float* h) const
177 if (ptsCnt == 0) return false;
179 Point min = { pts[0].x, pts[0].y };
180 Point max = { pts[0].x, pts[0].y };
182 for (uint32_t i = 1; i < ptsCnt; ++i) {
183 if (pts[i].x < min.x) min.x = pts[i].x;
184 if (pts[i].y < min.y) min.y = pts[i].y;
185 if (pts[i].x > max.x) max.x = pts[i].x;
186 if (pts[i].y > max.y) max.y = pts[i].y;
191 if (w) *w = max.x - min.x;
192 if (h) *h = max.y - min.y;
202 Fill *fill = nullptr;
203 ShapeStroke *stroke = nullptr;
204 uint8_t color[4] = {0, 0, 0, 0}; //r, g, b, a
205 FillRule rule = FillRule::Winding;
206 RenderData rdata = nullptr; //engine data
207 Shape *shape = nullptr;
208 uint32_t flag = RenderUpdateFlag::None;
210 Impl(Shape* s) : shape(s)
216 if (fill) delete(fill);
217 if (stroke) delete(stroke);
220 bool dispose(RenderMethod& renderer)
223 auto ret = renderer.dispose(rdata);
228 bool render(RenderMethod& renderer)
231 return renderer.renderShape(rdata);
234 void* update(RenderMethod& renderer, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag pFlag)
237 this->rdata = renderer.prepare(*shape, this->rdata, transform, opacity, clips, static_cast<RenderUpdateFlag>(pFlag | flag));
238 flag = RenderUpdateFlag::None;
242 RenderRegion bounds(RenderMethod& renderer)
244 return renderer.region(rdata);
247 bool bounds(float* x, float* y, float* w, float* h)
249 auto ret = path.bounds(x, y, w, h);
253 if (x) *x -= stroke->width * 0.5f;
254 if (y) *y -= stroke->width * 0.5f;
255 if (w) *w += stroke->width;
256 if (h) *h += stroke->width;
261 bool strokeWidth(float width)
263 //TODO: Size Exception?
265 if (!stroke) stroke = new ShapeStroke();
266 if (!stroke) return false;
268 stroke->width = width;
269 flag |= RenderUpdateFlag::Stroke;
274 bool strokeCap(StrokeCap cap)
276 if (!stroke) stroke = new ShapeStroke();
277 if (!stroke) return false;
280 flag |= RenderUpdateFlag::Stroke;
285 bool strokeJoin(StrokeJoin join)
287 if (!stroke) stroke = new ShapeStroke();
288 if (!stroke) return false;
291 flag |= RenderUpdateFlag::Stroke;
296 bool strokeColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
298 if (!stroke) stroke = new ShapeStroke();
299 if (!stroke) return false;
302 delete(stroke->fill);
303 stroke->fill = nullptr;
304 flag |= RenderUpdateFlag::GradientStroke;
307 stroke->color[0] = r;
308 stroke->color[1] = g;
309 stroke->color[2] = b;
310 stroke->color[3] = a;
312 flag |= RenderUpdateFlag::Stroke;
317 Result strokeFill(unique_ptr<Fill> f)
319 auto p = f.release();
320 if (!p) return Result::MemoryCorruption;
322 if (!stroke) stroke = new ShapeStroke();
323 if (!stroke) return Result::FailedAllocation;
325 if (stroke->fill && stroke->fill != p) delete(stroke->fill);
328 flag |= RenderUpdateFlag::Stroke;
329 flag |= RenderUpdateFlag::GradientStroke;
331 return Result::Success;
334 bool strokeDash(const float* pattern, uint32_t cnt)
336 if (!stroke) stroke = new ShapeStroke();
337 if (!stroke) return false;
339 if (stroke->dashCnt != cnt) {
340 if (stroke->dashPattern) free(stroke->dashPattern);
341 stroke->dashPattern = nullptr;
344 if (!stroke->dashPattern) {
345 stroke->dashPattern = static_cast<float*>(malloc(sizeof(float) * cnt));
346 if (!stroke->dashPattern) return false;
349 for (uint32_t i = 0; i < cnt; ++i)
350 stroke->dashPattern[i] = pattern[i];
352 stroke->dashCnt = cnt;
353 flag |= RenderUpdateFlag::Stroke;
360 auto ret = Shape::gen();
361 if (!ret) return nullptr;
363 auto dup = ret.get()->pImpl;
367 memcpy(dup->color, color, sizeof(color));
368 dup->flag = RenderUpdateFlag::Color;
371 dup->path.duplicate(&path);
372 dup->flag |= RenderUpdateFlag::Path;
376 dup->stroke = new ShapeStroke(stroke);
377 dup->flag |= RenderUpdateFlag::Stroke;
380 dup->flag |= RenderUpdateFlag::GradientStroke;
385 dup->fill = fill->duplicate();
386 dup->flag |= RenderUpdateFlag::Gradient;
389 return ret.release();
393 #endif //_TVG_SHAPE_IMPL_H_