tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / basegfx / source / curve / b2dbeziertools.cxx
blob8fd4c4b84c749edc474043b3417dbb98fc655de1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <basegfx/curve/b2dbeziertools.hxx>
21 #include <basegfx/curve/b2dcubicbezier.hxx>
22 #include <algorithm>
24 namespace basegfx
26 B2DCubicBezierHelper::B2DCubicBezierHelper(const B2DCubicBezier& rBase, sal_uInt32 nDivisions)
27 : mnEdgeCount(0)
29 const bool bIsBezier(rBase.isBezier());
31 if(bIsBezier)
33 // check nDivisions; at least one is needed, but also prevent too big values
34 if(nDivisions < 1)
36 nDivisions = 1;
38 else if(nDivisions > 1000)
40 nDivisions = 1000;
43 // set nEdgeCount
44 mnEdgeCount = nDivisions + 1;
46 // fill in maLengthArray
47 maLengthArray.clear();
48 maLengthArray.reserve(mnEdgeCount);
49 B2DPoint aCurrent(rBase.getStartPoint());
50 double fLength(0.0);
52 for(sal_uInt32 a(1);;)
54 const B2DPoint aNext(rBase.interpolatePoint(static_cast<double>(a) / static_cast<double>(mnEdgeCount)));
55 const B2DVector aEdge(aNext - aCurrent);
57 fLength += aEdge.getLength();
58 maLengthArray.push_back(fLength);
60 if(++a < mnEdgeCount)
62 aCurrent = aNext;
64 else
66 const B2DPoint& aLastNext(rBase.getEndPoint());
67 const B2DVector aLastEdge(aLastNext - aNext);
69 fLength += aLastEdge.getLength();
70 maLengthArray.push_back(fLength);
71 break;
75 else
77 maLengthArray.clear();
78 maLengthArray.push_back(rBase.getEdgeLength());
79 mnEdgeCount = 1;
83 double B2DCubicBezierHelper::distanceToRelative(double fDistance) const
85 if(fDistance <= 0.0)
87 return 0.0;
90 const double fLength(getLength());
92 if(fTools::moreOrEqual(fDistance, fLength))
94 return 1.0;
97 // fDistance is in ]0.0 .. fLength[
99 if(mnEdgeCount == 1)
101 // not a bezier, linear edge
102 return fDistance / fLength;
105 // it is a bezier
106 std::vector< double >::const_iterator aIter = std::lower_bound(maLengthArray.begin(), maLengthArray.end(), fDistance);
107 const sal_uInt32 nIndex(aIter - maLengthArray.begin());
108 const double fHighBound(maLengthArray[nIndex]);
109 const double fLowBound(nIndex ? maLengthArray[nIndex - 1] : 0.0);
110 const double fLinearInterpolatedLength((fDistance - fLowBound) / (fHighBound - fLowBound));
112 return (static_cast< double >(nIndex) + fLinearInterpolatedLength) / static_cast< double >(mnEdgeCount);
115 } // end of namespace basegfx
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */