LibreOffice Module basegfx (master) 1
b2dsvgpolypolygon.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
25
26#include <rtl/ustring.hxx>
27#include <sal/log.hxx>
28#include <rtl/math.hxx>
29#include <rtl/character.hxx>
31
32namespace
33{
34
35void putCommandChar(OUStringBuffer& rBuffer,sal_Unicode& rLastSVGCommand, sal_Unicode aChar, bool bToLower,bool bVerbose)
36{
37 const sal_Unicode aCommand = bToLower ? rtl::toAsciiLowerCase(aChar) : aChar;
38
39 if (bVerbose && rBuffer.getLength())
40 rBuffer.append(' ');
41
42 if (bVerbose || rLastSVGCommand != aCommand)
43 {
44 rBuffer.append(aCommand);
45 rLastSVGCommand = aCommand;
46 }
47}
48
49void putNumberChar(OUStringBuffer& rStr,double fValue, double fOldValue, bool bUseRelativeCoordinates,bool bVerbose)
50{
51 if (bUseRelativeCoordinates)
52 fValue -= fOldValue;
53
54 const sal_Int32 aLen(rStr.getLength());
55 if (bVerbose || (aLen && basegfx::internal::isOnNumberChar(rStr[aLen - 1], false) && fValue >= 0.0))
56 rStr.append(' ');
57
58 rStr.append(fValue);
59}
60
61}
62
63namespace basegfx::utils
64{
65 bool PointIndex::operator<(const PointIndex& rComp) const
66 {
67 if(rComp.getPolygonIndex() == getPolygonIndex())
68 {
69 return rComp.getPointIndex() < getPointIndex();
70 }
71
72 return rComp.getPolygonIndex() < getPolygonIndex();
73 }
74
76 B2DPolyPolygon& o_rPolyPolygon,
77 std::u16string_view rSvgDStatement,
78 bool bHandleRelativeNextPointCompatible,
79 PointIndexSet* pHelpPointIndexSet)
80 {
81 o_rPolyPolygon.clear();
82 const sal_Int32 nLen(rSvgDStatement.size());
83 sal_Int32 nPos(0);
84 double nLastX( 0.0 );
85 double nLastY( 0.0 );
86 B2DPolygon aCurrPoly;
87
88 // skip initial whitespace
89 basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
90
91 while(nPos < nLen)
92 {
93 bool bRelative(false);
94 const sal_Unicode aCurrChar(rSvgDStatement[nPos]);
95
96 if(o_rPolyPolygon.count() && !aCurrPoly.count() && aCurrChar != 'm' && aCurrChar != 'M')
97 {
98 // we have a new sub-polygon starting, but without a 'moveto' command.
99 // this requires to add the current point as start point to the polygon
100 // (see SVG1.1 8.3.3 The "closepath" command)
101 aCurrPoly.append(B2DPoint(nLastX, nLastY));
102 }
103
104 switch(aCurrChar)
105 {
106 case 'z' :
107 case 'Z' :
108 {
109 // consume CurrChar and whitespace
110 nPos++;
111 basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
112
113 // create closed polygon and reset import values
114 if(aCurrPoly.count())
115 {
116 if(!bHandleRelativeNextPointCompatible)
117 {
118 // SVG defines that "the next subpath starts at the
119 // same initial point as the current subpath", so set the
120 // current point if we do not need to be compatible
121 nLastX = aCurrPoly.getB2DPoint(0).getX();
122 nLastY = aCurrPoly.getB2DPoint(0).getY();
123 }
124
125 aCurrPoly.setClosed(true);
126 o_rPolyPolygon.append(aCurrPoly);
127 aCurrPoly.clear();
128 }
129
130 break;
131 }
132
133 case 'm' :
134 case 'M' :
135 {
136 // create non-closed polygon and reset import values
137 if(aCurrPoly.count())
138 {
139 o_rPolyPolygon.append(aCurrPoly);
140 aCurrPoly.clear();
141 }
142 [[fallthrough]]; // to add coordinate data as 1st point of new polygon
143 }
144 case 'l' :
145 case 'L' :
146 {
147 if(aCurrChar == 'm' || aCurrChar == 'l')
148 {
149 bRelative = true;
150 }
151
152 // consume CurrChar and whitespace
153 nPos++;
154 basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
155
156 while(nPos < nLen && basegfx::internal::isOnNumberChar(rSvgDStatement, nPos))
157 {
158 double nX, nY;
159
160 if(!basegfx::internal::importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
161 if(!basegfx::internal::importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
162
163 if(bRelative)
164 {
165 nX += nLastX;
166 nY += nLastY;
167 }
168
169 // set last position
170 nLastX = nX;
171 nLastY = nY;
172
173 // add point
174 aCurrPoly.append(B2DPoint(nX, nY));
175 }
176 break;
177 }
178
179 case 'h' :
180 {
181 bRelative = true;
182 [[fallthrough]];
183 }
184 case 'H' :
185 {
186 nPos++;
187 basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
188
189 while(nPos < nLen && basegfx::internal::isOnNumberChar(rSvgDStatement, nPos))
190 {
191 double nX, nY(nLastY);
192
193 if(!basegfx::internal::importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
194
195 if(bRelative)
196 {
197 nX += nLastX;
198 }
199
200 // set last position
201 nLastX = nX;
202
203 // add point
204 aCurrPoly.append(B2DPoint(nX, nY));
205 }
206 break;
207 }
208
209 case 'v' :
210 {
211 bRelative = true;
212 [[fallthrough]];
213 }
214 case 'V' :
215 {
216 nPos++;
217 basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
218
219 while(nPos < nLen && basegfx::internal::isOnNumberChar(rSvgDStatement, nPos))
220 {
221 double nX(nLastX), nY;
222
223 if(!basegfx::internal::importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
224
225 if(bRelative)
226 {
227 nY += nLastY;
228 }
229
230 // set last position
231 nLastY = nY;
232
233 // add point
234 aCurrPoly.append(B2DPoint(nX, nY));
235 }
236 break;
237 }
238
239 case 's' :
240 {
241 bRelative = true;
242 [[fallthrough]];
243 }
244 case 'S' :
245 {
246 nPos++;
247 basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
248
249 while(nPos < nLen && basegfx::internal::isOnNumberChar(rSvgDStatement, nPos))
250 {
251 double nX, nY;
252 double nX2, nY2;
253
254 if(!basegfx::internal::importDoubleAndSpaces(nX2, nPos, rSvgDStatement, nLen)) return false;
255 if(!basegfx::internal::importDoubleAndSpaces(nY2, nPos, rSvgDStatement, nLen)) return false;
256 if(!basegfx::internal::importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
257 if(!basegfx::internal::importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
258
259 if(bRelative)
260 {
261 nX2 += nLastX;
262 nY2 += nLastY;
263 nX += nLastX;
264 nY += nLastY;
265 }
266
267 // ensure existence of start point
268 if(!aCurrPoly.count())
269 {
270 aCurrPoly.append(B2DPoint(nLastX, nLastY));
271 }
272
273 // get first control point. It's the reflection of the PrevControlPoint
274 // of the last point. If not existent, use current point (see SVG)
275 B2DPoint aPrevControl(nLastX, nLastY);
276 const sal_uInt32 nIndex(aCurrPoly.count() - 1);
277
278 if(aCurrPoly.areControlPointsUsed() && aCurrPoly.isPrevControlPointUsed(nIndex))
279 {
280 const B2DPoint aPrevPoint(aCurrPoly.getB2DPoint(nIndex));
281 const B2DPoint aPrevControlPoint(aCurrPoly.getPrevControlPoint(nIndex));
282
283 // use mirrored previous control point
284 aPrevControl.setX((2.0 * aPrevPoint.getX()) - aPrevControlPoint.getX());
285 aPrevControl.setY((2.0 * aPrevPoint.getY()) - aPrevControlPoint.getY());
286 }
287
288 // append curved edge
289 aCurrPoly.appendBezierSegment(aPrevControl, B2DPoint(nX2, nY2), B2DPoint(nX, nY));
290
291 // set last position
292 nLastX = nX;
293 nLastY = nY;
294 }
295 break;
296 }
297
298 case 'c' :
299 {
300 bRelative = true;
301 [[fallthrough]];
302 }
303 case 'C' :
304 {
305 nPos++;
306 basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
307
308 while(nPos < nLen && basegfx::internal::isOnNumberChar(rSvgDStatement, nPos))
309 {
310 double nX, nY;
311 double nX1, nY1;
312 double nX2, nY2;
313
314 if(!basegfx::internal::importDoubleAndSpaces(nX1, nPos, rSvgDStatement, nLen)) return false;
315 if(!basegfx::internal::importDoubleAndSpaces(nY1, nPos, rSvgDStatement, nLen)) return false;
316 if(!basegfx::internal::importDoubleAndSpaces(nX2, nPos, rSvgDStatement, nLen)) return false;
317 if(!basegfx::internal::importDoubleAndSpaces(nY2, nPos, rSvgDStatement, nLen)) return false;
318 if(!basegfx::internal::importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
319 if(!basegfx::internal::importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
320
321 if(bRelative)
322 {
323 nX1 += nLastX;
324 nY1 += nLastY;
325 nX2 += nLastX;
326 nY2 += nLastY;
327 nX += nLastX;
328 nY += nLastY;
329 }
330
331 // ensure existence of start point
332 if(!aCurrPoly.count())
333 {
334 aCurrPoly.append(B2DPoint(nLastX, nLastY));
335 }
336
337 // append curved edge
338 aCurrPoly.appendBezierSegment(B2DPoint(nX1, nY1), B2DPoint(nX2, nY2), B2DPoint(nX, nY));
339
340 // set last position
341 nLastX = nX;
342 nLastY = nY;
343 }
344 break;
345 }
346
347 // #100617# quadratic beziers are imported as cubic ones
348 case 'q' :
349 {
350 bRelative = true;
351 [[fallthrough]];
352 }
353 case 'Q' :
354 {
355 nPos++;
356 basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
357
358 while(nPos < nLen && basegfx::internal::isOnNumberChar(rSvgDStatement, nPos))
359 {
360 double nX, nY;
361 double nX1, nY1;
362
363 if(!basegfx::internal::importDoubleAndSpaces(nX1, nPos, rSvgDStatement, nLen)) return false;
364 if(!basegfx::internal::importDoubleAndSpaces(nY1, nPos, rSvgDStatement, nLen)) return false;
365 if(!basegfx::internal::importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
366 if(!basegfx::internal::importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
367
368 if(bRelative)
369 {
370 nX1 += nLastX;
371 nY1 += nLastY;
372 nX += nLastX;
373 nY += nLastY;
374 }
375
376 // ensure existence of start point
377 if(!aCurrPoly.count())
378 {
379 aCurrPoly.append(B2DPoint(nLastX, nLastY));
380 }
381
382 // append curved edge
383 aCurrPoly.appendQuadraticBezierSegment(B2DPoint(nX1, nY1), B2DPoint(nX, nY));
384
385 // set last position
386 nLastX = nX;
387 nLastY = nY;
388 }
389 break;
390 }
391
392 // #100617# relative quadratic beziers are imported as cubic
393 case 't' :
394 {
395 bRelative = true;
396 [[fallthrough]];
397 }
398 case 'T' :
399 {
400 nPos++;
401 basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
402
403 while(nPos < nLen && basegfx::internal::isOnNumberChar(rSvgDStatement, nPos))
404 {
405 double nX, nY;
406
407 if(!basegfx::internal::importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
408 if(!basegfx::internal::importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
409
410 if(bRelative)
411 {
412 nX += nLastX;
413 nY += nLastY;
414 }
415
416 // ensure existence of start point
417 if(!aCurrPoly.count())
418 {
419 aCurrPoly.append(B2DPoint(nLastX, nLastY));
420 }
421
422 // get first control point. It's the reflection of the PrevControlPoint
423 // of the last point. If not existent, use current point (see SVG)
424 B2DPoint aPrevControl(nLastX, nLastY);
425 const sal_uInt32 nIndex(aCurrPoly.count() - 1);
426 const B2DPoint aPrevPoint(aCurrPoly.getB2DPoint(nIndex));
427
428 if(aCurrPoly.areControlPointsUsed() && aCurrPoly.isPrevControlPointUsed(nIndex))
429 {
430 const B2DPoint aPrevControlPoint(aCurrPoly.getPrevControlPoint(nIndex));
431
432 // use mirrored previous control point
433 aPrevControl.setX((2.0 * aPrevPoint.getX()) - aPrevControlPoint.getX());
434 aPrevControl.setY((2.0 * aPrevPoint.getY()) - aPrevControlPoint.getY());
435 }
436
437 if(!aPrevControl.equal(aPrevPoint))
438 {
439 // there is a prev control point, and we have the already mirrored one
440 // in aPrevControl. We also need the quadratic control point for this
441 // new quadratic segment to calculate the 2nd cubic control point
442 const B2DPoint aQuadControlPoint(
443 ((3.0 * aPrevControl.getX()) - aPrevPoint.getX()) / 2.0,
444 ((3.0 * aPrevControl.getY()) - aPrevPoint.getY()) / 2.0);
445
446 // calculate the cubic bezier coefficients from the quadratic ones.
447 const double nX2Prime((aQuadControlPoint.getX() * 2.0 + nX) / 3.0);
448 const double nY2Prime((aQuadControlPoint.getY() * 2.0 + nY) / 3.0);
449
450 // append curved edge, use mirrored cubic control point directly
451 aCurrPoly.appendBezierSegment(aPrevControl, B2DPoint(nX2Prime, nY2Prime), B2DPoint(nX, nY));
452 }
453 else
454 {
455 // when no previous control, SVG says to use current point -> straight line.
456 // Just add end point
457 aCurrPoly.append(B2DPoint(nX, nY));
458 }
459
460 // set last position
461 nLastX = nX;
462 nLastY = nY;
463 }
464 break;
465 }
466
467 case 'a' :
468 {
469 bRelative = true;
470 [[fallthrough]];
471 }
472 case 'A' :
473 {
474 nPos++;
475 basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
476
477 while(nPos < nLen && basegfx::internal::isOnNumberChar(rSvgDStatement, nPos))
478 {
479 double nX, nY;
480 double fRX, fRY, fPhi;
481 sal_Int32 bLargeArcFlag, bSweepFlag;
482
483 if(!basegfx::internal::importDoubleAndSpaces(fRX, nPos, rSvgDStatement, nLen)) return false;
484 if(!basegfx::internal::importDoubleAndSpaces(fRY, nPos, rSvgDStatement, nLen)) return false;
485 if(!basegfx::internal::importDoubleAndSpaces(fPhi, nPos, rSvgDStatement, nLen)) return false;
486 if(!basegfx::internal::importFlagAndSpaces(bLargeArcFlag, nPos, rSvgDStatement, nLen)) return false;
487 if(!basegfx::internal::importFlagAndSpaces(bSweepFlag, nPos, rSvgDStatement, nLen)) return false;
488 if(!basegfx::internal::importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
489 if(!basegfx::internal::importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
490
491 if(bRelative)
492 {
493 nX += nLastX;
494 nY += nLastY;
495 }
496
497 if( rtl::math::approxEqual(nX, nLastX) && rtl::math::approxEqual(nY, nLastY) )
498 continue; // start==end -> skip according to SVG spec
499
500 if( fRX == 0.0 || fRY == 0.0 )
501 {
502 // straight line segment according to SVG spec
503 aCurrPoly.append(B2DPoint(nX, nY));
504 }
505 else
506 {
507 // normalize according to SVG spec
508 fRX=fabs(fRX); fRY=fabs(fRY);
509
510 // from the SVG spec, appendix F.6.4
511
512 // |x1'| |cos phi sin phi| |(x1 - x2)/2|
513 // |y1'| = |-sin phi cos phi| |(y1 - y2)/2|
514 const B2DPoint p1(nLastX, nLastY);
515 const B2DPoint p2(nX, nY);
517 -deg2rad(fPhi)));
518
519 const B2DPoint p1_prime( aTransform * B2DPoint(((p1-p2)/2.0)) );
520
521 // ______________________________________ rx y1'
522 // |cx'| + / rx^2 ry^2 - rx^2 y1'^2 - ry^2 x1^2 ry
523 // |cy'| =-/ rx^2y1'^2 + ry^2 x1'^2 - ry x1'
524 // rx
525 // chose + if f_A != f_S
526 // chose - if f_A = f_S
527 B2DPoint aCenter_prime;
528 const double fRadicant(
529 (fRX*fRX*fRY*fRY - fRX*fRX*p1_prime.getY()*p1_prime.getY() - fRY*fRY*p1_prime.getX()*p1_prime.getX())/
530 (fRX*fRX*p1_prime.getY()*p1_prime.getY() + fRY*fRY*p1_prime.getX()*p1_prime.getX()));
531 if( fRadicant < 0.0 )
532 {
533 // no solution - according to SVG
534 // spec, scale up ellipse
535 // uniformly such that it passes
536 // through end points (denominator
537 // of radicant solved for fRY,
538 // with s=fRX/fRY)
539 const double fRatio(fRX/fRY);
540 const double fRadicant2(
541 p1_prime.getY()*p1_prime.getY() +
542 p1_prime.getX()*p1_prime.getX()/(fRatio*fRatio));
543 if( fRadicant2 < 0.0 )
544 {
545 // only trivial solution, one
546 // of the axes 0 -> straight
547 // line segment according to
548 // SVG spec
549 aCurrPoly.append(B2DPoint(nX, nY));
550 continue;
551 }
552
553 fRY=sqrt(fRadicant2);
554 fRX=fRatio*fRY;
555
556 // keep center_prime forced to (0,0)
557 }
558 else
559 {
560 const double fFactor(
561 (bLargeArcFlag==bSweepFlag ? -1.0 : 1.0) *
562 sqrt(fRadicant));
563
564 // actually calculate center_prime
565 aCenter_prime = B2DPoint(
566 fFactor*fRX*p1_prime.getY()/fRY,
567 -fFactor*fRY*p1_prime.getX()/fRX);
568 }
569
570 // + u - v
571 // angle(u,v) = arccos( ------------ ) (take the sign of (ux vy - uy vx))
572 // - ||u|| ||v||
573
574 // 1 | (x1' - cx')/rx |
575 // theta1 = angle(( ), | | )
576 // 0 | (y1' - cy')/ry |
577 const B2DPoint aRadii(fRX,fRY);
578 double fTheta1(
579 B2DVector(1.0,0.0).angle(
580 (p1_prime-aCenter_prime)/aRadii));
581
582 // |1| | (-x1' - cx')/rx |
583 // theta2 = angle( | | , | | )
584 // |0| | (-y1' - cy')/ry |
585 double fTheta2(
586 B2DVector(1.0,0.0).angle(
587 (-p1_prime-aCenter_prime)/aRadii));
588
589 // map both angles to [0,2pi)
590 fTheta1 = fmod(2*M_PI+fTheta1,2*M_PI);
591 fTheta2 = fmod(2*M_PI+fTheta2,2*M_PI);
592
593 // make sure the large arc is taken
594 // (since
595 // createPolygonFromEllipseSegment()
596 // normalizes to e.g. cw arc)
597 if( !bSweepFlag )
598 std::swap(fTheta1,fTheta2);
599
600 // finally, create bezier polygon from this
601 B2DPolygon aSegment(
603 fTheta1, fTheta2 ));
604
605 // transform ellipse by rotation & move to final center
606 aTransform = basegfx::utils::createScaleB2DHomMatrix(fRX, fRY);
607 aTransform.translate(aCenter_prime.getX(),
608 aCenter_prime.getY());
609 aTransform.rotate(deg2rad(fPhi));
610 const B2DPoint aOffset((p1+p2)/2.0);
611 aTransform.translate(aOffset.getX(),
612 aOffset.getY());
613 aSegment.transform(aTransform);
614
615 // createPolygonFromEllipseSegment()
616 // always creates arcs that are
617 // positively oriented - flip polygon
618 // if we swapped angles above
619 if( !bSweepFlag )
620 aSegment.flip();
621
622 // remember PointIndex of evtl. added pure helper points
623 sal_uInt32 nPointIndex(aCurrPoly.count() + 1);
624 aCurrPoly.append(aSegment);
625
626 // if asked for, mark pure helper points by adding them to the index list of
627 // helper points
628 if(pHelpPointIndexSet && aCurrPoly.count() > 1)
629 {
630 const sal_uInt32 nPolyIndex(o_rPolyPolygon.count());
631
632 for(;nPointIndex + 1 < aCurrPoly.count(); nPointIndex++)
633 {
634 pHelpPointIndexSet->insert(PointIndex(nPolyIndex, nPointIndex));
635 }
636 }
637 }
638
639 // set last position
640 nLastX = nX;
641 nLastY = nY;
642 }
643 break;
644 }
645
646 default:
647 {
648 SAL_WARN("basegfx", "importFromSvgD(): skipping tags in svg:d element (unknown: \""
649 << OUString(aCurrChar)
650 << "\")!");
651 ++nPos;
652 break;
653 }
654 }
655 }
656
657 // if there is polygon data, create non-closed polygon
658 if(aCurrPoly.count())
659 {
660 o_rPolyPolygon.append(aCurrPoly);
661 }
662
663 return true;
664 }
665
667 std::u16string_view rSvgPointsAttribute )
668 {
669 o_rPoly.clear();
670 const sal_Int32 nLen(rSvgPointsAttribute.size());
671 sal_Int32 nPos(0);
672 double nX, nY;
673
674 // skip initial whitespace
675 basegfx::internal::skipSpaces(nPos, rSvgPointsAttribute, nLen);
676
677 while(nPos < nLen)
678 {
679 if(!basegfx::internal::importDoubleAndSpaces(nX, nPos, rSvgPointsAttribute, nLen)) return false;
680 if(!basegfx::internal::importDoubleAndSpaces(nY, nPos, rSvgPointsAttribute, nLen)) return false;
681
682 // add point
683 o_rPoly.append(B2DPoint(nX, nY));
684
685 // skip to next number, or finish
686 basegfx::internal::skipSpaces(nPos, rSvgPointsAttribute, nLen);
687 }
688
689 return true;
690 }
691
692 OUString exportToSvgPoints( const B2DPolygon& rPoly )
693 {
694 SAL_WARN_IF(rPoly.areControlPointsUsed(), "basegfx", "exportToSvgPoints: Only non-bezier polygons allowed (!)");
695 const sal_uInt32 nPointCount(rPoly.count());
696 OUStringBuffer aResult;
697
698 for(sal_uInt32 a(0); a < nPointCount; a++)
699 {
700 const basegfx::B2DPoint aPoint(rPoly.getB2DPoint(a));
701
702 if(a)
703 {
704 aResult.append(' ');
705 }
706
707 aResult.append(OUString::number(aPoint.getX())
708 + ","
709 + OUString::number(aPoint.getY()));
710 }
711
712 return aResult.makeStringAndClear();
713 }
714
715 OUString exportToSvgD(
716 const B2DPolyPolygon& rPolyPolygon,
717 bool bUseRelativeCoordinates,
718 bool bDetectQuadraticBeziers,
719 bool bHandleRelativeNextPointCompatible,
720 bool bOOXMLMotionPath)
721 {
722 const sal_uInt32 nCount(rPolyPolygon.count());
723 sal_uInt32 nCombinedPointCount = 0;
724 for(sal_uInt32 i(0); i < nCount; i++)
725 {
726 const B2DPolygon& aPolygon(rPolyPolygon.getB2DPolygon(i));
727 nCombinedPointCount += aPolygon.count();
728 }
729
730 OUStringBuffer aResult(std::max<int>(nCombinedPointCount * 32,512));
731 B2DPoint aCurrentSVGPosition(0.0, 0.0); // SVG assumes (0,0) as the initial current point
732
733 for(sal_uInt32 i(0); i < nCount; i++)
734 {
735 const B2DPolygon& aPolygon(rPolyPolygon.getB2DPolygon(i));
736 const sal_uInt32 nPointCount(aPolygon.count());
737
738 if(nPointCount)
739 {
740 const bool bPolyUsesControlPoints(aPolygon.areControlPointsUsed());
741 const sal_uInt32 nEdgeCount(aPolygon.isClosed() ? nPointCount : nPointCount - 1);
742 sal_Unicode aLastSVGCommand(' '); // last SVG command char
743 B2DPoint aLeft, aRight; // for quadratic bezier test
744
745 // handle polygon start point
746 B2DPoint aEdgeStart(aPolygon.getB2DPoint(0));
747 bool bUseRelativeCoordinatesForFirstPoint(bUseRelativeCoordinates);
748
749 if(bHandleRelativeNextPointCompatible)
750 {
751 // To get around the error that the start point for the next polygon is the
752 // start point of the current one (and not the last as it was handled up to now)
753 // do force to write an absolute 'M' command as start for the next polygon
754 bUseRelativeCoordinatesForFirstPoint = false;
755 }
756
757 // Write 'moveto' and the 1st coordinates, set aLastSVGCommand to 'lineto'
758 putCommandChar(aResult, aLastSVGCommand, 'M', bUseRelativeCoordinatesForFirstPoint, bOOXMLMotionPath);
759 putNumberChar(aResult, aEdgeStart.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinatesForFirstPoint, bOOXMLMotionPath);
760 putNumberChar(aResult, aEdgeStart.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinatesForFirstPoint, bOOXMLMotionPath);
761 aLastSVGCommand = bUseRelativeCoordinatesForFirstPoint ? 'l' : 'L';
762 aCurrentSVGPosition = aEdgeStart;
763
764 for(sal_uInt32 nIndex(0); nIndex < nEdgeCount; nIndex++)
765 {
766 // prepare access to next point
767 const sal_uInt32 nNextIndex((nIndex + 1) % nPointCount);
768 const B2DPoint aEdgeEnd(aPolygon.getB2DPoint(nNextIndex));
769
770 // handle edge from (aEdgeStart, aEdgeEnd) using indices (nIndex, nNextIndex)
771 const bool bEdgeIsBezier(bPolyUsesControlPoints
772 && (aPolygon.isNextControlPointUsed(nIndex) || aPolygon.isPrevControlPointUsed(nNextIndex)));
773
774 if(bEdgeIsBezier)
775 {
776 // handle bezier edge
777 const B2DPoint aControlEdgeStart(aPolygon.getNextControlPoint(nIndex));
778 const B2DPoint aControlEdgeEnd(aPolygon.getPrevControlPoint(nNextIndex));
779 bool bIsQuadraticBezier(false);
780
781 // check continuity at current edge's start point. For SVG, do NOT use an
782 // existing continuity since no 'S' or 's' statement should be written. At
783 // import, that 'previous' control vector is not available. SVG documentation
784 // says for interpretation:
785
786 // "(If there is no previous command or if the previous command was
787 // not a C, c, S or s, assume the first control point is coincident
788 // with the current point.)"
789
790 // That's what is done from our import, so avoid exporting it as first statement
791 // is necessary.
792 const bool bSymmetricAtEdgeStart(
793 !bOOXMLMotionPath && nIndex != 0
795
796 if(bDetectQuadraticBeziers)
797 {
798 // check for quadratic beziers - that's
799 // the case if both control points are in
800 // the same place when they are prolonged
801 // to the common quadratic control point
802
803 // Left: P = (3P1 - P0) / 2
804 // Right: P = (3P2 - P3) / 2
805 aLeft = B2DPoint((3.0 * aControlEdgeStart - aEdgeStart) / 2.0);
806 aRight= B2DPoint((3.0 * aControlEdgeEnd - aEdgeEnd) / 2.0);
807 bIsQuadraticBezier = aLeft.equal(aRight);
808 }
809
810 if(bIsQuadraticBezier)
811 {
812 // approximately equal, export as quadratic bezier
813 if(bSymmetricAtEdgeStart)
814 {
815 putCommandChar(aResult, aLastSVGCommand, 'T', bUseRelativeCoordinates, bOOXMLMotionPath);
816
817 putNumberChar(aResult, aEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
818 putNumberChar(aResult, aEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
819 aCurrentSVGPosition = aEdgeEnd;
820 }
821 else
822 {
823 putCommandChar(aResult, aLastSVGCommand, 'Q', bUseRelativeCoordinates, bOOXMLMotionPath);
824
825 putNumberChar(aResult, aLeft.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
826 putNumberChar(aResult, aLeft.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
827 putNumberChar(aResult, aEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
828 putNumberChar(aResult, aEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
829 aCurrentSVGPosition = aEdgeEnd;
830 }
831 }
832 else
833 {
834 // export as cubic bezier
835 if(bSymmetricAtEdgeStart)
836 {
837 putCommandChar(aResult, aLastSVGCommand, 'S', bUseRelativeCoordinates, bOOXMLMotionPath);
838
839 putNumberChar(aResult, aControlEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
840 putNumberChar(aResult, aControlEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
841 putNumberChar(aResult, aEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
842 putNumberChar(aResult, aEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
843 aCurrentSVGPosition = aEdgeEnd;
844 }
845 else
846 {
847 putCommandChar(aResult, aLastSVGCommand, 'C', bUseRelativeCoordinates, bOOXMLMotionPath);
848
849 putNumberChar(aResult, aControlEdgeStart.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
850 putNumberChar(aResult, aControlEdgeStart.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
851 putNumberChar(aResult, aControlEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
852 putNumberChar(aResult, aControlEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
853 putNumberChar(aResult, aEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
854 putNumberChar(aResult, aEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
855 aCurrentSVGPosition = aEdgeEnd;
856 }
857 }
858 }
859 else
860 {
861 // straight edge
862 if(nNextIndex == 0)
863 {
864 // it's a closed polygon's last edge and it's not a bezier edge, so there is
865 // no need to write it
866 }
867 else
868 {
869 const bool bXEqual(rtl::math::approxEqual(aEdgeStart.getX(), aEdgeEnd.getX()));
870 const bool bYEqual(rtl::math::approxEqual(aEdgeStart.getY(), aEdgeEnd.getY()));
871
872 if(bXEqual && bYEqual)
873 {
874 // point is a double point; do not export at all
875 }
876 else if(bXEqual && !bOOXMLMotionPath)
877 {
878 // export as vertical line
879 putCommandChar(aResult, aLastSVGCommand, 'V', bUseRelativeCoordinates, bOOXMLMotionPath);
880
881 putNumberChar(aResult, aEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
882 aCurrentSVGPosition = aEdgeEnd;
883 }
884 else if(bYEqual && !bOOXMLMotionPath)
885 {
886 // export as horizontal line
887 putCommandChar(aResult, aLastSVGCommand, 'H', bUseRelativeCoordinates, bOOXMLMotionPath);
888
889 putNumberChar(aResult, aEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
890 aCurrentSVGPosition = aEdgeEnd;
891 }
892 else
893 {
894 // export as line
895 putCommandChar(aResult, aLastSVGCommand, 'L', bUseRelativeCoordinates, bOOXMLMotionPath);
896
897 putNumberChar(aResult, aEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
898 putNumberChar(aResult, aEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
899 aCurrentSVGPosition = aEdgeEnd;
900 }
901 }
902 }
903
904 // prepare edge start for next loop step
905 aEdgeStart = aEdgeEnd;
906 }
907
908 // close path if closed poly (Z and z are equivalent here, but looks nicer when case is matched)
909 if(aPolygon.isClosed())
910 {
911 putCommandChar(aResult, aLastSVGCommand, 'Z', bUseRelativeCoordinates, bOOXMLMotionPath);
912 }
913 else if (bOOXMLMotionPath)
914 {
915 putCommandChar(aResult, aLastSVGCommand, 'E', bUseRelativeCoordinates, bOOXMLMotionPath);
916 }
917
918 if(!bHandleRelativeNextPointCompatible)
919 {
920 // SVG defines that "the next subpath starts at the same initial point as the current subpath",
921 // so set aCurrentSVGPosition to the 1st point of the current, now ended and written path
922 aCurrentSVGPosition = aPolygon.getB2DPoint(0);
923 }
924 }
925 }
926
927 return aResult.makeStringAndClear();
928 }
929}
930
931/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void rotate(double fRadiant)
void translate(double fX, double fY)
Base Point class with two double values.
Definition: b2dpoint.hxx:42
B2DPolygon const & getB2DPolygon(sal_uInt32 nIndex) const
void append(const B2DPolygon &rPolygon, sal_uInt32 nCount=1)
sal_uInt32 count() const
bool isPrevControlPointUsed(sal_uInt32 nIndex) const
bool isNextControlPointUsed(sal_uInt32 nIndex) const
void clear()
clear all points
void appendQuadraticBezierSegment(const basegfx::B2DPoint &rQuadControlPoint, const basegfx::B2DPoint &rPoint)
This is a shortcut to append a quadratic bezier segment.
bool isClosed() const
closed state interface
basegfx::B2DPoint const & getB2DPoint(sal_uInt32 nIndex) const
Coordinate interface.
void transform(const basegfx::B2DHomMatrix &rMatrix)
apply transformation given in matrix form
basegfx::B2DPoint getPrevControlPoint(sal_uInt32 nIndex) const
Basic ControlPoint interface.
bool areControlPointsUsed() const
ControlPoint checks.
B2VectorContinuity getContinuityInPoint(sal_uInt32 nIndex) const
void append(const basegfx::B2DPoint &rPoint, sal_uInt32 nCount)
sal_uInt32 count() const
member count
void setClosed(bool bNew)
basegfx::B2DPoint getNextControlPoint(sal_uInt32 nIndex) const
void flip()
flip polygon direction
void appendBezierSegment(const basegfx::B2DPoint &rNextControlPoint, const basegfx::B2DPoint &rPrevControlPoint, const basegfx::B2DPoint &rPoint)
Bezier segment append with control points. The current last polygon point is implicitly taken as star...
Base Point class with two double values.
Definition: b2dvector.hxx:40
bool equal(const Tuple2D< TYPE > &rTup) const
Definition: Tuple2D.hxx:83
TYPE getX() const
Get X-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:63
void setY(TYPE fY)
Set Y-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:72
TYPE getY() const
Get Y-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:66
void setX(TYPE fX)
Set X-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:69
Helper class to transport PointIndices to a PolyPolygon, with an operator< for convenient sorting in ...
bool operator<(const PointIndex &rComp) const
std::pair< const_iterator, bool > insert(Value &&x)
int nCount
sal_Int32 nIndex
uno_Any a
sal_uInt16 nPos
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)
void skipSpaces(sal_Int32 &io_rPos, std::u16string_view rStr, const sal_Int32 nLen)
bool importFlagAndSpaces(sal_Int32 &o_nRetval, sal_Int32 &io_rPos, std::u16string_view rStr, const sal_Int32 nLen)
bool importDoubleAndSpaces(double &o_fRetval, sal_Int32 &io_rPos, std::u16string_view rStr, const sal_Int32 nLen)
bool isOnNumberChar(const sal_Unicode aChar, bool bSignAllowed)
bool importFromSvgPoints(B2DPolygon &o_rPoly, std::u16string_view rSvgPointsAttribute)
Read poly-polygon from SVG.
OUString exportToSvgD(const B2DPolyPolygon &rPolyPolygon, bool bUseRelativeCoordinates, bool bDetectQuadraticBeziers, bool bHandleRelativeNextPointCompatible, bool bOOXMLMotionPath)
Export poly-polygon to SVG.
B2DHomMatrix createScaleB2DHomMatrix(double fScaleX, double fScaleY)
Tooling methods for on-the-fly matrix generation e.g.
OUString exportToSvgPoints(const B2DPolygon &rPoly)
Write poly-polygon to SVG.
bool importFromSvgD(B2DPolyPolygon &o_rPolyPolygon, std::u16string_view rSvgDStatement, bool bHandleRelativeNextPointCompatible, PointIndexSet *pHelpPointIndexSet)
Read poly-polygon from SVG.
B2DHomMatrix createRotateB2DHomMatrix(double fRadiant)
B2DPolygon createPolygonFromUnitEllipseSegment(double fStart, double fEnd)
@ C2
mathematically neutral, thus parallel
constexpr double deg2rad(double v)
Convert value from degrees to radians.
Definition: ftools.hxx:82
int i
OUString aCommand
sal_uInt16 sal_Unicode