xref: /trunk/main/drawinglayer/source/primitive2d/baseprimitive2d.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_drawinglayer.hxx"
30 
31 #include <drawinglayer/primitive2d/baseprimitive2d.hxx>
32 #include <drawinglayer/geometry/viewinformation2d.hxx>
33 #include <basegfx/tools/canvastools.hxx>
34 
35 //////////////////////////////////////////////////////////////////////////////
36 
37 using namespace com::sun::star;
38 
39 //////////////////////////////////////////////////////////////////////////////
40 
41 namespace drawinglayer
42 {
43     namespace primitive2d
44     {
45         BasePrimitive2D::BasePrimitive2D()
46         :   BasePrimitive2DImplBase(m_aMutex)
47         {
48         }
49 
50         BasePrimitive2D::~BasePrimitive2D()
51         {
52         }
53 
54         bool BasePrimitive2D::operator==( const BasePrimitive2D& rPrimitive ) const
55         {
56             return (getPrimitive2DID() == rPrimitive.getPrimitive2DID());
57         }
58 
59         basegfx::B2DRange BasePrimitive2D::getB2DRange(const geometry::ViewInformation2D& rViewInformation) const
60         {
61             return getB2DRangeFromPrimitive2DSequence(get2DDecomposition(rViewInformation), rViewInformation);
62         }
63 
64         Primitive2DSequence BasePrimitive2D::get2DDecomposition(const geometry::ViewInformation2D& /*rViewInformation*/) const
65         {
66             return Primitive2DSequence();
67         }
68 
69         Primitive2DSequence SAL_CALL BasePrimitive2D::getDecomposition( const uno::Sequence< beans::PropertyValue >& rViewParameters ) throw ( uno::RuntimeException )
70         {
71             const geometry::ViewInformation2D aViewInformation(rViewParameters);
72             return get2DDecomposition(aViewInformation);
73         }
74 
75         com::sun::star::geometry::RealRectangle2D SAL_CALL BasePrimitive2D::getRange( const uno::Sequence< beans::PropertyValue >& rViewParameters ) throw ( uno::RuntimeException )
76         {
77             const geometry::ViewInformation2D aViewInformation(rViewParameters);
78             return basegfx::unotools::rectangle2DFromB2DRectangle(getB2DRange(aViewInformation));
79         }
80     } // end of namespace primitive2d
81 } // end of namespace drawinglayer
82 
83 //////////////////////////////////////////////////////////////////////////////
84 
85 namespace drawinglayer
86 {
87     namespace primitive2d
88     {
89         Primitive2DSequence BufferedDecompositionPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& /*rViewInformation*/) const
90         {
91             return Primitive2DSequence();
92         }
93 
94         BufferedDecompositionPrimitive2D::BufferedDecompositionPrimitive2D()
95         :   BasePrimitive2D(),
96             maBuffered2DDecomposition()
97         {
98         }
99 
100         Primitive2DSequence BufferedDecompositionPrimitive2D::get2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
101         {
102             ::osl::MutexGuard aGuard( m_aMutex );
103 
104             if(!getBuffered2DDecomposition().hasElements())
105             {
106                 const Primitive2DSequence aNewSequence(create2DDecomposition(rViewInformation));
107                 const_cast< BufferedDecompositionPrimitive2D* >(this)->setBuffered2DDecomposition(aNewSequence);
108             }
109 
110             return getBuffered2DDecomposition();
111         }
112     } // end of namespace primitive2d
113 } // end of namespace drawinglayer
114 
115 //////////////////////////////////////////////////////////////////////////////
116 // tooling
117 
118 namespace drawinglayer
119 {
120     namespace primitive2d
121     {
122         // get B2DRange from a given Primitive2DReference
123         basegfx::B2DRange getB2DRangeFromPrimitive2DReference(const Primitive2DReference& rCandidate, const geometry::ViewInformation2D& aViewInformation)
124         {
125             basegfx::B2DRange aRetval;
126 
127             if(rCandidate.is())
128             {
129                 // try to get C++ implementation base
130                 const BasePrimitive2D* pCandidate(dynamic_cast< BasePrimitive2D* >(rCandidate.get()));
131 
132                 if(pCandidate)
133                 {
134                     // use it if possible
135                     aRetval.expand(pCandidate->getB2DRange(aViewInformation));
136                 }
137                 else
138                 {
139                     // use UNO API call instead
140                     const uno::Sequence< beans::PropertyValue >& rViewParameters(aViewInformation.getViewInformationSequence());
141                     aRetval.expand(basegfx::unotools::b2DRectangleFromRealRectangle2D(rCandidate->getRange(rViewParameters)));
142                 }
143             }
144 
145             return aRetval;
146         }
147 
148         // get B2DRange from a given Primitive2DSequence
149         basegfx::B2DRange getB2DRangeFromPrimitive2DSequence(const Primitive2DSequence& rCandidate, const geometry::ViewInformation2D& aViewInformation)
150         {
151             basegfx::B2DRange aRetval;
152 
153             if(rCandidate.hasElements())
154             {
155                 const sal_Int32 nCount(rCandidate.getLength());
156 
157                 for(sal_Int32 a(0L); a < nCount; a++)
158                 {
159                     aRetval.expand(getB2DRangeFromPrimitive2DReference(rCandidate[a], aViewInformation));
160                 }
161             }
162 
163             return aRetval;
164         }
165 
166         bool arePrimitive2DReferencesEqual(const Primitive2DReference& rxA, const Primitive2DReference& rxB)
167         {
168             const sal_Bool bAIs(rxA.is());
169 
170             if(bAIs != rxB.is())
171             {
172                 return false;
173             }
174 
175             if(!bAIs)
176             {
177                 return true;
178             }
179 
180             const BasePrimitive2D* pA(dynamic_cast< const BasePrimitive2D* >(rxA.get()));
181             const BasePrimitive2D* pB(dynamic_cast< const BasePrimitive2D* >(rxB.get()));
182             const bool bAEqualZero(pA == 0L);
183 
184             if(bAEqualZero != (pB == 0L))
185             {
186                 return false;
187             }
188 
189             if(bAEqualZero)
190             {
191                 return false;
192             }
193 
194             return (pA->operator==(*pB));
195         }
196 
197         bool arePrimitive2DSequencesEqual(const Primitive2DSequence& rA, const Primitive2DSequence& rB)
198         {
199             const sal_Bool bAHasElements(rA.hasElements());
200 
201             if(bAHasElements != rB.hasElements())
202             {
203                 return false;
204             }
205 
206             if(!bAHasElements)
207             {
208                 return true;
209             }
210 
211             const sal_Int32 nCount(rA.getLength());
212 
213             if(nCount != rB.getLength())
214             {
215                 return false;
216             }
217 
218             for(sal_Int32 a(0L); a < nCount; a++)
219             {
220                 if(!arePrimitive2DReferencesEqual(rA[a], rB[a]))
221                 {
222                     return false;
223                 }
224             }
225 
226             return true;
227         }
228 
229         // concatenate sequence
230         void appendPrimitive2DSequenceToPrimitive2DSequence(Primitive2DSequence& rDest, const Primitive2DSequence& rSource)
231         {
232             if(rSource.hasElements())
233             {
234                 if(rDest.hasElements())
235                 {
236                     const sal_Int32 nSourceCount(rSource.getLength());
237                     const sal_Int32 nDestCount(rDest.getLength());
238                     const sal_Int32 nTargetCount(nSourceCount + nDestCount);
239                     sal_Int32 nInsertPos(nDestCount);
240 
241                     rDest.realloc(nTargetCount);
242 
243                     for(sal_Int32 a(0L); a < nSourceCount; a++)
244                     {
245                         if(rSource[a].is())
246                         {
247                             rDest[nInsertPos++] = rSource[a];
248                         }
249                     }
250 
251                     if(nInsertPos != nTargetCount)
252                     {
253                         rDest.realloc(nInsertPos);
254                     }
255                 }
256                 else
257                 {
258                     rDest = rSource;
259                 }
260             }
261         }
262 
263         // concatenate single Primitive2D
264         void appendPrimitive2DReferenceToPrimitive2DSequence(Primitive2DSequence& rDest, const Primitive2DReference& rSource)
265         {
266             if(rSource.is())
267             {
268                 const sal_Int32 nDestCount(rDest.getLength());
269                 rDest.realloc(nDestCount + 1L);
270                 rDest[nDestCount] = rSource;
271             }
272         }
273 
274     } // end of namespace primitive2d
275 } // end of namespace drawinglayer
276 
277 //////////////////////////////////////////////////////////////////////////////
278 // eof
279