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 void _outlineMoveTo(SwOutline& outline, const Point* to)
103 _growOutlinePoint(outline, 1);
105 outline.pts[outline.ptsCnt] = TO_SWPOINT(to);
106 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
108 if (outline.ptsCnt > 0) {
109 _growOutlineContour(outline, 1);
110 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
118 static void _outlineLineTo(SwOutline& outline, const Point* to)
122 _growOutlinePoint(outline, 1);
124 outline.pts[outline.ptsCnt] = TO_SWPOINT(to);
125 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
130 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to)
132 assert(ctrl1 && ctrl2 && to);
134 _growOutlinePoint(outline, 3);
136 outline.pts[outline.ptsCnt] = TO_SWPOINT(ctrl1);
137 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
140 outline.pts[outline.ptsCnt] = TO_SWPOINT(ctrl2);
141 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
144 outline.pts[outline.ptsCnt] = TO_SWPOINT(to);
145 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
150 static void _outlineClose(SwOutline& outline)
154 if (outline.cntrsCnt > 0) {
155 i = outline.cntrs[outline.cntrsCnt - 1] + 1;
160 //Make sure there is at least one point in the current path
161 if (outline.ptsCnt == i) {
162 outline.opened = true;
167 _growOutlinePoint(outline, 1);
169 outline.pts[outline.ptsCnt] = outline.pts[i];
170 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
173 outline.opened = false;
177 static void _initBBox(SwBBox& bbox)
179 bbox.min.x = bbox.min.y = 0;
180 bbox.max.x = bbox.max.y = 0;
184 static bool _updateBBox(SwOutline* outline, SwBBox& bbox)
186 if (!outline) return false;
188 auto pt = outline->pts;
191 if (outline->ptsCnt <= 0) {
203 for(uint32_t i = 1; i < outline->ptsCnt; ++i, ++pt) {
204 if (xMin > pt->x) xMin = pt->x;
205 if (xMax < pt->x) xMax = pt->x;
206 if (yMin > pt->y) yMin = pt->y;
207 if (yMax < pt->y) yMax = pt->y;
209 bbox.min.x = xMin >> 6;
210 bbox.max.x = (xMax + 63) >> 6;
211 bbox.min.y = yMin >> 6;
212 bbox.max.y = (yMax + 63) >> 6;
214 if (xMax - xMin < 1 && yMax - yMin < 1) return false;
220 static bool _checkValid(const SwOutline* outline, const SwBBox& bbox, const SwSize& clip)
224 if (outline->ptsCnt == 0 || outline->cntrsCnt <= 0) return false;
227 if (bbox.min.x >= clip.w || bbox.min.y >= clip.h || bbox.max.x <= 0 || bbox.max.y <= 0) return false;
233 static void _transformOutline(SwOutline* outline, const Matrix* transform)
235 if (!transform) return;
239 for(uint32_t i = 0; i < outline->ptsCnt; ++i) {
240 auto dx = static_cast<float>(outline->pts[i].x >> 6);
241 auto dy = static_cast<float>(outline->pts[i].y >> 6);
242 auto tx = dx * transform->e11 + dy * transform->e12 + transform->e31;
243 auto ty = dx * transform->e21 + dy * transform->e22 + transform->e32;
244 auto pt = Point{round(tx), round(ty)};
245 outline->pts[i] = TO_SWPOINT(&pt);
250 static void _dashLineTo(SwDashStroke& dash, const Point* to)
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);
262 _outlineLineTo(*dash.outline, to);
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);
272 _outlineLineTo(*dash.outline, &left.pt2);
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);
283 _outlineLineTo(*dash.outline, &cur.pt2);
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)
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);
308 _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to);
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);
318 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end);
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);
329 _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end);
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)
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);
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);
425 case PathCommand::CubicTo: {
426 _dashCubicTo(dash, pts, pts + 1, pts + 2);
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)) return false;
470 _transformOutline(shape.outline, transform);
472 if (!_updateBBox(shape.outline, shape.bbox)) return false;
474 if (!_checkValid(shape.outline, shape.bbox, clip)) return false;
480 bool shapeGenRle(SwShape& shape, const Shape* sdata, const SwSize& clip, bool antiAlias)
482 //FIXME: Should we draw it?
484 //if (shape.outline->opened) return true;
486 //Case A: Fast Track Rectangle Drawing
487 if ((shape.rect = _fastTrack(shape.outline))) return true;
488 //Case B: Normale Shape RLE Drawing
489 if ((shape.rle = rleRender(shape.outline, shape.bbox, clip, antiAlias))) return true;
495 void shapeDelOutline(SwShape& shape)
497 auto outline = shape.outline;
498 _delOutline(outline);
499 shape.outline = nullptr;
503 void shapeReset(SwShape& shape)
505 shapeDelOutline(shape);
509 _initBBox(shape.bbox);
513 bool shapeGenOutline(SwShape& shape, const Shape* sdata)
517 const PathCommand* cmds = nullptr;
518 auto cmdCnt = sdata->pathCommands(&cmds);
520 const Point* pts = nullptr;
521 auto ptsCnt = sdata->pathCoords(&pts);
523 //No actual shape data
524 if (cmdCnt == 0 || ptsCnt == 0) return false;
527 auto outlinePtsCnt = 0;
528 auto outlineCntrsCnt = 0;
530 for (uint32_t i = 0; i < cmdCnt; ++i) {
531 switch(*(cmds + i)) {
532 case PathCommand::Close: {
536 case PathCommand::MoveTo: {
541 case PathCommand::LineTo: {
545 case PathCommand::CubicTo: {
552 ++outlinePtsCnt; //for close
553 ++outlineCntrsCnt; //for end
555 auto outline = shape.outline;
556 if (!outline) outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
558 outline->opened = true;
560 _growOutlinePoint(*outline, outlinePtsCnt);
561 _growOutlineContour(*outline, outlineCntrsCnt);
566 while (cmdCnt-- > 0) {
568 case PathCommand::Close: {
569 _outlineClose(*outline);
573 case PathCommand::MoveTo: {
574 _outlineMoveTo(*outline, pts);
578 case PathCommand::LineTo: {
579 _outlineLineTo(*outline, pts);
583 case PathCommand::CubicTo: {
584 _outlineCubicTo(*outline, pts, pts + 1, pts + 2);
592 _outlineEnd(*outline);
594 if (closed) outline->opened = false;
597 //outline->flags = SwOutline::FillRule::Winding;
599 shape.outline = outline;
605 void shapeFree(SwShape& shape)
607 shapeDelOutline(shape);
612 rleFree(shape.strokeRle);
613 strokeFree(shape.stroke);
618 void shapeDelStroke(SwShape& shape)
620 if (!shape.stroke) return;
621 rleFree(shape.strokeRle);
622 shape.strokeRle = nullptr;
623 strokeFree(shape.stroke);
624 shape.stroke = nullptr;
628 void shapeResetStroke(SwShape& shape, const Shape* sdata)
630 if (!shape.stroke) shape.stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
631 auto stroke = shape.stroke;
634 strokeReset(*stroke, sdata);
636 rleFree(shape.strokeRle);
637 shape.strokeRle = nullptr;
641 bool shapeGenStrokeRle(SwShape& shape, const Shape* sdata, const SwSize& clip)
645 SwOutline* shapeOutline = nullptr;
648 if (sdata->strokeDash(nullptr) > 0) {
649 shapeOutline = _genDashOutline(sdata);
650 if (!shapeOutline) return false;
652 //Normal Style stroke
654 if (!shape.outline) {
655 if (!shapeGenOutline(shape, sdata)) return false;
657 shapeOutline = shape.outline;
660 if (!strokeParseOutline(*shape.stroke, *shapeOutline)) return false;
662 auto strokeOutline = strokeExportOutline(*shape.stroke);
663 if (!strokeOutline) return false;
666 _updateBBox(strokeOutline, bbox);
668 if (!_checkValid(strokeOutline, bbox, clip)) return false;
670 shape.strokeRle = rleRender(strokeOutline, bbox, clip, true);
672 _delOutline(strokeOutline);
678 bool shapeGenFillColors(SwShape& shape, const Fill* fill, const Matrix* transform, bool ctable)
680 return fillGenColorTable(shape.fill, fill, transform, ctable);
684 void shapeResetFill(SwShape& shape)
686 if (!shape.fill) shape.fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
689 fillReset(shape.fill);
693 void shapeDelFill(SwShape& shape)
695 if (!shape.fill) return;
696 fillFree(shape.fill);
697 shape.fill = nullptr;
701 #endif /* _TVG_SW_SHAPE_H_ */