Coverage Report

Created: 2025-07-07 10:01

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