LCOV - code coverage report
Current view: top level - libreoffice/basegfx/source/polygon - b2dlinegeometry.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 115 357 32.2 %
Date: 2012-12-27 Functions: 4 7 57.1 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : 
      20             : #include <cstdio>
      21             : #include <osl/diagnose.h>
      22             : #include <basegfx/polygon/b2dlinegeometry.hxx>
      23             : #include <basegfx/point/b2dpoint.hxx>
      24             : #include <basegfx/vector/b2dvector.hxx>
      25             : #include <basegfx/polygon/b2dpolygontools.hxx>
      26             : #include <basegfx/polygon/b2dpolypolygontools.hxx>
      27             : #include <basegfx/range/b2drange.hxx>
      28             : #include <basegfx/matrix/b2dhommatrix.hxx>
      29             : #include <basegfx/curve/b2dcubicbezier.hxx>
      30             : #include <basegfx/matrix/b2dhommatrixtools.hxx>
      31             : #include <com/sun/star/drawing/LineCap.hpp>
      32             : #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
      33             : 
      34             : //////////////////////////////////////////////////////////////////////////////
      35             : 
      36             : namespace basegfx
      37             : {
      38             :     namespace tools
      39             :     {
      40           2 :         B2DPolyPolygon createAreaGeometryForLineStartEnd(
      41             :             const B2DPolygon& rCandidate,
      42             :             const B2DPolyPolygon& rArrow,
      43             :             bool bStart,
      44             :             double fWidth,
      45             :             double fCandidateLength,
      46             :             double fDockingPosition, // 0->top, 1->bottom
      47             :             double* pConsumedLength)
      48             :         {
      49           2 :             B2DPolyPolygon aRetval;
      50             :             OSL_ENSURE(rCandidate.count() > 1L, "createAreaGeometryForLineStartEnd: Line polygon has too less points (!)");
      51             :             OSL_ENSURE(rArrow.count() > 0L, "createAreaGeometryForLineStartEnd: Empty arrow PolyPolygon (!)");
      52             :             OSL_ENSURE(fWidth > 0.0, "createAreaGeometryForLineStartEnd: Width too small (!)");
      53             :             OSL_ENSURE(fDockingPosition >= 0.0 && fDockingPosition <= 1.0,
      54             :                 "createAreaGeometryForLineStartEnd: fDockingPosition out of range [0.0 .. 1.0] (!)");
      55             : 
      56           2 :             if(fWidth < 0.0)
      57             :             {
      58           0 :                 fWidth = -fWidth;
      59             :             }
      60             : 
      61           2 :             if(rCandidate.count() > 1 && rArrow.count() && !fTools::equalZero(fWidth))
      62             :             {
      63           2 :                 if(fDockingPosition < 0.0)
      64             :                 {
      65           0 :                     fDockingPosition = 0.0;
      66             :                 }
      67           2 :                 else if(fDockingPosition > 1.0)
      68             :                 {
      69           0 :                     fDockingPosition = 1.0;
      70             :                 }
      71             : 
      72             :                 // init return value from arrow
      73           2 :                 aRetval.append(rArrow);
      74             : 
      75             :                 // get size of the arrow
      76           2 :                 const B2DRange aArrowSize(getRange(rArrow));
      77             : 
      78             :                 // build ArrowTransform; center in X, align with axis in Y
      79             :                 B2DHomMatrix aArrowTransform(basegfx::tools::createTranslateB2DHomMatrix(
      80           2 :                     -aArrowSize.getCenter().getX(), -aArrowSize.getMinimum().getY()));
      81             : 
      82             :                 // scale to target size
      83           2 :                 const double fArrowScale(fWidth / (aArrowSize.getRange().getX()));
      84           2 :                 aArrowTransform.scale(fArrowScale, fArrowScale);
      85             : 
      86             :                 // get arrow size in Y
      87           2 :                 B2DPoint aUpperCenter(aArrowSize.getCenter().getX(), aArrowSize.getMaximum().getY());
      88           2 :                 aUpperCenter *= aArrowTransform;
      89           2 :                 const double fArrowYLength(B2DVector(aUpperCenter).getLength());
      90             : 
      91             :                 // move arrow to have docking position centered
      92           2 :                 aArrowTransform.translate(0.0, -fArrowYLength * fDockingPosition);
      93             : 
      94             :                 // prepare polygon length
      95           2 :                 if(fTools::equalZero(fCandidateLength))
      96             :                 {
      97           0 :                     fCandidateLength = getLength(rCandidate);
      98             :                 }
      99             : 
     100             :                 // get the polygon vector we want to plant this arrow on
     101           2 :                 const double fConsumedLength(fArrowYLength * (1.0 - fDockingPosition));
     102           2 :                 const B2DVector aHead(rCandidate.getB2DPoint((bStart) ? 0L : rCandidate.count() - 1L));
     103             :                 const B2DVector aTail(getPositionAbsolute(rCandidate,
     104           2 :                     (bStart) ? fConsumedLength : fCandidateLength - fConsumedLength, fCandidateLength));
     105             : 
     106             :                 // from that vector, take the needed rotation and add rotate for arrow to transformation
     107           2 :                 const B2DVector aTargetDirection(aHead - aTail);
     108           2 :                 const double fRotation(atan2(aTargetDirection.getY(), aTargetDirection.getX()) + (90.0 * F_PI180));
     109             : 
     110             :                 // rotate around docking position
     111           2 :                 aArrowTransform.rotate(fRotation);
     112             : 
     113             :                 // move arrow docking position to polygon head
     114           2 :                 aArrowTransform.translate(aHead.getX(), aHead.getY());
     115             : 
     116             :                 // transform retval and close
     117           2 :                 aRetval.transform(aArrowTransform);
     118           2 :                 aRetval.setClosed(true);
     119             : 
     120             :                 // if pConsumedLength is asked for, fill it
     121           2 :                 if(pConsumedLength)
     122             :                 {
     123           1 :                     *pConsumedLength = fConsumedLength;
     124           2 :                 }
     125             :             }
     126             : 
     127           2 :             return aRetval;
     128             :         }
     129             :     } // end of namespace tools
     130             : } // end of namespace basegfx
     131             : 
     132             : //////////////////////////////////////////////////////////////////////////////
     133             : 
     134             : namespace basegfx
     135             : {
     136             :     // anonymus namespace for local helpers
     137             :     namespace
     138             :     {
     139           0 :         bool impIsSimpleEdge(const B2DCubicBezier& rCandidate, double fMaxCosQuad, double fMaxPartOfEdgeQuad)
     140             :         {
     141             :             // isBezier() is true, already tested by caller
     142           0 :             const B2DVector aEdge(rCandidate.getEndPoint() - rCandidate.getStartPoint());
     143             : 
     144           0 :             if(aEdge.equalZero())
     145             :             {
     146             :                 // start and end point the same, but control vectors used -> baloon curve loop
     147             :                 // is not a simple edge
     148           0 :                 return false;
     149             :             }
     150             : 
     151             :             // get tangentA and scalar with edge
     152           0 :             const B2DVector aTangentA(rCandidate.getTangent(0.0));
     153           0 :             const double fScalarAE(aEdge.scalar(aTangentA));
     154             : 
     155           0 :             if(fTools::lessOrEqual(fScalarAE, 0.0))
     156             :             {
     157             :                 // angle between TangentA and Edge is bigger or equal 90 degrees
     158           0 :                 return false;
     159             :             }
     160             : 
     161             :             // get self-scalars for E and A
     162           0 :             const double fScalarE(aEdge.scalar(aEdge));
     163           0 :             const double fScalarA(aTangentA.scalar(aTangentA));
     164           0 :             const double fLengthCompareE(fScalarE * fMaxPartOfEdgeQuad);
     165             : 
     166           0 :             if(fTools::moreOrEqual(fScalarA, fLengthCompareE))
     167             :             {
     168             :                 // length of TangentA is more than fMaxPartOfEdge of length of edge
     169           0 :                 return false;
     170             :             }
     171             : 
     172           0 :             if(fTools::lessOrEqual(fScalarAE * fScalarAE, fScalarA * fScalarE * fMaxCosQuad))
     173             :             {
     174             :                 // angle between TangentA and Edge is bigger or equal angle defined by fMaxCos
     175           0 :                 return false;
     176             :             }
     177             : 
     178             :             // get tangentB and scalar with edge
     179           0 :             const B2DVector aTangentB(rCandidate.getTangent(1.0));
     180           0 :             const double fScalarBE(aEdge.scalar(aTangentB));
     181             : 
     182           0 :             if(fTools::lessOrEqual(fScalarBE, 0.0))
     183             :             {
     184             :                 // angle between TangentB and Edge is bigger or equal 90 degrees
     185           0 :                 return false;
     186             :             }
     187             : 
     188             :             // get self-scalar for B
     189           0 :             const double fScalarB(aTangentB.scalar(aTangentB));
     190             : 
     191           0 :             if(fTools::moreOrEqual(fScalarB, fLengthCompareE))
     192             :             {
     193             :                 // length of TangentB is more than fMaxPartOfEdge of length of edge
     194           0 :                 return false;
     195             :             }
     196             : 
     197           0 :             if(fTools::lessOrEqual(fScalarBE * fScalarBE, fScalarB * fScalarE * fMaxCosQuad))
     198             :             {
     199             :                 // angle between TangentB and Edge is bigger or equal defined by fMaxCos
     200           0 :                 return false;
     201             :             }
     202             : 
     203           0 :             return true;
     204             :         }
     205             : 
     206           0 :         void impSubdivideToSimple(const B2DCubicBezier& rCandidate, B2DPolygon& rTarget, double fMaxCosQuad, double fMaxPartOfEdgeQuad, sal_uInt32 nMaxRecursionDepth)
     207             :         {
     208           0 :             if(!nMaxRecursionDepth || impIsSimpleEdge(rCandidate, fMaxCosQuad, fMaxPartOfEdgeQuad))
     209             :             {
     210           0 :                 rTarget.appendBezierSegment(rCandidate.getControlPointA(), rCandidate.getControlPointB(), rCandidate.getEndPoint());
     211             :             }
     212             :             else
     213             :             {
     214           0 :                 B2DCubicBezier aLeft, aRight;
     215           0 :                 rCandidate.split(0.5, &aLeft, &aRight);
     216             : 
     217           0 :                 impSubdivideToSimple(aLeft, rTarget, fMaxCosQuad, fMaxPartOfEdgeQuad, nMaxRecursionDepth - 1);
     218           0 :                 impSubdivideToSimple(aRight, rTarget, fMaxCosQuad, fMaxPartOfEdgeQuad, nMaxRecursionDepth - 1);
     219             :             }
     220           0 :         }
     221             : 
     222         100 :         B2DPolygon subdivideToSimple(const B2DPolygon& rCandidate, double fMaxCosQuad, double fMaxPartOfEdgeQuad)
     223             :         {
     224         100 :             const sal_uInt32 nPointCount(rCandidate.count());
     225             : 
     226         100 :             if(rCandidate.areControlPointsUsed() && nPointCount)
     227             :             {
     228           0 :                 const sal_uInt32 nEdgeCount(rCandidate.isClosed() ? nPointCount : nPointCount - 1);
     229           0 :                 B2DPolygon aRetval;
     230           0 :                 B2DCubicBezier aEdge;
     231             : 
     232             :                 // prepare edge for loop
     233           0 :                 aEdge.setStartPoint(rCandidate.getB2DPoint(0));
     234           0 :                 aRetval.append(aEdge.getStartPoint());
     235             : 
     236           0 :                 for(sal_uInt32 a(0); a < nEdgeCount; a++)
     237             :                 {
     238             :                     // fill B2DCubicBezier
     239           0 :                     const sal_uInt32 nNextIndex((a + 1) % nPointCount);
     240           0 :                     aEdge.setControlPointA(rCandidate.getNextControlPoint(a));
     241           0 :                     aEdge.setControlPointB(rCandidate.getPrevControlPoint(nNextIndex));
     242           0 :                     aEdge.setEndPoint(rCandidate.getB2DPoint(nNextIndex));
     243             : 
     244             :                     // get rid of unnecessary bezier segments
     245           0 :                     aEdge.testAndSolveTrivialBezier();
     246             : 
     247           0 :                     if(aEdge.isBezier())
     248             :                     {
     249             :                         // before splitting recursively with internal simple criteria, use
     250             :                         // ExtremumPosFinder to remove those
     251           0 :                         ::std::vector< double > aExtremumPositions;
     252             : 
     253           0 :                         aExtremumPositions.reserve(4);
     254           0 :                         aEdge.getAllExtremumPositions(aExtremumPositions);
     255             : 
     256           0 :                         const sal_uInt32 nCount(aExtremumPositions.size());
     257             : 
     258           0 :                         if(nCount)
     259             :                         {
     260           0 :                             if(nCount > 1)
     261             :                             {
     262             :                                 // create order from left to right
     263           0 :                                 ::std::sort(aExtremumPositions.begin(), aExtremumPositions.end());
     264             :                             }
     265             : 
     266           0 :                             for(sal_uInt32 b(0); b < nCount;)
     267             :                             {
     268             :                                 // split aEdge at next split pos
     269           0 :                                 B2DCubicBezier aLeft;
     270           0 :                                 const double fSplitPos(aExtremumPositions[b++]);
     271             : 
     272           0 :                                 aEdge.split(fSplitPos, &aLeft, &aEdge);
     273           0 :                                 aLeft.testAndSolveTrivialBezier();
     274             : 
     275             :                                 // consume left part
     276           0 :                                 if(aLeft.isBezier())
     277             :                                 {
     278           0 :                                     impSubdivideToSimple(aLeft, aRetval, fMaxCosQuad, fMaxPartOfEdgeQuad, 6);
     279             :                                 }
     280             :                                 else
     281             :                                 {
     282           0 :                                     aRetval.append(aLeft.getEndPoint());
     283             :                                 }
     284             : 
     285           0 :                                 if(b < nCount)
     286             :                                 {
     287             :                                     // correct the remaining split positions to fit to shortened aEdge
     288           0 :                                     const double fScaleFactor(1.0 / (1.0 - fSplitPos));
     289             : 
     290           0 :                                     for(sal_uInt32 c(b); c < nCount; c++)
     291             :                                     {
     292           0 :                                         aExtremumPositions[c] = (aExtremumPositions[c] - fSplitPos) * fScaleFactor;
     293             :                                     }
     294             :                                 }
     295           0 :                             }
     296             : 
     297             :                             // test the shortened rest of aEdge
     298           0 :                             aEdge.testAndSolveTrivialBezier();
     299             : 
     300             :                             // consume right part
     301           0 :                             if(aEdge.isBezier())
     302             :                             {
     303           0 :                                 impSubdivideToSimple(aEdge, aRetval, fMaxCosQuad, fMaxPartOfEdgeQuad, 6);
     304             :                             }
     305             :                             else
     306             :                             {
     307           0 :                                 aRetval.append(aEdge.getEndPoint());
     308             :                             }
     309             :                         }
     310             :                         else
     311             :                         {
     312           0 :                             impSubdivideToSimple(aEdge, aRetval, fMaxCosQuad, fMaxPartOfEdgeQuad, 6);
     313           0 :                         }
     314             :                     }
     315             :                     else
     316             :                     {
     317             :                         // straight edge, add point
     318           0 :                         aRetval.append(aEdge.getEndPoint());
     319             :                     }
     320             : 
     321             :                     // prepare edge for next step
     322           0 :                     aEdge.setStartPoint(aEdge.getEndPoint());
     323             :                 }
     324             : 
     325             :                 // copy closed flag and check for double points
     326           0 :                 aRetval.setClosed(rCandidate.isClosed());
     327           0 :                 aRetval.removeDoublePoints();
     328             : 
     329           0 :                 return aRetval;
     330             :             }
     331             :             else
     332             :             {
     333         100 :                 return rCandidate;
     334             :             }
     335             :         }
     336             : 
     337         100 :         B2DPolygon createAreaGeometryForEdge(
     338             :             const B2DCubicBezier& rEdge,
     339             :             double fHalfLineWidth,
     340             :             bool bStartRound,
     341             :             bool bEndRound,
     342             :             bool bStartSquare,
     343             :             bool bEndSquare)
     344             :         {
     345             :             // create polygon for edge
     346             :             // Unfortunately, while it would be geometrically correct to not add
     347             :             // the in-between points EdgeEnd and EdgeStart, it leads to rounding
     348             :             // errors when converting to integer polygon coordinates for painting
     349         100 :             if(rEdge.isBezier())
     350             :             {
     351             :                 // prepare target and data common for upper and lower
     352           0 :                 B2DPolygon aBezierPolygon;
     353           0 :                 const B2DVector aPureEdgeVector(rEdge.getEndPoint() - rEdge.getStartPoint());
     354           0 :                 const double fEdgeLength(aPureEdgeVector.getLength());
     355           0 :                 const bool bIsEdgeLengthZero(fTools::equalZero(fEdgeLength));
     356           0 :                 B2DVector aTangentA(rEdge.getTangent(0.0)); aTangentA.normalize();
     357           0 :                 B2DVector aTangentB(rEdge.getTangent(1.0)); aTangentB.normalize();
     358           0 :                 const B2DVector aNormalizedPerpendicularA(getPerpendicular(aTangentA));
     359           0 :                 const B2DVector aNormalizedPerpendicularB(getPerpendicular(aTangentB));
     360             : 
     361             :                 // create upper displacement vectors and check if they cut
     362           0 :                 const B2DVector aPerpendStartA(aNormalizedPerpendicularA * -fHalfLineWidth);
     363           0 :                 const B2DVector aPerpendEndA(aNormalizedPerpendicularB * -fHalfLineWidth);
     364           0 :                 double fCutA(0.0);
     365             :                 const tools::CutFlagValue aCutA(tools::findCut(
     366             :                     rEdge.getStartPoint(), aPerpendStartA,
     367             :                     rEdge.getEndPoint(), aPerpendEndA,
     368           0 :                     CUTFLAG_ALL, &fCutA));
     369           0 :                 const bool bCutA(CUTFLAG_NONE != aCutA);
     370             : 
     371             :                 // create lower displacement vectors and check if they cut
     372           0 :                 const B2DVector aPerpendStartB(aNormalizedPerpendicularA * fHalfLineWidth);
     373           0 :                 const B2DVector aPerpendEndB(aNormalizedPerpendicularB * fHalfLineWidth);
     374           0 :                 double fCutB(0.0);
     375             :                 const tools::CutFlagValue aCutB(tools::findCut(
     376             :                     rEdge.getEndPoint(), aPerpendEndB,
     377             :                     rEdge.getStartPoint(), aPerpendStartB,
     378           0 :                     CUTFLAG_ALL, &fCutB));
     379           0 :                 const bool bCutB(CUTFLAG_NONE != aCutB);
     380             : 
     381             :                 // check if cut happens
     382           0 :                 const bool bCut(bCutA || bCutB);
     383           0 :                 B2DPoint aCutPoint;
     384             : 
     385             :                 // create left edge
     386           0 :                 if(bStartRound || bStartSquare)
     387             :                 {
     388           0 :                     if(bStartRound)
     389             :                     {
     390           0 :                         basegfx::B2DPolygon aStartPolygon(tools::createHalfUnitCircle());
     391             : 
     392             :                         aStartPolygon.transform(
     393             :                             tools::createScaleShearXRotateTranslateB2DHomMatrix(
     394             :                                 fHalfLineWidth, fHalfLineWidth,
     395             :                                 0.0,
     396           0 :                                 atan2(aTangentA.getY(), aTangentA.getX()) + F_PI2,
     397           0 :                                 rEdge.getStartPoint().getX(), rEdge.getStartPoint().getY()));
     398           0 :                         aBezierPolygon.append(aStartPolygon);
     399             :                     }
     400             :                     else // bStartSquare
     401             :                     {
     402           0 :                         const basegfx::B2DPoint aStart(rEdge.getStartPoint() - (aTangentA * fHalfLineWidth));
     403             : 
     404           0 :                         if(bCutB)
     405             :                         {
     406           0 :                             aBezierPolygon.append(rEdge.getStartPoint() + aPerpendStartB);
     407             :                         }
     408             : 
     409           0 :                         aBezierPolygon.append(aStart + aPerpendStartB);
     410           0 :                         aBezierPolygon.append(aStart + aPerpendStartA);
     411             : 
     412           0 :                         if(bCutA)
     413             :                         {
     414           0 :                             aBezierPolygon.append(rEdge.getStartPoint() + aPerpendStartA);
     415           0 :                         }
     416           0 :                     }
     417             :                 }
     418             :                 else
     419             :                 {
     420             :                     // append original in-between point
     421           0 :                     aBezierPolygon.append(rEdge.getStartPoint());
     422             :                 }
     423             : 
     424             :                 // create upper edge.
     425             :                 {
     426           0 :                     if(bCutA)
     427             :                     {
     428             :                         // calculate cut point and add
     429           0 :                         aCutPoint = rEdge.getStartPoint() + (aPerpendStartA * fCutA);
     430           0 :                         aBezierPolygon.append(aCutPoint);
     431             :                     }
     432             :                     else
     433             :                     {
     434             :                         // create scaled bezier segment
     435           0 :                         const B2DPoint aStart(rEdge.getStartPoint() + aPerpendStartA);
     436           0 :                         const B2DPoint aEnd(rEdge.getEndPoint() + aPerpendEndA);
     437           0 :                         const B2DVector aEdge(aEnd - aStart);
     438           0 :                         const double fLength(aEdge.getLength());
     439           0 :                         const double fScale(bIsEdgeLengthZero ? 1.0 : fLength / fEdgeLength);
     440           0 :                         const B2DVector fRelNext(rEdge.getControlPointA() - rEdge.getStartPoint());
     441           0 :                         const B2DVector fRelPrev(rEdge.getControlPointB() - rEdge.getEndPoint());
     442             : 
     443           0 :                         aBezierPolygon.append(aStart);
     444           0 :                         aBezierPolygon.appendBezierSegment(aStart + (fRelNext * fScale), aEnd + (fRelPrev * fScale), aEnd);
     445             :                     }
     446             :                 }
     447             : 
     448             :                 // create right edge
     449           0 :                 if(bEndRound || bEndSquare)
     450             :                 {
     451           0 :                     if(bEndRound)
     452             :                     {
     453           0 :                         basegfx::B2DPolygon aEndPolygon(tools::createHalfUnitCircle());
     454             : 
     455             :                         aEndPolygon.transform(
     456             :                             tools::createScaleShearXRotateTranslateB2DHomMatrix(
     457             :                                 fHalfLineWidth, fHalfLineWidth,
     458             :                                 0.0,
     459           0 :                                 atan2(aTangentB.getY(), aTangentB.getX()) - F_PI2,
     460           0 :                                 rEdge.getEndPoint().getX(), rEdge.getEndPoint().getY()));
     461           0 :                         aBezierPolygon.append(aEndPolygon);
     462             :                     }
     463             :                     else // bEndSquare
     464             :                     {
     465           0 :                         const basegfx::B2DPoint aEnd(rEdge.getEndPoint() + (aTangentB * fHalfLineWidth));
     466             : 
     467           0 :                         if(bCutA)
     468             :                         {
     469           0 :                             aBezierPolygon.append(rEdge.getEndPoint() + aPerpendEndA);
     470             :                         }
     471             : 
     472           0 :                         aBezierPolygon.append(aEnd + aPerpendEndA);
     473           0 :                         aBezierPolygon.append(aEnd + aPerpendEndB);
     474             : 
     475           0 :                         if(bCutB)
     476             :                         {
     477           0 :                             aBezierPolygon.append(rEdge.getEndPoint() + aPerpendEndB);
     478           0 :                         }
     479           0 :                     }
     480             :                 }
     481             :                 else
     482             :                 {
     483             :                     // append original in-between point
     484           0 :                     aBezierPolygon.append(rEdge.getEndPoint());
     485             :                 }
     486             : 
     487             :                 // create lower edge.
     488             :                 {
     489           0 :                     if(bCutB)
     490             :                     {
     491             :                         // calculate cut point and add
     492           0 :                         aCutPoint = rEdge.getEndPoint() + (aPerpendEndB * fCutB);
     493           0 :                         aBezierPolygon.append(aCutPoint);
     494             :                     }
     495             :                     else
     496             :                     {
     497             :                         // create scaled bezier segment
     498           0 :                         const B2DPoint aStart(rEdge.getEndPoint() + aPerpendEndB);
     499           0 :                         const B2DPoint aEnd(rEdge.getStartPoint() + aPerpendStartB);
     500           0 :                         const B2DVector aEdge(aEnd - aStart);
     501           0 :                         const double fLength(aEdge.getLength());
     502           0 :                         const double fScale(bIsEdgeLengthZero ? 1.0 : fLength / fEdgeLength);
     503           0 :                         const B2DVector fRelNext(rEdge.getControlPointB() - rEdge.getEndPoint());
     504           0 :                         const B2DVector fRelPrev(rEdge.getControlPointA() - rEdge.getStartPoint());
     505             : 
     506           0 :                         aBezierPolygon.append(aStart);
     507           0 :                         aBezierPolygon.appendBezierSegment(aStart + (fRelNext * fScale), aEnd + (fRelPrev * fScale), aEnd);
     508             :                     }
     509             :                 }
     510             : 
     511             :                 // close
     512           0 :                 aBezierPolygon.setClosed(true);
     513             : 
     514           0 :                 if(bStartRound || bEndRound)
     515             :                 {
     516             :                     // double points possible when round caps are used at start or end
     517           0 :                     aBezierPolygon.removeDoublePoints();
     518             :                 }
     519             : 
     520           0 :                 if(bCut && ((bStartRound || bStartSquare) && (bEndRound || bEndSquare)))
     521             :                 {
     522             :                     // When cut exists and both ends are extended with caps, a self-intersecting polygon
     523             :                     // is created; one cut point is known, but there is a 2nd one in the caps geometry.
     524             :                     // Solve by using tooling.
     525             :                     // Remark: This nearly never happens due to curve preparations to extreme points
     526             :                     // and maximum angle turning, but I constructed a test case and checkd that it is
     527             :                     // working propery.
     528           0 :                     const B2DPolyPolygon aTemp(tools::solveCrossovers(aBezierPolygon));
     529           0 :                     const sal_uInt32 nTempCount(aTemp.count());
     530             : 
     531           0 :                     if(nTempCount)
     532             :                     {
     533           0 :                         if(nTempCount > 1)
     534             :                         {
     535             :                             // as expected, multiple polygons (with same orientation). Remove
     536             :                             // the one which contains aCutPoint, or better take the one without
     537           0 :                             for (sal_uInt32 a(0); a < nTempCount; a++)
     538             :                             {
     539           0 :                                 aBezierPolygon = aTemp.getB2DPolygon(a);
     540             : 
     541           0 :                                 const sal_uInt32 nCandCount(aBezierPolygon.count());
     542             : 
     543           0 :                                 for(sal_uInt32 b(0); b < nCandCount; b++)
     544             :                                 {
     545           0 :                                     if(aCutPoint.equal(aBezierPolygon.getB2DPoint(b)))
     546             :                                     {
     547           0 :                                         aBezierPolygon.clear();
     548           0 :                                         break;
     549             :                                     }
     550             :                                 }
     551             : 
     552           0 :                                 if(aBezierPolygon.count())
     553             :                                 {
     554           0 :                                     break;
     555             :                                 }
     556             :                             }
     557             : 
     558             :                             OSL_ENSURE(aBezierPolygon.count(), "Error in line geometry creation, could not solve self-intersection (!)");
     559             :                         }
     560             :                         else
     561             :                         {
     562             :                             // none found, use result
     563           0 :                             aBezierPolygon = aTemp.getB2DPolygon(0);
     564             :                         }
     565             :                     }
     566             :                     else
     567             :                     {
     568             :                         OSL_ENSURE(false, "Error in line geometry creation, could not solve self-intersection (!)");
     569           0 :                     }
     570             :                 }
     571             : 
     572             :                 // return
     573           0 :                 return aBezierPolygon;
     574             :             }
     575             :             else
     576             :             {
     577             :                 // Get start and  end point, create tangent and set to needed length
     578         100 :                 B2DVector aTangent(rEdge.getEndPoint() - rEdge.getStartPoint());
     579         100 :                 aTangent.setLength(fHalfLineWidth);
     580             : 
     581             :                 // prepare return value
     582         100 :                 B2DPolygon aEdgePolygon;
     583             : 
     584             :                 // buffered angle
     585         100 :                 double fAngle(0.0);
     586         100 :                 bool bAngle(false);
     587             : 
     588             :                 // buffered perpendicular
     589         100 :                 B2DVector aPerpend;
     590         100 :                 bool bPerpend(false);
     591             : 
     592             :                 // create left vertical
     593         100 :                 if(bStartRound)
     594             :                 {
     595           0 :                     aEdgePolygon = tools::createHalfUnitCircle();
     596           0 :                     fAngle = atan2(aTangent.getY(), aTangent.getX());
     597           0 :                     bAngle = true;
     598             :                     aEdgePolygon.transform(
     599             :                         tools::createScaleShearXRotateTranslateB2DHomMatrix(
     600             :                             fHalfLineWidth, fHalfLineWidth,
     601             :                             0.0,
     602             :                             fAngle + F_PI2,
     603           0 :                             rEdge.getStartPoint().getX(), rEdge.getStartPoint().getY()));
     604             :                 }
     605             :                 else
     606             :                 {
     607         100 :                     aPerpend.setX(-aTangent.getY());
     608         100 :                     aPerpend.setY(aTangent.getX());
     609         100 :                     bPerpend = true;
     610             : 
     611         100 :                     if(bStartSquare)
     612             :                     {
     613          77 :                         const basegfx::B2DPoint aStart(rEdge.getStartPoint() - aTangent);
     614             : 
     615          77 :                         aEdgePolygon.append(aStart + aPerpend);
     616          77 :                         aEdgePolygon.append(aStart - aPerpend);
     617             :                     }
     618             :                     else
     619             :                     {
     620          23 :                         aEdgePolygon.append(rEdge.getStartPoint() + aPerpend);
     621          23 :                         aEdgePolygon.append(rEdge.getStartPoint()); // keep the in-between point for numerical reasons
     622          23 :                         aEdgePolygon.append(rEdge.getStartPoint() - aPerpend);
     623             :                     }
     624             :                 }
     625             : 
     626             :                 // create right vertical
     627         100 :                 if(bEndRound)
     628             :                 {
     629           0 :                     basegfx::B2DPolygon aEndPolygon(tools::createHalfUnitCircle());
     630             : 
     631           0 :                     if(!bAngle)
     632             :                     {
     633           0 :                         fAngle = atan2(aTangent.getY(), aTangent.getX());
     634             :                     }
     635             : 
     636             :                     aEndPolygon.transform(
     637             :                         tools::createScaleShearXRotateTranslateB2DHomMatrix(
     638             :                             fHalfLineWidth, fHalfLineWidth,
     639             :                             0.0,
     640             :                             fAngle - F_PI2,
     641           0 :                             rEdge.getEndPoint().getX(), rEdge.getEndPoint().getY()));
     642           0 :                     aEdgePolygon.append(aEndPolygon);
     643             :                 }
     644             :                 else
     645             :                 {
     646         100 :                     if(!bPerpend)
     647             :                     {
     648           0 :                         aPerpend.setX(-aTangent.getY());
     649           0 :                         aPerpend.setY(aTangent.getX());
     650             :                     }
     651             : 
     652         100 :                     if(bEndSquare)
     653             :                     {
     654          77 :                         const basegfx::B2DPoint aEnd(rEdge.getEndPoint() + aTangent);
     655             : 
     656          77 :                         aEdgePolygon.append(aEnd - aPerpend);
     657          77 :                         aEdgePolygon.append(aEnd + aPerpend);
     658             :                     }
     659             :                     else
     660             :                     {
     661          23 :                         aEdgePolygon.append(rEdge.getEndPoint() - aPerpend);
     662          23 :                         aEdgePolygon.append(rEdge.getEndPoint()); // keep the in-between point for numerical reasons
     663          23 :                         aEdgePolygon.append(rEdge.getEndPoint() + aPerpend);
     664             :                     }
     665             :                 }
     666             : 
     667             :                 // close and return
     668         100 :                 aEdgePolygon.setClosed(true);
     669             : 
     670         100 :                 return aEdgePolygon;
     671             :             }
     672             :         }
     673             : 
     674           0 :         B2DPolygon createAreaGeometryForJoin(
     675             :             const B2DVector& rTangentPrev,
     676             :             const B2DVector& rTangentEdge,
     677             :             const B2DVector& rPerpendPrev,
     678             :             const B2DVector& rPerpendEdge,
     679             :             const B2DPoint& rPoint,
     680             :             double fHalfLineWidth,
     681             :             B2DLineJoin eJoin,
     682             :             double fMiterMinimumAngle)
     683             :         {
     684             :             OSL_ENSURE(fHalfLineWidth > 0.0, "createAreaGeometryForJoin: LineWidth too small (!)");
     685             :             OSL_ENSURE(B2DLINEJOIN_NONE != eJoin, "createAreaGeometryForJoin: B2DLINEJOIN_NONE not allowed (!)");
     686             : 
     687             :             // LineJoin from tangent rPerpendPrev to tangent rPerpendEdge in rPoint
     688           0 :             B2DPolygon aEdgePolygon;
     689           0 :             const B2DPoint aStartPoint(rPoint + rPerpendPrev);
     690           0 :             const B2DPoint aEndPoint(rPoint + rPerpendEdge);
     691             : 
     692             :             // test if for Miter, the angle is too small and the fallback
     693             :             // to bevel needs to be used
     694           0 :             if(B2DLINEJOIN_MITER == eJoin)
     695             :             {
     696           0 :                 const double fAngle(fabs(rPerpendPrev.angle(rPerpendEdge)));
     697             : 
     698           0 :                 if((F_PI - fAngle) < fMiterMinimumAngle)
     699             :                 {
     700             :                     // fallback to bevel
     701           0 :                     eJoin = B2DLINEJOIN_BEVEL;
     702             :                 }
     703             :             }
     704             : 
     705           0 :             switch(eJoin)
     706             :             {
     707             :                 case B2DLINEJOIN_MITER :
     708             :                 {
     709           0 :                     aEdgePolygon.append(aEndPoint);
     710           0 :                     aEdgePolygon.append(rPoint);
     711           0 :                     aEdgePolygon.append(aStartPoint);
     712             : 
     713             :                     // Look for the cut point between start point along rTangentPrev and
     714             :                     // end point along rTangentEdge. -rTangentEdge should be used, but since
     715             :                     // the cut value is used for interpolating along the first edge, the negation
     716             :                     // is not needed since the same fCut will be found on the first edge.
     717             :                     // If it exists, insert it to complete the mitered fill polygon.
     718           0 :                     double fCutPos(0.0);
     719           0 :                     tools::findCut(aStartPoint, rTangentPrev, aEndPoint, rTangentEdge, CUTFLAG_ALL, &fCutPos);
     720             : 
     721           0 :                     if(0.0 != fCutPos)
     722             :                     {
     723           0 :                         const B2DPoint aCutPoint(interpolate(aStartPoint, aStartPoint + rTangentPrev, fCutPos));
     724           0 :                         aEdgePolygon.append(aCutPoint);
     725             :                     }
     726             : 
     727             :                     break;
     728             :                 }
     729             :                 case B2DLINEJOIN_ROUND :
     730             :                 {
     731             :                     // use tooling to add needed EllipseSegment
     732           0 :                     double fAngleStart(atan2(rPerpendPrev.getY(), rPerpendPrev.getX()));
     733           0 :                     double fAngleEnd(atan2(rPerpendEdge.getY(), rPerpendEdge.getX()));
     734             : 
     735             :                     // atan2 results are [-PI .. PI], consolidate to [0.0 .. 2PI]
     736           0 :                     if(fAngleStart < 0.0)
     737             :                     {
     738           0 :                         fAngleStart += F_2PI;
     739             :                     }
     740             : 
     741           0 :                     if(fAngleEnd < 0.0)
     742             :                     {
     743           0 :                         fAngleEnd += F_2PI;
     744             :                     }
     745             : 
     746           0 :                     const B2DPolygon aBow(tools::createPolygonFromEllipseSegment(rPoint, fHalfLineWidth, fHalfLineWidth, fAngleStart, fAngleEnd));
     747             : 
     748           0 :                     if(aBow.count() > 1)
     749             :                     {
     750             :                         // #i101491#
     751             :                         // use the original start/end positions; the ones from bow creation may be numerically
     752             :                         // different due to their different creation. To guarantee good merging quality with edges
     753             :                         // and edge roundings (and to reduce point count)
     754           0 :                         aEdgePolygon = aBow;
     755           0 :                         aEdgePolygon.setB2DPoint(0, aStartPoint);
     756           0 :                         aEdgePolygon.setB2DPoint(aEdgePolygon.count() - 1, aEndPoint);
     757           0 :                         aEdgePolygon.append(rPoint);
     758             : 
     759             :                         break;
     760             :                     }
     761             :                     else
     762             :                     {
     763             :                         // wanted fall-through to default
     764           0 :                     }
     765             :                 }
     766             :                 default: // B2DLINEJOIN_BEVEL
     767             :                 {
     768           0 :                     aEdgePolygon.append(aEndPoint);
     769           0 :                     aEdgePolygon.append(rPoint);
     770           0 :                     aEdgePolygon.append(aStartPoint);
     771             : 
     772           0 :                     break;
     773             :                 }
     774             :             }
     775             : 
     776             :             // create last polygon part for edge
     777           0 :             aEdgePolygon.setClosed(true);
     778             : 
     779           0 :             return aEdgePolygon;
     780             :         }
     781             :     } // end of anonymus namespace
     782             : 
     783             :     namespace tools
     784             :     {
     785         100 :         B2DPolyPolygon createAreaGeometry(
     786             :             const B2DPolygon& rCandidate,
     787             :             double fHalfLineWidth,
     788             :             B2DLineJoin eJoin,
     789             :             com::sun::star::drawing::LineCap eCap,
     790             :             double fMaxAllowedAngle,
     791             :             double fMaxPartOfEdge,
     792             :             double fMiterMinimumAngle)
     793             :         {
     794         100 :             if(fMaxAllowedAngle > F_PI2)
     795             :             {
     796           0 :                 fMaxAllowedAngle = F_PI2;
     797             :             }
     798         100 :             else if(fMaxAllowedAngle < 0.01 * F_PI2)
     799             :             {
     800           0 :                 fMaxAllowedAngle = 0.01 * F_PI2;
     801             :             }
     802             : 
     803         100 :             if(fMaxPartOfEdge > 1.0)
     804             :             {
     805           0 :                 fMaxPartOfEdge = 1.0;
     806             :             }
     807         100 :             else if(fMaxPartOfEdge < 0.01)
     808             :             {
     809           0 :                 fMaxPartOfEdge = 0.01;
     810             :             }
     811             : 
     812         100 :             if(fMiterMinimumAngle > F_PI)
     813             :             {
     814           0 :                 fMiterMinimumAngle = F_PI;
     815             :             }
     816         100 :             else if(fMiterMinimumAngle < 0.01 * F_PI)
     817             :             {
     818           0 :                 fMiterMinimumAngle = 0.01 * F_PI;
     819             :             }
     820             : 
     821         100 :             B2DPolygon aCandidate(rCandidate);
     822         100 :             const double fMaxCos(cos(fMaxAllowedAngle));
     823             : 
     824         100 :             aCandidate.removeDoublePoints();
     825         100 :             aCandidate = subdivideToSimple(aCandidate, fMaxCos * fMaxCos, fMaxPartOfEdge * fMaxPartOfEdge);
     826             : 
     827         100 :             const sal_uInt32 nPointCount(aCandidate.count());
     828             : 
     829         100 :             if(nPointCount)
     830             :             {
     831         100 :                 B2DPolyPolygon aRetval;
     832         100 :                 const bool bIsClosed(aCandidate.isClosed());
     833         100 :                 const sal_uInt32 nEdgeCount(bIsClosed ? nPointCount : nPointCount - 1);
     834         100 :                 const bool bLineCap(!bIsClosed && com::sun::star::drawing::LineCap_BUTT != eCap);
     835             : 
     836         100 :                 if(nEdgeCount)
     837             :                 {
     838         100 :                     B2DCubicBezier aEdge;
     839         100 :                     B2DCubicBezier aPrev;
     840             : 
     841         100 :                     const bool bEventuallyCreateLineJoin(B2DLINEJOIN_NONE != eJoin);
     842             :                     // prepare edge
     843         100 :                     aEdge.setStartPoint(aCandidate.getB2DPoint(0));
     844             : 
     845         100 :                     if(bIsClosed && bEventuallyCreateLineJoin)
     846             :                     {
     847             :                         // prepare previous edge
     848           3 :                         const sal_uInt32 nPrevIndex(nPointCount - 1);
     849           3 :                         aPrev.setStartPoint(aCandidate.getB2DPoint(nPrevIndex));
     850           3 :                         aPrev.setControlPointA(aCandidate.getNextControlPoint(nPrevIndex));
     851           3 :                         aPrev.setControlPointB(aCandidate.getPrevControlPoint(0));
     852           3 :                         aPrev.setEndPoint(aEdge.getStartPoint());
     853             :                     }
     854             : 
     855         200 :                     for(sal_uInt32 a(0); a < nEdgeCount; a++)
     856             :                     {
     857             :                         // fill current Edge
     858         100 :                         const sal_uInt32 nNextIndex((a + 1) % nPointCount);
     859         100 :                         aEdge.setControlPointA(aCandidate.getNextControlPoint(a));
     860         100 :                         aEdge.setControlPointB(aCandidate.getPrevControlPoint(nNextIndex));
     861         100 :                         aEdge.setEndPoint(aCandidate.getB2DPoint(nNextIndex));
     862             : 
     863             :                         // check and create linejoin
     864         100 :                         if(bEventuallyCreateLineJoin && (bIsClosed || 0 != a))
     865             :                         {
     866           3 :                             B2DVector aTangentPrev(aPrev.getTangent(1.0)); aTangentPrev.normalize();
     867           3 :                             B2DVector aTangentEdge(aEdge.getTangent(0.0)); aTangentEdge.normalize();
     868           3 :                             B2VectorOrientation aOrientation(getOrientation(aTangentPrev, aTangentEdge));
     869             : 
     870           3 :                             if(ORIENTATION_NEUTRAL == aOrientation)
     871             :                             {
     872             :                                    // they are parallell or empty; if they are both not zero and point
     873             :                                    // in opposite direction, a half-circle is needed
     874           3 :                                    if(!aTangentPrev.equalZero() && !aTangentEdge.equalZero())
     875             :                                    {
     876           0 :                                     const double fAngle(fabs(aTangentPrev.angle(aTangentEdge)));
     877             : 
     878           0 :                                     if(fTools::equal(fAngle, F_PI))
     879             :                                     {
     880             :                                         // for half-circle production, fallback to positive
     881             :                                         // orientation
     882           0 :                                         aOrientation = ORIENTATION_POSITIVE;
     883             :                                     }
     884             :                                 }
     885             :                             }
     886             : 
     887           3 :                             if(ORIENTATION_POSITIVE == aOrientation)
     888             :                             {
     889           0 :                                 const B2DVector aPerpendPrev(getPerpendicular(aTangentPrev) * -fHalfLineWidth);
     890           0 :                                 const B2DVector aPerpendEdge(getPerpendicular(aTangentEdge) * -fHalfLineWidth);
     891             : 
     892             :                                 aRetval.append(
     893             :                                     createAreaGeometryForJoin(
     894             :                                         aTangentPrev,
     895             :                                         aTangentEdge,
     896             :                                         aPerpendPrev,
     897             :                                         aPerpendEdge,
     898             :                                         aEdge.getStartPoint(),
     899             :                                         fHalfLineWidth,
     900             :                                         eJoin,
     901           0 :                                         fMiterMinimumAngle));
     902             :                             }
     903           3 :                             else if(ORIENTATION_NEGATIVE == aOrientation)
     904             :                             {
     905           0 :                                 const B2DVector aPerpendPrev(getPerpendicular(aTangentPrev) * fHalfLineWidth);
     906           0 :                                 const B2DVector aPerpendEdge(getPerpendicular(aTangentEdge) * fHalfLineWidth);
     907             : 
     908             :                                 aRetval.append(
     909             :                                     createAreaGeometryForJoin(
     910             :                                         aTangentEdge,
     911             :                                         aTangentPrev,
     912             :                                         aPerpendEdge,
     913             :                                         aPerpendPrev,
     914             :                                         aEdge.getStartPoint(),
     915             :                                         fHalfLineWidth,
     916             :                                         eJoin,
     917           0 :                                         fMiterMinimumAngle));
     918           3 :                             }
     919             :                         }
     920             : 
     921             :                         // create geometry for edge
     922         100 :                         const bool bLast(a + 1 == nEdgeCount);
     923             : 
     924         100 :                         if(bLineCap)
     925             :                         {
     926          77 :                             const bool bFirst(!a);
     927             : 
     928             :                             aRetval.append(
     929             :                                 createAreaGeometryForEdge(
     930             :                                     aEdge,
     931             :                                     fHalfLineWidth,
     932             :                                     bFirst && com::sun::star::drawing::LineCap_ROUND == eCap,
     933             :                                     bLast && com::sun::star::drawing::LineCap_ROUND == eCap,
     934             :                                     bFirst && com::sun::star::drawing::LineCap_SQUARE == eCap,
     935          77 :                                     bLast && com::sun::star::drawing::LineCap_SQUARE == eCap));
     936             :                         }
     937             :                         else
     938             :                         {
     939             :                             aRetval.append(
     940             :                                 createAreaGeometryForEdge(
     941             :                                     aEdge,
     942             :                                     fHalfLineWidth,
     943             :                                     false,
     944             :                                     false,
     945             :                                     false,
     946          23 :                                     false));
     947             :                         }
     948             : 
     949             :                         // prepare next step
     950         100 :                         if(!bLast)
     951             :                         {
     952           0 :                             if(bEventuallyCreateLineJoin)
     953             :                             {
     954           0 :                                 aPrev = aEdge;
     955             :                             }
     956             : 
     957           0 :                             aEdge.setStartPoint(aEdge.getEndPoint());
     958             :                         }
     959         100 :                     }
     960             :                 }
     961             : 
     962         100 :                 return aRetval;
     963             :             }
     964             :             else
     965             :             {
     966           0 :                 return B2DPolyPolygon(rCandidate);
     967         100 :             }
     968             :         }
     969             :     } // end of namespace tools
     970             : } // end of namespace basegfx
     971             : 
     972             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10