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 "tvgSwCommon.h"
24#include "tvgBezier.h"
25#include <float.h>
26#include <math.h>
27
28/************************************************************************/
29/* Internal Class Implementation */
30/************************************************************************/
31
32struct Line
33{
34 Point pt1;
35 Point pt2;
36};
37
38
39static float _lineLength(const Point& pt1, const Point& pt2)
40{
41 /* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm.
42 With alpha = 1, beta = 3/8, giving results with the largest error less
43 than 7% compared to the exact value. */
44 Point diff = {pt2.x - pt1.x, pt2.y - pt1.y};
45 if (diff.x < 0) diff.x = -diff.x;
46 if (diff.y < 0) diff.y = -diff.y;
47 return (diff.x > diff.y) ? (diff.x + diff.y * 0.375f) : (diff.y + diff.x * 0.375f);
48}
49
50
51static void _lineSplitAt(const Line& cur, float at, Line& left, Line& right)
52{
53 auto len = _lineLength(cur.pt1, cur.pt2);
54 auto dx = ((cur.pt2.x - cur.pt1.x) / len) * at;
55 auto dy = ((cur.pt2.y - cur.pt1.y) / len) * at;
56 left.pt1 = cur.pt1;
57 left.pt2.x = left.pt1.x + dx;
58 left.pt2.y = left.pt1.y + dy;
59 right.pt1 = left.pt2;
60 right.pt2 = cur.pt2;
61}
62
63
64static void _outlineEnd(SwOutline& outline)
65{
66 if (outline.pts.empty()) return;
67 outline.cntrs.push(outline.pts.count - 1);
68}
69
70
71static void _outlineMoveTo(SwOutline& outline, const Point* to, const Matrix* transform)
72{
73 if (outline.pts.count > 0) outline.cntrs.push(outline.pts.count - 1);
74
75 outline.pts.push(mathTransform(to, transform));
76 outline.types.push(SW_CURVE_TYPE_POINT);
77}
78
79
80static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix* transform)
81{
82 outline.pts.push(mathTransform(to, transform));
83 outline.types.push(SW_CURVE_TYPE_POINT);
84}
85
86
87static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
88{
89 outline.pts.push(mathTransform(ctrl1, transform));
90 outline.types.push(SW_CURVE_TYPE_CUBIC);
91
92 outline.pts.push(mathTransform(ctrl2, transform));
93 outline.types.push(SW_CURVE_TYPE_CUBIC);
94
95 outline.pts.push(mathTransform(to, transform));
96 outline.types.push(SW_CURVE_TYPE_POINT);
97}
98
99
100static void _outlineClose(SwOutline& outline)
101{
102 uint32_t i = 0;
103
104 if (outline.cntrs.count > 0) i = outline.cntrs.last() + 1;
105 else i = 0; //First Path
106
107 //Make sure there is at least one point in the current path
108 if (outline.pts.count == i) return;
109
110 //Close the path
111 outline.pts.push(outline.pts.data[i]);
112 outline.types.push(SW_CURVE_TYPE_POINT);
113 outline.closed.push(true);
114}
115
116
117static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* transform)
118{
119 Line cur = {dash.ptCur, *to};
120 auto len = _lineLength(cur.pt1, cur.pt2);
121
122 if (len < dash.curLen) {
123 dash.curLen -= len;
124 if (!dash.curOpGap) {
125 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
126 _outlineLineTo(*dash.outline, to, transform);
127 }
128 } else {
129 while (len > dash.curLen) {
130 len -= dash.curLen;
131 Line left, right;
132 _lineSplitAt(cur, dash.curLen, left, right);;
133 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
134 if (!dash.curOpGap) {
135 _outlineMoveTo(*dash.outline, &left.pt1, transform);
136 _outlineLineTo(*dash.outline, &left.pt2, transform);
137 }
138 dash.curLen = dash.pattern[dash.curIdx];
139 dash.curOpGap = !dash.curOpGap;
140 cur = right;
141 dash.ptCur = cur.pt1;
142 }
143 //leftovers
144 dash.curLen -= len;
145 if (!dash.curOpGap) {
146 _outlineMoveTo(*dash.outline, &cur.pt1, transform);
147 _outlineLineTo(*dash.outline, &cur.pt2, transform);
148 }
149 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
150 //move to next dash
151 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
152 dash.curLen = dash.pattern[dash.curIdx];
153 dash.curOpGap = !dash.curOpGap;
154 }
155 }
156 dash.ptCur = *to;
157}
158
159
160static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
161{
162 Bezier cur = {dash.ptCur, *ctrl1, *ctrl2, *to};
163 auto len = bezLength(cur);
164
165 if (len < dash.curLen) {
166 dash.curLen -= len;
167 if (!dash.curOpGap) {
168 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
169 _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to, transform);
170 }
171 } else {
172 while (len > dash.curLen) {
173 Bezier left, right;
174 len -= dash.curLen;
175 bezSplitAt(cur, dash.curLen, left, right);
176 if (!dash.curOpGap) {
177 // leftovers from a previous command don't require moveTo
178 if (dash.pattern[dash.curIdx] - dash.curLen < FLT_EPSILON) {
179 _outlineMoveTo(*dash.outline, &left.start, transform);
180 }
181 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform);
182 }
183 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
184 dash.curLen = dash.pattern[dash.curIdx];
185 dash.curOpGap = !dash.curOpGap;
186 cur = right;
187 dash.ptCur = right.start;
188 }
189 //leftovers
190 dash.curLen -= len;
191 if (!dash.curOpGap) {
192 _outlineMoveTo(*dash.outline, &cur.start, transform);
193 _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform);
194 }
195 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
196 //move to next dash
197 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
198 dash.curLen = dash.pattern[dash.curIdx];
199 dash.curOpGap = !dash.curOpGap;
200 }
201 }
202 dash.ptCur = *to;
203}
204
205
206static SwOutline* _genDashOutline(const RenderShape* rshape, const Matrix* transform)
207{
208 const PathCommand* cmds = rshape->path.cmds.data;
209 auto cmdCnt = rshape->path.cmds.count;
210
211 const Point* pts = rshape->path.pts.data;
212 auto ptsCnt = rshape->path.pts.count;
213
214 //No actual shape data
215 if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
216
217 SwDashStroke dash;
218 dash.curIdx = 0;
219 dash.curLen = 0;
220 dash.ptStart = {0, 0};
221 dash.ptCur = {0, 0};
222 dash.curOpGap = false;
223
224 const float* pattern;
225 dash.cnt = rshape->strokeDash(&pattern);
226 if (dash.cnt == 0) return nullptr;
227
228 //OPTMIZE ME: Use mempool???
229 dash.pattern = const_cast<float*>(pattern);
230 dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
231
232 //smart reservation
233 auto outlinePtsCnt = 0;
234 auto outlineCntrsCnt = 0;
235
236 for (uint32_t i = 0; i < cmdCnt; ++i) {
237 switch (*(cmds + i)) {
238 case PathCommand::Close: {
239 ++outlinePtsCnt;
240 break;
241 }
242 case PathCommand::MoveTo: {
243 ++outlineCntrsCnt;
244 ++outlinePtsCnt;
245 break;
246 }
247 case PathCommand::LineTo: {
248 ++outlinePtsCnt;
249 break;
250 }
251 case PathCommand::CubicTo: {
252 outlinePtsCnt += 3;
253 break;
254 }
255 }
256 }
257
258 ++outlinePtsCnt; //for close
259 ++outlineCntrsCnt; //for end
260
261 //No idea exact count.... Reserve Approximitely 20x...
262 dash.outline->pts.grow(20 * outlinePtsCnt);
263 dash.outline->types.grow(20 * outlinePtsCnt);
264 dash.outline->cntrs.grow(20 * outlineCntrsCnt);
265
266 while (cmdCnt-- > 0) {
267 switch (*cmds) {
268 case PathCommand::Close: {
269 _dashLineTo(dash, &dash.ptStart, transform);
270 break;
271 }
272 case PathCommand::MoveTo: {
273 //reset the dash
274 dash.curIdx = 0;
275 dash.curLen = *dash.pattern;
276 dash.curOpGap = false;
277 dash.ptStart = dash.ptCur = *pts;
278 ++pts;
279 break;
280 }
281 case PathCommand::LineTo: {
282 _dashLineTo(dash, pts, transform);
283 ++pts;
284 break;
285 }
286 case PathCommand::CubicTo: {
287 _dashCubicTo(dash, pts, pts + 1, pts + 2, transform);
288 pts += 3;
289 break;
290 }
291 }
292 ++cmds;
293 }
294
295 _outlineEnd(*dash.outline);
296
297 return dash.outline;
298}
299
300
301static bool _axisAlignedRect(const SwOutline* outline)
302{
303 //Fast Track: axis-aligned rectangle?
304 if (outline->pts.count != 5) return false;
305
306 auto pt1 = outline->pts.data + 0;
307 auto pt2 = outline->pts.data + 1;
308 auto pt3 = outline->pts.data + 2;
309 auto pt4 = outline->pts.data + 3;
310
311 auto a = SwPoint{pt1->x, pt3->y};
312 auto b = SwPoint{pt3->x, pt1->y};
313
314 if ((*pt2 == a && *pt4 == b) || (*pt2 == b && *pt4 == a)) return true;
315
316 return false;
317}
318
319
320static bool _genOutline(SwShape* shape, const RenderShape* rshape, const Matrix* transform, SwMpool* mpool, unsigned tid, bool hasComposite)
321{
322 const PathCommand* cmds = rshape->path.cmds.data;
323 auto cmdCnt = rshape->path.cmds.count;
324
325 const Point* pts = rshape->path.pts.data;
326 auto ptsCnt = rshape->path.pts.count;
327
328 //No actual shape data
329 if (cmdCnt == 0 || ptsCnt == 0) return false;
330
331 //smart reservation
332 auto outlinePtsCnt = 0;
333 auto outlineCntrsCnt = 0;
334 auto closeCnt = 0;
335
336 for (uint32_t i = 0; i < cmdCnt; ++i) {
337 switch (*(cmds + i)) {
338 case PathCommand::Close: {
339 ++outlinePtsCnt;
340 ++closeCnt;
341 break;
342 }
343 case PathCommand::MoveTo: {
344 ++outlineCntrsCnt;
345 ++outlinePtsCnt;
346 break;
347 }
348 case PathCommand::LineTo: {
349 ++outlinePtsCnt;
350 break;
351 }
352 case PathCommand::CubicTo: {
353 outlinePtsCnt += 3;
354 break;
355 }
356 }
357 }
358
359 if (static_cast<uint32_t>(outlinePtsCnt - closeCnt) > ptsCnt) {
360 TVGERR("SW_ENGINE", "Wrong a pair of the commands & points - required(%d), current(%d)", outlinePtsCnt - closeCnt, ptsCnt);
361 return false;
362 }
363
364 ++outlinePtsCnt; //for close
365 ++outlineCntrsCnt; //for end
366
367 shape->outline = mpoolReqOutline(mpool, tid);
368 auto outline = shape->outline;
369
370 outline->pts.grow(outlinePtsCnt);
371 outline->types.grow(outlinePtsCnt);
372 outline->cntrs.grow(outlineCntrsCnt);
373
374 //Dash outlines are always opened.
375 //Only normal outlines use this information, it sholud be same to their contour counts.
376 outline->closed.reserve(outline->cntrs.reserved);
377
378 memset(outline->closed.data, 0x0, sizeof(bool) * outline->closed.reserved);
379
380 //Generate Outlines
381 while (cmdCnt-- > 0) {
382 switch (*cmds) {
383 case PathCommand::Close: {
384 _outlineClose(*outline);
385 break;
386 }
387 case PathCommand::MoveTo: {
388 _outlineMoveTo(*outline, pts, transform);
389 ++pts;
390 break;
391 }
392 case PathCommand::LineTo: {
393 _outlineLineTo(*outline, pts, transform);
394 ++pts;
395 break;
396 }
397 case PathCommand::CubicTo: {
398 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
399 pts += 3;
400 break;
401 }
402 }
403 ++cmds;
404 }
405
406 _outlineEnd(*outline);
407
408 outline->fillRule = rshape->rule;
409 shape->outline = outline;
410
411 shape->fastTrack = (!hasComposite && _axisAlignedRect(shape->outline));
412 return true;
413}
414
415
416/************************************************************************/
417/* External Class Implementation */
418/************************************************************************/
419
420bool shapePrepare(SwShape* shape, const RenderShape* rshape, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid, bool hasComposite)
421{
422 if (!_genOutline(shape, rshape, transform, mpool, tid, hasComposite)) return false;
423 if (!mathUpdateOutlineBBox(shape->outline, clipRegion, renderRegion, shape->fastTrack)) return false;
424
425 //Keep it for Rasterization Region
426 shape->bbox = renderRegion;
427
428 //Check valid region
429 if (renderRegion.max.x - renderRegion.min.x < 1 && renderRegion.max.y - renderRegion.min.y < 1) return false;
430
431 //Check boundary
432 if (renderRegion.min.x >= clipRegion.max.x || renderRegion.min.y >= clipRegion.max.y ||
433 renderRegion.max.x <= clipRegion.min.x || renderRegion.max.y <= clipRegion.min.y) return false;
434
435 return true;
436}
437
438
439bool shapePrepared(const SwShape* shape)
440{
441 return shape->rle ? true : false;
442}
443
444
445bool shapeGenRle(SwShape* shape, TVG_UNUSED const RenderShape* rshape, bool antiAlias)
446{
447 //FIXME: Should we draw it?
448 //Case: Stroke Line
449 //if (shape.outline->opened) return true;
450
451 //Case A: Fast Track Rectangle Drawing
452 if (shape->fastTrack) return true;
453
454 //Case B: Normal Shape RLE Drawing
455 if ((shape->rle = rleRender(shape->rle, shape->outline, shape->bbox, antiAlias))) return true;
456
457 return false;
458}
459
460
461void shapeDelOutline(SwShape* shape, SwMpool* mpool, uint32_t tid)
462{
463 mpoolRetOutline(mpool, tid);
464 shape->outline = nullptr;
465}
466
467
468void shapeReset(SwShape* shape)
469{
470 rleReset(shape->rle);
471 rleReset(shape->strokeRle);
472 shape->fastTrack = false;
473 shape->bbox.reset();
474}
475
476
477void shapeFree(SwShape* shape)
478{
479 rleFree(shape->rle);
480 shapeDelFill(shape);
481
482 if (shape->stroke) {
483 rleFree(shape->strokeRle);
484 strokeFree(shape->stroke);
485 }
486}
487
488
489void shapeDelStroke(SwShape* shape)
490{
491 if (!shape->stroke) return;
492 rleFree(shape->strokeRle);
493 shape->strokeRle = nullptr;
494 strokeFree(shape->stroke);
495 shape->stroke = nullptr;
496}
497
498
499void shapeResetStroke(SwShape* shape, const RenderShape* rshape, const Matrix* transform)
500{
501 if (!shape->stroke) shape->stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
502 auto stroke = shape->stroke;
503 if (!stroke) return;
504
505 strokeReset(stroke, rshape, transform);
506 rleReset(shape->strokeRle);
507}
508
509
510bool shapeGenStrokeRle(SwShape* shape, const RenderShape* rshape, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid)
511{
512 SwOutline* shapeOutline = nullptr;
513 SwOutline* strokeOutline = nullptr;
514 bool freeOutline = false;
515 bool ret = true;
516
517 //Dash Style Stroke
518 if (rshape->strokeDash(nullptr) > 0) {
519 shapeOutline = _genDashOutline(rshape, transform);
520 if (!shapeOutline) return false;
521 freeOutline = true;
522 //Normal Style stroke
523 } else {
524 if (!shape->outline) {
525 if (!_genOutline(shape, rshape, transform, mpool, tid, false)) return false;
526 }
527 shapeOutline = shape->outline;
528 }
529
530 if (!strokeParseOutline(shape->stroke, *shapeOutline)) {
531 ret = false;
532 goto fail;
533 }
534
535 strokeOutline = strokeExportOutline(shape->stroke, mpool, tid);
536
537 if (!mathUpdateOutlineBBox(strokeOutline, clipRegion, renderRegion, false)) {
538 ret = false;
539 goto fail;
540 }
541
542 shape->strokeRle = rleRender(shape->strokeRle, strokeOutline, renderRegion, true);
543
544fail:
545 if (freeOutline) {
546 free(shapeOutline->cntrs.data);
547 free(shapeOutline->pts.data);
548 free(shapeOutline->types.data);
549 free(shapeOutline->closed.data);
550 free(shapeOutline);
551 }
552 mpoolRetStrokeOutline(mpool, tid);
553
554 return ret;
555}
556
557
558bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint8_t opacity, bool ctable)
559{
560 return fillGenColorTable(shape->fill, fill, transform, surface, opacity, ctable);
561}
562
563
564bool shapeGenStrokeFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint8_t opacity, bool ctable)
565{
566 return fillGenColorTable(shape->stroke->fill, fill, transform, surface, opacity, ctable);
567}
568
569
570void shapeResetFill(SwShape* shape)
571{
572 if (!shape->fill) {
573 shape->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
574 if (!shape->fill) return;
575 }
576 fillReset(shape->fill);
577}
578
579
580void shapeResetStrokeFill(SwShape* shape)
581{
582 if (!shape->stroke->fill) {
583 shape->stroke->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
584 if (!shape->stroke->fill) return;
585 }
586 fillReset(shape->stroke->fill);
587}
588
589
590void shapeDelFill(SwShape* shape)
591{
592 if (!shape->fill) return;
593 fillFree(shape->fill);
594 shape->fill = nullptr;
595}
596
597
598void shapeDelStrokeFill(SwShape* shape)
599{
600 if (!shape->stroke->fill) return;
601 fillFree(shape->stroke->fill);
602 shape->stroke->fill = nullptr;
603}
604