LibreOffice Module basegfx (master) 1
canvastools.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <com/sun/star/geometry/RealSize2D.hpp>
21#include <com/sun/star/geometry/RealPoint2D.hpp>
22#include <com/sun/star/geometry/RealRectangle2D.hpp>
23#include <com/sun/star/geometry/RealRectangle3D.hpp>
24#include <com/sun/star/geometry/RealBezierSegment2D.hpp>
25#include <com/sun/star/geometry/AffineMatrix2D.hpp>
26#include <com/sun/star/geometry/AffineMatrix3D.hpp>
27#include <com/sun/star/geometry/IntegerSize2D.hpp>
28#include <com/sun/star/geometry/IntegerRectangle2D.hpp>
29#include <com/sun/star/lang/IllegalArgumentException.hpp>
30#include <com/sun/star/rendering/XPolyPolygon2D.hpp>
31#include <com/sun/star/rendering/XGraphicDevice.hpp>
32#include <com/sun/star/awt/Rectangle.hpp>
43
44using namespace ::com::sun::star;
45
47{
48 namespace
49 {
50 uno::Sequence< geometry::RealBezierSegment2D > bezierSequenceFromB2DPolygon(const ::basegfx::B2DPolygon& rPoly)
51 {
52 const sal_uInt32 nPointCount(rPoly.count());
53 uno::Sequence< geometry::RealBezierSegment2D > outputSequence(nPointCount);
54 geometry::RealBezierSegment2D* pOutput = outputSequence.getArray();
55
56 // fill sequences and imply closed polygon on this implementation layer
57 for(sal_uInt32 a(0); a < nPointCount; a++)
58 {
59 const basegfx::B2DPoint aStart(rPoly.getB2DPoint(a));
60 const basegfx::B2DPoint aControlA(rPoly.getNextControlPoint(a));
61 const basegfx::B2DPoint aControlB(rPoly.getPrevControlPoint((a + 1) % nPointCount));
62
63 pOutput[a] = geometry::RealBezierSegment2D(
64 aStart.getX(), aStart.getY(),
65 aControlA.getX(), aControlA.getY(),
66 aControlB.getX(), aControlB.getY());
67 }
68
69 return outputSequence;
70 }
71
72 uno::Sequence< geometry::RealPoint2D > pointSequenceFromB2DPolygon( const ::basegfx::B2DPolygon& rPoly )
73 {
74 const sal_uInt32 nNumPoints( rPoly.count() );
75
76 uno::Sequence< geometry::RealPoint2D > outputSequence( nNumPoints );
77 geometry::RealPoint2D* pOutput = outputSequence.getArray();
78
79 // fill sequence from polygon
80 sal_uInt32 i;
81 for( i=0; i<nNumPoints; ++i )
82 {
83 const ::basegfx::B2DPoint aPoint( rPoly.getB2DPoint(i) );
84
85 pOutput[i] = geometry::RealPoint2D( aPoint.getX(),
86 aPoint.getY() );
87 }
88
89 return outputSequence;
90 }
91 }
92
93 uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > > bezierSequenceSequenceFromB2DPolyPolygon( const ::basegfx::B2DPolyPolygon& rPolyPoly )
94 {
95 const sal_uInt32 nNumPolies( rPolyPoly.count() );
96 sal_uInt32 i;
97
98 uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > > outputSequence( nNumPolies );
99 uno::Sequence< geometry::RealBezierSegment2D >* pOutput = outputSequence.getArray();
100
101 for( i=0; i<nNumPolies; ++i )
102 {
103 pOutput[i] = bezierSequenceFromB2DPolygon( rPolyPoly.getB2DPolygon(i) );
104 }
105
106 return outputSequence;
107 }
108
109 uno::Sequence< uno::Sequence< geometry::RealPoint2D > > pointSequenceSequenceFromB2DPolyPolygon( const ::basegfx::B2DPolyPolygon& rPolyPoly )
110 {
111 const sal_uInt32 nNumPolies( rPolyPoly.count() );
112 sal_uInt32 i;
113
114 uno::Sequence< uno::Sequence< geometry::RealPoint2D > > outputSequence( nNumPolies );
115 uno::Sequence< geometry::RealPoint2D >* pOutput = outputSequence.getArray();
116
117 for( i=0; i<nNumPolies; ++i )
118 {
119 pOutput[i] = pointSequenceFromB2DPolygon( rPolyPoly.getB2DPolygon(i) );
120 }
121
122 return outputSequence;
123 }
124
125 uno::Reference< rendering::XPolyPolygon2D > xPolyPolygonFromB2DPolygon( const uno::Reference< rendering::XGraphicDevice >& xGraphicDevice,
126 const ::basegfx::B2DPolygon& rPoly )
127 {
128 uno::Reference< rendering::XPolyPolygon2D > xRes;
129
130 if( !xGraphicDevice.is() )
131 return xRes;
132
133 if( rPoly.areControlPointsUsed() )
134 {
135 uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > > outputSequence{ bezierSequenceFromB2DPolygon( rPoly )};
136
137 xRes = xGraphicDevice->createCompatibleBezierPolyPolygon( outputSequence );
138 }
139 else
140 {
141 uno::Sequence< uno::Sequence< geometry::RealPoint2D > > outputSequence{
142 pointSequenceFromB2DPolygon( rPoly )};
143
144 xRes = xGraphicDevice->createCompatibleLinePolyPolygon( outputSequence );
145 }
146
147 if( xRes.is() && rPoly.isClosed() )
148 xRes->setClosed( 0, true );
149
150 return xRes;
151 }
152
153 uno::Reference< rendering::XPolyPolygon2D > xPolyPolygonFromB2DPolyPolygon( const uno::Reference< rendering::XGraphicDevice >& xGraphicDevice,
154 const ::basegfx::B2DPolyPolygon& rPolyPoly )
155 {
156 uno::Reference< rendering::XPolyPolygon2D > xRes;
157
158 if( !xGraphicDevice.is() )
159 return xRes;
160
161 const sal_uInt32 nNumPolies( rPolyPoly.count() );
162 sal_uInt32 i;
163
164 if( rPolyPoly.areControlPointsUsed() )
165 {
166 xRes = xGraphicDevice->createCompatibleBezierPolyPolygon(
168 }
169 else
170 {
171 xRes = xGraphicDevice->createCompatibleLinePolyPolygon(
173 }
174
175 for( i=0; i<nNumPolies; ++i )
176 {
177 xRes->setClosed( i, rPolyPoly.getB2DPolygon(i).isClosed() );
178 }
179
180 return xRes;
181 }
182
183 ::basegfx::B2DPolygon polygonFromPoint2DSequence( const uno::Sequence< geometry::RealPoint2D >& points )
184 {
185 const sal_Int32 nCurrSize( points.getLength() );
186
188
189 for( sal_Int32 nCurrPoint=0; nCurrPoint<nCurrSize; ++nCurrPoint )
190 aPoly.append( b2DPointFromRealPoint2D( points[nCurrPoint] ) );
191
192 return aPoly;
193 }
194
195 ::basegfx::B2DPolyPolygon polyPolygonFromPoint2DSequenceSequence( const uno::Sequence< uno::Sequence< geometry::RealPoint2D > >& points )
196 {
198
199 for( const auto & p : points )
200 {
202 }
203
204 return aRes;
205 }
206
207 ::basegfx::B2DPolygon polygonFromBezier2DSequence( const uno::Sequence< geometry::RealBezierSegment2D >& curves )
208 {
209 const sal_Int32 nSize(curves.getLength());
210 basegfx::B2DPolygon aRetval;
211
212 if(nSize)
213 {
214 // prepare start with providing a start point. Use the first point from
215 // the sequence for this
216 const geometry::RealBezierSegment2D& rFirstSegment(curves[0]); // #i79917# first segment, not last
217 aRetval.append(basegfx::B2DPoint(rFirstSegment.Px, rFirstSegment.Py));
218
219 for(sal_Int32 a(0); a < nSize; a++)
220 {
221 const geometry::RealBezierSegment2D& rCurrSegment(curves[a]);
222 const geometry::RealBezierSegment2D& rNextSegment(curves[(a + 1) % nSize]);
223
224 // append curved edge with the control points and the next point
225 aRetval.appendBezierSegment(
226 basegfx::B2DPoint(rCurrSegment.C1x, rCurrSegment.C1y),
227 basegfx::B2DPoint(rCurrSegment.C2x, rCurrSegment.C2y), // #i79917# Argh! An x for an y!!
228 basegfx::B2DPoint(rNextSegment.Px, rNextSegment.Py));
229 }
230
231 // rescue the control point and remove the now double-added point
232 aRetval.setPrevControlPoint(0, aRetval.getPrevControlPoint(aRetval.count() - 1));
233 aRetval.remove(aRetval.count() - 1);
234 }
235
236 return aRetval;
237 }
238
239 ::basegfx::B2DPolyPolygon polyPolygonFromBezier2DSequenceSequence( const uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > >& curves )
240 {
242
243 for( const auto & c : curves )
244 {
246 }
247
248 return aRes;
249 }
250
251 ::basegfx::B2DPolyPolygon b2DPolyPolygonFromXPolyPolygon2D( const uno::Reference< rendering::XPolyPolygon2D >& xPoly )
252 {
254 dynamic_cast< ::basegfx::unotools::UnoPolyPolygon* >( xPoly.get() );
255
256 if( pPolyImpl )
257 {
258 return pPolyImpl->getPolyPolygon();
259 }
260 else
261 {
262 // not a known implementation object - try data source
263 // interfaces
264 const sal_Int32 nPolys( xPoly->getNumberOfPolygons() );
265
266 uno::Reference< rendering::XBezierPolyPolygon2D > xBezierPoly(
267 xPoly,
268 uno::UNO_QUERY );
269
270 if( xBezierPoly.is() )
271 {
273 xBezierPoly->getBezierSegments( 0,
274 nPolys,
275 0,
276 -1 ) );
277 }
278 else
279 {
280 uno::Reference< rendering::XLinePolyPolygon2D > xLinePoly(
281 xPoly,
282 uno::UNO_QUERY );
283
284 // no implementation class and no data provider
285 // found - contract violation.
286 if( !xLinePoly.is() )
287 {
288 throw lang::IllegalArgumentException(
289 "basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(): Invalid input"
290 "poly-polygon, cannot retrieve vertex data",
291 uno::Reference< uno::XInterface >(),
292 0 );
293 }
294
296 xLinePoly->getPoints( 0,
297 nPolys,
298 0,
299 -1 ));
300 }
301 }
302 }
303
305 const geometry::AffineMatrix2D& input )
306 {
307 // ensure last row is [0,0,1] (and optimized away)
308 output.identity();
309
310 output.set(0,0, input.m00);
311 output.set(0,1, input.m01);
312 output.set(0,2, input.m02);
313 output.set(1,0, input.m10);
314 output.set(1,1, input.m11);
315 output.set(1,2, input.m12);
316
317 return output;
318 }
319
320 ::basegfx::B3DHomMatrix homMatrixFromAffineMatrix3D( const ::css::geometry::AffineMatrix3D& input )
321 {
323
324 output.set(0,0, input.m00);
325 output.set(0,1, input.m01);
326 output.set(0,2, input.m02);
327 output.set(0,3, input.m03);
328
329 output.set(1,0, input.m10);
330 output.set(1,1, input.m11);
331 output.set(1,2, input.m12);
332 output.set(1,3, input.m13);
333
334 output.set(2,0, input.m20);
335 output.set(2,1, input.m21);
336 output.set(2,2, input.m22);
337 output.set(2,3, input.m23);
338
339 return output;
340 }
341
342 geometry::AffineMatrix2D& affineMatrixFromHomMatrix( geometry::AffineMatrix2D& output,
343 const ::basegfx::B2DHomMatrix& input)
344 {
345 output.m00 = input.get(0,0);
346 output.m01 = input.get(0,1);
347 output.m02 = input.get(0,2);
348 output.m10 = input.get(1,0);
349 output.m11 = input.get(1,1);
350 output.m12 = input.get(1,2);
351
352 return output;
353 }
354
355 geometry::AffineMatrix3D& affineMatrixFromHomMatrix3D(
356 geometry::AffineMatrix3D& output,
357 const ::basegfx::B3DHomMatrix& input)
358 {
359 output.m00 = input.get(0,0);
360 output.m01 = input.get(0,1);
361 output.m02 = input.get(0,2);
362 output.m03 = input.get(0,3);
363
364 output.m10 = input.get(1,0);
365 output.m11 = input.get(1,1);
366 output.m12 = input.get(1,2);
367 output.m13 = input.get(1,3);
368
369 output.m20 = input.get(2,0);
370 output.m21 = input.get(2,1);
371 output.m22 = input.get(2,2);
372 output.m23 = input.get(2,3);
373
374 return output;
375 }
376
377 geometry::RealSize2D size2DFromB2DSize(const ::basegfx::B2DSize& rSize)
378 {
379 return geometry::RealSize2D(rSize.getWidth(), rSize.getHeight());
380 }
381
382 geometry::RealPoint2D point2DFromB2DPoint( const ::basegfx::B2DPoint& rPoint )
383 {
384 return geometry::RealPoint2D( rPoint.getX(),
385 rPoint.getY() );
386 }
387
388 geometry::RealRectangle2D rectangle2DFromB2DRectangle( const ::basegfx::B2DRange& rRect )
389 {
390 return geometry::RealRectangle2D( rRect.getMinX(),
391 rRect.getMinY(),
392 rRect.getMaxX(),
393 rRect.getMaxY() );
394 }
395
396 geometry::RealRectangle3D rectangle3DFromB3DRectangle( const ::basegfx::B3DRange& rRect )
397 {
398 return geometry::RealRectangle3D( rRect.getMinX(),
399 rRect.getMinY(),
400 rRect.getMinZ(),
401 rRect.getMaxX(),
402 rRect.getMaxY(),
403 rRect.getMaxZ());
404 }
405
406 ::basegfx::B2DPoint b2DPointFromRealPoint2D( const geometry::RealPoint2D& rPoint )
407 {
408 return ::basegfx::B2DPoint( rPoint.X,
409 rPoint.Y );
410 }
411
412 ::basegfx::B2DRange b2DRectangleFromRealRectangle2D( const geometry::RealRectangle2D& rRect )
413 {
414 return ::basegfx::B2DRange( rRect.X1,
415 rRect.Y1,
416 rRect.X2,
417 rRect.Y2 );
418 }
419
420 ::basegfx::B3DRange b3DRectangleFromRealRectangle3D( const geometry::RealRectangle3D& rRect )
421 {
422 return ::basegfx::B3DRange( rRect.X1,
423 rRect.Y1,
424 rRect.Z1,
425 rRect.X2,
426 rRect.Y2,
427 rRect.Z2);
428 }
429
430 geometry::IntegerSize2D integerSize2DFromB2ISize( const ::basegfx::B2IVector& rSize )
431 {
432 return geometry::IntegerSize2D( rSize.getX(),
433 rSize.getY() );
434 }
435
436 ::basegfx::B2IVector b2ISizeFromIntegerSize2D( const geometry::IntegerSize2D& rSize )
437 {
438 return ::basegfx::B2IVector( rSize.Width,
439 rSize.Height );
440 }
441
442 ::basegfx::B2IRange b2IRectangleFromIntegerRectangle2D( const geometry::IntegerRectangle2D& rRectangle )
443 {
444 return ::basegfx::B2IRange( rRectangle.X1, rRectangle.Y1,
445 rRectangle.X2, rRectangle.Y2 );
446 }
447
449 {
450 return ::basegfx::B2IRange( rRect.X,
451 rRect.Y,
452 rRect.X + rRect.Width,
453 rRect.Y + rRect.Height );
454 }
455
456 ::basegfx::B2IRange b2ISurroundingRangeFromB2DRange( const ::basegfx::B2DRange& rRange )
457 {
458 return ::basegfx::B2IRange( static_cast<sal_Int32>( floor(rRange.getMinX()) ),
459 static_cast<sal_Int32>( floor(rRange.getMinY()) ),
460 static_cast<sal_Int32>( ceil(rRange.getMaxX()) ),
461 static_cast<sal_Int32>( ceil(rRange.getMaxY()) ) );
462 }
463
465 {
466 return ::basegfx::B2DRange( floor(rRange.getMinX()),
467 floor(rRange.getMinY()),
468 ceil(rRange.getMaxX()),
469 ceil(rRange.getMaxY()) );
470 }
471
472} // namespace
473
474/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void set(sal_uInt16 nRow, sal_uInt16 nColumn, double fValue)
Base Point class with two double values.
Definition: b2dpoint.hxx:42
void append(const B2DPolygon &rPolygon, sal_uInt32 nCount=1)
void setPrevControlPoint(sal_uInt32 nIndex, const basegfx::B2DPoint &rValue)
basegfx::B2DPoint getPrevControlPoint(sal_uInt32 nIndex) const
Basic ControlPoint interface.
void append(const basegfx::B2DPoint &rPoint, sal_uInt32 nCount)
sal_uInt32 count() const
member count
void remove(sal_uInt32 nIndex, sal_uInt32 nCount=1)
remove points
void appendBezierSegment(const basegfx::B2DPoint &rNextControlPoint, const basegfx::B2DPoint &rPrevControlPoint, const basegfx::B2DPoint &rPoint)
Bezier segment append with control points. The current last polygon point is implicitly taken as star...
A two-dimensional interval over doubles.
Definition: b2drange.hxx:54
A two-dimensional interval over integers.
Definition: b2irange.hxx:53
Base Point class with two sal_Int32 values.
Definition: b2ivector.hxx:40
void set(sal_uInt16 nRow, sal_uInt16 nColumn, double fValue)
TYPE getX() const
Get X-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:63
TYPE getY() const
Get Y-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:66
SAL_DLLPRIVATE B2DPolyPolygon getPolyPolygon() const
void * p
uno_Any a
geometry::AffineMatrix3D & affineMatrixFromHomMatrix3D(geometry::AffineMatrix3D &output, const ::basegfx::B3DHomMatrix &input)
::basegfx::B2DRange b2DSurroundingIntegerRangeFromB2DRange(const ::basegfx::B2DRange &rRange)
Return smalltest B2DRange with integer values, which completely contains given floating point range.
::basegfx::B3DRange b3DRectangleFromRealRectangle3D(const geometry::RealRectangle3D &rRect)
::basegfx::B2DPolyPolygon polyPolygonFromPoint2DSequenceSequence(const uno::Sequence< uno::Sequence< geometry::RealPoint2D > > &points)
geometry::IntegerSize2D integerSize2DFromB2ISize(const ::basegfx::B2IVector &rSize)
geometry::AffineMatrix2D & affineMatrixFromHomMatrix(geometry::AffineMatrix2D &output, const ::basegfx::B2DHomMatrix &input)
::basegfx::B2IVector b2ISizeFromIntegerSize2D(const geometry::IntegerSize2D &rSize)
::basegfx::B2IRange b2IRectangleFromIntegerRectangle2D(const geometry::IntegerRectangle2D &rRectangle)
geometry::RealRectangle2D rectangle2DFromB2DRectangle(const ::basegfx::B2DRange &rRect)
::basegfx::B2DPolyPolygon polyPolygonFromBezier2DSequenceSequence(const uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > > &curves)
::basegfx::B2DPolygon polygonFromBezier2DSequence(const uno::Sequence< geometry::RealBezierSegment2D > &curves)
uno::Reference< rendering::XPolyPolygon2D > xPolyPolygonFromB2DPolyPolygon(const uno::Reference< rendering::XGraphicDevice > &xGraphicDevice, const ::basegfx::B2DPolyPolygon &rPolyPoly)
::basegfx::B2DPoint b2DPointFromRealPoint2D(const geometry::RealPoint2D &rPoint)
::basegfx::B2DHomMatrix & homMatrixFromAffineMatrix(::basegfx::B2DHomMatrix &output, const geometry::AffineMatrix2D &input)
geometry::RealPoint2D point2DFromB2DPoint(const ::basegfx::B2DPoint &rPoint)
::basegfx::B2DPolyPolygon b2DPolyPolygonFromXPolyPolygon2D(const uno::Reference< rendering::XPolyPolygon2D > &xPoly)
::basegfx::B2IRange b2IRectangleFromAwtRectangle(const awt::Rectangle &rRect)
::basegfx::B2DPolygon polygonFromPoint2DSequence(const uno::Sequence< geometry::RealPoint2D > &points)
geometry::RealRectangle3D rectangle3DFromB3DRectangle(const ::basegfx::B3DRange &rRect)
::basegfx::B2IRange b2ISurroundingRangeFromB2DRange(const ::basegfx::B2DRange &rRange)
Return smalltest integer range, which completely contains given floating point range.
uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > > bezierSequenceSequenceFromB2DPolyPolygon(const ::basegfx::B2DPolyPolygon &rPolyPoly)
Definition: canvastools.cxx:93
geometry::RealSize2D size2DFromB2DSize(const ::basegfx::B2DSize &rSize)
::basegfx::B2DRange b2DRectangleFromRealRectangle2D(const geometry::RealRectangle2D &rRect)
::basegfx::B3DHomMatrix homMatrixFromAffineMatrix3D(const ::css::geometry::AffineMatrix3D &input)
uno::Reference< rendering::XPolyPolygon2D > xPolyPolygonFromB2DPolygon(const uno::Reference< rendering::XGraphicDevice > &xGraphicDevice, const ::basegfx::B2DPolygon &rPoly)
uno::Sequence< uno::Sequence< geometry::RealPoint2D > > pointSequenceSequenceFromB2DPolyPolygon(const ::basegfx::B2DPolyPolygon &rPolyPoly)
int i