Update ooo320-m1
[ooovba.git] / basegfx / source / tools / unopolypolygon.cxx
blob03ebf7dafcd5d3081ef151ef71edbb6b22bf0134
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: unopolypolygon.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basegfx.hxx"
34 #include <com/sun/star/geometry/AffineMatrix2D.hpp>
35 #include <com/sun/star/rendering/RenderState.hpp>
36 #include <com/sun/star/rendering/ViewState.hpp>
37 #include <com/sun/star/rendering/XCanvas.hpp>
38 #include <com/sun/star/rendering/CompositeOperation.hpp>
40 #include <basegfx/matrix/b2dhommatrix.hxx>
41 #include <basegfx/range/b2drange.hxx>
42 #include <basegfx/range/b2drectangle.hxx>
43 #include <basegfx/point/b2dpoint.hxx>
44 #include <basegfx/tools/canvastools.hxx>
45 #include <basegfx/polygon/b2dpolygon.hxx>
46 #include <basegfx/polygon/b2dpolypolygontools.hxx>
48 #include <basegfx/tools/unopolypolygon.hxx>
51 using namespace ::com::sun::star;
53 namespace basegfx
55 namespace unotools
57 UnoPolyPolygon::UnoPolyPolygon( const B2DPolyPolygon& rPolyPoly ) :
58 UnoPolyPolygonBase( m_aMutex ),
59 maPolyPoly( rPolyPoly ),
60 meFillRule( rendering::FillRule_EVEN_ODD )
62 // or else races will haunt us.
63 maPolyPoly.makeUnique();
66 void SAL_CALL UnoPolyPolygon::addPolyPolygon(
67 const geometry::RealPoint2D& position,
68 const uno::Reference< rendering::XPolyPolygon2D >& polyPolygon ) throw (lang::IllegalArgumentException,uno::RuntimeException)
70 osl::MutexGuard const guard( m_aMutex );
71 modifying();
73 // TODO(F1): Correctly fulfill the UNO API
74 // specification. This will probably result in a vector of
75 // poly-polygons to be stored in this object.
77 const sal_Int32 nPolys( polyPolygon->getNumberOfPolygons() );
79 if( !polyPolygon.is() || !nPolys )
81 // invalid or empty polygon - nothing to do.
82 return;
85 B2DPolyPolygon aSrcPoly;
86 const UnoPolyPolygon* pSrc( dynamic_cast< UnoPolyPolygon* >(polyPolygon.get()) );
88 // try to extract polygon data from interface. First,
89 // check whether it's the same implementation object,
90 // which we can tunnel then.
91 if( pSrc )
93 aSrcPoly = pSrc->getPolyPolygon();
95 else
97 // not a known implementation object - try data source
98 // interfaces
99 uno::Reference< rendering::XBezierPolyPolygon2D > xBezierPoly(
100 polyPolygon,
101 uno::UNO_QUERY );
103 if( xBezierPoly.is() )
105 aSrcPoly = unotools::polyPolygonFromBezier2DSequenceSequence(
106 xBezierPoly->getBezierSegments( 0,
107 nPolys,
109 -1 ) );
111 else
113 uno::Reference< rendering::XLinePolyPolygon2D > xLinePoly(
114 polyPolygon,
115 uno::UNO_QUERY );
117 // no implementation class and no data provider
118 // found - contract violation.
119 if( !xLinePoly.is() )
120 throw lang::IllegalArgumentException(
121 ::rtl::OUString(
122 RTL_CONSTASCII_USTRINGPARAM(
123 "UnoPolyPolygon::addPolyPolygon(): Invalid input "
124 "poly-polygon, cannot retrieve vertex data")),
125 static_cast<cppu::OWeakObject*>(this), 1);
127 aSrcPoly = unotools::polyPolygonFromPoint2DSequenceSequence(
128 xLinePoly->getPoints( 0,
129 nPolys,
131 -1 ) );
135 const B2DRange aBounds( tools::getRange( aSrcPoly ) );
136 const B2DVector aOffset( unotools::b2DPointFromRealPoint2D( position ) -
137 aBounds.getMinimum() );
139 if( !aOffset.equalZero() )
141 B2DHomMatrix aTranslate;
142 aTranslate.translate( aOffset.getX(), aOffset.getY() );
144 aSrcPoly.transform( aTranslate );
147 maPolyPoly.append( aSrcPoly );
150 sal_Int32 SAL_CALL UnoPolyPolygon::getNumberOfPolygons() throw (uno::RuntimeException)
152 osl::MutexGuard const guard( m_aMutex );
153 return maPolyPoly.count();
156 sal_Int32 SAL_CALL UnoPolyPolygon::getNumberOfPolygonPoints(
157 sal_Int32 polygon ) throw (lang::IndexOutOfBoundsException,uno::RuntimeException)
159 osl::MutexGuard const guard( m_aMutex );
160 checkIndex( polygon );
162 return maPolyPoly.getB2DPolygon(polygon).count();
165 rendering::FillRule SAL_CALL UnoPolyPolygon::getFillRule() throw (uno::RuntimeException)
167 osl::MutexGuard const guard( m_aMutex );
168 return meFillRule;
171 void SAL_CALL UnoPolyPolygon::setFillRule(
172 rendering::FillRule fillRule ) throw (uno::RuntimeException)
174 osl::MutexGuard const guard( m_aMutex );
175 modifying();
177 meFillRule = fillRule;
180 sal_Bool SAL_CALL UnoPolyPolygon::isClosed(
181 sal_Int32 index ) throw (lang::IndexOutOfBoundsException,uno::RuntimeException)
183 osl::MutexGuard const guard( m_aMutex );
184 checkIndex( index );
186 return maPolyPoly.getB2DPolygon(index).isClosed();
189 void SAL_CALL UnoPolyPolygon::setClosed(
190 sal_Int32 index,
191 sal_Bool closedState ) throw (lang::IndexOutOfBoundsException,uno::RuntimeException)
193 osl::MutexGuard const guard( m_aMutex );
194 modifying();
196 if( index == -1L )
198 // set all
199 maPolyPoly.setClosed( closedState );
201 else
203 checkIndex( index );
205 // fetch referenced polygon, change state
206 B2DPolygon aTmp( maPolyPoly.getB2DPolygon(index) );
207 aTmp.setClosed( closedState );
209 // set back to container
210 maPolyPoly.setB2DPolygon( index, aTmp );
214 uno::Sequence< uno::Sequence< geometry::RealPoint2D > > SAL_CALL UnoPolyPolygon::getPoints(
215 sal_Int32 nPolygonIndex,
216 sal_Int32 nNumberOfPolygons,
217 sal_Int32 nPointIndex,
218 sal_Int32 nNumberOfPoints ) throw (lang::IndexOutOfBoundsException,uno::RuntimeException)
220 osl::MutexGuard const guard( m_aMutex );
222 return unotools::pointSequenceSequenceFromB2DPolyPolygon(
223 getSubsetPolyPolygon( nPolygonIndex,
224 nNumberOfPolygons,
225 nPointIndex,
226 nNumberOfPoints ) );
229 void SAL_CALL UnoPolyPolygon::setPoints(
230 const uno::Sequence< uno::Sequence< geometry::RealPoint2D > >& points,
231 sal_Int32 nPolygonIndex ) throw (lang::IndexOutOfBoundsException,uno::RuntimeException)
233 osl::MutexGuard const guard( m_aMutex );
234 modifying();
236 const B2DPolyPolygon& rNewPolyPoly(
237 unotools::polyPolygonFromPoint2DSequenceSequence( points ) );
239 if( nPolygonIndex == -1 )
241 maPolyPoly = rNewPolyPoly;
243 else
245 checkIndex( nPolygonIndex );
247 maPolyPoly.insert( nPolygonIndex, rNewPolyPoly );
251 geometry::RealPoint2D SAL_CALL UnoPolyPolygon::getPoint(
252 sal_Int32 nPolygonIndex,
253 sal_Int32 nPointIndex ) throw (lang::IndexOutOfBoundsException,uno::RuntimeException)
255 osl::MutexGuard const guard( m_aMutex );
256 checkIndex( nPolygonIndex );
258 const B2DPolygon& rPoly( maPolyPoly.getB2DPolygon( nPolygonIndex ) );
260 if( nPointIndex < 0 || nPointIndex >= static_cast<sal_Int32>(rPoly.count()) )
261 throw lang::IndexOutOfBoundsException();
263 return unotools::point2DFromB2DPoint( rPoly.getB2DPoint( nPointIndex ) );
266 void SAL_CALL UnoPolyPolygon::setPoint(
267 const geometry::RealPoint2D& point,
268 sal_Int32 nPolygonIndex,
269 sal_Int32 nPointIndex ) throw (lang::IndexOutOfBoundsException,uno::RuntimeException)
271 osl::MutexGuard const guard( m_aMutex );
272 checkIndex( nPolygonIndex );
273 modifying();
275 B2DPolygon aPoly( maPolyPoly.getB2DPolygon( nPolygonIndex ) );
277 if( nPointIndex < 0 || nPointIndex >= static_cast<sal_Int32>(aPoly.count()) )
278 throw lang::IndexOutOfBoundsException();
280 aPoly.setB2DPoint( nPointIndex,
281 unotools::b2DPointFromRealPoint2D( point ) );
282 maPolyPoly.setB2DPolygon( nPolygonIndex, aPoly );
285 uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > > SAL_CALL UnoPolyPolygon::getBezierSegments(
286 sal_Int32 nPolygonIndex,
287 sal_Int32 nNumberOfPolygons,
288 sal_Int32 nPointIndex,
289 sal_Int32 nNumberOfPoints ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
291 osl::MutexGuard const guard( m_aMutex );
292 return unotools::bezierSequenceSequenceFromB2DPolyPolygon(
293 getSubsetPolyPolygon( nPolygonIndex,
294 nNumberOfPolygons,
295 nPointIndex,
296 nNumberOfPoints ) );
299 void SAL_CALL UnoPolyPolygon::setBezierSegments(
300 const uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > >& points,
301 sal_Int32 nPolygonIndex ) throw (lang::IndexOutOfBoundsException,
302 uno::RuntimeException)
304 osl::MutexGuard const guard( m_aMutex );
305 modifying();
306 const B2DPolyPolygon& rNewPolyPoly(
307 unotools::polyPolygonFromBezier2DSequenceSequence( points ) );
309 if( nPolygonIndex == -1 )
311 maPolyPoly = rNewPolyPoly;
313 else
315 checkIndex( nPolygonIndex );
317 maPolyPoly.insert( nPolygonIndex, rNewPolyPoly );
321 geometry::RealBezierSegment2D SAL_CALL UnoPolyPolygon::getBezierSegment( sal_Int32 nPolygonIndex,
322 sal_Int32 nPointIndex ) throw (lang::IndexOutOfBoundsException,
323 uno::RuntimeException)
325 osl::MutexGuard const guard( m_aMutex );
326 checkIndex( nPolygonIndex );
328 const B2DPolygon& rPoly( maPolyPoly.getB2DPolygon( nPolygonIndex ) );
329 const sal_uInt32 nPointCount(rPoly.count());
331 if( nPointIndex < 0 || nPointIndex >= static_cast<sal_Int32>(nPointCount) )
332 throw lang::IndexOutOfBoundsException();
334 const B2DPoint& rPt( rPoly.getB2DPoint( nPointIndex ) );
335 const B2DPoint& rCtrl0( rPoly.getNextControlPoint(nPointIndex) );
336 const B2DPoint& rCtrl1( rPoly.getPrevControlPoint((nPointIndex + 1) % nPointCount) );
338 return geometry::RealBezierSegment2D( rPt.getX(),
339 rPt.getY(),
340 rCtrl0.getX(),
341 rCtrl0.getY(),
342 rCtrl1.getX(),
343 rCtrl1.getY() );
346 void SAL_CALL UnoPolyPolygon::setBezierSegment( const geometry::RealBezierSegment2D& segment,
347 sal_Int32 nPolygonIndex,
348 sal_Int32 nPointIndex ) throw (lang::IndexOutOfBoundsException,
349 uno::RuntimeException)
351 osl::MutexGuard const guard( m_aMutex );
352 checkIndex( nPolygonIndex );
353 modifying();
355 B2DPolygon aPoly( maPolyPoly.getB2DPolygon( nPolygonIndex ) );
356 const sal_uInt32 nPointCount(aPoly.count());
358 if( nPointIndex < 0 || nPointIndex >= static_cast<sal_Int32>(nPointCount) )
359 throw lang::IndexOutOfBoundsException();
361 aPoly.setB2DPoint( nPointIndex,
362 B2DPoint( segment.Px,
363 segment.Py ) );
364 aPoly.setNextControlPoint(nPointIndex,
365 B2DPoint(segment.C1x, segment.C1y));
366 aPoly.setPrevControlPoint((nPointIndex + 1) % nPointCount,
367 B2DPoint(segment.C2x, segment.C2y));
369 maPolyPoly.setB2DPolygon( nPolygonIndex, aPoly );
372 B2DPolyPolygon UnoPolyPolygon::getSubsetPolyPolygon(
373 sal_Int32 nPolygonIndex,
374 sal_Int32 nNumberOfPolygons,
375 sal_Int32 nPointIndex,
376 sal_Int32 nNumberOfPoints ) const
378 osl::MutexGuard const guard( m_aMutex );
379 checkIndex( nPolygonIndex );
381 const sal_Int32 nPolyCount( maPolyPoly.count() );
383 // check for "full polygon" case
384 if( !nPolygonIndex &&
385 !nPointIndex &&
386 nNumberOfPolygons == nPolyCount &&
387 nNumberOfPoints == -1 )
389 return maPolyPoly;
392 B2DPolyPolygon aSubsetPoly;
394 // create temporary polygon (as an extract from maPoly,
395 // which contains the requested subset)
396 for( sal_Int32 i=nPolygonIndex; i<nNumberOfPolygons; ++i )
398 checkIndex(i);
400 const B2DPolygon& rCurrPoly( maPolyPoly.getB2DPolygon(i) );
402 sal_Int32 nFirstPoint(0);
403 sal_Int32 nLastPoint(nPolyCount-1);
405 if( nPointIndex && i==nPolygonIndex )
407 // very first polygon - respect nPointIndex, if
408 // not zero
410 // empty polygon - impossible to specify _any_
411 // legal value except 0 here!
412 if( !nPolyCount && nPointIndex )
413 throw lang::IndexOutOfBoundsException();
415 nFirstPoint = nPointIndex;
418 if( i==nNumberOfPolygons-1 && nNumberOfPoints != -1 )
420 // very last polygon - respect nNumberOfPoints
422 // empty polygon - impossible to specify _any_
423 // legal value except -1 here!
424 if( !nPolyCount )
425 throw lang::IndexOutOfBoundsException();
427 nLastPoint = nFirstPoint+nNumberOfPoints;
430 if( !nPolyCount )
432 // empty polygon - index checks already performed
433 // above, now simply append empty polygon
434 aSubsetPoly.append( rCurrPoly );
436 else
438 if( nFirstPoint < 0 || nFirstPoint >= nPolyCount )
439 throw lang::IndexOutOfBoundsException();
441 if( nLastPoint < 0 || nLastPoint >= nPolyCount )
442 throw lang::IndexOutOfBoundsException();
444 B2DPolygon aTmp;
445 for( sal_Int32 j=nFirstPoint; j<nLastPoint; ++j )
446 aTmp.append( rCurrPoly.getB2DPoint(j) );
448 aSubsetPoly.append( aTmp );
452 return aSubsetPoly;
455 B2DPolyPolygon UnoPolyPolygon::getPolyPolygonUnsafe() const
457 return maPolyPoly;
460 #define IMPLEMENTATION_NAME "gfx::internal::UnoPolyPolygon"
461 #define SERVICE_NAME "com.sun.star.rendering.PolyPolygon2D"
462 ::rtl::OUString SAL_CALL UnoPolyPolygon::getImplementationName() throw( uno::RuntimeException )
464 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
467 sal_Bool SAL_CALL UnoPolyPolygon::supportsService( const ::rtl::OUString& ServiceName ) throw( uno::RuntimeException )
469 return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ) );
472 uno::Sequence< ::rtl::OUString > SAL_CALL UnoPolyPolygon::getSupportedServiceNames() throw( uno::RuntimeException )
474 uno::Sequence< ::rtl::OUString > aRet(1);
475 aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
477 return aRet;
480 B2DPolyPolygon UnoPolyPolygon::getPolyPolygon() const
482 osl::MutexGuard const guard( m_aMutex );
484 // detach result from us
485 B2DPolyPolygon aRet( maPolyPoly );
486 aRet.makeUnique();
487 return aRet;