LibreOffice Module sw (master)  1
xmlithlp.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 <limits.h>
21 
22 #include "xmlithlp.hxx"
23 #include <sax/tools/converter.hxx>
24 #include <editeng/borderline.hxx>
25 #include <editeng/brushitem.hxx>
26 
27 #include <xmloff/xmluconv.hxx>
28 #include <osl/diagnose.h>
29 
30 #include <com/sun/star/table/BorderLineStyle.hpp>
31 #include <com/sun/star/text/HoriOrientation.hpp>
32 #include <com/sun/star/text/VertOrientation.hpp>
33 
34 using ::editeng::SvxBorderLine;
35 using namespace ::xmloff::token;
36 using namespace ::com::sun::star;
37 
38 #define SVX_XML_BORDER_WIDTH_THIN 0
39 #define SVX_XML_BORDER_WIDTH_MIDDLE 1
40 #define SVX_XML_BORDER_WIDTH_THICK 2
41 
42 const struct SvXMLEnumMapEntry<sal_uInt16> psXML_BorderStyles[] =
43 {
46  { XML_SOLID, table::BorderLineStyle::SOLID },
47  { XML_DOUBLE, table::BorderLineStyle::DOUBLE },
48  { XML_DOUBLE_THIN, table::BorderLineStyle::DOUBLE_THIN },
49  { XML_DOTTED, table::BorderLineStyle::DOTTED },
50  { XML_DASHED, table::BorderLineStyle::DASHED },
51  { XML_FINE_DASHED, table::BorderLineStyle::FINE_DASHED },
52  { XML_DASH_DOT, table::BorderLineStyle::DASH_DOT },
53  { XML_DASH_DOT_DOT, table::BorderLineStyle::DASH_DOT_DOT },
54  { XML_GROOVE, table::BorderLineStyle::ENGRAVED },
55  { XML_RIDGE, table::BorderLineStyle::EMBOSSED },
56  { XML_INSET, table::BorderLineStyle::INSET },
57  { XML_OUTSET, table::BorderLineStyle::OUTSET },
58  { XML_TOKEN_INVALID, 0 }
59 };
60 
61 const struct SvXMLEnumMapEntry<sal_uInt16> psXML_NamedBorderWidths[] =
62 {
66  { XML_TOKEN_INVALID, 0 }
67 };
68 // mapping tables to map external xml input to internal box line widths
69 
70 const sal_uInt16 aBorderWidths[] =
71 {
75 };
76 
77 bool sw_frmitems_parseXMLBorder( const OUString& rValue,
78  const SvXMLUnitConverter& rUnitConverter,
79  bool& rHasStyle, sal_uInt16& rStyle,
80  bool& rHasWidth, sal_uInt16& rWidth,
81  sal_uInt16& rNamedWidth,
82  bool& rHasColor, Color& rColor )
83 {
84  OUString aToken;
85  SvXMLTokenEnumerator aTokens( rValue );
86 
87  rHasStyle = false;
88  rHasWidth = false;
89  rHasColor = false;
90 
91  rStyle = USHRT_MAX;
92  rWidth = 0;
93  rNamedWidth = USHRT_MAX;
94 
95  sal_Int32 nTemp;
96  while( aTokens.getNextToken( aToken ) && !aToken.isEmpty() )
97  {
98  if( !rHasWidth &&
99  SvXMLUnitConverter::convertEnum( rNamedWidth, aToken,
100  psXML_NamedBorderWidths ) )
101  {
102  rHasWidth = true;
103  }
104  else if( !rHasStyle &&
105  SvXMLUnitConverter::convertEnum( rStyle, aToken,
106  psXML_BorderStyles ) )
107  {
108  rHasStyle = true;
109  }
110  else if (!rHasColor && ::sax::Converter::convertColor(rColor, aToken))
111  {
112  rHasColor = true;
113  }
114  else if( !rHasWidth &&
115  rUnitConverter.convertMeasureToCore(nTemp, aToken, 0, USHRT_MAX))
116  {
117  rWidth = static_cast<sal_uInt16>(nTemp);
118  rHasWidth = true;
119  }
120  else
121  {
122  // misformed
123  return false;
124  }
125  }
126 
127  return rHasStyle || rHasWidth || rHasColor;
128 }
129 
130 static void sw_frmitems_setXMLBorderStyle( SvxBorderLine& rLine, sal_uInt16 nStyle )
131 {
132  SvxBorderLineStyle eStyle = SvxBorderLineStyle::NONE;
133  if ( nStyle != table::BorderLineStyle::NONE )
134  eStyle = SvxBorderLineStyle( nStyle );
135  rLine.SetBorderLineStyle(eStyle);
136 }
137 
138 bool sw_frmitems_setXMLBorder( std::unique_ptr<SvxBorderLine>& rpLine,
139  bool bHasStyle, sal_uInt16 nStyle,
140  bool bHasWidth, sal_uInt16 nWidth,
141  sal_uInt16 nNamedWidth,
142  bool bHasColor, const Color& rColor )
143 {
144  // first of all, delete an empty line
145  if( (bHasStyle && table::BorderLineStyle::NONE == nStyle) ||
146  (bHasWidth && USHRT_MAX == nNamedWidth && 0 == nWidth) )
147  {
148  bool bRet = nullptr != rpLine;
149  rpLine.reset();
150  return bRet;
151  }
152 
153  // if there is no line and no style and no with, there will never be a line
154  if( !rpLine && !(bHasStyle && bHasWidth) )
155  return false;
156 
157  // We now do know that there will be a line
158  if( !rpLine )
159  rpLine.reset(new SvxBorderLine);
160 
161  if( ( bHasWidth &&
162  (USHRT_MAX != nNamedWidth || (nWidth != rpLine->GetWidth() ) ) ) ||
163  ( bHasStyle &&
164  ((table::BorderLineStyle::SOLID == nStyle && rpLine->GetDistance()) ||
165  (table::BorderLineStyle::DOUBLE == nStyle && !rpLine->GetDistance())) ) )
166  {
167  bool bDouble = (bHasWidth && table::BorderLineStyle::DOUBLE == nStyle ) ||
168  rpLine->GetDistance();
169 
170  // fdo#38542: for double borders, do not override the width
171  // set via style:border-line-width{,-left,-right,-top,-bottom}
172  if (!bDouble || !rpLine->GetWidth())
173  {
174  // The width has to be changed
175  if (bHasWidth && USHRT_MAX != nNamedWidth)
176  {
177  if (bDouble)
178  {
179  rpLine->SetBorderLineStyle( SvxBorderLineStyle::DOUBLE );
180  }
181  rpLine->SetWidth( aBorderWidths[nNamedWidth] );
182  }
183  else
184  {
185  if (!bHasWidth)
186  nWidth = rpLine->GetScaledWidth();
187 
188  rpLine->SetWidth( nWidth );
189  }
190  }
191  sw_frmitems_setXMLBorderStyle( *rpLine, nStyle );
192  }
193 
194  // set color
195  if( bHasColor )
196  rpLine->SetColor( rColor );
197 
198  return true;
199 }
200 
201 void sw_frmitems_setXMLBorder( std::unique_ptr<SvxBorderLine>& rpLine,
202  sal_uInt16 nWidth, sal_uInt16 nOutWidth,
203  sal_uInt16 nInWidth, sal_uInt16 nDistance )
204 {
205  if( !rpLine )
206  rpLine.reset(new SvxBorderLine);
207 
208  if( nWidth > 0 )
209  rpLine->SetWidth( nWidth );
210  else
211  rpLine->GuessLinesWidths(SvxBorderLineStyle::DOUBLE,
212  nOutWidth, nInWidth, nDistance);
213 }
214 
216 {
217  { XML_REPEAT, GPOS_TILED },
219  { XML_STRETCH, GPOS_AREA },
221 };
222 
224 {
225  { XML_LEFT, GPOS_LM },
226  { XML_RIGHT, GPOS_RM },
228 };
229 
231 {
232  { XML_TOP, GPOS_MT },
233  { XML_BOTTOM, GPOS_MB },
235 };
236 
238  SvxGraphicPosition eHori )
239 {
240  OSL_ENSURE( GPOS_LM==eHori || GPOS_MM==eHori || GPOS_RM==eHori,
241  "sw_frmitems_MergeXMLHoriPos: vertical pos must be middle" );
242 
243  switch( ePos )
244  {
245  case GPOS_LT:
246  case GPOS_MT:
247  case GPOS_RT:
248  ePos = GPOS_LM==eHori ? GPOS_LT : (GPOS_MM==eHori ? GPOS_MT : GPOS_RT);
249  break;
250 
251  case GPOS_LM:
252  case GPOS_MM:
253  case GPOS_RM:
254  ePos = eHori;
255  break;
256 
257  case GPOS_LB:
258  case GPOS_MB:
259  case GPOS_RB:
260  ePos = GPOS_LM==eHori ? GPOS_LB : (GPOS_MM==eHori ? GPOS_MB : GPOS_RB);
261  break;
262  default:
263  ;
264  }
265 }
266 
268  SvxGraphicPosition eVert )
269 {
270  OSL_ENSURE( GPOS_MT==eVert || GPOS_MM==eVert || GPOS_MB==eVert,
271  "sw_frmitems_MergeXMLVertPos: horizontal pos must be middle" );
272 
273  switch( ePos )
274  {
275  case GPOS_LT:
276  case GPOS_LM:
277  case GPOS_LB:
278  ePos = GPOS_MT==eVert ? GPOS_LT : (GPOS_MM==eVert ? GPOS_LM : GPOS_LB);
279  break;
280 
281  case GPOS_MT:
282  case GPOS_MM:
283  case GPOS_MB:
284  ePos = eVert;
285  break;
286 
287  case GPOS_RT:
288  case GPOS_RM:
289  case GPOS_RB:
290  ePos = GPOS_MT==eVert ? GPOS_RT : (GPOS_MM==eVert ? GPOS_RM : GPOS_RB);
291  break;
292  default:
293  ;
294  }
295 }
296 
297 const struct SvXMLEnumMapEntry<sal_uInt16> psXML_BreakType[] =
298 {
299  { XML_AUTO, 0 },
300  { XML_COLUMN, 1 },
301  { XML_PAGE, 2 },
302  { XML_EVEN_PAGE, 2 },
303  { XML_ODD_PAGE, 2 },
304  { XML_TOKEN_INVALID, 0}
305 };
306 
307 const struct SvXMLEnumMapEntry<sal_Int16> aXMLTableAlignMap[] =
308 {
309  { XML_LEFT, text::HoriOrientation::LEFT },
310  { XML_LEFT, text::HoriOrientation::LEFT_AND_WIDTH },
311  { XML_CENTER, text::HoriOrientation::CENTER },
312  { XML_RIGHT, text::HoriOrientation::RIGHT },
313  { XML_MARGINS, text::HoriOrientation::FULL },
315  { XML_TOKEN_INVALID, 0 }
316 };
317 
318 const struct SvXMLEnumMapEntry<sal_Int16> aXMLTableVAlignMap[] =
319 {
320  { XML_TOP, text::VertOrientation::TOP },
321  { XML_MIDDLE, text::VertOrientation::CENTER },
322  { XML_BOTTOM, text::VertOrientation::BOTTOM },
323  { XML_TOKEN_INVALID, 0 }
324 };
325 
326 const struct SvXMLEnumMapEntry<sal_uInt16> aXML_KeepTogetherType[] =
327 {
328  { XML_ALWAYS, 0 },
329  { XML_AUTO, 1 },
330  { XML_TOKEN_INVALID, 0}
331 };
332 
333 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XML_BOTTOM
static bool convertEnum(EnumT &rEnum, const OUString &rValue, const SvXMLEnumMapEntry< EnumT > *pMap)
XML_DOUBLE_THIN
GPOS_RM
static void sw_frmitems_setXMLBorderStyle(SvxBorderLine &rLine, sal_uInt16 nStyle)
Definition: xmlithlp.cxx:130
bool sw_frmitems_parseXMLBorder(const OUString &rValue, const SvXMLUnitConverter &rUnitConverter, bool &rHasStyle, sal_uInt16 &rStyle, bool &rHasWidth, sal_uInt16 &rWidth, sal_uInt16 &rNamedWidth, bool &rHasColor, Color &rColor)
Define various helper variables and functions for xmlimpit.cxx and xmlexpit.cxx.
Definition: xmlithlp.cxx:77
void sw_frmitems_MergeXMLHoriPos(SvxGraphicPosition &ePos, SvxGraphicPosition eHori)
Definition: xmlithlp.cxx:237
XML_OUTSET
XML_COLUMN
GPOS_LT
XML_DOUBLE
#define SVX_XML_BORDER_WIDTH_MIDDLE
Definition: xmlithlp.cxx:39
XML_ALWAYS
XML_MIDDLE
bool getNextToken(OUString &rToken)
const struct SvXMLEnumMapEntry< sal_uInt16 > psXML_BorderStyles[]
Definition: xmlithlp.cxx:42
const struct SvXMLEnumMapEntry< sal_Int16 > aXMLTableVAlignMap[]
Definition: xmlithlp.cxx:318
#define DEF_LINE_WIDTH_1
XML_RIGHT
const struct SvXMLEnumMapEntry< SvxGraphicPosition > psXML_BrushVertPos[]
Definition: xmlithlp.cxx:230
SvxBorderLineStyle
GPOS_TILED
bool convertMeasureToCore(sal_Int32 &rValue, const OUString &rString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32) const
GPOS_LM
XML_HIDDEN
XML_INSET
XML_REPEAT
XML_SOLID
XML_ODD_PAGE
const struct SvXMLEnumMapEntry< sal_Int16 > aXMLTableAlignMap[]
Definition: xmlithlp.cxx:307
XML_FINE_DASHED
XML_THIN
XML_STRETCH
XML_TOP
XML_EVEN_PAGE
GPOS_MT
SvxGraphicPosition
XML_TOKEN_INVALID
GPOS_MM
XML_CENTER
XML_GROOVE
#define SVX_XML_BORDER_WIDTH_THIN
Definition: xmlithlp.cxx:38
GPOS_AREA
bool sw_frmitems_setXMLBorder(std::unique_ptr< SvxBorderLine > &rpLine, bool bHasStyle, sal_uInt16 nStyle, bool bHasWidth, sal_uInt16 nWidth, sal_uInt16 nNamedWidth, bool bHasColor, const Color &rColor)
Definition: xmlithlp.cxx:138
XML_DASH_DOT_DOT
XML_LEFT
XML_RIDGE
XML_THICK
GPOS_MB
const struct SvXMLEnumMapEntry< sal_uInt16 > psXML_NamedBorderWidths[]
Definition: xmlithlp.cxx:61
static bool convertColor(sal_Int32 &rColor, const OUString &rValue)
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:253
const struct SvXMLEnumMapEntry< SvxGraphicPosition > psXML_BrushHoriPos[]
Definition: xmlithlp.cxx:223
#define SVX_XML_BORDER_WIDTH_THICK
Definition: xmlithlp.cxx:40
XML_MARGINS
void sw_frmitems_MergeXMLVertPos(SvxGraphicPosition &ePos, SvxGraphicPosition eVert)
Definition: xmlithlp.cxx:267
#define DEF_LINE_WIDTH_5
XML_PAGE
XML_DASHED
const struct SvXMLEnumMapEntry< sal_uInt16 > psXML_BreakType[]
Definition: xmlithlp.cxx:297
XML_NONE
XML_DOTTED
GPOS_RB
#define DEF_LINE_WIDTH_0
GPOS_LB
XML_BACKGROUND_NO_REPEAT
GPOS_RT
XML_AUTO
const sal_uInt16 aBorderWidths[]
Definition: xmlithlp.cxx:70
const struct SvXMLEnumMapEntry< sal_uInt16 > aXML_KeepTogetherType[]
Definition: xmlithlp.cxx:326
const struct SvXMLEnumMapEntry< SvxGraphicPosition > psXML_BrushRepeat[]
Definition: xmlithlp.cxx:215
XML_DASH_DOT