Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / basegfx / source / tools / unopolypolygon.cxx
blobb0c1de7d563613b731162b1ab44d0248647d1b88
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/lang/IllegalArgumentException.hpp>
22 #include <basegfx/matrix/b2dhommatrix.hxx>
23 #include <basegfx/range/b2drange.hxx>
24 #include <basegfx/point/b2dpoint.hxx>
25 #include <basegfx/utils/canvastools.hxx>
26 #include <basegfx/polygon/b2dpolygon.hxx>
27 #include <basegfx/polygon/b2dpolypolygontools.hxx>
28 #include <basegfx/utils/unopolypolygon.hxx>
29 #include <basegfx/matrix/b2dhommatrixtools.hxx>
30 #include <cppuhelper/supportsservice.hxx>
32 using namespace ::com::sun::star;
34 namespace basegfx
36 namespace unotools
38 UnoPolyPolygon::UnoPolyPolygon( const B2DPolyPolygon& rPolyPoly ) :
39 UnoPolyPolygonBase( m_aMutex ),
40 maPolyPoly( rPolyPoly ),
41 meFillRule( rendering::FillRule_EVEN_ODD )
43 // or else races will haunt us.
44 maPolyPoly.makeUnique();
47 void SAL_CALL UnoPolyPolygon::addPolyPolygon(
48 const geometry::RealPoint2D& position,
49 const uno::Reference< rendering::XPolyPolygon2D >& polyPolygon )
51 osl::MutexGuard const guard( m_aMutex );
52 modifying();
54 // TODO(F1): Correctly fulfill the UNO API
55 // specification. This will probably result in a vector of
56 // poly-polygons to be stored in this object.
58 const sal_Int32 nPolys( polyPolygon->getNumberOfPolygons() );
60 if( !polyPolygon.is() || !nPolys )
62 // invalid or empty polygon - nothing to do.
63 return;
66 B2DPolyPolygon aSrcPoly;
67 const UnoPolyPolygon* pSrc( dynamic_cast< UnoPolyPolygon* >(polyPolygon.get()) );
69 // try to extract polygon data from interface. First,
70 // check whether it's the same implementation object,
71 // which we can tunnel then.
72 if( pSrc )
74 aSrcPoly = pSrc->getPolyPolygon();
76 else
78 // not a known implementation object - try data source
79 // interfaces
80 uno::Reference< rendering::XBezierPolyPolygon2D > xBezierPoly(
81 polyPolygon,
82 uno::UNO_QUERY );
84 if( xBezierPoly.is() )
86 aSrcPoly = unotools::polyPolygonFromBezier2DSequenceSequence(
87 xBezierPoly->getBezierSegments( 0,
88 nPolys,
90 -1 ) );
92 else
94 uno::Reference< rendering::XLinePolyPolygon2D > xLinePoly(
95 polyPolygon,
96 uno::UNO_QUERY );
98 // no implementation class and no data provider
99 // found - contract violation.
100 if( !xLinePoly.is() )
101 throw lang::IllegalArgumentException(
102 "UnoPolyPolygon::addPolyPolygon(): Invalid input "
103 "poly-polygon, cannot retrieve vertex data",
104 static_cast<cppu::OWeakObject*>(this), 1);
106 aSrcPoly = unotools::polyPolygonFromPoint2DSequenceSequence(
107 xLinePoly->getPoints( 0,
108 nPolys,
110 -1 ) );
114 const B2DRange aBounds( utils::getRange( aSrcPoly ) );
115 const B2DVector aOffset( unotools::b2DPointFromRealPoint2D( position ) -
116 aBounds.getMinimum() );
118 if( !aOffset.equalZero() )
120 const B2DHomMatrix aTranslate(utils::createTranslateB2DHomMatrix(aOffset));
121 aSrcPoly.transform( aTranslate );
124 maPolyPoly.append( aSrcPoly );
127 sal_Int32 SAL_CALL UnoPolyPolygon::getNumberOfPolygons()
129 osl::MutexGuard const guard( m_aMutex );
130 return maPolyPoly.count();
133 sal_Int32 SAL_CALL UnoPolyPolygon::getNumberOfPolygonPoints(
134 sal_Int32 polygon )
136 osl::MutexGuard const guard( m_aMutex );
137 checkIndex( polygon );
139 return maPolyPoly.getB2DPolygon(polygon).count();
142 rendering::FillRule SAL_CALL UnoPolyPolygon::getFillRule()
144 osl::MutexGuard const guard( m_aMutex );
145 return meFillRule;
148 void SAL_CALL UnoPolyPolygon::setFillRule(
149 rendering::FillRule fillRule )
151 osl::MutexGuard const guard( m_aMutex );
152 modifying();
154 meFillRule = fillRule;
157 sal_Bool SAL_CALL UnoPolyPolygon::isClosed(
158 sal_Int32 index )
160 osl::MutexGuard const guard( m_aMutex );
161 checkIndex( index );
163 return maPolyPoly.getB2DPolygon(index).isClosed();
166 void SAL_CALL UnoPolyPolygon::setClosed(
167 sal_Int32 index,
168 sal_Bool closedState )
170 osl::MutexGuard const guard( m_aMutex );
171 modifying();
173 if( index == -1 )
175 // set all
176 maPolyPoly.setClosed( closedState );
178 else
180 checkIndex( index );
182 // fetch referenced polygon, change state
183 B2DPolygon aTmp( maPolyPoly.getB2DPolygon(index) );
184 aTmp.setClosed( closedState );
186 // set back to container
187 maPolyPoly.setB2DPolygon( index, aTmp );
191 uno::Sequence< uno::Sequence< geometry::RealPoint2D > > SAL_CALL UnoPolyPolygon::getPoints(
192 sal_Int32 nPolygonIndex,
193 sal_Int32 nNumberOfPolygons,
194 sal_Int32 nPointIndex,
195 sal_Int32 nNumberOfPoints )
197 osl::MutexGuard const guard( m_aMutex );
199 return unotools::pointSequenceSequenceFromB2DPolyPolygon(
200 getSubsetPolyPolygon( nPolygonIndex,
201 nNumberOfPolygons,
202 nPointIndex,
203 nNumberOfPoints ) );
206 void SAL_CALL UnoPolyPolygon::setPoints(
207 const uno::Sequence< uno::Sequence< geometry::RealPoint2D > >& points,
208 sal_Int32 nPolygonIndex )
210 osl::MutexGuard const guard( m_aMutex );
211 modifying();
213 const B2DPolyPolygon& rNewPolyPoly(
214 unotools::polyPolygonFromPoint2DSequenceSequence( points ) );
216 if( nPolygonIndex == -1 )
218 maPolyPoly = rNewPolyPoly;
220 else
222 checkIndex( nPolygonIndex );
224 maPolyPoly.insert( nPolygonIndex, rNewPolyPoly );
228 geometry::RealPoint2D SAL_CALL UnoPolyPolygon::getPoint(
229 sal_Int32 nPolygonIndex,
230 sal_Int32 nPointIndex )
232 osl::MutexGuard const guard( m_aMutex );
233 checkIndex( nPolygonIndex );
235 const B2DPolygon& rPoly( maPolyPoly.getB2DPolygon( nPolygonIndex ) );
237 if( nPointIndex < 0 || nPointIndex >= static_cast<sal_Int32>(rPoly.count()) )
238 throw lang::IndexOutOfBoundsException();
240 return unotools::point2DFromB2DPoint( rPoly.getB2DPoint( nPointIndex ) );
243 void SAL_CALL UnoPolyPolygon::setPoint(
244 const geometry::RealPoint2D& point,
245 sal_Int32 nPolygonIndex,
246 sal_Int32 nPointIndex )
248 osl::MutexGuard const guard( m_aMutex );
249 checkIndex( nPolygonIndex );
250 modifying();
252 B2DPolygon aPoly( maPolyPoly.getB2DPolygon( nPolygonIndex ) );
254 if( nPointIndex < 0 || nPointIndex >= static_cast<sal_Int32>(aPoly.count()) )
255 throw lang::IndexOutOfBoundsException();
257 aPoly.setB2DPoint( nPointIndex,
258 unotools::b2DPointFromRealPoint2D( point ) );
259 maPolyPoly.setB2DPolygon( nPolygonIndex, aPoly );
262 uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > > SAL_CALL UnoPolyPolygon::getBezierSegments(
263 sal_Int32 nPolygonIndex,
264 sal_Int32 nNumberOfPolygons,
265 sal_Int32 nPointIndex,
266 sal_Int32 nNumberOfPoints )
268 osl::MutexGuard const guard( m_aMutex );
269 return unotools::bezierSequenceSequenceFromB2DPolyPolygon(
270 getSubsetPolyPolygon( nPolygonIndex,
271 nNumberOfPolygons,
272 nPointIndex,
273 nNumberOfPoints ) );
276 void SAL_CALL UnoPolyPolygon::setBezierSegments(
277 const uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > >& points,
278 sal_Int32 nPolygonIndex )
280 osl::MutexGuard const guard( m_aMutex );
281 modifying();
282 const B2DPolyPolygon& rNewPolyPoly(
283 unotools::polyPolygonFromBezier2DSequenceSequence( points ) );
285 if( nPolygonIndex == -1 )
287 maPolyPoly = rNewPolyPoly;
289 else
291 checkIndex( nPolygonIndex );
293 maPolyPoly.insert( nPolygonIndex, rNewPolyPoly );
297 geometry::RealBezierSegment2D SAL_CALL UnoPolyPolygon::getBezierSegment( sal_Int32 nPolygonIndex,
298 sal_Int32 nPointIndex )
300 osl::MutexGuard const guard( m_aMutex );
301 checkIndex( nPolygonIndex );
303 const B2DPolygon& rPoly( maPolyPoly.getB2DPolygon( nPolygonIndex ) );
304 const sal_uInt32 nPointCount(rPoly.count());
306 if( nPointIndex < 0 || nPointIndex >= static_cast<sal_Int32>(nPointCount) )
307 throw lang::IndexOutOfBoundsException();
309 const B2DPoint& rPt( rPoly.getB2DPoint( nPointIndex ) );
310 const B2DPoint& rCtrl0( rPoly.getNextControlPoint(nPointIndex) );
311 const B2DPoint& rCtrl1( rPoly.getPrevControlPoint((nPointIndex + 1) % nPointCount) );
313 return geometry::RealBezierSegment2D( rPt.getX(),
314 rPt.getY(),
315 rCtrl0.getX(),
316 rCtrl0.getY(),
317 rCtrl1.getX(),
318 rCtrl1.getY() );
321 void SAL_CALL UnoPolyPolygon::setBezierSegment( const geometry::RealBezierSegment2D& segment,
322 sal_Int32 nPolygonIndex,
323 sal_Int32 nPointIndex )
325 osl::MutexGuard const guard( m_aMutex );
326 checkIndex( nPolygonIndex );
327 modifying();
329 B2DPolygon aPoly( maPolyPoly.getB2DPolygon( nPolygonIndex ) );
330 const sal_uInt32 nPointCount(aPoly.count());
332 if( nPointIndex < 0 || nPointIndex >= static_cast<sal_Int32>(nPointCount) )
333 throw lang::IndexOutOfBoundsException();
335 aPoly.setB2DPoint( nPointIndex,
336 B2DPoint( segment.Px,
337 segment.Py ) );
338 aPoly.setNextControlPoint(nPointIndex,
339 B2DPoint(segment.C1x, segment.C1y));
340 aPoly.setPrevControlPoint((nPointIndex + 1) % nPointCount,
341 B2DPoint(segment.C2x, segment.C2y));
343 maPolyPoly.setB2DPolygon( nPolygonIndex, aPoly );
346 B2DPolyPolygon UnoPolyPolygon::getSubsetPolyPolygon(
347 sal_Int32 nPolygonIndex,
348 sal_Int32 nNumberOfPolygons,
349 sal_Int32 nPointIndex,
350 sal_Int32 nNumberOfPoints ) const
352 osl::MutexGuard const guard( m_aMutex );
353 checkIndex( nPolygonIndex );
355 const sal_Int32 nPolyCount( maPolyPoly.count() );
357 // check for "full polygon" case
358 if( !nPolygonIndex &&
359 !nPointIndex &&
360 nNumberOfPolygons == nPolyCount &&
361 nNumberOfPoints == -1 )
363 return maPolyPoly;
366 B2DPolyPolygon aSubsetPoly;
368 // create temporary polygon (as an extract from maPoly,
369 // which contains the requested subset)
370 for( sal_Int32 i=nPolygonIndex; i<nNumberOfPolygons; ++i )
372 checkIndex(i);
374 const B2DPolygon& rCurrPoly( maPolyPoly.getB2DPolygon(i) );
376 sal_Int32 nFirstPoint(0);
377 sal_Int32 nLastPoint(nPolyCount-1);
379 if( nPointIndex && i==nPolygonIndex )
381 // very first polygon - respect nPointIndex, if
382 // not zero
384 // empty polygon - impossible to specify _any_
385 // legal value except 0 here!
386 if( !nPolyCount)
387 throw lang::IndexOutOfBoundsException();
389 nFirstPoint = nPointIndex;
392 if( i==nNumberOfPolygons-1 && nNumberOfPoints != -1 )
394 // very last polygon - respect nNumberOfPoints
396 // empty polygon - impossible to specify _any_
397 // legal value except -1 here!
398 if( !nPolyCount )
399 throw lang::IndexOutOfBoundsException();
401 nLastPoint = nFirstPoint+nNumberOfPoints;
404 if( !nPolyCount )
406 // empty polygon - index checks already performed
407 // above, now simply append empty polygon
408 aSubsetPoly.append( rCurrPoly );
410 else
412 if( nFirstPoint < 0 || nFirstPoint >= nPolyCount )
413 throw lang::IndexOutOfBoundsException();
415 if( nLastPoint < 0 || nLastPoint >= nPolyCount )
416 throw lang::IndexOutOfBoundsException();
418 B2DPolygon aTmp;
419 for( sal_Int32 j=nFirstPoint; j<nLastPoint; ++j )
420 aTmp.append( rCurrPoly.getB2DPoint(j) );
422 aSubsetPoly.append( aTmp );
426 return aSubsetPoly;
429 #define IMPLEMENTATION_NAME "gfx::internal::UnoPolyPolygon"
430 #define SERVICE_NAME "com.sun.star.rendering.PolyPolygon2D"
431 OUString SAL_CALL UnoPolyPolygon::getImplementationName()
433 return IMPLEMENTATION_NAME;
436 sal_Bool SAL_CALL UnoPolyPolygon::supportsService( const OUString& ServiceName )
438 return cppu::supportsService(this, ServiceName);
441 uno::Sequence< OUString > SAL_CALL UnoPolyPolygon::getSupportedServiceNames()
443 return { SERVICE_NAME };
446 B2DPolyPolygon UnoPolyPolygon::getPolyPolygon() const
448 osl::MutexGuard const guard( m_aMutex );
450 // detach result from us
451 B2DPolyPolygon aRet( maPolyPoly );
452 aRet.makeUnique();
453 return aRet;
459 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */