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