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 115 : VCLXFont::VCLXFont()
35 : {
36 115 : mpFontMetric = NULL;
37 115 : }
38 :
39 345 : VCLXFont::~VCLXFont()
40 : {
41 115 : delete mpFontMetric;
42 230 : }
43 :
44 115 : void VCLXFont::Init( ::com::sun::star::awt::XDevice& rxDev, const Font& rFont )
45 : {
46 115 : mxDevice = &rxDev;
47 :
48 115 : delete mpFontMetric;
49 115 : mpFontMetric = NULL;
50 :
51 115 : maFont = rFont;
52 115 : }
53 :
54 0 : sal_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 : 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 104 : ::com::sun::star::uno::Any VCLXFont::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException)
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 104 : (static_cast< ::com::sun::star::lang::XTypeProvider* >(this)) );
79 104 : 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 : getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::awt::XFont2>* ) NULL )
88 0 : IMPL_XTYPEPROVIDER_END
89 :
90 :
91 0 : ::com::sun::star::awt::FontDescriptor VCLXFont::getFontDescriptor( ) throw(::com::sun::star::uno::RuntimeException)
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)
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 121 : sal_Int16 VCLXFont::getCharWidth( sal_Unicode c ) throw(::com::sun::star::uno::RuntimeException)
110 : {
111 121 : ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
112 :
113 121 : sal_Int16 nRet = -1;
114 121 : OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
115 121 : if ( pOutDev )
116 : {
117 121 : Font aOldFont = pOutDev->GetFont();
118 121 : pOutDev->SetFont( maFont );
119 :
120 : nRet = sal::static_int_cast< sal_Int16 >(
121 121 : pOutDev->GetTextWidth( rtl::OUString(c) ));
122 :
123 121 : pOutDev->SetFont( aOldFont );
124 : }
125 121 : 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)
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 : 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 : rtl::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 ::rtl::OUString& str ) throw(::com::sun::star::uno::RuntimeException)
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 : 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 ::rtl::OUString& str, ::com::sun::star::uno::Sequence< sal_Int32 >& rDXArray ) throw(::com::sun::star::uno::RuntimeException)
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 : Font aOldFont = pOutDev->GetFont();
178 0 : pOutDev->SetFont( maFont );
179 0 : rDXArray = ::com::sun::star::uno::Sequence<sal_Int32>( str.getLength() );
180 0 : nRet = pOutDev->GetTextArray( str, rDXArray.getArray() );
181 0 : pOutDev->SetFont( aOldFont );
182 : }
183 0 : return nRet;
184 : }
185 :
186 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)
187 : {
188 : // NOTE: this empty method is just used for keeping the related UNO-API stable
189 0 : }
190 :
191 : // ::com::sun::star::awt::XFont2
192 2106 : sal_Bool VCLXFont::hasGlyphs( const ::rtl::OUString& aText )
193 : throw(::com::sun::star::uno::RuntimeException)
194 : {
195 2106 : ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
196 :
197 2106 : OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( mxDevice );
198 2106 : if ( pOutDev )
199 : {
200 2106 : String aStr( aText );
201 2106 : if ( pOutDev->HasGlyphs( maFont, aStr, 0, aStr.Len() ) == STRING_LEN )
202 : {
203 26 : return sal_True;
204 2106 : }
205 : }
206 :
207 2080 : return sal_False;
208 : }
209 :
210 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|