2 * Copyright (c) 2020 - 2023 the ThorVG project. All rights reserved.
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:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
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
23 #include "tvgSwCommon.h"
24 #include "tvgBezier.h"
28 /************************************************************************/
29 /* Internal Class Implementation */
30 /************************************************************************/
39 static float _lineLength(const Point& pt1, const Point& pt2)
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);
51 static void _lineSplitAt(const Line& cur, float at, Line& left, Line& right)
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;
57 left.pt2.x = left.pt1.x + dx;
58 left.pt2.y = left.pt1.y + dy;
64 static bool _growOutlineContour(SwOutline& outline, uint32_t n)
66 if (outline.reservedCntrsCnt >= outline.cntrsCnt + n) return false;
67 outline.reservedCntrsCnt = outline.cntrsCnt + n;
68 outline.cntrs = static_cast<uint32_t*>(realloc(outline.cntrs, outline.reservedCntrsCnt * sizeof(uint32_t)));
73 static void _reserveOutlineClose(SwOutline& outline)
75 //Dash outlines are always opened.
76 //Only normal outlines use this information, it sholud be same to their contour counts.
77 if (outline.closed) free(outline.closed);
78 outline.closed = static_cast<bool*>(calloc(outline.reservedCntrsCnt, sizeof(bool)));
82 static void _resetOutlineClose(SwOutline& outline)
84 memset(outline.closed, 0x0, outline.reservedCntrsCnt * sizeof(bool));
88 static void _growOutlinePoint(SwOutline& outline, uint32_t n)
90 if (outline.reservedPtsCnt >= outline.ptsCnt + n) return;
91 outline.reservedPtsCnt = outline.ptsCnt + n;
92 outline.pts = static_cast<SwPoint*>(realloc(outline.pts, outline.reservedPtsCnt * sizeof(SwPoint)));
93 outline.types = static_cast<uint8_t*>(realloc(outline.types, outline.reservedPtsCnt * sizeof(uint8_t)));
97 static void _outlineEnd(SwOutline& outline)
99 if (outline.ptsCnt == 0) return;
101 _growOutlineContour(outline, 1);
102 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
107 static void _outlineMoveTo(SwOutline& outline, const Point* to, const Matrix* transform)
109 _growOutlinePoint(outline, 1);
111 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
112 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
114 if (outline.ptsCnt > 0) {
115 _growOutlineContour(outline, 1);
116 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
124 static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix* transform)
126 _growOutlinePoint(outline, 1);
128 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
129 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
134 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
136 _growOutlinePoint(outline, 3);
138 outline.pts[outline.ptsCnt] = mathTransform(ctrl1, transform);
139 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
142 outline.pts[outline.ptsCnt] = mathTransform(ctrl2, transform);
143 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
146 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
147 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
152 static void _outlineClose(SwOutline& outline)
156 if (outline.cntrsCnt > 0) {
157 i = outline.cntrs[outline.cntrsCnt - 1] + 1;
162 //Make sure there is at least one point in the current path
163 if (outline.ptsCnt == i) return;
166 _growOutlinePoint(outline, 1);
168 outline.pts[outline.ptsCnt] = outline.pts[i];
169 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
171 outline.closed[outline.cntrsCnt] = true;
175 static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* transform)
177 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
178 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
180 Line cur = {dash.ptCur, *to};
181 auto len = _lineLength(cur.pt1, cur.pt2);
183 if (len < dash.curLen) {
185 if (!dash.curOpGap) {
186 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
187 _outlineLineTo(*dash.outline, to, transform);
190 while (len > dash.curLen) {
193 _lineSplitAt(cur, dash.curLen, left, right);;
194 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
195 if (!dash.curOpGap) {
196 _outlineMoveTo(*dash.outline, &left.pt1, transform);
197 _outlineLineTo(*dash.outline, &left.pt2, transform);
199 dash.curLen = dash.pattern[dash.curIdx];
200 dash.curOpGap = !dash.curOpGap;
202 dash.ptCur = cur.pt1;
206 if (!dash.curOpGap) {
207 _outlineMoveTo(*dash.outline, &cur.pt1, transform);
208 _outlineLineTo(*dash.outline, &cur.pt2, transform);
210 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
212 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
213 dash.curLen = dash.pattern[dash.curIdx];
214 dash.curOpGap = !dash.curOpGap;
221 static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
223 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
224 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
226 Bezier cur = {dash.ptCur, *ctrl1, *ctrl2, *to};
227 auto len = bezLength(cur);
229 if (len < dash.curLen) {
231 if (!dash.curOpGap) {
232 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
233 _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to, transform);
236 while (len > dash.curLen) {
239 bezSplitAt(cur, dash.curLen, left, right);
240 if (!dash.curOpGap) {
241 // leftovers from a previous command don't require moveTo
242 if (dash.pattern[dash.curIdx] - dash.curLen < FLT_EPSILON) {
243 _outlineMoveTo(*dash.outline, &left.start, transform);
245 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform);
247 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
248 dash.curLen = dash.pattern[dash.curIdx];
249 dash.curOpGap = !dash.curOpGap;
251 dash.ptCur = right.start;
255 if (!dash.curOpGap) {
256 _outlineMoveTo(*dash.outline, &cur.start, transform);
257 _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform);
259 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
261 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
262 dash.curLen = dash.pattern[dash.curIdx];
263 dash.curOpGap = !dash.curOpGap;
270 static SwOutline* _genDashOutline(const Shape* sdata, const Matrix* transform)
272 const PathCommand* cmds = nullptr;
273 auto cmdCnt = sdata->pathCommands(&cmds);
275 const Point* pts = nullptr;
276 auto ptsCnt = sdata->pathCoords(&pts);
278 //No actual shape data
279 if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
284 dash.ptStart = {0, 0};
286 dash.curOpGap = false;
288 const float* pattern;
289 dash.cnt = sdata->strokeDash(&pattern);
290 if (dash.cnt == 0) return nullptr;
292 //OPTMIZE ME: Use mempool???
293 dash.pattern = const_cast<float*>(pattern);
294 dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
297 auto outlinePtsCnt = 0;
298 auto outlineCntrsCnt = 0;
300 for (uint32_t i = 0; i < cmdCnt; ++i) {
301 switch (*(cmds + i)) {
302 case PathCommand::Close: {
306 case PathCommand::MoveTo: {
311 case PathCommand::LineTo: {
315 case PathCommand::CubicTo: {
322 ++outlinePtsCnt; //for close
323 ++outlineCntrsCnt; //for end
325 //No idea exact count.... Reserve Approximitely 20x...
326 _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
327 _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
329 while (cmdCnt-- > 0) {
331 case PathCommand::Close: {
332 _dashLineTo(dash, &dash.ptStart, transform);
335 case PathCommand::MoveTo: {
338 dash.curLen = *dash.pattern;
339 dash.curOpGap = false;
340 dash.ptStart = dash.ptCur = *pts;
344 case PathCommand::LineTo: {
345 _dashLineTo(dash, pts, transform);
349 case PathCommand::CubicTo: {
350 _dashCubicTo(dash, pts, pts + 1, pts + 2, transform);
358 _outlineEnd(*dash.outline);
364 static bool _axisAlignedRect(const SwOutline* outline)
366 //Fast Track: axis-aligned rectangle?
367 if (outline->ptsCnt != 5) return false;
369 auto pt1 = outline->pts + 0;
370 auto pt2 = outline->pts + 1;
371 auto pt3 = outline->pts + 2;
372 auto pt4 = outline->pts + 3;
374 auto a = SwPoint{pt1->x, pt3->y};
375 auto b = SwPoint{pt3->x, pt1->y};
377 if ((*pt2 == a && *pt4 == b) || (*pt2 == b && *pt4 == a)) return true;
384 static bool _genOutline(SwShape* shape, const Shape* sdata, const Matrix* transform, SwMpool* mpool, unsigned tid, bool hasComposite)
386 const PathCommand* cmds = nullptr;
387 auto cmdCnt = sdata->pathCommands(&cmds);
389 const Point* pts = nullptr;
390 auto ptsCnt = sdata->pathCoords(&pts);
392 //No actual shape data
393 if (cmdCnt == 0 || ptsCnt == 0) return false;
396 auto outlinePtsCnt = 0;
397 auto outlineCntrsCnt = 0;
400 for (uint32_t i = 0; i < cmdCnt; ++i) {
401 switch (*(cmds + i)) {
402 case PathCommand::Close: {
407 case PathCommand::MoveTo: {
412 case PathCommand::LineTo: {
416 case PathCommand::CubicTo: {
423 if (static_cast<uint32_t>(outlinePtsCnt - closeCnt) > ptsCnt) {
424 TVGERR("SW_ENGINE", "Wrong a pair of the commands & points - required(%d), current(%d)", outlinePtsCnt - closeCnt, ptsCnt);
428 ++outlinePtsCnt; //for close
429 ++outlineCntrsCnt; //for end
431 shape->outline = mpoolReqOutline(mpool, tid);
432 auto outline = shape->outline;
434 _growOutlinePoint(*outline, outlinePtsCnt);
436 if (_growOutlineContour(*outline, outlineCntrsCnt)) {
437 _reserveOutlineClose(*outline);
439 _resetOutlineClose(*outline);
443 while (cmdCnt-- > 0) {
445 case PathCommand::Close: {
446 _outlineClose(*outline);
449 case PathCommand::MoveTo: {
450 _outlineMoveTo(*outline, pts, transform);
454 case PathCommand::LineTo: {
455 _outlineLineTo(*outline, pts, transform);
459 case PathCommand::CubicTo: {
460 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
468 _outlineEnd(*outline);
470 outline->fillRule = sdata->fillRule();
471 shape->outline = outline;
473 shape->fastTrack = (!hasComposite && _axisAlignedRect(shape->outline));
478 /************************************************************************/
479 /* External Class Implementation */
480 /************************************************************************/
482 bool shapePrepare(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid, bool hasComposite)
484 if (!_genOutline(shape, sdata, transform, mpool, tid, hasComposite)) return false;
485 if (!mathUpdateOutlineBBox(shape->outline, clipRegion, renderRegion, shape->fastTrack)) return false;
487 //Keep it for Rasterization Region
488 shape->bbox = renderRegion;
491 if (renderRegion.max.x - renderRegion.min.x < 1 && renderRegion.max.y - renderRegion.min.y < 1) return false;
494 if (renderRegion.min.x >= clipRegion.max.x || renderRegion.min.y >= clipRegion.max.y ||
495 renderRegion.max.x <= clipRegion.min.x || renderRegion.max.y <= clipRegion.min.y) return false;
501 bool shapePrepared(const SwShape* shape)
503 return shape->rle ? true : false;
507 bool shapeGenRle(SwShape* shape, TVG_UNUSED const Shape* sdata, bool antiAlias)
509 //FIXME: Should we draw it?
511 //if (shape.outline->opened) return true;
513 //Case A: Fast Track Rectangle Drawing
514 if (shape->fastTrack) return true;
516 //Case B: Normal Shape RLE Drawing
517 if ((shape->rle = rleRender(shape->rle, shape->outline, shape->bbox, antiAlias))) return true;
523 void shapeDelOutline(SwShape* shape, SwMpool* mpool, uint32_t tid)
525 mpoolRetOutline(mpool, tid);
526 shape->outline = nullptr;
530 void shapeReset(SwShape* shape)
532 rleReset(shape->rle);
533 rleReset(shape->strokeRle);
534 shape->fastTrack = false;
539 void shapeFree(SwShape* shape)
545 rleFree(shape->strokeRle);
546 strokeFree(shape->stroke);
551 void shapeDelStroke(SwShape* shape)
553 if (!shape->stroke) return;
554 rleFree(shape->strokeRle);
555 shape->strokeRle = nullptr;
556 strokeFree(shape->stroke);
557 shape->stroke = nullptr;
561 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform)
563 if (!shape->stroke) shape->stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
564 auto stroke = shape->stroke;
567 strokeReset(stroke, sdata, transform);
568 rleReset(shape->strokeRle);
572 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid)
574 SwOutline* shapeOutline = nullptr;
575 SwOutline* strokeOutline = nullptr;
576 bool freeOutline = false;
580 if (sdata->strokeDash(nullptr) > 0) {
581 shapeOutline = _genDashOutline(sdata, transform);
582 if (!shapeOutline) return false;
584 //Normal Style stroke
586 if (!shape->outline) {
587 if (!_genOutline(shape, sdata, transform, mpool, tid, false)) return false;
589 shapeOutline = shape->outline;
592 if (!strokeParseOutline(shape->stroke, *shapeOutline)) {
597 strokeOutline = strokeExportOutline(shape->stroke, mpool, tid);
599 if (!mathUpdateOutlineBBox(strokeOutline, clipRegion, renderRegion, false)) {
604 shape->strokeRle = rleRender(shape->strokeRle, strokeOutline, renderRegion, true);
608 if (shapeOutline->cntrs) free(shapeOutline->cntrs);
609 if (shapeOutline->pts) free(shapeOutline->pts);
610 if (shapeOutline->types) free(shapeOutline->types);
611 if (shapeOutline->closed) free(shapeOutline->closed);
614 mpoolRetStrokeOutline(mpool, tid);
620 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable)
622 return fillGenColorTable(shape->fill, fill, transform, surface, opacity, ctable);
626 bool shapeGenStrokeFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable)
628 return fillGenColorTable(shape->stroke->fill, fill, transform, surface, opacity, ctable);
632 void shapeResetFill(SwShape* shape)
635 shape->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
636 if (!shape->fill) return;
638 fillReset(shape->fill);
642 void shapeResetStrokeFill(SwShape* shape)
644 if (!shape->stroke->fill) {
645 shape->stroke->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
646 if (!shape->stroke->fill) return;
648 fillReset(shape->stroke->fill);
652 void shapeDelFill(SwShape* shape)
654 if (!shape->fill) return;
655 fillFree(shape->fill);
656 shape->fill = nullptr;
660 void shapeDelStrokeFill(SwShape* shape)
662 if (!shape->stroke->fill) return;
663 fillFree(shape->stroke->fill);
664 shape->stroke->fill = nullptr;