1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <toolkit/awt/vclxfont.hxx>
23 #include <toolkit/helper/vclunohelper.hxx>
24 #include <toolkit/helper/macros.hxx>
25 #include <cppuhelper/typeprovider.hxx>
27 #include <rtl/ustring.h>
29 #include <vcl/outdev.hxx>
31 // ----------------------------------------------------
33 // ----------------------------------------------------
44 void VCLXFont::Init( ::com::sun::star::awt::XDevice
& rxDev
, const Font
& rFont
)
54 sal_Bool
VCLXFont::ImplAssertValidFontMetric()
56 if ( !mpFontMetric
&& mxDevice
.is() )
58 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
61 Font aOldFont
= pOutDev
->GetFont();
62 pOutDev
->SetFont( maFont
);
63 mpFontMetric
= new FontMetric( pOutDev
->GetFontMetric() );
64 pOutDev
->SetFont( aOldFont
);
67 return mpFontMetric
? sal_True
: sal_False
;
71 // ::com::sun::star::uno::XInterface
72 ::com::sun::star::uno::Any
VCLXFont::queryInterface( const ::com::sun::star::uno::Type
& rType
) throw(::com::sun::star::uno::RuntimeException
)
74 ::com::sun::star::uno::Any aRet
= ::cppu::queryInterface( rType
,
75 (static_cast< ::com::sun::star::awt::XFont
* >(this)),
76 (static_cast< ::com::sun::star::awt::XFont2
* >(this)),
77 (static_cast< ::com::sun::star::lang::XUnoTunnel
* >(this)),
78 (static_cast< ::com::sun::star::lang::XTypeProvider
* >(this)) );
79 return (aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
));
82 // ::com::sun::star::lang::XUnoTunnel
83 IMPL_XUNOTUNNEL( VCLXFont
)
85 // ::com::sun::star::lang::XTypeProvider
86 IMPL_XTYPEPROVIDER_START( VCLXFont
)
87 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XFont2
>* ) NULL
)
88 IMPL_XTYPEPROVIDER_END
91 ::com::sun::star::awt::FontDescriptor
VCLXFont::getFontDescriptor( ) throw(::com::sun::star::uno::RuntimeException
)
93 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
95 return VCLUnoHelper::CreateFontDescriptor( maFont
);
99 ::com::sun::star::awt::SimpleFontMetric
VCLXFont::getFontMetric( ) throw(::com::sun::star::uno::RuntimeException
)
101 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
103 ::com::sun::star::awt::SimpleFontMetric aFM
;
104 if ( ImplAssertValidFontMetric() )
105 aFM
= VCLUnoHelper::CreateFontMetric( *mpFontMetric
);
109 sal_Int16
VCLXFont::getCharWidth( sal_Unicode c
) throw(::com::sun::star::uno::RuntimeException
)
111 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
114 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
117 Font aOldFont
= pOutDev
->GetFont();
118 pOutDev
->SetFont( maFont
);
120 nRet
= sal::static_int_cast
< sal_Int16
>(
121 pOutDev
->GetTextWidth( OUString(c
) ));
123 pOutDev
->SetFont( aOldFont
);
128 ::com::sun::star::uno::Sequence
< sal_Int16
> VCLXFont::getCharWidths( sal_Unicode nFirst
, sal_Unicode nLast
) throw(::com::sun::star::uno::RuntimeException
)
130 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
132 ::com::sun::star::uno::Sequence
<sal_Int16
> aSeq
;
133 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
136 Font aOldFont
= pOutDev
->GetFont();
137 pOutDev
->SetFont( maFont
);
139 sal_Int16 nCount
= nLast
-nFirst
+ 1;
140 aSeq
= ::com::sun::star::uno::Sequence
<sal_Int16
>( nCount
);
141 for ( sal_uInt16 n
= 0; n
< nCount
; n
++ )
143 aSeq
.getArray()[n
] = sal::static_int_cast
< sal_Int16
>(
144 pOutDev
->GetTextWidth(
145 OUString(static_cast< sal_Unicode
>(nFirst
+n
)) ));
148 pOutDev
->SetFont( aOldFont
);
153 sal_Int32
VCLXFont::getStringWidth( const OUString
& str
) throw(::com::sun::star::uno::RuntimeException
)
155 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
158 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
161 Font aOldFont
= pOutDev
->GetFont();
162 pOutDev
->SetFont( maFont
);
163 nRet
= pOutDev
->GetTextWidth( str
);
164 pOutDev
->SetFont( aOldFont
);
169 sal_Int32
VCLXFont::getStringWidthArray( const OUString
& str
, ::com::sun::star::uno::Sequence
< sal_Int32
>& rDXArray
) throw(::com::sun::star::uno::RuntimeException
)
171 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
174 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
177 Font aOldFont
= pOutDev
->GetFont();
178 pOutDev
->SetFont( maFont
);
179 rDXArray
= ::com::sun::star::uno::Sequence
<sal_Int32
>( str
.getLength() );
180 nRet
= pOutDev
->GetTextArray( str
, rDXArray
.getArray() );
181 pOutDev
->SetFont( aOldFont
);
186 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
)
188 // NOTE: this empty method is just used for keeping the related UNO-API stable
191 // ::com::sun::star::awt::XFont2
192 sal_Bool
VCLXFont::hasGlyphs( const OUString
& aText
)
193 throw(::com::sun::star::uno::RuntimeException
)
195 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
197 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
200 String
aStr( aText
);
201 if ( pOutDev
->HasGlyphs( maFont
, aStr
, 0, aStr
.Len() ) == STRING_LEN
)
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */