/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ #define POLY_CLIP_INT 0 #define POLY_CLIP_UNION 1 #define POLY_CLIP_DIFF 2 #define POLY_CLIP_XOR 3 #include #include #include #include #include #include #include #include #include #include #include #include ImplPolyPolygon::ImplPolyPolygon( sal_uInt16 nInitSize ) { mnRefCount = 1; mnCount = nInitSize; mnSize = nInitSize; mnResize = 16; mpPolyAry = new SVPPOLYGON[ nInitSize ]; } ImplPolyPolygon::ImplPolyPolygon( const ImplPolyPolygon& rImplPolyPoly ) { mnRefCount = 1; mnCount = rImplPolyPoly.mnCount; mnSize = rImplPolyPoly.mnSize; mnResize = rImplPolyPoly.mnResize; if ( rImplPolyPoly.mpPolyAry ) { mpPolyAry = new SVPPOLYGON[mnSize]; for ( sal_uInt16 i = 0; i < mnCount; i++ ) mpPolyAry[i] = new Polygon( *rImplPolyPoly.mpPolyAry[i] ); } else mpPolyAry = NULL; } ImplPolyPolygon::~ImplPolyPolygon() { if ( mpPolyAry ) { for ( sal_uInt16 i = 0; i < mnCount; i++ ) delete mpPolyAry[i]; delete[] mpPolyAry; } } namespace tools { PolyPolygon::PolyPolygon( sal_uInt16 nInitSize, sal_uInt16 nResize ) { if ( nInitSize > MAX_POLYGONS ) nInitSize = MAX_POLYGONS; else if ( !nInitSize ) nInitSize = 1; if ( nResize > MAX_POLYGONS ) nResize = MAX_POLYGONS; else if ( !nResize ) nResize = 1; mpImplPolyPolygon = new ImplPolyPolygon( nInitSize, nResize ); } PolyPolygon::PolyPolygon( const Polygon& rPoly ) { if ( rPoly.GetSize() ) { mpImplPolyPolygon = new ImplPolyPolygon( 1 ); mpImplPolyPolygon->mpPolyAry[0] = new Polygon( rPoly ); } else mpImplPolyPolygon = new ImplPolyPolygon( 16, 16 ); } PolyPolygon::PolyPolygon( const tools::PolyPolygon& rPolyPoly ) { DBG_ASSERT( rPolyPoly.mpImplPolyPolygon->mnRefCount < 0xFFFFFFFE, "PolyPolygon: RefCount overflow" ); mpImplPolyPolygon = rPolyPoly.mpImplPolyPolygon; mpImplPolyPolygon->mnRefCount++; } PolyPolygon::~PolyPolygon() { if ( mpImplPolyPolygon->mnRefCount > 1 ) mpImplPolyPolygon->mnRefCount--; else delete mpImplPolyPolygon; } void PolyPolygon::Insert( const Polygon& rPoly, sal_uInt16 nPos ) { if ( mpImplPolyPolygon->mnCount >= MAX_POLYGONS ) return; if ( mpImplPolyPolygon->mnRefCount > 1 ) { mpImplPolyPolygon->mnRefCount--; mpImplPolyPolygon = new ImplPolyPolygon( *mpImplPolyPolygon ); } if ( nPos > mpImplPolyPolygon->mnCount ) nPos = mpImplPolyPolygon->mnCount; if ( !mpImplPolyPolygon->mpPolyAry ) mpImplPolyPolygon->mpPolyAry = new SVPPOLYGON[mpImplPolyPolygon->mnSize]; else if ( mpImplPolyPolygon->mnCount == mpImplPolyPolygon->mnSize ) { sal_uInt16 nOldSize = mpImplPolyPolygon->mnSize; sal_uInt16 nNewSize = nOldSize + mpImplPolyPolygon->mnResize; SVPPOLYGON* pNewAry; if ( nNewSize >= MAX_POLYGONS ) nNewSize = MAX_POLYGONS; pNewAry = new SVPPOLYGON[nNewSize]; memcpy( pNewAry, mpImplPolyPolygon->mpPolyAry, nPos*sizeof(SVPPOLYGON) ); memcpy( pNewAry+nPos+1, mpImplPolyPolygon->mpPolyAry+nPos, (nOldSize-nPos)*sizeof(SVPPOLYGON) ); delete[] mpImplPolyPolygon->mpPolyAry; mpImplPolyPolygon->mpPolyAry = pNewAry; mpImplPolyPolygon->mnSize = nNewSize; } else if ( nPos < mpImplPolyPolygon->mnCount ) { memmove( mpImplPolyPolygon->mpPolyAry+nPos+1, mpImplPolyPolygon->mpPolyAry+nPos, (mpImplPolyPolygon->mnCount-nPos)*sizeof(SVPPOLYGON) ); } mpImplPolyPolygon->mpPolyAry[nPos] = new Polygon( rPoly ); mpImplPolyPolygon->mnCount++; } void PolyPolygon::Remove( sal_uInt16 nPos ) { assert(nPos < Count() && "PolyPolygon::Remove(): nPos >= nSize"); if ( mpImplPolyPolygon->mnRefCount > 1 ) { mpImplPolyPolygon->mnRefCount--; mpImplPolyPolygon = new ImplPolyPolygon( *mpImplPolyPolygon ); } delete mpImplPolyPolygon->mpPolyAry[nPos]; mpImplPolyPolygon->mnCount--; memmove( mpImplPolyPolygon->mpPolyAry+nPos, mpImplPolyPolygon->mpPolyAry+nPos+1, (mpImplPolyPolygon->mnCount-nPos)*sizeof(SVPPOLYGON) ); } void PolyPolygon::Replace( const Polygon& rPoly, sal_uInt16 nPos ) { assert(nPos < Count() && "PolyPolygon::Replace(): nPos >= nSize"); if ( mpImplPolyPolygon->mnRefCount > 1 ) { mpImplPolyPolygon->mnRefCount--; mpImplPolyPolygon = new ImplPolyPolygon( *mpImplPolyPolygon ); } delete mpImplPolyPolygon->mpPolyAry[nPos]; mpImplPolyPolygon->mpPolyAry[nPos] = new Polygon( rPoly ); } const Polygon& PolyPolygon::GetObject( sal_uInt16 nPos ) const { assert(nPos < Count() && "PolyPolygon::GetObject(): nPos >= nSize"); return *(mpImplPolyPolygon->mpPolyAry[nPos]); } bool PolyPolygon::IsRect() const { bool bIsRect = false; if ( Count() == 1 ) bIsRect = mpImplPolyPolygon->mpPolyAry[ 0 ]->IsRect(); return bIsRect; } void PolyPolygon::Clear() { if ( mpImplPolyPolygon->mnRefCount > 1 ) { mpImplPolyPolygon->mnRefCount--; mpImplPolyPolygon = new ImplPolyPolygon( mpImplPolyPolygon->mnResize, mpImplPolyPolygon->mnResize ); } else { if ( mpImplPolyPolygon->mpPolyAry ) { for ( sal_uInt16 i = 0; i < mpImplPolyPolygon->mnCount; i++ ) delete mpImplPolyPolygon->mpPolyAry[i]; delete[] mpImplPolyPolygon->mpPolyAry; mpImplPolyPolygon->mpPolyAry = NULL; mpImplPolyPolygon->mnCount = 0; mpImplPolyPolygon->mnSize = mpImplPolyPolygon->mnResize; } } } void PolyPolygon::Optimize( PolyOptimizeFlags nOptimizeFlags, const PolyOptimizeData* pData ) { if(bool(nOptimizeFlags) && Count()) { // #115630# ImplDrawHatch does not work with beziers included in the polypolygon, take care of that bool bIsCurve(false); for(sal_uInt16 a(0); !bIsCurve && a < Count(); a++) { if((*this)[a].HasFlags()) { bIsCurve = true; } } if(bIsCurve) { OSL_ENSURE(false, "Optimize does *not* support curves, falling back to AdaptiveSubdivide()..."); tools::PolyPolygon aPolyPoly; AdaptiveSubdivide(aPolyPoly); aPolyPoly.Optimize(nOptimizeFlags, pData); *this = aPolyPoly; } else { double fArea; const bool bEdges = ( nOptimizeFlags & PolyOptimizeFlags::EDGES ) == PolyOptimizeFlags::EDGES; sal_uInt16 nPercent = 0; if( bEdges ) { const Rectangle aBound( GetBoundRect() ); fArea = ( aBound.GetWidth() + aBound.GetHeight() ) * 0.5; nPercent = pData ? pData->GetPercentValue() : 50; nOptimizeFlags &= ~PolyOptimizeFlags::EDGES; } // watch for ref counter if( mpImplPolyPolygon->mnRefCount > 1 ) { mpImplPolyPolygon->mnRefCount--; mpImplPolyPolygon = new ImplPolyPolygon( *mpImplPolyPolygon ); } // Optimize polygons for( sal_uInt16 i = 0, nPolyCount = mpImplPolyPolygon->mnCount; i < nPolyCount; i++ ) { if( bEdges ) { mpImplPolyPolygon->mpPolyAry[ i ]->Optimize( PolyOptimizeFlags::NO_SAME ); Polygon::ImplReduceEdges( *( mpImplPolyPolygon->mpPolyAry[ i ] ), fArea, nPercent ); } if( bool(nOptimizeFlags) ) mpImplPolyPolygon->mpPolyAry[ i ]->Optimize( nOptimizeFlags, pData ); } } } } void PolyPolygon::AdaptiveSubdivide( tools::PolyPolygon& rResult, const double d ) const { rResult.Clear(); Polygon aPolygon; for( sal_uInt16 i = 0; i < mpImplPolyPolygon->mnCount; i++ ) { mpImplPolyPolygon->mpPolyAry[ i ]->AdaptiveSubdivide( aPolygon, d ); rResult.Insert( aPolygon ); } } tools::PolyPolygon PolyPolygon::SubdivideBezier( const tools::PolyPolygon& rPolyPoly ) { sal_uInt16 i, nPolys = rPolyPoly.Count(); tools::PolyPolygon aPolyPoly( nPolys ); for( i=0; imnCount; } void PolyPolygon::Move( long nHorzMove, long nVertMove ) { // Required for DrawEngine if( nHorzMove || nVertMove ) { if ( mpImplPolyPolygon->mnRefCount > 1 ) { mpImplPolyPolygon->mnRefCount--; mpImplPolyPolygon = new ImplPolyPolygon( *mpImplPolyPolygon ); } // move points sal_uInt16 nPolyCount = mpImplPolyPolygon->mnCount; for ( sal_uInt16 i = 0; i < nPolyCount; i++ ) mpImplPolyPolygon->mpPolyAry[i]->Move( nHorzMove, nVertMove ); } } void PolyPolygon::Translate( const Point& rTrans ) { if( mpImplPolyPolygon->mnRefCount > 1 ) { mpImplPolyPolygon->mnRefCount--; mpImplPolyPolygon = new ImplPolyPolygon( *mpImplPolyPolygon ); } // move points for ( sal_uInt16 i = 0, nCount = mpImplPolyPolygon->mnCount; i < nCount; i++ ) mpImplPolyPolygon->mpPolyAry[ i ]->Translate( rTrans ); } void PolyPolygon::Scale( double fScaleX, double fScaleY ) { if( mpImplPolyPolygon->mnRefCount > 1 ) { mpImplPolyPolygon->mnRefCount--; mpImplPolyPolygon = new ImplPolyPolygon( *mpImplPolyPolygon ); } // Move points for ( sal_uInt16 i = 0, nCount = mpImplPolyPolygon->mnCount; i < nCount; i++ ) mpImplPolyPolygon->mpPolyAry[ i ]->Scale( fScaleX, fScaleY ); } void PolyPolygon::Rotate( const Point& rCenter, sal_uInt16 nAngle10 ) { nAngle10 %= 3600; if( nAngle10 ) { const double fAngle = F_PI1800 * nAngle10; Rotate( rCenter, sin( fAngle ), cos( fAngle ) ); } } void PolyPolygon::Rotate( const Point& rCenter, double fSin, double fCos ) { if( mpImplPolyPolygon->mnRefCount > 1 ) { mpImplPolyPolygon->mnRefCount--; mpImplPolyPolygon = new ImplPolyPolygon( *mpImplPolyPolygon ); } // move points for ( sal_uInt16 i = 0, nCount = mpImplPolyPolygon->mnCount; i < nCount; i++ ) mpImplPolyPolygon->mpPolyAry[ i ]->Rotate( rCenter, fSin, fCos ); } void PolyPolygon::Clip( const Rectangle& rRect ) { sal_uInt16 nPolyCount = mpImplPolyPolygon->mnCount; sal_uInt16 i; if ( !nPolyCount ) return; if ( mpImplPolyPolygon->mnRefCount > 1 ) { mpImplPolyPolygon->mnRefCount--; mpImplPolyPolygon = new ImplPolyPolygon( *mpImplPolyPolygon ); } // Clip every polygon, deleting the empty ones for ( i = 0; i < nPolyCount; i++ ) mpImplPolyPolygon->mpPolyAry[i]->Clip( rRect ); while ( nPolyCount ) { if ( GetObject( nPolyCount-1 ).GetSize() <= 2 ) Remove( nPolyCount-1 ); nPolyCount--; } } Rectangle PolyPolygon::GetBoundRect() const { long nXMin=0, nXMax=0, nYMin=0, nYMax=0; bool bFirst = true; sal_uInt16 nPolyCount = mpImplPolyPolygon->mnCount; for ( sal_uInt16 n = 0; n < nPolyCount; n++ ) { const Polygon* pPoly = mpImplPolyPolygon->mpPolyAry[n]; const Point* pAry = pPoly->GetConstPointAry(); sal_uInt16 nPointCount = pPoly->GetSize(); for ( sal_uInt16 i = 0; i < nPointCount; i++ ) { const Point* pPt = &pAry[ i ]; if ( bFirst ) { nXMin = nXMax = pPt->X(); nYMin = nYMax = pPt->Y(); bFirst = false; } else { if ( pPt->X() < nXMin ) nXMin = pPt->X(); if ( pPt->X() > nXMax ) nXMax = pPt->X(); if ( pPt->Y() < nYMin ) nYMin = pPt->Y(); if ( pPt->Y() > nYMax ) nYMax = pPt->Y(); } } } if ( !bFirst ) return Rectangle( nXMin, nYMin, nXMax, nYMax ); else return Rectangle(); } Polygon& PolyPolygon::operator[]( sal_uInt16 nPos ) { assert(nPos < Count() && "PolyPolygon::[](): nPos >= nSize"); if ( mpImplPolyPolygon->mnRefCount > 1 ) { mpImplPolyPolygon->mnRefCount--; mpImplPolyPolygon = new ImplPolyPolygon( *mpImplPolyPolygon ); } return *(mpImplPolyPolygon->mpPolyAry[nPos]); } PolyPolygon& PolyPolygon::operator=( const tools::PolyPolygon& rPolyPoly ) { if (this == &rPolyPoly) return *this; DBG_ASSERT( rPolyPoly.mpImplPolyPolygon->mnRefCount < 0xFFFFFFFE, "PolyPolygon: RefCount overflow" ); rPolyPoly.mpImplPolyPolygon->mnRefCount++; if ( mpImplPolyPolygon->mnRefCount > 1 ) mpImplPolyPolygon->mnRefCount--; else delete mpImplPolyPolygon; mpImplPolyPolygon = rPolyPoly.mpImplPolyPolygon; return *this; } bool PolyPolygon::operator==( const tools::PolyPolygon& rPolyPoly ) const { if ( rPolyPoly.mpImplPolyPolygon == mpImplPolyPolygon ) return true; else return false; } bool PolyPolygon::IsEqual( const tools::PolyPolygon& rPolyPoly ) const { bool bIsEqual = true; if ( Count() != rPolyPoly.Count() ) bIsEqual = false; else { sal_uInt16 i; for ( i = 0; i < Count(); i++ ) { if (!GetObject( i ).IsEqual( rPolyPoly.GetObject( i ) ) ) { bIsEqual = false; break; } } } return bIsEqual; } SvStream& ReadPolyPolygon( SvStream& rIStream, tools::PolyPolygon& rPolyPoly ) { DBG_ASSERTWARNING( rIStream.GetVersion(), "PolyPolygon::>> - Solar-Version not set on rIStream" ); Polygon* pPoly; sal_uInt16 nPolyCount(0); // Read number of polygons rIStream.ReadUInt16( nPolyCount ); const size_t nMinRecordSize = sizeof(sal_uInt16); const size_t nMaxRecords = rIStream.remainingSize() / nMinRecordSize; if (nPolyCount > nMaxRecords) { SAL_WARN("tools", "Parsing error: " << nMaxRecords << " max possible entries, but " << nPolyCount << " claimed, truncating"); nPolyCount = nMaxRecords; } if( nPolyCount ) { if ( rPolyPoly.mpImplPolyPolygon->mnRefCount > 1 ) rPolyPoly.mpImplPolyPolygon->mnRefCount--; else delete rPolyPoly.mpImplPolyPolygon; rPolyPoly.mpImplPolyPolygon = new ImplPolyPolygon( nPolyCount ); for ( sal_uInt16 i = 0; i < nPolyCount; i++ ) { pPoly = new Polygon; ReadPolygon( rIStream, *pPoly ); rPolyPoly.mpImplPolyPolygon->mpPolyAry[i] = pPoly; } } else rPolyPoly = tools::PolyPolygon(); return rIStream; } SvStream& WritePolyPolygon( SvStream& rOStream, const tools::PolyPolygon& rPolyPoly ) { DBG_ASSERTWARNING( rOStream.GetVersion(), "PolyPolygon::<< - Solar-Version not set on rOStream" ); // Write number of polygons sal_uInt16 nPolyCount = rPolyPoly.mpImplPolyPolygon->mnCount; rOStream.WriteUInt16( nPolyCount ); // output polygons for ( sal_uInt16 i = 0; i < nPolyCount; i++ ) WritePolygon( rOStream, *(rPolyPoly.mpImplPolyPolygon->mpPolyAry[i]) ); return rOStream; } void PolyPolygon::Read( SvStream& rIStream ) { VersionCompat aCompat( rIStream, StreamMode::READ ); DBG_ASSERTWARNING( rIStream.GetVersion(), "PolyPolygon::>> - Solar-Version not set on rIStream" ); Polygon* pPoly; sal_uInt16 nPolyCount(0); // Read number of polygons rIStream.ReadUInt16( nPolyCount ); const size_t nMinRecordSize = sizeof(sal_uInt16); const size_t nMaxRecords = rIStream.remainingSize() / nMinRecordSize; if (nPolyCount > nMaxRecords) { SAL_WARN("tools", "Parsing error: " << nMaxRecords << " max possible entries, but " << nPolyCount << " claimed, truncating"); nPolyCount = nMaxRecords; } if( nPolyCount ) { if ( mpImplPolyPolygon->mnRefCount > 1 ) mpImplPolyPolygon->mnRefCount--; else delete mpImplPolyPolygon; mpImplPolyPolygon = new ImplPolyPolygon( nPolyCount ); for ( sal_uInt16 i = 0; i < nPolyCount; i++ ) { pPoly = new Polygon; pPoly->ImplRead( rIStream ); mpImplPolyPolygon->mpPolyAry[i] = pPoly; } } else *this = tools::PolyPolygon(); } void PolyPolygon::Write( SvStream& rOStream ) const { VersionCompat aCompat( rOStream, StreamMode::WRITE, 1 ); DBG_ASSERTWARNING( rOStream.GetVersion(), "PolyPolygon::<< - Solar-Version not set on rOStream" ); // Write number of polygons sal_uInt16 nPolyCount = mpImplPolyPolygon->mnCount; rOStream.WriteUInt16( nPolyCount ); // Output polygons for ( sal_uInt16 i = 0; i < nPolyCount; i++ ) mpImplPolyPolygon->mpPolyAry[i]->ImplWrite( rOStream ); } // convert to basegfx::B2DPolyPolygon and return basegfx::B2DPolyPolygon PolyPolygon::getB2DPolyPolygon() const { basegfx::B2DPolyPolygon aRetval; for(sal_uInt16 a(0); a < mpImplPolyPolygon->mnCount; a++) { Polygon* pCandidate = mpImplPolyPolygon->mpPolyAry[a]; aRetval.append(pCandidate->getB2DPolygon()); } return aRetval; } // constructor to convert from basegfx::B2DPolyPolygon PolyPolygon::PolyPolygon(const basegfx::B2DPolyPolygon& rPolyPolygon) { const sal_uInt16 nCount(sal_uInt16(rPolyPolygon.count())); DBG_ASSERT(sal_uInt32(nCount) == rPolyPolygon.count(), "PolyPolygon::PolyPolygon: Too many sub-polygons in given basegfx::B2DPolyPolygon (!)"); if ( nCount ) { mpImplPolyPolygon = new ImplPolyPolygon( nCount ); for(sal_uInt16 a(0); a < nCount; a++) { basegfx::B2DPolygon aCandidate(rPolyPolygon.getB2DPolygon(sal_uInt32(a))); mpImplPolyPolygon->mpPolyAry[a] = new Polygon( aCandidate ); } } else { mpImplPolyPolygon = new ImplPolyPolygon( 16, 16 ); } } } /* namespace tools */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */