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 <com/sun/star/drawing/DashStyle.hpp>
21 #include <com/sun/star/drawing/LineDash.hpp>
23 #include <sax/tools/converter.hxx>
25 #include <xmloff/DashStyle.hxx>
26 #include <xmloff/attrlist.hxx>
27 #include <xmloff/nmspmap.hxx>
28 #include <xmloff/xmluconv.hxx>
29 #include <xmloff/xmlnmspe.hxx>
30 #include <xmloff/xmltoken.hxx>
31 #include <xmloff/xmlexp.hxx>
32 #include <xmloff/xmlimp.hxx>
33 #include <rtl/ustrbuf.hxx>
34 #include <rtl/ustring.hxx>
35 #include <tools/debug.hxx>
36 #include <xmloff/xmltkmap.hxx>
38 using namespace ::com::sun::star
;
40 using namespace ::xmloff::token
;
42 enum SvXMLTokenMapAttrs
45 XML_TOK_DASH_DISPLAY_NAME
,
48 XML_TOK_DASH_DOTS1LEN
,
50 XML_TOK_DASH_DOTS2LEN
,
51 XML_TOK_DASH_DISTANCE
,
52 XML_TOK_DASH_END
=XML_TOK_UNKNOWN
55 static SvXMLTokenMapEntry aDashStyleAttrTokenMap
[] =
57 { XML_NAMESPACE_DRAW
, XML_NAME
, XML_TOK_DASH_NAME
},
58 { XML_NAMESPACE_DRAW
, XML_DISPLAY_NAME
, XML_TOK_DASH_DISPLAY_NAME
},
59 { XML_NAMESPACE_DRAW
, XML_STYLE
, XML_TOK_DASH_STYLE
},
60 { XML_NAMESPACE_DRAW
, XML_DOTS1
, XML_TOK_DASH_DOTS1
},
61 { XML_NAMESPACE_DRAW
, XML_DOTS1_LENGTH
, XML_TOK_DASH_DOTS1LEN
},
62 { XML_NAMESPACE_DRAW
, XML_DOTS2
, XML_TOK_DASH_DOTS2
},
63 { XML_NAMESPACE_DRAW
, XML_DOTS2_LENGTH
, XML_TOK_DASH_DOTS2LEN
},
64 { XML_NAMESPACE_DRAW
, XML_DISTANCE
, XML_TOK_DASH_DISTANCE
},
68 SvXMLEnumMapEntry
const pXML_DashStyle_Enum
[] =
70 { XML_RECT
, drawing::DashStyle_RECT
},
71 { XML_ROUND
, drawing::DashStyle_ROUND
},
72 { XML_RECT
, drawing::DashStyle_RECTRELATIVE
},
73 { XML_ROUND
, drawing::DashStyle_ROUNDRELATIVE
},
74 { XML_TOKEN_INVALID
, 0 }
79 XMLDashStyleImport::XMLDashStyleImport( SvXMLImport
& rImp
)
84 XMLDashStyleImport::~XMLDashStyleImport()
88 void XMLDashStyleImport::importXML(
89 const uno::Reference
< xml::sax::XAttributeList
>& xAttrList
,
93 drawing::LineDash aLineDash
;
94 aLineDash
.Style
= drawing::DashStyle_RECT
;
98 aLineDash
.DashLen
= 0;
99 aLineDash
.Distance
= 20;
100 OUString aDisplayName
;
104 SvXMLNamespaceMap
& rNamespaceMap
= rImport
.GetNamespaceMap();
105 SvXMLUnitConverter
& rUnitConverter
= rImport
.GetMM100UnitConverter();
107 SvXMLTokenMap
aTokenMap( aDashStyleAttrTokenMap
);
109 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
110 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
112 const OUString
& rFullAttrName
= xAttrList
->getNameByIndex( i
);
113 OUString aStrAttrName
;
114 sal_uInt16 nPrefix
= rNamespaceMap
.GetKeyByAttrName( rFullAttrName
, &aStrAttrName
);
115 const OUString
& rStrValue
= xAttrList
->getValueByIndex( i
);
117 switch( aTokenMap
.Get( nPrefix
, aStrAttrName
) )
119 case XML_TOK_DASH_NAME
:
121 rStrName
= rStrValue
;
124 case XML_TOK_DASH_DISPLAY_NAME
:
126 aDisplayName
= rStrValue
;
129 case XML_TOK_DASH_STYLE
:
132 if( SvXMLUnitConverter::convertEnum( eValue
, rStrValue
, pXML_DashStyle_Enum
) )
134 aLineDash
.Style
= (drawing::DashStyle
) eValue
;
138 case XML_TOK_DASH_DOTS1
:
139 aLineDash
.Dots
= (sal_Int16
)rStrValue
.toInt32();
142 case XML_TOK_DASH_DOTS1LEN
:
144 if( rStrValue
.indexOf( '%' ) != -1 ) // it's a percentage
147 ::sax::Converter::convertPercent(aLineDash
.DotLen
, rStrValue
);
151 rUnitConverter
.convertMeasureToCore( aLineDash
.DotLen
,
157 case XML_TOK_DASH_DOTS2
:
158 aLineDash
.Dashes
= (sal_Int16
)rStrValue
.toInt32();
161 case XML_TOK_DASH_DOTS2LEN
:
163 if( rStrValue
.indexOf( '%' ) != -1 ) // it's a percentage
166 ::sax::Converter::convertPercent(aLineDash
.DashLen
, rStrValue
);
170 rUnitConverter
.convertMeasureToCore( aLineDash
.DashLen
,
176 case XML_TOK_DASH_DISTANCE
:
178 if( rStrValue
.indexOf( '%' ) != -1 ) // it's a percentage
181 ::sax::Converter::convertPercent(aLineDash
.Distance
, rStrValue
);
185 rUnitConverter
.convertMeasureToCore( aLineDash
.Distance
,
191 SAL_INFO("xmloff.style", "Unknown token at import dash style");
196 aLineDash
.Style
= aLineDash
.Style
== drawing::DashStyle_RECT
? drawing::DashStyle_RECTRELATIVE
: drawing::DashStyle_ROUNDRELATIVE
;
198 rValue
<<= aLineDash
;
200 if( !aDisplayName
.isEmpty() )
202 rImport
.AddStyleDisplayName( XML_STYLE_FAMILY_SD_STROKE_DASH_ID
,
203 rStrName
, aDisplayName
);
204 rStrName
= aDisplayName
;
210 XMLDashStyleExport::XMLDashStyleExport( SvXMLExport
& rExp
)
215 XMLDashStyleExport::~XMLDashStyleExport()
219 bool XMLDashStyleExport::exportXML(
220 const OUString
& rStrName
,
221 const uno::Any
& rValue
)
225 SvXMLUnitConverter
& rUnitConverter
= rExport
.GetMM100UnitConverter();
227 drawing::LineDash aLineDash
;
229 if( !rStrName
.isEmpty() )
231 if( rValue
>>= aLineDash
)
233 bool bIsRel
= aLineDash
.Style
== drawing::DashStyle_RECTRELATIVE
|| aLineDash
.Style
== drawing::DashStyle_ROUNDRELATIVE
;
239 bool bEncoded
= false;
240 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_NAME
,
241 rExport
.EncodeStyleName( rStrName
,
244 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DISPLAY_NAME
,
248 SvXMLUnitConverter::convertEnum( aOut
, aLineDash
.Style
, pXML_DashStyle_Enum
);
249 aStrValue
= aOut
.makeStringAndClear();
250 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_STYLE
, aStrValue
);
255 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS1
, OUString::number( aLineDash
.Dots
) );
257 if( aLineDash
.DotLen
)
262 ::sax::Converter::convertPercent(aOut
, aLineDash
.DotLen
);
266 rUnitConverter
.convertMeasureToXML( aOut
,
269 aStrValue
= aOut
.makeStringAndClear();
270 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS1_LENGTH
, aStrValue
);
275 if( aLineDash
.Dashes
)
277 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS2
, OUString::number( aLineDash
.Dashes
) );
279 if( aLineDash
.DashLen
)
284 ::sax::Converter::convertPercent(aOut
, aLineDash
.DashLen
);
288 rUnitConverter
.convertMeasureToXML( aOut
,
291 aStrValue
= aOut
.makeStringAndClear();
292 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS2_LENGTH
, aStrValue
);
299 ::sax::Converter::convertPercent( aOut
, aLineDash
.Distance
);
303 rUnitConverter
.convertMeasureToXML( aOut
,
304 aLineDash
.Distance
);
306 aStrValue
= aOut
.makeStringAndClear();
307 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DISTANCE
, aStrValue
);
310 SvXMLElementExport
rElem( rExport
,
311 XML_NAMESPACE_DRAW
, XML_STROKE_DASH
,
318 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */