Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/basegfx/source/curve/b2dbeziertools.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/curve/b2dbeziertools.hxx>
21
#include <basegfx/curve/b2dcubicbezier.hxx>
22
#include <basegfx/vector/b2dvector.hxx>
23
#include <algorithm>
24
#include <cassert>
25
26
namespace basegfx
27
{
28
    B2DCubicBezierHelper::B2DCubicBezierHelper(const B2DCubicBezier& rBase, sal_uInt32 nDivisions)
29
0
    :   mnEdgeCount(0)
30
0
    {
31
0
        const bool bIsBezier(rBase.isBezier());
32
33
0
        if(bIsBezier)
34
0
        {
35
            // check nDivisions; at least one is needed, but also prevent too big values
36
0
            if(nDivisions < 1)
37
0
            {
38
0
                nDivisions = 1;
39
0
            }
40
0
            else if(nDivisions > 1000)
41
0
            {
42
0
                nDivisions = 1000;
43
0
            }
44
45
            // set nEdgeCount
46
0
            mnEdgeCount = nDivisions + 1;
47
48
            // fill in maLengthArray
49
0
            maLengthArray.clear();
50
0
            maLengthArray.reserve(mnEdgeCount);
51
0
            B2DPoint aCurrent(rBase.getStartPoint());
52
0
            double fLength(0.0);
53
54
0
            for(sal_uInt32 a(1);;)
55
0
            {
56
0
                const B2DPoint aNext(rBase.interpolatePoint(static_cast<double>(a) / static_cast<double>(mnEdgeCount)));
57
0
                const B2DVector aEdge(aNext - aCurrent);
58
59
0
                fLength += aEdge.getLength();
60
0
                maLengthArray.push_back(fLength);
61
62
0
                if(++a < mnEdgeCount)
63
0
                {
64
0
                    aCurrent = aNext;
65
0
                }
66
0
                else
67
0
                {
68
0
                    const B2DPoint& aLastNext(rBase.getEndPoint());
69
0
                    const B2DVector aLastEdge(aLastNext - aNext);
70
71
0
                    fLength += aLastEdge.getLength();
72
0
                    maLengthArray.push_back(fLength);
73
0
                    break;
74
0
                }
75
0
            }
76
0
        }
77
0
        else
78
0
        {
79
0
            maLengthArray.clear();
80
0
            maLengthArray.push_back(rBase.getEdgeLength());
81
0
            mnEdgeCount = 1;
82
0
        }
83
0
    }
84
85
    double B2DCubicBezierHelper::distanceToRelative(double fDistance) const
86
0
    {
87
0
        if(fDistance <= 0.0)
88
0
        {
89
0
            return 0.0;
90
0
        }
91
92
0
        const double fLength(getLength());
93
94
0
        if(fTools::moreOrEqual(fDistance, fLength))
95
0
        {
96
0
            return 1.0;
97
0
        }
98
99
        // fDistance is >= fLength. For fLength to be zero, then fDistance has
100
        // to be <= 0.0, but we return early in that case.
101
0
        assert(fLength != 0 && "help coverity see it's not zero");
102
103
        // fDistance is in ]0.0 .. fLength[
104
105
0
        if(mnEdgeCount == 1)
106
0
        {
107
            // not a bezier, linear edge
108
0
            return fDistance / fLength;
109
0
        }
110
111
        // it is a bezier
112
0
        std::vector< double >::const_iterator aIter = std::lower_bound(maLengthArray.begin(), maLengthArray.end(), fDistance);
113
0
        const sal_uInt32 nIndex(aIter - maLengthArray.begin());
114
0
        const double fHighBound(maLengthArray[nIndex]);
115
0
        const double fLowBound(nIndex ?  maLengthArray[nIndex - 1] : 0.0);
116
0
        const double fLinearInterpolatedLength((fDistance - fLowBound) / (fHighBound - fLowBound));
117
118
0
        return (static_cast< double >(nIndex) + fLinearInterpolatedLength) / static_cast< double >(mnEdgeCount);
119
0
    }
120
121
} // end of namespace basegfx
122
123
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */