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>
44
45using namespace ::com::sun::star;
46
48{
49 namespace
50 {
51 uno::Sequence< geometry::RealBezierSegment2D > bezierSequenceFromB2DPolygon(const ::basegfx::B2DPolygon& rPoly)
52 {
53 const sal_uInt32 nPointCount(rPoly.count());
54 uno::Sequence< geometry::RealBezierSegment2D > outputSequence(nPointCount);
55 geometry::RealBezierSegment2D* pOutput = outputSequence.getArray();
56
57 // fill sequences and imply closed polygon on this implementation layer
58 for(sal_uInt32 a(0); a < nPointCount; a++)
59 {
60 const basegfx::B2DPoint aStart(rPoly.getB2DPoint(a));
61 const basegfx::B2DPoint aControlA(rPoly.getNextControlPoint(a));
62 const basegfx::B2DPoint aControlB(rPoly.getPrevControlPoint((a + 1) % nPointCount));
63
64 pOutput[a] = geometry::RealBezierSegment2D(
65 aStart.getX(), aStart.getY(),
66 aControlA.getX(), aControlA.getY(),
67 aControlB.getX(), aControlB.getY());
68 }
69
70 return outputSequence;
71 }
72
73 uno::Sequence< geometry::RealPoint2D > pointSequenceFromB2DPolygon( const ::basegfx::B2DPolygon& rPoly )
74 {
75 const sal_uInt32 nNumPoints( rPoly.count() );
76
77 uno::Sequence< geometry::RealPoint2D > outputSequence( nNumPoints );
78 geometry::RealPoint2D* pOutput = outputSequence.getArray();
79
80 // fill sequence from polygon
81 sal_uInt32 i;
82 for( i=0; i<nNumPoints; ++i )
83 {
84 const ::basegfx::B2DPoint aPoint( rPoly.getB2DPoint(i) );
85
86 pOutput[i] = geometry::RealPoint2D( aPoint.getX(),
87 aPoint.getY() );
88 }
89
90 return outputSequence;
91 }
92 }
93
94 uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > > bezierSequenceSequenceFromB2DPolyPolygon( const ::basegfx::B2DPolyPolygon& rPolyPoly )
95 {
96 const sal_uInt32 nNumPolies( rPolyPoly.count() );
97 sal_uInt32 i;
98
99 uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > > outputSequence( nNumPolies );
100 uno::Sequence< geometry::RealBezierSegment2D >* pOutput = outputSequence.getArray();
101
102 for( i=0; i<nNumPolies; ++i )
103 {
104 pOutput[i] = bezierSequenceFromB2DPolygon( rPolyPoly.getB2DPolygon(i) );
105 }
106
107 return outputSequence;
108 }
109
110 uno::Sequence< uno::Sequence< geometry::RealPoint2D > > pointSequenceSequenceFromB2DPolyPolygon( const ::basegfx::B2DPolyPolygon& rPolyPoly )
111 {
112 const sal_uInt32 nNumPolies( rPolyPoly.count() );
113 sal_uInt32 i;
114
115 uno::Sequence< uno::Sequence< geometry::RealPoint2D > > outputSequence( nNumPolies );
116 uno::Sequence< geometry::RealPoint2D >* pOutput = outputSequence.getArray();
117
118 for( i=0; i<nNumPolies; ++i )
119 {
120 pOutput[i] = pointSequenceFromB2DPolygon( rPolyPoly.getB2DPolygon(i) );
121 }
122
123 return outputSequence;
124 }
125
126 uno::Reference< rendering::XPolyPolygon2D > xPolyPolygonFromB2DPolygon( const uno::Reference< rendering::XGraphicDevice >& xGraphicDevice,
127 const ::basegfx::B2DPolygon& rPoly )
128 {
129 uno::Reference< rendering::XPolyPolygon2D > xRes;
130
131 if( !xGraphicDevice.is() )
132 return xRes;
133
134 if( rPoly.areControlPointsUsed() )
135 {
136 uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > > outputSequence{ bezierSequenceFromB2DPolygon( rPoly )};
137
138 xRes = xGraphicDevice->createCompatibleBezierPolyPolygon( outputSequence );
139 }
140 else
141 {
142 uno::Sequence< uno::Sequence< geometry::RealPoint2D > > outputSequence{
143 pointSequenceFromB2DPolygon( rPoly )};
144
145 xRes = xGraphicDevice->createCompatibleLinePolyPolygon( outputSequence );
146 }
147
148 if( xRes.is() && rPoly.isClosed() )
149 xRes->setClosed( 0, true );
150
151 return xRes;
152 }
153
154 uno::Reference< rendering::XPolyPolygon2D > xPolyPolygonFromB2DPolyPolygon( const uno::Reference< rendering::XGraphicDevice >& xGraphicDevice,
155 const ::basegfx::B2DPolyPolygon& rPolyPoly )
156 {
157 uno::Reference< rendering::XPolyPolygon2D > xRes;
158
159 if( !xGraphicDevice.is() )
160 return xRes;
161
162 const sal_uInt32 nNumPolies( rPolyPoly.count() );
163 sal_uInt32 i;
164
165 if( rPolyPoly.areControlPointsUsed() )
166 {
167 xRes = xGraphicDevice->createCompatibleBezierPolyPolygon(
169 }
170 else
171 {
172 xRes = xGraphicDevice->createCompatibleLinePolyPolygon(
174 }
175
176 for( i=0; i<nNumPolies; ++i )
177 {
178 xRes->setClosed( i, rPolyPoly.getB2DPolygon(i).isClosed() );
179 }
180
181 return xRes;
182 }
183
184 ::basegfx::B2DPolygon polygonFromPoint2DSequence( const uno::Sequence< geometry::RealPoint2D >& points )
185 {
186 const sal_Int32 nCurrSize( points.getLength() );
187
189
190 for( sal_Int32 nCurrPoint=0; nCurrPoint<nCurrSize; ++nCurrPoint )
191 aPoly.append( b2DPointFromRealPoint2D( points[nCurrPoint] ) );
192
193 return aPoly;
194 }
195
196 ::basegfx::B2DPolyPolygon polyPolygonFromPoint2DSequenceSequence( const uno::Sequence< uno::Sequence< geometry::RealPoint2D > >& points )
197 {
199
200 for( const auto & p : points )
201 {
203 }
204
205 return aRes;
206 }
207
208 ::basegfx::B2DPolygon polygonFromBezier2DSequence( const uno::Sequence< geometry::RealBezierSegment2D >& curves )
209 {
210 const sal_Int32 nSize(curves.getLength());
211 basegfx::B2DPolygon aRetval;
212
213 if(nSize)
214 {
215 // prepare start with providing a start point. Use the first point from
216 // the sequence for this
217 const geometry::RealBezierSegment2D& rFirstSegment(curves[0]); // #i79917# first segment, not last
218 aRetval.append(basegfx::B2DPoint(rFirstSegment.Px, rFirstSegment.Py));
219
220 for(sal_Int32 a(0); a < nSize; a++)
221 {
222 const geometry::RealBezierSegment2D& rCurrSegment(curves[a]);
223 const geometry::RealBezierSegment2D& rNextSegment(curves[(a + 1) % nSize]);
224
225 // append curved edge with the control points and the next point
226 aRetval.appendBezierSegment(
227 basegfx::B2DPoint(rCurrSegment.C1x, rCurrSegment.C1y),
228 basegfx::B2DPoint(rCurrSegment.C2x, rCurrSegment.C2y), // #i79917# Argh! An x for an y!!
229 basegfx::B2DPoint(rNextSegment.Px, rNextSegment.Py));
230 }
231
232 // rescue the control point and remove the now double-added point
233 aRetval.setPrevControlPoint(0, aRetval.getPrevControlPoint(aRetval.count() - 1));
234 aRetval.remove(aRetval.count() - 1);
235 }
236
237 return aRetval;
238 }
239
240 ::basegfx::B2DPolyPolygon polyPolygonFromBezier2DSequenceSequence( const uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > >& curves )
241 {
243
244 for( const auto & c : curves )
245 {
247 }
248
249 return aRes;
250 }
251
252 ::basegfx::B2DPolyPolygon b2DPolyPolygonFromXPolyPolygon2D( const uno::Reference< rendering::XPolyPolygon2D >& xPoly )
253 {
255 dynamic_cast< ::basegfx::unotools::UnoPolyPolygon* >( xPoly.get() );
256
257 if( pPolyImpl )
258 {
259 return pPolyImpl->getPolyPolygon();
260 }
261 else
262 {
263 // not a known implementation object - try data source
264 // interfaces
265 const sal_Int32 nPolys( xPoly->getNumberOfPolygons() );
266
267 uno::Reference< rendering::XBezierPolyPolygon2D > xBezierPoly(
268 xPoly,
269 uno::UNO_QUERY );
270
271 if( xBezierPoly.is() )
272 {
274 xBezierPoly->getBezierSegments( 0,
275 nPolys,
276 0,
277 -1 ) );
278 }
279 else
280 {
281 uno::Reference< rendering::XLinePolyPolygon2D > xLinePoly(
282 xPoly,
283 uno::UNO_QUERY );
284
285 // no implementation class and no data provider
286 // found - contract violation.
287 if( !xLinePoly.is() )
288 {
289 throw lang::IllegalArgumentException(
290 "basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(): Invalid input"
291 "poly-polygon, cannot retrieve vertex data",
292 uno::Reference< uno::XInterface >(),
293 0 );
294 }
295
297 xLinePoly->getPoints( 0,
298 nPolys,
299 0,
300 -1 ));
301 }
302 }
303 }
304
306 const geometry::AffineMatrix2D& input )
307 {
308 // ensure last row is [0,0,1] (and optimized away)
309 output.identity();
310
311 output.set(0,0, input.m00);
312 output.set(0,1, input.m01);
313 output.set(0,2, input.m02);
314 output.set(1,0, input.m10);
315 output.set(1,1, input.m11);
316 output.set(1,2, input.m12);
317
318 return output;
319 }
320
321 ::basegfx::B3DHomMatrix homMatrixFromAffineMatrix3D( const ::css::geometry::AffineMatrix3D& input )
322 {
324
325 output.set(0,0, input.m00);
326 output.set(0,1, input.m01);
327 output.set(0,2, input.m02);
328 output.set(0,3, input.m03);
329
330 output.set(1,0, input.m10);
331 output.set(1,1, input.m11);
332 output.set(1,2, input.m12);
333 output.set(1,3, input.m13);
334
335 output.set(2,0, input.m20);
336 output.set(2,1, input.m21);
337 output.set(2,2, input.m22);
338 output.set(2,3, input.m23);
339
340 return output;
341 }
342
343 geometry::AffineMatrix2D& affineMatrixFromHomMatrix( geometry::AffineMatrix2D& output,
344 const ::basegfx::B2DHomMatrix& input)
345 {
346 output.m00 = input.get(0,0);
347 output.m01 = input.get(0,1);
348 output.m02 = input.get(0,2);
349 output.m10 = input.get(1,0);
350 output.m11 = input.get(1,1);
351 output.m12 = input.get(1,2);
352
353 return output;
354 }
355
356 geometry::AffineMatrix3D& affineMatrixFromHomMatrix3D(
357 geometry::AffineMatrix3D& output,
358 const ::basegfx::B3DHomMatrix& input)
359 {
360 output.m00 = input.get(0,0);
361 output.m01 = input.get(0,1);
362 output.m02 = input.get(0,2);
363 output.m03 = input.get(0,3);
364
365 output.m10 = input.get(1,0);
366 output.m11 = input.get(1,1);
367 output.m12 = input.get(1,2);
368 output.m13 = input.get(1,3);
369
370 output.m20 = input.get(2,0);
371 output.m21 = input.get(2,1);
372 output.m22 = input.get(2,2);
373 output.m23 = input.get(2,3);
374
375 return output;
376 }
377
378 geometry::RealSize2D size2DFromB2DSize(const ::basegfx::B2DSize& rSize)
379 {
380 return geometry::RealSize2D(rSize.getWidth(), rSize.getHeight());
381 }
382
383 geometry::RealPoint2D point2DFromB2DPoint( const ::basegfx::B2DPoint& rPoint )
384 {
385 return geometry::RealPoint2D( rPoint.getX(),
386 rPoint.getY() );
387 }
388
389 geometry::RealRectangle2D rectangle2DFromB2DRectangle( const ::basegfx::B2DRange& rRect )
390 {
391 return geometry::RealRectangle2D( rRect.getMinX(),
392 rRect.getMinY(),
393 rRect.getMaxX(),
394 rRect.getMaxY() );
395 }
396
397 geometry::RealRectangle3D rectangle3DFromB3DRectangle( const ::basegfx::B3DRange& rRect )
398 {
399 return geometry::RealRectangle3D( rRect.getMinX(),
400 rRect.getMinY(),
401 rRect.getMinZ(),
402 rRect.getMaxX(),
403 rRect.getMaxY(),
404 rRect.getMaxZ());
405 }
406
407 ::basegfx::B2DPoint b2DPointFromRealPoint2D( const geometry::RealPoint2D& rPoint )
408 {
409 return ::basegfx::B2DPoint( rPoint.X,
410 rPoint.Y );
411 }
412
413 ::basegfx::B2DRange b2DRectangleFromRealRectangle2D( const geometry::RealRectangle2D& rRect )
414 {
415 return ::basegfx::B2DRange( rRect.X1,
416 rRect.Y1,
417 rRect.X2,
418 rRect.Y2 );
419 }
420
421 ::basegfx::B3DRange b3DRectangleFromRealRectangle3D( const geometry::RealRectangle3D& rRect )
422 {
423 return ::basegfx::B3DRange( rRect.X1,
424 rRect.Y1,
425 rRect.Z1,
426 rRect.X2,
427 rRect.Y2,
428 rRect.Z2);
429 }
430
431 geometry::IntegerSize2D integerSize2DFromB2ISize( const ::basegfx::B2ISize& rSize )
432 {
433 return geometry::IntegerSize2D( rSize.getWidth(),
434 rSize.getHeight() );
435 }
436
437 basegfx::B2ISize b2ISizeFromIntegerSize2D( const geometry::IntegerSize2D& rSize )
438 {
439 return basegfx::B2ISize(rSize.Width, 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
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::AffineMatrix2D & affineMatrixFromHomMatrix(geometry::AffineMatrix2D &output, const ::basegfx::B2DHomMatrix &input)
::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)
geometry::IntegerSize2D integerSize2DFromB2ISize(const ::basegfx::B2ISize &rSize)
::basegfx::B2DPolyPolygon b2DPolyPolygonFromXPolyPolygon2D(const uno::Reference< rendering::XPolyPolygon2D > &xPoly)
::basegfx::B2IRange b2IRectangleFromAwtRectangle(const awt::Rectangle &rRect)
basegfx::B2ISize b2ISizeFromIntegerSize2D(const geometry::IntegerSize2D &rSize)
::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:94
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