1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vclxfont.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_toolkit.hxx"
35 #include <toolkit/awt/vclxfont.hxx>
36 #include <toolkit/helper/vclunohelper.hxx>
37 #include <toolkit/helper/macros.hxx>
38 #include <cppuhelper/typeprovider.hxx>
39 #include <rtl/memory.h>
41 #include <rtl/ustring.h>
43 #include <vcl/outdev.hxx>
45 // ----------------------------------------------------
47 // ----------------------------------------------------
58 void VCLXFont::Init( ::com::sun::star::awt::XDevice
& rxDev
, const Font
& rFont
)
68 BOOL
VCLXFont::ImplAssertValidFontMetric()
70 if ( !mpFontMetric
&& mxDevice
.is() )
72 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
75 Font aOldFont
= pOutDev
->GetFont();
76 pOutDev
->SetFont( maFont
);
77 mpFontMetric
= new FontMetric( pOutDev
->GetFontMetric() );
78 pOutDev
->SetFont( aOldFont
);
81 return mpFontMetric
? TRUE
: FALSE
;
85 // ::com::sun::star::uno::XInterface
86 ::com::sun::star::uno::Any
VCLXFont::queryInterface( const ::com::sun::star::uno::Type
& rType
) throw(::com::sun::star::uno::RuntimeException
)
88 ::com::sun::star::uno::Any aRet
= ::cppu::queryInterface( rType
,
89 SAL_STATIC_CAST( ::com::sun::star::awt::XFont
*, this ),
90 SAL_STATIC_CAST( ::com::sun::star::awt::XFont2
*, this ),
91 SAL_STATIC_CAST( ::com::sun::star::lang::XUnoTunnel
*, this ),
92 SAL_STATIC_CAST( ::com::sun::star::lang::XTypeProvider
*, this ) );
93 return (aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
));
96 // ::com::sun::star::lang::XUnoTunnel
97 IMPL_XUNOTUNNEL( VCLXFont
)
99 // ::com::sun::star::lang::XTypeProvider
100 IMPL_XTYPEPROVIDER_START( VCLXFont
)
101 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XFont2
>* ) NULL
)
102 IMPL_XTYPEPROVIDER_END
105 ::com::sun::star::awt::FontDescriptor
VCLXFont::getFontDescriptor( ) throw(::com::sun::star::uno::RuntimeException
)
107 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
109 return VCLUnoHelper::CreateFontDescriptor( maFont
);
113 ::com::sun::star::awt::SimpleFontMetric
VCLXFont::getFontMetric( ) throw(::com::sun::star::uno::RuntimeException
)
115 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
117 ::com::sun::star::awt::SimpleFontMetric aFM
;
118 if ( ImplAssertValidFontMetric() )
119 aFM
= VCLUnoHelper::CreateFontMetric( *mpFontMetric
);
123 sal_Int16
VCLXFont::getCharWidth( sal_Unicode c
) throw(::com::sun::star::uno::RuntimeException
)
125 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
128 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
131 Font aOldFont
= pOutDev
->GetFont();
132 pOutDev
->SetFont( maFont
);
134 nRet
= sal::static_int_cast
< sal_Int16
>(
135 pOutDev
->GetTextWidth( String(c
) ));
137 pOutDev
->SetFont( aOldFont
);
142 ::com::sun::star::uno::Sequence
< sal_Int16
> VCLXFont::getCharWidths( sal_Unicode nFirst
, sal_Unicode nLast
) throw(::com::sun::star::uno::RuntimeException
)
144 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
146 ::com::sun::star::uno::Sequence
<sal_Int16
> aSeq
;
147 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
150 Font aOldFont
= pOutDev
->GetFont();
151 pOutDev
->SetFont( maFont
);
153 sal_Int16 nCount
= nLast
-nFirst
+ 1;
154 aSeq
= ::com::sun::star::uno::Sequence
<sal_Int16
>( nCount
);
155 for ( USHORT n
= 0; n
< nCount
; n
++ )
157 aSeq
.getArray()[n
] = sal::static_int_cast
< sal_Int16
>(
158 pOutDev
->GetTextWidth(
159 String(static_cast< sal_Unicode
>(nFirst
+n
)) ));
162 pOutDev
->SetFont( aOldFont
);
167 sal_Int32
VCLXFont::getStringWidth( const ::rtl::OUString
& str
) throw(::com::sun::star::uno::RuntimeException
)
169 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
172 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
175 Font aOldFont
= pOutDev
->GetFont();
176 pOutDev
->SetFont( maFont
);
177 nRet
= pOutDev
->GetTextWidth( str
);
178 pOutDev
->SetFont( aOldFont
);
183 sal_Int32
VCLXFont::getStringWidthArray( const ::rtl::OUString
& str
, ::com::sun::star::uno::Sequence
< sal_Int32
>& rDXArray
) throw(::com::sun::star::uno::RuntimeException
)
185 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
188 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
191 Font aOldFont
= pOutDev
->GetFont();
192 pOutDev
->SetFont( maFont
);
193 rDXArray
= ::com::sun::star::uno::Sequence
<sal_Int32
>( str
.getLength() );
194 nRet
= pOutDev
->GetTextArray( str
, rDXArray
.getArray() );
195 pOutDev
->SetFont( aOldFont
);
200 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
)
202 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
204 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
207 Font aOldFont
= pOutDev
->GetFont();
208 pOutDev
->SetFont( maFont
);
210 ULONG nPairs
= pOutDev
->GetKerningPairCount();
213 KerningPair
* pData
= new KerningPair
[ nPairs
];
214 pOutDev
->GetKerningPairs( nPairs
, pData
);
216 rnChars1
= ::com::sun::star::uno::Sequence
<sal_Unicode
>( nPairs
);
217 rnChars2
= ::com::sun::star::uno::Sequence
<sal_Unicode
>( nPairs
);
218 rnKerns
= ::com::sun::star::uno::Sequence
<sal_Int16
>( nPairs
);
220 sal_Unicode
* pChars1
= rnChars1
.getArray();
221 sal_Unicode
* pChars2
= rnChars2
.getArray();
222 sal_Int16
* pKerns
= rnKerns
.getArray();
224 for ( ULONG n
= 0; n
< nPairs
; n
++ )
226 pChars1
[n
] = pData
[n
].nChar1
;
227 pChars2
[n
] = pData
[n
].nChar2
;
228 pKerns
[n
] = sal::static_int_cast
< sal_Int16
>(pData
[n
].nKern
);
234 pOutDev
->SetFont( aOldFont
);
238 // ::com::sun::star::awt::XFont2
239 sal_Bool
VCLXFont::hasGlyphs( const ::rtl::OUString
& aText
)
240 throw(::com::sun::star::uno::RuntimeException
)
242 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
244 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
247 String
aStr( aText
);
248 if ( pOutDev
->HasGlyphs( maFont
, aStr
, 0, aStr
.Len() ) == STRING_LEN
)