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 #pragma once
21 
22 #include <ostream>
23 #include <vector>
24 
25 #include <sal/types.h>
26 #include <o3tl/cow_wrapper.hxx>
27 #include <basegfx/range/b2drange.hxx>
28 #include <basegfx/basegfxdllapi.h>
29 #include <basegfx/polygon/b2dpolygon.hxx>
30 
31 class ImplB2DPolyPolygon;
32 
33 namespace basegfx
34 {
35     class B2DHomMatrix;
36 }
37 
38 namespace basegfx
39 {
40     class BASEGFX_DLLPUBLIC B2DPolyPolygon
41     {
42     public:
43         typedef o3tl::cow_wrapper< ImplB2DPolyPolygon > ImplType;
44 
45     private:
46         ImplType                                        mpPolyPolygon;
47 
48     public:
49         B2DPolyPolygon();
50         B2DPolyPolygon(const B2DPolyPolygon& rPolyPolygon);
51         B2DPolyPolygon(B2DPolyPolygon&& rPolyPolygon);
52         explicit B2DPolyPolygon(const B2DPolygon& rPolygon);
53         ~B2DPolyPolygon();
54 
55         // assignment operator
56         B2DPolyPolygon& operator=(const B2DPolyPolygon& rPolyPolygon);
57         B2DPolyPolygon& operator=(B2DPolyPolygon&& rPolyPolygon);
58 
59         /// unshare this poly-polygon (and all included polygons) with all internally shared instances
60         void makeUnique();
61 
62         // compare operators
63         bool operator==(const B2DPolyPolygon& rPolyPolygon) const;
64         bool operator!=(const B2DPolyPolygon& rPolyPolygon) const;
65 
66         // polygon interface
67         sal_uInt32 count() const;
68 
69         B2DPolygon const & getB2DPolygon(sal_uInt32 nIndex) const;
70         void setB2DPolygon(sal_uInt32 nIndex, const B2DPolygon& rPolygon);
71 
72         // test for curve
73         bool areControlPointsUsed() const;
74 
75         // insert/append single polygon
76         void insert(sal_uInt32 nIndex, const B2DPolygon& rPolygon, sal_uInt32 nCount = 1);
77         void append(const B2DPolygon& rPolygon, sal_uInt32 nCount = 1);
78 
79         /** Default adaptive subdivision access
80 
81             For details refer to B2DPolygon::getDefaultAdaptiveSubdivision()
82 
83             @return
84             The default subdivision of this polygon
85         */
86         B2DPolyPolygon getDefaultAdaptiveSubdivision() const;
87 
88         /** Get the B2DRange (Rectangle dimensions) of this B2DPolyPolygon
89 
90             For details refer to B2DPolygon::getB2DRange()
91 
92             @return
93             The outer range of the bezier curve/polygon
94         */
95         B2DRange getB2DRange() const;
96 
97         // insert/append multiple polygons
98         void insert(sal_uInt32 nIndex, const B2DPolyPolygon& rPolyPolygon);
99         void append(const B2DPolyPolygon& rPolyPolygon);
100 
101         // remove
102         void remove(sal_uInt32 nIndex, sal_uInt32 nCount = 1);
103 
104         // reset to empty state
105         void clear();
106 
107         // closed state
108         bool isClosed() const;
109         void setClosed(bool bNew);
110 
111         // flip polygon direction
112         void flip();
113 
114         // test if tools::PolyPolygon has double points
115         bool hasDoublePoints() const;
116 
117         // remove double points, at the begin/end and follow-ups, too
118         void removeDoublePoints();
119 
120         // apply transformation given in matrix form to the polygon
121         void transform(const basegfx::B2DHomMatrix& rMatrix);
122 
123         // polygon iterators (same iterator validity conditions as for vector)
124         const B2DPolygon* begin() const;
125         const B2DPolygon* end() const;
126         B2DPolygon* begin();
127         B2DPolygon* end();
128 
129         // exclusive management op's for SystemDependentData at B2DPolygon
130         template<class T>
getSystemDependentData() const131         std::shared_ptr<T> getSystemDependentData() const
132         {
133             return std::static_pointer_cast<T>(getSystemDependantDataInternal(typeid(T).hash_code()));
134         }
135 
136         template<class T, class... Args>
addOrReplaceSystemDependentData(SystemDependentDataManager & manager,Args &&...args) const137         std::shared_ptr<T> addOrReplaceSystemDependentData(SystemDependentDataManager& manager, Args&&... args) const
138         {
139             std::shared_ptr<T> r = std::make_shared<T>(manager, std::forward<Args>(args)...);
140 
141             // tdf#129845 only add to buffer if a relevant buffer time is estimated
142             if(r->calculateCombinedHoldCyclesInSeconds() > 0)
143             {
144                 basegfx::SystemDependentData_SharedPtr r2(r);
145                 addOrReplaceSystemDependentDataInternal(r2);
146             }
147 
148             return r;
149         }
150 
151     private:
152         void addOrReplaceSystemDependentDataInternal(SystemDependentData_SharedPtr& rData) const;
153         SystemDependentData_SharedPtr getSystemDependantDataInternal(size_t hash_code) const;
154     };
155 
156     // typedef for a vector of B2DPolyPolygons
157     typedef ::std::vector< B2DPolyPolygon > B2DPolyPolygonVector;
158 
159     template< typename charT, typename traits >
operator <<(std::basic_ostream<charT,traits> & stream,const B2DPolyPolygon & poly)160     inline std::basic_ostream<charT, traits> & operator <<(
161         std::basic_ostream<charT, traits> & stream, const B2DPolyPolygon& poly )
162     {
163         stream << "[" << poly.count() << ":";
164         for (sal_uInt32 i = 0; i < poly.count(); i++)
165         {
166             if (i > 0)
167                 stream << ",";
168             stream << poly.getB2DPolygon(i);
169         }
170         stream << "]";
171 
172         return stream;
173     }
174 
175 } // end of namespace basegfx
176 
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
178