1*b0724fc6SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*b0724fc6SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*b0724fc6SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*b0724fc6SAndrew Rist * distributed with this work for additional information 6*b0724fc6SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*b0724fc6SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*b0724fc6SAndrew Rist * "License"); you may not use this file except in compliance 9*b0724fc6SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*b0724fc6SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*b0724fc6SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*b0724fc6SAndrew Rist * software distributed under the License is distributed on an 15*b0724fc6SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*b0724fc6SAndrew Rist * KIND, either express or implied. See the License for the 17*b0724fc6SAndrew Rist * specific language governing permissions and limitations 18*b0724fc6SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*b0724fc6SAndrew Rist *************************************************************/ 21*b0724fc6SAndrew Rist 22*b0724fc6SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_toolkit.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir 28cdf0e10cSrcweir #include <toolkit/awt/vclxfont.hxx> 29cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx> 30cdf0e10cSrcweir #include <toolkit/helper/macros.hxx> 31cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx> 32cdf0e10cSrcweir #include <rtl/memory.h> 33cdf0e10cSrcweir #include <rtl/uuid.h> 34cdf0e10cSrcweir #include <rtl/ustring.h> 35cdf0e10cSrcweir 36cdf0e10cSrcweir #include <vcl/outdev.hxx> 37cdf0e10cSrcweir 38cdf0e10cSrcweir // ---------------------------------------------------- 39cdf0e10cSrcweir // class VCLXFont 40cdf0e10cSrcweir // ---------------------------------------------------- 41cdf0e10cSrcweir VCLXFont::VCLXFont() 42cdf0e10cSrcweir { 43cdf0e10cSrcweir mpFontMetric = NULL; 44cdf0e10cSrcweir } 45cdf0e10cSrcweir 46cdf0e10cSrcweir VCLXFont::~VCLXFont() 47cdf0e10cSrcweir { 48cdf0e10cSrcweir delete mpFontMetric; 49cdf0e10cSrcweir } 50cdf0e10cSrcweir 51cdf0e10cSrcweir void VCLXFont::Init( ::com::sun::star::awt::XDevice& rxDev, const Font& rFont ) 52cdf0e10cSrcweir { 53cdf0e10cSrcweir mxDevice = &rxDev; 54cdf0e10cSrcweir 55cdf0e10cSrcweir delete mpFontMetric; 56cdf0e10cSrcweir mpFontMetric = NULL; 57cdf0e10cSrcweir 58cdf0e10cSrcweir maFont = rFont; 59cdf0e10cSrcweir } 60cdf0e10cSrcweir 61cdf0e10cSrcweir sal_Bool VCLXFont::ImplAssertValidFontMetric() 62cdf0e10cSrcweir { 63cdf0e10cSrcweir if ( !mpFontMetric && mxDevice.is() ) 64cdf0e10cSrcweir { 65cdf0e10cSrcweir OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice ); 66cdf0e10cSrcweir if ( pOutDev ) 67cdf0e10cSrcweir { 68cdf0e10cSrcweir Font aOldFont = pOutDev->GetFont(); 69cdf0e10cSrcweir pOutDev->SetFont( maFont ); 70cdf0e10cSrcweir mpFontMetric = new FontMetric( pOutDev->GetFontMetric() ); 71cdf0e10cSrcweir pOutDev->SetFont( aOldFont ); 72cdf0e10cSrcweir } 73cdf0e10cSrcweir } 74cdf0e10cSrcweir return mpFontMetric ? sal_True : sal_False; 75cdf0e10cSrcweir } 76cdf0e10cSrcweir 77cdf0e10cSrcweir 78cdf0e10cSrcweir // ::com::sun::star::uno::XInterface 79cdf0e10cSrcweir ::com::sun::star::uno::Any VCLXFont::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException) 80cdf0e10cSrcweir { 81cdf0e10cSrcweir ::com::sun::star::uno::Any aRet = ::cppu::queryInterface( rType, 82cdf0e10cSrcweir SAL_STATIC_CAST( ::com::sun::star::awt::XFont*, this ), 83cdf0e10cSrcweir SAL_STATIC_CAST( ::com::sun::star::awt::XFont2*, this ), 84cdf0e10cSrcweir SAL_STATIC_CAST( ::com::sun::star::lang::XUnoTunnel*, this ), 85cdf0e10cSrcweir SAL_STATIC_CAST( ::com::sun::star::lang::XTypeProvider*, this ) ); 86cdf0e10cSrcweir return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType )); 87cdf0e10cSrcweir } 88cdf0e10cSrcweir 89cdf0e10cSrcweir // ::com::sun::star::lang::XUnoTunnel 90cdf0e10cSrcweir IMPL_XUNOTUNNEL( VCLXFont ) 91cdf0e10cSrcweir 92cdf0e10cSrcweir // ::com::sun::star::lang::XTypeProvider 93cdf0e10cSrcweir IMPL_XTYPEPROVIDER_START( VCLXFont ) 94cdf0e10cSrcweir getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::awt::XFont2>* ) NULL ) 95cdf0e10cSrcweir IMPL_XTYPEPROVIDER_END 96cdf0e10cSrcweir 97cdf0e10cSrcweir 98cdf0e10cSrcweir ::com::sun::star::awt::FontDescriptor VCLXFont::getFontDescriptor( ) throw(::com::sun::star::uno::RuntimeException) 99cdf0e10cSrcweir { 100cdf0e10cSrcweir ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); 101cdf0e10cSrcweir 102cdf0e10cSrcweir return VCLUnoHelper::CreateFontDescriptor( maFont ); 103cdf0e10cSrcweir 104cdf0e10cSrcweir } 105cdf0e10cSrcweir 106cdf0e10cSrcweir ::com::sun::star::awt::SimpleFontMetric VCLXFont::getFontMetric( ) throw(::com::sun::star::uno::RuntimeException) 107cdf0e10cSrcweir { 108cdf0e10cSrcweir ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); 109cdf0e10cSrcweir 110cdf0e10cSrcweir ::com::sun::star::awt::SimpleFontMetric aFM; 111cdf0e10cSrcweir if ( ImplAssertValidFontMetric() ) 112cdf0e10cSrcweir aFM = VCLUnoHelper::CreateFontMetric( *mpFontMetric ); 113cdf0e10cSrcweir return aFM; 114cdf0e10cSrcweir } 115cdf0e10cSrcweir 116cdf0e10cSrcweir sal_Int16 VCLXFont::getCharWidth( sal_Unicode c ) throw(::com::sun::star::uno::RuntimeException) 117cdf0e10cSrcweir { 118cdf0e10cSrcweir ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); 119cdf0e10cSrcweir 120cdf0e10cSrcweir sal_Int16 nRet = -1; 121cdf0e10cSrcweir OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice ); 122cdf0e10cSrcweir if ( pOutDev ) 123cdf0e10cSrcweir { 124cdf0e10cSrcweir Font aOldFont = pOutDev->GetFont(); 125cdf0e10cSrcweir pOutDev->SetFont( maFont ); 126cdf0e10cSrcweir 127cdf0e10cSrcweir nRet = sal::static_int_cast< sal_Int16 >( 128cdf0e10cSrcweir pOutDev->GetTextWidth( String(c) )); 129cdf0e10cSrcweir 130cdf0e10cSrcweir pOutDev->SetFont( aOldFont ); 131cdf0e10cSrcweir } 132cdf0e10cSrcweir return nRet; 133cdf0e10cSrcweir } 134cdf0e10cSrcweir 135cdf0e10cSrcweir ::com::sun::star::uno::Sequence< sal_Int16 > VCLXFont::getCharWidths( sal_Unicode nFirst, sal_Unicode nLast ) throw(::com::sun::star::uno::RuntimeException) 136cdf0e10cSrcweir { 137cdf0e10cSrcweir ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); 138cdf0e10cSrcweir 139cdf0e10cSrcweir ::com::sun::star::uno::Sequence<sal_Int16> aSeq; 140cdf0e10cSrcweir OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice ); 141cdf0e10cSrcweir if ( pOutDev ) 142cdf0e10cSrcweir { 143cdf0e10cSrcweir Font aOldFont = pOutDev->GetFont(); 144cdf0e10cSrcweir pOutDev->SetFont( maFont ); 145cdf0e10cSrcweir 146cdf0e10cSrcweir sal_Int16 nCount = nLast-nFirst + 1; 147cdf0e10cSrcweir aSeq = ::com::sun::star::uno::Sequence<sal_Int16>( nCount ); 148cdf0e10cSrcweir for ( sal_uInt16 n = 0; n < nCount; n++ ) 149cdf0e10cSrcweir { 150cdf0e10cSrcweir aSeq.getArray()[n] = sal::static_int_cast< sal_Int16 >( 151cdf0e10cSrcweir pOutDev->GetTextWidth( 152cdf0e10cSrcweir String(static_cast< sal_Unicode >(nFirst+n)) )); 153cdf0e10cSrcweir } 154cdf0e10cSrcweir 155cdf0e10cSrcweir pOutDev->SetFont( aOldFont ); 156cdf0e10cSrcweir } 157cdf0e10cSrcweir return aSeq; 158cdf0e10cSrcweir } 159cdf0e10cSrcweir 160cdf0e10cSrcweir sal_Int32 VCLXFont::getStringWidth( const ::rtl::OUString& str ) throw(::com::sun::star::uno::RuntimeException) 161cdf0e10cSrcweir { 162cdf0e10cSrcweir ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); 163cdf0e10cSrcweir 164cdf0e10cSrcweir sal_Int32 nRet = -1; 165cdf0e10cSrcweir OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice ); 166cdf0e10cSrcweir if ( pOutDev ) 167cdf0e10cSrcweir { 168cdf0e10cSrcweir Font aOldFont = pOutDev->GetFont(); 169cdf0e10cSrcweir pOutDev->SetFont( maFont ); 170cdf0e10cSrcweir nRet = pOutDev->GetTextWidth( str ); 171cdf0e10cSrcweir pOutDev->SetFont( aOldFont ); 172cdf0e10cSrcweir } 173cdf0e10cSrcweir return nRet; 174cdf0e10cSrcweir } 175cdf0e10cSrcweir 176cdf0e10cSrcweir sal_Int32 VCLXFont::getStringWidthArray( const ::rtl::OUString& str, ::com::sun::star::uno::Sequence< sal_Int32 >& rDXArray ) throw(::com::sun::star::uno::RuntimeException) 177cdf0e10cSrcweir { 178cdf0e10cSrcweir ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); 179cdf0e10cSrcweir 180cdf0e10cSrcweir sal_Int32 nRet = -1; 181cdf0e10cSrcweir OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice ); 182cdf0e10cSrcweir if ( pOutDev ) 183cdf0e10cSrcweir { 184cdf0e10cSrcweir Font aOldFont = pOutDev->GetFont(); 185cdf0e10cSrcweir pOutDev->SetFont( maFont ); 186cdf0e10cSrcweir rDXArray = ::com::sun::star::uno::Sequence<sal_Int32>( str.getLength() ); 187cdf0e10cSrcweir nRet = pOutDev->GetTextArray( str, rDXArray.getArray() ); 188cdf0e10cSrcweir pOutDev->SetFont( aOldFont ); 189cdf0e10cSrcweir } 190cdf0e10cSrcweir return nRet; 191cdf0e10cSrcweir } 192cdf0e10cSrcweir 1936c6cc06cSHerbert Dürr 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) 194cdf0e10cSrcweir { 1956c6cc06cSHerbert Dürr // NOTE: this empty method is just used for keeping the related UNO-API stable 196cdf0e10cSrcweir } 197cdf0e10cSrcweir 198cdf0e10cSrcweir // ::com::sun::star::awt::XFont2 199cdf0e10cSrcweir sal_Bool VCLXFont::hasGlyphs( const ::rtl::OUString& aText ) 200cdf0e10cSrcweir throw(::com::sun::star::uno::RuntimeException) 201cdf0e10cSrcweir { 202cdf0e10cSrcweir ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); 203cdf0e10cSrcweir 204cdf0e10cSrcweir OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice ); 205cdf0e10cSrcweir if ( pOutDev ) 206cdf0e10cSrcweir { 207cdf0e10cSrcweir String aStr( aText ); 208cdf0e10cSrcweir if ( pOutDev->HasGlyphs( maFont, aStr, 0, aStr.Len() ) == STRING_LEN ) 209cdf0e10cSrcweir { 210cdf0e10cSrcweir return sal_True; 211cdf0e10cSrcweir } 212cdf0e10cSrcweir } 213cdf0e10cSrcweir 214cdf0e10cSrcweir return sal_False; 215cdf0e10cSrcweir } 216