1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <basegfx/polygon/b2dpolygontools.hxx>
21 #include <basegfx/polygon/b2dpolypolygontools.hxx>
22 #include <basegfx/polygon/b2dpolypolygon.hxx>
23 #include <basegfx/matrix/b2dhommatrix.hxx>
24 #include <basegfx/matrix/b2dhommatrixtools.hxx>
26 #include <rtl/ustring.hxx>
27 #include <rtl/math.hxx>
28 #include <stringconversiontools.hxx>
34 bool PointIndex::operator<(const PointIndex
& rComp
) const
36 if(rComp
.getPolygonIndex() == getPolygonIndex())
38 return rComp
.getPointIndex() < getPointIndex();
41 return rComp
.getPolygonIndex() < getPolygonIndex();
45 B2DPolyPolygon
& o_rPolyPolygon
,
46 const OUString
& rSvgDStatement
,
47 bool bHandleRelativeNextPointCompatible
,
48 PointIndexSet
* pHelpPointIndexSet
)
50 o_rPolyPolygon
.clear();
51 const sal_Int32
nLen(rSvgDStatement
.getLength());
57 // skip initial whitespace
58 basegfx::internal::skipSpaces(nPos
, rSvgDStatement
, nLen
);
62 bool bRelative(false);
63 const sal_Unicode
aCurrChar(rSvgDStatement
[nPos
]);
65 if(o_rPolyPolygon
.count() && !aCurrPoly
.count() && !(aCurrChar
== 'm' || aCurrChar
== 'M'))
67 // we have a new sub-polygon starting, but without a 'moveto' command.
68 // this requires to add the current point as start point to the polygon
69 // (see SVG1.1 8.3.3 The "closepath" command)
70 aCurrPoly
.append(B2DPoint(nLastX
, nLastY
));
78 // consume CurrChar and whitespace
80 basegfx::internal::skipSpaces(nPos
, rSvgDStatement
, nLen
);
82 // create closed polygon and reset import values
85 if(!bHandleRelativeNextPointCompatible
)
87 // SVG defines that "the next subpath starts at the
88 // same initial point as the current subpath", so set the
89 // current point if we do not need to be compatible
90 nLastX
= aCurrPoly
.getB2DPoint(0).getX();
91 nLastY
= aCurrPoly
.getB2DPoint(0).getY();
94 aCurrPoly
.setClosed(true);
95 o_rPolyPolygon
.append(aCurrPoly
);
105 // create non-closed polygon and reset import values
106 if(aCurrPoly
.count())
108 o_rPolyPolygon
.append(aCurrPoly
);
111 SAL_FALLTHROUGH
; // to add coordinate data as 1st point of new polygon
116 if(aCurrChar
== 'm' || aCurrChar
== 'l')
121 // consume CurrChar and whitespace
123 basegfx::internal::skipSpaces(nPos
, rSvgDStatement
, nLen
);
125 while(nPos
< nLen
&& basegfx::internal::isOnNumberChar(rSvgDStatement
, nPos
))
129 if(!basegfx::internal::importDoubleAndSpaces(nX
, nPos
, rSvgDStatement
, nLen
)) return false;
130 if(!basegfx::internal::importDoubleAndSpaces(nY
, nPos
, rSvgDStatement
, nLen
)) return false;
143 aCurrPoly
.append(B2DPoint(nX
, nY
));
156 basegfx::internal::skipSpaces(nPos
, rSvgDStatement
, nLen
);
158 while(nPos
< nLen
&& basegfx::internal::isOnNumberChar(rSvgDStatement
, nPos
))
160 double nX
, nY(nLastY
);
162 if(!basegfx::internal::importDoubleAndSpaces(nX
, nPos
, rSvgDStatement
, nLen
)) return false;
173 aCurrPoly
.append(B2DPoint(nX
, nY
));
186 basegfx::internal::skipSpaces(nPos
, rSvgDStatement
, nLen
);
188 while(nPos
< nLen
&& basegfx::internal::isOnNumberChar(rSvgDStatement
, nPos
))
190 double nX(nLastX
), nY
;
192 if(!basegfx::internal::importDoubleAndSpaces(nY
, nPos
, rSvgDStatement
, nLen
)) return false;
203 aCurrPoly
.append(B2DPoint(nX
, nY
));
216 basegfx::internal::skipSpaces(nPos
, rSvgDStatement
, nLen
);
218 while(nPos
< nLen
&& basegfx::internal::isOnNumberChar(rSvgDStatement
, nPos
))
223 if(!basegfx::internal::importDoubleAndSpaces(nX2
, nPos
, rSvgDStatement
, nLen
)) return false;
224 if(!basegfx::internal::importDoubleAndSpaces(nY2
, nPos
, rSvgDStatement
, nLen
)) return false;
225 if(!basegfx::internal::importDoubleAndSpaces(nX
, nPos
, rSvgDStatement
, nLen
)) return false;
226 if(!basegfx::internal::importDoubleAndSpaces(nY
, nPos
, rSvgDStatement
, nLen
)) return false;
236 // ensure existence of start point
237 if(!aCurrPoly
.count())
239 aCurrPoly
.append(B2DPoint(nLastX
, nLastY
));
242 // get first control point. It's the reflection of the PrevControlPoint
243 // of the last point. If not existent, use current point (see SVG)
244 B2DPoint
aPrevControl(B2DPoint(nLastX
, nLastY
));
245 const sal_uInt32
nIndex(aCurrPoly
.count() - 1);
247 if(aCurrPoly
.areControlPointsUsed() && aCurrPoly
.isPrevControlPointUsed(nIndex
))
249 const B2DPoint
aPrevPoint(aCurrPoly
.getB2DPoint(nIndex
));
250 const B2DPoint
aPrevControlPoint(aCurrPoly
.getPrevControlPoint(nIndex
));
252 // use mirrored previous control point
253 aPrevControl
.setX((2.0 * aPrevPoint
.getX()) - aPrevControlPoint
.getX());
254 aPrevControl
.setY((2.0 * aPrevPoint
.getY()) - aPrevControlPoint
.getY());
257 // append curved edge
258 aCurrPoly
.appendBezierSegment(aPrevControl
, B2DPoint(nX2
, nY2
), B2DPoint(nX
, nY
));
275 basegfx::internal::skipSpaces(nPos
, rSvgDStatement
, nLen
);
277 while(nPos
< nLen
&& basegfx::internal::isOnNumberChar(rSvgDStatement
, nPos
))
283 if(!basegfx::internal::importDoubleAndSpaces(nX1
, nPos
, rSvgDStatement
, nLen
)) return false;
284 if(!basegfx::internal::importDoubleAndSpaces(nY1
, nPos
, rSvgDStatement
, nLen
)) return false;
285 if(!basegfx::internal::importDoubleAndSpaces(nX2
, nPos
, rSvgDStatement
, nLen
)) return false;
286 if(!basegfx::internal::importDoubleAndSpaces(nY2
, nPos
, rSvgDStatement
, nLen
)) return false;
287 if(!basegfx::internal::importDoubleAndSpaces(nX
, nPos
, rSvgDStatement
, nLen
)) return false;
288 if(!basegfx::internal::importDoubleAndSpaces(nY
, nPos
, rSvgDStatement
, nLen
)) return false;
300 // ensure existence of start point
301 if(!aCurrPoly
.count())
303 aCurrPoly
.append(B2DPoint(nLastX
, nLastY
));
306 // append curved edge
307 aCurrPoly
.appendBezierSegment(B2DPoint(nX1
, nY1
), B2DPoint(nX2
, nY2
), B2DPoint(nX
, nY
));
316 // #100617# quadratic beziers are imported as cubic ones
325 basegfx::internal::skipSpaces(nPos
, rSvgDStatement
, nLen
);
327 while(nPos
< nLen
&& basegfx::internal::isOnNumberChar(rSvgDStatement
, nPos
))
332 if(!basegfx::internal::importDoubleAndSpaces(nX1
, nPos
, rSvgDStatement
, nLen
)) return false;
333 if(!basegfx::internal::importDoubleAndSpaces(nY1
, nPos
, rSvgDStatement
, nLen
)) return false;
334 if(!basegfx::internal::importDoubleAndSpaces(nX
, nPos
, rSvgDStatement
, nLen
)) return false;
335 if(!basegfx::internal::importDoubleAndSpaces(nY
, nPos
, rSvgDStatement
, nLen
)) return false;
345 // calculate the cubic bezier coefficients from the quadratic ones
346 const double nX1Prime((nX1
* 2.0 + nLastX
) / 3.0);
347 const double nY1Prime((nY1
* 2.0 + nLastY
) / 3.0);
348 const double nX2Prime((nX1
* 2.0 + nX
) / 3.0);
349 const double nY2Prime((nY1
* 2.0 + nY
) / 3.0);
351 // ensure existence of start point
352 if(!aCurrPoly
.count())
354 aCurrPoly
.append(B2DPoint(nLastX
, nLastY
));
357 // append curved edge
358 aCurrPoly
.appendBezierSegment(B2DPoint(nX1Prime
, nY1Prime
), B2DPoint(nX2Prime
, nY2Prime
), B2DPoint(nX
, nY
));
367 // #100617# relative quadratic beziers are imported as cubic
376 basegfx::internal::skipSpaces(nPos
, rSvgDStatement
, nLen
);
378 while(nPos
< nLen
&& basegfx::internal::isOnNumberChar(rSvgDStatement
, nPos
))
382 if(!basegfx::internal::importDoubleAndSpaces(nX
, nPos
, rSvgDStatement
, nLen
)) return false;
383 if(!basegfx::internal::importDoubleAndSpaces(nY
, nPos
, rSvgDStatement
, nLen
)) return false;
391 // ensure existence of start point
392 if(!aCurrPoly
.count())
394 aCurrPoly
.append(B2DPoint(nLastX
, nLastY
));
397 // get first control point. It's the reflection of the PrevControlPoint
398 // of the last point. If not existent, use current point (see SVG)
399 B2DPoint
aPrevControl(B2DPoint(nLastX
, nLastY
));
400 const sal_uInt32
nIndex(aCurrPoly
.count() - 1);
401 const B2DPoint
aPrevPoint(aCurrPoly
.getB2DPoint(nIndex
));
403 if(aCurrPoly
.areControlPointsUsed() && aCurrPoly
.isPrevControlPointUsed(nIndex
))
405 const B2DPoint
aPrevControlPoint(aCurrPoly
.getPrevControlPoint(nIndex
));
407 // use mirrored previous control point
408 aPrevControl
.setX((2.0 * aPrevPoint
.getX()) - aPrevControlPoint
.getX());
409 aPrevControl
.setY((2.0 * aPrevPoint
.getY()) - aPrevControlPoint
.getY());
412 if(!aPrevControl
.equal(aPrevPoint
))
414 // there is a prev control point, and we have the already mirrored one
415 // in aPrevControl. We also need the quadratic control point for this
416 // new quadratic segment to calculate the 2nd cubic control point
417 const B2DPoint
aQuadControlPoint(
418 ((3.0 * aPrevControl
.getX()) - aPrevPoint
.getX()) / 2.0,
419 ((3.0 * aPrevControl
.getY()) - aPrevPoint
.getY()) / 2.0);
421 // calculate the cubic bezier coefficients from the quadratic ones.
422 const double nX2Prime((aQuadControlPoint
.getX() * 2.0 + nX
) / 3.0);
423 const double nY2Prime((aQuadControlPoint
.getY() * 2.0 + nY
) / 3.0);
425 // append curved edge, use mirrored cubic control point directly
426 aCurrPoly
.appendBezierSegment(aPrevControl
, B2DPoint(nX2Prime
, nY2Prime
), B2DPoint(nX
, nY
));
430 // when no previous control, SVG says to use current point -> straight line.
431 // Just add end point
432 aCurrPoly
.append(B2DPoint(nX
, nY
));
450 basegfx::internal::skipSpaces(nPos
, rSvgDStatement
, nLen
);
452 while(nPos
< nLen
&& basegfx::internal::isOnNumberChar(rSvgDStatement
, nPos
))
455 double fRX
, fRY
, fPhi
;
456 sal_Int32 bLargeArcFlag
, bSweepFlag
;
458 if(!basegfx::internal::importDoubleAndSpaces(fRX
, nPos
, rSvgDStatement
, nLen
)) return false;
459 if(!basegfx::internal::importDoubleAndSpaces(fRY
, nPos
, rSvgDStatement
, nLen
)) return false;
460 if(!basegfx::internal::importDoubleAndSpaces(fPhi
, nPos
, rSvgDStatement
, nLen
)) return false;
461 if(!basegfx::internal::importFlagAndSpaces(bLargeArcFlag
, nPos
, rSvgDStatement
, nLen
)) return false;
462 if(!basegfx::internal::importFlagAndSpaces(bSweepFlag
, nPos
, rSvgDStatement
, nLen
)) return false;
463 if(!basegfx::internal::importDoubleAndSpaces(nX
, nPos
, rSvgDStatement
, nLen
)) return false;
464 if(!basegfx::internal::importDoubleAndSpaces(nY
, nPos
, rSvgDStatement
, nLen
)) return false;
472 if( rtl::math::approxEqual(nX
, nLastX
) && rtl::math::approxEqual(nY
, nLastY
) )
473 continue; // start==end -> skip according to SVG spec
475 if( fRX
== 0.0 || fRY
== 0.0 )
477 // straight line segment according to SVG spec
478 aCurrPoly
.append(B2DPoint(nX
, nY
));
482 // normalize according to SVG spec
483 fRX
=fabs(fRX
); fRY
=fabs(fRY
);
485 // from the SVG spec, appendix F.6.4
487 // |x1'| |cos phi sin phi| |(x1 - x2)/2|
488 // |y1'| = |-sin phi cos phi| |(y1 - y2)/2|
489 const B2DPoint
p1(nLastX
, nLastY
);
490 const B2DPoint
p2(nX
, nY
);
491 B2DHomMatrix
aTransform(basegfx::utils::createRotateB2DHomMatrix(-fPhi
*M_PI
/180));
493 const B2DPoint
p1_prime( aTransform
* B2DPoint(((p1
-p2
)/2.0)) );
495 // ______________________________________ rx y1'
496 // |cx'| + / rx^2 ry^2 - rx^2 y1'^2 - ry^2 x1^2 ry
497 // |cy'| =-/ rx^2y1'^2 + ry^2 x1'^2 - ry x1'
499 // chose + if f_A != f_S
500 // chose - if f_A = f_S
501 B2DPoint aCenter_prime
;
502 const double fRadicant(
503 (fRX
*fRX
*fRY
*fRY
- fRX
*fRX
*p1_prime
.getY()*p1_prime
.getY() - fRY
*fRY
*p1_prime
.getX()*p1_prime
.getX())/
504 (fRX
*fRX
*p1_prime
.getY()*p1_prime
.getY() + fRY
*fRY
*p1_prime
.getX()*p1_prime
.getX()));
505 if( fRadicant
< 0.0 )
507 // no solution - according to SVG
508 // spec, scale up ellipse
509 // uniformly such that it passes
510 // through end points (denominator
511 // of radicant solved for fRY,
513 const double fRatio(fRX
/fRY
);
514 const double fRadicant2(
515 p1_prime
.getY()*p1_prime
.getY() +
516 p1_prime
.getX()*p1_prime
.getX()/(fRatio
*fRatio
));
517 if( fRadicant2
< 0.0 )
519 // only trivial solution, one
520 // of the axes 0 -> straight
521 // line segment according to
523 aCurrPoly
.append(B2DPoint(nX
, nY
));
527 fRY
=sqrt(fRadicant2
);
530 // keep center_prime forced to (0,0)
534 const double fFactor(
535 (bLargeArcFlag
==bSweepFlag
? -1.0 : 1.0) *
538 // actually calculate center_prime
539 aCenter_prime
= B2DPoint(
540 fFactor
*fRX
*p1_prime
.getY()/fRY
,
541 -fFactor
*fRY
*p1_prime
.getX()/fRX
);
545 // angle(u,v) = arccos( ------------ ) (take the sign of (ux vy - uy vx))
548 // 1 | (x1' - cx')/rx |
549 // theta1 = angle(( ), | | )
550 // 0 | (y1' - cy')/ry |
551 const B2DPoint
aRadii(fRX
,fRY
);
553 B2DVector(1.0,0.0).angle(
554 (p1_prime
-aCenter_prime
)/aRadii
));
556 // |1| | (-x1' - cx')/rx |
557 // theta2 = angle( | | , | | )
558 // |0| | (-y1' - cy')/ry |
560 B2DVector(1.0,0.0).angle(
561 (-p1_prime
-aCenter_prime
)/aRadii
));
563 // map both angles to [0,2pi)
564 fTheta1
= fmod(2*M_PI
+fTheta1
,2*M_PI
);
565 fTheta2
= fmod(2*M_PI
+fTheta2
,2*M_PI
);
567 // make sure the large arc is taken
569 // createPolygonFromEllipseSegment()
570 // normalizes to e.g. cw arc)
572 std::swap(fTheta1
,fTheta2
);
574 // finally, create bezier polygon from this
576 utils::createPolygonFromUnitEllipseSegment(
579 // transform ellipse by rotation & move to final center
580 aTransform
= basegfx::utils::createScaleB2DHomMatrix(fRX
, fRY
);
581 aTransform
.translate(aCenter_prime
.getX(),
582 aCenter_prime
.getY());
583 aTransform
.rotate(fPhi
*M_PI
/180);
584 const B2DPoint
aOffset((p1
+p2
)/2.0);
585 aTransform
.translate(aOffset
.getX(),
587 aSegment
.transform(aTransform
);
589 // createPolygonFromEllipseSegment()
590 // always creates arcs that are
591 // positively oriented - flip polygon
592 // if we swapped angles above
596 // remember PointIndex of evtl. added pure helper points
597 sal_uInt32
nPointIndex(aCurrPoly
.count() + 1);
598 aCurrPoly
.append(aSegment
);
600 // if asked for, mark pure helper points by adding them to the index list of
602 if(pHelpPointIndexSet
&& aCurrPoly
.count() > 1)
604 const sal_uInt32
nPolyIndex(o_rPolyPolygon
.count());
606 for(;nPointIndex
+ 1 < aCurrPoly
.count(); nPointIndex
++)
608 pHelpPointIndexSet
->insert(PointIndex(nPolyIndex
, nPointIndex
));
622 SAL_WARN("basegfx", "importFromSvgD(): skipping tags in svg:d element (unknown: \""
631 // if there is polygon data, create non-closed polygon
632 if(aCurrPoly
.count())
634 o_rPolyPolygon
.append(aCurrPoly
);
640 bool importFromSvgPoints( B2DPolygon
& o_rPoly
,
641 const OUString
& rSvgPointsAttribute
)
644 const sal_Int32
nLen(rSvgPointsAttribute
.getLength());
648 // skip initial whitespace
649 basegfx::internal::skipSpaces(nPos
, rSvgPointsAttribute
, nLen
);
653 if(!basegfx::internal::importDoubleAndSpaces(nX
, nPos
, rSvgPointsAttribute
, nLen
)) return false;
654 if(!basegfx::internal::importDoubleAndSpaces(nY
, nPos
, rSvgPointsAttribute
, nLen
)) return false;
657 o_rPoly
.append(B2DPoint(nX
, nY
));
659 // skip to next number, or finish
660 basegfx::internal::skipSpaces(nPos
, rSvgPointsAttribute
, nLen
);
666 OUString
exportToSvgPoints( const B2DPolygon
& rPoly
)
668 SAL_WARN_IF(rPoly
.areControlPointsUsed(), "basegfx", "exportToSvgPoints: Only non-bezier polygons allowed (!)");
669 const sal_uInt32
nPointCount(rPoly
.count());
670 OUStringBuffer aResult
;
672 for(sal_uInt32
a(0); a
< nPointCount
; a
++)
674 const basegfx::B2DPoint
aPoint(rPoly
.getB2DPoint(a
));
681 aResult
.append(aPoint
.getX());
683 aResult
.append(aPoint
.getY());
686 return aResult
.makeStringAndClear();
689 OUString
exportToSvgD(
690 const B2DPolyPolygon
& rPolyPolygon
,
691 bool bUseRelativeCoordinates
,
692 bool bDetectQuadraticBeziers
,
693 bool bHandleRelativeNextPointCompatible
)
695 const sal_uInt32
nCount(rPolyPolygon
.count());
696 OUStringBuffer aResult
;
697 B2DPoint
aCurrentSVGPosition(0.0, 0.0); // SVG assumes (0,0) as the initial current point
699 for(sal_uInt32
i(0); i
< nCount
; i
++)
701 const B2DPolygon
aPolygon(rPolyPolygon
.getB2DPolygon(i
));
702 const sal_uInt32
nPointCount(aPolygon
.count());
706 const bool bPolyUsesControlPoints(aPolygon
.areControlPointsUsed());
707 const sal_uInt32
nEdgeCount(aPolygon
.isClosed() ? nPointCount
: nPointCount
- 1);
708 sal_Unicode
aLastSVGCommand(' '); // last SVG command char
709 B2DPoint aLeft
, aRight
; // for quadratic bezier test
711 // handle polygon start point
712 B2DPoint
aEdgeStart(aPolygon
.getB2DPoint(0));
713 bool bUseRelativeCoordinatesForFirstPoint(bUseRelativeCoordinates
);
715 if(bHandleRelativeNextPointCompatible
)
717 // To get around the error that the start point for the next polygon is the
718 // start point of the current one (and not the last as it was handled up to now)
719 // do force to write an absolute 'M' command as start for the next polygon
720 bUseRelativeCoordinatesForFirstPoint
= false;
723 // Write 'moveto' and the 1st coordinates, set aLastSVGCommand to 'lineto'
724 aResult
.append(basegfx::internal::getCommand('M', 'm', bUseRelativeCoordinatesForFirstPoint
));
725 basegfx::internal::putNumberCharWithSpace(aResult
, aEdgeStart
.getX(), aCurrentSVGPosition
.getX(), bUseRelativeCoordinatesForFirstPoint
);
726 basegfx::internal::putNumberCharWithSpace(aResult
, aEdgeStart
.getY(), aCurrentSVGPosition
.getY(), bUseRelativeCoordinatesForFirstPoint
);
727 aLastSVGCommand
= basegfx::internal::getCommand('L', 'l', bUseRelativeCoordinatesForFirstPoint
);
728 aCurrentSVGPosition
= aEdgeStart
;
730 for(sal_uInt32
nIndex(0); nIndex
< nEdgeCount
; nIndex
++)
732 // prepare access to next point
733 const sal_uInt32
nNextIndex((nIndex
+ 1) % nPointCount
);
734 const B2DPoint
aEdgeEnd(aPolygon
.getB2DPoint(nNextIndex
));
736 // handle edge from (aEdgeStart, aEdgeEnd) using indices (nIndex, nNextIndex)
737 const bool bEdgeIsBezier(bPolyUsesControlPoints
738 && (aPolygon
.isNextControlPointUsed(nIndex
) || aPolygon
.isPrevControlPointUsed(nNextIndex
)));
742 // handle bezier edge
743 const B2DPoint
aControlEdgeStart(aPolygon
.getNextControlPoint(nIndex
));
744 const B2DPoint
aControlEdgeEnd(aPolygon
.getPrevControlPoint(nNextIndex
));
745 bool bIsQuadraticBezier(false);
747 // check continuity at current edge's start point. For SVG, do NOT use an
748 // existing continuity since no 'S' or 's' statement should be written. At
749 // import, that 'previous' control vector is not available. SVG documentation
750 // says for interpretation:
752 // "(If there is no previous command or if the previous command was
753 // not an C, c, S or s, assume the first control point is coincident
754 // with the current point.)"
756 // That's what is done from our import, so avoid exporting it as first statement
758 const bool bSymmetricAtEdgeStart(
760 && aPolygon
.getContinuityInPoint(nIndex
) == B2VectorContinuity::C2
);
762 if(bDetectQuadraticBeziers
)
764 // check for quadratic beziers - that's
765 // the case if both control points are in
766 // the same place when they are prolonged
767 // to the common quadratic control point
769 // Left: P = (3P1 - P0) / 2
770 // Right: P = (3P2 - P3) / 2
771 aLeft
= B2DPoint((3.0 * aControlEdgeStart
- aEdgeStart
) / 2.0);
772 aRight
= B2DPoint((3.0 * aControlEdgeEnd
- aEdgeEnd
) / 2.0);
773 bIsQuadraticBezier
= aLeft
.equal(aRight
);
776 if(bIsQuadraticBezier
)
778 // approximately equal, export as quadratic bezier
779 if(bSymmetricAtEdgeStart
)
781 const sal_Unicode
aCommand(basegfx::internal::getCommand('T', 't', bUseRelativeCoordinates
));
783 if(aLastSVGCommand
!= aCommand
)
785 aResult
.append(aCommand
);
788 basegfx::internal::putNumberCharWithSpace(aResult
, aEdgeEnd
.getX(), aCurrentSVGPosition
.getX(), bUseRelativeCoordinates
);
789 basegfx::internal::putNumberCharWithSpace(aResult
, aEdgeEnd
.getY(), aCurrentSVGPosition
.getY(), bUseRelativeCoordinates
);
790 aLastSVGCommand
= aCommand
;
791 aCurrentSVGPosition
= aEdgeEnd
;
795 const sal_Unicode
aCommand(basegfx::internal::getCommand('Q', 'q', bUseRelativeCoordinates
));
797 if(aLastSVGCommand
!= aCommand
)
799 aResult
.append(aCommand
);
802 basegfx::internal::putNumberCharWithSpace(aResult
, aLeft
.getX(), aCurrentSVGPosition
.getX(), bUseRelativeCoordinates
);
803 basegfx::internal::putNumberCharWithSpace(aResult
, aLeft
.getY(), aCurrentSVGPosition
.getY(), bUseRelativeCoordinates
);
804 basegfx::internal::putNumberCharWithSpace(aResult
, aEdgeEnd
.getX(), aCurrentSVGPosition
.getX(), bUseRelativeCoordinates
);
805 basegfx::internal::putNumberCharWithSpace(aResult
, aEdgeEnd
.getY(), aCurrentSVGPosition
.getY(), bUseRelativeCoordinates
);
806 aLastSVGCommand
= aCommand
;
807 aCurrentSVGPosition
= aEdgeEnd
;
812 // export as cubic bezier
813 if(bSymmetricAtEdgeStart
)
815 const sal_Unicode
aCommand(basegfx::internal::getCommand('S', 's', bUseRelativeCoordinates
));
817 if(aLastSVGCommand
!= aCommand
)
819 aResult
.append(aCommand
);
822 basegfx::internal::putNumberCharWithSpace(aResult
, aControlEdgeEnd
.getX(), aCurrentSVGPosition
.getX(), bUseRelativeCoordinates
);
823 basegfx::internal::putNumberCharWithSpace(aResult
, aControlEdgeEnd
.getY(), aCurrentSVGPosition
.getY(), bUseRelativeCoordinates
);
824 basegfx::internal::putNumberCharWithSpace(aResult
, aEdgeEnd
.getX(), aCurrentSVGPosition
.getX(), bUseRelativeCoordinates
);
825 basegfx::internal::putNumberCharWithSpace(aResult
, aEdgeEnd
.getY(), aCurrentSVGPosition
.getY(), bUseRelativeCoordinates
);
826 aLastSVGCommand
= aCommand
;
827 aCurrentSVGPosition
= aEdgeEnd
;
831 const sal_Unicode
aCommand(basegfx::internal::getCommand('C', 'c', bUseRelativeCoordinates
));
833 if(aLastSVGCommand
!= aCommand
)
835 aResult
.append(aCommand
);
838 basegfx::internal::putNumberCharWithSpace(aResult
, aControlEdgeStart
.getX(), aCurrentSVGPosition
.getX(), bUseRelativeCoordinates
);
839 basegfx::internal::putNumberCharWithSpace(aResult
, aControlEdgeStart
.getY(), aCurrentSVGPosition
.getY(), bUseRelativeCoordinates
);
840 basegfx::internal::putNumberCharWithSpace(aResult
, aControlEdgeEnd
.getX(), aCurrentSVGPosition
.getX(), bUseRelativeCoordinates
);
841 basegfx::internal::putNumberCharWithSpace(aResult
, aControlEdgeEnd
.getY(), aCurrentSVGPosition
.getY(), bUseRelativeCoordinates
);
842 basegfx::internal::putNumberCharWithSpace(aResult
, aEdgeEnd
.getX(), aCurrentSVGPosition
.getX(), bUseRelativeCoordinates
);
843 basegfx::internal::putNumberCharWithSpace(aResult
, aEdgeEnd
.getY(), aCurrentSVGPosition
.getY(), bUseRelativeCoordinates
);
844 aLastSVGCommand
= aCommand
;
845 aCurrentSVGPosition
= aEdgeEnd
;
854 // it's a closed polygon's last edge and it's not a bezier edge, so there is
855 // no need to write it
859 const bool bXEqual(rtl::math::approxEqual(aEdgeStart
.getX(), aEdgeEnd
.getX()));
860 const bool bYEqual(rtl::math::approxEqual(aEdgeStart
.getY(), aEdgeEnd
.getY()));
862 if(bXEqual
&& bYEqual
)
864 // point is a double point; do not export at all
868 // export as vertical line
869 const sal_Unicode
aCommand(basegfx::internal::getCommand('V', 'v', bUseRelativeCoordinates
));
871 if(aLastSVGCommand
!= aCommand
)
873 aResult
.append(aCommand
);
874 aLastSVGCommand
= aCommand
;
877 basegfx::internal::putNumberCharWithSpace(aResult
, aEdgeEnd
.getY(), aCurrentSVGPosition
.getY(), bUseRelativeCoordinates
);
878 aCurrentSVGPosition
= aEdgeEnd
;
882 // export as horizontal line
883 const sal_Unicode
aCommand(basegfx::internal::getCommand('H', 'h', bUseRelativeCoordinates
));
885 if(aLastSVGCommand
!= aCommand
)
887 aResult
.append(aCommand
);
888 aLastSVGCommand
= aCommand
;
891 basegfx::internal::putNumberCharWithSpace(aResult
, aEdgeEnd
.getX(), aCurrentSVGPosition
.getX(), bUseRelativeCoordinates
);
892 aCurrentSVGPosition
= aEdgeEnd
;
897 const sal_Unicode
aCommand(basegfx::internal::getCommand('L', 'l', bUseRelativeCoordinates
));
899 if(aLastSVGCommand
!= aCommand
)
901 aResult
.append(aCommand
);
902 aLastSVGCommand
= aCommand
;
905 basegfx::internal::putNumberCharWithSpace(aResult
, aEdgeEnd
.getX(), aCurrentSVGPosition
.getX(), bUseRelativeCoordinates
);
906 basegfx::internal::putNumberCharWithSpace(aResult
, aEdgeEnd
.getY(), aCurrentSVGPosition
.getY(), bUseRelativeCoordinates
);
907 aCurrentSVGPosition
= aEdgeEnd
;
912 // prepare edge start for next loop step
913 aEdgeStart
= aEdgeEnd
;
916 // close path if closed poly (Z and z are equivalent here, but looks nicer when case is matched)
917 if(aPolygon
.isClosed())
919 aResult
.append(basegfx::internal::getCommand('Z', 'z', bUseRelativeCoordinates
));
922 if(!bHandleRelativeNextPointCompatible
)
924 // SVG defines that "the next subpath starts at the same initial point as the current subpath",
925 // so set aCurrentSVGPosition to the 1st point of the current, now ended and written path
926 aCurrentSVGPosition
= aPolygon
.getB2DPoint(0);
931 return aResult
.makeStringAndClear();
936 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */