Debian Codegraph @

thirdparty/thorvg/src/renderer/sw_engine/tvgSwShape.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 "tvgSwCommon.h"
24 #include "tvgMath.h"
25
26 /************************************************************************/
27 /* Internal Class Implementation */
28 /************************************************************************/
29
30 static bool _outlineBegin(SwOutline& outline)
31 {
32 //Make a contour if lineTo/curveTo without calling close or moveTo beforehand.
33 if (outline.pts.empty()) return false;
34 outline.cntrs.push(outline.pts.count - 1);
35 outline.closed.push(false);
36 outline.pts.push(outline.pts[outline.cntrs.last()]);
37 outline.types.push(SW_CURVE_TYPE_POINT);
38 return false;
39 }
40
41
42 static bool _outlineEnd(SwOutline& outline)
43 {
44 if (outline.pts.empty()) return false;
45 outline.cntrs.push(outline.pts.count - 1);
46 outline.closed.push(false);
47 return false;
48 }
49
50
51 static bool _outlineMoveTo(SwOutline& outline, const Point* to, const Matrix& transform, bool closed = false)
52 {
53 //make it a contour, if the last contour is not closed yet.
54 if (!closed) _outlineEnd(outline);
55
56 outline.pts.push(mathTransform(to, transform));
57 outline.types.push(SW_CURVE_TYPE_POINT);
58 return false;
59 }
60
61
62 static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix& transform)
63 {
64 outline.pts.push(mathTransform(to, transform));
65 outline.types.push(SW_CURVE_TYPE_POINT);
66 }
67
68
69 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix& transform)
70 {
71 outline.pts.push(mathTransform(ctrl1, transform));
72 outline.types.push(SW_CURVE_TYPE_CUBIC);
73
74 outline.pts.push(mathTransform(ctrl2, transform));
75 outline.types.push(SW_CURVE_TYPE_CUBIC);
76
77 outline.pts.push(mathTransform(to, transform));
78 outline.types.push(SW_CURVE_TYPE_POINT);
79 }
80
81
82 static bool _outlineClose(SwOutline& outline)
83 {
84 uint32_t i;
85 if (outline.cntrs.count > 0) i = outline.cntrs.last() + 1;
86 else i = 0;
87
88 //Make sure there is at least one point in the current path
89 if (outline.pts.count == i) return false;
90
91 //Close the path
92 outline.pts.push(outline.pts[i]);
93 outline.cntrs.push(outline.pts.count - 1);
94 outline.types.push(SW_CURVE_TYPE_POINT);
95 outline.closed.push(true);
96
97 return true;
98 }
99
100
101 static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix& transform)
102 {
103 Line cur = {dash.ptCur, *to};
104 auto len = cur.length();
105 if (tvg::zero(len)) {
106 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
107 //draw the current line fully
108 } else if (len <= dash.curLen) {
109 dash.curLen -= len;
110 if (!dash.curOpGap) {
111 if (dash.move) {
112 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
113 dash.move = false;
114 }
115 _outlineLineTo(*dash.outline, to, transform);
116 }
117 //draw the current line partially
118 } else {
119 while (len - dash.curLen > DASH_PATTERN_THRESHOLD) {
120 Line left, right;
121 if (dash.curLen > 0) {
122 len -= dash.curLen;
123 cur.split(dash.curLen, left, right);
124 if (!dash.curOpGap) {
125 if (dash.move || dash.pattern[dash.curIdx] - dash.curLen < FLOAT_EPSILON) {
126 _outlineMoveTo(*dash.outline, &left.pt1, transform);
127 dash.move = false;
128 }
129 _outlineLineTo(*dash.outline, &left.pt2, transform);
130 }
131 } else {
132 right = cur;
133 }
134 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
135 dash.curLen = dash.pattern[dash.curIdx];
136 dash.curOpGap = !dash.curOpGap;
137 cur = right;
138 dash.ptCur = cur.pt1;
139 dash.move = true;
140 }
141 //leftovers
142 dash.curLen -= len;
143 if (!dash.curOpGap) {
144 if (dash.move) {
145 _outlineMoveTo(*dash.outline, &cur.pt1, transform);
146 dash.move = false;
147 }
148 _outlineLineTo(*dash.outline, &cur.pt2, transform);
149 }
150 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
151 //move to next dash
152 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
153 dash.curLen = dash.pattern[dash.curIdx];
154 dash.curOpGap = !dash.curOpGap;
155 }
156 }
157 dash.ptCur = *to;
158 }
159
160
161 static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix& transform)
162 {
163 Bezier cur = {dash.ptCur, *ctrl1, *ctrl2, *to};
164 auto len = cur.length();
165
166 //draw the current line fully
167 if (tvg::zero(len)) {
168 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
169 } else if (len <= dash.curLen) {
170 dash.curLen -= len;
171 if (!dash.curOpGap) {
172 if (dash.move) {
173 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
174 dash.move = false;
175 }
176 _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to, transform);
177 }
178 //draw the current line partially
179 } else {
180 while ((len - dash.curLen) > DASH_PATTERN_THRESHOLD) {
181 Bezier left, right;
182 if (dash.curLen > 0) {
183 len -= dash.curLen;
184 cur.split(dash.curLen, left, right);
185 if (!dash.curOpGap) {
186 if (dash.move || dash.pattern[dash.curIdx] - dash.curLen < FLOAT_EPSILON) {
187 _outlineMoveTo(*dash.outline, &left.start, transform);
188 dash.move = false;
189 }
190 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform);
191 }
192 } else {
193 right = cur;
194 }
195 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
196 dash.curLen = dash.pattern[dash.curIdx];
197 dash.curOpGap = !dash.curOpGap;
198 cur = right;
199 dash.ptCur = right.start;
200 dash.move = true;
201 }
202 //leftovers
203 dash.curLen -= len;
204 if (!dash.curOpGap) {
205 if (dash.move) {
206 _outlineMoveTo(*dash.outline, &cur.start, transform);
207 dash.move = false;
208 }
209 _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform);
210 }
211 if (dash.curLen < 0.1f && TO_SWCOORD(len) > 1) {
212 //move to next dash
213 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
214 dash.curLen = dash.pattern[dash.curIdx];
215 dash.curOpGap = !dash.curOpGap;
216 }
217 }
218 dash.ptCur = *to;
219 }
220
221
222 static void _dashClose(SwDashStroke& dash, const Matrix& transform)
223 {
224 _dashLineTo(dash, &dash.ptStart, transform);
225 }
226
227
228 static void _dashMoveTo(SwDashStroke& dash, const Point* pts)
229 {
230 dash.ptCur = *pts;
231 dash.ptStart = *pts;
232 dash.move = true;
233 }
234
235
236 static void _dashMoveTo(SwDashStroke& dash, uint32_t offIdx, float offset, const Point* pts)
237 {
238 dash.curIdx = offIdx % dash.cnt;
239 dash.curLen = dash.pattern[dash.curIdx] - offset;
240 dash.curOpGap = offIdx % 2;
241 dash.ptStart = dash.ptCur = *pts;
242 dash.move = true;
243 }
244
245
246 static void _trimPattern(SwDashStroke* dash, const RenderShape* rshape, float length, float trimBegin, float trimEnd)
247 {
248 auto begin = length * trimBegin;
249 auto end = length * trimEnd;
250
251 //default
252 if (end > begin) {
253 if (begin > 0.0f) dash->cnt = 4;
254 else dash->cnt = 2;
255 //looping
256 } else dash->cnt = 3;
257
258 if (dash->cnt == 2) {
259 dash->pattern[0] = end - begin;
260 dash->pattern[1] = length - (end - begin);
261 } else if (dash->cnt == 3) {
262 dash->pattern[0] = end;
263 dash->pattern[1] = (begin - end);
264 dash->pattern[2] = length - begin;
265 } else {
266 dash->pattern[0] = 0; //zero dash to start with a space.
267 dash->pattern[1] = begin;
268 dash->pattern[2] = end - begin;
269 dash->pattern[3] = length - end;
270 }
271 }
272
273
274 static float _outlineLength(const RenderShape* rshape, uint32_t shiftPts, uint32_t shiftCmds, bool subpath)
275 {
276 const PathCommand* cmds = rshape->path.cmds.data + shiftCmds;
277 auto cmdCnt = rshape->path.cmds.count - shiftCmds;
278 const Point* pts = rshape->path.pts.data + shiftPts;
279 auto ptsCnt = rshape->path.pts.count - shiftPts;
280
281 //No actual shape data
282 if (cmdCnt <= 0 || ptsCnt <= 0) return 0.0f;
283
284 const Point* close = nullptr;
285 auto len = 0.0f;
286
287 //must begin with moveTo
288 if (cmds[0] == PathCommand::MoveTo) {
289 close = pts;
290 cmds++;
291 pts++;
292 cmdCnt--;
293 }
294
295 while (cmdCnt-- > 0) {
296 switch (*cmds) {
297 case PathCommand::Close: {
298 len += length(pts - 1, close);
299 if (subpath) return len;
300 break;
301 }
302 case PathCommand::MoveTo: {
303 if (subpath) return len;
304 close = pts;
305 ++pts;
306 break;
307 }
308 case PathCommand::LineTo: {
309 len += length(pts - 1, pts);
310 ++pts;
311 break;
312 }
313 case PathCommand::CubicTo: {
314 len += Bezier{*(pts - 1), *pts, *(pts + 1), *(pts + 2)}.length();
315 pts += 3;
316 break;
317 }
318 }
319 ++cmds;
320 }
321 return len;
322 }
323
324
325 static SwOutline* _genDashOutline(const RenderShape* rshape, const Matrix& transform, bool trimmed, SwMpool* mpool, unsigned tid)
326 {
327 const PathCommand* cmds = rshape->path.cmds.data;
328 auto cmdCnt = rshape->path.cmds.count;
329 const Point* pts = rshape->path.pts.data;
330 auto ptsCnt = rshape->path.pts.count;
331
332 //No actual shape data
333 if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
334
335 auto startPts = pts;
336 auto startCmds = cmds;
337
338 SwDashStroke dash;
339 auto offset = 0.0f;
340 dash.cnt = rshape->strokeDash((const float**)&dash.pattern, &offset);
341 auto simultaneous = rshape->stroke->trim.simultaneous;
342 float trimBegin = 0.0f, trimEnd = 1.0f;
343 if (trimmed) rshape->stroke->strokeTrim(trimBegin, trimEnd);
344
345 if (dash.cnt == 0) {
346 if (trimmed) dash.pattern = (float*)malloc(sizeof(float) * 4);
347 else return nullptr;
348 } else {
349 //TODO: handle dash + trim - for now trimming ignoring is forced
350 trimmed = false;
351 }
352
353 //offset
354 auto patternLength = 0.0f;
355 uint32_t offIdx = 0;
356 if (!tvg::zero(offset)) {
357 for (size_t i = 0; i < dash.cnt; ++i) patternLength += dash.pattern[i];
358 bool isOdd = dash.cnt % 2;
359 if (isOdd) patternLength *= 2;
360
361 offset = fmodf(offset, patternLength);
362 if (offset < 0) offset += patternLength;
363
364 for (size_t i = 0; i < dash.cnt * (1 + (size_t)isOdd); ++i, ++offIdx) {
365 auto curPattern = dash.pattern[i % dash.cnt];
366 if (offset < curPattern) break;
367 offset -= curPattern;
368 }
369 }
370
371 mpoolRetOutline(mpool, tid); //retreive the outline cache and use it for dash outline.
372 dash.outline = mpoolReqOutline(mpool, tid);
373
374 //must begin with moveTo
375 if (cmds[0] == PathCommand::MoveTo) {
376 if (trimmed) _trimPattern(&dash, rshape, _outlineLength(rshape, 0, 0, simultaneous), trimBegin, trimEnd);
377 _dashMoveTo(dash, offIdx, offset, pts);
378 cmds++;
379 pts++;
380 }
381
382 while (--cmdCnt > 0) {
383 switch (*cmds) {
384 case PathCommand::Close: {
385 _dashClose(dash, transform);
386 break;
387 }
388 case PathCommand::MoveTo: {
389 if (trimmed) {
390 if (simultaneous) {
391 _trimPattern(&dash, rshape, _outlineLength(rshape, pts - startPts, cmds - startCmds, true), trimBegin, trimEnd);
392 _dashMoveTo(dash, offIdx, offset, pts);
393 } else _dashMoveTo(dash, pts);
394 } else _dashMoveTo(dash, offIdx, offset, pts);
395 ++pts;
396 break;
397 }
398 case PathCommand::LineTo: {
399 _dashLineTo(dash, pts, transform);
400 ++pts;
401 break;
402 }
403 case PathCommand::CubicTo: {
404 _dashCubicTo(dash, pts, pts + 1, pts + 2, transform);
405 pts += 3;
406 break;
407 }
408 }
409 ++cmds;
410 }
411
412 _outlineEnd(*dash.outline);
413
414 if (trimmed) free(dash.pattern);
415
416 return dash.outline;
417 }
418
419
420 static bool _axisAlignedRect(const SwOutline* outline)
421 {
422 //Fast Track: axis-aligned rectangle?
423 if (outline->pts.count != 5) return false;
424 if (outline->types[2] == SW_CURVE_TYPE_CUBIC) return false;
425
426 auto pt1 = outline->pts.data + 0;
427 auto pt2 = outline->pts.data + 1;
428 auto pt3 = outline->pts.data + 2;
429 auto pt4 = outline->pts.data + 3;
430
431 auto a = SwPoint{pt1->x, pt3->y};
432 auto b = SwPoint{pt3->x, pt1->y};
433
434 if ((*pt2 == a && *pt4 == b) || (*pt2 == b && *pt4 == a)) return true;
435
436 return false;
437 }
438
439
440 static bool _genOutline(SwShape* shape, const RenderShape* rshape, const Matrix& transform, SwMpool* mpool, unsigned tid, bool hasComposite)
441 {
442 const PathCommand* cmds = rshape->path.cmds.data;
443 auto cmdCnt = rshape->path.cmds.count;
444 const Point* pts = rshape->path.pts.data;
445 auto ptsCnt = rshape->path.pts.count;
446
447 //No actual shape data
448 if (cmdCnt == 0 || ptsCnt == 0) return false;
449
450 shape->outline = mpoolReqOutline(mpool, tid);
451 auto outline = shape->outline;
452 auto closed = false;
453
454 //Generate Outlines
455 while (cmdCnt-- > 0) {
456 switch (*cmds) {
457 case PathCommand::Close: {
458 if (!closed) closed = _outlineClose(*outline);
459 break;
460 }
461 case PathCommand::MoveTo: {
462 closed = _outlineMoveTo(*outline, pts, transform, closed);
463 ++pts;
464 break;
465 }
466 case PathCommand::LineTo: {
467 if (closed) closed = _outlineBegin(*outline);
468 _outlineLineTo(*outline, pts, transform);
469 ++pts;
470 break;
471 }
472 case PathCommand::CubicTo: {
473 if (closed) closed = _outlineBegin(*outline);
474 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
475 pts += 3;
476 break;
477 }
478 }
479 ++cmds;
480 }
481
482 if (!closed) _outlineEnd(*outline);
483
484 outline->fillRule = rshape->rule;
485 shape->outline = outline;
486
487 shape->fastTrack = (!hasComposite && _axisAlignedRect(shape->outline));
488 return true;
489 }
490
491
492 /************************************************************************/
493 /* External Class Implementation */
494 /************************************************************************/
495
496 bool shapePrepare(SwShape* shape, const RenderShape* rshape, const Matrix& transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid, bool hasComposite)
497 {
498 if (!_genOutline(shape, rshape, transform, mpool, tid, hasComposite)) return false;
499 if (!mathUpdateOutlineBBox(shape->outline, clipRegion, renderRegion, shape->fastTrack)) return false;
500
501 shape->bbox = renderRegion;
502
503 //Check valid region
504 if (renderRegion.max.x - renderRegion.min.x < 1 && renderRegion.max.y - renderRegion.min.y < 1) return false;
505
506 //Check boundary
507 if (renderRegion.min.x >= clipRegion.max.x || renderRegion.min.y >= clipRegion.max.y ||
508 renderRegion.max.x <= clipRegion.min.x || renderRegion.max.y <= clipRegion.min.y) return false;
509
510 return true;
511 }
512
513
514 bool shapePrepared(const SwShape* shape)
515 {
516 return shape->rle ? true : false;
517 }
518
519
520 bool shapeGenRle(SwShape* shape, TVG_UNUSED const RenderShape* rshape, SwMpool* mpool, unsigned tid, bool antiAlias)
521 {
522 //FIXME: Should we draw it?
523 //Case: Stroke Line
524 //if (shape.outline->opened) return true;
525
526 //Case A: Fast Track Rectangle Drawing
527 if (shape->fastTrack) return true;
528
529 //Case B: Normal Shape RLE Drawing
530 if ((shape->rle = rleRender(shape->rle, shape->outline, shape->bbox, mpool, tid, antiAlias))) return true;
531
532 return false;
533 }
534
535
536 void shapeDelOutline(SwShape* shape, SwMpool* mpool, uint32_t tid)
537 {
538 mpoolRetOutline(mpool, tid);
539 shape->outline = nullptr;
540 }
541
542
543 void shapeReset(SwShape* shape)
544 {
545 rleReset(shape->rle);
546 shape->fastTrack = false;
547 shape->bbox.reset();
548 }
549
550
551 void shapeFree(SwShape* shape)
552 {
553 rleFree(shape->rle);
554 shape->rle = nullptr;
555
556 shapeDelFill(shape);
557
558 if (shape->stroke) {
559 rleFree(shape->strokeRle);
560 shape->strokeRle = nullptr;
561 strokeFree(shape->stroke);
562 shape->stroke = nullptr;
563 }
564 }
565
566
567 void shapeDelStroke(SwShape* shape)
568 {
569 if (!shape->stroke) return;
570 rleFree(shape->strokeRle);
571 shape->strokeRle = nullptr;
572 strokeFree(shape->stroke);
573 shape->stroke = nullptr;
574 }
575
576
577 void shapeResetStroke(SwShape* shape, const RenderShape* rshape, const Matrix& transform)
578 {
579 if (!shape->stroke) shape->stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
580 auto stroke = shape->stroke;
581 if (!stroke) return;
582
583 strokeReset(stroke, rshape, transform);
584 rleReset(shape->strokeRle);
585 }
586
587
588 bool shapeGenStrokeRle(SwShape* shape, const RenderShape* rshape, const Matrix& transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid)
589 {
590 SwOutline* shapeOutline = nullptr;
591
592 //Dash style (+trimming)
593 auto trimmed = rshape->strokeTrim();
594 if (rshape->stroke->dashCnt > 0 || trimmed) {
595 shapeOutline = _genDashOutline(rshape, transform, trimmed, mpool, tid);
596 if (!shapeOutline) return false;
597 //Trimming & Normal style
598 } else {
599 if (!shape->outline) {
600 if (!_genOutline(shape, rshape, transform, mpool, tid, false)) return false;
601 }
602 shapeOutline = shape->outline;
603 }
604
605 if (!strokeParseOutline(shape->stroke, *shapeOutline)) return false;
606 auto strokeOutline = strokeExportOutline(shape->stroke, mpool, tid);
607 auto ret = mathUpdateOutlineBBox(strokeOutline, clipRegion, renderRegion, false);
608 shape->strokeRle = rleRender(shape->strokeRle, strokeOutline, renderRegion, mpool, tid, true);
609 mpoolRetStrokeOutline(mpool, tid);
610 return ret;
611 }
612
613
614 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix& transform, SwSurface* surface, uint8_t opacity, bool ctable)
615 {
616 return fillGenColorTable(shape->fill, fill, transform, surface, opacity, ctable);
617 }
618
619
620 bool shapeGenStrokeFillColors(SwShape* shape, const Fill* fill, const Matrix& transform, SwSurface* surface, uint8_t opacity, bool ctable)
621 {
622 return fillGenColorTable(shape->stroke->fill, fill, transform, surface, opacity, ctable);
623 }
624
625
626 void shapeResetFill(SwShape* shape)
627 {
628 if (!shape->fill) {
629 shape->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
630 if (!shape->fill) return;
631 }
632 fillReset(shape->fill);
633 }
634
635
636 void shapeResetStrokeFill(SwShape* shape)
637 {
638 if (!shape->stroke->fill) {
639 shape->stroke->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
640 if (!shape->stroke->fill) return;
641 }
642 fillReset(shape->stroke->fill);
643 }
644
645
646 void shapeDelFill(SwShape* shape)
647 {
648 if (!shape->fill) return;
649 fillFree(shape->fill);
650 shape->fill = nullptr;
651 }
652
653
654 void shapeDelStrokeFill(SwShape* shape)
655 {
656 if (!shape->stroke->fill) return;
657 fillFree(shape->stroke->fill);
658 shape->stroke->fill = nullptr;
659 }