xref: /aoo42x/main/sw/source/ui/vba/vbafont.cxx (revision efeef26f)
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 #include "vbafont.hxx"
23 #include <com/sun/star/awt/FontUnderline.hpp>
24 #include <ooo/vba/word/WdUnderline.hpp>
25 #include <hash_map>
26 #include <ooo/vba/word/WdColorIndex.hpp>
27 
28 using namespace ::ooo::vba;
29 using namespace ::com::sun::star;
30 
31 const uno::Any aLongAnyTrue( sal_Int16(-1) );
32 const uno::Any aLongAnyFalse( sal_Int16( 0 ) );
33 
34 struct MapPair
35 {
36     sal_Int32 nMSOConst;
37     sal_Int32 nOOOConst;
38 };
39 
40 static MapPair UnderLineTable[] = {
41         { word::WdUnderline::wdUnderlineNone, com::sun::star::awt::FontUnderline::NONE },
42         { word::WdUnderline::wdUnderlineSingle, com::sun::star::awt::FontUnderline::SINGLE },
43         { word::WdUnderline::wdUnderlineWords, com::sun::star::awt::FontUnderline::SINGLE },
44         { word::WdUnderline::wdUnderlineDouble, com::sun::star::awt::FontUnderline::DOUBLE },
45         { word::WdUnderline::wdUnderlineDotted, com::sun::star::awt::FontUnderline::DOTTED },
46         { word::WdUnderline::wdUnderlineThick, com::sun::star::awt::FontUnderline::BOLDDASH },
47         { word::WdUnderline::wdUnderlineDash, com::sun::star::awt::FontUnderline::DASH },
48 	{ word::WdUnderline::wdUnderlineDotDash, com::sun::star::awt::FontUnderline::DASHDOT },
49 	{ word::WdUnderline::wdUnderlineDotDotDash, com::sun::star::awt::FontUnderline::DASHDOTDOT },
50         { word::WdUnderline::wdUnderlineWavy, com::sun::star::awt::FontUnderline::WAVE },
51         { word::WdUnderline::wdUnderlineDottedHeavy, com::sun::star::awt::FontUnderline::BOLDDOTTED },
52         { word::WdUnderline::wdUnderlineDashHeavy, com::sun::star::awt::FontUnderline::BOLDDASH },
53         { word::WdUnderline::wdUnderlineDotDashHeavy, com::sun::star::awt::FontUnderline::BOLDDASHDOT },
54         { word::WdUnderline::wdUnderlineDotDotDashHeavy, com::sun::star::awt::FontUnderline::BOLDDASHDOTDOT },
55         { word::WdUnderline::wdUnderlineWavyHeavy, com::sun::star::awt::FontUnderline::BOLDWAVE },
56         { word::WdUnderline::wdUnderlineDashLong, com::sun::star::awt::FontUnderline::LONGDASH },
57         { word::WdUnderline::wdUnderlineWavyDouble, com::sun::star::awt::FontUnderline::DOUBLEWAVE },
58         { word::WdUnderline::wdUnderlineDashLongHeavy, com::sun::star::awt::FontUnderline::BOLDLONGDASH },
59 };
60 
61 typedef std::hash_map< sal_Int32, sal_Int32 > ConstToConst;
62 class UnderLineMapper
63 {
64     ConstToConst MSO2OOO;
65     ConstToConst OOO2MSO;
66 private:
UnderLineMapper()67     UnderLineMapper()
68     {
69         sal_Int32 nLen = sizeof( UnderLineTable )/ sizeof( UnderLineTable[0] );
70 
71         for ( sal_Int32 index=0; index<nLen; ++index )
72         {
73             MSO2OOO[ UnderLineTable[ index ].nMSOConst ] = UnderLineTable[ index ].nOOOConst;
74             OOO2MSO[ UnderLineTable[ index ].nOOOConst ] = UnderLineTable[ index ].nMSOConst;
75         }
76     }
77 public:
propName()78     static rtl::OUString propName()
79     {
80         static rtl::OUString sPropName( RTL_CONSTASCII_USTRINGPARAM("CharUnderline") );
81         return sPropName;
82     }
83 
instance()84     static UnderLineMapper& instance()
85     {
86         static  UnderLineMapper theMapper;
87         return theMapper;
88     }
89 
getOOOFromMSO(sal_Int32 nMSOConst)90     sal_Int32 getOOOFromMSO( sal_Int32 nMSOConst ) throw( lang::IllegalArgumentException )
91     {
92         ConstToConst::iterator it = MSO2OOO.find( nMSOConst );
93         if ( it == MSO2OOO.end() )
94             throw lang::IllegalArgumentException();
95         return it->second;
96     }
getMSOFromOOO(sal_Int32 nOOOConst)97     sal_Int32 getMSOFromOOO( sal_Int32 nOOOConst ) throw( lang::IllegalArgumentException )
98     {
99         ConstToConst::iterator it = OOO2MSO.find( nOOOConst );
100         if ( it == OOO2MSO.end() )
101             throw lang::IllegalArgumentException();
102         return it->second;
103     }
104 };
105 
SwVbaFont(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<container::XIndexAccess> & xPalette,uno::Reference<css::beans::XPropertySet> xPropertySet)106 SwVbaFont::SwVbaFont( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XIndexAccess >& xPalette, uno::Reference< css::beans::XPropertySet > xPropertySet ) throw ( css::uno::RuntimeException ) : SwVbaFont_BASE( xParent, xContext, xPalette, xPropertySet )
107 {
108 }
109 
110 uno::Any SAL_CALL
getUnderline()111 SwVbaFont::getUnderline() throw (uno::RuntimeException)
112 {
113     sal_Int32 nOOVal = 0;
114     mxFont->getPropertyValue(  UnderLineMapper::propName() ) >>= nOOVal;
115     return uno::makeAny( UnderLineMapper::instance().getMSOFromOOO( nOOVal ) );
116 }
117 
118 void SAL_CALL
setUnderline(const uno::Any & _underline)119 SwVbaFont::setUnderline( const uno::Any& _underline ) throw (uno::RuntimeException)
120 {
121     sal_Int32 nMSOVal = 0;
122 
123     if ( _underline >>= nMSOVal )
124     {
125         sal_Int32 nOOVal =  UnderLineMapper::instance().getOOOFromMSO( nMSOVal );
126         mxFont->setPropertyValue(  UnderLineMapper::propName(), uno::makeAny( nOOVal ) );
127     }
128 }
129 
130 rtl::OUString&
getServiceImplName()131 SwVbaFont::getServiceImplName()
132 {
133         static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaFont") );
134         return sImplName;
135 }
136 
137 void SAL_CALL
setColorIndex(const uno::Any & _colorindex)138 SwVbaFont::setColorIndex( const uno::Any& _colorindex ) throw( uno::RuntimeException )
139 {
140         sal_Int32 nIndex = 0;
141         _colorindex >>= nIndex;
142         return setColor( OORGBToXLRGB(mxPalette->getByIndex( nIndex )) );
143 }
144 
145 uno::Any SAL_CALL
getColorIndex()146 SwVbaFont::getColorIndex() throw ( uno::RuntimeException )
147 {
148     	sal_Int32 nColor = 0;
149 
150 	XLRGBToOORGB( getColor() ) >>= nColor;
151 	sal_Int32 nElems = mxPalette->getCount();
152 	sal_Int32 nIndex = 0;
153 	for ( sal_Int32 count=0; count<nElems; ++count )
154        	{
155 		sal_Int32 nPaletteColor = 0;
156 		mxPalette->getByIndex( count ) >>= nPaletteColor;
157 		if ( nPaletteColor == nColor )
158 		{
159 			nIndex = count;
160 			break;
161 		}
162 	}
163 	return uno::makeAny( nIndex );
164 }
165 uno::Any SAL_CALL
getSubscript()166 SwVbaFont::getSubscript() throw ( uno::RuntimeException )
167 {
168     sal_Bool bRes = sal_False;
169     SwVbaFont_BASE::getSubscript() >>= bRes;
170     if ( bRes )
171         return aLongAnyTrue;
172     return aLongAnyFalse;
173 }
174 
175 uno::Any SAL_CALL
getSuperscript()176 SwVbaFont::getSuperscript() throw ( uno::RuntimeException )
177 {
178     sal_Bool bRes = sal_False;
179     SwVbaFont_BASE::getSuperscript() >>= bRes;
180     if ( bRes )
181         return aLongAnyTrue;
182     return aLongAnyFalse;
183 }
184 
185 uno::Any SAL_CALL
getBold()186 SwVbaFont::getBold() throw (uno::RuntimeException)
187 {
188     sal_Bool bRes = sal_False;
189     SwVbaFont_BASE::getBold() >>= bRes;
190     if ( bRes )
191         return aLongAnyTrue;
192     return aLongAnyFalse;
193 }
194 
195 uno::Any SAL_CALL
getItalic()196 SwVbaFont::getItalic() throw (uno::RuntimeException)
197 {
198     sal_Bool bRes = sal_False;
199     SwVbaFont_BASE::getItalic() >>= bRes;
200     if ( bRes )
201         return aLongAnyTrue;
202     return aLongAnyFalse;
203 }
204 
205 uno::Any SAL_CALL
getStrikethrough()206 SwVbaFont::getStrikethrough() throw (css::uno::RuntimeException)
207 {
208     sal_Bool bRes = sal_False;
209     SwVbaFont_BASE::getStrikethrough() >>= bRes;
210     if ( bRes )
211         return aLongAnyTrue;
212     return aLongAnyFalse;
213 }
214 
215 uno::Any SAL_CALL
getShadow()216 SwVbaFont::getShadow() throw (uno::RuntimeException)
217 {
218     sal_Bool bRes = sal_False;
219     SwVbaFont_BASE::getShadow() >>= bRes;
220     if ( bRes )
221         return aLongAnyTrue;
222     return aLongAnyFalse;
223 }
224 
225 uno::Sequence< rtl::OUString >
getServiceNames()226 SwVbaFont::getServiceNames()
227 {
228         static uno::Sequence< rtl::OUString > aServiceNames;
229         if ( aServiceNames.getLength() == 0 )
230         {
231                 aServiceNames.realloc( 1 );
232                 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Font" ) );
233         }
234         return aServiceNames;
235 }
236 
237 
238