bump product version to 4.1.6.2
[LibreOffice.git] / toolkit / source / awt / vclxfont.cxx
blob01ef619fdd11ea6e4858621a906faf8f1470e7c6
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 <rtl/uuid.h>
27 #include <rtl/ustring.h>
29 #include <vcl/outdev.hxx>
31 // ----------------------------------------------------
32 // class VCLXFont
33 // ----------------------------------------------------
34 VCLXFont::VCLXFont()
36 mpFontMetric = NULL;
39 VCLXFont::~VCLXFont()
41 delete mpFontMetric;
44 void VCLXFont::Init( ::com::sun::star::awt::XDevice& rxDev, const Font& rFont )
46 mxDevice = &rxDev;
48 delete mpFontMetric;
49 mpFontMetric = NULL;
51 maFont = rFont;
54 sal_Bool VCLXFont::ImplAssertValidFontMetric()
56 if ( !mpFontMetric && mxDevice.is() )
58 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
59 if ( pOutDev )
61 Font aOldFont = pOutDev->GetFont();
62 pOutDev->SetFont( maFont );
63 mpFontMetric = new FontMetric( pOutDev->GetFontMetric() );
64 pOutDev->SetFont( aOldFont );
67 return mpFontMetric ? sal_True : sal_False;
71 // ::com::sun::star::uno::XInterface
72 ::com::sun::star::uno::Any VCLXFont::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException)
74 ::com::sun::star::uno::Any aRet = ::cppu::queryInterface( rType,
75 (static_cast< ::com::sun::star::awt::XFont* >(this)),
76 (static_cast< ::com::sun::star::awt::XFont2* >(this)),
77 (static_cast< ::com::sun::star::lang::XUnoTunnel* >(this)),
78 (static_cast< ::com::sun::star::lang::XTypeProvider* >(this)) );
79 return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
82 // ::com::sun::star::lang::XUnoTunnel
83 IMPL_XUNOTUNNEL( VCLXFont )
85 // ::com::sun::star::lang::XTypeProvider
86 IMPL_XTYPEPROVIDER_START( VCLXFont )
87 getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::awt::XFont2>* ) NULL )
88 IMPL_XTYPEPROVIDER_END
91 ::com::sun::star::awt::FontDescriptor VCLXFont::getFontDescriptor( ) throw(::com::sun::star::uno::RuntimeException)
93 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
95 return VCLUnoHelper::CreateFontDescriptor( maFont );
99 ::com::sun::star::awt::SimpleFontMetric VCLXFont::getFontMetric( ) throw(::com::sun::star::uno::RuntimeException)
101 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
103 ::com::sun::star::awt::SimpleFontMetric aFM;
104 if ( ImplAssertValidFontMetric() )
105 aFM = VCLUnoHelper::CreateFontMetric( *mpFontMetric );
106 return aFM;
109 sal_Int16 VCLXFont::getCharWidth( sal_Unicode c ) throw(::com::sun::star::uno::RuntimeException)
111 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
113 sal_Int16 nRet = -1;
114 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
115 if ( pOutDev )
117 Font aOldFont = pOutDev->GetFont();
118 pOutDev->SetFont( maFont );
120 nRet = sal::static_int_cast< sal_Int16 >(
121 pOutDev->GetTextWidth( OUString(c) ));
123 pOutDev->SetFont( aOldFont );
125 return nRet;
128 ::com::sun::star::uno::Sequence< sal_Int16 > VCLXFont::getCharWidths( sal_Unicode nFirst, sal_Unicode nLast ) throw(::com::sun::star::uno::RuntimeException)
130 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
132 ::com::sun::star::uno::Sequence<sal_Int16> aSeq;
133 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
134 if ( pOutDev )
136 Font aOldFont = pOutDev->GetFont();
137 pOutDev->SetFont( maFont );
139 sal_Int16 nCount = nLast-nFirst + 1;
140 aSeq = ::com::sun::star::uno::Sequence<sal_Int16>( nCount );
141 for ( sal_uInt16 n = 0; n < nCount; n++ )
143 aSeq.getArray()[n] = sal::static_int_cast< sal_Int16 >(
144 pOutDev->GetTextWidth(
145 OUString(static_cast< sal_Unicode >(nFirst+n)) ));
148 pOutDev->SetFont( aOldFont );
150 return aSeq;
153 sal_Int32 VCLXFont::getStringWidth( const OUString& str ) throw(::com::sun::star::uno::RuntimeException)
155 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
157 sal_Int32 nRet = -1;
158 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
159 if ( pOutDev )
161 Font aOldFont = pOutDev->GetFont();
162 pOutDev->SetFont( maFont );
163 nRet = pOutDev->GetTextWidth( str );
164 pOutDev->SetFont( aOldFont );
166 return nRet;
169 sal_Int32 VCLXFont::getStringWidthArray( const OUString& str, ::com::sun::star::uno::Sequence< sal_Int32 >& rDXArray ) throw(::com::sun::star::uno::RuntimeException)
171 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
173 sal_Int32 nRet = -1;
174 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
175 if ( pOutDev )
177 Font aOldFont = pOutDev->GetFont();
178 pOutDev->SetFont( maFont );
179 rDXArray = ::com::sun::star::uno::Sequence<sal_Int32>( str.getLength() );
180 nRet = pOutDev->GetTextArray( str, rDXArray.getArray() );
181 pOutDev->SetFont( aOldFont );
183 return nRet;
186 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)
188 // NOTE: this empty method is just used for keeping the related UNO-API stable
191 // ::com::sun::star::awt::XFont2
192 sal_Bool VCLXFont::hasGlyphs( const OUString& aText )
193 throw(::com::sun::star::uno::RuntimeException)
195 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
197 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
198 if ( pOutDev )
200 String aStr( aText );
201 if ( pOutDev->HasGlyphs( maFont, aStr, 0, aStr.Len() ) == STRING_LEN )
203 return sal_True;
207 return sal_False;
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */