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
24 #include "tvgShapeImpl.h"
26 /************************************************************************/
27 /* Internal Class Implementation */
28 /************************************************************************/
29 constexpr auto PATH_KAPPA = 0.552284f;
31 /************************************************************************/
32 /* External Class Implementation */
33 /************************************************************************/
35 Shape :: Shape() : pImpl(new Impl(this))
37 Paint::pImpl->id = TVG_CLASS_ID_SHAPE;
38 Paint::pImpl->method(new PaintMethod<Shape::Impl>(pImpl));
48 unique_ptr<Shape> Shape::gen() noexcept
50 return unique_ptr<Shape>(new Shape);
54 uint32_t Shape::identifier() noexcept
56 return TVG_CLASS_ID_SHAPE;
60 Result Shape::reset() noexcept
63 pImpl->flag = RenderUpdateFlag::Path;
65 return Result::Success;
69 uint32_t Shape::pathCommands(const PathCommand** cmds) const noexcept
73 *cmds = pImpl->path.cmds;
75 return pImpl->path.cmdCnt;
79 uint32_t Shape::pathCoords(const Point** pts) const noexcept
83 *pts = pImpl->path.pts;
85 return pImpl->path.ptsCnt;
89 Result Shape::appendPath(const PathCommand *cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept
91 if (cmdCnt == 0 || ptsCnt == 0 || !cmds || !pts) return Result::InvalidArguments;
93 pImpl->path.grow(cmdCnt, ptsCnt);
94 pImpl->path.append(cmds, cmdCnt, pts, ptsCnt);
96 pImpl->flag |= RenderUpdateFlag::Path;
98 return Result::Success;
102 Result Shape::moveTo(float x, float y) noexcept
104 pImpl->path.moveTo(x, y);
106 pImpl->flag |= RenderUpdateFlag::Path;
108 return Result::Success;
112 Result Shape::lineTo(float x, float y) noexcept
114 pImpl->path.lineTo(x, y);
116 pImpl->flag |= RenderUpdateFlag::Path;
118 return Result::Success;
122 Result Shape::cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept
124 pImpl->path.cubicTo(cx1, cy1, cx2, cy2, x, y);
126 pImpl->flag |= RenderUpdateFlag::Path;
128 return Result::Success;
132 Result Shape::close() noexcept
136 pImpl->flag |= RenderUpdateFlag::Path;
138 return Result::Success;
142 Result Shape::appendCircle(float cx, float cy, float rx, float ry) noexcept
144 auto rxKappa = rx * PATH_KAPPA;
145 auto ryKappa = ry * PATH_KAPPA;
147 pImpl->path.grow(6, 13);
148 pImpl->path.moveTo(cx, cy - ry);
149 pImpl->path.cubicTo(cx + rxKappa, cy - ry, cx + rx, cy - ryKappa, cx + rx, cy);
150 pImpl->path.cubicTo(cx + rx, cy + ryKappa, cx + rxKappa, cy + ry, cx, cy + ry);
151 pImpl->path.cubicTo(cx - rxKappa, cy + ry, cx - rx, cy + ryKappa, cx - rx, cy);
152 pImpl->path.cubicTo(cx - rx, cy - ryKappa, cx - rxKappa, cy - ry, cx, cy - ry);
155 pImpl->flag |= RenderUpdateFlag::Path;
157 return Result::Success;
160 Result Shape::appendArc(float cx, float cy, float radius, float startAngle, float sweep, bool pie) noexcept
163 if (sweep >= 360.0f || sweep <= -360.0f) return appendCircle(cx, cy, radius, radius);
165 startAngle = (startAngle * M_PI) / 180.0f;
166 sweep = sweep * M_PI / 180.0f;
168 auto nCurves = ceil(fabsf(sweep / float(M_PI_2)));
169 auto sweepSign = (sweep < 0 ? -1 : 1);
170 auto fract = fmodf(sweep, float(M_PI_2));
171 fract = (mathZero(fract)) ? float(M_PI_2) * sweepSign : fract;
174 Point start = {radius * cosf(startAngle), radius * sinf(startAngle)};
177 pImpl->path.moveTo(cx, cy);
178 pImpl->path.lineTo(start.x + cx, start.y + cy);
180 pImpl->path.moveTo(start.x + cx, start.y + cy);
183 for (int i = 0; i < nCurves; ++i) {
184 auto endAngle = startAngle + ((i != nCurves - 1) ? float(M_PI_2) * sweepSign : fract);
185 Point end = {radius * cosf(endAngle), radius * sinf(endAngle)};
187 //variables needed to calculate bezier control points
189 //get bezier control points using article:
190 //(http://itc.ktu.lt/index.php/ITC/article/view/11812/6479)
195 auto q1 = ax * ax + ay * ay;
196 auto q2 = ax * bx + ay * by + q1;
197 auto k2 = (4.0f/3.0f) * ((sqrtf(2 * q1 * q2) - q2) / (ax * by - ay * bx));
199 start = end; //Next start point is the current end point
204 Point ctrl1 = {ax - k2 * ay + cx, ay + k2 * ax + cy};
205 Point ctrl2 = {bx + k2 * by + cx, by - k2 * bx + cy};
207 pImpl->path.cubicTo(ctrl1.x, ctrl1.y, ctrl2.x, ctrl2.y, end.x, end.y);
209 startAngle = endAngle;
212 if (pie) pImpl->path.close();
214 pImpl->flag |= RenderUpdateFlag::Path;
216 return Result::Success;
220 Result Shape::appendRect(float x, float y, float w, float h, float rx, float ry) noexcept
222 auto halfW = w * 0.5f;
223 auto halfH = h * 0.5f;
225 //clamping cornerRadius by minimum size
226 if (rx > halfW) rx = halfW;
227 if (ry > halfH) ry = halfH;
230 if (rx == 0 && ry == 0) {
231 pImpl->path.grow(5, 4);
232 pImpl->path.moveTo(x, y);
233 pImpl->path.lineTo(x + w, y);
234 pImpl->path.lineTo(x + w, y + h);
235 pImpl->path.lineTo(x, y + h);
238 } else if (mathEqual(rx, halfW) && mathEqual(ry, halfH)) {
239 return appendCircle(x + (w * 0.5f), y + (h * 0.5f), rx, ry);
241 auto hrx = rx * 0.5f;
242 auto hry = ry * 0.5f;
243 pImpl->path.grow(10, 17);
244 pImpl->path.moveTo(x + rx, y);
245 pImpl->path.lineTo(x + w - rx, y);
246 pImpl->path.cubicTo(x + w - rx + hrx, y, x + w, y + ry - hry, x + w, y + ry);
247 pImpl->path.lineTo(x + w, y + h - ry);
248 pImpl->path.cubicTo(x + w, y + h - ry + hry, x + w - rx + hrx, y + h, x + w - rx, y + h);
249 pImpl->path.lineTo(x + rx, y + h);
250 pImpl->path.cubicTo(x + rx - hrx, y + h, x, y + h - ry + hry, x, y + h - ry);
251 pImpl->path.lineTo(x, y + ry);
252 pImpl->path.cubicTo(x, y + ry - hry, x + rx - hrx, y, x + rx, y);
256 pImpl->flag |= RenderUpdateFlag::Path;
258 return Result::Success;
262 Result Shape::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
268 pImpl->flag |= RenderUpdateFlag::Color;
272 pImpl->fill = nullptr;
273 pImpl->flag |= RenderUpdateFlag::Gradient;
276 return Result::Success;
280 Result Shape::fill(unique_ptr<Fill> f) noexcept
282 auto p = f.release();
283 if (!p) return Result::MemoryCorruption;
285 if (pImpl->fill && pImpl->fill != p) delete(pImpl->fill);
287 pImpl->flag |= RenderUpdateFlag::Gradient;
289 return Result::Success;
293 Result Shape::fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept
295 if (r) *r = pImpl->color[0];
296 if (g) *g = pImpl->color[1];
297 if (b) *b = pImpl->color[2];
298 if (a) *a = pImpl->color[3];
300 return Result::Success;
303 const Fill* Shape::fill() const noexcept
309 Result Shape::stroke(float width) noexcept
311 if (!pImpl->strokeWidth(width)) return Result::FailedAllocation;
313 return Result::Success;
317 float Shape::strokeWidth() const noexcept
319 if (!pImpl->stroke) return 0;
320 return pImpl->stroke->width;
324 Result Shape::stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
326 if (!pImpl->strokeColor(r, g, b, a)) return Result::FailedAllocation;
328 return Result::Success;
332 Result Shape::strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept
334 if (!pImpl->stroke) return Result::InsufficientCondition;
336 if (r) *r = pImpl->stroke->color[0];
337 if (g) *g = pImpl->stroke->color[1];
338 if (b) *b = pImpl->stroke->color[2];
339 if (a) *a = pImpl->stroke->color[3];
341 return Result::Success;
345 Result Shape::stroke(unique_ptr<Fill> f) noexcept
347 return pImpl->strokeFill(move(f));
351 const Fill* Shape::strokeFill() const noexcept
353 if (!pImpl->stroke) return nullptr;
355 return pImpl->stroke->fill;
359 Result Shape::stroke(const float* dashPattern, uint32_t cnt) noexcept
361 if ((cnt == 1) || (!dashPattern && cnt > 0) || (dashPattern && cnt == 0)) {
362 return Result::InvalidArguments;
365 for (uint32_t i = 0; i < cnt; i++)
366 if (dashPattern[i] < FLT_EPSILON) return Result::InvalidArguments;
368 if (!pImpl->strokeDash(dashPattern, cnt)) return Result::FailedAllocation;
370 return Result::Success;
374 uint32_t Shape::strokeDash(const float** dashPattern) const noexcept
376 if (!pImpl->stroke) return 0;
378 if (dashPattern) *dashPattern = pImpl->stroke->dashPattern;
380 return pImpl->stroke->dashCnt;
384 Result Shape::stroke(StrokeCap cap) noexcept
386 if (!pImpl->strokeCap(cap)) return Result::FailedAllocation;
388 return Result::Success;
392 Result Shape::stroke(StrokeJoin join) noexcept
394 if (!pImpl->strokeJoin(join)) return Result::FailedAllocation;
396 return Result::Success;
400 StrokeCap Shape::strokeCap() const noexcept
402 if (!pImpl->stroke) return StrokeCap::Square;
404 return pImpl->stroke->cap;
408 StrokeJoin Shape::strokeJoin() const noexcept
410 if (!pImpl->stroke) return StrokeJoin::Bevel;
412 return pImpl->stroke->join;
416 Result Shape::fill(FillRule r) noexcept
420 return Result::Success;
424 FillRule Shape::fillRule() const noexcept