Bump version to 24.04.3.4
[LibreOffice.git] / toolkit / source / awt / vclxfont.cxx
blob01e7aaae7944b5310a69dce58e272ddb931110ea
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()
35 mpFontMetric = nullptr;
38 VCLXFont::~VCLXFont()
42 void VCLXFont::Init( css::awt::XDevice& rxDev, const vcl::Font& rFont )
44 mxDevice = &rxDev;
46 mpFontMetric.reset();
48 maFont = rFont;
51 bool VCLXFont::ImplAssertValidFontMetric()
53 if ( !mpFontMetric && mxDevice.is() )
55 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
56 if ( pOutDev )
58 vcl::Font aOldFont = pOutDev->GetFont();
59 pOutDev->SetFont( maFont );
60 mpFontMetric.reset( new FontMetric( pOutDev->GetFontMetric() ) );
61 pOutDev->SetFont( aOldFont );
64 return mpFontMetric != nullptr;
67 css::awt::FontDescriptor VCLXFont::getFontDescriptor( )
69 std::unique_lock aGuard( maMutex );
71 return VCLUnoHelper::CreateFontDescriptor( maFont );
75 css::awt::SimpleFontMetric VCLXFont::getFontMetric( )
77 std::unique_lock aGuard( maMutex );
79 css::awt::SimpleFontMetric aFM;
80 if ( ImplAssertValidFontMetric() )
81 aFM = VCLUnoHelper::CreateFontMetric( *mpFontMetric );
82 return aFM;
85 sal_Int16 VCLXFont::getCharWidth( sal_Unicode c )
87 std::unique_lock aGuard( maMutex );
89 sal_Int16 nRet = -1;
90 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
91 if ( pOutDev )
93 vcl::Font aOldFont = pOutDev->GetFont();
94 pOutDev->SetFont( maFont );
96 nRet = sal::static_int_cast< sal_Int16 >(
97 pOutDev->GetTextWidth( OUString(c) ));
99 pOutDev->SetFont( aOldFont );
101 return nRet;
104 css::uno::Sequence< sal_Int16 > VCLXFont::getCharWidths( sal_Unicode nFirst, sal_Unicode nLast )
106 std::unique_lock aGuard( maMutex );
108 css::uno::Sequence<sal_Int16> aSeq;
109 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
110 if ( pOutDev )
112 vcl::Font aOldFont = pOutDev->GetFont();
113 pOutDev->SetFont( maFont );
115 sal_Int16 nCount = nLast-nFirst + 1;
116 aSeq = css::uno::Sequence<sal_Int16>( nCount );
117 for ( sal_uInt16 n = 0; n < nCount; n++ )
119 aSeq.getArray()[n] = sal::static_int_cast< sal_Int16 >(
120 pOutDev->GetTextWidth(
121 OUString(static_cast< sal_Unicode >(nFirst+n)) ));
124 pOutDev->SetFont( aOldFont );
126 return aSeq;
129 sal_Int32 VCLXFont::getStringWidth( const OUString& str )
131 std::unique_lock aGuard( maMutex );
133 sal_Int32 nRet = -1;
134 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
135 if ( pOutDev )
137 vcl::Font aOldFont = pOutDev->GetFont();
138 pOutDev->SetFont( maFont );
139 nRet = pOutDev->GetTextWidth( str );
140 pOutDev->SetFont( aOldFont );
142 return nRet;
145 sal_Int32 VCLXFont::getStringWidthArray( const OUString& str, css::uno::Sequence< sal_Int32 >& rDXArray )
147 std::unique_lock aGuard( maMutex );
149 sal_Int32 nRet = -1;
150 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
151 if ( pOutDev )
153 vcl::Font aOldFont = pOutDev->GetFont();
154 pOutDev->SetFont( maFont );
155 KernArray aDXA;
156 nRet = pOutDev->GetTextArray( str, &aDXA );
157 rDXArray.realloc(aDXA.size());
158 sal_Int32* pArray = rDXArray.getArray();
159 for (size_t i = 0, nLen = aDXA.size(); i < nLen; ++i)
160 pArray[i] = aDXA[i];
161 pOutDev->SetFont( aOldFont );
163 return nRet;
166 void VCLXFont::getKernPairs( css::uno::Sequence< sal_Unicode >& /*rnChars1*/, css::uno::Sequence< sal_Unicode >& /*rnChars2*/, css::uno::Sequence< sal_Int16 >& /*rnKerns*/ )
168 // NOTE: this empty method is just used for keeping the related UNO-API stable
171 // css::awt::XFont2
172 sal_Bool VCLXFont::hasGlyphs( const OUString& aText )
174 std::unique_lock aGuard( maMutex );
175 SolarMutexGuard aSolarGuard;
177 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
178 if ( pOutDev )
180 if ( pOutDev->HasGlyphs( maFont, aText ) == -1 )
182 return true;
186 return false;
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */