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/namespacemap.hxx>
27 #include <xmloff/xmluconv.hxx>
28 #include <xmloff/xmlnamespace.hxx>
29 #include <xmloff/xmltoken.hxx>
30 #include <xmloff/xmlexp.hxx>
31 #include <xmloff/xmlimp.hxx>
32 #include <xmloff/xmlement.hxx>
33 #include <rtl/ustrbuf.hxx>
34 #include <rtl/ustring.hxx>
35 #include <sal/log.hxx>
36 #include <xmloff/xmltkmap.hxx>
38 using namespace ::com::sun::star
;
39 using namespace ::xmloff::token
;
41 SvXMLEnumMapEntry
<drawing::DashStyle
> const pXML_DashStyle_Enum
[] =
43 { XML_RECT
, drawing::DashStyle_RECT
},
44 { XML_ROUND
, drawing::DashStyle_ROUND
},
45 { XML_RECT
, drawing::DashStyle_RECTRELATIVE
},
46 { XML_ROUND
, drawing::DashStyle_ROUNDRELATIVE
},
47 { XML_TOKEN_INVALID
, drawing::DashStyle(0) }
52 XMLDashStyleImport::XMLDashStyleImport( SvXMLImport
& rImp
)
57 void XMLDashStyleImport::importXML(
58 const uno::Reference
< xml::sax::XFastAttributeList
>& xAttrList
,
62 drawing::LineDash aLineDash
;
63 aLineDash
.Style
= drawing::DashStyle_RECT
;
67 aLineDash
.DashLen
= 0;
68 aLineDash
.Distance
= 20;
69 OUString aDisplayName
;
73 SvXMLUnitConverter
& rUnitConverter
= m_rImport
.GetMM100UnitConverter();
75 for (auto &aIter
: sax_fastparser::castToFastAttributeList( xAttrList
))
77 switch( aIter
.getToken() )
79 case XML_ELEMENT(DRAW
, XML_NAME
):
80 case XML_ELEMENT(DRAW_OOO
, XML_NAME
):
82 rStrName
= aIter
.toString();
85 case XML_ELEMENT(DRAW
, XML_DISPLAY_NAME
):
86 case XML_ELEMENT(DRAW_OOO
, XML_DISPLAY_NAME
):
88 aDisplayName
= aIter
.toString();
91 case XML_ELEMENT(DRAW
, XML_STYLE
):
92 case XML_ELEMENT(DRAW_OOO
, XML_STYLE
):
94 SvXMLUnitConverter::convertEnum( aLineDash
.Style
, aIter
.toView(), pXML_DashStyle_Enum
);
97 case XML_ELEMENT(DRAW
, XML_DOTS1
):
98 case XML_ELEMENT(DRAW_OOO
, XML_DOTS1
):
99 aLineDash
.Dots
= static_cast<sal_Int16
>(aIter
.toInt32());
102 case XML_ELEMENT(DRAW
, XML_DOTS1_LENGTH
):
103 case XML_ELEMENT(DRAW_OOO
, XML_DOTS1_LENGTH
):
105 if( aIter
.toView().find( '%' ) != std::string_view::npos
) // it's a percentage
108 ::sax::Converter::convertPercent(aLineDash
.DotLen
, aIter
.toView());
112 rUnitConverter
.convertMeasureToCore( aLineDash
.DotLen
,
118 case XML_ELEMENT(DRAW
, XML_DOTS2
):
119 case XML_ELEMENT(DRAW_OOO
, XML_DOTS2
):
120 aLineDash
.Dashes
= static_cast<sal_Int16
>(aIter
.toInt32());
123 case XML_ELEMENT(DRAW
, XML_DOTS2_LENGTH
):
124 case XML_ELEMENT(DRAW_OOO
, XML_DOTS2_LENGTH
):
126 if( aIter
.toView().find( '%' ) != std::string_view::npos
) // it's a percentage
129 ::sax::Converter::convertPercent(aLineDash
.DashLen
, aIter
.toView());
133 rUnitConverter
.convertMeasureToCore( aLineDash
.DashLen
,
139 case XML_ELEMENT(DRAW
, XML_DISTANCE
):
140 case XML_ELEMENT(DRAW_OOO
, XML_DISTANCE
):
142 if( aIter
.toView().find( '%' ) != std::string_view::npos
) // it's a percentage
145 ::sax::Converter::convertPercent(aLineDash
.Distance
, aIter
.toView());
149 rUnitConverter
.convertMeasureToCore( aLineDash
.Distance
,
155 XMLOFF_WARN_UNKNOWN("xmloff.style", aIter
);
160 aLineDash
.Style
= aLineDash
.Style
== drawing::DashStyle_RECT
? drawing::DashStyle_RECTRELATIVE
: drawing::DashStyle_ROUNDRELATIVE
;
162 rValue
<<= aLineDash
;
164 if( !aDisplayName
.isEmpty() )
166 m_rImport
.AddStyleDisplayName( XmlStyleFamily::SD_STROKE_DASH_ID
,
167 rStrName
, aDisplayName
);
168 rStrName
= aDisplayName
;
174 XMLDashStyleExport::XMLDashStyleExport( SvXMLExport
& rExp
)
179 void XMLDashStyleExport::exportXML(
180 const OUString
& rStrName
,
181 const uno::Any
& rValue
)
183 SvXMLUnitConverter
& rUnitConverter
= m_rExport
.GetMM100UnitConverter();
185 drawing::LineDash aLineDash
;
187 if( rStrName
.isEmpty() )
190 if( !(rValue
>>= aLineDash
) )
193 bool bIsRel
= aLineDash
.Style
== drawing::DashStyle_RECTRELATIVE
|| aLineDash
.Style
== drawing::DashStyle_ROUNDRELATIVE
;
199 bool bEncoded
= false;
200 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_NAME
,
201 m_rExport
.EncodeStyleName( rStrName
,
204 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DISPLAY_NAME
,
208 SvXMLUnitConverter::convertEnum( aOut
, aLineDash
.Style
, pXML_DashStyle_Enum
);
209 aStrValue
= aOut
.makeStringAndClear();
210 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_STYLE
, aStrValue
);
215 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS1
, OUString::number( aLineDash
.Dots
) );
217 if( aLineDash
.DotLen
)
222 ::sax::Converter::convertPercent(aOut
, aLineDash
.DotLen
);
226 rUnitConverter
.convertMeasureToXML( aOut
,
229 aStrValue
= aOut
.makeStringAndClear();
230 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS1_LENGTH
, aStrValue
);
235 if( aLineDash
.Dashes
)
237 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS2
, OUString::number( aLineDash
.Dashes
) );
239 if( aLineDash
.DashLen
)
244 ::sax::Converter::convertPercent(aOut
, aLineDash
.DashLen
);
248 rUnitConverter
.convertMeasureToXML( aOut
,
251 aStrValue
= aOut
.makeStringAndClear();
252 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS2_LENGTH
, aStrValue
);
259 ::sax::Converter::convertPercent( aOut
, aLineDash
.Distance
);
263 rUnitConverter
.convertMeasureToXML( aOut
,
264 aLineDash
.Distance
);
266 aStrValue
= aOut
.makeStringAndClear();
267 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DISTANCE
, aStrValue
);
270 SvXMLElementExport
rElem( m_rExport
,
271 XML_NAMESPACE_DRAW
, XML_STROKE_DASH
,
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */