2 * Copyright (c) 2020 - 2023 the ThorVG project. 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())
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
64 return Result::Success;
68 uint32_t Shape::pathCommands(const PathCommand** cmds) const noexcept
72 *cmds = pImpl->rs.path.cmds;
74 return pImpl->rs.path.cmdCnt;
78 uint32_t Shape::pathCoords(const Point** pts) const noexcept
82 *pts = pImpl->rs.path.pts;
84 return pImpl->rs.path.ptsCnt;
88 Result Shape::appendPath(const PathCommand *cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept
90 if (cmdCnt == 0 || ptsCnt == 0 || !cmds || !pts) return Result::InvalidArguments;
92 pImpl->grow(cmdCnt, ptsCnt);
93 pImpl->append(cmds, cmdCnt, pts, ptsCnt);
95 return Result::Success;
99 Result Shape::moveTo(float x, float y) noexcept
103 return Result::Success;
107 Result Shape::lineTo(float x, float y) noexcept
111 return Result::Success;
115 Result Shape::cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept
117 pImpl->cubicTo(cx1, cy1, cx2, cy2, x, y);
119 return Result::Success;
123 Result Shape::close() noexcept
127 return Result::Success;
131 Result Shape::appendCircle(float cx, float cy, float rx, float ry) noexcept
133 auto rxKappa = rx * PATH_KAPPA;
134 auto ryKappa = ry * PATH_KAPPA;
137 pImpl->moveTo(cx, cy - ry);
138 pImpl->cubicTo(cx + rxKappa, cy - ry, cx + rx, cy - ryKappa, cx + rx, cy);
139 pImpl->cubicTo(cx + rx, cy + ryKappa, cx + rxKappa, cy + ry, cx, cy + ry);
140 pImpl->cubicTo(cx - rxKappa, cy + ry, cx - rx, cy + ryKappa, cx - rx, cy);
141 pImpl->cubicTo(cx - rx, cy - ryKappa, cx - rxKappa, cy - ry, cx, cy - ry);
144 return Result::Success;
147 Result Shape::appendArc(float cx, float cy, float radius, float startAngle, float sweep, bool pie) noexcept
150 if (sweep >= 360.0f || sweep <= -360.0f) return appendCircle(cx, cy, radius, radius);
152 startAngle = (startAngle * M_PI) / 180.0f;
153 sweep = sweep * M_PI / 180.0f;
155 auto nCurves = ceil(fabsf(sweep / float(M_PI_2)));
156 auto sweepSign = (sweep < 0 ? -1 : 1);
157 auto fract = fmodf(sweep, float(M_PI_2));
158 fract = (mathZero(fract)) ? float(M_PI_2) * sweepSign : fract;
161 Point start = {radius * cosf(startAngle), radius * sinf(startAngle)};
164 pImpl->moveTo(cx, cy);
165 pImpl->lineTo(start.x + cx, start.y + cy);
167 pImpl->moveTo(start.x + cx, start.y + cy);
170 for (int i = 0; i < nCurves; ++i) {
171 auto endAngle = startAngle + ((i != nCurves - 1) ? float(M_PI_2) * sweepSign : fract);
172 Point end = {radius * cosf(endAngle), radius * sinf(endAngle)};
174 //variables needed to calculate bezier control points
176 //get bezier control points using article:
177 //(http://itc.ktu.lt/index.php/ITC/article/view/11812/6479)
182 auto q1 = ax * ax + ay * ay;
183 auto q2 = ax * bx + ay * by + q1;
184 auto k2 = (4.0f/3.0f) * ((sqrtf(2 * q1 * q2) - q2) / (ax * by - ay * bx));
186 start = end; //Next start point is the current end point
191 Point ctrl1 = {ax - k2 * ay + cx, ay + k2 * ax + cy};
192 Point ctrl2 = {bx + k2 * by + cx, by - k2 * bx + cy};
194 pImpl->cubicTo(ctrl1.x, ctrl1.y, ctrl2.x, ctrl2.y, end.x, end.y);
196 startAngle = endAngle;
199 if (pie) pImpl->close();
201 return Result::Success;
205 Result Shape::appendRect(float x, float y, float w, float h, float rx, float ry) noexcept
207 auto halfW = w * 0.5f;
208 auto halfH = h * 0.5f;
210 //clamping cornerRadius by minimum size
211 if (rx > halfW) rx = halfW;
212 if (ry > halfH) ry = halfH;
215 if (rx == 0 && ry == 0) {
218 pImpl->lineTo(x + w, y);
219 pImpl->lineTo(x + w, y + h);
220 pImpl->lineTo(x, y + h);
223 } else if (mathEqual(rx, halfW) && mathEqual(ry, halfH)) {
224 return appendCircle(x + (w * 0.5f), y + (h * 0.5f), rx, ry);
226 auto hrx = rx * 0.5f;
227 auto hry = ry * 0.5f;
229 pImpl->moveTo(x + rx, y);
230 pImpl->lineTo(x + w - rx, y);
231 pImpl->cubicTo(x + w - rx + hrx, y, x + w, y + ry - hry, x + w, y + ry);
232 pImpl->lineTo(x + w, y + h - ry);
233 pImpl->cubicTo(x + w, y + h - ry + hry, x + w - rx + hrx, y + h, x + w - rx, y + h);
234 pImpl->lineTo(x + rx, y + h);
235 pImpl->cubicTo(x + rx - hrx, y + h, x, y + h - ry + hry, x, y + h - ry);
236 pImpl->lineTo(x, y + ry);
237 pImpl->cubicTo(x, y + ry - hry, x + rx - hrx, y, x + rx, y);
241 return Result::Success;
245 Result Shape::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
247 pImpl->rs.color[0] = r;
248 pImpl->rs.color[1] = g;
249 pImpl->rs.color[2] = b;
250 pImpl->rs.color[3] = a;
251 pImpl->flag |= RenderUpdateFlag::Color;
253 if (pImpl->rs.fill) {
254 delete(pImpl->rs.fill);
255 pImpl->rs.fill = nullptr;
256 pImpl->flag |= RenderUpdateFlag::Gradient;
259 return Result::Success;
263 Result Shape::fill(unique_ptr<Fill> f) noexcept
265 auto p = f.release();
266 if (!p) return Result::MemoryCorruption;
268 if (pImpl->rs.fill && pImpl->rs.fill != p) delete(pImpl->rs.fill);
270 pImpl->flag |= RenderUpdateFlag::Gradient;
272 return Result::Success;
276 Result Shape::fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept
278 pImpl->rs.fillColor(r, g, b, a);
280 return Result::Success;
284 const Fill* Shape::fill() const noexcept
286 return pImpl->rs.fill;
290 Result Shape::stroke(float width) noexcept
292 if (!pImpl->strokeWidth(width)) return Result::FailedAllocation;
294 return Result::Success;
298 float Shape::strokeWidth() const noexcept
300 return pImpl->rs.strokeWidth();
304 Result Shape::stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
306 if (!pImpl->strokeColor(r, g, b, a)) return Result::FailedAllocation;
308 return Result::Success;
312 Result Shape::strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept
314 if (!pImpl->rs.strokeColor(r, g, b, a)) return Result::InsufficientCondition;
316 return Result::Success;
320 Result Shape::stroke(unique_ptr<Fill> f) noexcept
322 return pImpl->strokeFill(move(f));
326 const Fill* Shape::strokeFill() const noexcept
328 return pImpl->rs.strokeFill();
332 Result Shape::stroke(const float* dashPattern, uint32_t cnt) noexcept
334 if ((cnt == 1) || (!dashPattern && cnt > 0) || (dashPattern && cnt == 0)) {
335 return Result::InvalidArguments;
338 for (uint32_t i = 0; i < cnt; i++)
339 if (dashPattern[i] < FLT_EPSILON) return Result::InvalidArguments;
341 if (!pImpl->strokeDash(dashPattern, cnt)) return Result::FailedAllocation;
343 return Result::Success;
347 uint32_t Shape::strokeDash(const float** dashPattern) const noexcept
349 return pImpl->rs.strokeDash(dashPattern);
353 Result Shape::stroke(StrokeCap cap) noexcept
355 if (!pImpl->strokeCap(cap)) return Result::FailedAllocation;
357 return Result::Success;
361 Result Shape::stroke(StrokeJoin join) noexcept
363 if (!pImpl->strokeJoin(join)) return Result::FailedAllocation;
365 return Result::Success;
369 StrokeCap Shape::strokeCap() const noexcept
371 return pImpl->rs.strokeCap();
375 StrokeJoin Shape::strokeJoin() const noexcept
377 return pImpl->rs.strokeJoin();
381 Result Shape::fill(FillRule r) noexcept
385 return Result::Success;
389 FillRule Shape::fillRule() const noexcept
391 return pImpl->rs.rule;