Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / toolkit / source / awt / vclxfont.cxx
blob8bd86eb799055e3088676170e758360793149352
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>
21 #include <string.h>
23 #include <toolkit/awt/vclxfont.hxx>
24 #include <toolkit/helper/vclunohelper.hxx>
25 #include <toolkit/helper/macros.hxx>
26 #include <cppuhelper/typeprovider.hxx>
27 #include <cppuhelper/queryinterface.hxx>
28 #include <rtl/uuid.h>
29 #include <rtl/ustring.h>
31 #include <vcl/outdev.hxx>
32 #include <vcl/svapp.hxx>
35 // class VCLXFont
37 VCLXFont::VCLXFont()
39 mpFontMetric = nullptr;
42 VCLXFont::~VCLXFont()
46 void VCLXFont::Init( css::awt::XDevice& rxDev, const vcl::Font& rFont )
48 mxDevice = &rxDev;
50 mpFontMetric.reset();
52 maFont = rFont;
55 bool VCLXFont::ImplAssertValidFontMetric()
57 if ( !mpFontMetric && mxDevice.is() )
59 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
60 if ( pOutDev )
62 vcl::Font aOldFont = pOutDev->GetFont();
63 pOutDev->SetFont( maFont );
64 mpFontMetric.reset( new FontMetric( pOutDev->GetFontMetric() ) );
65 pOutDev->SetFont( aOldFont );
68 return mpFontMetric != nullptr;
72 // css::uno::XInterface
73 css::uno::Any VCLXFont::queryInterface( const css::uno::Type & rType )
75 css::uno::Any aRet = ::cppu::queryInterface( rType,
76 (static_cast< css::awt::XFont* >(this)),
77 (static_cast< css::awt::XFont2* >(this)),
78 (static_cast< css::lang::XUnoTunnel* >(this)),
79 (static_cast< css::lang::XTypeProvider* >(this)) );
80 return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
83 // css::lang::XUnoTunnel
84 IMPL_XUNOTUNNEL( VCLXFont )
86 // css::lang::XTypeProvider
87 IMPL_XTYPEPROVIDER_START( VCLXFont )
88 cppu::UnoType<css::awt::XFont2>::get()
89 IMPL_XTYPEPROVIDER_END
92 css::awt::FontDescriptor VCLXFont::getFontDescriptor( )
94 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
96 return VCLUnoHelper::CreateFontDescriptor( maFont );
100 css::awt::SimpleFontMetric VCLXFont::getFontMetric( )
102 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
104 css::awt::SimpleFontMetric aFM;
105 if ( ImplAssertValidFontMetric() )
106 aFM = VCLUnoHelper::CreateFontMetric( *mpFontMetric );
107 return aFM;
110 sal_Int16 VCLXFont::getCharWidth( sal_Unicode c )
112 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
114 sal_Int16 nRet = -1;
115 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
116 if ( pOutDev )
118 vcl::Font aOldFont = pOutDev->GetFont();
119 pOutDev->SetFont( maFont );
121 nRet = sal::static_int_cast< sal_Int16 >(
122 pOutDev->GetTextWidth( OUString(c) ));
124 pOutDev->SetFont( aOldFont );
126 return nRet;
129 css::uno::Sequence< sal_Int16 > VCLXFont::getCharWidths( sal_Unicode nFirst, sal_Unicode nLast )
131 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
133 css::uno::Sequence<sal_Int16> aSeq;
134 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
135 if ( pOutDev )
137 vcl::Font aOldFont = pOutDev->GetFont();
138 pOutDev->SetFont( maFont );
140 sal_Int16 nCount = nLast-nFirst + 1;
141 aSeq = css::uno::Sequence<sal_Int16>( nCount );
142 for ( sal_uInt16 n = 0; n < nCount; n++ )
144 aSeq.getArray()[n] = sal::static_int_cast< sal_Int16 >(
145 pOutDev->GetTextWidth(
146 OUString(static_cast< sal_Unicode >(nFirst+n)) ));
149 pOutDev->SetFont( aOldFont );
151 return aSeq;
154 sal_Int32 VCLXFont::getStringWidth( const OUString& str )
156 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
158 sal_Int32 nRet = -1;
159 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
160 if ( pOutDev )
162 vcl::Font aOldFont = pOutDev->GetFont();
163 pOutDev->SetFont( maFont );
164 nRet = pOutDev->GetTextWidth( str );
165 pOutDev->SetFont( aOldFont );
167 return nRet;
170 sal_Int32 VCLXFont::getStringWidthArray( const OUString& str, css::uno::Sequence< sal_Int32 >& rDXArray )
172 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
174 sal_Int32 nRet = -1;
175 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
176 if ( pOutDev )
178 vcl::Font aOldFont = pOutDev->GetFont();
179 pOutDev->SetFont( maFont );
180 std::unique_ptr<long []> pDXA(new long[str.getLength()]);
181 nRet = pOutDev->GetTextArray( str, pDXA.get() );
182 rDXArray = css::uno::Sequence<sal_Int32>( str.getLength() );
183 for(int i = 0; i < str.getLength(); i++)
185 rDXArray[i] = pDXA[i];
187 pOutDev->SetFont( aOldFont );
189 return nRet;
192 void VCLXFont::getKernPairs( css::uno::Sequence< sal_Unicode >& /*rnChars1*/, css::uno::Sequence< sal_Unicode >& /*rnChars2*/, css::uno::Sequence< sal_Int16 >& /*rnKerns*/ )
194 // NOTE: this empty method is just used for keeping the related UNO-API stable
197 // css::awt::XFont2
198 sal_Bool VCLXFont::hasGlyphs( const OUString& aText )
200 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
201 SolarMutexGuard aSolarGuard;
203 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
204 if ( pOutDev )
206 if ( pOutDev->HasGlyphs( maFont, aText ) == -1 )
208 return true;
212 return false;
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */