Coverage Report

Created: 2026-08-14 10:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/basegfx/source/tools/tools.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 <basegfx/utils/tools.hxx>
21
#include <basegfx/range/b2drange.hxx>
22
23
#include <algorithm>
24
25
namespace basegfx::utils
26
{
27
        namespace
28
        {
29
            double distance( const double&                   nX,
30
                                    const double&                   nY,
31
                                    const ::basegfx::B2DVector&     rNormal,
32
                                    const double&                   nC )
33
0
            {
34
0
                return nX*rNormal.getX() + nY*rNormal.getY() - nC;
35
0
            }
36
37
            void moveLineOutsideRect( ::basegfx::B2DPoint&          io_rStart,
38
                                      ::basegfx::B2DPoint&          io_rEnd,
39
                                      const ::basegfx::B2DVector&   rMoveDirection,
40
                                      const ::basegfx::B2DRange&    rFitTarget      )
41
0
            {
42
                // calc c for normal line form equation n x - c = 0
43
0
                const double nC( rMoveDirection.scalar( io_rStart ) );
44
45
                // calc maximum orthogonal distance for all four bound
46
                // rect corners to the line
47
0
                const double nMaxDistance( std::max(
48
0
                                               0.0,
49
0
                                               std::max(
50
0
                                                   distance(rFitTarget.getMinX(),
51
0
                                                            rFitTarget.getMinY(),
52
0
                                                            rMoveDirection,
53
0
                                                            nC),
54
0
                                                   std::max(
55
0
                                                       distance(rFitTarget.getMinX(),
56
0
                                                                rFitTarget.getMaxY(),
57
0
                                                                rMoveDirection,
58
0
                                                                nC),
59
0
                                                       std::max(
60
0
                                                           distance(rFitTarget.getMaxX(),
61
0
                                                                    rFitTarget.getMinY(),
62
0
                                                                    rMoveDirection,
63
0
                                                                    nC),
64
0
                                                           distance(rFitTarget.getMaxX(),
65
0
                                                                    rFitTarget.getMaxY(),
66
0
                                                                    rMoveDirection,
67
0
                                                                    nC) ) ) ) ) );
68
69
                // now move line points, such that the bound rect
70
                // points are all either 'on' or on the negative side
71
                // of the half-plane
72
0
                io_rStart += nMaxDistance*rMoveDirection;
73
0
                io_rEnd   += nMaxDistance*rMoveDirection;
74
0
            }
75
        }
76
77
        void infiniteLineFromParallelogram( ::basegfx::B2DPoint&        io_rLeftTop,
78
                                            ::basegfx::B2DPoint&        io_rLeftBottom,
79
                                            ::basegfx::B2DPoint&        io_rRightTop,
80
                                            ::basegfx::B2DPoint&        io_rRightBottom,
81
                                            const ::basegfx::B2DRange&  rFitTarget  )
82
0
        {
83
            // For the top and bottom border line of the
84
            // parallelogram, we determine the distance to all four
85
            // corner points of the bound rect (tl, tr, bl, br). When
86
            // using the unit normal form for lines (n x - c = 0), and
87
            // choosing n to point 'outwards' the parallelogram, then
88
            // all bound rect corner points having positive distance
89
            // to the line lie outside the extended gradient rect, and
90
            // thus, the corresponding border line must be moved the
91
            // maximum distance outwards.
92
93
            // don't use the top and bottom border line direction, and
94
            // calculate the normal from them. Instead, use the
95
            // vertical lines (lt - lb or rt - rb), as they more
96
            // faithfully represent the direction of the
97
            // to-be-generated infinite line
98
0
            ::basegfx::B2DVector aDirectionVertical( io_rLeftTop - io_rLeftBottom );
99
0
            aDirectionVertical.normalize();
100
101
0
            const ::basegfx::B2DVector aNormalTop( aDirectionVertical );
102
0
            const ::basegfx::B2DVector aNormalBottom( -aDirectionVertical );
103
104
            // now extend parallelogram, such that the bound rect
105
            // point are included
106
0
            moveLineOutsideRect( io_rLeftTop, io_rRightTop, aNormalTop, rFitTarget );
107
0
            moveLineOutsideRect( io_rLeftBottom, io_rRightBottom, aNormalBottom, rFitTarget );
108
0
        }
109
}
110
111
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */