Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / tools / RelativeSizeHelper.cxx
blob4a3cd84cfb887d4e46a60f8a9c921c51d7040ee1
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 <RelativeSizeHelper.hxx>
21 #include <com/sun/star/awt/Size.hpp>
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <comphelper/diagnose_ex.hxx>
24 #include <svx/unoshape.hxx>
25 #include <vector>
26 #include <algorithm>
28 using namespace ::com::sun::star;
29 using namespace ::com::sun::star::beans;
31 using ::com::sun::star::uno::Reference;
32 using ::com::sun::star::uno::Any;
33 using ::com::sun::star::uno::Exception;
35 namespace chart
38 double RelativeSizeHelper::calculate(
39 double fValue,
40 const awt::Size & rOldReferenceSize,
41 const awt::Size & rNewReferenceSize )
43 if( rOldReferenceSize.Width <= 0 ||
44 rOldReferenceSize.Height <= 0 )
45 return fValue;
47 return std::min(
48 static_cast< double >( rNewReferenceSize.Width ) / static_cast< double >( rOldReferenceSize.Width ),
49 static_cast< double >( rNewReferenceSize.Height ) / static_cast< double >( rOldReferenceSize.Height ))
50 * fValue;
53 void RelativeSizeHelper::adaptFontSizes(
54 SvxShapeText& xTargetProperties,
55 const awt::Size & rOldReferenceSize,
56 const awt::Size & rNewReferenceSize )
58 float fFontHeight = 0;
60 std::vector< OUString > aProperties;
61 aProperties.emplace_back("CharHeight" );
62 aProperties.emplace_back("CharHeightAsian" );
63 aProperties.emplace_back("CharHeightComplex" );
65 for (auto const& property : aProperties)
67 try
69 if( xTargetProperties.SvxShape::getPropertyValue(property) >>= fFontHeight )
71 xTargetProperties.SvxShape::setPropertyValue(
72 property,
73 Any( static_cast< float >(
74 calculate( fFontHeight, rOldReferenceSize, rNewReferenceSize ))));
77 catch( const Exception & )
79 DBG_UNHANDLED_EXCEPTION("chart2");
84 void RelativeSizeHelper::adaptFontSizes(
85 const Reference< XPropertySet > & xTargetProperties,
86 const awt::Size & rOldReferenceSize,
87 const awt::Size & rNewReferenceSize )
89 if( ! xTargetProperties.is())
90 return;
92 float fFontHeight = 0;
94 std::vector< OUString > aProperties;
95 aProperties.emplace_back("CharHeight" );
96 aProperties.emplace_back("CharHeightAsian" );
97 aProperties.emplace_back("CharHeightComplex" );
99 for (auto const& property : aProperties)
103 if( xTargetProperties->getPropertyValue(property) >>= fFontHeight )
105 xTargetProperties->setPropertyValue(
106 property,
107 Any( static_cast< float >(
108 calculate( fFontHeight, rOldReferenceSize, rNewReferenceSize ))));
111 catch( const Exception & )
113 DBG_UNHANDLED_EXCEPTION("chart2");
118 } // namespace chart
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */