xref: /AOO41X/main/vbahelper/source/vbahelper/vbafontbase.cxx (revision e6ed5fbc51cf474df369618b58e945b84a21a167)
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 #include <com/sun/star/beans/XProperty.hpp>
24 #include <com/sun/star/awt/FontWeight.hpp>
25 #include <com/sun/star/awt/FontUnderline.hpp>
26 #include <com/sun/star/awt/FontStrikeout.hpp>
27 #include <com/sun/star/awt/FontSlant.hpp>
28 #include <com/sun/star/text/XSimpleText.hpp>
29 #include <vbahelper/vbafontbase.hxx>
30 
31 using namespace ::ooo::vba;
32 using namespace ::com::sun::star;
33 
34 // form controls use other property name as the remaining OOo API
35 #define VBAFONTBASE_PROPNAME( ascii_normal, ascii_control ) \
36     mbFormControl ? rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ascii_control ) ) : rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ascii_normal ) )
37 
VbaFontBase(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<css::container::XIndexAccess> & xPalette,const uno::Reference<beans::XPropertySet> & xPropertySet,bool bFormControl)38 VbaFontBase::VbaFontBase(
39         const uno::Reference< XHelperInterface >& xParent,
40         const uno::Reference< uno::XComponentContext >& xContext,
41         const uno::Reference< css::container::XIndexAccess >& xPalette,
42         const uno::Reference< beans::XPropertySet >& xPropertySet,
43         bool bFormControl ) throw ( uno::RuntimeException ) :
44     VbaFontBase_BASE( xParent, xContext ),
45     mxFont( xPropertySet, uno::UNO_SET_THROW ),
46     mxPalette( xPalette, uno::UNO_SET_THROW ),
47     mbFormControl( bFormControl )
48 {
49 }
50 
~VbaFontBase()51 VbaFontBase::~VbaFontBase()
52 {
53 }
54 
55 void SAL_CALL
setSuperscript(const uno::Any & aValue)56 VbaFontBase::setSuperscript( const uno::Any& aValue ) throw ( uno::RuntimeException )
57 {
58     // not supported in form controls
59     if( mbFormControl )
60         return;
61 
62     sal_Bool bValue = sal_False;
63     aValue >>= bValue;
64     sal_Int16 nValue = NORMAL;
65     sal_Int8 nValue2 = NORMALHEIGHT;
66 
67     if( bValue )
68     {
69         nValue = SUPERSCRIPT;
70         nValue2 = SUPERSCRIPTHEIGHT;
71     }
72     mxFont->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharEscapement" ) ), ( uno::Any )nValue );
73     mxFont->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharEscapementHeight" ) ), ( uno::Any )nValue2 );
74 }
75 
76 uno::Any SAL_CALL
getSuperscript()77 VbaFontBase::getSuperscript() throw ( uno::RuntimeException )
78 {
79     short nValue = NORMAL;
80     // not supported in form controls
81     if( !mbFormControl )
82        mxFont->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharEscapement" ) ) ) >>= nValue;
83     return uno::makeAny( ( nValue == SUPERSCRIPT ) );
84 }
85 
86 void SAL_CALL
setSubscript(const uno::Any & aValue)87 VbaFontBase::setSubscript( const uno::Any& aValue ) throw ( uno::RuntimeException )
88 {
89     // not supported in form controls
90     if( mbFormControl )
91         return;
92 
93     sal_Bool bValue = sal_False;
94     aValue >>= bValue;
95     sal_Int16 nValue = NORMAL;
96     sal_Int8 nValue2 = NORMALHEIGHT;
97 
98     if( bValue )
99     {
100         nValue= SUBSCRIPT;
101         nValue2 = SUBSCRIPTHEIGHT;
102     }
103 
104     mxFont->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharEscapementHeight" ) ), ( uno::Any )nValue2 );
105     mxFont->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharEscapement" ) ), ( uno::Any )nValue );
106 
107 }
108 
109 uno::Any SAL_CALL
getSubscript()110 VbaFontBase::getSubscript() throw ( uno::RuntimeException )
111 {
112     short nValue = NORMAL;
113     // not supported in form controls
114     if( !mbFormControl )
115        mxFont->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharEscapement" ) ) ) >>= nValue;
116     return uno::makeAny( ( nValue == SUBSCRIPT ) );
117 }
118 
119 void SAL_CALL
setSize(const uno::Any & aValue)120 VbaFontBase::setSize( const uno::Any& aValue ) throw( uno::RuntimeException )
121 {
122     // form controls need a sal_Int16 containing points, other APIs need a float
123     uno::Any aVal( aValue );
124     if( mbFormControl )
125     {
126         float fVal = 0.0;
127         aVal >>= fVal;
128         aVal <<= static_cast< sal_Int16 >( fVal );
129     }
130     mxFont->setPropertyValue( VBAFONTBASE_PROPNAME( "CharHeight", "FontHeight" ), aVal );
131 }
132 
133 uno::Any SAL_CALL
getSize()134 VbaFontBase::getSize() throw ( uno::RuntimeException )
135 {
136     return mxFont->getPropertyValue( VBAFONTBASE_PROPNAME( "CharHeight", "FontHeight" ) );
137 }
138 
139 void SAL_CALL
setColorIndex(const uno::Any & _colorindex)140 VbaFontBase::setColorIndex( const uno::Any& _colorindex ) throw( uno::RuntimeException )
141 {
142     sal_Int32 nIndex = 0;
143     _colorindex >>= nIndex;
144 
145     --nIndex; // OOo indices are zero bases
146 
147     // setColor expects colors in XL RGB values
148     // #FIXME this is daft we convert OO RGB val to XL RGB val and
149     // then back again to OO RGB value
150     setColor( OORGBToXLRGB(mxPalette->getByIndex( nIndex )) );
151 }
152 
153 
154 uno::Any SAL_CALL
getColorIndex()155 VbaFontBase::getColorIndex() throw ( uno::RuntimeException )
156 {
157     sal_Int32 nColor = 0;
158 
159     XLRGBToOORGB( getColor() ) >>= nColor;
160     sal_Int32 nElems = mxPalette->getCount();
161     sal_Int32 nIndex = -1;
162     for ( sal_Int32 count=0; count<nElems; ++count )
163         {
164         sal_Int32 nPaletteColor = 0;
165         mxPalette->getByIndex( count ) >>= nPaletteColor;
166         if ( nPaletteColor == nColor )
167         {
168             nIndex = count + 1; // 1 based
169             break;
170         }
171     }
172     return uno::makeAny( nIndex );
173 }
174 
175 void SAL_CALL
setBold(const uno::Any & aValue)176 VbaFontBase::setBold( const uno::Any& aValue ) throw( uno::RuntimeException )
177 {
178     sal_Bool bValue = sal_False;
179     aValue >>= bValue;
180     double fBoldValue = awt::FontWeight::NORMAL;
181     if( bValue )
182         fBoldValue = awt::FontWeight::BOLD;
183     mxFont->setPropertyValue( VBAFONTBASE_PROPNAME( "CharWeight", "FontWeight" ), uno::Any( fBoldValue ) );
184 
185 }
186 
187 uno::Any SAL_CALL
getBold()188 VbaFontBase::getBold() throw ( uno::RuntimeException )
189 {
190     double fValue = 0.0;
191     mxFont->getPropertyValue( VBAFONTBASE_PROPNAME( "CharWeight", "FontWeight" ) ) >>= fValue;
192     return uno::makeAny( fValue == awt::FontWeight::BOLD );
193 }
194 
195 void SAL_CALL
setStrikethrough(const uno::Any & aValue)196 VbaFontBase::setStrikethrough( const uno::Any& aValue ) throw ( uno::RuntimeException )
197 {
198     sal_Bool bValue = sal_False;
199     aValue >>= bValue;
200     short nValue = awt::FontStrikeout::NONE;
201     if( bValue )
202         nValue = awt::FontStrikeout::SINGLE;
203     mxFont->setPropertyValue( VBAFONTBASE_PROPNAME( "CharStrikeout", "FontStrikeout" ), uno::Any( nValue ) );
204 }
205 
206 uno::Any SAL_CALL
getStrikethrough()207 VbaFontBase::getStrikethrough() throw ( uno::RuntimeException )
208 {
209     short nValue = 0;
210     mxFont->getPropertyValue( VBAFONTBASE_PROPNAME( "CharStrikeout", "FontStrikeout" ) ) >>= nValue;
211     return uno::Any( nValue == awt::FontStrikeout::SINGLE );
212 }
213 
214 void  SAL_CALL
setShadow(const uno::Any & aValue)215 VbaFontBase::setShadow( const uno::Any& aValue ) throw ( uno::RuntimeException )
216 {
217     if( !mbFormControl )
218        mxFont->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharShadowed" ) ), aValue );
219 }
220 
221 uno::Any SAL_CALL
getShadow()222 VbaFontBase::getShadow() throw (uno::RuntimeException)
223 {
224     return mbFormControl ? uno::Any( false ) : mxFont->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharShadowed" ) ) );
225 }
226 
227 void  SAL_CALL
setItalic(const uno::Any & aValue)228 VbaFontBase::setItalic( const uno::Any& aValue ) throw ( uno::RuntimeException )
229 {
230     sal_Bool bValue = sal_False;
231     aValue >>= bValue;
232     short nValue = awt::FontSlant_NONE;
233     if( bValue )
234         nValue = awt::FontSlant_ITALIC;
235     mxFont->setPropertyValue( VBAFONTBASE_PROPNAME( "CharPosture", "FontSlant" ), uno::Any( nValue ) );
236 }
237 
238 uno::Any SAL_CALL
getItalic()239 VbaFontBase::getItalic() throw ( uno::RuntimeException )
240 {
241     awt::FontSlant aFS;
242     mxFont->getPropertyValue( VBAFONTBASE_PROPNAME( "CharPosture", "FontSlant" ) ) >>= aFS;
243     return uno::makeAny( aFS == awt::FontSlant_ITALIC );
244 }
245 
246 void  SAL_CALL
setName(const uno::Any & aValue)247 VbaFontBase::setName( const uno::Any& aValue ) throw ( uno::RuntimeException )
248 {
249     rtl::OUString sString;
250     aValue >>= sString;
251     mxFont->setPropertyValue( VBAFONTBASE_PROPNAME( "CharFontName", "FontName" ), aValue );
252 }
253 
254 uno::Any SAL_CALL
getName()255 VbaFontBase::getName() throw ( uno::RuntimeException )
256 {
257     return mxFont->getPropertyValue( VBAFONTBASE_PROPNAME( "CharFontName", "FontName" ) );
258 }
259 
260 uno::Any
getColor()261 VbaFontBase::getColor() throw (uno::RuntimeException)
262 {
263     uno::Any aAny;
264     aAny = OORGBToXLRGB( mxFont->getPropertyValue( VBAFONTBASE_PROPNAME( "CharColor", "TextColor" ) ) );
265     return aAny;
266 }
267 
268 void
setColor(const uno::Any & _color)269 VbaFontBase::setColor( const uno::Any& _color  ) throw (uno::RuntimeException)
270 {
271     mxFont->setPropertyValue( VBAFONTBASE_PROPNAME( "CharColor", "TextColor" ), XLRGBToOORGB(_color) );
272 }
273 
274 void SAL_CALL
setUnderline(const uno::Any &)275 VbaFontBase::setUnderline( const uno::Any& /*aValue*/ ) throw ( uno::RuntimeException )
276 {
277 /*
278     // default
279     sal_Int32 nValue = excel::XlUnderlineStyle::xlUnderlineStyleNone;
280     aValue >>= nValue;
281     switch ( nValue )
282     {
283 // NOTE:: #TODO #FIMXE
284 // xlUnderlineStyleDoubleAccounting & xlUnderlineStyleSingleAccounting
285 // don't seem to be supported in Openoffice.
286 // The import filter converts them to single or double underlines as appropriate
287 // So, here at the moment we are similarly silently converting
288 // xlUnderlineStyleSingleAccounting to xlUnderlineStyleSingle.
289 
290         case excel::XlUnderlineStyle::xlUnderlineStyleNone:
291             nValue = awt::FontUnderline::NONE;
292             break;
293         case excel::XlUnderlineStyle::xlUnderlineStyleSingle:
294         case excel::XlUnderlineStyle::xlUnderlineStyleSingleAccounting:
295             nValue = awt::FontUnderline::SINGLE;
296             break;
297         case excel::XlUnderlineStyle::xlUnderlineStyleDouble:
298         case excel::XlUnderlineStyle::xlUnderlineStyleDoubleAccounting:
299             nValue = awt::FontUnderline::DOUBLE;
300             break;
301         default:
302             throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Unknown value for Underline")), uno::Reference< uno::XInterface >() );
303     }
304 
305     mxFont->setPropertyValue( VBAFONTBASE_PROPNAME( "CharUnderline", "FontUnderline" ), uno::Any( nValue ) );
306 */
307 
308 }
309 
310 uno::Any SAL_CALL
getUnderline()311 VbaFontBase::getUnderline() throw ( uno::RuntimeException )
312 {
313     sal_Int32 nValue = awt::FontUnderline::NONE;
314     mxFont->getPropertyValue( VBAFONTBASE_PROPNAME( "CharUnderline", "FontUnderline" ) ) >>= nValue;
315 /*
316     switch ( nValue )
317     {
318         case  awt::FontUnderline::DOUBLE:
319             nValue = excel::XlUnderlineStyle::xlUnderlineStyleDouble;
320             break;
321         case  awt::FontUnderline::SINGLE:
322             nValue = excel::XlUnderlineStyle::xlUnderlineStyleSingle;
323             break;
324         case  awt::FontUnderline::NONE:
325             nValue = excel::XlUnderlineStyle::xlUnderlineStyleNone;
326             break;
327         default:
328             throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Unknown value retrieved for Underline") ), uno::Reference< uno::XInterface >() );
329 
330     }
331 */
332     return uno::makeAny( nValue );
333 }
334 
335 
336