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 <sal/log.hxx>
21 #include <osl/diagnose.h>
22 #include <poly.h>
23 #include <tools/poly.hxx>
24 #include <tools/debug.hxx>
25 #include <tools/stream.hxx>
26 #include <tools/vcompat.hxx>
27 #include <tools/gen.hxx>
28 #include <basegfx/polygon/b2dpolypolygon.hxx>
29 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
30 
31 namespace tools {
32 
PolyPolygon(sal_uInt16 nInitSize)33 PolyPolygon::PolyPolygon( sal_uInt16 nInitSize )
34     : mpImplPolyPolygon( ImplPolyPolygon( nInitSize ) )
35 {
36 }
37 
PolyPolygon(const tools::Polygon & rPoly)38 PolyPolygon::PolyPolygon( const tools::Polygon& rPoly )
39     : mpImplPolyPolygon( rPoly )
40 {
41 }
42 
PolyPolygon(const tools::PolyPolygon & rPolyPoly)43 PolyPolygon::PolyPolygon( const tools::PolyPolygon& rPolyPoly )
44     : mpImplPolyPolygon( rPolyPoly.mpImplPolyPolygon )
45 {
46 }
47 
PolyPolygon(tools::PolyPolygon && rPolyPoly)48 PolyPolygon::PolyPolygon( tools::PolyPolygon&& rPolyPoly ) noexcept
49     : mpImplPolyPolygon( std::move(rPolyPoly.mpImplPolyPolygon) )
50 {
51 }
52 
~PolyPolygon()53 PolyPolygon::~PolyPolygon()
54 {
55 }
56 
Insert(const tools::Polygon & rPoly,sal_uInt16 nPos)57 void PolyPolygon::Insert( const tools::Polygon& rPoly, sal_uInt16 nPos )
58 {
59     assert ( mpImplPolyPolygon->mvPolyAry.size() < MAX_POLYGONS );
60 
61     if ( nPos > mpImplPolyPolygon->mvPolyAry.size() )
62         nPos = mpImplPolyPolygon->mvPolyAry.size();
63 
64     mpImplPolyPolygon->mvPolyAry.insert(mpImplPolyPolygon->mvPolyAry.begin() + nPos, rPoly);
65 }
66 
Remove(sal_uInt16 nPos)67 void PolyPolygon::Remove( sal_uInt16 nPos )
68 {
69     assert(nPos < Count() && "PolyPolygon::Remove(): nPos >= nSize");
70 
71     mpImplPolyPolygon->mvPolyAry.erase(mpImplPolyPolygon->mvPolyAry.begin() + nPos);
72 }
73 
Replace(const tools::Polygon & rPoly,sal_uInt16 nPos)74 void PolyPolygon::Replace( const tools::Polygon& rPoly, sal_uInt16 nPos )
75 {
76     assert(nPos < Count() && "PolyPolygon::Replace(): nPos >= nSize");
77 
78     mpImplPolyPolygon->mvPolyAry[nPos] = rPoly;
79 }
80 
GetObject(sal_uInt16 nPos) const81 const tools::Polygon& PolyPolygon::GetObject( sal_uInt16 nPos ) const
82 {
83     assert(nPos < Count() && "PolyPolygon::GetObject(): nPos >= nSize");
84 
85     return mpImplPolyPolygon->mvPolyAry[nPos];
86 }
87 
IsRect() const88 bool PolyPolygon::IsRect() const
89 {
90     bool bIsRect = false;
91     if ( Count() == 1 )
92         bIsRect = mpImplPolyPolygon->mvPolyAry[ 0 ].IsRect();
93     return bIsRect;
94 }
95 
Clear()96 void PolyPolygon::Clear()
97 {
98     mpImplPolyPolygon->mvPolyAry.clear();
99 }
100 
Optimize(PolyOptimizeFlags nOptimizeFlags)101 void PolyPolygon::Optimize( PolyOptimizeFlags nOptimizeFlags )
102 {
103     if(bool(nOptimizeFlags) && Count())
104     {
105         // #115630# ImplDrawHatch does not work with beziers included in the polypolygon, take care of that
106         bool bIsCurve(false);
107 
108         for(sal_uInt16 a(0); !bIsCurve && a < Count(); a++)
109         {
110             if((*this)[a].HasFlags())
111             {
112                 bIsCurve = true;
113             }
114         }
115 
116         if(bIsCurve)
117         {
118             OSL_ENSURE(false, "Optimize does *not* support curves, falling back to AdaptiveSubdivide()...");
119             tools::PolyPolygon aPolyPoly;
120 
121             AdaptiveSubdivide(aPolyPoly);
122             aPolyPoly.Optimize(nOptimizeFlags);
123             *this = aPolyPoly;
124         }
125         else
126         {
127             double      fArea;
128             const bool  bEdges = ( nOptimizeFlags & PolyOptimizeFlags::EDGES ) == PolyOptimizeFlags::EDGES;
129             sal_uInt16      nPercent = 0;
130 
131             if( bEdges )
132             {
133                 const tools::Rectangle aBound( GetBoundRect() );
134 
135                 fArea = ( aBound.GetWidth() + aBound.GetHeight() ) * 0.5;
136                 nPercent = 50;
137                 nOptimizeFlags &= ~PolyOptimizeFlags::EDGES;
138             }
139 
140             // Optimize polygons
141             for( sal_uInt16 i = 0, nPolyCount = mpImplPolyPolygon->mvPolyAry.size(); i < nPolyCount; i++ )
142             {
143                 if( bEdges )
144                 {
145                     mpImplPolyPolygon->mvPolyAry[ i ].Optimize( PolyOptimizeFlags::NO_SAME );
146                     tools::Polygon::ImplReduceEdges( mpImplPolyPolygon->mvPolyAry[ i ], fArea, nPercent );
147                 }
148 
149                 if( bool(nOptimizeFlags) )
150                     mpImplPolyPolygon->mvPolyAry[ i ].Optimize( nOptimizeFlags );
151             }
152         }
153     }
154 }
155 
AdaptiveSubdivide(tools::PolyPolygon & rResult) const156 void PolyPolygon::AdaptiveSubdivide( tools::PolyPolygon& rResult ) const
157 {
158     rResult.Clear();
159 
160     tools::Polygon aPolygon;
161 
162     for( size_t i = 0; i < mpImplPolyPolygon->mvPolyAry.size(); i++ )
163     {
164         mpImplPolyPolygon->mvPolyAry[ i ].AdaptiveSubdivide( aPolygon, 1.0 );
165         rResult.Insert( aPolygon );
166     }
167 }
168 
SubdivideBezier(const tools::PolyPolygon & rPolyPoly)169 tools::PolyPolygon PolyPolygon::SubdivideBezier( const tools::PolyPolygon& rPolyPoly )
170 {
171     sal_uInt16 i, nPolys = rPolyPoly.Count();
172     tools::PolyPolygon aPolyPoly( nPolys );
173     for( i=0; i<nPolys; ++i )
174         aPolyPoly.Insert( tools::Polygon::SubdivideBezier( rPolyPoly.GetObject(i) ) );
175 
176     return aPolyPoly;
177 }
178 
179 
GetIntersection(const tools::PolyPolygon & rPolyPoly,tools::PolyPolygon & rResult) const180 void PolyPolygon::GetIntersection( const tools::PolyPolygon& rPolyPoly, tools::PolyPolygon& rResult ) const
181 {
182     ImplDoOperation( rPolyPoly, rResult, PolyClipOp::INTERSECT );
183 }
184 
GetUnion(const tools::PolyPolygon & rPolyPoly,tools::PolyPolygon & rResult) const185 void PolyPolygon::GetUnion( const tools::PolyPolygon& rPolyPoly, tools::PolyPolygon& rResult ) const
186 {
187     ImplDoOperation( rPolyPoly, rResult, PolyClipOp::UNION );
188 }
189 
ImplDoOperation(const tools::PolyPolygon & rPolyPoly,tools::PolyPolygon & rResult,PolyClipOp nOperation) const190 void PolyPolygon::ImplDoOperation( const tools::PolyPolygon& rPolyPoly, tools::PolyPolygon& rResult, PolyClipOp nOperation ) const
191 {
192     // Convert to B2DPolyPolygon, temporarily. It might be
193     // advantageous in the future, to have a tools::PolyPolygon adaptor that
194     // just simulates a B2DPolyPolygon here...
195     basegfx::B2DPolyPolygon aMergePolyPolygonA( getB2DPolyPolygon() );
196     basegfx::B2DPolyPolygon aMergePolyPolygonB( rPolyPoly.getB2DPolyPolygon() );
197 
198     // normalize the two polypolygons before. Force properly oriented
199     // polygons.
200     aMergePolyPolygonA = basegfx::utils::prepareForPolygonOperation( aMergePolyPolygonA );
201     aMergePolyPolygonB = basegfx::utils::prepareForPolygonOperation( aMergePolyPolygonB );
202 
203     switch( nOperation )
204     {
205         // All code extracted from svx/source/svdraw/svedtv2.cxx
206 
207         case PolyClipOp::UNION:
208         {
209             // merge A and B (OR)
210             aMergePolyPolygonA = basegfx::utils::solvePolygonOperationOr(aMergePolyPolygonA, aMergePolyPolygonB);
211             break;
212         }
213 
214         default:
215         case PolyClipOp::INTERSECT:
216         {
217             // cut poly 1 against polys 2..n (AND)
218             aMergePolyPolygonA = basegfx::utils::solvePolygonOperationAnd(aMergePolyPolygonA, aMergePolyPolygonB);
219             break;
220         }
221     }
222 
223     rResult = tools::PolyPolygon( aMergePolyPolygonA );
224 }
225 
Count() const226 sal_uInt16 PolyPolygon::Count() const
227 {
228     return mpImplPolyPolygon->mvPolyAry.size();
229 }
230 
Move(long nHorzMove,long nVertMove)231 void PolyPolygon::Move( long nHorzMove, long nVertMove )
232 {
233     // Required for DrawEngine
234     if( nHorzMove || nVertMove )
235     {
236         // move points
237         sal_uInt16 nPolyCount = mpImplPolyPolygon->mvPolyAry.size();
238         for ( sal_uInt16 i = 0; i < nPolyCount; i++ )
239             mpImplPolyPolygon->mvPolyAry[i].Move( nHorzMove, nVertMove );
240     }
241 }
242 
Translate(const Point & rTrans)243 void PolyPolygon::Translate( const Point& rTrans )
244 {
245     // move points
246     for ( sal_uInt16 i = 0, nCount = mpImplPolyPolygon->mvPolyAry.size(); i < nCount; i++ )
247         mpImplPolyPolygon->mvPolyAry[ i ].Translate( rTrans );
248 }
249 
Scale(double fScaleX,double fScaleY)250 void PolyPolygon::Scale( double fScaleX, double fScaleY )
251 {
252     // Move points
253     for ( sal_uInt16 i = 0, nCount = mpImplPolyPolygon->mvPolyAry.size(); i < nCount; i++ )
254         mpImplPolyPolygon->mvPolyAry[ i ].Scale( fScaleX, fScaleY );
255 }
256 
Rotate(const Point & rCenter,sal_uInt16 nAngle10)257 void PolyPolygon::Rotate( const Point& rCenter, sal_uInt16 nAngle10 )
258 {
259     nAngle10 %= 3600;
260 
261     if( nAngle10 )
262     {
263         const double fAngle = F_PI1800 * nAngle10;
264         Rotate( rCenter, sin( fAngle ), cos( fAngle ) );
265     }
266 }
267 
Rotate(const Point & rCenter,double fSin,double fCos)268 void PolyPolygon::Rotate( const Point& rCenter, double fSin, double fCos )
269 {
270     // move points
271     for ( sal_uInt16 i = 0, nCount = mpImplPolyPolygon->mvPolyAry.size(); i < nCount; i++ )
272         mpImplPolyPolygon->mvPolyAry[ i ].Rotate( rCenter, fSin, fCos );
273 }
274 
Clip(const tools::Rectangle & rRect)275 void PolyPolygon::Clip( const tools::Rectangle& rRect )
276 {
277     sal_uInt16 nPolyCount = mpImplPolyPolygon->mvPolyAry.size();
278     sal_uInt16 i;
279 
280     if ( !nPolyCount )
281         return;
282 
283     // Clip every polygon, deleting the empty ones
284     for ( i = 0; i < nPolyCount; i++ )
285         mpImplPolyPolygon->mvPolyAry[i].Clip( rRect );
286     while ( nPolyCount )
287     {
288         if ( GetObject( nPolyCount-1 ).GetSize() <= 2 )
289             Remove( nPolyCount-1 );
290         nPolyCount--;
291     }
292 }
293 
GetBoundRect() const294 tools::Rectangle PolyPolygon::GetBoundRect() const
295 {
296     long    nXMin=0, nXMax=0, nYMin=0, nYMax=0;
297     bool    bFirst = true;
298     sal_uInt16  nPolyCount = mpImplPolyPolygon->mvPolyAry.size();
299 
300     for ( sal_uInt16 n = 0; n < nPolyCount; n++ )
301     {
302         const tools::Polygon*  pPoly = &mpImplPolyPolygon->mvPolyAry[n];
303         const Point*    pAry = pPoly->GetConstPointAry();
304         sal_uInt16          nPointCount = pPoly->GetSize();
305 
306         for ( sal_uInt16 i = 0; i < nPointCount; i++ )
307         {
308             const Point* pPt = &pAry[ i ];
309 
310             if ( bFirst )
311             {
312                 nXMin = nXMax = pPt->X();
313                 nYMin = nYMax = pPt->Y();
314                 bFirst = false;
315             }
316             else
317             {
318                 if ( pPt->X() < nXMin )
319                     nXMin = pPt->X();
320                 if ( pPt->X() > nXMax )
321                     nXMax = pPt->X();
322                 if ( pPt->Y() < nYMin )
323                     nYMin = pPt->Y();
324                 if ( pPt->Y() > nYMax )
325                     nYMax = pPt->Y();
326             }
327         }
328     }
329 
330     if ( !bFirst )
331         return tools::Rectangle( nXMin, nYMin, nXMax, nYMax );
332     else
333         return tools::Rectangle();
334 }
335 
operator [](sal_uInt16 nPos)336 Polygon& PolyPolygon::operator[]( sal_uInt16 nPos )
337 {
338     assert(nPos < Count() && "PolyPolygon::[](): nPos >= nSize");
339 
340     return mpImplPolyPolygon->mvPolyAry[nPos];
341 }
342 
operator =(const tools::PolyPolygon & rPolyPoly)343 PolyPolygon& PolyPolygon::operator=( const tools::PolyPolygon& rPolyPoly )
344 {
345     mpImplPolyPolygon = rPolyPoly.mpImplPolyPolygon;
346     return *this;
347 }
348 
operator =(tools::PolyPolygon && rPolyPoly)349 PolyPolygon& PolyPolygon::operator=( tools::PolyPolygon&& rPolyPoly ) noexcept
350 {
351     mpImplPolyPolygon = std::move(rPolyPoly.mpImplPolyPolygon);
352     return *this;
353 }
354 
operator ==(const tools::PolyPolygon & rPolyPoly) const355 bool PolyPolygon::operator==( const tools::PolyPolygon& rPolyPoly ) const
356 {
357     return rPolyPoly.mpImplPolyPolygon == mpImplPolyPolygon;
358 }
359 
ReadPolyPolygon(SvStream & rIStream,tools::PolyPolygon & rPolyPoly)360 SvStream& ReadPolyPolygon( SvStream& rIStream, tools::PolyPolygon& rPolyPoly )
361 {
362     sal_uInt16 nPolyCount(0);
363 
364     // Read number of polygons
365     rIStream.ReadUInt16( nPolyCount );
366 
367     const size_t nMinRecordSize = sizeof(sal_uInt16);
368     const size_t nMaxRecords = rIStream.remainingSize() / nMinRecordSize;
369     if (nPolyCount > nMaxRecords)
370     {
371         SAL_WARN("tools", "Parsing error: " << nMaxRecords <<
372                  " max possible entries, but " << nPolyCount << " claimed, truncating");
373         nPolyCount = nMaxRecords;
374     }
375 
376     if( nPolyCount )
377     {
378         rPolyPoly.mpImplPolyPolygon->mvPolyAry.resize(nPolyCount);
379 
380         tools::Polygon aTempPoly;
381         for ( sal_uInt16 i = 0; i < nPolyCount; i++ )
382         {
383             ReadPolygon( rIStream, aTempPoly );
384             rPolyPoly.mpImplPolyPolygon->mvPolyAry[i] = aTempPoly;
385         }
386     }
387     else
388         rPolyPoly = tools::PolyPolygon();
389 
390     return rIStream;
391 }
392 
WritePolyPolygon(SvStream & rOStream,const tools::PolyPolygon & rPolyPoly)393 SvStream& WritePolyPolygon( SvStream& rOStream, const tools::PolyPolygon& rPolyPoly )
394 {
395     // Write number of polygons
396     sal_uInt16 nPolyCount = rPolyPoly.mpImplPolyPolygon->mvPolyAry.size();
397     rOStream.WriteUInt16( nPolyCount );
398 
399     // output polygons
400     for ( sal_uInt16 i = 0; i < nPolyCount; i++ )
401         WritePolygon( rOStream, rPolyPoly.mpImplPolyPolygon->mvPolyAry[i] );
402 
403     return rOStream;
404 }
405 
Read(SvStream & rIStream)406 void PolyPolygon::Read( SvStream& rIStream )
407 {
408     VersionCompat aCompat( rIStream, StreamMode::READ );
409 
410     sal_uInt16 nPolyCount(0);
411 
412     // Read number of polygons
413     rIStream.ReadUInt16( nPolyCount );
414 
415     const size_t nMinRecordSize = sizeof(sal_uInt16);
416     const size_t nMaxRecords = rIStream.remainingSize() / nMinRecordSize;
417     if (nPolyCount > nMaxRecords)
418     {
419         SAL_WARN("tools", "Parsing error: " << nMaxRecords <<
420                  " max possible entries, but " << nPolyCount << " claimed, truncating");
421         nPolyCount = nMaxRecords;
422     }
423 
424     if( nPolyCount )
425     {
426         mpImplPolyPolygon->mvPolyAry.clear();
427 
428         for ( sal_uInt16 i = 0; i < nPolyCount; i++ )
429         {
430             tools::Polygon aTempPoly;
431             aTempPoly.ImplRead( rIStream );
432             mpImplPolyPolygon->mvPolyAry.emplace_back( aTempPoly );
433         }
434     }
435     else
436         *this = tools::PolyPolygon();
437 }
438 
Write(SvStream & rOStream) const439 void PolyPolygon::Write( SvStream& rOStream ) const
440 {
441     VersionCompat aCompat( rOStream, StreamMode::WRITE, 1 );
442 
443     // Write number of polygons
444     sal_uInt16 nPolyCount = mpImplPolyPolygon->mvPolyAry.size();
445     rOStream.WriteUInt16( nPolyCount );
446 
447     // Output polygons
448     for ( sal_uInt16 i = 0; i < nPolyCount; i++ )
449         mpImplPolyPolygon->mvPolyAry[i].ImplWrite( rOStream );
450 }
451 
452 // convert to basegfx::B2DPolyPolygon and return
getB2DPolyPolygon() const453 basegfx::B2DPolyPolygon PolyPolygon::getB2DPolyPolygon() const
454 {
455     basegfx::B2DPolyPolygon aRetval;
456 
457     for(size_t a(0); a < mpImplPolyPolygon->mvPolyAry.size(); a++)
458     {
459         tools::Polygon const & rCandidate = mpImplPolyPolygon->mvPolyAry[a];
460         aRetval.append(rCandidate.getB2DPolygon());
461     }
462 
463     return aRetval;
464 }
465 
466 // constructor to convert from basegfx::B2DPolyPolygon
PolyPolygon(const basegfx::B2DPolyPolygon & rPolyPolygon)467 PolyPolygon::PolyPolygon(const basegfx::B2DPolyPolygon& rPolyPolygon)
468     : mpImplPolyPolygon(rPolyPolygon)
469 {
470 }
471 
472 } /* namespace tools */
473 
ImplPolyPolygon(const basegfx::B2DPolyPolygon & rPolyPolygon)474 ImplPolyPolygon::ImplPolyPolygon(const basegfx::B2DPolyPolygon& rPolyPolygon)
475 {
476     const sal_uInt16 nCount(sal_uInt16(rPolyPolygon.count()));
477     DBG_ASSERT(sal_uInt32(nCount) == rPolyPolygon.count(),
478         "PolyPolygon::PolyPolygon: Too many sub-polygons in given basegfx::B2DPolyPolygon (!)");
479 
480     if ( nCount )
481     {
482         mvPolyAry.resize( nCount );
483 
484         for(sal_uInt16 a(0); a < nCount; a++)
485         {
486             const basegfx::B2DPolygon& aCandidate(rPolyPolygon.getB2DPolygon(sal_uInt32(a)));
487             mvPolyAry[a] = tools::Polygon( aCandidate );
488         }
489     }
490     else
491        mvPolyAry.reserve(16);
492 }
493 
494 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
495