Debian Codegraph @

thirdparty/thorvg/src/renderer/sw_engine/tvgSwStroke.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved.
3
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:
10
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13
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
20 * SOFTWARE.
21 */
22
23 #include <string.h>
24 #include <math.h>
25 #include "tvgSwCommon.h"
26
27 /************************************************************************/
28 /* Internal Class Implementation */
29 /************************************************************************/
30
31 static constexpr auto SW_STROKE_TAG_POINT = 1;
32 static constexpr auto SW_STROKE_TAG_CUBIC = 2;
33 static constexpr auto SW_STROKE_TAG_BEGIN = 4;
34 static constexpr auto SW_STROKE_TAG_END = 8;
35
36 static inline SwFixed SIDE_TO_ROTATE(const int32_t s)
37 {
38 return (SW_ANGLE_PI2 - static_cast<SwFixed>(s) * SW_ANGLE_PI);
39 }
40
41
42 static inline void SCALE(const SwStroke& stroke, SwPoint& pt)
43 {
44 pt.x = static_cast<SwCoord>(pt.x * stroke.sx);
45 pt.y = static_cast<SwCoord>(pt.y * stroke.sy);
46 }
47
48
49 static void _growBorder(SwStrokeBorder* border, uint32_t newPts)
50 {
51 auto maxOld = border->maxPts;
52 auto maxNew = border->ptsCnt + newPts;
53
54 if (maxNew <= maxOld) return;
55
56 auto maxCur = maxOld;
57
58 while (maxCur < maxNew)
59 maxCur += (maxCur >> 1) + 16;
60 //OPTIMIZE: use mempool!
61 border->pts = static_cast<SwPoint*>(realloc(border->pts, maxCur * sizeof(SwPoint)));
62 border->tags = static_cast<uint8_t*>(realloc(border->tags, maxCur * sizeof(uint8_t)));
63 border->maxPts = maxCur;
64 }
65
66
67 static void _borderClose(SwStrokeBorder* border, bool reverse)
68 {
69 auto start = border->start;
70 auto count = border->ptsCnt;
71
72 //Don't record empty paths!
73 if (count <= start + 1U) {
74 border->ptsCnt = start;
75 } else {
76 /* Copy the last point to the start of this sub-path,
77 since it contains the adjusted starting coordinates */
78 border->ptsCnt = --count;
79 border->pts[start] = border->pts[count];
80
81 if (reverse) {
82 //reverse the points
83 auto pt1 = border->pts + start + 1;
84 auto pt2 = border->pts + count - 1;
85
86 while (pt1 < pt2) {
87 auto tmp = *pt1;
88 *pt1 = *pt2;
89 *pt2 = tmp;
90 ++pt1;
91 --pt2;
92 }
93
94 //reverse the tags
95 auto tag1 = border->tags + start + 1;
96 auto tag2 = border->tags + count - 1;
97
98 while (tag1 < tag2) {
99 auto tmp = *tag1;
100 *tag1 = *tag2;
101 *tag2 = tmp;
102 ++tag1;
103 --tag2;
104 }
105 }
106
107 border->tags[start] |= SW_STROKE_TAG_BEGIN;
108 border->tags[count - 1] |= SW_STROKE_TAG_END;
109 }
110
111 border->start = -1;
112 border->movable = false;
113 }
114
115
116 static void _borderCubicTo(SwStrokeBorder* border, const SwPoint& ctrl1, const SwPoint& ctrl2, const SwPoint& to)
117 {
118 _growBorder(border, 3);
119
120 auto pt = border->pts + border->ptsCnt;
121 auto tag = border->tags + border->ptsCnt;
122
123 pt[0] = ctrl1;
124 pt[1] = ctrl2;
125 pt[2] = to;
126
127 tag[0] = SW_STROKE_TAG_CUBIC;
128 tag[1] = SW_STROKE_TAG_CUBIC;
129 tag[2] = SW_STROKE_TAG_POINT;
130
131 border->ptsCnt += 3;
132 border->movable = false;
133 }
134
135
136 static void _borderArcTo(SwStrokeBorder* border, const SwPoint& center, SwFixed radius, SwFixed angleStart, SwFixed angleDiff, SwStroke& stroke)
137 {
138 constexpr SwFixed ARC_CUBIC_ANGLE = SW_ANGLE_PI / 2;
139 SwPoint a = {static_cast<SwCoord>(radius), 0};
140 mathRotate(a, angleStart);
141 SCALE(stroke, a);
142 a += center;
143
144 auto total = angleDiff;
145 auto angle = angleStart;
146 auto rotate = (angleDiff >= 0) ? SW_ANGLE_PI2 : -SW_ANGLE_PI2;
147
148 while (total != 0) {
149 auto step = total;
150 if (step > ARC_CUBIC_ANGLE) step = ARC_CUBIC_ANGLE;
151 else if (step < -ARC_CUBIC_ANGLE) step = -ARC_CUBIC_ANGLE;
152
153 auto next = angle + step;
154 auto theta = step;
155 if (theta < 0) theta = -theta;
156
157 theta >>= 1;
158
159 //compute end point
160 SwPoint b = {static_cast<SwCoord>(radius), 0};
161 mathRotate(b, next);
162 SCALE(stroke, b);
163 b += center;
164
165 //compute first and second control points
166 auto length = mathMulDiv(radius, mathSin(theta) * 4, (0x10000L + mathCos(theta)) * 3);
167
168 SwPoint a2 = {static_cast<SwCoord>(length), 0};
169 mathRotate(a2, angle + rotate);
170 SCALE(stroke, a2);
171 a2 += a;
172
173 SwPoint b2 = {static_cast<SwCoord>(length), 0};
174 mathRotate(b2, next - rotate);
175 SCALE(stroke, b2);
176 b2 += b;
177
178 //add cubic arc
179 _borderCubicTo(border, a2, b2, b);
180
181 //process the rest of the arc?
182 a = b;
183 total -= step;
184 angle = next;
185 }
186 }
187
188
189 static void _borderLineTo(SwStrokeBorder* border, const SwPoint& to, bool movable)
190 {
191 if (border->movable) {
192 //move last point
193 border->pts[border->ptsCnt - 1] = to;
194 } else {
195 //don't add zero-length line_to
196 if (border->ptsCnt > 0 && (border->pts[border->ptsCnt - 1] - to).small()) return;
197
198 _growBorder(border, 1);
199 border->pts[border->ptsCnt] = to;
200 border->tags[border->ptsCnt] = SW_STROKE_TAG_POINT;
201 border->ptsCnt += 1;
202 }
203
204 border->movable = movable;
205 }
206
207
208 static void _borderMoveTo(SwStrokeBorder* border, SwPoint& to)
209 {
210 //close current open path if any?
211 if (border->start >= 0) _borderClose(border, false);
212
213 border->start = border->ptsCnt;
214 border->movable = false;
215
216 _borderLineTo(border, to, false);
217 }
218
219
220 static void _arcTo(SwStroke& stroke, int32_t side)
221 {
222 auto border = stroke.borders + side;
223 auto rotate = SIDE_TO_ROTATE(side);
224 auto total = mathDiff(stroke.angleIn, stroke.angleOut);
225 if (total == SW_ANGLE_PI) total = -rotate * 2;
226
227 _borderArcTo(border, stroke.center, stroke.width, stroke.angleIn + rotate, total, stroke);
228 border->movable = false;
229 }
230
231
232 static void _outside(SwStroke& stroke, int32_t side, SwFixed lineLength)
233 {
234 auto border = stroke.borders + side;
235
236 if (stroke.join == StrokeJoin::Round) {
237 _arcTo(stroke, side);
238 } else {
239 //this is a mitered (pointed) or beveled (truncated) corner
240 auto rotate = SIDE_TO_ROTATE(side);
241 auto bevel = stroke.join == StrokeJoin::Bevel;
242 SwFixed phi = 0;
243 SwFixed thcos = 0;
244
245 if (!bevel) {
246 auto theta = mathDiff(stroke.angleIn, stroke.angleOut);
247 if (theta == SW_ANGLE_PI) {
248 theta = rotate;
249 phi = stroke.angleIn;
250 } else {
251 theta /= 2;
252 phi = stroke.angleIn + theta + rotate;
253 }
254
255 thcos = mathCos(theta);
256 auto sigma = mathMultiply(stroke.miterlimit, thcos);
257
258 //is miter limit exceeded?
259 if (sigma < 0x10000L) bevel = true;
260 }
261
262 //this is a bevel (broken angle)
263 if (bevel) {
264 SwPoint delta = {static_cast<SwCoord>(stroke.width), 0};
265 mathRotate(delta, stroke.angleOut + rotate);
266 SCALE(stroke, delta);
267 delta += stroke.center;
268 border->movable = false;
269 _borderLineTo(border, delta, false);
270 //this is a miter (intersection)
271 } else {
272 auto length = mathDivide(stroke.width, thcos);
273 SwPoint delta = {static_cast<SwCoord>(length), 0};
274 mathRotate(delta, phi);
275 SCALE(stroke, delta);
276 delta += stroke.center;
277 _borderLineTo(border, delta, false);
278
279 /* Now add and end point
280 Only needed if not lineto (lineLength is zero for curves) */
281 if (lineLength == 0) {
282 delta = {static_cast<SwCoord>(stroke.width), 0};
283 mathRotate(delta, stroke.angleOut + rotate);
284 SCALE(stroke, delta);
285 delta += stroke.center;
286 _borderLineTo(border, delta, false);
287 }
288 }
289 }
290 }
291
292
293 static void _inside(SwStroke& stroke, int32_t side, SwFixed lineLength)
294 {
295 auto border = stroke.borders + side;
296 auto theta = mathDiff(stroke.angleIn, stroke.angleOut) / 2;
297 SwPoint delta;
298 bool intersect = false;
299
300 /* Only intersect borders if between two line_to's and both
301 lines are long enough (line length is zero for curves). */
302 if (border->movable && lineLength > 0) {
303 //compute minimum required length of lines
304 SwFixed minLength = abs(mathMultiply(stroke.width, mathTan(theta)));
305 if (stroke.lineLength >= minLength && lineLength >= minLength) intersect = true;
306 }
307
308 auto rotate = SIDE_TO_ROTATE(side);
309
310 if (!intersect) {
311 delta = {static_cast<SwCoord>(stroke.width), 0};
312 mathRotate(delta, stroke.angleOut + rotate);
313 SCALE(stroke, delta);
314 delta += stroke.center;
315 border->movable = false;
316 } else {
317 //compute median angle
318 auto phi = stroke.angleIn + theta;
319 auto thcos = mathCos(theta);
320 delta = {static_cast<SwCoord>(mathDivide(stroke.width, thcos)), 0};
321 mathRotate(delta, phi + rotate);
322 SCALE(stroke, delta);
323 delta += stroke.center;
324 }
325
326 _borderLineTo(border, delta, false);
327 }
328
329
330 void _processCorner(SwStroke& stroke, SwFixed lineLength)
331 {
332 auto turn = mathDiff(stroke.angleIn, stroke.angleOut);
333
334 //no specific corner processing is required if the turn is 0
335 if (turn == 0) return;
336
337 //when we turn to the right, the inside side is 0
338 int32_t inside = 0;
339
340 //otherwise, the inside is 1
341 if (turn < 0) inside = 1;
342
343 //process the inside
344 _inside(stroke, inside, lineLength);
345
346 //process the outside
347 _outside(stroke, 1 - inside, lineLength);
348 }
349
350
351 void _firstSubPath(SwStroke& stroke, SwFixed startAngle, SwFixed lineLength)
352 {
353 SwPoint delta = {static_cast<SwCoord>(stroke.width), 0};
354 mathRotate(delta, startAngle + SW_ANGLE_PI2);
355 SCALE(stroke, delta);
356
357 auto pt = stroke.center + delta;
358 auto border = stroke.borders;
359 _borderMoveTo(border, pt);
360
361 pt = stroke.center - delta;
362 ++border;
363 _borderMoveTo(border, pt);
364
365 /* Save angle, position and line length for last join
366 lineLength is zero for curves */
367 stroke.subPathAngle = startAngle;
368 stroke.firstPt = false;
369 stroke.subPathLineLength = lineLength;
370 }
371
372
373 static void _lineTo(SwStroke& stroke, const SwPoint& to)
374 {
375 auto delta = to - stroke.center;
376
377 //a zero-length lineto is a no-op
378 if (delta.zero()) {
379 //round and square caps are expected to be drawn as a dot even for zero-length lines
380 if (stroke.firstPt && stroke.cap != StrokeCap::Butt) _firstSubPath(stroke, 0, 0);
381 return;
382 }
383
384 /* The lineLength is used to determine the intersection of strokes outlines.
385 The scale needs to be reverted since the stroke width has not been scaled.
386 An alternative option is to scale the width of the stroke properly by
387 calculating the mixture of the sx/sy rating on the stroke direction. */
388 delta.x = static_cast<SwCoord>(delta.x / stroke.sx);
389 delta.y = static_cast<SwCoord>(delta.y / stroke.sy);
390 auto lineLength = mathLength(delta);
391 auto angle = mathAtan(delta);
392
393 delta = {static_cast<SwCoord>(stroke.width), 0};
394 mathRotate(delta, angle + SW_ANGLE_PI2);
395 SCALE(stroke, delta);
396
397 //process corner if necessary
398 if (stroke.firstPt) {
399 /* This is the first segment of a subpath. We need to add a point to each border
400 at their respective starting point locations. */
401 _firstSubPath(stroke, angle, lineLength);
402 } else {
403 //process the current corner
404 stroke.angleOut = angle;
405 _processCorner(stroke, lineLength);
406 }
407
408 //now add a line segment to both the inside and outside paths
409 auto border = stroke.borders;
410 auto side = 1;
411
412 while (side >= 0) {
413 auto pt = to + delta;
414
415 //the ends of lineto borders are movable
416 _borderLineTo(border, pt, true);
417
418 delta.x = -delta.x;
419 delta.y = -delta.y;
420
421 --side;
422 ++border;
423 }
424
425 stroke.angleIn = angle;
426 stroke.center = to;
427 stroke.lineLength = lineLength;
428 }
429
430
431 static void _cubicTo(SwStroke& stroke, const SwPoint& ctrl1, const SwPoint& ctrl2, const SwPoint& to)
432 {
433 SwPoint bezStack[37]; //TODO: static?
434 auto limit = bezStack + 32;
435 auto arc = bezStack;
436 auto firstArc = true;
437 arc[0] = to;
438 arc[1] = ctrl2;
439 arc[2] = ctrl1;
440 arc[3] = stroke.center;
441
442 while (arc >= bezStack) {
443 SwFixed angleIn, angleOut, angleMid;
444
445 //initialize with current direction
446 angleIn = angleOut = angleMid = stroke.angleIn;
447
448 auto valid = mathCubicAngle(arc, angleIn, angleMid, angleOut);
449
450 //valid size
451 if (valid > 0 && arc < limit) {
452 if (stroke.firstPt) stroke.angleIn = angleIn;
453 mathSplitCubic(arc);
454 arc += 3;
455 continue;
456 }
457
458 //ignoreable size
459 if (valid < 0 && arc == bezStack) {
460 stroke.center = to;
461
462 //round and square caps are expected to be drawn as a dot even for zero-length lines
463 if (stroke.firstPt && stroke.cap != StrokeCap::Butt) _firstSubPath(stroke, 0, 0);
464 return;
465 }
466
467 //small size
468 if (firstArc) {
469 firstArc = false;
470 //process corner if necessary
471 if (stroke.firstPt) {
472 _firstSubPath(stroke, angleIn, 0);
473 } else {
474 stroke.angleOut = angleIn;
475 _processCorner(stroke, 0);
476 }
477 } else if (abs(mathDiff(stroke.angleIn, angleIn)) > (SW_ANGLE_PI / 8) / 4) {
478 //if the deviation from one arc to the next is too great add a round corner
479 stroke.center = arc[3];
480 stroke.angleOut = angleIn;
481 stroke.join = StrokeJoin::Round;
482
483 _processCorner(stroke, 0);
484
485 //reinstate line join style
486 stroke.join = stroke.joinSaved;
487 }
488
489 //the arc's angle is small enough; we can add it directly to each border
490 auto theta1 = mathDiff(angleIn, angleMid) / 2;
491 auto theta2 = mathDiff(angleMid, angleOut) / 2;
492 auto phi1 = mathMean(angleIn, angleMid);
493 auto phi2 = mathMean(angleMid, angleOut);
494 auto length1 = mathDivide(stroke.width, mathCos(theta1));
495 auto length2 = mathDivide(stroke.width, mathCos(theta2));
496 SwFixed alpha0 = 0;
497
498 //compute direction of original arc
499 if (stroke.handleWideStrokes) {
500 alpha0 = mathAtan(arc[0] - arc[3]);
501 }
502
503 auto border = stroke.borders;
504 int32_t side = 0;
505
506 while (side < 2) {
507 auto rotate = SIDE_TO_ROTATE(side);
508
509 //compute control points
510 SwPoint _ctrl1 = {static_cast<SwCoord>(length1), 0};
511 mathRotate(_ctrl1, phi1 + rotate);
512 SCALE(stroke, _ctrl1);
513 _ctrl1 += arc[2];
514
515 SwPoint _ctrl2 = {static_cast<SwCoord>(length2), 0};
516 mathRotate(_ctrl2, phi2 + rotate);
517 SCALE(stroke, _ctrl2);
518 _ctrl2 += arc[1];
519
520 //compute end point
521 SwPoint _end = {static_cast<SwCoord>(stroke.width), 0};
522 mathRotate(_end, angleOut + rotate);
523 SCALE(stroke, _end);
524 _end += arc[0];
525
526 if (stroke.handleWideStrokes) {
527 /* determine whether the border radius is greater than the radius of
528 curvature of the original arc */
529 auto _start = border->pts[border->ptsCnt - 1];
530 auto alpha1 = mathAtan(_end - _start);
531
532 //is the direction of the border arc opposite to that of the original arc?
533 if (abs(mathDiff(alpha0, alpha1)) > SW_ANGLE_PI / 2) {
534
535 //use the sine rule to find the intersection point
536 auto beta = mathAtan(arc[3] - _start);
537 auto gamma = mathAtan(arc[0] - _end);
538 auto bvec = _end - _start;
539 auto blen = mathLength(bvec);
540 auto sinA = abs(mathSin(alpha1 - gamma));
541 auto sinB = abs(mathSin(beta - gamma));
542 auto alen = mathMulDiv(blen, sinA, sinB);
543
544 SwPoint delta = {static_cast<SwCoord>(alen), 0};
545 mathRotate(delta, beta);
546 delta += _start;
547
548 //circumnavigate the negative sector backwards
549 border->movable = false;
550 _borderLineTo(border, delta, false);
551 _borderLineTo(border, _end, false);
552 _borderCubicTo(border, _ctrl2, _ctrl1, _start);
553
554 //and then move to the endpoint
555 _borderLineTo(border, _end, false);
556
557 ++side;
558 ++border;
559 continue;
560 }
561 }
562 _borderCubicTo(border, _ctrl1, _ctrl2, _end);
563 ++side;
564 ++border;
565 }
566 arc -= 3;
567 stroke.angleIn = angleOut;
568 }
569 stroke.center = to;
570 }
571
572
573 static void _addCap(SwStroke& stroke, SwFixed angle, int32_t side)
574 {
575 if (stroke.cap == StrokeCap::Square) {
576 auto rotate = SIDE_TO_ROTATE(side);
577 auto border = stroke.borders + side;
578
579 SwPoint delta = {static_cast<SwCoord>(stroke.width), 0};
580 mathRotate(delta, angle);
581 SCALE(stroke, delta);
582
583 SwPoint delta2 = {static_cast<SwCoord>(stroke.width), 0};
584 mathRotate(delta2, angle + rotate);
585 SCALE(stroke, delta2);
586 delta += stroke.center + delta2;
587
588 _borderLineTo(border, delta, false);
589
590 delta = {static_cast<SwCoord>(stroke.width), 0};
591 mathRotate(delta, angle);
592 SCALE(stroke, delta);
593
594 delta2 = {static_cast<SwCoord>(stroke.width), 0};
595 mathRotate(delta2, angle - rotate);
596 SCALE(stroke, delta2);
597 delta += delta2 + stroke.center;
598
599 _borderLineTo(border, delta, false);
600
601 } else if (stroke.cap == StrokeCap::Round) {
602
603 stroke.angleIn = angle;
604 stroke.angleOut = angle + SW_ANGLE_PI;
605 _arcTo(stroke, side);
606 return;
607
608 } else { //Butt
609 auto rotate = SIDE_TO_ROTATE(side);
610 auto border = stroke.borders + side;
611
612 SwPoint delta = {static_cast<SwCoord>(stroke.width), 0};
613 mathRotate(delta, angle + rotate);
614 SCALE(stroke, delta);
615 delta += stroke.center;
616
617 _borderLineTo(border, delta, false);
618
619 delta = {static_cast<SwCoord>(stroke.width), 0};
620 mathRotate(delta, angle - rotate);
621 SCALE(stroke, delta);
622 delta += stroke.center;
623
624 _borderLineTo(border, delta, false);
625 }
626 }
627
628
629 static void _addReverseLeft(SwStroke& stroke, bool opened)
630 {
631 auto right = stroke.borders + 0;
632 auto left = stroke.borders + 1;
633 auto newPts = left->ptsCnt - left->start;
634
635 if (newPts <= 0) return;
636
637 _growBorder(right, newPts);
638
639 auto dstPt = right->pts + right->ptsCnt;
640 auto dstTag = right->tags + right->ptsCnt;
641 auto srcPt = left->pts + left->ptsCnt - 1;
642 auto srcTag = left->tags + left->ptsCnt - 1;
643
644 while (srcPt >= left->pts + left->start) {
645 *dstPt = *srcPt;
646 *dstTag = *srcTag;
647
648 if (opened) {
649 dstTag[0] &= ~(SW_STROKE_TAG_BEGIN | SW_STROKE_TAG_END);
650 } else {
651 //switch begin/end tags if necessary
652 auto ttag = dstTag[0] & (SW_STROKE_TAG_BEGIN | SW_STROKE_TAG_END);
653 if (ttag == SW_STROKE_TAG_BEGIN || ttag == SW_STROKE_TAG_END)
654 dstTag[0] ^= (SW_STROKE_TAG_BEGIN | SW_STROKE_TAG_END);
655 }
656 --srcPt;
657 --srcTag;
658 ++dstPt;
659 ++dstTag;
660 }
661
662 left->ptsCnt = left->start;
663 right->ptsCnt += newPts;
664 right->movable = false;
665 left->movable = false;
666 }
667
668
669 static void _beginSubPath(SwStroke& stroke, const SwPoint& to, bool closed)
670 {
671 /* We cannot process the first point because there is not enough
672 information regarding its corner/cap. Later, it will be processed
673 in the _endSubPath() */
674
675 stroke.firstPt = true;
676 stroke.center = to;
677 stroke.closedSubPath = closed;
678
679 /* Determine if we need to check whether the border radius is greater
680 than the radius of curvature of a curve, to handle this case specially.
681 This is only required if bevel joins or butt caps may be created because
682 round & miter joins and round & square caps cover the negative sector
683 created with wide strokes. */
684 if ((stroke.join != StrokeJoin::Round) || (!stroke.closedSubPath && stroke.cap == StrokeCap::Butt))
685 stroke.handleWideStrokes = true;
686 else
687 stroke.handleWideStrokes = false;
688
689 stroke.ptStartSubPath = to;
690 stroke.angleIn = 0;
691 }
692
693
694 static void _endSubPath(SwStroke& stroke)
695 {
696 if (stroke.closedSubPath) {
697 //close the path if needed
698 if (stroke.center != stroke.ptStartSubPath)
699 _lineTo(stroke, stroke.ptStartSubPath);
700
701 //process the corner
702 stroke.angleOut = stroke.subPathAngle;
703 auto turn = mathDiff(stroke.angleIn, stroke.angleOut);
704
705 //No specific corner processing is required if the turn is 0
706 if (turn != 0) {
707 //when we turn to the right, the inside is 0
708 int32_t inside = 0;
709
710 //otherwise, the inside is 1
711 if (turn < 0) inside = 1;
712
713 _inside(stroke, inside, stroke.subPathLineLength); //inside
714 _outside(stroke, 1 - inside, stroke.subPathLineLength); //outside
715 }
716
717 _borderClose(stroke.borders + 0, false);
718 _borderClose(stroke.borders + 1, true);
719 } else {
720 auto right = stroke.borders;
721
722 /* all right, this is an opened path, we need to add a cap between
723 right & left, add the reverse of left, then add a final cap
724 between left & right */
725 _addCap(stroke, stroke.angleIn, 0);
726
727 //add reversed points from 'left' to 'right'
728 _addReverseLeft(stroke, true);
729
730 //now add the final cap
731 stroke.center = stroke.ptStartSubPath;
732 _addCap(stroke, stroke.subPathAngle + SW_ANGLE_PI, 0);
733
734 /* now end the right subpath accordingly. The left one is rewind
735 and doesn't need further processing */
736 _borderClose(right, false);
737 }
738 }
739
740
741 static void _getCounts(SwStrokeBorder* border, uint32_t& ptsCnt, uint32_t& cntrsCnt)
742 {
743 auto count = border->ptsCnt;
744 auto tags = border->tags;
745 uint32_t _ptsCnt = 0;
746 uint32_t _cntrsCnt = 0;
747 bool inCntr = false;
748
749 while (count > 0) {
750 if (tags[0] & SW_STROKE_TAG_BEGIN) {
751 if (inCntr) goto fail;
752 inCntr = true;
753 } else if (!inCntr) goto fail;
754
755 if (tags[0] & SW_STROKE_TAG_END) {
756 inCntr = false;
757 ++_cntrsCnt;
758 }
759 --count;
760 ++_ptsCnt;
761 ++tags;
762 }
763
764 if (inCntr) goto fail;
765
766 ptsCnt = _ptsCnt;
767 cntrsCnt = _cntrsCnt;
768
769 return;
770
771 fail:
772 ptsCnt = 0;
773 cntrsCnt = 0;
774 }
775
776
777 static void _exportBorderOutline(const SwStroke& stroke, SwOutline* outline, uint32_t side)
778 {
779 auto border = stroke.borders + side;
780 if (border->ptsCnt == 0) return;
781
782 memcpy(outline->pts.data + outline->pts.count, border->pts, border->ptsCnt * sizeof(SwPoint));
783
784 auto cnt = border->ptsCnt;
785 auto src = border->tags;
786 auto tags = outline->types.data + outline->types.count;
787 auto idx = outline->pts.count;
788
789 while (cnt > 0) {
790 if (*src & SW_STROKE_TAG_POINT) *tags = SW_CURVE_TYPE_POINT;
791 else if (*src & SW_STROKE_TAG_CUBIC) *tags = SW_CURVE_TYPE_CUBIC;
792 else TVGERR("SW_ENGINE", "Invalid stroke tag was given! = %d", *src);
793 if (*src & SW_STROKE_TAG_END) outline->cntrs.push(idx);
794 ++src;
795 ++tags;
796 ++idx;
797 --cnt;
798 }
799 outline->pts.count += border->ptsCnt;
800 outline->types.count += border->ptsCnt;
801 }
802
803
804 /************************************************************************/
805 /* External Class Implementation */
806 /************************************************************************/
807
808 void strokeFree(SwStroke* stroke)
809 {
810 if (!stroke) return;
811
812 //free borders
813 if (stroke->borders[0].pts) free(stroke->borders[0].pts);
814 if (stroke->borders[0].tags) free(stroke->borders[0].tags);
815 if (stroke->borders[1].pts) free(stroke->borders[1].pts);
816 if (stroke->borders[1].tags) free(stroke->borders[1].tags);
817
818 fillFree(stroke->fill);
819 stroke->fill = nullptr;
820
821 free(stroke);
822 }
823
824
825 void strokeReset(SwStroke* stroke, const RenderShape* rshape, const Matrix& transform)
826 {
827 stroke->sx = sqrtf(powf(transform.e11, 2.0f) + powf(transform.e21, 2.0f));
828 stroke->sy = sqrtf(powf(transform.e12, 2.0f) + powf(transform.e22, 2.0f));
829 stroke->width = HALF_STROKE(rshape->strokeWidth());
830 stroke->cap = rshape->strokeCap();
831 stroke->miterlimit = static_cast<SwFixed>(rshape->strokeMiterlimit() * 65536.0f);
832
833 //Save line join: it can be temporarily changed when stroking curves...
834 stroke->joinSaved = stroke->join = rshape->strokeJoin();
835
836 stroke->borders[0].ptsCnt = 0;
837 stroke->borders[0].start = -1;
838 stroke->borders[1].ptsCnt = 0;
839 stroke->borders[1].start = -1;
840 }
841
842
843 bool strokeParseOutline(SwStroke* stroke, const SwOutline& outline)
844 {
845 uint32_t first = 0;
846 uint32_t i = 0;
847
848 for (auto cntr = outline.cntrs.begin(); cntr < outline.cntrs.end(); ++cntr, ++i) {
849 auto last = *cntr; //index of last point in contour
850 auto limit = outline.pts.data + last;
851
852 //Skip empty points
853 if (last <= first) {
854 first = last + 1;
855 continue;
856 }
857
858 auto start = outline.pts[first];
859 auto pt = outline.pts.data + first;
860 auto types = outline.types.data + first;
861 auto type = types[0];
862
863 //A contour cannot start with a cubic control point
864 if (type == SW_CURVE_TYPE_CUBIC) return false;
865 ++types;
866
867 auto closed = outline.closed.data ? outline.closed.data[i]: false;
868
869 _beginSubPath(*stroke, start, closed);
870
871 while (pt < limit) {
872 //emit a single line_to
873 if (types[0] == SW_CURVE_TYPE_POINT) {
874 ++pt;
875 ++types;
876 _lineTo(*stroke, *pt);
877 //types cubic
878 } else {
879 pt += 3;
880 types += 3;
881 if (pt <= limit) _cubicTo(*stroke, pt[-2], pt[-1], pt[0]);
882 else if (pt - 1 == limit) _cubicTo(*stroke, pt[-2], pt[-1], start);
883 else goto close;
884 }
885 }
886 close:
887 if (!stroke->firstPt) _endSubPath(*stroke);
888 first = last + 1;
889 }
890 return true;
891 }
892
893
894 SwOutline* strokeExportOutline(SwStroke* stroke, SwMpool* mpool, unsigned tid)
895 {
896 uint32_t count1, count2, count3, count4;
897
898 _getCounts(stroke->borders + 0, count1, count2);
899 _getCounts(stroke->borders + 1, count3, count4);
900
901 auto ptsCnt = count1 + count3;
902 auto cntrsCnt = count2 + count4;
903
904 auto outline = mpoolReqStrokeOutline(mpool, tid);
905 outline->pts.reserve(ptsCnt);
906 outline->types.reserve(ptsCnt);
907 outline->cntrs.reserve(cntrsCnt);
908
909 _exportBorderOutline(*stroke, outline, 0); //left
910 _exportBorderOutline(*stroke, outline, 1); //right
911
912 return outline;
913 }