update credits
[LibreOffice.git] / sw / source / filter / xml / xmlithlp.cxx
blob10627afe22e83ce604e5f817a478a437534b1e70
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "xmlithlp.hxx"
21 #include "hintids.hxx"
22 #include "unomid.h"
23 #include <sax/tools/converter.hxx>
24 #include <svx/unomid.hxx>
25 #include <editeng/lrspitem.hxx>
26 #include <editeng/ulspitem.hxx>
27 #include <editeng/shaditem.hxx>
28 #include <editeng/boxitem.hxx>
29 #include <editeng/formatbreakitem.hxx>
30 #include <editeng/keepitem.hxx>
31 #include <editeng/brushitem.hxx>
32 #include "fmtpdsc.hxx"
33 #include "fmtornt.hxx"
34 #include "fmtfsize.hxx"
36 #include "fmtlsplt.hxx"
37 #include <xmloff/xmluconv.hxx>
39 using ::editeng::SvxBorderLine;
40 using namespace ::xmloff::token;
41 using namespace ::com::sun::star;
44 #define API_LINE_NONE 0x7FFF
45 #define API_LINE_SOLID 0
46 #define API_LINE_DOTTED 1
47 #define API_LINE_DASHED 2
48 #define API_LINE_DOUBLE 3
49 #define API_LINE_EMBOSSED 10
50 #define API_LINE_ENGRAVED 11
51 #define API_LINE_OUTSET 12
52 #define API_LINE_INSET 13
54 #define SVX_XML_BORDER_WIDTH_THIN 0
55 #define SVX_XML_BORDER_WIDTH_MIDDLE 1
56 #define SVX_XML_BORDER_WIDTH_THICK 2
59 const struct SvXMLEnumMapEntry psXML_BorderStyles[] =
61 { XML_NONE, API_LINE_NONE },
62 { XML_HIDDEN, API_LINE_NONE },
63 { XML_SOLID, API_LINE_SOLID },
64 { XML_DOUBLE, API_LINE_DOUBLE },
65 { XML_DOTTED, API_LINE_DOTTED },
66 { XML_DASHED, API_LINE_DASHED },
67 { XML_GROOVE, API_LINE_ENGRAVED },
68 { XML_RIDGE, API_LINE_EMBOSSED },
69 { XML_INSET, API_LINE_INSET },
70 { XML_OUTSET, API_LINE_OUTSET },
71 { XML_TOKEN_INVALID, 0 }
74 const struct SvXMLEnumMapEntry psXML_NamedBorderWidths[] =
76 { XML_THIN, SVX_XML_BORDER_WIDTH_THIN },
77 { XML_MIDDLE, SVX_XML_BORDER_WIDTH_MIDDLE },
78 { XML_THICK, SVX_XML_BORDER_WIDTH_THICK },
79 { XML_TOKEN_INVALID, 0 }
81 // mapping tables to map external xml input to intarnal box line widths
84 const sal_uInt16 aBorderWidths[] =
86 DEF_LINE_WIDTH_0,
87 DEF_LINE_WIDTH_5,
88 DEF_LINE_WIDTH_1,
91 bool sw_frmitems_parseXMLBorder( const OUString& rValue,
92 const SvXMLUnitConverter& rUnitConverter,
93 bool& rHasStyle, sal_uInt16& rStyle,
94 bool& rHasWidth, sal_uInt16& rWidth,
95 sal_uInt16& rNamedWidth,
96 bool& rHasColor, Color& rColor )
98 OUString aToken;
99 SvXMLTokenEnumerator aTokens( rValue );
101 rHasStyle = false;
102 rHasWidth = false;
103 rHasColor = false;
105 rStyle = USHRT_MAX;
106 rWidth = 0;
107 rNamedWidth = USHRT_MAX;
109 sal_Int32 nTemp;
110 while( aTokens.getNextToken( aToken ) && !aToken.isEmpty() )
112 if( !rHasWidth &&
113 rUnitConverter.convertEnum( rNamedWidth, aToken,
114 psXML_NamedBorderWidths ) )
116 rHasWidth = true;
118 else if( !rHasStyle &&
119 rUnitConverter.convertEnum( rStyle, aToken,
120 psXML_BorderStyles ) )
122 rHasStyle = true;
124 else if (!rHasColor && ::sax::Converter::convertColor(nTemp, aToken))
126 rColor.SetColor(nTemp);
127 rHasColor = true;
129 else if( !rHasWidth &&
130 rUnitConverter.convertMeasureToCore(nTemp, aToken, 0, USHRT_MAX))
132 rWidth = (sal_uInt16)nTemp;
133 rHasWidth = true;
135 else
137 // missformed
138 return false;
142 return rHasStyle || rHasWidth || rHasColor;
145 void sw_frmitems_setXMLBorderStyle( SvxBorderLine& rLine, sal_uInt16 nStyle )
147 ::editeng::SvxBorderStyle eStyle = table::BorderLineStyle::NONE;
148 if ( nStyle != API_LINE_NONE )
149 eStyle = ::editeng::SvxBorderStyle( nStyle );
150 rLine.SetBorderLineStyle(eStyle);
153 bool sw_frmitems_setXMLBorder( SvxBorderLine*& rpLine,
154 bool bHasStyle, sal_uInt16 nStyle,
155 bool bHasWidth, sal_uInt16 nWidth,
156 sal_uInt16 nNamedWidth,
157 bool bHasColor, const Color& rColor )
159 // first of all, delete an empty line
160 if( (bHasStyle && API_LINE_NONE == nStyle) ||
161 (bHasWidth && USHRT_MAX == nNamedWidth && 0 == nWidth) )
163 bool bRet = 0 != rpLine;
164 if( rpLine )
166 delete rpLine;
167 rpLine = 0;
170 return bRet;
173 // if there is no line and no style and no with, there will never be a line
174 if( !rpLine && !(bHasStyle && bHasWidth) )
175 return false;
177 // We now do know that there will be a line
178 if( !rpLine )
179 rpLine = new SvxBorderLine;
182 if( ( bHasWidth &&
183 (USHRT_MAX != nNamedWidth || (nWidth != rpLine->GetWidth() ) ) ) ||
184 ( bHasStyle &&
185 ((API_LINE_SOLID == nStyle && rpLine->GetDistance()) ||
186 (API_LINE_DOUBLE == nStyle && !rpLine->GetDistance())) ) )
188 bool bDouble = (bHasWidth && API_LINE_DOUBLE == nStyle ) ||
189 rpLine->GetDistance();
191 // fdo#38542: for double borders, do not override the width
192 // set via style:border-line-width{,-left,-right,-top,-bottom}
193 if (!bDouble || !rpLine->GetWidth())
195 // The width has to be changed
196 if (bHasWidth && USHRT_MAX != nNamedWidth)
198 if (bDouble)
200 rpLine->SetBorderLineStyle( table::BorderLineStyle::DOUBLE );
202 rpLine->SetWidth( aBorderWidths[nNamedWidth] );
204 else
206 if (!bHasWidth)
207 nWidth = rpLine->GetInWidth() + rpLine->GetDistance() +
208 rpLine->GetOutWidth();
210 rpLine->SetWidth( nWidth );
213 sw_frmitems_setXMLBorderStyle( *rpLine, nStyle );
216 // set color
217 if( bHasColor )
218 rpLine->SetColor( rColor );
220 return true;
223 void sw_frmitems_setXMLBorder( SvxBorderLine*& rpLine,
224 sal_uInt16 nWidth, sal_uInt16 nOutWidth,
225 sal_uInt16 nInWidth, sal_uInt16 nDistance )
227 if( !rpLine )
228 rpLine = new SvxBorderLine;
230 if( nWidth > 0 )
231 rpLine->SetWidth( nWidth );
232 else
233 rpLine->GuessLinesWidths(table::BorderLineStyle::DOUBLE,
234 nOutWidth, nInWidth, nDistance);
237 const struct SvXMLEnumMapEntry psXML_BrushRepeat[] =
239 { XML_BACKGROUND_REPEAT, GPOS_TILED },
240 { XML_BACKGROUND_NO_REPEAT, GPOS_MM },
241 { XML_BACKGROUND_STRETCH, GPOS_AREA },
242 { XML_TOKEN_INVALID, 0 }
245 const struct SvXMLEnumMapEntry psXML_BrushHoriPos[] =
247 { XML_LEFT, GPOS_LM },
248 { XML_RIGHT, GPOS_RM },
249 { XML_TOKEN_INVALID, 0 }
252 const struct SvXMLEnumMapEntry psXML_BrushVertPos[] =
254 { XML_TOP, GPOS_MT },
255 { XML_BOTTOM, GPOS_MB },
256 { XML_TOKEN_INVALID, 0 }
259 void sw_frmitems_MergeXMLHoriPos( SvxGraphicPosition& ePos,
260 SvxGraphicPosition eHori )
262 OSL_ENSURE( GPOS_LM==eHori || GPOS_MM==eHori || GPOS_RM==eHori,
263 "sw_frmitems_MergeXMLHoriPos: vertical pos must be middle" );
265 switch( ePos )
267 case GPOS_LT:
268 case GPOS_MT:
269 case GPOS_RT:
270 ePos = GPOS_LM==eHori ? GPOS_LT : (GPOS_MM==eHori ? GPOS_MT : GPOS_RT);
271 break;
273 case GPOS_LM:
274 case GPOS_MM:
275 case GPOS_RM:
276 ePos = eHori;
277 break;
279 case GPOS_LB:
280 case GPOS_MB:
281 case GPOS_RB:
282 ePos = GPOS_LM==eHori ? GPOS_LB : (GPOS_MM==eHori ? GPOS_MB : GPOS_RB);
283 break;
284 default:
289 void sw_frmitems_MergeXMLVertPos( SvxGraphicPosition& ePos,
290 SvxGraphicPosition eVert )
292 OSL_ENSURE( GPOS_MT==eVert || GPOS_MM==eVert || GPOS_MB==eVert,
293 "sw_frmitems_MergeXMLVertPos: horizontal pos must be middle" );
295 switch( ePos )
297 case GPOS_LT:
298 case GPOS_LM:
299 case GPOS_LB:
300 ePos = GPOS_MT==eVert ? GPOS_LT : (GPOS_MM==eVert ? GPOS_LM : GPOS_LB);
301 ePos = eVert;
302 break;
304 case GPOS_MT:
305 case GPOS_MM:
306 case GPOS_MB:
307 ePos = eVert;
308 break;
310 case GPOS_RT:
311 case GPOS_RM:
312 case GPOS_RB:
313 ePos = GPOS_MT==eVert ? GPOS_RT : (GPOS_MM==eVert ? GPOS_RM : GPOS_RB);
314 break;
315 default:
321 const struct SvXMLEnumMapEntry psXML_BreakType[] =
323 { XML_AUTO, 0 },
324 { XML_COLUMN, 1 },
325 { XML_PAGE, 2 },
326 { XML_EVEN_PAGE, 2 },
327 { XML_ODD_PAGE, 2 },
328 { XML_TOKEN_INVALID, 0}
331 const struct SvXMLEnumMapEntry aXMLTableAlignMap[] =
333 { XML_LEFT, text::HoriOrientation::LEFT },
334 { XML_LEFT, text::HoriOrientation::LEFT_AND_WIDTH },
335 { XML_CENTER, text::HoriOrientation::CENTER },
336 { XML_RIGHT, text::HoriOrientation::RIGHT },
337 { XML_MARGINS, text::HoriOrientation::FULL },
338 { XML_MARGINS, text::HoriOrientation::NONE },
339 { XML_TOKEN_INVALID, 0 }
342 const struct SvXMLEnumMapEntry aXMLTableVAlignMap[] =
344 { XML_TOP, text::VertOrientation::TOP },
345 { XML_MIDDLE, text::VertOrientation::CENTER },
346 { XML_BOTTOM, text::VertOrientation::BOTTOM },
347 { XML_TOKEN_INVALID, 0 }
350 const struct SvXMLEnumMapEntry aXML_KeepTogetherType[] =
352 { XML_ALWAYS, 0 },
353 { XML_AUTO, 1 },
354 { XML_TOKEN_INVALID, 0}
358 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */