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)
222 auto ret = renderer.dispose(rdata);
227 bool render(RenderMethod& renderer)
229 return renderer.renderShape(rdata);
232 void* update(RenderMethod& renderer, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag pFlag)
234 this->rdata = renderer.prepare(*shape, this->rdata, transform, opacity, clips, static_cast<RenderUpdateFlag>(pFlag | flag));
235 flag = RenderUpdateFlag::None;
239 RenderRegion bounds(RenderMethod& renderer)
241 return renderer.region(rdata);
244 bool bounds(float* x, float* y, float* w, float* h)
246 auto ret = path.bounds(x, y, w, h);
250 if (x) *x -= stroke->width * 0.5f;
251 if (y) *y -= stroke->width * 0.5f;
252 if (w) *w += stroke->width;
253 if (h) *h += stroke->width;
258 bool strokeWidth(float width)
260 //TODO: Size Exception?
262 if (!stroke) stroke = new ShapeStroke();
263 if (!stroke) return false;
265 stroke->width = width;
266 flag |= RenderUpdateFlag::Stroke;
271 bool strokeCap(StrokeCap cap)
273 if (!stroke) stroke = new ShapeStroke();
274 if (!stroke) return false;
277 flag |= RenderUpdateFlag::Stroke;
282 bool strokeJoin(StrokeJoin join)
284 if (!stroke) stroke = new ShapeStroke();
285 if (!stroke) return false;
288 flag |= RenderUpdateFlag::Stroke;
293 bool strokeColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
295 if (!stroke) stroke = new ShapeStroke();
296 if (!stroke) return false;
299 delete(stroke->fill);
300 stroke->fill = nullptr;
301 flag |= RenderUpdateFlag::GradientStroke;
304 stroke->color[0] = r;
305 stroke->color[1] = g;
306 stroke->color[2] = b;
307 stroke->color[3] = a;
309 flag |= RenderUpdateFlag::Stroke;
314 Result strokeFill(unique_ptr<Fill> f)
316 auto p = f.release();
317 if (!p) return Result::MemoryCorruption;
319 if (!stroke) stroke = new ShapeStroke();
320 if (!stroke) return Result::FailedAllocation;
322 if (stroke->fill && stroke->fill != p) delete(stroke->fill);
325 flag |= RenderUpdateFlag::Stroke;
326 flag |= RenderUpdateFlag::GradientStroke;
328 return Result::Success;
331 bool strokeDash(const float* pattern, uint32_t cnt)
333 if (!stroke) stroke = new ShapeStroke();
334 if (!stroke) return false;
336 if (stroke->dashCnt != cnt) {
337 if (stroke->dashPattern) free(stroke->dashPattern);
338 stroke->dashPattern = nullptr;
341 if (!stroke->dashPattern) {
342 stroke->dashPattern = static_cast<float*>(malloc(sizeof(float) * cnt));
343 if (!stroke->dashPattern) return false;
346 for (uint32_t i = 0; i < cnt; ++i)
347 stroke->dashPattern[i] = pattern[i];
349 stroke->dashCnt = cnt;
350 flag |= RenderUpdateFlag::Stroke;
357 auto ret = Shape::gen();
358 if (!ret) return nullptr;
360 auto dup = ret.get()->pImpl;
364 memcpy(dup->color, color, sizeof(color));
365 dup->flag = RenderUpdateFlag::Color;
368 dup->path.duplicate(&path);
369 dup->flag |= RenderUpdateFlag::Path;
373 dup->stroke = new ShapeStroke(stroke);
374 dup->flag |= RenderUpdateFlag::Stroke;
377 dup->flag |= RenderUpdateFlag::GradientStroke;
382 dup->fill = fill->duplicate();
383 dup->flag |= RenderUpdateFlag::Gradient;
386 return ret.release();
390 #endif //_TVG_SHAPE_IMPL_H_