Version 24.2.2.2, tag libreoffice-24.2.2.2
[LibreOffice.git] / basegfx / source / polygon / b2dpolypolygon.cxx
blob547634dc4dad1456a684188a0a11226deb9e0d68
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <cassert>
23 #include <utility>
25 #include <basegfx/polygon/b2dpolypolygon.hxx>
26 #include <basegfx/polygon/b2dpolygon.hxx>
27 #include <basegfx/matrix/b2dhommatrix.hxx>
28 #include <basegfx/utils/systemdependentdata.hxx>
30 namespace basegfx
33 class ImplB2DPolyPolygon
35 basegfx::B2DPolygonVector maPolygons;
36 // we do not want to 'modify' the ImplB2DPolyPolygon,
37 // but add buffered data that is valid for all referencing instances
38 mutable std::unique_ptr<basegfx::SystemDependentDataHolder> mpSystemDependentDataHolder;
40 public:
41 ImplB2DPolyPolygon()
45 explicit ImplB2DPolyPolygon(const ImplB2DPolyPolygon& rSource)
46 : maPolygons(rSource.maPolygons)
50 explicit ImplB2DPolyPolygon(const basegfx::B2DPolygon& rToBeCopied)
51 : maPolygons(1,rToBeCopied)
55 ImplB2DPolyPolygon& operator=(const ImplB2DPolyPolygon& rSource)
57 if (this != &rSource)
59 maPolygons = rSource.maPolygons;
60 mpSystemDependentDataHolder.reset();
63 return *this;
66 void addOrReplaceSystemDependentData(basegfx::SystemDependentData_SharedPtr& rData) const
68 if(!mpSystemDependentDataHolder)
70 mpSystemDependentDataHolder.reset(new basegfx::SystemDependentDataHolder());
73 mpSystemDependentDataHolder->addOrReplaceSystemDependentData(rData);
76 basegfx::SystemDependentData_SharedPtr getSystemDependentData(size_t hash_code) const
78 if(!mpSystemDependentDataHolder)
80 return basegfx::SystemDependentData_SharedPtr();
83 return mpSystemDependentDataHolder->getSystemDependentData(hash_code);
86 bool operator==(const ImplB2DPolyPolygon& rPolygonList) const
88 return maPolygons == rPolygonList.maPolygons;
91 const basegfx::B2DPolygon& getB2DPolygon(sal_uInt32 nIndex) const
93 assert(nIndex < maPolygons.size());
94 return maPolygons[nIndex];
97 void setB2DPolygon(sal_uInt32 nIndex, const basegfx::B2DPolygon& rPolygon)
99 assert(nIndex < maPolygons.size());
100 maPolygons[nIndex] = rPolygon;
103 void insert(sal_uInt32 nIndex, const basegfx::B2DPolygon& rPolygon, sal_uInt32 nCount)
105 assert(nCount > 0);
106 assert(nIndex <= maPolygons.size());
107 // add nCount copies of rPolygon
108 maPolygons.insert(maPolygons.begin() + nIndex, nCount, rPolygon);
111 void append(const basegfx::B2DPolygon& rPolygon, sal_uInt32 nCount)
113 insert(maPolygons.size(), rPolygon, nCount);
116 void reserve(sal_uInt32 nCount)
118 maPolygons.reserve(nCount);
121 void insert(sal_uInt32 nIndex, const basegfx::B2DPolyPolygon& rPolyPolygon)
123 assert(nIndex <= maPolygons.size());
124 // add nCount polygons from rPolyPolygon
125 maPolygons.insert(maPolygons.begin() + nIndex, rPolyPolygon.begin(), rPolyPolygon.end());
128 void append(const basegfx::B2DPolyPolygon& rPolyPolygon)
130 insert(maPolygons.size(), rPolyPolygon);
133 void remove(sal_uInt32 nIndex, sal_uInt32 nCount)
135 assert(nCount > 0);
136 assert(nIndex + nCount <= maPolygons.size());
137 // remove polygon data
138 auto aStart(maPolygons.begin() + nIndex);
139 auto aEnd(aStart + nCount);
141 maPolygons.erase(aStart, aEnd);
144 sal_uInt32 count() const
146 return maPolygons.size();
149 void setClosed(bool bNew)
151 for(basegfx::B2DPolygon & rPolygon : maPolygons)
153 rPolygon.setClosed(bNew);
157 void flip()
159 for (auto& aPolygon : maPolygons)
160 aPolygon.flip();
163 void removeDoublePoints()
165 for (auto& aPolygon : maPolygons)
166 aPolygon.removeDoublePoints();
169 void transform(const basegfx::B2DHomMatrix& rMatrix)
171 for (auto& aPolygon : maPolygons)
172 aPolygon.transform(rMatrix);
175 void makeUnique()
177 for (auto& aPolygon : maPolygons)
178 aPolygon.makeUnique();
181 const basegfx::B2DPolygon* begin() const
183 if (maPolygons.empty())
184 return nullptr;
185 else
186 return maPolygons.data();
189 const basegfx::B2DPolygon* end() const
191 if (maPolygons.empty())
192 return nullptr;
193 else
194 return maPolygons.data() + maPolygons.size();
197 basegfx::B2DPolygon* begin()
199 if (maPolygons.empty())
200 return nullptr;
201 else
202 return maPolygons.data();
205 basegfx::B2DPolygon* end()
207 if (maPolygons.empty())
208 return nullptr;
209 else
210 return maPolygons.data() + maPolygons.size();
214 B2DPolyPolygon::B2DPolyPolygon() = default;
216 B2DPolyPolygon::B2DPolyPolygon(const B2DPolyPolygon&) = default;
218 B2DPolyPolygon::B2DPolyPolygon(B2DPolyPolygon&&) = default;
220 B2DPolyPolygon::B2DPolyPolygon(const B2DPolygon& rPolygon) :
221 mpPolyPolygon( ImplB2DPolyPolygon(rPolygon) )
225 B2DPolyPolygon::~B2DPolyPolygon() = default;
227 B2DPolyPolygon& B2DPolyPolygon::operator=(const B2DPolyPolygon&) = default;
229 B2DPolyPolygon& B2DPolyPolygon::operator=(B2DPolyPolygon&&) = default;
231 void B2DPolyPolygon::makeUnique()
233 mpPolyPolygon->makeUnique(); // non-const cow_wrapper::operator-> calls make_unique
236 bool B2DPolyPolygon::operator==(const B2DPolyPolygon& rPolyPolygon) const
238 if(mpPolyPolygon.same_object(rPolyPolygon.mpPolyPolygon))
239 return true;
241 return ((*mpPolyPolygon) == (*rPolyPolygon.mpPolyPolygon));
244 bool B2DPolyPolygon::operator!=(const B2DPolyPolygon& rPolyPolygon) const
246 return !((*this) == rPolyPolygon);
249 sal_uInt32 B2DPolyPolygon::count() const
251 return mpPolyPolygon->count();
254 B2DPolygon const & B2DPolyPolygon::getB2DPolygon(sal_uInt32 nIndex) const
256 return mpPolyPolygon->getB2DPolygon(nIndex);
259 void B2DPolyPolygon::setB2DPolygon(sal_uInt32 nIndex, const B2DPolygon& rPolygon)
261 if(getB2DPolygon(nIndex) != rPolygon)
262 mpPolyPolygon->setB2DPolygon(nIndex, rPolygon);
265 bool B2DPolyPolygon::areControlPointsUsed() const
267 for(sal_uInt32 a(0); a < count(); a++)
269 if(getB2DPolygon(a).areControlPointsUsed())
271 return true;
275 return false;
278 void B2DPolyPolygon::insert(sal_uInt32 nIndex, const B2DPolygon& rPolygon, sal_uInt32 nCount)
280 if(nCount)
281 mpPolyPolygon->insert(nIndex, rPolygon, nCount);
284 void B2DPolyPolygon::append(const B2DPolygon& rPolygon, sal_uInt32 nCount)
286 if(nCount)
287 mpPolyPolygon->append(rPolygon, nCount);
290 void B2DPolyPolygon::reserve(sal_uInt32 nCount)
292 if(nCount)
293 mpPolyPolygon->reserve(nCount);
296 B2DPolyPolygon B2DPolyPolygon::getDefaultAdaptiveSubdivision() const
298 B2DPolyPolygon aRetval;
299 if (count())
301 ImplB2DPolyPolygon& dest = *aRetval.mpPolyPolygon;
302 dest.reserve(count());
304 for (sal_uInt32 a(0); a < count(); a++)
306 dest.append(getB2DPolygon(a).getDefaultAdaptiveSubdivision(), 1);
310 return aRetval;
313 B2DRange B2DPolyPolygon::getB2DRange() const
315 B2DRange aRetval;
317 for(sal_uInt32 a(0); a < count(); a++)
319 aRetval.expand(getB2DPolygon(a).getB2DRange());
322 return aRetval;
325 void B2DPolyPolygon::insert(sal_uInt32 nIndex, const B2DPolyPolygon& rPolyPolygon)
327 if(rPolyPolygon.count())
328 mpPolyPolygon->insert(nIndex, rPolyPolygon);
331 void B2DPolyPolygon::append(const B2DPolyPolygon& rPolyPolygon)
333 if(rPolyPolygon.count())
334 mpPolyPolygon->append(rPolyPolygon);
337 void B2DPolyPolygon::remove(sal_uInt32 nIndex, sal_uInt32 nCount)
339 if(nCount)
340 mpPolyPolygon->remove(nIndex, nCount);
343 void B2DPolyPolygon::clear()
345 *mpPolyPolygon = ImplB2DPolyPolygon();
348 bool B2DPolyPolygon::isClosed() const
350 // PolyPOlygon is closed when all contained Polygons are closed or
351 // no Polygon exists.
352 for(sal_uInt32 a(0); a < count(); a++)
354 if(!getB2DPolygon(a).isClosed())
355 return false;
358 return true;
361 void B2DPolyPolygon::setClosed(bool bNew)
363 if(bNew != isClosed())
364 mpPolyPolygon->setClosed(bNew);
367 void B2DPolyPolygon::flip()
369 if(count())
371 mpPolyPolygon->flip();
375 bool B2DPolyPolygon::hasDoublePoints() const
377 for(sal_uInt32 a(0); a < count(); a++)
379 if(getB2DPolygon(a).hasDoublePoints())
380 return true;
383 return false;
386 void B2DPolyPolygon::removeDoublePoints()
388 if(hasDoublePoints())
389 mpPolyPolygon->removeDoublePoints();
392 void B2DPolyPolygon::transform(const B2DHomMatrix& rMatrix)
394 if(count() && !rMatrix.isIdentity())
396 mpPolyPolygon->transform(rMatrix);
400 const B2DPolygon* B2DPolyPolygon::begin() const
402 return mpPolyPolygon->begin();
405 const B2DPolygon* B2DPolyPolygon::end() const
407 return mpPolyPolygon->end();
410 B2DPolygon* B2DPolyPolygon::begin()
412 return mpPolyPolygon->begin();
415 B2DPolygon* B2DPolyPolygon::end()
417 return mpPolyPolygon->end();
420 void B2DPolyPolygon::addOrReplaceSystemDependentDataInternal(SystemDependentData_SharedPtr& rData) const
422 mpPolyPolygon->addOrReplaceSystemDependentData(rData);
425 SystemDependentData_SharedPtr B2DPolyPolygon::getSystemDependantDataInternal(size_t hash_code) const
427 return mpPolyPolygon->getSystemDependentData(hash_code);
430 } // end of namespace basegfx
432 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */