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