bump product version to 5.0.4.1
[LibreOffice.git] / toolkit / source / awt / vclxfont.cxx
bloba4b0c73a2d2a4df830235600d6673d37a7ea4820
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 <string.h>
22 #include <toolkit/awt/vclxfont.hxx>
23 #include <toolkit/helper/vclunohelper.hxx>
24 #include <toolkit/helper/macros.hxx>
25 #include <cppuhelper/typeprovider.hxx>
26 #include <cppuhelper/queryinterface.hxx>
27 #include <rtl/uuid.h>
28 #include <rtl/ustring.h>
29 #include <sal/alloca.h>
31 #include <vcl/outdev.hxx>
32 #include <vcl/svapp.hxx>
35 // class VCLXFont
37 VCLXFont::VCLXFont()
39 mpFontMetric = NULL;
42 VCLXFont::~VCLXFont()
44 delete mpFontMetric;
47 void VCLXFont::Init( ::com::sun::star::awt::XDevice& rxDev, const vcl::Font& rFont )
49 mxDevice = &rxDev;
51 delete mpFontMetric;
52 mpFontMetric = NULL;
54 maFont = rFont;
57 bool VCLXFont::ImplAssertValidFontMetric()
59 if ( !mpFontMetric && mxDevice.is() )
61 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
62 if ( pOutDev )
64 vcl::Font aOldFont = pOutDev->GetFont();
65 pOutDev->SetFont( maFont );
66 mpFontMetric = new FontMetric( pOutDev->GetFontMetric() );
67 pOutDev->SetFont( aOldFont );
70 return mpFontMetric != nullptr;
74 // ::com::sun::star::uno::XInterface
75 ::com::sun::star::uno::Any VCLXFont::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException, std::exception)
77 ::com::sun::star::uno::Any aRet = ::cppu::queryInterface( rType,
78 (static_cast< ::com::sun::star::awt::XFont* >(this)),
79 (static_cast< ::com::sun::star::awt::XFont2* >(this)),
80 (static_cast< ::com::sun::star::lang::XUnoTunnel* >(this)),
81 (static_cast< ::com::sun::star::lang::XTypeProvider* >(this)) );
82 return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
85 // ::com::sun::star::lang::XUnoTunnel
86 IMPL_XUNOTUNNEL( VCLXFont )
88 // ::com::sun::star::lang::XTypeProvider
89 IMPL_XTYPEPROVIDER_START( VCLXFont )
90 cppu::UnoType<com::sun::star::awt::XFont2>::get()
91 IMPL_XTYPEPROVIDER_END
94 ::com::sun::star::awt::FontDescriptor VCLXFont::getFontDescriptor( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
96 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
98 return VCLUnoHelper::CreateFontDescriptor( maFont );
102 ::com::sun::star::awt::SimpleFontMetric VCLXFont::getFontMetric( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
104 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
106 ::com::sun::star::awt::SimpleFontMetric aFM;
107 if ( ImplAssertValidFontMetric() )
108 aFM = VCLUnoHelper::CreateFontMetric( *mpFontMetric );
109 return aFM;
112 sal_Int16 VCLXFont::getCharWidth( sal_Unicode c ) throw(::com::sun::star::uno::RuntimeException, std::exception)
114 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
116 sal_Int16 nRet = -1;
117 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
118 if ( pOutDev )
120 vcl::Font aOldFont = pOutDev->GetFont();
121 pOutDev->SetFont( maFont );
123 nRet = sal::static_int_cast< sal_Int16 >(
124 pOutDev->GetTextWidth( OUString(c) ));
126 pOutDev->SetFont( aOldFont );
128 return nRet;
131 ::com::sun::star::uno::Sequence< sal_Int16 > VCLXFont::getCharWidths( sal_Unicode nFirst, sal_Unicode nLast ) throw(::com::sun::star::uno::RuntimeException, std::exception)
133 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
135 ::com::sun::star::uno::Sequence<sal_Int16> aSeq;
136 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
137 if ( pOutDev )
139 vcl::Font aOldFont = pOutDev->GetFont();
140 pOutDev->SetFont( maFont );
142 sal_Int16 nCount = nLast-nFirst + 1;
143 aSeq = ::com::sun::star::uno::Sequence<sal_Int16>( nCount );
144 for ( sal_uInt16 n = 0; n < nCount; n++ )
146 aSeq.getArray()[n] = sal::static_int_cast< sal_Int16 >(
147 pOutDev->GetTextWidth(
148 OUString(static_cast< sal_Unicode >(nFirst+n)) ));
151 pOutDev->SetFont( aOldFont );
153 return aSeq;
156 sal_Int32 VCLXFont::getStringWidth( const OUString& str ) throw(::com::sun::star::uno::RuntimeException, std::exception)
158 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
160 sal_Int32 nRet = -1;
161 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
162 if ( pOutDev )
164 vcl::Font aOldFont = pOutDev->GetFont();
165 pOutDev->SetFont( maFont );
166 nRet = pOutDev->GetTextWidth( str );
167 pOutDev->SetFont( aOldFont );
169 return nRet;
172 sal_Int32 VCLXFont::getStringWidthArray( const OUString& str, ::com::sun::star::uno::Sequence< sal_Int32 >& rDXArray ) throw(::com::sun::star::uno::RuntimeException, std::exception)
174 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
176 sal_Int32 nRet = -1;
177 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
178 if ( pOutDev )
180 vcl::Font aOldFont = pOutDev->GetFont();
181 pOutDev->SetFont( maFont );
182 long* pDXA = static_cast<long*>(alloca(str.getLength() * sizeof(long)));
183 nRet = pOutDev->GetTextArray( str, pDXA );
184 rDXArray = ::com::sun::star::uno::Sequence<sal_Int32>( str.getLength() );
185 for(int i = 0; i < str.getLength(); i++)
187 rDXArray[i] = pDXA[i];
189 pOutDev->SetFont( aOldFont );
191 return nRet;
194 void VCLXFont::getKernPairs( ::com::sun::star::uno::Sequence< sal_Unicode >& /*rnChars1*/, ::com::sun::star::uno::Sequence< sal_Unicode >& /*rnChars2*/, ::com::sun::star::uno::Sequence< sal_Int16 >& /*rnKerns*/ ) throw(::com::sun::star::uno::RuntimeException, std::exception)
196 // NOTE: this empty method is just used for keeping the related UNO-API stable
199 // ::com::sun::star::awt::XFont2
200 sal_Bool VCLXFont::hasGlyphs( const OUString& aText )
201 throw(::com::sun::star::uno::RuntimeException, std::exception)
203 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
204 SolarMutexGuard aSolarGuard;
206 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
207 if ( pOutDev )
209 OUString aStr( aText );
210 if ( pOutDev->HasGlyphs( maFont, aStr ) == -1 )
212 return sal_True;
216 return sal_False;
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */