Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <string.h>
21 :
22 : #include <toolkit/awt/vclxfont.hxx>
23 : #include <toolkit/helper/vclunohelper.hxx>
24 : #include <toolkit/helper/macros.hxx>
25 : #include <cppuhelper/typeprovider.hxx>
26 : #include <rtl/uuid.h>
27 : #include <rtl/ustring.h>
28 :
29 : #include <vcl/outdev.hxx>
30 :
31 :
32 : // class VCLXFont
33 :
34 686 : VCLXFont::VCLXFont()
35 : {
36 686 : mpFontMetric = NULL;
37 686 : }
38 :
39 2058 : VCLXFont::~VCLXFont()
40 : {
41 686 : delete mpFontMetric;
42 1372 : }
43 :
44 686 : void VCLXFont::Init( ::com::sun::star::awt::XDevice& rxDev, const vcl::Font& rFont )
45 : {
46 686 : mxDevice = &rxDev;
47 :
48 686 : delete mpFontMetric;
49 686 : mpFontMetric = NULL;
50 :
51 686 : maFont = rFont;
52 686 : }
53 :
54 0 : bool VCLXFont::ImplAssertValidFontMetric()
55 : {
56 0 : if ( !mpFontMetric && mxDevice.is() )
57 : {
58 0 : OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
59 0 : if ( pOutDev )
60 : {
61 0 : vcl::Font aOldFont = pOutDev->GetFont();
62 0 : pOutDev->SetFont( maFont );
63 0 : mpFontMetric = new FontMetric( pOutDev->GetFontMetric() );
64 0 : pOutDev->SetFont( aOldFont );
65 : }
66 : }
67 0 : return mpFontMetric ? sal_True : sal_False;
68 : }
69 :
70 :
71 : // ::com::sun::star::uno::XInterface
72 558 : ::com::sun::star::uno::Any VCLXFont::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException, std::exception)
73 : {
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 558 : (static_cast< ::com::sun::star::lang::XTypeProvider* >(this)) );
79 558 : return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
80 : }
81 :
82 : // ::com::sun::star::lang::XUnoTunnel
83 0 : IMPL_XUNOTUNNEL( VCLXFont )
84 :
85 : // ::com::sun::star::lang::XTypeProvider
86 0 : IMPL_XTYPEPROVIDER_START( VCLXFont )
87 0 : cppu::UnoType<com::sun::star::awt::XFont2>::get()
88 0 : IMPL_XTYPEPROVIDER_END
89 :
90 :
91 0 : ::com::sun::star::awt::FontDescriptor VCLXFont::getFontDescriptor( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
92 : {
93 0 : ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
94 :
95 0 : return VCLUnoHelper::CreateFontDescriptor( maFont );
96 :
97 : }
98 :
99 0 : ::com::sun::star::awt::SimpleFontMetric VCLXFont::getFontMetric( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
100 : {
101 0 : ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
102 :
103 0 : ::com::sun::star::awt::SimpleFontMetric aFM;
104 0 : if ( ImplAssertValidFontMetric() )
105 0 : aFM = VCLUnoHelper::CreateFontMetric( *mpFontMetric );
106 0 : return aFM;
107 : }
108 :
109 1408 : sal_Int16 VCLXFont::getCharWidth( sal_Unicode c ) throw(::com::sun::star::uno::RuntimeException, std::exception)
110 : {
111 1408 : ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
112 :
113 1408 : sal_Int16 nRet = -1;
114 1408 : OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
115 1408 : if ( pOutDev )
116 : {
117 1408 : vcl::Font aOldFont = pOutDev->GetFont();
118 1408 : pOutDev->SetFont( maFont );
119 :
120 : nRet = sal::static_int_cast< sal_Int16 >(
121 1408 : pOutDev->GetTextWidth( OUString(c) ));
122 :
123 1408 : pOutDev->SetFont( aOldFont );
124 : }
125 1408 : return nRet;
126 : }
127 :
128 0 : ::com::sun::star::uno::Sequence< sal_Int16 > VCLXFont::getCharWidths( sal_Unicode nFirst, sal_Unicode nLast ) throw(::com::sun::star::uno::RuntimeException, std::exception)
129 : {
130 0 : ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
131 :
132 0 : ::com::sun::star::uno::Sequence<sal_Int16> aSeq;
133 0 : OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
134 0 : if ( pOutDev )
135 : {
136 0 : vcl::Font aOldFont = pOutDev->GetFont();
137 0 : pOutDev->SetFont( maFont );
138 :
139 0 : sal_Int16 nCount = nLast-nFirst + 1;
140 0 : aSeq = ::com::sun::star::uno::Sequence<sal_Int16>( nCount );
141 0 : for ( sal_uInt16 n = 0; n < nCount; n++ )
142 : {
143 0 : aSeq.getArray()[n] = sal::static_int_cast< sal_Int16 >(
144 : pOutDev->GetTextWidth(
145 0 : OUString(static_cast< sal_Unicode >(nFirst+n)) ));
146 : }
147 :
148 0 : pOutDev->SetFont( aOldFont );
149 : }
150 0 : return aSeq;
151 : }
152 :
153 0 : sal_Int32 VCLXFont::getStringWidth( const OUString& str ) throw(::com::sun::star::uno::RuntimeException, std::exception)
154 : {
155 0 : ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
156 :
157 0 : sal_Int32 nRet = -1;
158 0 : OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
159 0 : if ( pOutDev )
160 : {
161 0 : vcl::Font aOldFont = pOutDev->GetFont();
162 0 : pOutDev->SetFont( maFont );
163 0 : nRet = pOutDev->GetTextWidth( str );
164 0 : pOutDev->SetFont( aOldFont );
165 : }
166 0 : return nRet;
167 : }
168 :
169 0 : sal_Int32 VCLXFont::getStringWidthArray( const OUString& str, ::com::sun::star::uno::Sequence< sal_Int32 >& rDXArray ) throw(::com::sun::star::uno::RuntimeException, std::exception)
170 : {
171 0 : ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
172 :
173 0 : sal_Int32 nRet = -1;
174 0 : OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
175 0 : if ( pOutDev )
176 : {
177 0 : vcl::Font aOldFont = pOutDev->GetFont();
178 0 : pOutDev->SetFont( maFont );
179 0 : long* pDXA = (long*)alloca(str.getLength() * sizeof(long));
180 0 : nRet = pOutDev->GetTextArray( str, pDXA );
181 0 : rDXArray = ::com::sun::star::uno::Sequence<sal_Int32>( str.getLength() );
182 0 : for(int i = 0; i < str.getLength(); i++)
183 : {
184 0 : rDXArray[i] = pDXA[i];
185 : }
186 0 : pOutDev->SetFont( aOldFont );
187 : }
188 0 : return nRet;
189 : }
190 :
191 0 : 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, std::exception)
192 : {
193 : // NOTE: this empty method is just used for keeping the related UNO-API stable
194 0 : }
195 :
196 : // ::com::sun::star::awt::XFont2
197 8814 : sal_Bool VCLXFont::hasGlyphs( const OUString& aText )
198 : throw(::com::sun::star::uno::RuntimeException, std::exception)
199 : {
200 8814 : ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
201 :
202 8814 : OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
203 8814 : if ( pOutDev )
204 : {
205 8814 : OUString aStr( aText );
206 8814 : if ( pOutDev->HasGlyphs( maFont, aStr ) == -1 )
207 : {
208 828 : return sal_True;
209 7986 : }
210 : }
211 :
212 7986 : return sal_False;
213 1227 : }
214 :
215 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|