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