Version 24.2.2.2, tag libreoffice-24.2.2.2
[LibreOffice.git] / basegfx / source / tools / unopolypolygon.cxx
blob41f1497319651ff8bf5c4353bb25b0d4cf8c142f
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>
31 #include <utility>
33 using namespace ::com::sun::star;
35 namespace basegfx::unotools
37 UnoPolyPolygon::UnoPolyPolygon( B2DPolyPolygon aPolyPoly ) :
38 maPolyPoly(std::move( aPolyPoly )),
39 meFillRule( rendering::FillRule_EVEN_ODD )
43 void SAL_CALL UnoPolyPolygon::addPolyPolygon(
44 const geometry::RealPoint2D& position,
45 const uno::Reference< rendering::XPolyPolygon2D >& polyPolygon )
47 std::unique_lock const guard( m_aMutex );
48 modifying();
50 // TODO(F1): Correctly fulfill the UNO API
51 // specification. This will probably result in a vector of
52 // poly-polygons to be stored in this object.
54 const sal_Int32 nPolys( polyPolygon->getNumberOfPolygons() );
56 if( !polyPolygon.is() || !nPolys )
58 // invalid or empty polygon - nothing to do.
59 return;
62 B2DPolyPolygon aSrcPoly;
63 const UnoPolyPolygon* pSrc( dynamic_cast< UnoPolyPolygon* >(polyPolygon.get()) );
65 // try to extract polygon data from interface. First,
66 // check whether it's the same implementation object,
67 // which we can tunnel then.
68 if( pSrc )
70 aSrcPoly = pSrc->getPolyPolygon();
72 else
74 // not a known implementation object - try data source
75 // interfaces
76 uno::Reference< rendering::XBezierPolyPolygon2D > xBezierPoly(
77 polyPolygon,
78 uno::UNO_QUERY );
80 if( xBezierPoly.is() )
82 aSrcPoly = unotools::polyPolygonFromBezier2DSequenceSequence(
83 xBezierPoly->getBezierSegments( 0,
84 nPolys,
86 -1 ) );
88 else
90 uno::Reference< rendering::XLinePolyPolygon2D > xLinePoly(
91 polyPolygon,
92 uno::UNO_QUERY );
94 // no implementation class and no data provider
95 // found - contract violation.
96 if( !xLinePoly.is() )
97 throw lang::IllegalArgumentException(
98 "UnoPolyPolygon::addPolyPolygon(): Invalid input "
99 "poly-polygon, cannot retrieve vertex data",
100 getXWeak(), 1);
102 aSrcPoly = unotools::polyPolygonFromPoint2DSequenceSequence(
103 xLinePoly->getPoints( 0,
104 nPolys,
106 -1 ) );
110 const B2DRange aBounds( utils::getRange( aSrcPoly ) );
111 const B2DVector aOffset( unotools::b2DPointFromRealPoint2D( position ) -
112 aBounds.getMinimum() );
114 if( !aOffset.equalZero() )
116 const B2DHomMatrix aTranslate(utils::createTranslateB2DHomMatrix(aOffset));
117 aSrcPoly.transform( aTranslate );
120 maPolyPoly.append( aSrcPoly );
123 sal_Int32 SAL_CALL UnoPolyPolygon::getNumberOfPolygons()
125 std::unique_lock const guard( m_aMutex );
126 return maPolyPoly.count();
129 sal_Int32 SAL_CALL UnoPolyPolygon::getNumberOfPolygonPoints(
130 sal_Int32 polygon )
132 std::unique_lock const guard( m_aMutex );
133 checkIndex( polygon );
135 return maPolyPoly.getB2DPolygon(polygon).count();
138 rendering::FillRule SAL_CALL UnoPolyPolygon::getFillRule()
140 std::unique_lock const guard( m_aMutex );
141 return meFillRule;
144 void SAL_CALL UnoPolyPolygon::setFillRule(
145 rendering::FillRule fillRule )
147 std::unique_lock const guard( m_aMutex );
148 modifying();
150 meFillRule = fillRule;
153 sal_Bool SAL_CALL UnoPolyPolygon::isClosed(
154 sal_Int32 index )
156 std::unique_lock const guard( m_aMutex );
157 checkIndex( index );
159 return maPolyPoly.getB2DPolygon(index).isClosed();
162 void SAL_CALL UnoPolyPolygon::setClosed(
163 sal_Int32 index,
164 sal_Bool closedState )
166 std::unique_lock const guard( m_aMutex );
167 modifying();
169 if( index == -1 )
171 // set all
172 maPolyPoly.setClosed( closedState );
174 else
176 checkIndex( index );
178 // fetch referenced polygon, change state
179 B2DPolygon aTmp( maPolyPoly.getB2DPolygon(index) );
180 aTmp.setClosed( closedState );
182 // set back to container
183 maPolyPoly.setB2DPolygon( index, aTmp );
187 uno::Sequence< uno::Sequence< geometry::RealPoint2D > > SAL_CALL UnoPolyPolygon::getPoints(
188 sal_Int32 nPolygonIndex,
189 sal_Int32 nNumberOfPolygons,
190 sal_Int32 nPointIndex,
191 sal_Int32 nNumberOfPoints )
193 return unotools::pointSequenceSequenceFromB2DPolyPolygon(
194 getSubsetPolyPolygon( nPolygonIndex,
195 nNumberOfPolygons,
196 nPointIndex,
197 nNumberOfPoints ) );
200 void SAL_CALL UnoPolyPolygon::setPoints(
201 const uno::Sequence< uno::Sequence< geometry::RealPoint2D > >& points,
202 sal_Int32 nPolygonIndex )
204 std::unique_lock const guard( m_aMutex );
205 modifying();
207 const B2DPolyPolygon& rNewPolyPoly(
208 unotools::polyPolygonFromPoint2DSequenceSequence( points ) );
210 if( nPolygonIndex == -1 )
212 maPolyPoly = rNewPolyPoly;
214 else
216 checkIndex( nPolygonIndex );
218 maPolyPoly.insert( nPolygonIndex, rNewPolyPoly );
222 geometry::RealPoint2D SAL_CALL UnoPolyPolygon::getPoint(
223 sal_Int32 nPolygonIndex,
224 sal_Int32 nPointIndex )
226 std::unique_lock const guard( m_aMutex );
227 checkIndex( nPolygonIndex );
229 const B2DPolygon& rPoly( maPolyPoly.getB2DPolygon( nPolygonIndex ) );
231 if( nPointIndex < 0 || o3tl::make_unsigned(nPointIndex) >= rPoly.count() )
232 throw lang::IndexOutOfBoundsException();
234 return unotools::point2DFromB2DPoint( rPoly.getB2DPoint( nPointIndex ) );
237 void SAL_CALL UnoPolyPolygon::setPoint(
238 const geometry::RealPoint2D& point,
239 sal_Int32 nPolygonIndex,
240 sal_Int32 nPointIndex )
242 std::unique_lock const guard( m_aMutex );
243 checkIndex( nPolygonIndex );
244 modifying();
246 B2DPolygon aPoly( maPolyPoly.getB2DPolygon( nPolygonIndex ) );
248 if( nPointIndex < 0 || o3tl::make_unsigned(nPointIndex) >= aPoly.count() )
249 throw lang::IndexOutOfBoundsException();
251 aPoly.setB2DPoint( nPointIndex,
252 unotools::b2DPointFromRealPoint2D( point ) );
253 maPolyPoly.setB2DPolygon( nPolygonIndex, aPoly );
256 uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > > SAL_CALL UnoPolyPolygon::getBezierSegments(
257 sal_Int32 nPolygonIndex,
258 sal_Int32 nNumberOfPolygons,
259 sal_Int32 nPointIndex,
260 sal_Int32 nNumberOfPoints )
262 return unotools::bezierSequenceSequenceFromB2DPolyPolygon(
263 getSubsetPolyPolygon( nPolygonIndex,
264 nNumberOfPolygons,
265 nPointIndex,
266 nNumberOfPoints ) );
269 void SAL_CALL UnoPolyPolygon::setBezierSegments(
270 const uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > >& points,
271 sal_Int32 nPolygonIndex )
273 std::unique_lock const guard( m_aMutex );
274 modifying();
275 const B2DPolyPolygon& rNewPolyPoly(
276 unotools::polyPolygonFromBezier2DSequenceSequence( points ) );
278 if( nPolygonIndex == -1 )
280 maPolyPoly = rNewPolyPoly;
282 else
284 checkIndex( nPolygonIndex );
286 maPolyPoly.insert( nPolygonIndex, rNewPolyPoly );
290 geometry::RealBezierSegment2D SAL_CALL UnoPolyPolygon::getBezierSegment( sal_Int32 nPolygonIndex,
291 sal_Int32 nPointIndex )
293 std::unique_lock const guard( m_aMutex );
294 checkIndex( nPolygonIndex );
296 const B2DPolygon& rPoly( maPolyPoly.getB2DPolygon( nPolygonIndex ) );
297 const sal_uInt32 nPointCount(rPoly.count());
299 if( nPointIndex < 0 || o3tl::make_unsigned(nPointIndex) >= nPointCount )
300 throw lang::IndexOutOfBoundsException();
302 const B2DPoint& rPt( rPoly.getB2DPoint( nPointIndex ) );
303 const B2DPoint& rCtrl0( rPoly.getNextControlPoint(nPointIndex) );
304 const B2DPoint& rCtrl1( rPoly.getPrevControlPoint((nPointIndex + 1) % nPointCount) );
306 return geometry::RealBezierSegment2D( rPt.getX(),
307 rPt.getY(),
308 rCtrl0.getX(),
309 rCtrl0.getY(),
310 rCtrl1.getX(),
311 rCtrl1.getY() );
314 void SAL_CALL UnoPolyPolygon::setBezierSegment( const geometry::RealBezierSegment2D& segment,
315 sal_Int32 nPolygonIndex,
316 sal_Int32 nPointIndex )
318 std::unique_lock const guard( m_aMutex );
319 checkIndex( nPolygonIndex );
320 modifying();
322 B2DPolygon aPoly( maPolyPoly.getB2DPolygon( nPolygonIndex ) );
323 const sal_uInt32 nPointCount(aPoly.count());
325 if( nPointIndex < 0 || o3tl::make_unsigned(nPointIndex) >= nPointCount )
326 throw lang::IndexOutOfBoundsException();
328 aPoly.setB2DPoint( nPointIndex,
329 B2DPoint( segment.Px,
330 segment.Py ) );
331 aPoly.setNextControlPoint(nPointIndex,
332 B2DPoint(segment.C1x, segment.C1y));
333 aPoly.setPrevControlPoint((nPointIndex + 1) % nPointCount,
334 B2DPoint(segment.C2x, segment.C2y));
336 maPolyPoly.setB2DPolygon( nPolygonIndex, aPoly );
339 B2DPolyPolygon UnoPolyPolygon::getSubsetPolyPolygon(
340 sal_Int32 nPolygonIndex,
341 sal_Int32 nNumberOfPolygons,
342 sal_Int32 nPointIndex,
343 sal_Int32 nNumberOfPoints ) const
345 std::unique_lock const guard( m_aMutex );
346 checkIndex( nPolygonIndex );
348 const sal_Int32 nPolyCount( maPolyPoly.count() );
350 // check for "full polygon" case
351 if( !nPolygonIndex &&
352 !nPointIndex &&
353 nNumberOfPolygons == nPolyCount &&
354 nNumberOfPoints == -1 )
356 return maPolyPoly;
359 B2DPolyPolygon aSubsetPoly;
361 // create temporary polygon (as an extract from maPoly,
362 // which contains the requested subset)
363 for( sal_Int32 i=nPolygonIndex; i<nNumberOfPolygons; ++i )
365 checkIndex(i);
367 const B2DPolygon& rCurrPoly( maPolyPoly.getB2DPolygon(i) );
369 sal_Int32 nFirstPoint(0);
370 sal_Int32 nLastPoint(nPolyCount-1);
372 if( nPointIndex && i==nPolygonIndex )
374 // very first polygon - respect nPointIndex, if
375 // not zero
377 // empty polygon - impossible to specify _any_
378 // legal value except 0 here!
379 if( !nPolyCount)
380 throw lang::IndexOutOfBoundsException();
382 nFirstPoint = nPointIndex;
385 if( i==nNumberOfPolygons-1 && nNumberOfPoints != -1 )
387 // very last polygon - respect nNumberOfPoints
389 // empty polygon - impossible to specify _any_
390 // legal value except -1 here!
391 if( !nPolyCount )
392 throw lang::IndexOutOfBoundsException();
394 nLastPoint = nFirstPoint+nNumberOfPoints;
397 if( !nPolyCount )
399 // empty polygon - index checks already performed
400 // above, now simply append empty polygon
401 aSubsetPoly.append( rCurrPoly );
403 else
405 if( nFirstPoint < 0 || nFirstPoint >= nPolyCount )
406 throw lang::IndexOutOfBoundsException();
408 if( nLastPoint < 0 || nLastPoint >= nPolyCount )
409 throw lang::IndexOutOfBoundsException();
411 B2DPolygon aTmp;
412 for( sal_Int32 j=nFirstPoint; j<nLastPoint; ++j )
413 aTmp.append( rCurrPoly.getB2DPoint(j) );
415 aSubsetPoly.append( aTmp );
419 return aSubsetPoly;
422 OUString SAL_CALL UnoPolyPolygon::getImplementationName()
424 return "gfx::internal::UnoPolyPolygon";
427 sal_Bool SAL_CALL UnoPolyPolygon::supportsService( const OUString& ServiceName )
429 return cppu::supportsService(this, ServiceName);
432 uno::Sequence< OUString > SAL_CALL UnoPolyPolygon::getSupportedServiceNames()
434 return { "com.sun.star.rendering.PolyPolygon2D" };
437 B2DPolyPolygon UnoPolyPolygon::getPolyPolygon() const
439 std::unique_lock const guard( m_aMutex );
441 return maPolyPoly;
446 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */