Coverage Report

Created: 2026-04-09 11:41

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