bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / style / bordrhdl.cxx
blob0e12efcdafa353c1e9def1435713862940148d5f
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 <bordrhdl.hxx>
21 #include <sax/tools/converter.hxx>
22 #include <xmloff/xmltoken.hxx>
23 #include <xmloff/xmluconv.hxx>
24 #include <rtl/ustrbuf.hxx>
25 #include <com/sun/star/uno/Any.hxx>
26 #include <com/sun/star/table/BorderLine2.hpp>
27 #include <com/sun/star/table/BorderLineStyle.hpp>
29 using namespace ::com::sun::star;
30 using namespace ::xmloff::token;
32 #define DEF_LINE_WIDTH_0 1
33 #define DEF_LINE_WIDTH_1 35
34 #define DEF_LINE_WIDTH_2 88
36 #define SVX_XML_BORDER_WIDTH_THIN 0
37 #define SVX_XML_BORDER_WIDTH_MIDDLE 1
38 #define SVX_XML_BORDER_WIDTH_THICK 2
40 SvXMLEnumMapEntry pXML_BorderStyles[] =
42 { XML_NONE, table::BorderLineStyle::NONE },
43 { XML_HIDDEN, table::BorderLineStyle::NONE },
44 { XML_SOLID, table::BorderLineStyle::SOLID },
45 { XML_DOUBLE, table::BorderLineStyle::DOUBLE },
46 { XML_DOUBLE_THIN, table::BorderLineStyle::DOUBLE_THIN },
47 { XML_DOTTED, table::BorderLineStyle::DOTTED },
48 { XML_DASHED, table::BorderLineStyle::DASHED },
49 { XML_GROOVE, table::BorderLineStyle::ENGRAVED },
50 { XML_RIDGE, table::BorderLineStyle::EMBOSSED },
51 { XML_INSET, table::BorderLineStyle::INSET },
52 { XML_OUTSET, table::BorderLineStyle::OUTSET },
53 { XML_FINE_DASHED, table::BorderLineStyle::FINE_DASHED },
54 { XML_DASH_DOT, table::BorderLineStyle::DASH_DOT },
55 { XML_DASH_DOT_DOT, table::BorderLineStyle::DASH_DOT_DOT },
56 { XML_TOKEN_INVALID, 0 }
59 SvXMLEnumMapEntry pXML_NamedBorderWidths[] =
61 { XML_THIN, SVX_XML_BORDER_WIDTH_THIN },
62 { XML_MIDDLE, SVX_XML_BORDER_WIDTH_MIDDLE },
63 { XML_THICK, SVX_XML_BORDER_WIDTH_THICK },
64 { XML_TOKEN_INVALID, 0 }
66 // mapping tables to map external xml input to internal box line widths
68 static sal_uInt16 const aBorderWidths[] =
70 DEF_LINE_WIDTH_0,
71 DEF_LINE_WIDTH_1,
72 DEF_LINE_WIDTH_2
75 static void lcl_frmitems_setXMLBorderStyle( table::BorderLine2 & rBorderLine, sal_uInt16 nStyle )
77 sal_Int16 eStyle = -1; // None
78 if (nStyle != table::BorderLineStyle::NONE)
79 eStyle = sal_Int16( nStyle );
81 rBorderLine.LineStyle = eStyle;
85 // class XMLEscapementPropHdl
88 XMLBorderWidthHdl::~XMLBorderWidthHdl()
90 // nothing to do
93 bool XMLBorderWidthHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
95 SvXMLTokenEnumerator aTokenEnum( rStrImpValue );
97 sal_Int32 nInWidth, nDistance, nOutWidth;
99 OUString aToken;
100 if( !aTokenEnum.getNextToken( aToken ) )
101 return false;
103 if (!rUnitConverter.convertMeasureToCore( nInWidth, aToken, 0, 500 ))
104 return false;
106 if( !aTokenEnum.getNextToken( aToken ) )
107 return false;
109 if (!rUnitConverter.convertMeasureToCore( nDistance, aToken, 0, 500 ))
110 return false;
112 if( !aTokenEnum.getNextToken( aToken ) )
113 return false;
115 if (!rUnitConverter.convertMeasureToCore( nOutWidth, aToken, 0, 500 ))
116 return false;
118 table::BorderLine2 aBorderLine;
119 if(!(rValue >>= aBorderLine))
120 aBorderLine.Color = 0;
122 aBorderLine.InnerLineWidth = sal::static_int_cast< sal_Int16 >(nInWidth);
123 aBorderLine.OuterLineWidth = sal::static_int_cast< sal_Int16 >(nOutWidth);
124 aBorderLine.LineDistance = sal::static_int_cast< sal_Int16 >(nDistance);
126 rValue <<= aBorderLine;
127 return true;
130 bool XMLBorderWidthHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
132 OUStringBuffer aOut;
134 table::BorderLine2 aBorderLine;
135 if(!(rValue >>= aBorderLine))
136 return false;
138 bool bDouble = false;
139 switch ( aBorderLine.LineStyle )
141 case table::BorderLineStyle::DOUBLE:
142 case table::BorderLineStyle::DOUBLE_THIN:
143 case table::BorderLineStyle::THINTHICK_SMALLGAP:
144 case table::BorderLineStyle::THINTHICK_MEDIUMGAP:
145 case table::BorderLineStyle::THINTHICK_LARGEGAP:
146 case table::BorderLineStyle::THICKTHIN_SMALLGAP:
147 case table::BorderLineStyle::THICKTHIN_MEDIUMGAP:
148 case table::BorderLineStyle::THICKTHIN_LARGEGAP:
149 bDouble = true;
150 break;
151 default:
152 break;
155 if( ( aBorderLine.LineDistance == 0 && aBorderLine.InnerLineWidth == 0 ) || !bDouble )
156 return false;
158 rUnitConverter.convertMeasureToXML( aOut, aBorderLine.InnerLineWidth );
159 aOut.append( ' ' );
160 rUnitConverter.convertMeasureToXML( aOut, aBorderLine.LineDistance );
161 aOut.append( ' ' );
162 rUnitConverter.convertMeasureToXML( aOut, aBorderLine.OuterLineWidth );
164 rStrExpValue = aOut.makeStringAndClear();
165 return true;
169 // class XMLEscapementHeightPropHdl
172 XMLBorderHdl::~XMLBorderHdl()
174 // nothing to do
177 bool XMLBorderHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
179 OUString aToken;
180 SvXMLTokenEnumerator aTokens( rStrImpValue );
182 bool bHasStyle = false;
183 bool bHasWidth = false;
184 bool bHasColor = false;
186 sal_uInt16 nStyle = USHRT_MAX;
187 sal_uInt16 nWidth = 0;
188 sal_uInt16 nNamedWidth = USHRT_MAX;
189 sal_Int32 nColor = 0;
191 sal_Int32 nTemp;
192 while( aTokens.getNextToken( aToken ) && !aToken.isEmpty() )
194 if( !bHasWidth &&
195 SvXMLUnitConverter::convertEnum( nNamedWidth, aToken,
196 pXML_NamedBorderWidths ) )
198 bHasWidth = true;
200 else if( !bHasStyle &&
201 SvXMLUnitConverter::convertEnum( nStyle, aToken,
202 pXML_BorderStyles ) )
204 bHasStyle = true;
206 else if (!bHasColor && ::sax::Converter::convertColor(nColor, aToken))
208 bHasColor = true;
210 else if( !bHasWidth &&
211 rUnitConverter.convertMeasureToCore( nTemp, aToken, 0,
212 USHRT_MAX ) )
214 nWidth = (sal_uInt16)nTemp;
215 bHasWidth = true;
217 else
219 // missformed
220 return false;
224 // if there is no style or a different style than none but no width,
225 // then the declaration is not valid.
226 if (!bHasStyle || (table::BorderLineStyle::NONE != nStyle && !bHasWidth))
227 return false;
229 table::BorderLine2 aBorderLine;
230 if(!(rValue >>= aBorderLine))
232 aBorderLine.Color = 0;
233 aBorderLine.InnerLineWidth = 0;
234 aBorderLine.OuterLineWidth = 0;
235 aBorderLine.LineDistance = 0;
236 aBorderLine.LineWidth = 0;
239 // first of all, delete an empty line
240 if (table::BorderLineStyle::NONE == nStyle ||
241 (bHasWidth && USHRT_MAX == nNamedWidth && 0 == nWidth) )
243 aBorderLine.InnerLineWidth = 0;
244 aBorderLine.OuterLineWidth = 0;
245 aBorderLine.LineDistance = 0;
246 aBorderLine.LineWidth = 0;
248 else
250 if( USHRT_MAX != nNamedWidth )
252 aBorderLine.LineWidth = aBorderWidths[nNamedWidth];
254 else
256 aBorderLine.LineWidth = nWidth;
257 lcl_frmitems_setXMLBorderStyle( aBorderLine, nStyle );
261 // set color
262 if( bHasColor )
264 aBorderLine.Color = nColor;
267 rValue <<= aBorderLine;
268 return true;
271 bool XMLBorderHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& /* rUnitConverter */ ) const
273 OUStringBuffer aOut;
275 table::BorderLine2 aBorderLine;
276 if(!(rValue >>= aBorderLine))
277 return false;
279 sal_Int32 nWidth = aBorderLine.LineWidth;
281 if( nWidth == 0 )
283 aOut.append( GetXMLToken( XML_NONE ) );
285 else
287 ::sax::Converter::convertMeasure( aOut, nWidth,
288 util::MeasureUnit::MM_100TH, util::MeasureUnit::POINT);
290 aOut.append( ' ' );
292 XMLTokenEnum eStyleToken = XML_SOLID;
293 switch ( aBorderLine.LineStyle )
295 case table::BorderLineStyle::DASHED:
296 eStyleToken = XML_DASHED;
297 break;
298 case table::BorderLineStyle::DOTTED:
299 eStyleToken = XML_DOTTED;
300 break;
301 case table::BorderLineStyle::DOUBLE:
302 case table::BorderLineStyle::THINTHICK_SMALLGAP:
303 case table::BorderLineStyle::THINTHICK_MEDIUMGAP:
304 case table::BorderLineStyle::THINTHICK_LARGEGAP:
305 case table::BorderLineStyle::THICKTHIN_SMALLGAP:
306 case table::BorderLineStyle::THICKTHIN_MEDIUMGAP:
307 case table::BorderLineStyle::THICKTHIN_LARGEGAP:
308 eStyleToken = XML_DOUBLE;
309 break;
310 case table::BorderLineStyle::EMBOSSED:
311 eStyleToken = XML_RIDGE;
312 break;
313 case table::BorderLineStyle::ENGRAVED:
314 eStyleToken = XML_GROOVE;
315 break;
316 case table::BorderLineStyle::OUTSET:
317 eStyleToken = XML_OUTSET;
318 break;
319 case table::BorderLineStyle::INSET:
320 eStyleToken = XML_INSET;
321 break;
322 case table::BorderLineStyle::FINE_DASHED:
323 eStyleToken = XML_FINE_DASHED;
324 break;
325 case table::BorderLineStyle::DASH_DOT:
326 eStyleToken = XML_DASH_DOT;
327 break;
328 case table::BorderLineStyle::DASH_DOT_DOT:
329 eStyleToken = XML_DASH_DOT_DOT;
330 break;
331 case table::BorderLineStyle::DOUBLE_THIN:
332 eStyleToken = XML_DOUBLE_THIN;
333 break;
334 case table::BorderLineStyle::SOLID:
335 default:
336 break;
338 aOut.append( GetXMLToken( eStyleToken ) );
340 aOut.append( ' ' );
342 ::sax::Converter::convertColor( aOut, aBorderLine.Color );
345 rStrExpValue = aOut.makeStringAndClear();
347 return true;
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */