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 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;
64 {
65  ConstToConst MSO2OOO;
66  ConstToConst OOO2MSO;
67 private:
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:
77  static OUString propName()
78  {
79  return "CharUnderline";
80  }
81 
83  {
84  static UnderLineMapper theMapper;
85  return theMapper;
86  }
87 
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  }
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 
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
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
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
132 {
133  return "SwVbaFont";
134 }
135 
136 void SAL_CALL
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
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
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
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
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
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
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
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 >
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: */
sal_Int32 getOOOFromMSO(sal_Int32 nMSOConst)
Definition: vbafont.cxx:89
const size_t count(pCandidateA->getBorderLines().size())
virtual css::uno::Any SAL_CALL getSubscript() override
Definition: vbafont.cxx:165
virtual OUString getServiceImplName() override
Definition: vbafont.cxx:131
virtual void SAL_CALL setColorIndex(const css::uno::Any &_colorindex) override
Definition: vbafont.cxx:137
const uno::Any aLongAnyTrue(sal_Int16(-1))
uno::Reference< rendering::XCanvasFont > mxFont
sal_Int32 XLRGBToOORGB(sal_Int32 nCol)
virtual void SAL_CALL setUnderline(const css::uno::Any &_underline) override
Definition: vbafont.cxx:119
sal_Int32 getMSOFromOOO(sal_Int32 nOOOConst)
Definition: vbafont.cxx:97
virtual css::uno::Any SAL_CALL getSuperscript() override
Definition: vbafont.cxx:175
virtual css::uno::Any SAL_CALL getUnderline() override
Definition: vbafont.cxx:111
ConstToConst OOO2MSO
Definition: vbafont.cxx:66
cppu::ImplInheritanceHelper< VbaFontBase, ov::word::XFont > SwVbaFont_BASE
Definition: vbafont.hxx:27
virtual css::uno::Any SAL_CALL getShadow() override
Definition: vbafont.cxx:215
virtual css::uno::Sequence< OUString > getServiceNames() override
Definition: vbafont.cxx:225
#define SAL_N_ELEMENTS(arr)
const uno::Any aLongAnyFalse(sal_Int16(0))
ConstToConst MSO2OOO
Definition: vbafont.cxx:65
static OUString propName()
Definition: vbafont.cxx:77
sal_Int32 OORGBToXLRGB(sal_Int32 nCol)
tuple index
static UnderLineMapper & instance()
Definition: vbafont.cxx:82
virtual css::uno::Any SAL_CALL getBold() override
Definition: vbafont.cxx:185
std::unordered_map< sal_Int32, sal_Int32 > ConstToConst
Definition: vbafont.cxx:62
virtual css::uno::Any SAL_CALL getColorIndex() override
Definition: vbafont.cxx:145
sal_Int32 nMSOConst
Definition: vbafont.cxx:37
static MapPair const UnderLineTable[]
Definition: vbafont.cxx:41
sal_Int32 nOOOConst
Definition: vbafont.cxx:38
virtual css::uno::Any SAL_CALL getStrikethrough() override
Definition: vbafont.cxx:205
virtual css::uno::Any SAL_CALL getItalic() override
Definition: vbafont.cxx:195
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:106