LibreOffice Module xmloff (master)  1
controlpropertyhdl.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 
21 
22 #include <com/sun/star/util/MeasureUnit.hpp>
23 #include <com/sun/star/awt/TextAlign.hpp>
24 #include <com/sun/star/awt/FontEmphasisMark.hpp>
25 
26 #include <sax/tools/converter.hxx>
27 
28 #include <xmloff/xmltypes.hxx>
30 #include "formenums.hxx"
31 #include <xmloff/xmluconv.hxx>
32 #include <xmloff/xmltoken.hxx>
33 #include <rtl/ustrbuf.hxx>
35 
36 namespace xmloff
37 {
38 
39  using namespace ::com::sun::star;
40  using namespace ::com::sun::star::uno;
41  using namespace ::com::sun::star::awt;
42  using namespace ::com::sun::star::beans;
43  using namespace ::xmloff::token;
44 
45  //= OControlPropertyHandlerFactory
47  {
48  }
49 
51  {
52  const XMLPropertyHandler* pHandler = nullptr;
53 
54  switch (_nType)
55  {
58  m_pTextAlignHandler = std::make_unique<XMLConstantsPropertyHandler>(aTextAlignMap, XML_TOKEN_INVALID );
59  pHandler = m_pTextAlignHandler.get();
60  break;
61 
64  m_pControlBorderStyleHandler = std::make_unique<OControlBorderHandler>( OControlBorderHandler::STYLE );
65  pHandler = m_pControlBorderStyleHandler.get();
66  break;
67 
70  m_pControlBorderColorHandler = std::make_unique<OControlBorderHandler>( OControlBorderHandler::COLOR );
71  pHandler = m_pControlBorderColorHandler.get();
72  break;
73 
76  m_pRotationAngleHandler = std::make_unique<ORotationAngleHandler>();
77  pHandler = m_pRotationAngleHandler.get();
78  break;
79 
82  m_pFontWidthHandler = std::make_unique<OFontWidthHandler>();
83  pHandler = m_pFontWidthHandler.get();
84  break;
85 
88  m_pFontEmphasisHandler = std::make_unique<XMLConstantsPropertyHandler>( aFontEmphasisMap, XML_NONE );
89  pHandler = m_pFontEmphasisHandler.get();
90  break;
91 
94  m_pFontReliefHandler = std::make_unique<XMLConstantsPropertyHandler>( aFontReliefMap, XML_NONE );
95  pHandler = m_pFontReliefHandler.get();
96  break;
99  {
100  m_pTextLineModeHandler = std::make_unique<XMLNamedBoolPropertyHdl>(
103  }
104  pHandler = m_pTextLineModeHandler.get();
105  break;
106  }
107 
108  if (!pHandler)
110  return pHandler;
111  }
112 
113  //= OControlTextEmphasisHandler
115  {
116  }
117 
118  bool OControlTextEmphasisHandler::exportXML( OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
119  {
120  OUStringBuffer aReturn;
121  bool bSuccess = false;
122  sal_Int16 nFontEmphasis = sal_Int16();
123  if (_rValue >>= nFontEmphasis)
124  {
125  // the type
126  sal_uInt16 nType = nFontEmphasis & ~(awt::FontEmphasisMark::ABOVE | awt::FontEmphasisMark::BELOW);
127  // the position of the mark
128  bool bBelow = 0 != (nFontEmphasis & awt::FontEmphasisMark::BELOW);
129 
130  // convert
131  bSuccess = SvXMLUnitConverter::convertEnum(aReturn, nType, aFontEmphasisMap, XML_NONE);
132  if (bSuccess)
133  {
134  aReturn.append( ' ' );
135  aReturn.append( GetXMLToken(bBelow ? XML_BELOW : XML_ABOVE) );
136 
137  _rStrExpValue = aReturn.makeStringAndClear();
138  }
139  }
140 
141  return bSuccess;
142  }
143 
144  bool OControlTextEmphasisHandler::importXML( const OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
145  {
146  bool bSuccess = true;
147  sal_uInt16 nEmphasis = awt::FontEmphasisMark::NONE;
148 
149  bool bBelow = false;
150  bool bHasPos = false, bHasType = false;
151 
152  OUString sToken;
153  SvXMLTokenEnumerator aTokenEnum(_rStrImpValue);
154  while (aTokenEnum.getNextToken(sToken))
155  {
156  if (!bHasPos)
157  {
158  if (IsXMLToken(sToken, XML_ABOVE))
159  {
160  bBelow = false;
161  bHasPos = true;
162  }
163  else if (IsXMLToken(sToken, XML_BELOW))
164  {
165  bBelow = true;
166  bHasPos = true;
167  }
168  }
169  if (!bHasType)
170  {
171  if (SvXMLUnitConverter::convertEnum(nEmphasis, sToken, aFontEmphasisMap))
172  {
173  bHasType = true;
174  }
175  else
176  {
177  bSuccess = false;
178  break;
179  }
180  }
181  }
182 
183  if (bSuccess)
184  {
185  nEmphasis |= bBelow ? awt::FontEmphasisMark::BELOW : awt::FontEmphasisMark::ABOVE;
186  _rValue <<= nEmphasis;
187  }
188 
189  return bSuccess;
190  }
191 
192  //= OControlBorderHandlerBase
194  :m_eFacet( _eFacet )
195  {
196  }
197 
198  bool OControlBorderHandler::importXML( const OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
199  {
200  OUString sToken;
201  SvXMLTokenEnumerator aTokens(_rStrImpValue);
202 
203  sal_uInt16 nStyle = 1;
204 
205  while ( aTokens.getNextToken(sToken) // have a new token
206  && (!sToken.isEmpty()) // really have a new token
207  )
208  {
209  // try interpreting the token as border style
210  if ( m_eFacet == STYLE )
211  {
212  // is it a valid enum value?
213  if ( SvXMLUnitConverter::convertEnum( nStyle, sToken, aBorderTypeMap ) )
214  {
215  _rValue <<= nStyle;
216  return true;
217  }
218  }
219 
220  // try interpreting it as color value
221  if ( m_eFacet == COLOR )
222  {
223  sal_Int32 nColor(0);
224  if (::sax::Converter::convertColor( nColor, sToken ))
225  {
226  _rValue <<= nColor;
227  return true;
228  }
229  }
230  }
231 
232  return false;
233  }
234 
235  bool OControlBorderHandler::exportXML( OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
236  {
237  bool bSuccess = false;
238 
239  OUStringBuffer aOut;
240  switch ( m_eFacet )
241  {
242  case STYLE:
243  {
244  sal_uInt16 nBorder = 0;
245  bSuccess = (_rValue >>= nBorder)
246  && SvXMLUnitConverter::convertEnum( aOut, nBorder, aBorderTypeMap );
247  }
248  break;
249  case COLOR:
250  {
251  sal_Int32 nBorderColor = 0;
252  if ( _rValue >>= nBorderColor )
253  {
254  ::sax::Converter::convertColor(aOut, nBorderColor);
255  bSuccess = true;
256  }
257  }
258  break;
259  } // switch ( m_eFacet )
260 
261  if ( !bSuccess )
262  return false;
263 
264  if ( !_rStrExpValue.isEmpty() )
265  _rStrExpValue += " ";
266  _rStrExpValue += aOut;
267 
268  return true;
269  }
270 
271  //= OFontWidthHandler
273  {
274  }
275 
276  bool OFontWidthHandler::importXML( const OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
277  {
278  sal_Int32 nWidth = 0;
279  bool const bSuccess = ::sax::Converter::convertMeasure(
280  nWidth, _rStrImpValue, util::MeasureUnit::POINT);
281  if (bSuccess)
282  _rValue <<= static_cast<sal_Int16>(nWidth);
283 
284  return bSuccess;
285  }
286 
287  bool OFontWidthHandler::exportXML( OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
288  {
289  sal_Int16 nWidth = 0;
290  OUStringBuffer aResult;
291  if (_rValue >>= nWidth)
292  {
293  ::sax::Converter::convertMeasure(aResult, nWidth,
294  util::MeasureUnit::POINT, util::MeasureUnit::POINT);
295  }
296  _rStrExpValue = aResult.makeStringAndClear();
297 
298  return !_rStrExpValue.isEmpty();
299  }
300 
301  //= ORotationAngleHandler
303  {
304  }
305 
306  bool ORotationAngleHandler::importXML( const OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
307  {
308  double fValue;
309  bool const bSucces =
310  ::sax::Converter::convertDouble(fValue, _rStrImpValue);
311  if (bSucces)
312  {
313  fValue *= 10;
314  _rValue <<= static_cast<float>(fValue);
315  }
316 
317  return bSucces;
318  }
319 
320  bool ORotationAngleHandler::exportXML( OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
321  {
322  float fAngle = 0;
323  bool bSuccess = (_rValue >>= fAngle);
324 
325  if (bSuccess)
326  {
327  OUStringBuffer sValue;
328  ::sax::Converter::convertDouble(sValue, static_cast<double>(fAngle) / 10);
329  _rStrExpValue = sValue.makeStringAndClear();
330  }
331 
332  return bSuccess;
333  }
334 
335  //= ImageScaleModeHandler
338  {
339  }
340 
341 } // namespace xmloff
342 
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3434
virtual const XMLPropertyHandler * GetPropertyHandler(sal_Int32 nType) const
This method retrieves a PropertyHandler for the given XML-type.
Definition: prhdlfac.cxx:138
static bool convertEnum(EnumT &rEnum, const OUString &rValue, const SvXMLEnumMapEntry< EnumT > *pMap)
convert string to enum using given enum map, if the enum is not found in the map, this method will re...
Definition: xmluconv.hxx:128
static bool convertMeasure(sal_Int32 &rValue, const OUString &rString, sal_Int16 nTargetUnit=css::util::MeasureUnit::MM_100TH, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32)
the SvXMLTypeConverter converts values of various types from their internal representation to the tex...
Definition: xmluconv.hxx:77
virtual bool exportXML(OUString &_rStrExpValue, const css::uno::Any &_rValue, const SvXMLUnitConverter &_rUnitConverter) const override
Exports the given value according to the XML-data-type corresponding to the derived class...
#define XML_TYPE_CONTROL_BORDER_COLOR
Definition: xmltypes.hxx:253
std::unique_ptr< XMLNamedBoolPropertyHdl > m_pTextLineModeHandler
virtual bool importXML(const OUString &_rStrImpValue, css::uno::Any &_rValue, const SvXMLUnitConverter &_rUnitConverter) const override
Imports the given value according to the XML-data-type corresponding to the derived class...
bool getNextToken(OUString &rToken)
Definition: xmluconv.cxx:449
static void convertDouble(OUStringBuffer &rBuffer, double fNumber, bool bWriteUnits, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit)
#define XML_TYPE_TEXT_ALIGN
Definition: xmltypes.hxx:229
virtual bool importXML(const OUString &_rStrImpValue, css::uno::Any &_rValue, const SvXMLUnitConverter &_rUnitConverter) const override
Imports the given value according to the XML-data-type corresponding to the derived class...
std::unique_ptr< XMLConstantsPropertyHandler > m_pFontReliefHandler
#define XML_TYPE_TEXT_LINE_MODE
Definition: xmltypes.hxx:255
virtual bool importXML(const OUString &_rStrImpValue, css::uno::Any &_rValue, const SvXMLUnitConverter &_rUnitConverter) const override
Imports the given value according to the XML-data-type corresponding to the derived class...
#define XML_TYPE_CONTROL_TEXT_EMPHASIZE
Definition: xmltypes.hxx:246
virtual bool exportXML(OUString &_rStrExpValue, const css::uno::Any &_rValue, const SvXMLUnitConverter &_rUnitConverter) const override
Exports the given value according to the XML-data-type corresponding to the derived class...
const SvXMLEnumMapEntry< sal_uInt16 > aBorderTypeMap[]
Definition: formenums.cxx:115
#define XML_TYPE_ROTATION_ANGLE
Definition: xmltypes.hxx:231
long const nBorder
std::unique_ptr< XMLConstantsPropertyHandler > m_pFontEmphasisHandler
#define XML_TYPE_TEXT_FONT_RELIEF
Definition: xmltypes.hxx:241
virtual bool exportXML(OUString &_rStrExpValue, const css::uno::Any &_rValue, const SvXMLUnitConverter &_rUnitConverter) const override
Exports the given value according to the XML-data-type corresponding to the derived class...
#define XML_TYPE_CONTROL_BORDER
Definition: xmltypes.hxx:232
const SvXMLEnumMapEntry< sal_Int16 > aTextAlignMap[]
Definition: formenums.cxx:106
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3378
static bool convertColor(sal_Int32 &rColor, const OUString &rValue)
Handling of tokens in XML:
std::unique_ptr< OControlBorderHandler > m_pControlBorderColorHandler
const SvXMLEnumMapEntry< sal_uInt16 > aFontReliefMap[]
Definition: formenums.cxx:138
const SvXMLEnumMapEntry< sal_uInt16 > aFontEmphasisMap[]
Definition: formenums.cxx:129
OControlBorderHandler(const BorderFacet _eFacet)
QPRO_FUNC_TYPE nType
std::unique_ptr< XMLConstantsPropertyHandler > m_pTextAlignHandler
virtual bool importXML(const OUString &_rStrImpValue, css::uno::Any &_rValue, const SvXMLUnitConverter &_rUnitConverter) const override
Imports the given value according to the XML-data-type corresponding to the derived class...
Abstract base-class for different XML-types.
Definition: xmlprhdl.hxx:35
const SvXMLEnumMapEntry< sal_uInt16 > aScaleModeMap[]
Definition: formenums.cxx:180
virtual const XMLPropertyHandler * GetPropertyHandler(sal_Int32 _nType) const override
This method retrieves a PropertyHandler for the given XML-type.
Abstract base-class for different XML-types.
virtual bool exportXML(OUString &_rStrExpValue, const css::uno::Any &_rValue, const SvXMLUnitConverter &_rUnitConverter) const override
Exports the given value according to the XML-data-type corresponding to the derived class...
std::unique_ptr< OControlBorderHandler > m_pControlBorderStyleHandler
#define XML_TYPE_FONT_WIDTH
Definition: xmltypes.hxx:230
std::unique_ptr< OFontWidthHandler > m_pFontWidthHandler
std::unique_ptr< ORotationAngleHandler > m_pRotationAngleHandler