2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. 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
22 #ifndef _TVG_SW_SHAPE_H_
23 #define _TVG_SW_SHAPE_H_
25 #include "tvgSwCommon.h"
27 /************************************************************************/
28 /* Internal Class Implementation */
29 /************************************************************************/
38 static SwPoint _transform(const Point* to, const Matrix* transform)
40 if (!transform) return {TO_SWCOORD(to->x), TO_SWCOORD(to->y)};
42 auto tx = round(to->x * transform->e11 + to->y * transform->e12 + transform->e13);
43 auto ty = round(to->x * transform->e21 + to->y * transform->e22 + transform->e23);
45 return {TO_SWCOORD(tx), TO_SWCOORD(ty)};
49 static float _lineLength(const Point& pt1, const Point& pt2)
51 /* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm.
52 With alpha = 1, beta = 3/8, giving results with the largest error less
53 than 7% compared to the exact value. */
54 Point diff = {pt2.x - pt1.x, pt2.y - pt1.y};
55 if (diff.x < 0) diff.x = -diff.x;
56 if (diff.y < 0) diff.y = -diff.y;
57 return (diff.x > diff.y) ? (diff.x + diff.y * 0.375f) : (diff.y + diff.x * 0.375f);
61 static void _lineSplitAt(const Line& cur, float at, Line& left, Line& right)
63 auto len = _lineLength(cur.pt1, cur.pt2);
64 auto dx = ((cur.pt2.x - cur.pt1.x) / len) * at;
65 auto dy = ((cur.pt2.y - cur.pt1.y) / len) * at;
67 left.pt2.x = left.pt1.x + dx;
68 left.pt2.y = left.pt1.y + dy;
74 static void _growOutlineContour(SwOutline& outline, uint32_t n)
76 if (outline.reservedCntrsCnt >= outline.cntrsCnt + n) return;
77 outline.reservedCntrsCnt = outline.cntrsCnt + n;
78 outline.cntrs = static_cast<uint32_t*>(realloc(outline.cntrs, outline.reservedCntrsCnt * sizeof(uint32_t)));
79 assert(outline.cntrs);
83 static void _growOutlinePoint(SwOutline& outline, uint32_t n)
85 if (outline.reservedPtsCnt >= outline.ptsCnt + n) return;
86 outline.reservedPtsCnt = outline.ptsCnt + n;
87 outline.pts = static_cast<SwPoint*>(realloc(outline.pts, outline.reservedPtsCnt * sizeof(SwPoint)));
89 outline.types = static_cast<uint8_t*>(realloc(outline.types, outline.reservedPtsCnt * sizeof(uint8_t)));
90 assert(outline.types);
94 static void _delOutline(SwOutline* outline)
98 if (outline->cntrs) free(outline->cntrs);
99 if (outline->pts) free(outline->pts);
100 if (outline->types) free(outline->types);
105 static void _outlineEnd(SwOutline& outline)
107 _growOutlineContour(outline, 1);
108 if (outline.ptsCnt > 0) {
109 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
115 static void _outlineMoveTo(SwOutline& outline, const Point* to, const Matrix* transform)
119 _growOutlinePoint(outline, 1);
121 outline.pts[outline.ptsCnt] = _transform(to, transform);
123 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
125 if (outline.ptsCnt > 0) {
126 _growOutlineContour(outline, 1);
127 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
135 static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix* transform)
139 _growOutlinePoint(outline, 1);
141 outline.pts[outline.ptsCnt] = _transform(to, transform);
142 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
147 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
149 assert(ctrl1 && ctrl2 && to);
151 _growOutlinePoint(outline, 3);
153 outline.pts[outline.ptsCnt] = _transform(ctrl1, transform);
154 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
157 outline.pts[outline.ptsCnt] = _transform(ctrl2, transform);
158 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
161 outline.pts[outline.ptsCnt] = _transform(to, transform);
162 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
167 static void _outlineClose(SwOutline& outline)
171 if (outline.cntrsCnt > 0) {
172 i = outline.cntrs[outline.cntrsCnt - 1] + 1;
177 //Make sure there is at least one point in the current path
178 if (outline.ptsCnt == i) {
179 outline.opened = true;
184 _growOutlinePoint(outline, 1);
186 outline.pts[outline.ptsCnt] = outline.pts[i];
187 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
190 outline.opened = false;
194 static void _initBBox(SwBBox& bbox)
196 bbox.min.x = bbox.min.y = 0;
197 bbox.max.x = bbox.max.y = 0;
201 static bool _updateBBox(SwOutline* outline, SwBBox& bbox)
203 if (!outline) return false;
205 auto pt = outline->pts;
208 if (outline->ptsCnt <= 0) {
220 for(uint32_t i = 1; i < outline->ptsCnt; ++i, ++pt) {
221 if (xMin > pt->x) xMin = pt->x;
222 if (xMax < pt->x) xMax = pt->x;
223 if (yMin > pt->y) yMin = pt->y;
224 if (yMax < pt->y) yMax = pt->y;
226 bbox.min.x = xMin >> 6;
227 bbox.max.x = (xMax + 63) >> 6;
228 bbox.min.y = yMin >> 6;
229 bbox.max.y = (yMax + 63) >> 6;
231 if (xMax - xMin < 1 && yMax - yMin < 1) return false;
237 static bool _checkValid(const SwOutline* outline, const SwBBox& bbox, const SwSize& clip)
241 if (outline->ptsCnt == 0 || outline->cntrsCnt <= 0) return false;
244 if (bbox.min.x >= clip.w || bbox.min.y >= clip.h || bbox.max.x <= 0 || bbox.max.y <= 0) return false;
250 static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* transform)
252 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
253 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
255 Line cur = {dash.ptCur, *to};
256 auto len = _lineLength(cur.pt1, cur.pt2);
258 if (len < dash.curLen) {
260 if (!dash.curOpGap) {
261 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
262 _outlineLineTo(*dash.outline, to, transform);
265 while (len > dash.curLen) {
268 _lineSplitAt(cur, dash.curLen, left, right);;
269 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
270 if (!dash.curOpGap) {
271 _outlineMoveTo(*dash.outline, &left.pt1, transform);
272 _outlineLineTo(*dash.outline, &left.pt2, transform);
274 dash.curLen = dash.pattern[dash.curIdx];
275 dash.curOpGap = !dash.curOpGap;
277 dash.ptCur = cur.pt1;
281 if (!dash.curOpGap) {
282 _outlineMoveTo(*dash.outline, &cur.pt1, transform);
283 _outlineLineTo(*dash.outline, &cur.pt2, transform);
285 if (dash.curLen < 1) {
287 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
288 dash.curLen = dash.pattern[dash.curIdx];
289 dash.curOpGap = !dash.curOpGap;
296 static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
298 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
299 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
301 Bezier cur = { dash.ptCur, *ctrl1, *ctrl2, *to};
302 auto len = bezLength(cur);
304 if (len < dash.curLen) {
306 if (!dash.curOpGap) {
307 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
308 _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to, transform);
311 while (len > dash.curLen) {
314 bezSplitAt(cur, dash.curLen, left, right);
315 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
316 if (!dash.curOpGap) {
317 _outlineMoveTo(*dash.outline, &left.start, transform);
318 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform);
320 dash.curLen = dash.pattern[dash.curIdx];
321 dash.curOpGap = !dash.curOpGap;
323 dash.ptCur = right.start;
327 if (!dash.curOpGap) {
328 _outlineMoveTo(*dash.outline, &cur.start, transform);
329 _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform);
331 if (dash.curLen < 1) {
333 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
334 dash.curLen = dash.pattern[dash.curIdx];
335 dash.curOpGap = !dash.curOpGap;
342 SwOutline* _genDashOutline(const Shape* sdata, const Matrix* transform)
346 const PathCommand* cmds = nullptr;
347 auto cmdCnt = sdata->pathCommands(&cmds);
349 const Point* pts = nullptr;
350 auto ptsCnt = sdata->pathCoords(&pts);
352 //No actual shape data
353 if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
358 dash.ptStart = {0, 0};
360 dash.curOpGap = false;
362 const float* pattern;
363 dash.cnt = sdata->strokeDash(&pattern);
364 assert(dash.cnt > 0 && pattern);
366 //Is it safe to mutual exclusive?
367 dash.pattern = const_cast<float*>(pattern);
368 dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
369 assert(dash.outline);
370 dash.outline->opened = true;
373 auto outlinePtsCnt = 0;
374 auto outlineCntrsCnt = 0;
376 for (uint32_t i = 0; i < cmdCnt; ++i) {
377 switch(*(cmds + i)) {
378 case PathCommand::Close: {
382 case PathCommand::MoveTo: {
387 case PathCommand::LineTo: {
391 case PathCommand::CubicTo: {
398 ++outlinePtsCnt; //for close
399 ++outlineCntrsCnt; //for end
401 //Reserve Approximitely 20x...
402 _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
403 _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
405 while (cmdCnt-- > 0) {
407 case PathCommand::Close: {
408 _dashLineTo(dash, &dash.ptStart, transform);
411 case PathCommand::MoveTo: {
414 dash.curLen = *dash.pattern;
415 dash.curOpGap = false;
416 dash.ptStart = dash.ptCur = *pts;
420 case PathCommand::LineTo: {
421 _dashLineTo(dash, pts, transform);
425 case PathCommand::CubicTo: {
426 _dashCubicTo(dash, pts, pts + 1, pts + 2, transform);
434 _outlineEnd(*dash.outline);
440 bool _fastTrack(const SwOutline* outline)
442 //Fast Track: Othogonal rectangle?
443 if (outline->ptsCnt != 5) return false;
445 auto pt1 = outline->pts + 0;
446 auto pt2 = outline->pts + 1;
447 auto pt3 = outline->pts + 2;
448 auto pt4 = outline->pts + 3;
450 auto min1 = pt1->y < pt3->y ? pt1 : pt3;
451 auto min2 = pt2->y < pt4->y ? pt2 : pt4;
452 if (min1->y != min2->y) return false;
454 SwCoord len1 = pow(pt1->x - pt3->x, 2) + pow(pt1->y - pt3->y, 2);
455 SwCoord len2 = pow(pt2->x - pt4->x, 2) + pow(pt2->y - pt4->y, 2);
456 if (len1 == len2) return true;
462 /************************************************************************/
463 /* External Class Implementation */
464 /************************************************************************/
466 bool shapePrepare(SwShape* shape, const Shape* sdata, const SwSize& clip, const Matrix* transform)
468 if (!shapeGenOutline(shape, sdata, transform)) return false;
470 if (!_updateBBox(shape->outline, shape->bbox)) return false;
472 if (!_checkValid(shape->outline, shape->bbox, clip)) return false;
478 bool shapeGenRle(SwShape* shape, TVG_UNUSED const Shape* sdata, const SwSize& clip, bool antiAlias)
480 //FIXME: Should we draw it?
482 //if (shape.outline->opened) return true;
484 //Case A: Fast Track Rectangle Drawing
485 if ((shape->rect = _fastTrack(shape->outline))) return true;
486 //Case B: Normale Shape RLE Drawing
487 if ((shape->rle = rleRender(shape->outline, shape->bbox, clip, antiAlias))) return true;
493 void shapeDelOutline(SwShape* shape)
495 auto outline = shape->outline;
496 _delOutline(outline);
497 shape->outline = nullptr;
501 void shapeReset(SwShape* shape)
503 shapeDelOutline(shape);
505 shape->rle = nullptr;
507 _initBBox(shape->bbox);
511 bool shapeGenOutline(SwShape* shape, const Shape* sdata, const Matrix* transform)
515 const PathCommand* cmds = nullptr;
516 auto cmdCnt = sdata->pathCommands(&cmds);
518 const Point* pts = nullptr;
519 auto ptsCnt = sdata->pathCoords(&pts);
521 //No actual shape data
522 if (cmdCnt == 0 || ptsCnt == 0) return false;
525 auto outlinePtsCnt = 0;
526 auto outlineCntrsCnt = 0;
528 for (uint32_t i = 0; i < cmdCnt; ++i) {
529 switch(*(cmds + i)) {
530 case PathCommand::Close: {
534 case PathCommand::MoveTo: {
539 case PathCommand::LineTo: {
543 case PathCommand::CubicTo: {
550 ++outlinePtsCnt; //for close
551 ++outlineCntrsCnt; //for end
553 auto outline = shape->outline;
554 if (!outline) outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
556 outline->opened = true;
558 _growOutlinePoint(*outline, outlinePtsCnt);
559 _growOutlineContour(*outline, outlineCntrsCnt);
564 while (cmdCnt-- > 0) {
566 case PathCommand::Close: {
567 _outlineClose(*outline);
571 case PathCommand::MoveTo: {
572 _outlineMoveTo(*outline, pts, transform);
576 case PathCommand::LineTo: {
577 _outlineLineTo(*outline, pts, transform);
581 case PathCommand::CubicTo: {
582 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
590 _outlineEnd(*outline);
592 if (closed) outline->opened = false;
595 //outline->flags = SwOutline::FillRule::Winding;
597 shape->outline = outline;
603 void shapeFree(SwShape* shape)
605 shapeDelOutline(shape);
610 rleFree(shape->strokeRle);
611 strokeFree(shape->stroke);
616 void shapeDelStroke(SwShape* shape)
618 if (!shape->stroke) return;
619 rleFree(shape->strokeRle);
620 shape->strokeRle = nullptr;
621 strokeFree(shape->stroke);
622 shape->stroke = nullptr;
626 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform)
628 if (!shape->stroke) shape->stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
629 auto stroke = shape->stroke;
632 strokeReset(stroke, sdata, transform);
634 rleFree(shape->strokeRle);
635 shape->strokeRle = nullptr;
639 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwSize& clip)
643 SwOutline* shapeOutline = nullptr;
646 if (sdata->strokeDash(nullptr) > 0) {
647 shapeOutline = _genDashOutline(sdata, transform);
648 if (!shapeOutline) return false;
649 //Normal Style stroke
651 if (!shape->outline) {
652 if (!shapeGenOutline(shape, sdata, transform)) return false;
654 shapeOutline = shape->outline;
657 if (!strokeParseOutline(shape->stroke, *shapeOutline)) return false;
659 auto strokeOutline = strokeExportOutline(shape->stroke);
660 if (!strokeOutline) return false;
663 _updateBBox(strokeOutline, bbox);
665 if (!_checkValid(strokeOutline, bbox, clip)) return false;
667 shape->strokeRle = rleRender(strokeOutline, bbox, clip, true);
669 _delOutline(strokeOutline);
675 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, bool ctable)
677 return fillGenColorTable(shape->fill, fill, transform, surface, ctable);
681 void shapeResetFill(SwShape* shape)
684 shape->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
685 if (!shape->fill) return;
687 fillReset(shape->fill);
691 void shapeDelFill(SwShape* shape)
693 if (!shape->fill) return;
694 fillFree(shape->fill);
695 shape->fill = nullptr;
699 #endif /* _TVG_SW_SHAPE_H_ */