common shape: code refactoring & data optimization.
[platform/core/graphics/tizenvg.git] / src / lib / tvgShape.cpp
1 /*
2  * Copyright (c) 2020 - 2023 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 "tvgMath.h"
24 #include "tvgShapeImpl.h"
25
26 /************************************************************************/
27 /* Internal Class Implementation                                        */
28 /************************************************************************/
29 constexpr auto PATH_KAPPA = 0.552284f;
30
31 /************************************************************************/
32 /* External Class Implementation                                        */
33 /************************************************************************/
34
35 Shape :: Shape() : pImpl(new Impl())
36 {
37     Paint::pImpl->id = TVG_CLASS_ID_SHAPE;
38     Paint::pImpl->method(new PaintMethod<Shape::Impl>(pImpl));
39 }
40
41
42 Shape :: ~Shape()
43 {
44     delete(pImpl);
45 }
46
47
48 unique_ptr<Shape> Shape::gen() noexcept
49 {
50     return unique_ptr<Shape>(new Shape);
51 }
52
53
54 uint32_t Shape::identifier() noexcept
55 {
56     return TVG_CLASS_ID_SHAPE;
57 }
58
59
60 Result Shape::reset() noexcept
61 {
62     pImpl->reset();
63
64     return Result::Success;
65 }
66
67
68 uint32_t Shape::pathCommands(const PathCommand** cmds) const noexcept
69 {
70     if (!cmds) return 0;
71
72     *cmds = pImpl->rs.path.cmds;
73
74     return pImpl->rs.path.cmdCnt;
75 }
76
77
78 uint32_t Shape::pathCoords(const Point** pts) const noexcept
79 {
80     if (!pts) return 0;
81
82     *pts = pImpl->rs.path.pts;
83
84     return pImpl->rs.path.ptsCnt;
85 }
86
87
88 Result Shape::appendPath(const PathCommand *cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept
89 {
90     if (cmdCnt == 0 || ptsCnt == 0 || !cmds || !pts) return Result::InvalidArguments;
91
92     pImpl->grow(cmdCnt, ptsCnt);
93     pImpl->append(cmds, cmdCnt, pts, ptsCnt);
94
95     return Result::Success;
96 }
97
98
99 Result Shape::moveTo(float x, float y) noexcept
100 {
101     pImpl->moveTo(x, y);
102
103     return Result::Success;
104 }
105
106
107 Result Shape::lineTo(float x, float y) noexcept
108 {
109     pImpl->lineTo(x, y);
110
111     return Result::Success;
112 }
113
114
115 Result Shape::cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept
116 {
117     pImpl->cubicTo(cx1, cy1, cx2, cy2, x, y);
118
119     return Result::Success;
120 }
121
122
123 Result Shape::close() noexcept
124 {
125     pImpl->close();
126
127     return Result::Success;
128 }
129
130
131 Result Shape::appendCircle(float cx, float cy, float rx, float ry) noexcept
132 {
133     auto rxKappa = rx * PATH_KAPPA;
134     auto ryKappa = ry * PATH_KAPPA;
135
136     pImpl->grow(6, 13);
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);
142     pImpl->close();
143
144     return Result::Success;
145 }
146
147 Result Shape::appendArc(float cx, float cy, float radius, float startAngle, float sweep, bool pie) noexcept
148 {
149     //just circle
150     if (sweep >= 360.0f || sweep <= -360.0f) return appendCircle(cx, cy, radius, radius);
151
152     startAngle = (startAngle * M_PI) / 180.0f;
153     sweep = sweep * M_PI / 180.0f;
154
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;
159
160     //Start from here
161     Point start = {radius * cosf(startAngle), radius * sinf(startAngle)};
162
163     if (pie) {
164         pImpl->moveTo(cx, cy);
165         pImpl->lineTo(start.x + cx, start.y + cy);
166     } else {
167         pImpl->moveTo(start.x + cx, start.y + cy);
168     }
169
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)};
173
174         //variables needed to calculate bezier control points
175
176         //get bezier control points using article:
177         //(http://itc.ktu.lt/index.php/ITC/article/view/11812/6479)
178         auto ax = start.x;
179         auto ay = start.y;
180         auto bx = end.x;
181         auto by = end.y;
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));
185
186         start = end; //Next start point is the current end point
187
188         end.x += cx;
189         end.y += cy;
190
191         Point ctrl1 = {ax - k2 * ay + cx, ay + k2 * ax + cy};
192         Point ctrl2 = {bx + k2 * by + cx, by - k2 * bx + cy};
193
194         pImpl->cubicTo(ctrl1.x, ctrl1.y, ctrl2.x, ctrl2.y, end.x, end.y);
195
196         startAngle = endAngle;
197     }
198
199     if (pie) pImpl->close();
200
201     return Result::Success;
202 }
203
204
205 Result Shape::appendRect(float x, float y, float w, float h, float rx, float ry) noexcept
206 {
207     auto halfW = w * 0.5f;
208     auto halfH = h * 0.5f;
209
210     //clamping cornerRadius by minimum size
211     if (rx > halfW) rx = halfW;
212     if (ry > halfH) ry = halfH;
213
214     //rectangle
215     if (rx == 0 && ry == 0) {
216         pImpl->grow(5, 4);
217         pImpl->moveTo(x, y);
218         pImpl->lineTo(x + w, y);
219         pImpl->lineTo(x + w, y + h);
220         pImpl->lineTo(x, y + h);
221         pImpl->close();
222     //circle
223     } else if (mathEqual(rx, halfW) && mathEqual(ry, halfH)) {
224         return appendCircle(x + (w * 0.5f), y + (h * 0.5f), rx, ry);
225     } else {
226         auto hrx = rx * 0.5f;
227         auto hry = ry * 0.5f;
228         pImpl->grow(10, 17);
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);
238         pImpl->close();
239     }
240
241     return Result::Success;
242 }
243
244
245 Result Shape::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
246 {
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;
252
253     if (pImpl->rs.fill) {
254         delete(pImpl->rs.fill);
255         pImpl->rs.fill = nullptr;
256         pImpl->flag |= RenderUpdateFlag::Gradient;
257     }
258
259     return Result::Success;
260 }
261
262
263 Result Shape::fill(unique_ptr<Fill> f) noexcept
264 {
265     auto p = f.release();
266     if (!p) return Result::MemoryCorruption;
267
268     if (pImpl->rs.fill && pImpl->rs.fill != p) delete(pImpl->rs.fill);
269     pImpl->rs.fill = p;
270     pImpl->flag |= RenderUpdateFlag::Gradient;
271
272     return Result::Success;
273 }
274
275
276 Result Shape::fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept
277 {
278     pImpl->rs.fillColor(r, g, b, a);
279
280     return Result::Success;
281 }
282
283
284 const Fill* Shape::fill() const noexcept
285 {
286     return pImpl->rs.fill;
287 }
288
289
290 Result Shape::stroke(float width) noexcept
291 {
292     if (!pImpl->strokeWidth(width)) return Result::FailedAllocation;
293
294     return Result::Success;
295 }
296
297
298 float Shape::strokeWidth() const noexcept
299 {
300     return pImpl->rs.strokeWidth();
301 }
302
303
304 Result Shape::stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
305 {
306     if (!pImpl->strokeColor(r, g, b, a)) return Result::FailedAllocation;
307
308     return Result::Success;
309 }
310
311
312 Result Shape::strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept
313 {
314     if (!pImpl->rs.strokeColor(r, g, b, a)) return Result::InsufficientCondition;
315
316     return Result::Success;
317 }
318
319
320 Result Shape::stroke(unique_ptr<Fill> f) noexcept
321 {
322     return pImpl->strokeFill(move(f));
323 }
324
325
326 const Fill* Shape::strokeFill() const noexcept
327 {
328     return pImpl->rs.strokeFill();
329 }
330
331
332 Result Shape::stroke(const float* dashPattern, uint32_t cnt) noexcept
333 {
334     if ((cnt == 1) || (!dashPattern && cnt > 0) || (dashPattern && cnt == 0)) {
335         return Result::InvalidArguments;
336     }
337
338     for (uint32_t i = 0; i < cnt; i++)
339         if (dashPattern[i] < FLT_EPSILON) return Result::InvalidArguments;
340
341     if (!pImpl->strokeDash(dashPattern, cnt)) return Result::FailedAllocation;
342
343     return Result::Success;
344 }
345
346
347 uint32_t Shape::strokeDash(const float** dashPattern) const noexcept
348 {
349     return pImpl->rs.strokeDash(dashPattern);
350 }
351
352
353 Result Shape::stroke(StrokeCap cap) noexcept
354 {
355     if (!pImpl->strokeCap(cap)) return Result::FailedAllocation;
356
357     return Result::Success;
358 }
359
360
361 Result Shape::stroke(StrokeJoin join) noexcept
362 {
363     if (!pImpl->strokeJoin(join)) return Result::FailedAllocation;
364
365     return Result::Success;
366 }
367
368
369 StrokeCap Shape::strokeCap() const noexcept
370 {
371     return pImpl->rs.strokeCap();
372 }
373
374
375 StrokeJoin Shape::strokeJoin() const noexcept
376 {
377     return pImpl->rs.strokeJoin();
378 }
379
380
381 Result Shape::fill(FillRule r) noexcept
382 {
383     pImpl->rs.rule = r;
384
385     return Result::Success;
386 }
387
388
389 FillRule Shape::fillRule() const noexcept
390 {
391     return pImpl->rs.rule;
392 }