2 * Copyright (c) 2020 - 2022 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 /************************************************************************/
42 void copy(const ShapeStroke* src)
45 dashCnt = src->dashCnt;
49 memcpy(color, src->color, sizeof(color));
51 dashPattern = static_cast<float*>(malloc(sizeof(float) * dashCnt));
52 memcpy(dashPattern, src->dashPattern, sizeof(float) * dashCnt);
54 if (src->fill) 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)
87 if (src->cmdCnt == 0 || src->ptsCnt == 0) return;
90 reservedCmdCnt = src->reservedCmdCnt;
92 reservedPtsCnt = src->reservedPtsCnt;
94 cmds = static_cast<PathCommand*>(malloc(sizeof(PathCommand) * reservedCmdCnt));
96 memcpy(cmds, src->cmds, sizeof(PathCommand) * cmdCnt);
98 pts = static_cast<Point*>(malloc(sizeof(Point) * reservedPtsCnt));
103 memcpy(pts, src->pts, sizeof(Point) * ptsCnt);
106 void reserveCmd(uint32_t cmdCnt)
108 if (cmdCnt <= reservedCmdCnt) return;
109 reservedCmdCnt = cmdCnt;
110 cmds = static_cast<PathCommand*>(realloc(cmds, sizeof(PathCommand) * reservedCmdCnt));
113 void reservePts(uint32_t ptsCnt)
115 if (ptsCnt <= reservedPtsCnt) return;
116 reservedPtsCnt = ptsCnt;
117 pts = static_cast<Point*>(realloc(pts, sizeof(Point) * reservedPtsCnt));
120 void grow(uint32_t cmdCnt, uint32_t ptsCnt)
122 reserveCmd(this->cmdCnt + cmdCnt);
123 reservePts(this->ptsCnt + ptsCnt);
132 void append(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt)
134 memcpy(this->cmds + this->cmdCnt, cmds, sizeof(PathCommand) * cmdCnt);
135 memcpy(this->pts + this->ptsCnt, pts, sizeof(Point) * ptsCnt);
136 this->cmdCnt += cmdCnt;
137 this->ptsCnt += ptsCnt;
140 void moveTo(float x, float y)
142 if (cmdCnt + 1 > reservedCmdCnt) reserveCmd((cmdCnt + 1) * 2);
143 if (ptsCnt + 2 > reservedPtsCnt) reservePts((ptsCnt + 2) * 2);
145 cmds[cmdCnt++] = PathCommand::MoveTo;
146 pts[ptsCnt++] = {x, y};
149 void lineTo(float x, float y)
151 if (cmdCnt + 1 > reservedCmdCnt) reserveCmd((cmdCnt + 1) * 2);
152 if (ptsCnt + 2 > reservedPtsCnt) reservePts((ptsCnt + 2) * 2);
154 cmds[cmdCnt++] = PathCommand::LineTo;
155 pts[ptsCnt++] = {x, y};
158 void cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y)
160 if (cmdCnt + 1 > reservedCmdCnt) reserveCmd((cmdCnt + 1) * 2);
161 if (ptsCnt + 3 > reservedPtsCnt) reservePts((ptsCnt + 3) * 2);
163 cmds[cmdCnt++] = PathCommand::CubicTo;
164 pts[ptsCnt++] = {cx1, cy1};
165 pts[ptsCnt++] = {cx2, cy2};
166 pts[ptsCnt++] = {x, y};
171 if (cmdCnt > 0 && cmds[cmdCnt - 1] == PathCommand::Close) return;
173 if (cmdCnt + 1 > reservedCmdCnt) reserveCmd((cmdCnt + 1) * 2);
174 cmds[cmdCnt++] = PathCommand::Close;
177 bool bounds(float* x, float* y, float* w, float* h) const
179 if (ptsCnt == 0) return false;
181 Point min = { pts[0].x, pts[0].y };
182 Point max = { pts[0].x, pts[0].y };
184 for (uint32_t i = 1; i < ptsCnt; ++i) {
185 if (pts[i].x < min.x) min.x = pts[i].x;
186 if (pts[i].y < min.y) min.y = pts[i].y;
187 if (pts[i].x > max.x) max.x = pts[i].x;
188 if (pts[i].y > max.y) max.y = pts[i].y;
193 if (w) *w = max.x - min.x;
194 if (h) *h = max.y - min.y;
204 Fill *fill = nullptr;
205 ShapeStroke *stroke = nullptr;
206 uint8_t color[4] = {0, 0, 0, 0}; //r, g, b, a
207 FillRule rule = FillRule::Winding;
208 RenderData rdata = nullptr; //engine data
209 Shape *shape = nullptr;
210 uint32_t flag = RenderUpdateFlag::None;
212 Impl(Shape* s) : shape(s)
218 if (fill) delete(fill);
225 bool dispose(RenderMethod& renderer)
227 auto ret = renderer.dispose(rdata);
232 bool render(RenderMethod& renderer)
234 return renderer.renderShape(rdata);
237 void* update(RenderMethod& renderer, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag pFlag)
239 this->rdata = renderer.prepare(*shape, this->rdata, transform, opacity, clips, static_cast<RenderUpdateFlag>(pFlag | flag));
240 flag = RenderUpdateFlag::None;
244 RenderRegion bounds(RenderMethod& renderer)
246 return renderer.region(rdata);
249 bool bounds(float* x, float* y, float* w, float* h)
251 auto ret = path.bounds(x, y, w, h);
255 if (x) *x -= stroke->width * 0.5f;
256 if (y) *y -= stroke->width * 0.5f;
257 if (w) *w += stroke->width;
258 if (h) *h += stroke->width;
263 bool strokeWidth(float width)
265 //TODO: Size Exception?
267 if (!stroke) stroke = static_cast<ShapeStroke*>(calloc(sizeof(ShapeStroke), 1));
268 stroke->width = width;
269 flag |= RenderUpdateFlag::Stroke;
274 bool strokeCap(StrokeCap cap)
276 if (!stroke) stroke = static_cast<ShapeStroke*>(calloc(sizeof(ShapeStroke), 1));
278 flag |= RenderUpdateFlag::Stroke;
283 bool strokeJoin(StrokeJoin join)
285 if (!stroke) stroke = static_cast<ShapeStroke*>(calloc(sizeof(ShapeStroke), 1));
287 flag |= RenderUpdateFlag::Stroke;
292 bool strokeColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
294 if (!stroke) stroke = static_cast<ShapeStroke*>(calloc(sizeof(ShapeStroke), 1));
296 delete(stroke->fill);
297 stroke->fill = nullptr;
298 flag |= RenderUpdateFlag::GradientStroke;
301 stroke->color[0] = r;
302 stroke->color[1] = g;
303 stroke->color[2] = b;
304 stroke->color[3] = a;
306 flag |= RenderUpdateFlag::Stroke;
311 Result strokeFill(unique_ptr<Fill> f)
313 auto p = f.release();
314 if (!p) return Result::MemoryCorruption;
316 if (!stroke) stroke = static_cast<ShapeStroke*>(calloc(sizeof(ShapeStroke), 1));
317 if (stroke->fill && stroke->fill != p) delete(stroke->fill);
320 flag |= RenderUpdateFlag::Stroke;
321 flag |= RenderUpdateFlag::GradientStroke;
323 return Result::Success;
326 bool strokeDash(const float* pattern, uint32_t cnt)
329 if (!pattern && cnt == 0) {
330 free(stroke->dashPattern);
331 stroke->dashPattern = nullptr;
333 if (!stroke) stroke = static_cast<ShapeStroke*>(calloc(sizeof(ShapeStroke), 1));
334 if (stroke->dashCnt != cnt) {
335 free(stroke->dashPattern);
336 stroke->dashPattern = nullptr;
338 if (!stroke->dashPattern) {
339 stroke->dashPattern = static_cast<float*>(malloc(sizeof(float) * cnt));
340 if (!stroke->dashPattern) return false;
342 for (uint32_t i = 0; i < cnt; ++i) {
343 stroke->dashPattern[i] = pattern[i];
346 stroke->dashCnt = cnt;
347 flag |= RenderUpdateFlag::Stroke;
354 auto ret = Shape::gen();
356 auto dup = ret.get()->pImpl;
360 memcpy(dup->color, color, sizeof(color));
361 dup->flag = RenderUpdateFlag::Color;
364 dup->path.duplicate(&path);
365 dup->flag |= RenderUpdateFlag::Path;
369 dup->stroke = static_cast<ShapeStroke*>(calloc(sizeof(ShapeStroke), 1));
370 dup->stroke->copy(stroke);
371 dup->flag |= RenderUpdateFlag::Stroke;
374 dup->flag |= RenderUpdateFlag::GradientStroke;
379 dup->fill = fill->duplicate();
380 dup->flag |= RenderUpdateFlag::Gradient;
383 return ret.release();
392 #endif //_TVG_SHAPE_IMPL_H_