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)));
82 static void _growOutlinePoint(SwOutline& outline, uint32_t n)
84 if (outline.reservedPtsCnt >= outline.ptsCnt + n) return;
85 outline.reservedPtsCnt = outline.ptsCnt + n;
86 outline.pts = static_cast<SwPoint*>(realloc(outline.pts, outline.reservedPtsCnt * sizeof(SwPoint)));
87 outline.types = static_cast<uint8_t*>(realloc(outline.types, outline.reservedPtsCnt * sizeof(uint8_t)));
91 static void _delOutline(SwOutline* outline)
95 if (outline->cntrs) free(outline->cntrs);
96 if (outline->pts) free(outline->pts);
97 if (outline->types) free(outline->types);
102 static void _outlineEnd(SwOutline& outline)
104 _growOutlineContour(outline, 1);
105 if (outline.ptsCnt > 0) {
106 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
112 static void _outlineMoveTo(SwOutline& outline, const Point* to, const Matrix* transform)
114 _growOutlinePoint(outline, 1);
116 outline.pts[outline.ptsCnt] = _transform(to, transform);
118 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
120 if (outline.ptsCnt > 0) {
121 _growOutlineContour(outline, 1);
122 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
130 static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix* transform)
132 _growOutlinePoint(outline, 1);
134 outline.pts[outline.ptsCnt] = _transform(to, transform);
135 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
140 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
142 _growOutlinePoint(outline, 3);
144 outline.pts[outline.ptsCnt] = _transform(ctrl1, transform);
145 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
148 outline.pts[outline.ptsCnt] = _transform(ctrl2, transform);
149 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
152 outline.pts[outline.ptsCnt] = _transform(to, transform);
153 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
158 static void _outlineClose(SwOutline& outline)
162 if (outline.cntrsCnt > 0) {
163 i = outline.cntrs[outline.cntrsCnt - 1] + 1;
168 //Make sure there is at least one point in the current path
169 if (outline.ptsCnt == i) {
170 outline.opened = true;
175 _growOutlinePoint(outline, 1);
177 outline.pts[outline.ptsCnt] = outline.pts[i];
178 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
181 outline.opened = false;
185 static void _initBBox(SwBBox& bbox)
187 bbox.min.x = bbox.min.y = 0;
188 bbox.max.x = bbox.max.y = 0;
192 static bool _updateBBox(SwOutline* outline, SwBBox& bbox)
194 if (!outline) return false;
196 auto pt = outline->pts;
198 if (outline->ptsCnt <= 0) {
210 for(uint32_t i = 1; i < outline->ptsCnt; ++i, ++pt) {
211 if (xMin > pt->x) xMin = pt->x;
212 if (xMax < pt->x) xMax = pt->x;
213 if (yMin > pt->y) yMin = pt->y;
214 if (yMax < pt->y) yMax = pt->y;
216 bbox.min.x = xMin >> 6;
217 bbox.max.x = (xMax + 63) >> 6;
218 bbox.min.y = yMin >> 6;
219 bbox.max.y = (yMax + 63) >> 6;
221 if (xMax - xMin < 1 && yMax - yMin < 1) return false;
227 static bool _checkValid(const SwOutline* outline, const SwBBox& bbox, const SwSize& clip)
229 if (outline->ptsCnt == 0 || outline->cntrsCnt <= 0) return false;
232 if (bbox.min.x >= clip.w || bbox.min.y >= clip.h || bbox.max.x <= 0 || bbox.max.y <= 0) return false;
238 static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* transform)
240 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
241 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
243 Line cur = {dash.ptCur, *to};
244 auto len = _lineLength(cur.pt1, cur.pt2);
246 if (len < dash.curLen) {
248 if (!dash.curOpGap) {
249 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
250 _outlineLineTo(*dash.outline, to, transform);
253 while (len > dash.curLen) {
256 _lineSplitAt(cur, dash.curLen, left, right);;
257 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
258 if (!dash.curOpGap) {
259 _outlineMoveTo(*dash.outline, &left.pt1, transform);
260 _outlineLineTo(*dash.outline, &left.pt2, transform);
262 dash.curLen = dash.pattern[dash.curIdx];
263 dash.curOpGap = !dash.curOpGap;
265 dash.ptCur = cur.pt1;
269 if (!dash.curOpGap) {
270 _outlineMoveTo(*dash.outline, &cur.pt1, transform);
271 _outlineLineTo(*dash.outline, &cur.pt2, transform);
273 if (dash.curLen < 1) {
275 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
276 dash.curLen = dash.pattern[dash.curIdx];
277 dash.curOpGap = !dash.curOpGap;
284 static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
286 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
287 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
289 Bezier cur = { dash.ptCur, *ctrl1, *ctrl2, *to};
290 auto len = bezLength(cur);
292 if (len < dash.curLen) {
294 if (!dash.curOpGap) {
295 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
296 _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to, transform);
299 while (len > dash.curLen) {
302 bezSplitAt(cur, dash.curLen, left, right);
303 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
304 if (!dash.curOpGap) {
305 _outlineMoveTo(*dash.outline, &left.start, transform);
306 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform);
308 dash.curLen = dash.pattern[dash.curIdx];
309 dash.curOpGap = !dash.curOpGap;
311 dash.ptCur = right.start;
315 if (!dash.curOpGap) {
316 _outlineMoveTo(*dash.outline, &cur.start, transform);
317 _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform);
319 if (dash.curLen < 1) {
321 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
322 dash.curLen = dash.pattern[dash.curIdx];
323 dash.curOpGap = !dash.curOpGap;
330 SwOutline* _genDashOutline(const Shape* sdata, const Matrix* transform)
332 const PathCommand* cmds = nullptr;
333 auto cmdCnt = sdata->pathCommands(&cmds);
335 const Point* pts = nullptr;
336 auto ptsCnt = sdata->pathCoords(&pts);
338 //No actual shape data
339 if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
344 dash.ptStart = {0, 0};
346 dash.curOpGap = false;
348 const float* pattern;
349 dash.cnt = sdata->strokeDash(&pattern);
351 //Is it safe to mutual exclusive?
352 dash.pattern = const_cast<float*>(pattern);
353 dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
354 dash.outline->opened = true;
357 auto outlinePtsCnt = 0;
358 auto outlineCntrsCnt = 0;
360 for (uint32_t i = 0; i < cmdCnt; ++i) {
361 switch(*(cmds + i)) {
362 case PathCommand::Close: {
366 case PathCommand::MoveTo: {
371 case PathCommand::LineTo: {
375 case PathCommand::CubicTo: {
382 ++outlinePtsCnt; //for close
383 ++outlineCntrsCnt; //for end
385 //Reserve Approximitely 20x...
386 _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
387 _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
389 while (cmdCnt-- > 0) {
391 case PathCommand::Close: {
392 _dashLineTo(dash, &dash.ptStart, transform);
395 case PathCommand::MoveTo: {
398 dash.curLen = *dash.pattern;
399 dash.curOpGap = false;
400 dash.ptStart = dash.ptCur = *pts;
404 case PathCommand::LineTo: {
405 _dashLineTo(dash, pts, transform);
409 case PathCommand::CubicTo: {
410 _dashCubicTo(dash, pts, pts + 1, pts + 2, transform);
418 _outlineEnd(*dash.outline);
424 bool _fastTrack(const SwOutline* outline)
426 //Fast Track: Othogonal rectangle?
427 if (outline->ptsCnt != 5) return false;
429 auto pt1 = outline->pts + 0;
430 auto pt2 = outline->pts + 1;
431 auto pt3 = outline->pts + 2;
432 auto pt4 = outline->pts + 3;
434 auto min1 = pt1->y < pt3->y ? pt1 : pt3;
435 auto min2 = pt2->y < pt4->y ? pt2 : pt4;
436 if (min1->y != min2->y) return false;
438 SwCoord len1 = pow(pt1->x - pt3->x, 2) + pow(pt1->y - pt3->y, 2);
439 SwCoord len2 = pow(pt2->x - pt4->x, 2) + pow(pt2->y - pt4->y, 2);
440 if (len1 == len2) return true;
446 /************************************************************************/
447 /* External Class Implementation */
448 /************************************************************************/
450 bool shapePrepare(SwShape* shape, const Shape* sdata, const SwSize& clip, const Matrix* transform)
452 if (!shapeGenOutline(shape, sdata, transform)) return false;
454 if (!_updateBBox(shape->outline, shape->bbox)) return false;
456 if (!_checkValid(shape->outline, shape->bbox, clip)) return false;
462 bool shapeGenRle(SwShape* shape, TVG_UNUSED const Shape* sdata, const SwSize& clip, bool antiAlias)
464 //FIXME: Should we draw it?
466 //if (shape.outline->opened) return true;
468 //Case A: Fast Track Rectangle Drawing
469 if ((shape->rect = _fastTrack(shape->outline))) return true;
470 //Case B: Normale Shape RLE Drawing
471 if ((shape->rle = rleRender(shape->outline, shape->bbox, clip, antiAlias))) return true;
477 void shapeDelOutline(SwShape* shape)
479 auto outline = shape->outline;
480 _delOutline(outline);
481 shape->outline = nullptr;
485 void shapeReset(SwShape* shape)
487 shapeDelOutline(shape);
489 shape->rle = nullptr;
491 _initBBox(shape->bbox);
495 bool shapeGenOutline(SwShape* shape, const Shape* sdata, const Matrix* transform)
497 const PathCommand* cmds = nullptr;
498 auto cmdCnt = sdata->pathCommands(&cmds);
500 const Point* pts = nullptr;
501 auto ptsCnt = sdata->pathCoords(&pts);
503 //No actual shape data
504 if (cmdCnt == 0 || ptsCnt == 0) return false;
507 auto outlinePtsCnt = 0;
508 auto outlineCntrsCnt = 0;
510 for (uint32_t i = 0; i < cmdCnt; ++i) {
511 switch(*(cmds + i)) {
512 case PathCommand::Close: {
516 case PathCommand::MoveTo: {
521 case PathCommand::LineTo: {
525 case PathCommand::CubicTo: {
532 ++outlinePtsCnt; //for close
533 ++outlineCntrsCnt; //for end
535 auto outline = shape->outline;
536 if (!outline) outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
537 outline->opened = true;
539 _growOutlinePoint(*outline, outlinePtsCnt);
540 _growOutlineContour(*outline, outlineCntrsCnt);
545 while (cmdCnt-- > 0) {
547 case PathCommand::Close: {
548 _outlineClose(*outline);
552 case PathCommand::MoveTo: {
553 _outlineMoveTo(*outline, pts, transform);
557 case PathCommand::LineTo: {
558 _outlineLineTo(*outline, pts, transform);
562 case PathCommand::CubicTo: {
563 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
571 _outlineEnd(*outline);
573 if (closed) outline->opened = false;
576 //outline->flags = SwOutline::FillRule::Winding;
578 shape->outline = outline;
584 void shapeFree(SwShape* shape)
586 shapeDelOutline(shape);
591 rleFree(shape->strokeRle);
592 strokeFree(shape->stroke);
597 void shapeDelStroke(SwShape* shape)
599 if (!shape->stroke) return;
600 rleFree(shape->strokeRle);
601 shape->strokeRle = nullptr;
602 strokeFree(shape->stroke);
603 shape->stroke = nullptr;
607 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform)
609 if (!shape->stroke) shape->stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
610 auto stroke = shape->stroke;
613 strokeReset(stroke, sdata, transform);
615 rleFree(shape->strokeRle);
616 shape->strokeRle = nullptr;
620 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwSize& clip)
622 SwOutline* shapeOutline = nullptr;
625 if (sdata->strokeDash(nullptr) > 0) {
626 shapeOutline = _genDashOutline(sdata, transform);
627 if (!shapeOutline) return false;
628 //Normal Style stroke
630 if (!shape->outline) {
631 if (!shapeGenOutline(shape, sdata, transform)) return false;
633 shapeOutline = shape->outline;
636 if (!strokeParseOutline(shape->stroke, *shapeOutline)) return false;
638 auto strokeOutline = strokeExportOutline(shape->stroke);
639 if (!strokeOutline) return false;
642 _updateBBox(strokeOutline, bbox);
644 if (!_checkValid(strokeOutline, bbox, clip)) return false;
646 shape->strokeRle = rleRender(strokeOutline, bbox, clip, true);
648 _delOutline(strokeOutline);
654 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, bool ctable)
656 return fillGenColorTable(shape->fill, fill, transform, surface, ctable);
660 void shapeResetFill(SwShape* shape)
663 shape->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
664 if (!shape->fill) return;
666 fillReset(shape->fill);
670 void shapeDelFill(SwShape* shape)
672 if (!shape->fill) return;
673 fillFree(shape->fill);
674 shape->fill = nullptr;
678 #endif /* _TVG_SW_SHAPE_H_ */