LibreOffice Module xmloff (master)  1
bordrhdl.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 "bordrhdl.hxx"
21 #include <sax/tools/converter.hxx>
22 #include <xmloff/xmltoken.hxx>
23 #include <xmloff/xmluconv.hxx>
24 #include <xmloff/xmlement.hxx>
25 #include <rtl/ustrbuf.hxx>
26 #include <com/sun/star/uno/Any.hxx>
27 #include <com/sun/star/table/BorderLine2.hpp>
28 #include <com/sun/star/table/BorderLineStyle.hpp>
29 
30 #include <limits.h>
31 
32 using namespace ::com::sun::star;
33 using namespace ::xmloff::token;
34 
35 #define DEF_LINE_WIDTH_0 1
36 #define DEF_LINE_WIDTH_1 35
37 #define DEF_LINE_WIDTH_2 88
38 
39 #define SVX_XML_BORDER_WIDTH_THIN 0
40 #define SVX_XML_BORDER_WIDTH_MIDDLE 1
41 #define SVX_XML_BORDER_WIDTH_THICK 2
42 
44 {
45  { XML_NONE, table::BorderLineStyle::NONE },
46  { XML_HIDDEN, table::BorderLineStyle::NONE },
47  { XML_SOLID, table::BorderLineStyle::SOLID },
48  { XML_DOUBLE, table::BorderLineStyle::DOUBLE },
49  { XML_DOUBLE_THIN, table::BorderLineStyle::DOUBLE_THIN },
50  { XML_DOTTED, table::BorderLineStyle::DOTTED },
51  { XML_DASHED, table::BorderLineStyle::DASHED },
52  { XML_GROOVE, table::BorderLineStyle::ENGRAVED },
53  { XML_RIDGE, table::BorderLineStyle::EMBOSSED },
54  { XML_INSET, table::BorderLineStyle::INSET },
55  { XML_OUTSET, table::BorderLineStyle::OUTSET },
56  { XML_FINE_DASHED, table::BorderLineStyle::FINE_DASHED },
57  { XML_DASH_DOT, table::BorderLineStyle::DASH_DOT },
58  { XML_DASH_DOT_DOT, table::BorderLineStyle::DASH_DOT_DOT },
59  { XML_TOKEN_INVALID, 0 }
60 };
61 
63 {
67  { XML_TOKEN_INVALID, 0 }
68 };
69 // mapping tables to map external xml input to internal box line widths
70 
71 sal_uInt16 const aBorderWidths[] =
72 {
76 };
77 
78 static void lcl_frmitems_setXMLBorderStyle( table::BorderLine2 & rBorderLine, sal_uInt16 nStyle )
79 {
80  sal_Int16 eStyle = -1; // None
81  if (nStyle != table::BorderLineStyle::NONE)
82  eStyle = sal_Int16( nStyle );
83 
84  rBorderLine.LineStyle = eStyle;
85 }
86 
87 
88 
89 
91 {
92  // nothing to do
93 }
94 
95 bool XMLBorderWidthHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
96 {
97  SvXMLTokenEnumerator aTokenEnum( rStrImpValue );
98 
99  sal_Int32 nInWidth, nDistance, nOutWidth;
100 
101  OUString aToken;
102  if( !aTokenEnum.getNextToken( aToken ) )
103  return false;
104 
105  if (!rUnitConverter.convertMeasureToCore( nInWidth, aToken, 0, 500 ))
106  return false;
107 
108  if( !aTokenEnum.getNextToken( aToken ) )
109  return false;
110 
111  if (!rUnitConverter.convertMeasureToCore( nDistance, aToken, 0, 500 ))
112  return false;
113 
114  if( !aTokenEnum.getNextToken( aToken ) )
115  return false;
116 
117  if (!rUnitConverter.convertMeasureToCore( nOutWidth, aToken, 0, 500 ))
118  return false;
119 
120  table::BorderLine2 aBorderLine;
121  if(!(rValue >>= aBorderLine))
122  aBorderLine.Color = 0;
123 
124  aBorderLine.InnerLineWidth = sal::static_int_cast< sal_Int16 >(nInWidth);
125  aBorderLine.OuterLineWidth = sal::static_int_cast< sal_Int16 >(nOutWidth);
126  aBorderLine.LineDistance = sal::static_int_cast< sal_Int16 >(nDistance);
127 
128  rValue <<= aBorderLine;
129  return true;
130 }
131 
132 bool XMLBorderWidthHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
133 {
134  OUStringBuffer aOut;
135 
136  table::BorderLine2 aBorderLine;
137  if(!(rValue >>= aBorderLine))
138  return false;
139 
140  bool bDouble = false;
141  switch ( aBorderLine.LineStyle )
142  {
143  case table::BorderLineStyle::DOUBLE:
144  case table::BorderLineStyle::DOUBLE_THIN:
145  case table::BorderLineStyle::THINTHICK_SMALLGAP:
146  case table::BorderLineStyle::THINTHICK_MEDIUMGAP:
147  case table::BorderLineStyle::THINTHICK_LARGEGAP:
148  case table::BorderLineStyle::THICKTHIN_SMALLGAP:
149  case table::BorderLineStyle::THICKTHIN_MEDIUMGAP:
150  case table::BorderLineStyle::THICKTHIN_LARGEGAP:
151  bDouble = true;
152  break;
153  default:
154  break;
155  }
156 
157  if( ( aBorderLine.LineDistance == 0 && aBorderLine.InnerLineWidth == 0 ) || !bDouble )
158  return false;
159 
160  rUnitConverter.convertMeasureToXML( aOut, aBorderLine.InnerLineWidth );
161  aOut.append( ' ' );
162  rUnitConverter.convertMeasureToXML( aOut, aBorderLine.LineDistance );
163  aOut.append( ' ' );
164  rUnitConverter.convertMeasureToXML( aOut, aBorderLine.OuterLineWidth );
165 
166  rStrExpValue = aOut.makeStringAndClear();
167  return true;
168 }
169 
170 
171 
172 
174 {
175  // nothing to do
176 }
177 
178 bool XMLBorderHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
179 {
180  OUString aToken;
181  SvXMLTokenEnumerator aTokens( rStrImpValue );
182 
183  bool bHasStyle = false;
184  bool bHasWidth = false;
185  bool bHasColor = false;
186 
187  sal_uInt16 nStyle = USHRT_MAX;
188  sal_uInt16 nWidth = 0;
189  sal_uInt16 nNamedWidth = USHRT_MAX;
190  sal_Int32 nColor = 0;
191 
192  sal_Int32 nTemp;
193  while( aTokens.getNextToken( aToken ) && !aToken.isEmpty() )
194  {
195  if( !bHasWidth &&
196  SvXMLUnitConverter::convertEnum( nNamedWidth, aToken,
197  pXML_NamedBorderWidths ) )
198  {
199  bHasWidth = true;
200  }
201  else if( !bHasStyle &&
202  SvXMLUnitConverter::convertEnum( nStyle, aToken,
203  pXML_BorderStyles ) )
204  {
205  bHasStyle = true;
206  }
207  else if (!bHasColor && ::sax::Converter::convertColor(nColor, aToken))
208  {
209  bHasColor = true;
210  }
211  else if( !bHasWidth &&
212  rUnitConverter.convertMeasureToCore( nTemp, aToken, 0,
213  USHRT_MAX ) )
214  {
215  nWidth = static_cast<sal_uInt16>(nTemp);
216  bHasWidth = true;
217  }
218  else
219  {
220  // misformed
221  return false;
222  }
223  }
224 
225  // if there is no style or a different style than none but no width,
226  // then the declaration is not valid.
227  if (!bHasStyle || (table::BorderLineStyle::NONE != nStyle && !bHasWidth))
228  return false;
229 
230  table::BorderLine2 aBorderLine;
231  if(!(rValue >>= aBorderLine))
232  {
233  aBorderLine.Color = 0;
234  aBorderLine.InnerLineWidth = 0;
235  aBorderLine.OuterLineWidth = 0;
236  aBorderLine.LineDistance = 0;
237  aBorderLine.LineWidth = 0;
238  }
239 
240  // first of all, delete an empty line
241  if (table::BorderLineStyle::NONE == nStyle ||
242  (bHasWidth && USHRT_MAX == nNamedWidth && 0 == nWidth) )
243  {
244  aBorderLine.InnerLineWidth = 0;
245  aBorderLine.OuterLineWidth = 0;
246  aBorderLine.LineDistance = 0;
247  aBorderLine.LineWidth = 0;
248  }
249  else
250  {
251  if( USHRT_MAX != nNamedWidth )
252  {
253  aBorderLine.LineWidth = aBorderWidths[nNamedWidth];
254  }
255  else
256  {
257  aBorderLine.LineWidth = nWidth;
258  lcl_frmitems_setXMLBorderStyle( aBorderLine, nStyle );
259  }
260  }
261 
262  // set color
263  if( bHasColor )
264  {
265  aBorderLine.Color = nColor;
266  }
267 
268  rValue <<= aBorderLine;
269  return true;
270 }
271 
272 bool XMLBorderHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& /* rUnitConverter */ ) const
273 {
274  OUStringBuffer aOut;
275 
276  table::BorderLine2 aBorderLine;
277  if(!(rValue >>= aBorderLine))
278  return false;
279 
280  sal_Int32 nWidth = aBorderLine.LineWidth;
281 
282  if( nWidth == 0 )
283  {
284  aOut.append( GetXMLToken( XML_NONE ) );
285  }
286  else
287  {
288  ::sax::Converter::convertMeasure( aOut, nWidth,
289  util::MeasureUnit::MM_100TH, util::MeasureUnit::POINT);
290 
291  aOut.append( ' ' );
292 
293  XMLTokenEnum eStyleToken = XML_SOLID;
294  switch ( aBorderLine.LineStyle )
295  {
296  case table::BorderLineStyle::DASHED:
297  eStyleToken = XML_DASHED;
298  break;
299  case table::BorderLineStyle::DOTTED:
300  eStyleToken = XML_DOTTED;
301  break;
302  case table::BorderLineStyle::DOUBLE:
303  case table::BorderLineStyle::THINTHICK_SMALLGAP:
304  case table::BorderLineStyle::THINTHICK_MEDIUMGAP:
305  case table::BorderLineStyle::THINTHICK_LARGEGAP:
306  case table::BorderLineStyle::THICKTHIN_SMALLGAP:
307  case table::BorderLineStyle::THICKTHIN_MEDIUMGAP:
308  case table::BorderLineStyle::THICKTHIN_LARGEGAP:
309  eStyleToken = XML_DOUBLE;
310  break;
311  case table::BorderLineStyle::EMBOSSED:
312  eStyleToken = XML_RIDGE;
313  break;
314  case table::BorderLineStyle::ENGRAVED:
315  eStyleToken = XML_GROOVE;
316  break;
317  case table::BorderLineStyle::OUTSET:
318  eStyleToken = XML_OUTSET;
319  break;
320  case table::BorderLineStyle::INSET:
321  eStyleToken = XML_INSET;
322  break;
323  case table::BorderLineStyle::FINE_DASHED:
324  eStyleToken = XML_FINE_DASHED;
325  break;
326  case table::BorderLineStyle::DASH_DOT:
327  eStyleToken = XML_DASH_DOT;
328  break;
329  case table::BorderLineStyle::DASH_DOT_DOT:
330  eStyleToken = XML_DASH_DOT_DOT;
331  break;
332  case table::BorderLineStyle::DOUBLE_THIN:
333  eStyleToken = XML_DOUBLE_THIN;
334  break;
335  case table::BorderLineStyle::SOLID:
336  default:
337  break;
338  }
339  aOut.append( GetXMLToken( eStyleToken ) );
340 
341  aOut.append( ' ' );
342 
343  ::sax::Converter::convertColor( aOut, aBorderLine.Color );
344  }
345 
346  rStrExpValue = aOut.makeStringAndClear();
347 
348  return true;
349 }
350 
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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...
Definition: bordrhdl.cxx:132
bool getNextToken(OUString &rToken)
Definition: xmluconv.cxx:449
virtual ~XMLBorderWidthHdl() override
Definition: bordrhdl.cxx:90
sal_uInt16 const aBorderWidths[]
Definition: bordrhdl.cxx:71
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
bool convertMeasureToCore(sal_Int32 &rValue, const OUString &rString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32) const
convert string to measure with meCoreMeasureUnit, using optional min and max values ...
Definition: xmluconv.cxx:171
virtual ~XMLBorderHdl() override
Definition: bordrhdl.cxx:173
#define DEF_LINE_WIDTH_1
Definition: bordrhdl.cxx:36
XMLTokenEnum
The enumeration of all XML tokens.
Definition: xmltoken.hxx:49
SvXMLEnumMapEntry< sal_uInt16 > const pXML_BorderStyles[]
Definition: bordrhdl.cxx:43
#define DEF_LINE_WIDTH_0
Definition: bordrhdl.cxx:35
SvXMLEnumMapEntry< sal_uInt16 > const pXML_NamedBorderWidths[]
Definition: bordrhdl.cxx:62
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)
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Handling of tokens in XML:
#define DEF_LINE_WIDTH_2
Definition: bordrhdl.cxx:37
#define SVX_XML_BORDER_WIDTH_THIN
Definition: bordrhdl.cxx:39
#define SVX_XML_BORDER_WIDTH_THICK
Definition: bordrhdl.cxx:41
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...
Definition: bordrhdl.cxx:178
#define SVX_XML_BORDER_WIDTH_MIDDLE
Definition: bordrhdl.cxx:40
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...
Definition: bordrhdl.cxx:272
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...
Definition: bordrhdl.cxx:95
void convertMeasureToXML(OUStringBuffer &rBuffer, sal_Int32 nMeasure) const
convert measure to string: from meCoreMeasureUnit to meXMLMeasureUnit
Definition: xmluconv.cxx:181
static void lcl_frmitems_setXMLBorderStyle(table::BorderLine2 &rBorderLine, sal_uInt16 nStyle)
Definition: bordrhdl.cxx:78