bump product version to 7.6.3.2-android
[LibreOffice.git] / xmloff / source / style / DashStyle.cxx
blob009b7f12973ef76bd980106121f7ccb47694b6f8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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) }
50 // Import
52 XMLDashStyleImport::XMLDashStyleImport( SvXMLImport& rImp )
53 : m_rImport(rImp)
57 void XMLDashStyleImport::importXML(
58 const uno::Reference< xml::sax::XFastAttributeList >& xAttrList,
59 uno::Any& rValue,
60 OUString& rStrName )
62 drawing::LineDash aLineDash;
63 aLineDash.Style = drawing::DashStyle_RECT;
64 aLineDash.Dots = 0;
65 aLineDash.DotLen = 0;
66 aLineDash.Dashes = 0;
67 aLineDash.DashLen = 0;
68 aLineDash.Distance = 20;
69 OUString aDisplayName;
71 bool bIsRel = false;
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();
84 break;
85 case XML_ELEMENT(DRAW, XML_DISPLAY_NAME):
86 case XML_ELEMENT(DRAW_OOO, XML_DISPLAY_NAME):
88 aDisplayName = aIter.toString();
90 break;
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 );
96 break;
97 case XML_ELEMENT(DRAW, XML_DOTS1):
98 case XML_ELEMENT(DRAW_OOO, XML_DOTS1):
99 aLineDash.Dots = static_cast<sal_Int16>(aIter.toInt32());
100 break;
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
107 bIsRel = true;
108 ::sax::Converter::convertPercent(aLineDash.DotLen, aIter.toView());
110 else
112 rUnitConverter.convertMeasureToCore( aLineDash.DotLen,
113 aIter.toView() );
116 break;
118 case XML_ELEMENT(DRAW, XML_DOTS2):
119 case XML_ELEMENT(DRAW_OOO, XML_DOTS2):
120 aLineDash.Dashes = static_cast<sal_Int16>(aIter.toInt32());
121 break;
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
128 bIsRel = true;
129 ::sax::Converter::convertPercent(aLineDash.DashLen, aIter.toView());
131 else
133 rUnitConverter.convertMeasureToCore( aLineDash.DashLen,
134 aIter.toView() );
137 break;
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
144 bIsRel = true;
145 ::sax::Converter::convertPercent(aLineDash.Distance, aIter.toView());
147 else
149 rUnitConverter.convertMeasureToCore( aLineDash.Distance,
150 aIter.toView() );
153 break;
154 default:
155 XMLOFF_WARN_UNKNOWN("xmloff.style", aIter);
159 if( bIsRel )
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;
172 // Export
174 XMLDashStyleExport::XMLDashStyleExport( SvXMLExport& rExp )
175 : m_rExport(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() )
188 return;
190 if( !(rValue >>= aLineDash) )
191 return;
193 bool bIsRel = aLineDash.Style == drawing::DashStyle_RECTRELATIVE || aLineDash.Style == drawing::DashStyle_ROUNDRELATIVE;
195 OUString aStrValue;
196 OUStringBuffer aOut;
198 // Name
199 bool bEncoded = false;
200 m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
201 m_rExport.EncodeStyleName( rStrName,
202 &bEncoded ) );
203 if( bEncoded )
204 m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
205 rStrName );
207 // Style
208 SvXMLUnitConverter::convertEnum( aOut, aLineDash.Style, pXML_DashStyle_Enum );
209 aStrValue = aOut.makeStringAndClear();
210 m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
212 // dots
213 if( aLineDash.Dots )
215 m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1, OUString::number( aLineDash.Dots ) );
217 if( aLineDash.DotLen )
219 // dashes length
220 if( bIsRel )
222 ::sax::Converter::convertPercent(aOut, aLineDash.DotLen);
224 else
226 rUnitConverter.convertMeasureToXML( aOut,
227 aLineDash.DotLen );
229 aStrValue = aOut.makeStringAndClear();
230 m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1_LENGTH, aStrValue );
234 // dashes
235 if( aLineDash.Dashes )
237 m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2, OUString::number( aLineDash.Dashes ) );
239 if( aLineDash.DashLen )
241 // dashes length
242 if( bIsRel )
244 ::sax::Converter::convertPercent(aOut, aLineDash.DashLen);
246 else
248 rUnitConverter.convertMeasureToXML( aOut,
249 aLineDash.DashLen );
251 aStrValue = aOut.makeStringAndClear();
252 m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2_LENGTH, aStrValue );
256 // distance
257 if( bIsRel )
259 ::sax::Converter::convertPercent( aOut, aLineDash.Distance );
261 else
263 rUnitConverter.convertMeasureToXML( aOut,
264 aLineDash.Distance );
266 aStrValue = aOut.makeStringAndClear();
267 m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISTANCE, aStrValue );
269 // do Write
270 SvXMLElementExport rElem( m_rExport,
271 XML_NAMESPACE_DRAW, XML_STROKE_DASH,
272 true, false );
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */