xref: /aoo41x/main/toolkit/source/awt/vclxfont.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_toolkit.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <toolkit/awt/vclxfont.hxx>
33*cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx>
34*cdf0e10cSrcweir #include <toolkit/helper/macros.hxx>
35*cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
36*cdf0e10cSrcweir #include <rtl/memory.h>
37*cdf0e10cSrcweir #include <rtl/uuid.h>
38*cdf0e10cSrcweir #include <rtl/ustring.h>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include <vcl/outdev.hxx>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir //	----------------------------------------------------
43*cdf0e10cSrcweir //	class VCLXFont
44*cdf0e10cSrcweir //	----------------------------------------------------
45*cdf0e10cSrcweir VCLXFont::VCLXFont()
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir 	mpFontMetric = NULL;
48*cdf0e10cSrcweir }
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir VCLXFont::~VCLXFont()
51*cdf0e10cSrcweir {
52*cdf0e10cSrcweir 	delete mpFontMetric;
53*cdf0e10cSrcweir }
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir void VCLXFont::Init( ::com::sun::star::awt::XDevice& rxDev, const Font& rFont )
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir 	mxDevice = &rxDev;
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir 	delete mpFontMetric;
60*cdf0e10cSrcweir 	mpFontMetric = NULL;
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 	maFont = rFont;
63*cdf0e10cSrcweir }
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir sal_Bool VCLXFont::ImplAssertValidFontMetric()
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir 	if ( !mpFontMetric && mxDevice.is() )
68*cdf0e10cSrcweir 	{
69*cdf0e10cSrcweir 		OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
70*cdf0e10cSrcweir 		if ( pOutDev )
71*cdf0e10cSrcweir 		{
72*cdf0e10cSrcweir 			Font aOldFont = pOutDev->GetFont();
73*cdf0e10cSrcweir 			pOutDev->SetFont( maFont );
74*cdf0e10cSrcweir 			mpFontMetric = new FontMetric( pOutDev->GetFontMetric() );
75*cdf0e10cSrcweir 			pOutDev->SetFont( aOldFont );
76*cdf0e10cSrcweir 		}
77*cdf0e10cSrcweir 	}
78*cdf0e10cSrcweir 	return mpFontMetric ? sal_True : sal_False;
79*cdf0e10cSrcweir }
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir // ::com::sun::star::uno::XInterface
83*cdf0e10cSrcweir ::com::sun::star::uno::Any VCLXFont::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException)
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir 	::com::sun::star::uno::Any aRet = ::cppu::queryInterface( rType,
86*cdf0e10cSrcweir 										SAL_STATIC_CAST( ::com::sun::star::awt::XFont*, this ),
87*cdf0e10cSrcweir                                         SAL_STATIC_CAST( ::com::sun::star::awt::XFont2*, this ),
88*cdf0e10cSrcweir 										SAL_STATIC_CAST( ::com::sun::star::lang::XUnoTunnel*, this ),
89*cdf0e10cSrcweir 										SAL_STATIC_CAST( ::com::sun::star::lang::XTypeProvider*, this ) );
90*cdf0e10cSrcweir 	return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
91*cdf0e10cSrcweir }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir // ::com::sun::star::lang::XUnoTunnel
94*cdf0e10cSrcweir IMPL_XUNOTUNNEL( VCLXFont )
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir // ::com::sun::star::lang::XTypeProvider
97*cdf0e10cSrcweir IMPL_XTYPEPROVIDER_START( VCLXFont )
98*cdf0e10cSrcweir     getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::awt::XFont2>* ) NULL )
99*cdf0e10cSrcweir IMPL_XTYPEPROVIDER_END
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir ::com::sun::star::awt::FontDescriptor VCLXFont::getFontDescriptor(  ) throw(::com::sun::star::uno::RuntimeException)
103*cdf0e10cSrcweir {
104*cdf0e10cSrcweir 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 	return VCLUnoHelper::CreateFontDescriptor( maFont );
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir ::com::sun::star::awt::SimpleFontMetric VCLXFont::getFontMetric(  ) throw(::com::sun::star::uno::RuntimeException)
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 	::com::sun::star::awt::SimpleFontMetric aFM;
115*cdf0e10cSrcweir 	if ( ImplAssertValidFontMetric() )
116*cdf0e10cSrcweir 		aFM = VCLUnoHelper::CreateFontMetric( *mpFontMetric );
117*cdf0e10cSrcweir 	return aFM;
118*cdf0e10cSrcweir }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir sal_Int16 VCLXFont::getCharWidth( sal_Unicode c ) throw(::com::sun::star::uno::RuntimeException)
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 	sal_Int16 nRet = -1;
125*cdf0e10cSrcweir 	OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
126*cdf0e10cSrcweir 	if ( pOutDev )
127*cdf0e10cSrcweir 	{
128*cdf0e10cSrcweir 		Font aOldFont = pOutDev->GetFont();
129*cdf0e10cSrcweir 		pOutDev->SetFont( maFont );
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 		nRet = sal::static_int_cast< sal_Int16 >(
132*cdf0e10cSrcweir             pOutDev->GetTextWidth( String(c) ));
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 		pOutDev->SetFont( aOldFont );
135*cdf0e10cSrcweir 	}
136*cdf0e10cSrcweir 	return nRet;
137*cdf0e10cSrcweir }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir ::com::sun::star::uno::Sequence< sal_Int16 > VCLXFont::getCharWidths( sal_Unicode nFirst, sal_Unicode nLast ) throw(::com::sun::star::uno::RuntimeException)
140*cdf0e10cSrcweir {
141*cdf0e10cSrcweir 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 	::com::sun::star::uno::Sequence<sal_Int16> aSeq;
144*cdf0e10cSrcweir 	OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
145*cdf0e10cSrcweir 	if ( pOutDev )
146*cdf0e10cSrcweir 	{
147*cdf0e10cSrcweir 		Font aOldFont = pOutDev->GetFont();
148*cdf0e10cSrcweir 		pOutDev->SetFont( maFont );
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 		sal_Int16 nCount = nLast-nFirst + 1;
151*cdf0e10cSrcweir 		aSeq = ::com::sun::star::uno::Sequence<sal_Int16>( nCount );
152*cdf0e10cSrcweir 		for ( sal_uInt16 n = 0; n < nCount; n++ )
153*cdf0e10cSrcweir 		{
154*cdf0e10cSrcweir 			aSeq.getArray()[n] = sal::static_int_cast< sal_Int16 >(
155*cdf0e10cSrcweir                 pOutDev->GetTextWidth(
156*cdf0e10cSrcweir                     String(static_cast< sal_Unicode >(nFirst+n)) ));
157*cdf0e10cSrcweir 		}
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 		pOutDev->SetFont( aOldFont );
160*cdf0e10cSrcweir 	}
161*cdf0e10cSrcweir 	return aSeq;
162*cdf0e10cSrcweir }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir sal_Int32 VCLXFont::getStringWidth( const ::rtl::OUString& str ) throw(::com::sun::star::uno::RuntimeException)
165*cdf0e10cSrcweir {
166*cdf0e10cSrcweir 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 	sal_Int32 nRet = -1;
169*cdf0e10cSrcweir 	OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
170*cdf0e10cSrcweir 	if ( pOutDev )
171*cdf0e10cSrcweir 	{
172*cdf0e10cSrcweir 		Font aOldFont = pOutDev->GetFont();
173*cdf0e10cSrcweir 		pOutDev->SetFont( maFont );
174*cdf0e10cSrcweir 		nRet = pOutDev->GetTextWidth( str );
175*cdf0e10cSrcweir 		pOutDev->SetFont( aOldFont );
176*cdf0e10cSrcweir 	}
177*cdf0e10cSrcweir 	return nRet;
178*cdf0e10cSrcweir }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir sal_Int32 VCLXFont::getStringWidthArray( const ::rtl::OUString& str, ::com::sun::star::uno::Sequence< sal_Int32 >& rDXArray ) throw(::com::sun::star::uno::RuntimeException)
181*cdf0e10cSrcweir {
182*cdf0e10cSrcweir 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 	sal_Int32 nRet = -1;
185*cdf0e10cSrcweir 	OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
186*cdf0e10cSrcweir 	if ( pOutDev )
187*cdf0e10cSrcweir 	{
188*cdf0e10cSrcweir 		Font aOldFont = pOutDev->GetFont();
189*cdf0e10cSrcweir 		pOutDev->SetFont( maFont );
190*cdf0e10cSrcweir 		rDXArray = ::com::sun::star::uno::Sequence<sal_Int32>( str.getLength() );
191*cdf0e10cSrcweir 		nRet = pOutDev->GetTextArray( str, rDXArray.getArray() );
192*cdf0e10cSrcweir 		pOutDev->SetFont( aOldFont );
193*cdf0e10cSrcweir 	}
194*cdf0e10cSrcweir 	return nRet;
195*cdf0e10cSrcweir }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 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)
198*cdf0e10cSrcweir {
199*cdf0e10cSrcweir 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 	OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
202*cdf0e10cSrcweir 	if( pOutDev )
203*cdf0e10cSrcweir 	{
204*cdf0e10cSrcweir 		Font aOldFont = pOutDev->GetFont();
205*cdf0e10cSrcweir 		pOutDev->SetFont( maFont );
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 		sal_uLong nPairs = pOutDev->GetKerningPairCount();
208*cdf0e10cSrcweir 		if ( nPairs )
209*cdf0e10cSrcweir 		{
210*cdf0e10cSrcweir 			KerningPair* pData = new KerningPair[ nPairs ];
211*cdf0e10cSrcweir 			pOutDev->GetKerningPairs( nPairs, pData );
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir 			rnChars1 = ::com::sun::star::uno::Sequence<sal_Unicode>( nPairs );
214*cdf0e10cSrcweir 			rnChars2 = ::com::sun::star::uno::Sequence<sal_Unicode>( nPairs );
215*cdf0e10cSrcweir 			rnKerns = ::com::sun::star::uno::Sequence<sal_Int16>( nPairs );
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 			sal_Unicode* pChars1 = rnChars1.getArray();
218*cdf0e10cSrcweir 			sal_Unicode* pChars2 = rnChars2.getArray();
219*cdf0e10cSrcweir 			sal_Int16* pKerns = rnKerns.getArray();
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir 			for ( sal_uLong n = 0; n < nPairs; n++ )
222*cdf0e10cSrcweir 			{
223*cdf0e10cSrcweir 				pChars1[n] = pData[n].nChar1;
224*cdf0e10cSrcweir 				pChars2[n] = pData[n].nChar2;
225*cdf0e10cSrcweir 				pKerns[n] = sal::static_int_cast< sal_Int16 >(pData[n].nKern);
226*cdf0e10cSrcweir 			}
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir 			delete[] pData;
230*cdf0e10cSrcweir 		}
231*cdf0e10cSrcweir 		pOutDev->SetFont( aOldFont );
232*cdf0e10cSrcweir 	}
233*cdf0e10cSrcweir }
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir // ::com::sun::star::awt::XFont2
236*cdf0e10cSrcweir sal_Bool VCLXFont::hasGlyphs( const ::rtl::OUString& aText )
237*cdf0e10cSrcweir     throw(::com::sun::star::uno::RuntimeException)
238*cdf0e10cSrcweir {
239*cdf0e10cSrcweir 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir     OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
242*cdf0e10cSrcweir     if ( pOutDev )
243*cdf0e10cSrcweir     {
244*cdf0e10cSrcweir         String aStr( aText );
245*cdf0e10cSrcweir         if ( pOutDev->HasGlyphs( maFont, aStr, 0, aStr.Len() ) == STRING_LEN )
246*cdf0e10cSrcweir         {
247*cdf0e10cSrcweir             return sal_True;
248*cdf0e10cSrcweir         }
249*cdf0e10cSrcweir     }
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir     return sal_False;
252*cdf0e10cSrcweir }
253