1 /*
2  * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved.
3 
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:
10 
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13 
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
20  * SOFTWARE.
21  */
22 
23 #include "../../lv_conf_internal.h"
24 #if LV_USE_THORVG_INTERNAL
25 
26 #include "tvgMath.h"
27 #include "tvgShape.h"
28 
29 /************************************************************************/
30 /* Internal Class Implementation                                        */
31 /************************************************************************/
32 
33 
34 /************************************************************************/
35 /* External Class Implementation                                        */
36 /************************************************************************/
37 
Shape()38 Shape :: Shape() : pImpl(new Impl(this))
39 {
40 }
41 
42 
~Shape()43 Shape :: ~Shape()
44 {
45     delete(pImpl);
46 }
47 
48 
gen()49 unique_ptr<Shape> Shape::gen() noexcept
50 {
51     return unique_ptr<Shape>(new Shape);
52 }
53 
54 
identifier()55 uint32_t Shape::identifier() noexcept
56 {
57     return (uint32_t) Type::Shape;
58 }
59 
60 
type() const61 Type Shape::type() const noexcept
62 {
63     return Type::Shape;
64 }
65 
66 
reset()67 Result Shape::reset() noexcept
68 {
69     pImpl->rs.path.cmds.clear();
70     pImpl->rs.path.pts.clear();
71 
72     pImpl->flag |= RenderUpdateFlag::Path;
73 
74     return Result::Success;
75 }
76 
77 
pathCommands(const PathCommand ** cmds) const78 uint32_t Shape::pathCommands(const PathCommand** cmds) const noexcept
79 {
80     if (cmds) *cmds = pImpl->rs.path.cmds.data;
81     return pImpl->rs.path.cmds.count;
82 }
83 
84 
pathCoords(const Point ** pts) const85 uint32_t Shape::pathCoords(const Point** pts) const noexcept
86 {
87     if (pts) *pts = pImpl->rs.path.pts.data;
88     return pImpl->rs.path.pts.count;
89 }
90 
91 
appendPath(const PathCommand * cmds,uint32_t cmdCnt,const Point * pts,uint32_t ptsCnt)92 Result Shape::appendPath(const PathCommand *cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept
93 {
94     if (cmdCnt == 0 || ptsCnt == 0 || !cmds || !pts) return Result::InvalidArguments;
95 
96     pImpl->grow(cmdCnt, ptsCnt);
97     pImpl->append(cmds, cmdCnt, pts, ptsCnt);
98 
99     pImpl->flag |= RenderUpdateFlag::Path;
100 
101     return Result::Success;
102 }
103 
104 
moveTo(float x,float y)105 Result Shape::moveTo(float x, float y) noexcept
106 {
107     pImpl->moveTo(x, y);
108 
109     return Result::Success;
110 }
111 
112 
lineTo(float x,float y)113 Result Shape::lineTo(float x, float y) noexcept
114 {
115     pImpl->lineTo(x, y);
116 
117     pImpl->flag |= RenderUpdateFlag::Path;
118 
119     return Result::Success;
120 }
121 
122 
cubicTo(float cx1,float cy1,float cx2,float cy2,float x,float y)123 Result Shape::cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept
124 {
125     pImpl->cubicTo(cx1, cy1, cx2, cy2, x, y);
126 
127     pImpl->flag |= RenderUpdateFlag::Path;
128 
129     return Result::Success;
130 }
131 
132 
close()133 Result Shape::close() noexcept
134 {
135     pImpl->close();
136 
137     pImpl->flag |= RenderUpdateFlag::Path;
138 
139     return Result::Success;
140 }
141 
142 
appendCircle(float cx,float cy,float rx,float ry)143 Result Shape::appendCircle(float cx, float cy, float rx, float ry) noexcept
144 {
145     auto rxKappa = rx * PATH_KAPPA;
146     auto ryKappa = ry * PATH_KAPPA;
147 
148     pImpl->grow(6, 13);
149     pImpl->moveTo(cx + rx, cy);
150     pImpl->cubicTo(cx + rx, cy + ryKappa, cx + rxKappa, cy + ry, cx, cy + ry);
151     pImpl->cubicTo(cx - rxKappa, cy + ry, cx - rx, cy + ryKappa, cx - rx, cy);
152     pImpl->cubicTo(cx - rx, cy - ryKappa, cx - rxKappa, cy - ry, cx, cy - ry);
153     pImpl->cubicTo(cx + rxKappa, cy - ry, cx + rx, cy - ryKappa, cx + rx, cy);
154     pImpl->close();
155 
156     pImpl->flag |= RenderUpdateFlag::Path;
157 
158     return Result::Success;
159 }
160 
161 
appendArc(float cx,float cy,float radius,float startAngle,float sweep,bool pie)162 Result Shape::appendArc(float cx, float cy, float radius, float startAngle, float sweep, bool pie) noexcept
163 {
164     //just circle
165     if (sweep >= 360.0f || sweep <= -360.0f) return appendCircle(cx, cy, radius, radius);
166 
167     const float arcPrecision = 1e-5f;
168     startAngle = deg2rad(startAngle);
169     sweep = deg2rad(sweep);
170 
171     auto nCurves = static_cast<int>(fabsf(sweep / MATH_PI2));
172     if (fabsf(sweep / MATH_PI2) - nCurves > arcPrecision) ++nCurves;
173     auto sweepSign = (sweep < 0 ? -1 : 1);
174     auto fract = fmodf(sweep, MATH_PI2);
175     fract = (fabsf(fract) < arcPrecision) ? MATH_PI2 * sweepSign : fract;
176 
177     //Start from here
178     Point start = {radius * cosf(startAngle), radius * sinf(startAngle)};
179 
180     if (pie) {
181         pImpl->moveTo(cx, cy);
182         pImpl->lineTo(start.x + cx, start.y + cy);
183     } else {
184         pImpl->moveTo(start.x + cx, start.y + cy);
185     }
186 
187     for (int i = 0; i < nCurves; ++i) {
188         auto endAngle = startAngle + ((i != nCurves - 1) ? MATH_PI2 * sweepSign : fract);
189         Point end = {radius * cosf(endAngle), radius * sinf(endAngle)};
190 
191         //variables needed to calculate bezier control points
192 
193         //get bezier control points using article:
194         //(http://itc.ktu.lt/index.php/ITC/article/view/11812/6479)
195         auto ax = start.x;
196         auto ay = start.y;
197         auto bx = end.x;
198         auto by = end.y;
199         auto q1 = ax * ax + ay * ay;
200         auto q2 = ax * bx + ay * by + q1;
201         auto k2 = (4.0f/3.0f) * ((sqrtf(2 * q1 * q2) - q2) / (ax * by - ay * bx));
202 
203         start = end; //Next start point is the current end point
204 
205         end.x += cx;
206         end.y += cy;
207 
208         Point ctrl1 = {ax - k2 * ay + cx, ay + k2 * ax + cy};
209         Point ctrl2 = {bx + k2 * by + cx, by - k2 * bx + cy};
210 
211         pImpl->cubicTo(ctrl1.x, ctrl1.y, ctrl2.x, ctrl2.y, end.x, end.y);
212 
213         startAngle = endAngle;
214     }
215 
216     if (pie) pImpl->close();
217 
218     pImpl->flag |= RenderUpdateFlag::Path;
219 
220     return Result::Success;
221 }
222 
223 
appendRect(float x,float y,float w,float h,float rx,float ry)224 Result Shape::appendRect(float x, float y, float w, float h, float rx, float ry) noexcept
225 {
226     auto halfW = w * 0.5f;
227     auto halfH = h * 0.5f;
228 
229     //clamping cornerRadius by minimum size
230     if (rx > halfW) rx = halfW;
231     if (ry > halfH) ry = halfH;
232 
233     //rectangle
234     if (rx == 0 && ry == 0) {
235         pImpl->grow(5, 4);
236         pImpl->moveTo(x, y);
237         pImpl->lineTo(x + w, y);
238         pImpl->lineTo(x + w, y + h);
239         pImpl->lineTo(x, y + h);
240         pImpl->close();
241     //rounded rectangle or circle
242     } else {
243         auto hrx = rx * PATH_KAPPA;
244         auto hry = ry * PATH_KAPPA;
245         pImpl->grow(10, 17);
246         pImpl->moveTo(x + rx, y);
247         pImpl->lineTo(x + w - rx, y);
248         pImpl->cubicTo(x + w - rx + hrx, y, x + w, y + ry - hry, x + w, y + ry);
249         pImpl->lineTo(x + w, y + h - ry);
250         pImpl->cubicTo(x + w, y + h - ry + hry, x + w - rx + hrx, y + h, x + w - rx, y + h);
251         pImpl->lineTo(x + rx, y + h);
252         pImpl->cubicTo(x + rx - hrx, y + h, x, y + h - ry + hry, x, y + h - ry);
253         pImpl->lineTo(x, y + ry);
254         pImpl->cubicTo(x, y + ry - hry, x + rx - hrx, y, x + rx, y);
255         pImpl->close();
256     }
257 
258     pImpl->flag |= RenderUpdateFlag::Path;
259 
260     return Result::Success;
261 }
262 
263 
fill(uint8_t r,uint8_t g,uint8_t b,uint8_t a)264 Result Shape::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
265 {
266     if (pImpl->rs.fill) {
267         delete(pImpl->rs.fill);
268         pImpl->rs.fill = nullptr;
269         pImpl->flag |= RenderUpdateFlag::Gradient;
270     }
271 
272     if (r == pImpl->rs.color[0] && g == pImpl->rs.color[1] && b == pImpl->rs.color[2] && a == pImpl->rs.color[3]) return Result::Success;
273 
274     pImpl->rs.color[0] = r;
275     pImpl->rs.color[1] = g;
276     pImpl->rs.color[2] = b;
277     pImpl->rs.color[3] = a;
278     pImpl->flag |= RenderUpdateFlag::Color;
279 
280     return Result::Success;
281 }
282 
283 
fill(unique_ptr<Fill> f)284 Result Shape::fill(unique_ptr<Fill> f) noexcept
285 {
286     auto p = f.release();
287     if (!p) return Result::MemoryCorruption;
288 
289     if (pImpl->rs.fill && pImpl->rs.fill != p) delete(pImpl->rs.fill);
290     pImpl->rs.fill = p;
291     pImpl->flag |= RenderUpdateFlag::Gradient;
292 
293     return Result::Success;
294 }
295 
296 
fillColor(uint8_t * r,uint8_t * g,uint8_t * b,uint8_t * a) const297 Result Shape::fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept
298 {
299     pImpl->rs.fillColor(r, g, b, a);
300 
301     return Result::Success;
302 }
303 
304 
fill() const305 const Fill* Shape::fill() const noexcept
306 {
307     return pImpl->rs.fill;
308 }
309 
310 
order(bool strokeFirst)311 Result Shape::order(bool strokeFirst) noexcept
312 {
313     pImpl->strokeFirst(strokeFirst);
314     return Result::Success;
315 }
316 
317 
stroke(float width)318 Result Shape::stroke(float width) noexcept
319 {
320     pImpl->strokeWidth(width);
321     return Result::Success;
322 }
323 
324 
strokeWidth() const325 float Shape::strokeWidth() const noexcept
326 {
327     return pImpl->rs.strokeWidth();
328 }
329 
330 
stroke(uint8_t r,uint8_t g,uint8_t b,uint8_t a)331 Result Shape::stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
332 {
333     pImpl->strokeColor(r, g, b, a);
334     return Result::Success;
335 }
336 
337 
strokeColor(uint8_t * r,uint8_t * g,uint8_t * b,uint8_t * a) const338 Result Shape::strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept
339 {
340     if (!pImpl->rs.strokeColor(r, g, b, a)) return Result::InsufficientCondition;
341 
342     return Result::Success;
343 }
344 
345 
stroke(unique_ptr<Fill> f)346 Result Shape::stroke(unique_ptr<Fill> f) noexcept
347 {
348     return pImpl->strokeFill(std::move(f));
349 }
350 
351 
strokeFill() const352 const Fill* Shape::strokeFill() const noexcept
353 {
354     return pImpl->rs.strokeFill();
355 }
356 
357 
stroke(const float * dashPattern,uint32_t cnt)358 Result Shape::stroke(const float* dashPattern, uint32_t cnt) noexcept
359 {
360     return pImpl->strokeDash(dashPattern, cnt, 0);
361 }
362 
363 
strokeDash(const float ** dashPattern) const364 uint32_t Shape::strokeDash(const float** dashPattern) const noexcept
365 {
366     return pImpl->rs.strokeDash(dashPattern, nullptr);
367 }
368 
369 
stroke(StrokeCap cap)370 Result Shape::stroke(StrokeCap cap) noexcept
371 {
372     pImpl->strokeCap(cap);
373     return Result::Success;
374 }
375 
376 
stroke(StrokeJoin join)377 Result Shape::stroke(StrokeJoin join) noexcept
378 {
379     pImpl->strokeJoin(join);
380     return Result::Success;
381 }
382 
383 
strokeMiterlimit(float miterlimit)384 Result Shape::strokeMiterlimit(float miterlimit) noexcept
385 {
386     // https://www.w3.org/TR/SVG2/painting.html#LineJoin
387     // - A negative value for stroke-miterlimit must be treated as an illegal value.
388     if (miterlimit < 0.0f) return Result::InvalidArguments;
389     // TODO Find out a reasonable max value.
390     pImpl->strokeMiterlimit(miterlimit);
391     return Result::Success;
392 }
393 
394 
strokeCap() const395 StrokeCap Shape::strokeCap() const noexcept
396 {
397     return pImpl->rs.strokeCap();
398 }
399 
400 
strokeJoin() const401 StrokeJoin Shape::strokeJoin() const noexcept
402 {
403     return pImpl->rs.strokeJoin();
404 }
405 
406 
strokeMiterlimit() const407 float Shape::strokeMiterlimit() const noexcept
408 {
409     return pImpl->rs.strokeMiterlimit();
410 }
411 
412 
strokeTrim(float begin,float end,bool simultaneous)413 Result Shape::strokeTrim(float begin, float end, bool simultaneous) noexcept
414 {
415     pImpl->strokeTrim(begin, end, simultaneous);
416     return Result::Success;
417 }
418 
419 
fill(FillRule r)420 Result Shape::fill(FillRule r) noexcept
421 {
422     pImpl->rs.rule = r;
423 
424     return Result::Success;
425 }
426 
427 
fillRule() const428 FillRule Shape::fillRule() const noexcept
429 {
430     return pImpl->rs.rule;
431 }
432 
433 #endif /* LV_USE_THORVG_INTERNAL */
434 
435