example: just renamed the svg file.
[platform/core/graphics/tizenvg.git] / src / lib / tvgShape.cpp
1 /*
2  * Copyright (c) 2020 - 2022 Samsung Electronics Co., Ltd. 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(this))
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->path.reset();
63     pImpl->flag = RenderUpdateFlag::Path;
64
65     return Result::Success;
66 }
67
68
69 uint32_t Shape::pathCommands(const PathCommand** cmds) const noexcept
70 {
71     if (!cmds) return 0;
72
73     *cmds = pImpl->path.cmds;
74
75     return pImpl->path.cmdCnt;
76 }
77
78
79 uint32_t Shape::pathCoords(const Point** pts) const noexcept
80 {
81     if (!pts) return 0;
82
83     *pts = pImpl->path.pts;
84
85     return pImpl->path.ptsCnt;
86 }
87
88
89 Result Shape::appendPath(const PathCommand *cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept
90 {
91     if (cmdCnt == 0 || ptsCnt == 0 || !cmds || !pts) return Result::InvalidArguments;
92
93     pImpl->path.grow(cmdCnt, ptsCnt);
94     pImpl->path.append(cmds, cmdCnt, pts, ptsCnt);
95
96     pImpl->flag |= RenderUpdateFlag::Path;
97
98     return Result::Success;
99 }
100
101
102 Result Shape::moveTo(float x, float y) noexcept
103 {
104     pImpl->path.moveTo(x, y);
105
106     pImpl->flag |= RenderUpdateFlag::Path;
107
108     return Result::Success;
109 }
110
111
112 Result Shape::lineTo(float x, float y) noexcept
113 {
114     pImpl->path.lineTo(x, y);
115
116     pImpl->flag |= RenderUpdateFlag::Path;
117
118     return Result::Success;
119 }
120
121
122 Result Shape::cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept
123 {
124     pImpl->path.cubicTo(cx1, cy1, cx2, cy2, x, y);
125
126     pImpl->flag |= RenderUpdateFlag::Path;
127
128     return Result::Success;
129 }
130
131
132 Result Shape::close() noexcept
133 {
134     pImpl->path.close();
135
136     pImpl->flag |= RenderUpdateFlag::Path;
137
138     return Result::Success;
139 }
140
141
142 Result Shape::appendCircle(float cx, float cy, float rx, float ry) noexcept
143 {
144     auto rxKappa = rx * PATH_KAPPA;
145     auto ryKappa = ry * PATH_KAPPA;
146
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);
153     pImpl->path.close();
154
155     pImpl->flag |= RenderUpdateFlag::Path;
156
157     return Result::Success;
158 }
159
160 Result Shape::appendArc(float cx, float cy, float radius, float startAngle, float sweep, bool pie) noexcept
161 {
162     //just circle
163     if (sweep >= 360.0f || sweep <= -360.0f) return appendCircle(cx, cy, radius, radius);
164
165     startAngle = (startAngle * M_PI) / 180.0f;
166     sweep = sweep * M_PI / 180.0f;
167
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;
172
173     //Start from here
174     Point start = {radius * cosf(startAngle), radius * sinf(startAngle)};
175
176     if (pie) {
177         pImpl->path.moveTo(cx, cy);
178         pImpl->path.lineTo(start.x + cx, start.y + cy);
179     } else {
180         pImpl->path.moveTo(start.x + cx, start.y + cy);
181     }
182
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)};
186
187         //variables needed to calculate bezier control points
188
189         //get bezier control points using article:
190         //(http://itc.ktu.lt/index.php/ITC/article/view/11812/6479)
191         auto ax = start.x;
192         auto ay = start.y;
193         auto bx = end.x;
194         auto by = end.y;
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));
198
199         start = end; //Next start point is the current end point
200
201         end.x += cx;
202         end.y += cy;
203
204         Point ctrl1 = {ax - k2 * ay + cx, ay + k2 * ax + cy};
205         Point ctrl2 = {bx + k2 * by + cx, by - k2 * bx + cy};
206
207         pImpl->path.cubicTo(ctrl1.x, ctrl1.y, ctrl2.x, ctrl2.y, end.x, end.y);
208
209         startAngle = endAngle;
210     }
211
212     if (pie) pImpl->path.close();
213
214     pImpl->flag |= RenderUpdateFlag::Path;
215
216     return Result::Success;
217 }
218
219
220 Result Shape::appendRect(float x, float y, float w, float h, float rx, float ry) noexcept
221 {
222     auto halfW = w * 0.5f;
223     auto halfH = h * 0.5f;
224
225     //clamping cornerRadius by minimum size
226     if (rx > halfW) rx = halfW;
227     if (ry > halfH) ry = halfH;
228
229     //rectangle
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);
236         pImpl->path.close();
237     //circle
238     } else if (mathEqual(rx, halfW) && mathEqual(ry, halfH)) {
239         return appendCircle(x + (w * 0.5f), y + (h * 0.5f), rx, ry);
240     } else {
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);
253         pImpl->path.close();
254     }
255
256     pImpl->flag |= RenderUpdateFlag::Path;
257
258     return Result::Success;
259 }
260
261
262 Result Shape::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
263 {
264     pImpl->color[0] = r;
265     pImpl->color[1] = g;
266     pImpl->color[2] = b;
267     pImpl->color[3] = a;
268     pImpl->flag |= RenderUpdateFlag::Color;
269
270     if (pImpl->fill) {
271         delete(pImpl->fill);
272         pImpl->fill = nullptr;
273         pImpl->flag |= RenderUpdateFlag::Gradient;
274     }
275
276     return Result::Success;
277 }
278
279
280 Result Shape::fill(unique_ptr<Fill> f) noexcept
281 {
282     auto p = f.release();
283     if (!p) return Result::MemoryCorruption;
284
285     if (pImpl->fill && pImpl->fill != p) delete(pImpl->fill);
286     pImpl->fill = p;
287     pImpl->flag |= RenderUpdateFlag::Gradient;
288
289     return Result::Success;
290 }
291
292
293 Result Shape::fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept
294 {
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];
299
300     return Result::Success;
301 }
302
303 const Fill* Shape::fill() const noexcept
304 {
305     return pImpl->fill;
306 }
307
308
309 Result Shape::stroke(float width) noexcept
310 {
311     if (!pImpl->strokeWidth(width)) return Result::FailedAllocation;
312
313     return Result::Success;
314 }
315
316
317 float Shape::strokeWidth() const noexcept
318 {
319     if (!pImpl->stroke) return 0;
320     return pImpl->stroke->width;
321 }
322
323
324 Result Shape::stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
325 {
326     if (!pImpl->strokeColor(r, g, b, a)) return Result::FailedAllocation;
327
328     return Result::Success;
329 }
330
331
332 Result Shape::strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept
333 {
334     if (!pImpl->stroke) return Result::InsufficientCondition;
335
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];
340
341     return Result::Success;
342 }
343
344
345 Result Shape::stroke(unique_ptr<Fill> f) noexcept
346 {
347     return pImpl->strokeFill(move(f));
348 }
349
350
351 const Fill* Shape::strokeFill() const noexcept
352 {
353     if (!pImpl->stroke) return nullptr;
354
355     return pImpl->stroke->fill;
356 }
357
358
359 Result Shape::stroke(const float* dashPattern, uint32_t cnt) noexcept
360 {
361     if ((cnt == 1) || (!dashPattern && cnt > 0) || (dashPattern && cnt == 0)) {
362         return Result::InvalidArguments;
363     }
364
365     for (uint32_t i = 0; i < cnt; i++)
366         if (dashPattern[i] < FLT_EPSILON) return Result::InvalidArguments;
367
368     if (!pImpl->strokeDash(dashPattern, cnt)) return Result::FailedAllocation;
369
370     return Result::Success;
371 }
372
373
374 uint32_t Shape::strokeDash(const float** dashPattern) const noexcept
375 {
376     if (!pImpl->stroke) return 0;
377
378     if (dashPattern) *dashPattern = pImpl->stroke->dashPattern;
379
380     return pImpl->stroke->dashCnt;
381 }
382
383
384 Result Shape::stroke(StrokeCap cap) noexcept
385 {
386     if (!pImpl->strokeCap(cap)) return Result::FailedAllocation;
387
388     return Result::Success;
389 }
390
391
392 Result Shape::stroke(StrokeJoin join) noexcept
393 {
394     if (!pImpl->strokeJoin(join)) return Result::FailedAllocation;
395
396     return Result::Success;
397 }
398
399
400 StrokeCap Shape::strokeCap() const noexcept
401 {
402     if (!pImpl->stroke) return StrokeCap::Square;
403
404     return pImpl->stroke->cap;
405 }
406
407
408 StrokeJoin Shape::strokeJoin() const noexcept
409 {
410     if (!pImpl->stroke) return StrokeJoin::Bevel;
411
412     return pImpl->stroke->join;
413 }
414
415
416 Result Shape::fill(FillRule r) noexcept
417 {
418     pImpl->rule = r;
419
420     return Result::Success;
421 }
422
423
424 FillRule Shape::fillRule() const noexcept
425 {
426     return pImpl->rule;
427 }