Line data Source code
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/Matrix2D.hpp>
28 : #include <com/sun/star/geometry/IntegerSize2D.hpp>
29 : #include <com/sun/star/geometry/IntegerPoint2D.hpp>
30 : #include <com/sun/star/geometry/IntegerRectangle2D.hpp>
31 : #include <com/sun/star/rendering/XPolyPolygon2D.hpp>
32 : #include <com/sun/star/rendering/XGraphicDevice.hpp>
33 : #include <com/sun/star/awt/Size.hpp>
34 : #include <com/sun/star/awt/Point.hpp>
35 : #include <com/sun/star/awt/Rectangle.hpp>
36 : #include <basegfx/tools/unopolypolygon.hxx>
37 : #include <basegfx/matrix/b2dhommatrix.hxx>
38 : #include <basegfx/matrix/b3dhommatrix.hxx>
39 : #include <basegfx/vector/b2dsize.hxx>
40 : #include <basegfx/point/b2dpoint.hxx>
41 : #include <basegfx/range/b2drectangle.hxx>
42 : #include <basegfx/range/b3drange.hxx>
43 : #include <basegfx/vector/b2isize.hxx>
44 : #include <basegfx/point/b2ipoint.hxx>
45 : #include <basegfx/range/b2irectangle.hxx>
46 : #include <basegfx/range/b2ibox.hxx>
47 : #include <basegfx/polygon/b2dpolygon.hxx>
48 : #include <basegfx/polygon/b2dpolypolygon.hxx>
49 : #include <basegfx/tools/canvastools.hxx>
50 : #include <limits>
51 :
52 : using namespace ::com::sun::star;
53 :
54 : namespace basegfx
55 : {
56 :
57 : namespace unotools
58 : {
59 : namespace
60 : {
61 0 : uno::Sequence< geometry::RealBezierSegment2D > bezierSequenceFromB2DPolygon(const ::basegfx::B2DPolygon& rPoly)
62 : {
63 0 : const sal_uInt32 nPointCount(rPoly.count());
64 0 : uno::Sequence< geometry::RealBezierSegment2D > outputSequence(nPointCount);
65 0 : geometry::RealBezierSegment2D* pOutput = outputSequence.getArray();
66 :
67 : // fill sequences and imply clodes polygon on this implementation layer
68 0 : for(sal_uInt32 a(0); a < nPointCount; a++)
69 : {
70 0 : const basegfx::B2DPoint aStart(rPoly.getB2DPoint(a));
71 0 : const basegfx::B2DPoint aControlA(rPoly.getNextControlPoint(a));
72 0 : const basegfx::B2DPoint aControlB(rPoly.getPrevControlPoint((a + 1) % nPointCount));
73 :
74 : pOutput[a] = geometry::RealBezierSegment2D(
75 0 : aStart.getX(), aStart.getY(),
76 0 : aControlA.getX(), aControlA.getY(),
77 0 : aControlB.getX(), aControlB.getY());
78 0 : }
79 :
80 0 : return outputSequence;
81 : }
82 :
83 0 : uno::Sequence< geometry::RealPoint2D > pointSequenceFromB2DPolygon( const ::basegfx::B2DPolygon& rPoly )
84 : {
85 0 : const sal_uInt32 nNumPoints( rPoly.count() );
86 :
87 0 : uno::Sequence< geometry::RealPoint2D > outputSequence( nNumPoints );
88 0 : geometry::RealPoint2D* pOutput = outputSequence.getArray();
89 :
90 : // fill sequence from polygon
91 : sal_uInt32 i;
92 0 : for( i=0; i<nNumPoints; ++i )
93 : {
94 0 : const ::basegfx::B2DPoint aPoint( rPoly.getB2DPoint(i) );
95 :
96 0 : pOutput[i] = geometry::RealPoint2D( aPoint.getX(),
97 0 : aPoint.getY() );
98 0 : }
99 :
100 0 : return outputSequence;
101 : }
102 : }
103 :
104 : //---------------------------------------------------------------------------------------
105 :
106 0 : uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > > bezierSequenceSequenceFromB2DPolyPolygon( const ::basegfx::B2DPolyPolygon& rPolyPoly )
107 : {
108 0 : const sal_uInt32 nNumPolies( rPolyPoly.count() );
109 : sal_uInt32 i;
110 :
111 0 : uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > > outputSequence( nNumPolies );
112 0 : uno::Sequence< geometry::RealBezierSegment2D >* pOutput = outputSequence.getArray();
113 :
114 0 : for( i=0; i<nNumPolies; ++i )
115 : {
116 0 : pOutput[i] = bezierSequenceFromB2DPolygon( rPolyPoly.getB2DPolygon(i) );
117 : }
118 :
119 0 : return outputSequence;
120 : }
121 :
122 : //---------------------------------------------------------------------------------------
123 :
124 0 : uno::Sequence< uno::Sequence< geometry::RealPoint2D > > pointSequenceSequenceFromB2DPolyPolygon( const ::basegfx::B2DPolyPolygon& rPolyPoly )
125 : {
126 0 : const sal_uInt32 nNumPolies( rPolyPoly.count() );
127 : sal_uInt32 i;
128 :
129 0 : uno::Sequence< uno::Sequence< geometry::RealPoint2D > > outputSequence( nNumPolies );
130 0 : uno::Sequence< geometry::RealPoint2D >* pOutput = outputSequence.getArray();
131 :
132 0 : for( i=0; i<nNumPolies; ++i )
133 : {
134 0 : pOutput[i] = pointSequenceFromB2DPolygon( rPolyPoly.getB2DPolygon(i) );
135 : }
136 :
137 0 : return outputSequence;
138 : }
139 :
140 : //---------------------------------------------------------------------------------------
141 :
142 0 : uno::Reference< rendering::XPolyPolygon2D > xPolyPolygonFromB2DPolygon( const uno::Reference< rendering::XGraphicDevice >& xGraphicDevice,
143 : const ::basegfx::B2DPolygon& rPoly )
144 : {
145 0 : uno::Reference< rendering::XPolyPolygon2D > xRes;
146 :
147 0 : if( !xGraphicDevice.is() )
148 0 : return xRes;
149 :
150 0 : if( rPoly.areControlPointsUsed() )
151 : {
152 0 : uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > > outputSequence( 1 );
153 0 : outputSequence[0] = bezierSequenceFromB2DPolygon( rPoly );
154 :
155 0 : xRes.set( xGraphicDevice->createCompatibleBezierPolyPolygon( outputSequence ),
156 0 : uno::UNO_QUERY );
157 : }
158 : else
159 : {
160 0 : uno::Sequence< uno::Sequence< geometry::RealPoint2D > > outputSequence( 1 );
161 0 : outputSequence[0] = pointSequenceFromB2DPolygon( rPoly );
162 :
163 0 : xRes.set( xGraphicDevice->createCompatibleLinePolyPolygon( outputSequence ),
164 0 : uno::UNO_QUERY );
165 : }
166 :
167 0 : if( xRes.is() && rPoly.isClosed() )
168 0 : xRes->setClosed( 0, sal_True );
169 :
170 0 : return xRes;
171 : }
172 :
173 : //---------------------------------------------------------------------------------------
174 :
175 0 : uno::Reference< rendering::XPolyPolygon2D > xPolyPolygonFromB2DPolyPolygon( const uno::Reference< rendering::XGraphicDevice >& xGraphicDevice,
176 : const ::basegfx::B2DPolyPolygon& rPolyPoly )
177 : {
178 0 : uno::Reference< rendering::XPolyPolygon2D > xRes;
179 :
180 0 : if( !xGraphicDevice.is() )
181 0 : return xRes;
182 :
183 0 : const sal_uInt32 nNumPolies( rPolyPoly.count() );
184 : sal_uInt32 i;
185 :
186 0 : if( rPolyPoly.areControlPointsUsed() )
187 : {
188 0 : xRes.set( xGraphicDevice->createCompatibleBezierPolyPolygon(
189 0 : bezierSequenceSequenceFromB2DPolyPolygon( rPolyPoly ) ),
190 0 : uno::UNO_QUERY );
191 : }
192 : else
193 : {
194 0 : xRes.set( xGraphicDevice->createCompatibleLinePolyPolygon(
195 0 : pointSequenceSequenceFromB2DPolyPolygon( rPolyPoly ) ),
196 0 : uno::UNO_QUERY );
197 : }
198 :
199 0 : for( i=0; i<nNumPolies; ++i )
200 : {
201 0 : xRes->setClosed( i, rPolyPoly.getB2DPolygon(i).isClosed() );
202 : }
203 :
204 0 : return xRes;
205 : }
206 :
207 : //---------------------------------------------------------------------------------------
208 :
209 0 : ::basegfx::B2DPolygon polygonFromPoint2DSequence( const uno::Sequence< geometry::RealPoint2D >& points )
210 : {
211 0 : const sal_Int32 nCurrSize( points.getLength() );
212 :
213 0 : ::basegfx::B2DPolygon aPoly;
214 :
215 0 : for( sal_Int32 nCurrPoint=0; nCurrPoint<nCurrSize; ++nCurrPoint )
216 0 : aPoly.append( b2DPointFromRealPoint2D( points[nCurrPoint] ) );
217 :
218 0 : return aPoly;
219 : }
220 :
221 : //---------------------------------------------------------------------------------------
222 :
223 0 : ::basegfx::B2DPolyPolygon polyPolygonFromPoint2DSequenceSequence( const uno::Sequence< uno::Sequence< geometry::RealPoint2D > >& points )
224 : {
225 0 : ::basegfx::B2DPolyPolygon aRes;
226 :
227 0 : for( sal_Int32 nCurrPoly=0; nCurrPoly<points.getLength(); ++nCurrPoly )
228 : {
229 0 : aRes.append( polygonFromPoint2DSequence( points[nCurrPoly] ) );
230 : }
231 :
232 0 : return aRes;
233 : }
234 :
235 : //---------------------------------------------------------------------------------------
236 :
237 0 : ::basegfx::B2DPolygon polygonFromBezier2DSequence( const uno::Sequence< geometry::RealBezierSegment2D >& curves )
238 : {
239 0 : const sal_Int32 nSize(curves.getLength());
240 0 : basegfx::B2DPolygon aRetval;
241 :
242 0 : if(nSize)
243 : {
244 : // prepare start with providing a start point. Use the first point from
245 : // the sequence for this
246 0 : const geometry::RealBezierSegment2D& rFirstSegment(curves[0]); // #i79917# first segment, not last
247 0 : aRetval.append(basegfx::B2DPoint(rFirstSegment.Px, rFirstSegment.Py));
248 :
249 0 : for(sal_Int32 a(0); a < nSize; a++)
250 : {
251 0 : const geometry::RealBezierSegment2D& rCurrSegment(curves[a]);
252 0 : const geometry::RealBezierSegment2D& rNextSegment(curves[(a + 1) % nSize]);
253 :
254 : // append curved edge with the control points and the next point
255 : aRetval.appendBezierSegment(
256 : basegfx::B2DPoint(rCurrSegment.C1x, rCurrSegment.C1y),
257 : basegfx::B2DPoint(rCurrSegment.C2x, rCurrSegment.C2y), // #i79917# Argh! An x for an y!!
258 0 : basegfx::B2DPoint(rNextSegment.Px, rNextSegment.Py));
259 : }
260 :
261 : // rescue the control point and remove the now double-added point
262 0 : aRetval.setPrevControlPoint(0, aRetval.getPrevControlPoint(aRetval.count() - 1));
263 0 : aRetval.remove(aRetval.count() - 1);
264 : }
265 :
266 0 : return aRetval;
267 : }
268 :
269 : //---------------------------------------------------------------------------------------
270 :
271 0 : ::basegfx::B2DPolyPolygon polyPolygonFromBezier2DSequenceSequence( const uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > >& curves )
272 : {
273 0 : ::basegfx::B2DPolyPolygon aRes;
274 :
275 0 : for( sal_Int32 nCurrPoly=0; nCurrPoly<curves.getLength(); ++nCurrPoly )
276 : {
277 0 : aRes.append( polygonFromBezier2DSequence( curves[nCurrPoly] ) );
278 : }
279 :
280 0 : return aRes;
281 : }
282 :
283 : //---------------------------------------------------------------------------------------
284 :
285 15 : ::basegfx::B2DPolyPolygon b2DPolyPolygonFromXPolyPolygon2D( const uno::Reference< rendering::XPolyPolygon2D >& xPoly )
286 : {
287 : ::basegfx::unotools::UnoPolyPolygon* pPolyImpl =
288 15 : dynamic_cast< ::basegfx::unotools::UnoPolyPolygon* >( xPoly.get() );
289 :
290 15 : if( pPolyImpl )
291 : {
292 15 : return pPolyImpl->getPolyPolygon();
293 : }
294 : else
295 : {
296 : // not a known implementation object - try data source
297 : // interfaces
298 0 : const sal_Int32 nPolys( xPoly->getNumberOfPolygons() );
299 :
300 : uno::Reference< rendering::XBezierPolyPolygon2D > xBezierPoly(
301 : xPoly,
302 0 : uno::UNO_QUERY );
303 :
304 0 : if( xBezierPoly.is() )
305 : {
306 : return ::basegfx::unotools::polyPolygonFromBezier2DSequenceSequence(
307 0 : xBezierPoly->getBezierSegments( 0,
308 : nPolys,
309 : 0,
310 0 : -1 ) );
311 : }
312 : else
313 : {
314 : uno::Reference< rendering::XLinePolyPolygon2D > xLinePoly(
315 : xPoly,
316 0 : uno::UNO_QUERY );
317 :
318 : // no implementation class and no data provider
319 : // found - contract violation.
320 0 : if( !xLinePoly.is() )
321 : {
322 : throw lang::IllegalArgumentException(
323 : OUString(
324 : "basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(): Invalid input"
325 : "poly-polygon, cannot retrieve vertex data"),
326 : uno::Reference< uno::XInterface >(),
327 0 : 0 );
328 : }
329 :
330 : return ::basegfx::unotools::polyPolygonFromPoint2DSequenceSequence(
331 0 : xLinePoly->getPoints( 0,
332 : nPolys,
333 : 0,
334 0 : -1 ));
335 0 : }
336 : }
337 : }
338 :
339 : //---------------------------------------------------------------------------------------
340 :
341 6 : ::basegfx::B2DHomMatrix& homMatrixFromAffineMatrix( ::basegfx::B2DHomMatrix& output,
342 : const geometry::AffineMatrix2D& input )
343 : {
344 : // ensure last row is [0,0,1] (and optimized away)
345 6 : output.identity();
346 :
347 6 : output.set(0,0, input.m00);
348 6 : output.set(0,1, input.m01);
349 6 : output.set(0,2, input.m02);
350 6 : output.set(1,0, input.m10);
351 6 : output.set(1,1, input.m11);
352 6 : output.set(1,2, input.m12);
353 :
354 6 : return output;
355 : }
356 :
357 0 : ::basegfx::B3DHomMatrix homMatrixFromAffineMatrix3D( const ::com::sun::star::geometry::AffineMatrix3D& input )
358 : {
359 0 : ::basegfx::B3DHomMatrix output;
360 :
361 0 : output.set(0,0, input.m00);
362 0 : output.set(0,1, input.m01);
363 0 : output.set(0,2, input.m02);
364 0 : output.set(0,3, input.m03);
365 :
366 0 : output.set(1,0, input.m10);
367 0 : output.set(1,1, input.m11);
368 0 : output.set(1,2, input.m12);
369 0 : output.set(1,3, input.m13);
370 :
371 0 : output.set(2,0, input.m20);
372 0 : output.set(2,1, input.m21);
373 0 : output.set(2,2, input.m22);
374 0 : output.set(2,3, input.m23);
375 :
376 0 : return output;
377 : }
378 :
379 0 : geometry::AffineMatrix2D& affineMatrixFromHomMatrix( geometry::AffineMatrix2D& output,
380 : const ::basegfx::B2DHomMatrix& input)
381 : {
382 0 : output.m00 = input.get(0,0);
383 0 : output.m01 = input.get(0,1);
384 0 : output.m02 = input.get(0,2);
385 0 : output.m10 = input.get(1,0);
386 0 : output.m11 = input.get(1,1);
387 0 : output.m12 = input.get(1,2);
388 :
389 0 : return output;
390 : }
391 :
392 0 : geometry::AffineMatrix3D& affineMatrixFromHomMatrix3D(
393 : geometry::AffineMatrix3D& output,
394 : const ::basegfx::B3DHomMatrix& input)
395 : {
396 0 : output.m00 = input.get(0,0);
397 0 : output.m01 = input.get(0,1);
398 0 : output.m02 = input.get(0,2);
399 0 : output.m03 = input.get(0,3);
400 :
401 0 : output.m10 = input.get(1,0);
402 0 : output.m11 = input.get(1,1);
403 0 : output.m12 = input.get(1,2);
404 0 : output.m13 = input.get(1,3);
405 :
406 0 : output.m20 = input.get(2,0);
407 0 : output.m21 = input.get(2,1);
408 0 : output.m22 = input.get(2,2);
409 0 : output.m23 = input.get(2,3);
410 :
411 0 : return output;
412 : }
413 :
414 : //---------------------------------------------------------------------------------------
415 :
416 22 : ::basegfx::B2DHomMatrix& homMatrixFromMatrix( ::basegfx::B2DHomMatrix& output,
417 : const geometry::Matrix2D& input )
418 : {
419 : // ensure last row is [0,0,1] (and optimized away)
420 22 : output.identity();
421 :
422 22 : output.set(0,0, input.m00);
423 22 : output.set(0,1, input.m01);
424 22 : output.set(1,0, input.m10);
425 22 : output.set(1,1, input.m11);
426 :
427 22 : return output;
428 : }
429 :
430 : //---------------------------------------------------------------------------------------
431 :
432 0 : geometry::RealSize2D size2DFromB2DSize( const ::basegfx::B2DVector& rVec )
433 : {
434 0 : return geometry::RealSize2D( rVec.getX(),
435 0 : rVec.getY() );
436 : }
437 :
438 0 : geometry::RealPoint2D point2DFromB2DPoint( const ::basegfx::B2DPoint& rPoint )
439 : {
440 0 : return geometry::RealPoint2D( rPoint.getX(),
441 0 : rPoint.getY() );
442 : }
443 :
444 0 : geometry::RealRectangle2D rectangle2DFromB2DRectangle( const ::basegfx::B2DRange& rRect )
445 : {
446 0 : return geometry::RealRectangle2D( rRect.getMinX(),
447 0 : rRect.getMinY(),
448 0 : rRect.getMaxX(),
449 0 : rRect.getMaxY() );
450 : }
451 :
452 0 : geometry::RealRectangle3D rectangle3DFromB3DRectangle( const ::basegfx::B3DRange& rRect )
453 : {
454 0 : return geometry::RealRectangle3D( rRect.getMinX(),
455 0 : rRect.getMinY(),
456 0 : rRect.getMinZ(),
457 0 : rRect.getMaxX(),
458 0 : rRect.getMaxY(),
459 0 : rRect.getMaxZ());
460 : }
461 :
462 0 : ::basegfx::B2DPoint b2DPointFromRealPoint2D( const geometry::RealPoint2D& rPoint )
463 : {
464 : return ::basegfx::B2DPoint( rPoint.X,
465 0 : rPoint.Y );
466 : }
467 :
468 208 : ::basegfx::B2DRange b2DRectangleFromRealRectangle2D( const geometry::RealRectangle2D& rRect )
469 : {
470 : return ::basegfx::B2DRange( rRect.X1,
471 : rRect.Y1,
472 : rRect.X2,
473 208 : rRect.Y2 );
474 : }
475 :
476 0 : ::basegfx::B3DRange b3DRectangleFromRealRectangle3D( const geometry::RealRectangle3D& rRect )
477 : {
478 : return ::basegfx::B3DRange( rRect.X1,
479 : rRect.Y1,
480 : rRect.Z1,
481 : rRect.X2,
482 : rRect.Y2,
483 0 : rRect.Z2);
484 : }
485 :
486 0 : geometry::IntegerSize2D integerSize2DFromB2ISize( const ::basegfx::B2IVector& rSize )
487 : {
488 0 : return geometry::IntegerSize2D( rSize.getX(),
489 0 : rSize.getY() );
490 : }
491 :
492 0 : ::basegfx::B2IVector b2ISizeFromIntegerSize2D( const geometry::IntegerSize2D& rSize )
493 : {
494 : return ::basegfx::B2IVector( rSize.Width,
495 0 : rSize.Height );
496 : }
497 :
498 0 : ::basegfx::B2IRange b2IRectangleFromIntegerRectangle2D( const geometry::IntegerRectangle2D& rRectangle )
499 : {
500 : return ::basegfx::B2IRange( rRectangle.X1, rRectangle.Y1,
501 0 : rRectangle.X2, rRectangle.Y2 );
502 : }
503 :
504 0 : ::basegfx::B2IRange b2IRectangleFromAwtRectangle( const awt::Rectangle& rRect )
505 : {
506 : return ::basegfx::B2IRange( rRect.X,
507 : rRect.Y,
508 : rRect.X + rRect.Width,
509 0 : rRect.Y + rRect.Height );
510 : }
511 :
512 0 : ::basegfx::B2IBox b2ISurroundingBoxFromB2DRange( const ::basegfx::B2DRange& rRange )
513 : {
514 0 : return ::basegfx::B2IBox( static_cast<sal_Int32>( floor(rRange.getMinX()) ),
515 0 : static_cast<sal_Int32>( floor(rRange.getMinY()) ),
516 0 : static_cast<sal_Int32>( ceil(rRange.getMaxX()) ),
517 0 : static_cast<sal_Int32>( ceil(rRange.getMaxY()) ) );
518 : }
519 :
520 0 : ::basegfx::B2IRange b2ISurroundingRangeFromB2DRange( const ::basegfx::B2DRange& rRange )
521 : {
522 0 : return ::basegfx::B2IRange( static_cast<sal_Int32>( floor(rRange.getMinX()) ),
523 0 : static_cast<sal_Int32>( floor(rRange.getMinY()) ),
524 0 : static_cast<sal_Int32>( ceil(rRange.getMaxX()) ),
525 0 : static_cast<sal_Int32>( ceil(rRange.getMaxY()) ) );
526 : }
527 :
528 0 : ::basegfx::B2DRange b2DSurroundingIntegerRangeFromB2DRange( const ::basegfx::B2DRange& rRange )
529 : {
530 : return ::basegfx::B2DRange( floor(rRange.getMinX()),
531 : floor(rRange.getMinY()),
532 : ceil(rRange.getMaxX()),
533 0 : ceil(rRange.getMaxY()) );
534 : }
535 :
536 : } // namespace bgfxtools
537 :
538 : } // namespace canvas
539 :
540 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|