2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef _TVG_SW_SHAPE_H_
18 #define _TVG_SW_SHAPE_H_
20 #include "tvgSwCommon.h"
22 /************************************************************************/
23 /* Internal Class Implementation */
24 /************************************************************************/
33 static float _lineLength(const Point& pt1, const Point& pt2)
35 /* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm.
36 With alpha = 1, beta = 3/8, giving results with the largest error less
37 than 7% compared to the exact value. */
38 Point diff = {pt2.x - pt1.x, pt2.y - pt1.y};
39 if (diff.x < 0) diff.x = -diff.x;
40 if (diff.y < 0) diff.y = -diff.y;
41 return (diff.x > diff.y) ? (diff.x + diff.y * 0.375f) : (diff.y + diff.x * 0.375f);
45 static void _lineSplitAt(const Line& cur, float at, Line& left, Line& right)
47 auto len = _lineLength(cur.pt1, cur.pt2);
48 auto dx = ((cur.pt2.x - cur.pt1.x) / len) * at;
49 auto dy = ((cur.pt2.y - cur.pt1.y) / len) * at;
51 left.pt2.x = left.pt1.x + dx;
52 left.pt2.y = left.pt1.y + dy;
58 static void _growOutlineContour(SwOutline& outline, uint32_t n)
60 if (outline.reservedCntrsCnt >= outline.cntrsCnt + n) return;
61 outline.reservedCntrsCnt = outline.cntrsCnt + n;
62 outline.cntrs = static_cast<uint32_t*>(realloc(outline.cntrs, outline.reservedCntrsCnt * sizeof(uint32_t)));
63 assert(outline.cntrs);
67 static void _growOutlinePoint(SwOutline& outline, uint32_t n)
69 if (outline.reservedPtsCnt >= outline.ptsCnt + n) return;
70 outline.reservedPtsCnt = outline.ptsCnt + n;
71 outline.pts = static_cast<SwPoint*>(realloc(outline.pts, outline.reservedPtsCnt * sizeof(SwPoint)));
73 outline.types = static_cast<uint8_t*>(realloc(outline.types, outline.reservedPtsCnt * sizeof(uint8_t)));
74 assert(outline.types);
78 static void _delOutline(SwOutline* outline)
82 if (outline->cntrs) free(outline->cntrs);
83 if (outline->pts) free(outline->pts);
84 if (outline->types) free(outline->types);
89 static void _outlineEnd(SwOutline& outline)
91 _growOutlineContour(outline, 1);
92 if (outline.ptsCnt > 0) {
93 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
99 static inline SwPoint _transform(const Point* to, const Matrix* transform)
101 if (!transform) return {TO_SWCOORD(to->x), TO_SWCOORD(to->y)};
103 auto tx = round(to->x * transform->e11 + to->y * transform->e12 + transform->e13);
104 auto ty = round(to->x * transform->e21 + to->y * transform->e22 + transform->e23);
106 return {TO_SWCOORD(tx), TO_SWCOORD(ty)};
110 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)
134 _growOutlinePoint(outline, 1);
136 outline.pts[outline.ptsCnt] = _transform(to, transform);
137 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
142 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
144 assert(ctrl1 && ctrl2 && to);
146 _growOutlinePoint(outline, 3);
148 outline.pts[outline.ptsCnt] = _transform(ctrl1, transform);
149 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
152 outline.pts[outline.ptsCnt] = _transform(ctrl2, transform);
153 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
156 outline.pts[outline.ptsCnt] = _transform(to, transform);
157 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
162 static void _outlineClose(SwOutline& outline)
166 if (outline.cntrsCnt > 0) {
167 i = outline.cntrs[outline.cntrsCnt - 1] + 1;
172 //Make sure there is at least one point in the current path
173 if (outline.ptsCnt == i) {
174 outline.opened = true;
179 _growOutlinePoint(outline, 1);
181 outline.pts[outline.ptsCnt] = outline.pts[i];
182 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
185 outline.opened = false;
189 static void _initBBox(SwBBox& bbox)
191 bbox.min.x = bbox.min.y = 0;
192 bbox.max.x = bbox.max.y = 0;
196 static bool _updateBBox(SwOutline* outline, SwBBox& bbox)
198 if (!outline) return false;
200 auto pt = outline->pts;
203 if (outline->ptsCnt <= 0) {
215 for(uint32_t i = 1; i < outline->ptsCnt; ++i, ++pt) {
216 if (xMin > pt->x) xMin = pt->x;
217 if (xMax < pt->x) xMax = pt->x;
218 if (yMin > pt->y) yMin = pt->y;
219 if (yMax < pt->y) yMax = pt->y;
221 bbox.min.x = xMin >> 6;
222 bbox.max.x = (xMax + 63) >> 6;
223 bbox.min.y = yMin >> 6;
224 bbox.max.y = (yMax + 63) >> 6;
226 if (xMax - xMin < 1 && yMax - yMin < 1) return false;
232 static bool _checkValid(const SwOutline* outline, const SwBBox& bbox, const SwSize& clip)
236 if (outline->ptsCnt == 0 || outline->cntrsCnt <= 0) return false;
239 if (bbox.min.x >= clip.w || bbox.min.y >= clip.h || bbox.max.x <= 0 || bbox.max.y <= 0) return false;
245 static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* transform)
247 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
248 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
250 Line cur = {dash.ptCur, *to};
251 auto len = _lineLength(cur.pt1, cur.pt2);
253 if (len < dash.curLen) {
255 if (!dash.curOpGap) {
256 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
257 _outlineLineTo(*dash.outline, to, transform);
260 while (len > dash.curLen) {
263 _lineSplitAt(cur, dash.curLen, left, right);;
264 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
265 if (!dash.curOpGap) {
266 _outlineMoveTo(*dash.outline, &left.pt1, transform);
267 _outlineLineTo(*dash.outline, &left.pt2, transform);
269 dash.curLen = dash.pattern[dash.curIdx];
270 dash.curOpGap = !dash.curOpGap;
272 dash.ptCur = cur.pt1;
276 if (!dash.curOpGap) {
277 _outlineMoveTo(*dash.outline, &cur.pt1, transform);
278 _outlineLineTo(*dash.outline, &cur.pt2, transform);
280 if (dash.curLen < 1) {
282 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
283 dash.curLen = dash.pattern[dash.curIdx];
284 dash.curOpGap = !dash.curOpGap;
291 static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
293 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
294 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
296 Bezier cur = { dash.ptCur, *ctrl1, *ctrl2, *to};
297 auto len = bezLength(cur);
299 if (len < dash.curLen) {
301 if (!dash.curOpGap) {
302 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
303 _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to, transform);
306 while (len > dash.curLen) {
309 bezSplitAt(cur, dash.curLen, left, right);
310 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
311 if (!dash.curOpGap) {
312 _outlineMoveTo(*dash.outline, &left.start, transform);
313 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform);
315 dash.curLen = dash.pattern[dash.curIdx];
316 dash.curOpGap = !dash.curOpGap;
318 dash.ptCur = right.start;
322 if (!dash.curOpGap) {
323 _outlineMoveTo(*dash.outline, &cur.start, transform);
324 _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform);
326 if (dash.curLen < 1) {
328 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
329 dash.curLen = dash.pattern[dash.curIdx];
330 dash.curOpGap = !dash.curOpGap;
337 SwOutline* _genDashOutline(const Shape* sdata, const Matrix* transform)
341 const PathCommand* cmds = nullptr;
342 auto cmdCnt = sdata->pathCommands(&cmds);
344 const Point* pts = nullptr;
345 auto ptsCnt = sdata->pathCoords(&pts);
347 //No actual shape data
348 if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
353 dash.ptStart = {0, 0};
355 dash.curOpGap = false;
357 const float* pattern;
358 dash.cnt = sdata->strokeDash(&pattern);
359 assert(dash.cnt > 0 && pattern);
361 //Is it safe to mutual exclusive?
362 dash.pattern = const_cast<float*>(pattern);
363 dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
364 assert(dash.outline);
365 dash.outline->opened = true;
368 auto outlinePtsCnt = 0;
369 auto outlineCntrsCnt = 0;
371 for (uint32_t i = 0; i < cmdCnt; ++i) {
372 switch(*(cmds + i)) {
373 case PathCommand::Close: {
377 case PathCommand::MoveTo: {
382 case PathCommand::LineTo: {
386 case PathCommand::CubicTo: {
393 ++outlinePtsCnt; //for close
394 ++outlineCntrsCnt; //for end
396 //Reserve Approximitely 20x...
397 _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
398 _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
400 while (cmdCnt-- > 0) {
402 case PathCommand::Close: {
403 _dashLineTo(dash, &dash.ptStart, transform);
406 case PathCommand::MoveTo: {
409 dash.curLen = *dash.pattern;
410 dash.curOpGap = false;
411 dash.ptStart = dash.ptCur = *pts;
415 case PathCommand::LineTo: {
416 _dashLineTo(dash, pts, transform);
420 case PathCommand::CubicTo: {
421 _dashCubicTo(dash, pts, pts + 1, pts + 2, transform);
429 _outlineEnd(*dash.outline);
435 bool _fastTrack(const SwOutline* outline)
437 //Fast Track: Othogonal rectangle?
438 if (outline->ptsCnt != 5) return false;
440 auto pt1 = outline->pts + 0;
441 auto pt2 = outline->pts + 1;
442 auto pt3 = outline->pts + 2;
443 auto pt4 = outline->pts + 3;
445 auto min1 = pt1->y < pt3->y ? pt1 : pt3;
446 auto min2 = pt2->y < pt4->y ? pt2 : pt4;
447 if (min1->y != min2->y) return false;
449 SwCoord len1 = pow(pt1->x - pt3->x, 2) + pow(pt1->y - pt3->y, 2);
450 SwCoord len2 = pow(pt2->x - pt4->x, 2) + pow(pt2->y - pt4->y, 2);
451 if (len1 == len2) return true;
457 /************************************************************************/
458 /* External Class Implementation */
459 /************************************************************************/
461 bool shapePrepare(SwShape& shape, const Shape* sdata, const SwSize& clip, const Matrix* transform)
463 if (!shapeGenOutline(shape, sdata, transform)) return false;
465 if (!_updateBBox(shape.outline, shape.bbox)) return false;
467 if (!_checkValid(shape.outline, shape.bbox, clip)) return false;
473 bool shapeGenRle(SwShape& shape, const Shape* sdata, const SwSize& clip, bool antiAlias)
475 //FIXME: Should we draw it?
477 //if (shape.outline->opened) return true;
479 //Case A: Fast Track Rectangle Drawing
480 if ((shape.rect = _fastTrack(shape.outline))) return true;
481 //Case B: Normale Shape RLE Drawing
482 if ((shape.rle = rleRender(shape.outline, shape.bbox, clip, antiAlias))) return true;
488 void shapeDelOutline(SwShape& shape)
490 auto outline = shape.outline;
491 _delOutline(outline);
492 shape.outline = nullptr;
496 void shapeReset(SwShape& shape)
498 shapeDelOutline(shape);
502 _initBBox(shape.bbox);
506 bool shapeGenOutline(SwShape& shape, const Shape* sdata, const Matrix* transform)
510 const PathCommand* cmds = nullptr;
511 auto cmdCnt = sdata->pathCommands(&cmds);
513 const Point* pts = nullptr;
514 auto ptsCnt = sdata->pathCoords(&pts);
516 //No actual shape data
517 if (cmdCnt == 0 || ptsCnt == 0) return false;
520 auto outlinePtsCnt = 0;
521 auto outlineCntrsCnt = 0;
523 for (uint32_t i = 0; i < cmdCnt; ++i) {
524 switch(*(cmds + i)) {
525 case PathCommand::Close: {
529 case PathCommand::MoveTo: {
534 case PathCommand::LineTo: {
538 case PathCommand::CubicTo: {
545 ++outlinePtsCnt; //for close
546 ++outlineCntrsCnt; //for end
548 auto outline = shape.outline;
549 if (!outline) outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
551 outline->opened = true;
553 _growOutlinePoint(*outline, outlinePtsCnt);
554 _growOutlineContour(*outline, outlineCntrsCnt);
559 while (cmdCnt-- > 0) {
561 case PathCommand::Close: {
562 _outlineClose(*outline);
566 case PathCommand::MoveTo: {
567 _outlineMoveTo(*outline, pts, transform);
571 case PathCommand::LineTo: {
572 _outlineLineTo(*outline, pts, transform);
576 case PathCommand::CubicTo: {
577 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
585 _outlineEnd(*outline);
587 if (closed) outline->opened = false;
590 //outline->flags = SwOutline::FillRule::Winding;
592 shape.outline = outline;
598 void shapeFree(SwShape& shape)
600 shapeDelOutline(shape);
605 rleFree(shape.strokeRle);
606 strokeFree(shape.stroke);
611 void shapeDelStroke(SwShape& shape)
613 if (!shape.stroke) return;
614 rleFree(shape.strokeRle);
615 shape.strokeRle = nullptr;
616 strokeFree(shape.stroke);
617 shape.stroke = nullptr;
621 void shapeResetStroke(SwShape& shape, const Shape* sdata, const Matrix* transform)
623 if (!shape.stroke) shape.stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
624 auto stroke = shape.stroke;
627 strokeReset(*stroke, sdata, transform);
629 rleFree(shape.strokeRle);
630 shape.strokeRle = nullptr;
634 bool shapeGenStrokeRle(SwShape& shape, const Shape* sdata, const Matrix* transform, const SwSize& clip)
638 SwOutline* shapeOutline = nullptr;
641 if (sdata->strokeDash(nullptr) > 0) {
642 shapeOutline = _genDashOutline(sdata, transform);
643 if (!shapeOutline) return false;
644 //Normal Style stroke
646 if (!shape.outline) {
647 if (!shapeGenOutline(shape, sdata, transform)) return false;
649 shapeOutline = shape.outline;
652 if (!strokeParseOutline(*shape.stroke, *shapeOutline)) return false;
654 auto strokeOutline = strokeExportOutline(*shape.stroke);
655 if (!strokeOutline) return false;
658 _updateBBox(strokeOutline, bbox);
660 if (!_checkValid(strokeOutline, bbox, clip)) return false;
662 shape.strokeRle = rleRender(strokeOutline, bbox, clip, true);
664 _delOutline(strokeOutline);
670 bool shapeGenFillColors(SwShape& shape, const Fill* fill, const Matrix* transform, bool ctable)
672 return fillGenColorTable(shape.fill, fill, transform, ctable);
676 void shapeResetFill(SwShape& shape)
678 if (!shape.fill) shape.fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
681 fillReset(shape.fill);
685 void shapeDelFill(SwShape& shape)
687 if (!shape.fill) return;
688 fillFree(shape.fill);
689 shape.fill = nullptr;
693 #endif /* _TVG_SW_SHAPE_H_ */