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/xmluconv.hxx>
27 #include <xmloff/xmlnamespace.hxx>
28 #include <xmloff/xmltoken.hxx>
29 #include <xmloff/xmlexp.hxx>
30 #include <xmloff/xmlimp.hxx>
31 #include <xmloff/xmlement.hxx>
32 #include <rtl/ustrbuf.hxx>
33 #include <rtl/ustring.hxx>
34 #include <sal/log.hxx>
36 using namespace ::com::sun::star
;
37 using namespace ::xmloff::token
;
39 SvXMLEnumMapEntry
<drawing::DashStyle
> const pXML_DashStyle_Enum
[] =
41 { XML_RECT
, drawing::DashStyle_RECT
},
42 { XML_ROUND
, drawing::DashStyle_ROUND
},
43 { XML_RECT
, drawing::DashStyle_RECTRELATIVE
},
44 { XML_ROUND
, drawing::DashStyle_ROUNDRELATIVE
},
45 { XML_TOKEN_INVALID
, drawing::DashStyle(0) }
50 XMLDashStyleImport::XMLDashStyleImport( SvXMLImport
& rImp
)
55 void XMLDashStyleImport::importXML(
56 const uno::Reference
< xml::sax::XFastAttributeList
>& xAttrList
,
60 drawing::LineDash aLineDash
;
61 aLineDash
.Style
= drawing::DashStyle_RECT
;
65 aLineDash
.DashLen
= 0;
66 aLineDash
.Distance
= 20;
67 OUString aDisplayName
;
71 SvXMLUnitConverter
& rUnitConverter
= m_rImport
.GetMM100UnitConverter();
73 for (auto &aIter
: sax_fastparser::castToFastAttributeList( xAttrList
))
75 switch( aIter
.getToken() )
77 case XML_ELEMENT(DRAW
, XML_NAME
):
78 case XML_ELEMENT(DRAW_OOO
, XML_NAME
):
80 rStrName
= aIter
.toString();
83 case XML_ELEMENT(DRAW
, XML_DISPLAY_NAME
):
84 case XML_ELEMENT(DRAW_OOO
, XML_DISPLAY_NAME
):
86 aDisplayName
= aIter
.toString();
89 case XML_ELEMENT(DRAW
, XML_STYLE
):
90 case XML_ELEMENT(DRAW_OOO
, XML_STYLE
):
92 SvXMLUnitConverter::convertEnum( aLineDash
.Style
, aIter
.toView(), pXML_DashStyle_Enum
);
95 case XML_ELEMENT(DRAW
, XML_DOTS1
):
96 case XML_ELEMENT(DRAW_OOO
, XML_DOTS1
):
97 aLineDash
.Dots
= static_cast<sal_Int16
>(aIter
.toInt32());
100 case XML_ELEMENT(DRAW
, XML_DOTS1_LENGTH
):
101 case XML_ELEMENT(DRAW_OOO
, XML_DOTS1_LENGTH
):
103 if( aIter
.toView().find( '%' ) != std::string_view::npos
) // it's a percentage
106 ::sax::Converter::convertPercent(aLineDash
.DotLen
, aIter
.toView());
110 rUnitConverter
.convertMeasureToCore( aLineDash
.DotLen
,
116 case XML_ELEMENT(DRAW
, XML_DOTS2
):
117 case XML_ELEMENT(DRAW_OOO
, XML_DOTS2
):
118 aLineDash
.Dashes
= static_cast<sal_Int16
>(aIter
.toInt32());
121 case XML_ELEMENT(DRAW
, XML_DOTS2_LENGTH
):
122 case XML_ELEMENT(DRAW_OOO
, XML_DOTS2_LENGTH
):
124 if( aIter
.toView().find( '%' ) != std::string_view::npos
) // it's a percentage
127 ::sax::Converter::convertPercent(aLineDash
.DashLen
, aIter
.toView());
131 rUnitConverter
.convertMeasureToCore( aLineDash
.DashLen
,
137 case XML_ELEMENT(DRAW
, XML_DISTANCE
):
138 case XML_ELEMENT(DRAW_OOO
, XML_DISTANCE
):
140 if( aIter
.toView().find( '%' ) != std::string_view::npos
) // it's a percentage
143 ::sax::Converter::convertPercent(aLineDash
.Distance
, aIter
.toView());
147 rUnitConverter
.convertMeasureToCore( aLineDash
.Distance
,
153 XMLOFF_WARN_UNKNOWN("xmloff.style", aIter
);
158 aLineDash
.Style
= aLineDash
.Style
== drawing::DashStyle_RECT
? drawing::DashStyle_RECTRELATIVE
: drawing::DashStyle_ROUNDRELATIVE
;
160 rValue
<<= aLineDash
;
162 if( !aDisplayName
.isEmpty() )
164 m_rImport
.AddStyleDisplayName( XmlStyleFamily::SD_STROKE_DASH_ID
,
165 rStrName
, aDisplayName
);
166 rStrName
= aDisplayName
;
172 XMLDashStyleExport::XMLDashStyleExport( SvXMLExport
& rExp
)
177 void XMLDashStyleExport::exportXML(
178 const OUString
& rStrName
,
179 const uno::Any
& rValue
)
181 SvXMLUnitConverter
& rUnitConverter
= m_rExport
.GetMM100UnitConverter();
183 drawing::LineDash aLineDash
;
185 if( rStrName
.isEmpty() )
188 if( !(rValue
>>= aLineDash
) )
191 bool bIsRel
= aLineDash
.Style
== drawing::DashStyle_RECTRELATIVE
|| aLineDash
.Style
== drawing::DashStyle_ROUNDRELATIVE
;
197 bool bEncoded
= false;
198 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_NAME
,
199 m_rExport
.EncodeStyleName( rStrName
,
202 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DISPLAY_NAME
,
206 SvXMLUnitConverter::convertEnum( aOut
, aLineDash
.Style
, pXML_DashStyle_Enum
);
207 aStrValue
= aOut
.makeStringAndClear();
208 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_STYLE
, aStrValue
);
213 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS1
, OUString::number( aLineDash
.Dots
) );
215 if( aLineDash
.DotLen
)
220 ::sax::Converter::convertPercent(aOut
, aLineDash
.DotLen
);
224 rUnitConverter
.convertMeasureToXML( aOut
,
227 aStrValue
= aOut
.makeStringAndClear();
228 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS1_LENGTH
, aStrValue
);
233 if( aLineDash
.Dashes
)
235 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS2
, OUString::number( aLineDash
.Dashes
) );
237 if( aLineDash
.DashLen
)
242 ::sax::Converter::convertPercent(aOut
, aLineDash
.DashLen
);
246 rUnitConverter
.convertMeasureToXML( aOut
,
249 aStrValue
= aOut
.makeStringAndClear();
250 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS2_LENGTH
, aStrValue
);
257 ::sax::Converter::convertPercent( aOut
, aLineDash
.Distance
);
261 rUnitConverter
.convertMeasureToXML( aOut
,
262 aLineDash
.Distance
);
264 aStrValue
= aOut
.makeStringAndClear();
265 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DISTANCE
, aStrValue
);
268 SvXMLElementExport
rElem( m_rExport
,
269 XML_NAMESPACE_DRAW
, XML_STROKE_DASH
,
273 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */