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