Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / tools / Scaling.cxx
blobb7179537c43b5baf82c261ea18679c3e73fdec07
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 <Scaling.hxx>
21 #include <com/sun/star/uno/RuntimeException.hpp>
22 #include <cppuhelper/supportsservice.hxx>
24 #include <cmath>
25 #include <limits>
27 namespace com::sun::star::uno { class XComponentContext; }
29 namespace
32 constexpr OUString lcl_aServiceName_Logarithmic = u"com.sun.star.chart2.LogarithmicScaling"_ustr;
33 constexpr OUString lcl_aServiceName_Exponential = u"com.sun.star.chart2.ExponentialScaling"_ustr;
34 constexpr OUString lcl_aServiceName_Linear = u"com.sun.star.chart2.LinearScaling"_ustr;
35 constexpr OUString lcl_aServiceName_Power = u"com.sun.star.chart2.PowerScaling"_ustr;
39 namespace chart
41 using namespace ::com::sun::star;
42 using namespace ::com::sun::star::chart2;
44 LogarithmicScaling::LogarithmicScaling() :
45 m_fBase( 10.0 ),
46 m_fLogOfBase( log( 10.0 ) )
50 LogarithmicScaling::LogarithmicScaling( double fBase ) :
51 m_fBase( fBase ),
52 m_fLogOfBase( log( fBase ) )
56 LogarithmicScaling::~LogarithmicScaling()
60 double SAL_CALL LogarithmicScaling::doScaling( double value )
62 if( std::isnan( value ) || std::isinf( value ) )
63 return std::numeric_limits<double>::quiet_NaN();
64 return std::log( value ) / m_fLogOfBase;
67 uno::Reference< XScaling > SAL_CALL LogarithmicScaling::getInverseScaling()
69 return new ExponentialScaling( m_fBase );
72 OUString SAL_CALL LogarithmicScaling::getServiceName()
74 return lcl_aServiceName_Logarithmic;
77 OUString SAL_CALL LogarithmicScaling::getImplementationName()
79 return lcl_aServiceName_Logarithmic;
82 sal_Bool SAL_CALL LogarithmicScaling::supportsService( const OUString& rServiceName )
84 return cppu::supportsService(this, rServiceName);
87 css::uno::Sequence< OUString > SAL_CALL LogarithmicScaling::getSupportedServiceNames()
89 return { lcl_aServiceName_Logarithmic };
92 ExponentialScaling::ExponentialScaling() :
93 m_fBase( 10.0 )
97 ExponentialScaling::ExponentialScaling( double fBase ) :
98 m_fBase( fBase )
102 ExponentialScaling::~ExponentialScaling()
106 double SAL_CALL ExponentialScaling::doScaling( double value )
108 if( std::isnan( value ) || std::isinf( value ) )
109 return std::numeric_limits<double>::quiet_NaN();
110 return std::pow( m_fBase, value );
113 uno::Reference< XScaling > SAL_CALL ExponentialScaling::getInverseScaling()
115 return new LogarithmicScaling( m_fBase );
118 OUString SAL_CALL ExponentialScaling::getServiceName()
120 return lcl_aServiceName_Exponential;
123 OUString SAL_CALL ExponentialScaling::getImplementationName()
125 return lcl_aServiceName_Exponential;
128 sal_Bool SAL_CALL ExponentialScaling::supportsService( const OUString& rServiceName )
130 return cppu::supportsService(this, rServiceName);
133 css::uno::Sequence< OUString > SAL_CALL ExponentialScaling::getSupportedServiceNames()
135 return { lcl_aServiceName_Exponential };
138 LinearScaling::LinearScaling() :
139 m_fSlope( 1.0 ),
140 m_fOffset( 0.0 )
143 LinearScaling::LinearScaling( double fSlope, double fOffset ) :
144 m_fSlope( fSlope ),
145 m_fOffset( fOffset )
148 LinearScaling::~LinearScaling()
151 double SAL_CALL LinearScaling::doScaling( double value )
153 if( std::isnan( value ) || std::isinf( value ) )
154 return std::numeric_limits<double>::quiet_NaN();
155 return m_fOffset + m_fSlope * value;
158 uno::Reference< XScaling > SAL_CALL
159 LinearScaling::getInverseScaling()
161 // ToDo: ApproxEqual ?
162 if( m_fSlope == 0 )
163 throw uno::RuntimeException("Divide by zero exception");
165 return new LinearScaling( 1.0 / m_fSlope, m_fOffset / m_fSlope );
168 OUString SAL_CALL LinearScaling::getServiceName()
170 return lcl_aServiceName_Linear;
173 OUString SAL_CALL LinearScaling::getImplementationName()
175 return lcl_aServiceName_Linear ;
178 sal_Bool SAL_CALL LinearScaling::supportsService( const OUString& rServiceName )
180 return cppu::supportsService(this, rServiceName);
183 css::uno::Sequence< OUString > SAL_CALL LinearScaling::getSupportedServiceNames()
185 return { lcl_aServiceName_Linear };
188 PowerScaling::PowerScaling() :
189 m_fExponent( 10.0 )
192 PowerScaling::PowerScaling( double fExponent ) :
193 m_fExponent( fExponent )
196 PowerScaling::~PowerScaling()
199 double SAL_CALL PowerScaling::doScaling( double value )
201 if( std::isnan( value ) || std::isinf( value ) )
202 return std::numeric_limits<double>::quiet_NaN();
203 return std::pow( value, m_fExponent );
206 uno::Reference< XScaling > SAL_CALL
207 PowerScaling::getInverseScaling()
209 // ToDo: ApproxEqual ?
210 if( m_fExponent == 0 )
211 throw uno::RuntimeException("Divide by zero exception");
213 return new PowerScaling( 1.0 / m_fExponent );
216 OUString SAL_CALL
217 PowerScaling::getServiceName()
219 return lcl_aServiceName_Power;
222 OUString SAL_CALL PowerScaling::getImplementationName()
224 return lcl_aServiceName_Power;
227 sal_Bool SAL_CALL PowerScaling::supportsService( const OUString& rServiceName )
229 return cppu::supportsService(this, rServiceName);
232 css::uno::Sequence< OUString > SAL_CALL PowerScaling::getSupportedServiceNames()
234 return { lcl_aServiceName_Power };
237 } //namespace chart
239 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
240 com_sun_star_chart2_LinearScaling_get_implementation(css::uno::XComponentContext *,
241 css::uno::Sequence<css::uno::Any> const &)
243 return cppu::acquire(new chart::LinearScaling );
246 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
247 com_sun_star_chart2_ExponentialScaling_get_implementation(css::uno::XComponentContext *,
248 css::uno::Sequence<css::uno::Any> const &)
250 return cppu::acquire(new chart::ExponentialScaling );
253 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
254 com_sun_star_chart2_LogarithmicScaling_get_implementation(css::uno::XComponentContext *,
255 css::uno::Sequence<css::uno::Any> const &)
257 return cppu::acquire(new chart::LogarithmicScaling );
260 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
261 com_sun_star_chart2_PowerScaling_get_implementation(css::uno::XComponentContext *,
262 css::uno::Sequence<css::uno::Any> const &)
264 return cppu::acquire(new chart::PowerScaling );
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */