Debian Codegraph @

thirdparty/thorvg/src/renderer/tvgShape.cpp

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