tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / toolkit / source / awt / vclxfont.cxx
blobd95ec480191b208337a1addcb696a213629e19f3
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 <memory>
22 #include <com/sun/star/awt/XDevice.hpp>
24 #include <comphelper/sequence.hxx>
25 #include <toolkit/awt/vclxfont.hxx>
26 #include <toolkit/helper/vclunohelper.hxx>
28 #include <vcl/kernarray.hxx>
29 #include <vcl/metric.hxx>
30 #include <vcl/outdev.hxx>
31 #include <vcl/svapp.hxx>
33 VCLXFont::VCLXFont(css::awt::XDevice& rxDev, const vcl::Font& rFont)
35 mxDevice = &rxDev;
36 maFont = rFont;
39 VCLXFont::~VCLXFont()
43 bool VCLXFont::ImplAssertValidFontMetric()
45 if ( !mpFontMetric && mxDevice.is() )
47 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
48 if ( pOutDev )
50 vcl::Font aOldFont = pOutDev->GetFont();
51 pOutDev->SetFont( maFont );
52 mpFontMetric.reset( new FontMetric( pOutDev->GetFontMetric() ) );
53 pOutDev->SetFont( aOldFont );
56 return mpFontMetric != nullptr;
59 css::awt::FontDescriptor VCLXFont::getFontDescriptor( )
61 std::unique_lock aGuard( maMutex );
63 return VCLUnoHelper::CreateFontDescriptor( maFont );
67 css::awt::SimpleFontMetric VCLXFont::getFontMetric( )
69 std::unique_lock aGuard( maMutex );
71 css::awt::SimpleFontMetric aFM;
72 if ( ImplAssertValidFontMetric() )
73 aFM = VCLUnoHelper::CreateFontMetric( *mpFontMetric );
74 return aFM;
77 sal_Int16 VCLXFont::getCharWidth( sal_Unicode c )
79 std::unique_lock aGuard( maMutex );
81 sal_Int16 nRet = -1;
82 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
83 if ( pOutDev )
85 vcl::Font aOldFont = pOutDev->GetFont();
86 pOutDev->SetFont( maFont );
88 nRet = sal::static_int_cast< sal_Int16 >(
89 pOutDev->GetTextWidth( OUString(c) ));
91 pOutDev->SetFont( aOldFont );
93 return nRet;
96 css::uno::Sequence< sal_Int16 > VCLXFont::getCharWidths( sal_Unicode nFirst, sal_Unicode nLast )
98 std::unique_lock aGuard( maMutex );
100 css::uno::Sequence<sal_Int16> aSeq;
101 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
102 if ( pOutDev )
104 vcl::Font aOldFont = pOutDev->GetFont();
105 pOutDev->SetFont( maFont );
107 sal_Int16 nCount = nLast-nFirst + 1;
108 aSeq = css::uno::Sequence<sal_Int16>( nCount );
109 for ( sal_uInt16 n = 0; n < nCount; n++ )
111 aSeq.getArray()[n] = sal::static_int_cast< sal_Int16 >(
112 pOutDev->GetTextWidth(
113 OUString(static_cast< sal_Unicode >(nFirst+n)) ));
116 pOutDev->SetFont( aOldFont );
118 return aSeq;
121 sal_Int32 VCLXFont::getStringWidth( const OUString& str )
123 std::unique_lock aGuard( maMutex );
125 sal_Int32 nRet = -1;
126 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
127 if ( pOutDev )
129 vcl::Font aOldFont = pOutDev->GetFont();
130 pOutDev->SetFont( maFont );
131 nRet = pOutDev->GetTextWidth( str );
132 pOutDev->SetFont( aOldFont );
134 return nRet;
137 sal_Int32 VCLXFont::getStringWidthArray( const OUString& str, css::uno::Sequence< sal_Int32 >& rDXArray )
139 std::unique_lock aGuard( maMutex );
141 sal_Int32 nRet = -1;
142 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
143 if ( pOutDev )
145 vcl::Font aOldFont = pOutDev->GetFont();
146 pOutDev->SetFont( maFont );
147 KernArray aDXA;
148 nRet = basegfx::fround(pOutDev->GetTextArray(str, &aDXA).nWidth);
149 rDXArray.realloc(aDXA.size());
150 sal_Int32* pArray = rDXArray.getArray();
151 for (size_t i = 0, nLen = aDXA.size(); i < nLen; ++i)
152 pArray[i] = aDXA[i];
153 pOutDev->SetFont( aOldFont );
155 return nRet;
158 void VCLXFont::getKernPairs( css::uno::Sequence< sal_Unicode >& /*rnChars1*/, css::uno::Sequence< sal_Unicode >& /*rnChars2*/, css::uno::Sequence< sal_Int16 >& /*rnKerns*/ )
160 // NOTE: this empty method is just used for keeping the related UNO-API stable
163 // css::awt::XFont2
164 sal_Bool VCLXFont::hasGlyphs( const OUString& aText )
166 std::unique_lock aGuard( maMutex );
167 SolarMutexGuard aSolarGuard;
169 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
170 if ( pOutDev )
172 if ( pOutDev->HasGlyphs( maFont, aText ) == -1 )
174 return true;
178 return false;
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */