Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / basegfx / source / tools / unopolypolygon.cxx
blobaa93b41428ffe0c4c926be9565b6295d344ef6b6
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 <com/sun/star/geometry/AffineMatrix2D.hpp>
21 #include <com/sun/star/rendering/RenderState.hpp>
22 #include <com/sun/star/rendering/ViewState.hpp>
23 #include <com/sun/star/rendering/XCanvas.hpp>
24 #include <com/sun/star/rendering/CompositeOperation.hpp>
26 #include <basegfx/matrix/b2dhommatrix.hxx>
27 #include <basegfx/range/b2drange.hxx>
28 #include <basegfx/range/b2drectangle.hxx>
29 #include <basegfx/point/b2dpoint.hxx>
30 #include <basegfx/tools/canvastools.hxx>
31 #include <basegfx/polygon/b2dpolygon.hxx>
32 #include <basegfx/polygon/b2dpolypolygontools.hxx>
33 #include <basegfx/tools/unopolypolygon.hxx>
34 #include <basegfx/matrix/b2dhommatrixtools.hxx>
37 using namespace ::com::sun::star;
39 namespace basegfx
41 namespace unotools
43 UnoPolyPolygon::UnoPolyPolygon( const B2DPolyPolygon& rPolyPoly ) :
44 UnoPolyPolygonBase( m_aMutex ),
45 maPolyPoly( rPolyPoly ),
46 meFillRule( rendering::FillRule_EVEN_ODD )
48 // or else races will haunt us.
49 maPolyPoly.makeUnique();
52 void SAL_CALL UnoPolyPolygon::addPolyPolygon(
53 const geometry::RealPoint2D& position,
54 const uno::Reference< rendering::XPolyPolygon2D >& polyPolygon ) throw (lang::IllegalArgumentException,uno::RuntimeException)
56 osl::MutexGuard const guard( m_aMutex );
57 modifying();
59 // TODO(F1): Correctly fulfill the UNO API
60 // specification. This will probably result in a vector of
61 // poly-polygons to be stored in this object.
63 const sal_Int32 nPolys( polyPolygon->getNumberOfPolygons() );
65 if( !polyPolygon.is() || !nPolys )
67 // invalid or empty polygon - nothing to do.
68 return;
71 B2DPolyPolygon aSrcPoly;
72 const UnoPolyPolygon* pSrc( dynamic_cast< UnoPolyPolygon* >(polyPolygon.get()) );
74 // try to extract polygon data from interface. First,
75 // check whether it's the same implementation object,
76 // which we can tunnel then.
77 if( pSrc )
79 aSrcPoly = pSrc->getPolyPolygon();
81 else
83 // not a known implementation object - try data source
84 // interfaces
85 uno::Reference< rendering::XBezierPolyPolygon2D > xBezierPoly(
86 polyPolygon,
87 uno::UNO_QUERY );
89 if( xBezierPoly.is() )
91 aSrcPoly = unotools::polyPolygonFromBezier2DSequenceSequence(
92 xBezierPoly->getBezierSegments( 0,
93 nPolys,
95 -1 ) );
97 else
99 uno::Reference< rendering::XLinePolyPolygon2D > xLinePoly(
100 polyPolygon,
101 uno::UNO_QUERY );
103 // no implementation class and no data provider
104 // found - contract violation.
105 if( !xLinePoly.is() )
106 throw lang::IllegalArgumentException(
107 "UnoPolyPolygon::addPolyPolygon(): Invalid input "
108 "poly-polygon, cannot retrieve vertex data",
109 static_cast<cppu::OWeakObject*>(this), 1);
111 aSrcPoly = unotools::polyPolygonFromPoint2DSequenceSequence(
112 xLinePoly->getPoints( 0,
113 nPolys,
115 -1 ) );
119 const B2DRange aBounds( tools::getRange( aSrcPoly ) );
120 const B2DVector aOffset( unotools::b2DPointFromRealPoint2D( position ) -
121 aBounds.getMinimum() );
123 if( !aOffset.equalZero() )
125 const B2DHomMatrix aTranslate(tools::createTranslateB2DHomMatrix(aOffset));
126 aSrcPoly.transform( aTranslate );
129 maPolyPoly.append( aSrcPoly );
132 sal_Int32 SAL_CALL UnoPolyPolygon::getNumberOfPolygons() throw (uno::RuntimeException)
134 osl::MutexGuard const guard( m_aMutex );
135 return maPolyPoly.count();
138 sal_Int32 SAL_CALL UnoPolyPolygon::getNumberOfPolygonPoints(
139 sal_Int32 polygon ) throw (lang::IndexOutOfBoundsException,uno::RuntimeException)
141 osl::MutexGuard const guard( m_aMutex );
142 checkIndex( polygon );
144 return maPolyPoly.getB2DPolygon(polygon).count();
147 rendering::FillRule SAL_CALL UnoPolyPolygon::getFillRule() throw (uno::RuntimeException)
149 osl::MutexGuard const guard( m_aMutex );
150 return meFillRule;
153 void SAL_CALL UnoPolyPolygon::setFillRule(
154 rendering::FillRule fillRule ) throw (uno::RuntimeException)
156 osl::MutexGuard const guard( m_aMutex );
157 modifying();
159 meFillRule = fillRule;
162 sal_Bool SAL_CALL UnoPolyPolygon::isClosed(
163 sal_Int32 index ) throw (lang::IndexOutOfBoundsException,uno::RuntimeException)
165 osl::MutexGuard const guard( m_aMutex );
166 checkIndex( index );
168 return maPolyPoly.getB2DPolygon(index).isClosed();
171 void SAL_CALL UnoPolyPolygon::setClosed(
172 sal_Int32 index,
173 sal_Bool closedState ) throw (lang::IndexOutOfBoundsException,uno::RuntimeException)
175 osl::MutexGuard const guard( m_aMutex );
176 modifying();
178 if( index == -1L )
180 // set all
181 maPolyPoly.setClosed( closedState );
183 else
185 checkIndex( index );
187 // fetch referenced polygon, change state
188 B2DPolygon aTmp( maPolyPoly.getB2DPolygon(index) );
189 aTmp.setClosed( closedState );
191 // set back to container
192 maPolyPoly.setB2DPolygon( index, aTmp );
196 uno::Sequence< uno::Sequence< geometry::RealPoint2D > > SAL_CALL UnoPolyPolygon::getPoints(
197 sal_Int32 nPolygonIndex,
198 sal_Int32 nNumberOfPolygons,
199 sal_Int32 nPointIndex,
200 sal_Int32 nNumberOfPoints ) throw (lang::IndexOutOfBoundsException,uno::RuntimeException)
202 osl::MutexGuard const guard( m_aMutex );
204 return unotools::pointSequenceSequenceFromB2DPolyPolygon(
205 getSubsetPolyPolygon( nPolygonIndex,
206 nNumberOfPolygons,
207 nPointIndex,
208 nNumberOfPoints ) );
211 void SAL_CALL UnoPolyPolygon::setPoints(
212 const uno::Sequence< uno::Sequence< geometry::RealPoint2D > >& points,
213 sal_Int32 nPolygonIndex ) throw (lang::IndexOutOfBoundsException,uno::RuntimeException)
215 osl::MutexGuard const guard( m_aMutex );
216 modifying();
218 const B2DPolyPolygon& rNewPolyPoly(
219 unotools::polyPolygonFromPoint2DSequenceSequence( points ) );
221 if( nPolygonIndex == -1 )
223 maPolyPoly = rNewPolyPoly;
225 else
227 checkIndex( nPolygonIndex );
229 maPolyPoly.insert( nPolygonIndex, rNewPolyPoly );
233 geometry::RealPoint2D SAL_CALL UnoPolyPolygon::getPoint(
234 sal_Int32 nPolygonIndex,
235 sal_Int32 nPointIndex ) throw (lang::IndexOutOfBoundsException,uno::RuntimeException)
237 osl::MutexGuard const guard( m_aMutex );
238 checkIndex( nPolygonIndex );
240 const B2DPolygon& rPoly( maPolyPoly.getB2DPolygon( nPolygonIndex ) );
242 if( nPointIndex < 0 || nPointIndex >= static_cast<sal_Int32>(rPoly.count()) )
243 throw lang::IndexOutOfBoundsException();
245 return unotools::point2DFromB2DPoint( rPoly.getB2DPoint( nPointIndex ) );
248 void SAL_CALL UnoPolyPolygon::setPoint(
249 const geometry::RealPoint2D& point,
250 sal_Int32 nPolygonIndex,
251 sal_Int32 nPointIndex ) throw (lang::IndexOutOfBoundsException,uno::RuntimeException)
253 osl::MutexGuard const guard( m_aMutex );
254 checkIndex( nPolygonIndex );
255 modifying();
257 B2DPolygon aPoly( maPolyPoly.getB2DPolygon( nPolygonIndex ) );
259 if( nPointIndex < 0 || nPointIndex >= static_cast<sal_Int32>(aPoly.count()) )
260 throw lang::IndexOutOfBoundsException();
262 aPoly.setB2DPoint( nPointIndex,
263 unotools::b2DPointFromRealPoint2D( point ) );
264 maPolyPoly.setB2DPolygon( nPolygonIndex, aPoly );
267 uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > > SAL_CALL UnoPolyPolygon::getBezierSegments(
268 sal_Int32 nPolygonIndex,
269 sal_Int32 nNumberOfPolygons,
270 sal_Int32 nPointIndex,
271 sal_Int32 nNumberOfPoints ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
273 osl::MutexGuard const guard( m_aMutex );
274 return unotools::bezierSequenceSequenceFromB2DPolyPolygon(
275 getSubsetPolyPolygon( nPolygonIndex,
276 nNumberOfPolygons,
277 nPointIndex,
278 nNumberOfPoints ) );
281 void SAL_CALL UnoPolyPolygon::setBezierSegments(
282 const uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > >& points,
283 sal_Int32 nPolygonIndex ) throw (lang::IndexOutOfBoundsException,
284 uno::RuntimeException)
286 osl::MutexGuard const guard( m_aMutex );
287 modifying();
288 const B2DPolyPolygon& rNewPolyPoly(
289 unotools::polyPolygonFromBezier2DSequenceSequence( points ) );
291 if( nPolygonIndex == -1 )
293 maPolyPoly = rNewPolyPoly;
295 else
297 checkIndex( nPolygonIndex );
299 maPolyPoly.insert( nPolygonIndex, rNewPolyPoly );
303 geometry::RealBezierSegment2D SAL_CALL UnoPolyPolygon::getBezierSegment( sal_Int32 nPolygonIndex,
304 sal_Int32 nPointIndex ) throw (lang::IndexOutOfBoundsException,
305 uno::RuntimeException)
307 osl::MutexGuard const guard( m_aMutex );
308 checkIndex( nPolygonIndex );
310 const B2DPolygon& rPoly( maPolyPoly.getB2DPolygon( nPolygonIndex ) );
311 const sal_uInt32 nPointCount(rPoly.count());
313 if( nPointIndex < 0 || nPointIndex >= static_cast<sal_Int32>(nPointCount) )
314 throw lang::IndexOutOfBoundsException();
316 const B2DPoint& rPt( rPoly.getB2DPoint( nPointIndex ) );
317 const B2DPoint& rCtrl0( rPoly.getNextControlPoint(nPointIndex) );
318 const B2DPoint& rCtrl1( rPoly.getPrevControlPoint((nPointIndex + 1) % nPointCount) );
320 return geometry::RealBezierSegment2D( rPt.getX(),
321 rPt.getY(),
322 rCtrl0.getX(),
323 rCtrl0.getY(),
324 rCtrl1.getX(),
325 rCtrl1.getY() );
328 void SAL_CALL UnoPolyPolygon::setBezierSegment( const geometry::RealBezierSegment2D& segment,
329 sal_Int32 nPolygonIndex,
330 sal_Int32 nPointIndex ) throw (lang::IndexOutOfBoundsException,
331 uno::RuntimeException)
333 osl::MutexGuard const guard( m_aMutex );
334 checkIndex( nPolygonIndex );
335 modifying();
337 B2DPolygon aPoly( maPolyPoly.getB2DPolygon( nPolygonIndex ) );
338 const sal_uInt32 nPointCount(aPoly.count());
340 if( nPointIndex < 0 || nPointIndex >= static_cast<sal_Int32>(nPointCount) )
341 throw lang::IndexOutOfBoundsException();
343 aPoly.setB2DPoint( nPointIndex,
344 B2DPoint( segment.Px,
345 segment.Py ) );
346 aPoly.setNextControlPoint(nPointIndex,
347 B2DPoint(segment.C1x, segment.C1y));
348 aPoly.setPrevControlPoint((nPointIndex + 1) % nPointCount,
349 B2DPoint(segment.C2x, segment.C2y));
351 maPolyPoly.setB2DPolygon( nPolygonIndex, aPoly );
354 B2DPolyPolygon UnoPolyPolygon::getSubsetPolyPolygon(
355 sal_Int32 nPolygonIndex,
356 sal_Int32 nNumberOfPolygons,
357 sal_Int32 nPointIndex,
358 sal_Int32 nNumberOfPoints ) const
360 osl::MutexGuard const guard( m_aMutex );
361 checkIndex( nPolygonIndex );
363 const sal_Int32 nPolyCount( maPolyPoly.count() );
365 // check for "full polygon" case
366 if( !nPolygonIndex &&
367 !nPointIndex &&
368 nNumberOfPolygons == nPolyCount &&
369 nNumberOfPoints == -1 )
371 return maPolyPoly;
374 B2DPolyPolygon aSubsetPoly;
376 // create temporary polygon (as an extract from maPoly,
377 // which contains the requested subset)
378 for( sal_Int32 i=nPolygonIndex; i<nNumberOfPolygons; ++i )
380 checkIndex(i);
382 const B2DPolygon& rCurrPoly( maPolyPoly.getB2DPolygon(i) );
384 sal_Int32 nFirstPoint(0);
385 sal_Int32 nLastPoint(nPolyCount-1);
387 if( nPointIndex && i==nPolygonIndex )
389 // very first polygon - respect nPointIndex, if
390 // not zero
392 // empty polygon - impossible to specify _any_
393 // legal value except 0 here!
394 if( !nPolyCount && nPointIndex )
395 throw lang::IndexOutOfBoundsException();
397 nFirstPoint = nPointIndex;
400 if( i==nNumberOfPolygons-1 && nNumberOfPoints != -1 )
402 // very last polygon - respect nNumberOfPoints
404 // empty polygon - impossible to specify _any_
405 // legal value except -1 here!
406 if( !nPolyCount )
407 throw lang::IndexOutOfBoundsException();
409 nLastPoint = nFirstPoint+nNumberOfPoints;
412 if( !nPolyCount )
414 // empty polygon - index checks already performed
415 // above, now simply append empty polygon
416 aSubsetPoly.append( rCurrPoly );
418 else
420 if( nFirstPoint < 0 || nFirstPoint >= nPolyCount )
421 throw lang::IndexOutOfBoundsException();
423 if( nLastPoint < 0 || nLastPoint >= nPolyCount )
424 throw lang::IndexOutOfBoundsException();
426 B2DPolygon aTmp;
427 for( sal_Int32 j=nFirstPoint; j<nLastPoint; ++j )
428 aTmp.append( rCurrPoly.getB2DPoint(j) );
430 aSubsetPoly.append( aTmp );
434 return aSubsetPoly;
437 #define IMPLEMENTATION_NAME "gfx::internal::UnoPolyPolygon"
438 #define SERVICE_NAME "com.sun.star.rendering.PolyPolygon2D"
439 OUString SAL_CALL UnoPolyPolygon::getImplementationName() throw( uno::RuntimeException )
441 return OUString( IMPLEMENTATION_NAME );
444 sal_Bool SAL_CALL UnoPolyPolygon::supportsService( const OUString& ServiceName ) throw( uno::RuntimeException )
446 return ServiceName == SERVICE_NAME;
449 uno::Sequence< OUString > SAL_CALL UnoPolyPolygon::getSupportedServiceNames() throw( uno::RuntimeException )
451 uno::Sequence< OUString > aRet(1);
452 aRet[0] = SERVICE_NAME ;
454 return aRet;
457 B2DPolyPolygon UnoPolyPolygon::getPolyPolygon() const
459 osl::MutexGuard const guard( m_aMutex );
461 // detach result from us
462 B2DPolyPolygon aRet( maPolyPoly );
463 aRet.makeUnique();
464 return aRet;
470 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */