LCOV - code coverage report
Current view: top level - basegfx/source/tools - unotools.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 102 106 96.2 %
Date: 2014-04-11 Functions: 2 2 100.0 %
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 <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
      21             : #include <com/sun/star/drawing/PointSequence.hpp>
      22             : #include <com/sun/star/drawing/FlagSequence.hpp>
      23             : #include <basegfx/polygon/b2dpolypolygontools.hxx>
      24             : #include <basegfx/polygon/b2dpolygontools.hxx>
      25             : #include <basegfx/polygon/b2dpolygon.hxx>
      26             : #include <basegfx/curve/b2dcubicbezier.hxx>
      27             : 
      28             : #include <basegfx/tools/unotools.hxx>
      29             : #include <comphelper/sequence.hxx>
      30             : 
      31             : 
      32             : using namespace ::com::sun::star;
      33             : 
      34             : namespace basegfx
      35             : {
      36             : namespace unotools
      37             : {
      38             : 
      39         458 :     B2DPolyPolygon polyPolygonBezierToB2DPolyPolygon(const drawing::PolyPolygonBezierCoords& rSourcePolyPolygon)
      40             :         throw( lang::IllegalArgumentException )
      41             :     {
      42         458 :         const sal_Int32 nOuterSequenceCount(rSourcePolyPolygon.Coordinates.getLength());
      43         458 :         B2DPolyPolygon aNewPolyPolygon;
      44             : 
      45         458 :         if(rSourcePolyPolygon.Flags.getLength() != nOuterSequenceCount)
      46           0 :             throw lang::IllegalArgumentException();
      47             : 
      48             :         // get pointers to inner sequence
      49         458 :         const drawing::PointSequence* pInnerSequence = rSourcePolyPolygon.Coordinates.getConstArray();
      50         458 :         const drawing::FlagSequence* pInnerSequenceFlags = rSourcePolyPolygon.Flags.getConstArray();
      51             : 
      52        1133 :         for(sal_Int32 a(0); a < nOuterSequenceCount; a++)
      53             :         {
      54         675 :             const sal_Int32 nInnerSequenceCount(pInnerSequence->getLength());
      55             : 
      56         675 :             if(pInnerSequenceFlags->getLength() != nInnerSequenceCount)
      57           0 :                 throw lang::IllegalArgumentException();
      58             : 
      59             :             // prepare new polygon
      60         675 :             basegfx::B2DPolygon aNewPolygon;
      61         675 :             const awt::Point* pArray = pInnerSequence->getConstArray();
      62         675 :             const drawing::PolygonFlags* pArrayFlags = pInnerSequenceFlags->getConstArray();
      63             : 
      64             :             // get first point and flag
      65        1350 :             basegfx::B2DPoint aNewCoordinatePair(pArray->X, pArray->Y); pArray++;
      66         675 :             drawing::PolygonFlags ePolyFlag(*pArrayFlags); pArrayFlags++;
      67        1350 :             basegfx::B2DPoint aControlA;
      68        1350 :             basegfx::B2DPoint aControlB;
      69             : 
      70             :             // first point is not allowed to be a control point
      71         675 :             if(drawing::PolygonFlags_CONTROL == ePolyFlag)
      72           0 :                 throw lang::IllegalArgumentException();
      73             : 
      74             :             // add first point as start point
      75         675 :             aNewPolygon.append(aNewCoordinatePair);
      76        6782 :             for(sal_Int32 b(1); b < nInnerSequenceCount;)
      77             :             {
      78             :                 // prepare loop
      79        5432 :                 bool bControlA(false);
      80        5432 :                 bool bControlB(false);
      81             : 
      82             :                 // get next point and flag
      83        5432 :                 aNewCoordinatePair = basegfx::B2DPoint(pArray->X, pArray->Y);
      84        5432 :                 ePolyFlag = *pArrayFlags;
      85        5432 :                 pArray++; pArrayFlags++; b++;
      86             : 
      87        5432 :                 if(b < nInnerSequenceCount && drawing::PolygonFlags_CONTROL == ePolyFlag)
      88             :                 {
      89        1929 :                     aControlA = aNewCoordinatePair;
      90        1929 :                     bControlA = true;
      91             : 
      92             :                     // get next point and flag
      93        1929 :                     aNewCoordinatePair = basegfx::B2DPoint(pArray->X, pArray->Y);
      94        1929 :                     ePolyFlag = *pArrayFlags;
      95        1929 :                     pArray++; pArrayFlags++; b++;
      96             :                 }
      97             : 
      98        5432 :                 if(b < nInnerSequenceCount && drawing::PolygonFlags_CONTROL == ePolyFlag)
      99             :                 {
     100        1929 :                     aControlB = aNewCoordinatePair;
     101        1929 :                     bControlB = true;
     102             : 
     103             :                     // get next point and flag
     104        1929 :                     aNewCoordinatePair = basegfx::B2DPoint(pArray->X, pArray->Y);
     105        1929 :                     ePolyFlag = *pArrayFlags;
     106        1929 :                     pArray++; pArrayFlags++; b++;
     107             :                 }
     108             : 
     109             :                 // two or no control points are consumed, another one would be an error.
     110             :                 // It's also an error if only one control point was read
     111        5432 :                 if(drawing::PolygonFlags_CONTROL == ePolyFlag || bControlA != bControlB)
     112           0 :                     throw lang::IllegalArgumentException();
     113             : 
     114             :                 // the previous writes used the B2DPolyPoygon -> PolyPolygon converter
     115             :                 // which did not create minimal PolyPolygons, but created all control points
     116             :                 // as null vectors (identical points). Because of the former P(CA)(CB)-norm of
     117             :                 // B2DPolygon and it's unused sign of being the zero-vector and CA and CB being
     118             :                 // relative to P, an empty edge was exported as P == CA == CB. Luckily, the new
     119             :                 // export format can be read without errors by the old OOo-versions, so we need only
     120             :                 // to correct here at read and do not need to export a wrong but compatible version
     121             :                 // for the future.
     122       15602 :                 if(bControlA
     123        1929 :                    && aControlA.equal(aControlB)
     124       12252 :                    && aControlA.equal(aNewPolygon.getB2DPoint(aNewPolygon.count() - 1)))
     125             :                 {
     126         692 :                     bControlA = bControlB = false;
     127             :                 }
     128             : 
     129        5432 :                 if(bControlA)
     130             :                 {
     131             :                     // add bezier edge
     132        1237 :                     aNewPolygon.appendBezierSegment(aControlA, aControlB, aNewCoordinatePair);
     133             :                 }
     134             :                 else
     135             :                 {
     136             :                     // add edge
     137        4195 :                     aNewPolygon.append(aNewCoordinatePair);
     138             :                 }
     139             :             }
     140             : 
     141             :             // next sequence
     142         675 :             pInnerSequence++;
     143         675 :             pInnerSequenceFlags++;
     144             : 
     145             :             // #i72807# API import uses old line start/end-equal definition for closed,
     146             :             // so we need to correct this to closed state here
     147         675 :             basegfx::tools::checkClosed(aNewPolygon);
     148             : 
     149             :             // add new subpolygon
     150         675 :             aNewPolyPolygon.append(aNewPolygon);
     151         675 :         }
     152             : 
     153         458 :         return aNewPolyPolygon;
     154             :     }
     155             : 
     156             : 
     157             : 
     158        1942 :     void b2DPolyPolygonToPolyPolygonBezier( const basegfx::B2DPolyPolygon& rPolyPoly,
     159             :                                             drawing::PolyPolygonBezierCoords& rRetval )
     160             :     {
     161        1942 :         rRetval.Coordinates.realloc(rPolyPoly.count());
     162        1942 :         rRetval.Flags.realloc(rPolyPoly.count());
     163             : 
     164        1942 :         drawing::PointSequence* pOuterSequence = rRetval.Coordinates.getArray();
     165        1942 :         drawing::FlagSequence*  pOuterFlags = rRetval.Flags.getArray();
     166             : 
     167        2206 :         for(sal_uInt32 a=0;a<rPolyPoly.count();a++)
     168             :         {
     169         264 :             const B2DPolygon& rPoly = rPolyPoly.getB2DPolygon(a);
     170         264 :             sal_uInt32 nCount(rPoly.count());
     171         264 :             const bool bClosed(rPoly.isClosed());
     172             : 
     173             :             // calculate input vertex count
     174         264 :             const sal_uInt32 nLoopCount(bClosed ? nCount : (nCount ? nCount - 1L : 0L ));
     175             : 
     176         528 :             std::vector<awt::Point> aPoints; aPoints.reserve(nLoopCount);
     177         528 :             std::vector<drawing::PolygonFlags> aFlags; aFlags.reserve(nLoopCount);
     178             : 
     179         264 :             if( nCount )
     180             :             {
     181             :                 // prepare insert index and current point
     182         264 :                 basegfx::B2DCubicBezier aBezier;
     183         264 :                 aBezier.setStartPoint(rPoly.getB2DPoint(0));
     184             : 
     185        2056 :                 for(sal_uInt32 b(0L); b<nLoopCount; b++)
     186             :                 {
     187             :                     // add current point (always) and remember StartPointIndex for evtl. later corrections
     188        3584 :                     const awt::Point aStartPoint(fround(aBezier.getStartPoint().getX()),
     189        5376 :                                                  fround(aBezier.getStartPoint().getY()));
     190        1792 :                     const sal_uInt32 nStartPointIndex(aPoints.size());
     191        1792 :                     aPoints.push_back(aStartPoint);
     192        1792 :                     aFlags.push_back(drawing::PolygonFlags_NORMAL);
     193             : 
     194             :                     // prepare next segment
     195        1792 :                     const sal_uInt32 nNextIndex((b + 1) % nCount);
     196        1792 :                     aBezier.setEndPoint(rPoly.getB2DPoint(nNextIndex));
     197        1792 :                     aBezier.setControlPointA(rPoly.getNextControlPoint(b));
     198        1792 :                     aBezier.setControlPointB(rPoly.getPrevControlPoint(nNextIndex));
     199             : 
     200        1792 :                     if(aBezier.isBezier())
     201             :                     {
     202             :                         // if one is used, add always two control points due to the old schema
     203        1044 :                         aPoints.push_back( awt::Point(fround(aBezier.getControlPointA().getX()),
     204        1566 :                                                       fround(aBezier.getControlPointA().getY())) );
     205         522 :                         aFlags.push_back(drawing::PolygonFlags_CONTROL);
     206             : 
     207        1044 :                         aPoints.push_back( awt::Point(fround(aBezier.getControlPointB().getX()),
     208        1566 :                                                       fround(aBezier.getControlPointB().getY())) );
     209         522 :                         aFlags.push_back(drawing::PolygonFlags_CONTROL);
     210             :                     }
     211             : 
     212             :                     // test continuity with previous control point to set flag value
     213        1792 :                     if(aBezier.getControlPointA() != aBezier.getStartPoint() && (bClosed || b))
     214             :                     {
     215         512 :                         const basegfx::B2VectorContinuity eCont(rPoly.getContinuityInPoint(b));
     216             : 
     217         512 :                         if(basegfx::CONTINUITY_C1 == eCont)
     218             :                         {
     219          66 :                             aFlags[nStartPointIndex] = drawing::PolygonFlags_SMOOTH;
     220             :                         }
     221         446 :                         else if(basegfx::CONTINUITY_C2 == eCont)
     222             :                         {
     223          16 :                             aFlags[nStartPointIndex] = drawing::PolygonFlags_SYMMETRIC;
     224             :                         }
     225             :                     }
     226             : 
     227             :                     // prepare next polygon step
     228        1792 :                     aBezier.setStartPoint(aBezier.getEndPoint());
     229             :                 }
     230             : 
     231         264 :                 if(bClosed)
     232             :                 {
     233             :                     // add first point again as closing point due to old definition
     234         258 :                     aPoints.push_back( aPoints[0] );
     235         258 :                     aFlags.push_back(drawing::PolygonFlags_NORMAL);
     236             :                 }
     237             :                 else
     238             :                 {
     239             :                     // add last point as closing point
     240           6 :                     const basegfx::B2DPoint aClosingPoint(rPoly.getB2DPoint(nCount - 1L));
     241           6 :                     const awt::Point aEnd(fround(aClosingPoint.getX()),
     242          12 :                                           fround(aClosingPoint.getY()));
     243           6 :                     aPoints.push_back(aEnd);
     244           6 :                     aFlags.push_back(drawing::PolygonFlags_NORMAL);
     245         264 :                 }
     246             :             }
     247             : 
     248         264 :             *pOuterSequence++ = comphelper::containerToSequence(aPoints);
     249         264 :             *pOuterFlags++ = comphelper::containerToSequence(aFlags);
     250         264 :         }
     251        1942 :     }
     252             : 
     253             : }
     254             : }
     255             : 
     256             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10