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