bump product version to 7.6.3.2-android
[LibreOffice.git] / xmloff / source / style / bordrhdl.cxx
blobd04d3f2f85c98c8fbf8e91e9ce2bbb87c9882285
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 <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>
30 #include <limits.h>
32 using namespace ::com::sun::star;
33 using namespace ::xmloff::token;
35 #define DEF_LINE_WIDTH_0 1
36 #define DEF_LINE_WIDTH_1 35
37 #define DEF_LINE_WIDTH_2 88
39 #define SVX_XML_BORDER_WIDTH_THIN 0
40 #define SVX_XML_BORDER_WIDTH_MIDDLE 1
41 #define SVX_XML_BORDER_WIDTH_THICK 2
43 SvXMLEnumMapEntry<sal_uInt16> const pXML_BorderStyles[] =
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 }
62 SvXMLEnumMapEntry<sal_uInt16> const pXML_NamedBorderWidths[] =
64 { XML_THIN, SVX_XML_BORDER_WIDTH_THIN },
65 { XML_MIDDLE, SVX_XML_BORDER_WIDTH_MIDDLE },
66 { XML_THICK, SVX_XML_BORDER_WIDTH_THICK },
67 { XML_TOKEN_INVALID, 0 }
69 // mapping tables to map external xml input to internal box line widths
71 sal_uInt16 const aBorderWidths[] =
73 DEF_LINE_WIDTH_0,
74 DEF_LINE_WIDTH_1,
75 DEF_LINE_WIDTH_2
78 static void lcl_frmitems_setXMLBorderStyle( table::BorderLine2 & rBorderLine, sal_uInt16 nStyle )
80 sal_Int16 eStyle = -1; // None
81 if (nStyle != table::BorderLineStyle::NONE)
82 eStyle = sal_Int16( nStyle );
84 rBorderLine.LineStyle = eStyle;
90 XMLBorderWidthHdl::~XMLBorderWidthHdl()
92 // nothing to do
95 bool XMLBorderWidthHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
97 SvXMLTokenEnumerator aTokenEnum( rStrImpValue );
99 sal_Int32 nInWidth, nDistance, nOutWidth;
101 std::u16string_view aToken;
102 if( !aTokenEnum.getNextToken( aToken ) )
103 return false;
105 if (!rUnitConverter.convertMeasureToCore( nInWidth, aToken, 0, 500 ))
106 return false;
108 if( !aTokenEnum.getNextToken( aToken ) )
109 return false;
111 if (!rUnitConverter.convertMeasureToCore( nDistance, aToken, 0, 500 ))
112 return false;
114 if( !aTokenEnum.getNextToken( aToken ) )
115 return false;
117 if (!rUnitConverter.convertMeasureToCore( nOutWidth, aToken, 0, 500 ))
118 return false;
120 table::BorderLine2 aBorderLine;
121 if(!(rValue >>= aBorderLine))
122 aBorderLine.Color = 0;
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);
128 rValue <<= aBorderLine;
129 return true;
132 bool XMLBorderWidthHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
134 OUStringBuffer aOut;
136 table::BorderLine2 aBorderLine;
137 if(!(rValue >>= aBorderLine))
138 return false;
140 bool bDouble = false;
141 switch ( aBorderLine.LineStyle )
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;
157 if( ( aBorderLine.LineDistance == 0 && aBorderLine.InnerLineWidth == 0 ) || !bDouble )
158 return false;
160 rUnitConverter.convertMeasureToXML( aOut, aBorderLine.InnerLineWidth );
161 aOut.append( ' ' );
162 rUnitConverter.convertMeasureToXML( aOut, aBorderLine.LineDistance );
163 aOut.append( ' ' );
164 rUnitConverter.convertMeasureToXML( aOut, aBorderLine.OuterLineWidth );
166 rStrExpValue = aOut.makeStringAndClear();
167 return true;
173 XMLBorderHdl::~XMLBorderHdl()
175 // nothing to do
178 bool XMLBorderHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
180 std::u16string_view aToken;
181 SvXMLTokenEnumerator aTokens( rStrImpValue );
183 bool bHasStyle = false;
184 bool bHasWidth = false;
185 bool bHasColor = false;
187 sal_uInt16 nStyle = USHRT_MAX;
188 sal_uInt16 nWidth = 0;
189 sal_uInt16 nNamedWidth = USHRT_MAX;
190 sal_Int32 nColor = 0;
192 sal_Int32 nTemp;
193 while( aTokens.getNextToken( aToken ) && !aToken.empty() )
195 if( !bHasWidth &&
196 SvXMLUnitConverter::convertEnum( nNamedWidth, aToken,
197 pXML_NamedBorderWidths ) )
199 bHasWidth = true;
201 else if( !bHasStyle &&
202 SvXMLUnitConverter::convertEnum( nStyle, aToken,
203 pXML_BorderStyles ) )
205 bHasStyle = true;
207 else if (!bHasColor && ::sax::Converter::convertColor(nColor, aToken))
209 bHasColor = true;
211 else if( !bHasWidth &&
212 rUnitConverter.convertMeasureToCore( nTemp, aToken, 0,
213 USHRT_MAX ) )
215 nWidth = static_cast<sal_uInt16>(nTemp);
216 bHasWidth = true;
218 else
220 // misformed
221 return false;
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;
230 table::BorderLine2 aBorderLine;
231 if(!(rValue >>= aBorderLine))
233 aBorderLine.Color = 0;
234 aBorderLine.InnerLineWidth = 0;
235 aBorderLine.OuterLineWidth = 0;
236 aBorderLine.LineDistance = 0;
237 aBorderLine.LineWidth = 0;
240 // first of all, delete an empty line
241 if (table::BorderLineStyle::NONE == nStyle ||
242 (bHasWidth && USHRT_MAX == nNamedWidth && 0 == nWidth) )
244 aBorderLine.InnerLineWidth = 0;
245 aBorderLine.OuterLineWidth = 0;
246 aBorderLine.LineDistance = 0;
247 aBorderLine.LineWidth = 0;
249 else
251 if( USHRT_MAX != nNamedWidth )
253 aBorderLine.LineWidth = aBorderWidths[nNamedWidth];
255 else
257 aBorderLine.LineWidth = nWidth;
258 lcl_frmitems_setXMLBorderStyle( aBorderLine, nStyle );
262 // set color
263 if( bHasColor )
265 aBorderLine.Color = nColor;
268 rValue <<= aBorderLine;
269 return true;
272 bool XMLBorderHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& /* rUnitConverter */ ) const
274 OUStringBuffer aOut;
276 table::BorderLine2 aBorderLine;
277 if(!(rValue >>= aBorderLine))
278 return false;
280 sal_Int32 nWidth = aBorderLine.LineWidth;
282 if( nWidth == 0 )
284 aOut.append( GetXMLToken( XML_NONE ) );
286 else
288 ::sax::Converter::convertMeasure( aOut, nWidth,
289 util::MeasureUnit::MM_100TH, util::MeasureUnit::POINT);
291 aOut.append( ' ' );
293 XMLTokenEnum eStyleToken = XML_SOLID;
294 switch ( aBorderLine.LineStyle )
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;
339 aOut.append( GetXMLToken( eStyleToken ) + " " );
341 ::sax::Converter::convertColor( aOut, aBorderLine.Color );
344 rStrExpValue = aOut.makeStringAndClear();
346 return true;
349 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */