1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
[] =
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()
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
) )
105 if (!rUnitConverter
.convertMeasureToCore( nInWidth
, aToken
, 0, 500 ))
108 if( !aTokenEnum
.getNextToken( aToken
) )
111 if (!rUnitConverter
.convertMeasureToCore( nDistance
, aToken
, 0, 500 ))
114 if( !aTokenEnum
.getNextToken( aToken
) )
117 if (!rUnitConverter
.convertMeasureToCore( nOutWidth
, aToken
, 0, 500 ))
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
;
132 bool XMLBorderWidthHdl::exportXML( OUString
& rStrExpValue
, const uno::Any
& rValue
, const SvXMLUnitConverter
& rUnitConverter
) const
136 table::BorderLine2 aBorderLine
;
137 if(!(rValue
>>= aBorderLine
))
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
:
157 if( ( aBorderLine
.LineDistance
== 0 && aBorderLine
.InnerLineWidth
== 0 ) || !bDouble
)
160 rUnitConverter
.convertMeasureToXML( aOut
, aBorderLine
.InnerLineWidth
);
162 rUnitConverter
.convertMeasureToXML( aOut
, aBorderLine
.LineDistance
);
164 rUnitConverter
.convertMeasureToXML( aOut
, aBorderLine
.OuterLineWidth
);
166 rStrExpValue
= aOut
.makeStringAndClear();
173 XMLBorderHdl::~XMLBorderHdl()
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;
193 while( aTokens
.getNextToken( aToken
) && !aToken
.empty() )
196 SvXMLUnitConverter::convertEnum( nNamedWidth
, aToken
,
197 pXML_NamedBorderWidths
) )
201 else if( !bHasStyle
&&
202 SvXMLUnitConverter::convertEnum( nStyle
, aToken
,
203 pXML_BorderStyles
) )
207 else if (!bHasColor
&& ::sax::Converter::convertColor(nColor
, aToken
))
211 else if( !bHasWidth
&&
212 rUnitConverter
.convertMeasureToCore( nTemp
, aToken
, 0,
215 nWidth
= static_cast<sal_uInt16
>(nTemp
);
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
))
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;
251 if( USHRT_MAX
!= nNamedWidth
)
253 aBorderLine
.LineWidth
= aBorderWidths
[nNamedWidth
];
257 aBorderLine
.LineWidth
= nWidth
;
258 lcl_frmitems_setXMLBorderStyle( aBorderLine
, nStyle
);
265 aBorderLine
.Color
= nColor
;
268 rValue
<<= aBorderLine
;
272 bool XMLBorderHdl::exportXML( OUString
& rStrExpValue
, const uno::Any
& rValue
, const SvXMLUnitConverter
& /* rUnitConverter */ ) const
276 table::BorderLine2 aBorderLine
;
277 if(!(rValue
>>= aBorderLine
))
280 sal_Int32 nWidth
= aBorderLine
.LineWidth
;
284 aOut
.append( GetXMLToken( XML_NONE
) );
288 ::sax::Converter::convertMeasure( aOut
, nWidth
,
289 util::MeasureUnit::MM_100TH
, util::MeasureUnit::POINT
);
293 XMLTokenEnum eStyleToken
= XML_SOLID
;
294 switch ( aBorderLine
.LineStyle
)
296 case table::BorderLineStyle::DASHED
:
297 eStyleToken
= XML_DASHED
;
299 case table::BorderLineStyle::DOTTED
:
300 eStyleToken
= XML_DOTTED
;
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
;
311 case table::BorderLineStyle::EMBOSSED
:
312 eStyleToken
= XML_RIDGE
;
314 case table::BorderLineStyle::ENGRAVED
:
315 eStyleToken
= XML_GROOVE
;
317 case table::BorderLineStyle::OUTSET
:
318 eStyleToken
= XML_OUTSET
;
320 case table::BorderLineStyle::INSET
:
321 eStyleToken
= XML_INSET
;
323 case table::BorderLineStyle::FINE_DASHED
:
324 eStyleToken
= XML_FINE_DASHED
;
326 case table::BorderLineStyle::DASH_DOT
:
327 eStyleToken
= XML_DASH_DOT
;
329 case table::BorderLineStyle::DASH_DOT_DOT
:
330 eStyleToken
= XML_DASH_DOT_DOT
;
332 case table::BorderLineStyle::DOUBLE_THIN
:
333 eStyleToken
= XML_DOUBLE_THIN
;
335 case table::BorderLineStyle::SOLID
:
339 aOut
.append( GetXMLToken( eStyleToken
) + " " );
341 ::sax::Converter::convertColor( aOut
, aBorderLine
.Color
);
344 rStrExpValue
= aOut
.makeStringAndClear();
349 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */