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) {
205 if (xMin > pt->x) xMin = pt->x;
206 if (xMax < pt->x) xMax = pt->x;
207 if (yMin > pt->y) yMin = pt->y;
208 if (yMax < pt->y) yMax = pt->y;
210 bbox.min.x = xMin >> 6;
211 bbox.max.x = (xMax + 63) >> 6;
212 bbox.min.y = yMin >> 6;
213 bbox.max.y = (yMax + 63) >> 6;
215 if (xMax - xMin < 1 || yMax - yMin < 1) return false;
221 static bool _checkValid(const SwOutline* outline, const SwBBox& bbox, const SwSize& clip)
225 if (outline->ptsCnt == 0 || outline->cntrsCnt <= 0) return false;
228 if (bbox.min.x >= clip.w || bbox.min.y >= clip.h || bbox.max.x <= 0 || bbox.max.y <= 0) return false;
234 static void _transformOutline(SwOutline* outline, const Matrix* transform)
236 if (!transform) return;
240 for(uint32_t i = 0; i < outline->ptsCnt; ++i) {
241 auto dx = static_cast<float>(outline->pts[i].x >> 6);
242 auto dy = static_cast<float>(outline->pts[i].y >> 6);
243 auto tx = dx * transform->e11 + dy * transform->e12 + transform->e31;
244 auto ty = dx * transform->e21 + dy * transform->e22 + transform->e32;
245 auto pt = Point{tx, ty};
246 outline->pts[i] = TO_SWPOINT(&pt);
251 static void _dashLineTo(SwDashStroke& dash, const Point* to)
253 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
254 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
256 Line cur = {dash.ptCur, *to};
257 auto len = _lineLength(cur.pt1, cur.pt2);
259 if (len < dash.curLen) {
261 if (!dash.curOpGap) {
262 _outlineMoveTo(*dash.outline, &dash.ptCur);
263 _outlineLineTo(*dash.outline, to);
266 while (len > dash.curLen) {
269 _lineSplitAt(cur, dash.curLen, left, right);;
270 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
271 if (!dash.curOpGap) {
272 _outlineMoveTo(*dash.outline, &left.pt1);
273 _outlineLineTo(*dash.outline, &left.pt2);
275 dash.curLen = dash.pattern[dash.curIdx];
276 dash.curOpGap = !dash.curOpGap;
278 dash.ptCur = cur.pt1;
282 if (!dash.curOpGap) {
283 _outlineMoveTo(*dash.outline, &cur.pt1);
284 _outlineLineTo(*dash.outline, &cur.pt2);
286 if (dash.curLen < 1) {
288 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
289 dash.curLen = dash.pattern[dash.curIdx];
290 dash.curOpGap = !dash.curOpGap;
297 static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to)
299 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
300 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
302 Bezier cur = { dash.ptCur, *ctrl1, *ctrl2, *to};
303 auto len = bezLength(cur);
305 if (len < dash.curLen) {
307 if (!dash.curOpGap) {
308 _outlineMoveTo(*dash.outline, &dash.ptCur);
309 _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to);
312 while (len > dash.curLen) {
315 bezSplitAt(cur, dash.curLen, left, right);
316 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
317 if (!dash.curOpGap) {
318 _outlineMoveTo(*dash.outline, &left.start);
319 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end);
321 dash.curLen = dash.pattern[dash.curIdx];
322 dash.curOpGap = !dash.curOpGap;
324 dash.ptCur = right.start;
328 if (!dash.curOpGap) {
329 _outlineMoveTo(*dash.outline, &cur.start);
330 _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end);
332 if (dash.curLen < 1) {
334 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
335 dash.curLen = dash.pattern[dash.curIdx];
336 dash.curOpGap = !dash.curOpGap;
343 SwOutline* _genDashOutline(const Shape* sdata)
347 const PathCommand* cmds = nullptr;
348 auto cmdCnt = sdata->pathCommands(&cmds);
350 const Point* pts = nullptr;
351 auto ptsCnt = sdata->pathCoords(&pts);
353 //No actual shape data
354 if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
359 dash.ptStart = {0, 0};
361 dash.curOpGap = false;
363 const float* pattern;
364 dash.cnt = sdata->strokeDash(&pattern);
365 assert(dash.cnt > 0 && pattern);
367 //Is it safe to mutual exclusive?
368 dash.pattern = const_cast<float*>(pattern);
369 dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
370 assert(dash.outline);
371 dash.outline->opened = true;
374 auto outlinePtsCnt = 0;
375 auto outlineCntrsCnt = 0;
377 for (uint32_t i = 0; i < cmdCnt; ++i) {
378 switch(*(cmds + i)) {
379 case PathCommand::Close: {
383 case PathCommand::MoveTo: {
388 case PathCommand::LineTo: {
392 case PathCommand::CubicTo: {
399 ++outlinePtsCnt; //for close
400 ++outlineCntrsCnt; //for end
402 //Reserve Approximitely 20x...
403 _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
404 _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
406 while (cmdCnt-- > 0) {
408 case PathCommand::Close: {
409 _dashLineTo(dash, &dash.ptStart);
412 case PathCommand::MoveTo: {
415 dash.curLen = *dash.pattern;
416 dash.curOpGap = false;
417 dash.ptStart = dash.ptCur = *pts;
421 case PathCommand::LineTo: {
422 _dashLineTo(dash, pts);
426 case PathCommand::CubicTo: {
427 _dashCubicTo(dash, pts, pts + 1, pts + 2);
435 _outlineEnd(*dash.outline);
441 /************************************************************************/
442 /* External Class Implementation */
443 /************************************************************************/
445 bool shapeGenRle(SwShape& shape, const Shape* sdata, const SwSize& clip, const Matrix* transform)
447 if (!shapeGenOutline(shape, sdata)) return false;
449 _transformOutline(shape.outline, transform);
451 if (!_updateBBox(shape.outline, shape.bbox)) goto end;
453 if (!_checkValid(shape.outline, shape.bbox, clip)) goto end;
455 shape.rle = rleRender(shape.outline, shape.bbox, clip);
457 if (shape.rle) return true;
462 void shapeDelOutline(SwShape& shape)
464 auto outline = shape.outline;
465 _delOutline(outline);
466 shape.outline = nullptr;
470 void shapeReset(SwShape& shape)
472 shapeDelOutline(shape);
475 _initBBox(shape.bbox);
479 bool shapeGenOutline(SwShape& shape, const Shape* sdata)
483 const PathCommand* cmds = nullptr;
484 auto cmdCnt = sdata->pathCommands(&cmds);
486 const Point* pts = nullptr;
487 auto ptsCnt = sdata->pathCoords(&pts);
489 //No actual shape data
490 if (cmdCnt == 0 || ptsCnt == 0) return false;
493 auto outlinePtsCnt = 0;
494 auto outlineCntrsCnt = 0;
496 for (uint32_t i = 0; i < cmdCnt; ++i) {
497 switch(*(cmds + i)) {
498 case PathCommand::Close: {
502 case PathCommand::MoveTo: {
507 case PathCommand::LineTo: {
511 case PathCommand::CubicTo: {
518 ++outlinePtsCnt; //for close
519 ++outlineCntrsCnt; //for end
521 auto outline = shape.outline;
522 if (!outline) outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
524 outline->opened = true;
526 _growOutlinePoint(*outline, outlinePtsCnt);
527 _growOutlineContour(*outline, outlineCntrsCnt);
530 while (cmdCnt-- > 0) {
532 case PathCommand::Close: {
533 _outlineClose(*outline);
536 case PathCommand::MoveTo: {
537 _outlineMoveTo(*outline, pts);
541 case PathCommand::LineTo: {
542 _outlineLineTo(*outline, pts);
546 case PathCommand::CubicTo: {
547 _outlineCubicTo(*outline, pts, pts + 1, pts + 2);
555 _outlineEnd(*outline);
558 //outline->flags = SwOutline::FillRule::Winding;
560 shape.outline = outline;
566 void shapeFree(SwShape& shape)
568 shapeDelOutline(shape);
574 rleFree(shape.strokeRle);
575 strokeFree(shape.stroke);
580 void shapeDelStroke(SwShape& shape)
582 if (!shape.stroke) return;
583 rleFree(shape.strokeRle);
584 shape.strokeRle = nullptr;
585 strokeFree(shape.stroke);
586 shape.stroke = nullptr;
590 void shapeResetStroke(SwShape& shape, const Shape* sdata)
592 if (!shape.stroke) shape.stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
593 auto stroke = shape.stroke;
596 strokeReset(*stroke, sdata);
598 rleFree(shape.strokeRle);
599 shape.strokeRle = nullptr;
603 bool shapeGenStrokeRle(SwShape& shape, const Shape* sdata, const SwSize& clip)
607 SwOutline* shapeOutline = nullptr;
610 if (sdata->strokeDash(nullptr) > 0) {
611 shapeOutline = _genDashOutline(sdata);
612 if (!shapeOutline) return false;
614 //Normal Style stroke
616 if (!shape.outline) {
617 if (!shapeGenOutline(shape, sdata)) return false;
619 shapeOutline = shape.outline;
622 if (!strokeParseOutline(*shape.stroke, *shapeOutline)) return false;
624 auto strokeOutline = strokeExportOutline(*shape.stroke);
625 if (!strokeOutline) return false;
628 _updateBBox(strokeOutline, bbox);
630 if (!_checkValid(strokeOutline, bbox, clip)) return false;
632 shape.strokeRle = rleRender(strokeOutline, bbox, clip);
634 _delOutline(strokeOutline);
640 bool shapeGenFillColors(SwShape& shape, const Fill* fill, const Matrix* transform, bool ctable)
642 return fillGenColorTable(shape.fill, fill, transform, ctable);
646 void shapeResetFill(SwShape& shape)
648 if (!shape.fill) shape.fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
651 fillReset(shape.fill);
655 void shapeDelFill(SwShape& shape)
657 if (!shape.fill) return;
658 fillFree(shape.fill);
659 shape.fill = nullptr;
663 #endif /* _TVG_SW_SHAPE_H_ */