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 .
21 #include <com/sun/star/drawing/DashStyle.hpp>
22 #include <com/sun/star/drawing/LineDash.hpp>
24 #include <sax/tools/converter.hxx>
26 #include "xmloff/DashStyle.hxx"
27 #include <xmloff/attrlist.hxx>
28 #include <xmloff/nmspmap.hxx>
29 #include <xmloff/xmluconv.hxx>
30 #include "xmloff/xmlnmspe.hxx"
31 #include <xmloff/xmltoken.hxx>
32 #include <xmloff/xmlexp.hxx>
33 #include <xmloff/xmlimp.hxx>
34 #include <rtl/ustrbuf.hxx>
35 #include <rtl/ustring.hxx>
36 #include <tools/debug.hxx>
37 #include <xmloff/xmltkmap.hxx>
39 using namespace ::com::sun::star
;
41 using namespace ::xmloff::token
;
43 enum SvXMLTokenMapAttrs
46 XML_TOK_DASH_DISPLAY_NAME
,
49 XML_TOK_DASH_DOTS1LEN
,
51 XML_TOK_DASH_DOTS2LEN
,
52 XML_TOK_DASH_DISTANCE
,
53 XML_TOK_DASH_END
=XML_TOK_UNKNOWN
56 static SvXMLTokenMapEntry aDashStyleAttrTokenMap
[] =
58 { XML_NAMESPACE_DRAW
, XML_NAME
, XML_TOK_DASH_NAME
},
59 { XML_NAMESPACE_DRAW
, XML_DISPLAY_NAME
, XML_TOK_DASH_DISPLAY_NAME
},
60 { XML_NAMESPACE_DRAW
, XML_STYLE
, XML_TOK_DASH_STYLE
},
61 { XML_NAMESPACE_DRAW
, XML_DOTS1
, XML_TOK_DASH_DOTS1
},
62 { XML_NAMESPACE_DRAW
, XML_DOTS1_LENGTH
, XML_TOK_DASH_DOTS1LEN
},
63 { XML_NAMESPACE_DRAW
, XML_DOTS2
, XML_TOK_DASH_DOTS2
},
64 { XML_NAMESPACE_DRAW
, XML_DOTS2_LENGTH
, XML_TOK_DASH_DOTS2LEN
},
65 { XML_NAMESPACE_DRAW
, XML_DISTANCE
, XML_TOK_DASH_DISTANCE
},
69 SvXMLEnumMapEntry
const pXML_DashStyle_Enum
[] =
71 { XML_RECT
, drawing::DashStyle_RECT
},
72 { XML_ROUND
, drawing::DashStyle_ROUND
},
73 { XML_RECT
, drawing::DashStyle_RECTRELATIVE
},
74 { XML_ROUND
, drawing::DashStyle_ROUNDRELATIVE
},
75 { XML_TOKEN_INVALID
, 0 }
78 //-------------------------------------------------------------
80 //-------------------------------------------------------------
82 XMLDashStyleImport::XMLDashStyleImport( SvXMLImport
& rImp
)
87 XMLDashStyleImport::~XMLDashStyleImport()
91 sal_Bool
XMLDashStyleImport::importXML(
92 const uno::Reference
< xml::sax::XAttributeList
>& xAttrList
,
96 drawing::LineDash aLineDash
;
97 aLineDash
.Style
= drawing::DashStyle_RECT
;
100 aLineDash
.Dashes
= 0;
101 aLineDash
.DashLen
= 0;
102 aLineDash
.Distance
= 20;
103 OUString aDisplayName
;
105 sal_Bool bIsRel
= sal_False
;
107 SvXMLNamespaceMap
& rNamespaceMap
= rImport
.GetNamespaceMap();
108 SvXMLUnitConverter
& rUnitConverter
= rImport
.GetMM100UnitConverter();
110 SvXMLTokenMap
aTokenMap( aDashStyleAttrTokenMap
);
112 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
113 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
115 const OUString
& rFullAttrName
= xAttrList
->getNameByIndex( i
);
116 OUString aStrAttrName
;
117 sal_uInt16 nPrefix
= rNamespaceMap
.GetKeyByAttrName( rFullAttrName
, &aStrAttrName
);
118 const OUString
& rStrValue
= xAttrList
->getValueByIndex( i
);
120 switch( aTokenMap
.Get( nPrefix
, aStrAttrName
) )
122 case XML_TOK_DASH_NAME
:
124 rStrName
= rStrValue
;
127 case XML_TOK_DASH_DISPLAY_NAME
:
129 aDisplayName
= rStrValue
;
132 case XML_TOK_DASH_STYLE
:
135 if( rUnitConverter
.convertEnum( eValue
, rStrValue
, pXML_DashStyle_Enum
) )
137 aLineDash
.Style
= (drawing::DashStyle
) eValue
;
141 case XML_TOK_DASH_DOTS1
:
142 aLineDash
.Dots
= (sal_Int16
)rStrValue
.toInt32();
145 case XML_TOK_DASH_DOTS1LEN
:
147 if( rStrValue
.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
150 ::sax::Converter::convertPercent(aLineDash
.DotLen
, rStrValue
);
154 rUnitConverter
.convertMeasureToCore( aLineDash
.DotLen
,
160 case XML_TOK_DASH_DOTS2
:
161 aLineDash
.Dashes
= (sal_Int16
)rStrValue
.toInt32();
164 case XML_TOK_DASH_DOTS2LEN
:
166 if( rStrValue
.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
169 ::sax::Converter::convertPercent(aLineDash
.DashLen
, rStrValue
);
173 rUnitConverter
.convertMeasureToCore( aLineDash
.DashLen
,
179 case XML_TOK_DASH_DISTANCE
:
181 if( rStrValue
.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
184 ::sax::Converter::convertPercent(aLineDash
.Distance
, rStrValue
);
188 rUnitConverter
.convertMeasureToCore( aLineDash
.Distance
,
194 DBG_WARNING( "Unknown token at import gradient style" );
199 aLineDash
.Style
= aLineDash
.Style
== drawing::DashStyle_RECT
? drawing::DashStyle_RECTRELATIVE
: drawing::DashStyle_ROUNDRELATIVE
;
201 rValue
<<= aLineDash
;
203 if( !aDisplayName
.isEmpty() )
205 rImport
.AddStyleDisplayName( XML_STYLE_FAMILY_SD_STROKE_DASH_ID
,
206 rStrName
, aDisplayName
);
207 rStrName
= aDisplayName
;
214 //-------------------------------------------------------------
216 //-------------------------------------------------------------
218 XMLDashStyleExport::XMLDashStyleExport( SvXMLExport
& rExp
)
223 XMLDashStyleExport::~XMLDashStyleExport()
227 sal_Bool
XMLDashStyleExport::exportXML(
228 const OUString
& rStrName
,
229 const uno::Any
& rValue
)
231 sal_Bool bRet
= sal_False
;
233 SvXMLUnitConverter
& rUnitConverter
= rExport
.GetMM100UnitConverter();
235 drawing::LineDash aLineDash
;
237 if( !rStrName
.isEmpty() )
239 if( rValue
>>= aLineDash
)
241 sal_Bool bIsRel
= aLineDash
.Style
== drawing::DashStyle_RECTRELATIVE
|| aLineDash
.Style
== drawing::DashStyle_ROUNDRELATIVE
;
247 sal_Bool bEncoded
= sal_False
;
248 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_NAME
,
249 rExport
.EncodeStyleName( rStrName
,
252 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DISPLAY_NAME
,
256 rUnitConverter
.convertEnum( aOut
, aLineDash
.Style
, pXML_DashStyle_Enum
);
257 aStrValue
= aOut
.makeStringAndClear();
258 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_STYLE
, aStrValue
);
264 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS1
, OUString::valueOf( (sal_Int32
)aLineDash
.Dots
) );
266 if( aLineDash
.DotLen
)
271 ::sax::Converter::convertPercent(aOut
, aLineDash
.DotLen
);
275 rUnitConverter
.convertMeasureToXML( aOut
,
278 aStrValue
= aOut
.makeStringAndClear();
279 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS1_LENGTH
, aStrValue
);
284 if( aLineDash
.Dashes
)
286 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS2
, OUString::valueOf( (sal_Int32
)aLineDash
.Dashes
) );
288 if( aLineDash
.DashLen
)
293 ::sax::Converter::convertPercent(aOut
, aLineDash
.DashLen
);
297 rUnitConverter
.convertMeasureToXML( aOut
,
300 aStrValue
= aOut
.makeStringAndClear();
301 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS2_LENGTH
, aStrValue
);
308 ::sax::Converter::convertPercent( aOut
, aLineDash
.Distance
);
312 rUnitConverter
.convertMeasureToXML( aOut
,
313 aLineDash
.Distance
);
315 aStrValue
= aOut
.makeStringAndClear();
316 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DISTANCE
, aStrValue
);
320 SvXMLElementExport
rElem( rExport
,
321 XML_NAMESPACE_DRAW
, XML_STROKE_DASH
,
322 sal_True
, sal_False
);
328 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */