bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / style / DashStyle.cxx
blobd1aafde9c3778f65d39b32a9837fa850415ae3a0
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/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
44 XML_TOK_DASH_NAME,
45 XML_TOK_DASH_DISPLAY_NAME,
46 XML_TOK_DASH_STYLE,
47 XML_TOK_DASH_DOTS1,
48 XML_TOK_DASH_DOTS1LEN,
49 XML_TOK_DASH_DOTS2,
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 },
65 XML_TOKEN_MAP_END
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 }
77 // Import
79 XMLDashStyleImport::XMLDashStyleImport( SvXMLImport& rImp )
80 : rImport(rImp)
84 XMLDashStyleImport::~XMLDashStyleImport()
88 void XMLDashStyleImport::importXML(
89 const uno::Reference< xml::sax::XAttributeList >& xAttrList,
90 uno::Any& rValue,
91 OUString& rStrName )
93 drawing::LineDash aLineDash;
94 aLineDash.Style = drawing::DashStyle_RECT;
95 aLineDash.Dots = 0;
96 aLineDash.DotLen = 0;
97 aLineDash.Dashes = 0;
98 aLineDash.DashLen = 0;
99 aLineDash.Distance = 20;
100 OUString aDisplayName;
102 bool bIsRel = false;
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;
123 break;
124 case XML_TOK_DASH_DISPLAY_NAME:
126 aDisplayName = rStrValue;
128 break;
129 case XML_TOK_DASH_STYLE:
131 sal_uInt16 eValue;
132 if( SvXMLUnitConverter::convertEnum( eValue, rStrValue, pXML_DashStyle_Enum ) )
134 aLineDash.Style = (drawing::DashStyle) eValue;
137 break;
138 case XML_TOK_DASH_DOTS1:
139 aLineDash.Dots = (sal_Int16)rStrValue.toInt32();
140 break;
142 case XML_TOK_DASH_DOTS1LEN:
144 if( rStrValue.indexOf( '%' ) != -1 ) // it's a percentage
146 bIsRel = true;
147 ::sax::Converter::convertPercent(aLineDash.DotLen, rStrValue);
149 else
151 rUnitConverter.convertMeasureToCore( aLineDash.DotLen,
152 rStrValue );
155 break;
157 case XML_TOK_DASH_DOTS2:
158 aLineDash.Dashes = (sal_Int16)rStrValue.toInt32();
159 break;
161 case XML_TOK_DASH_DOTS2LEN:
163 if( rStrValue.indexOf( '%' ) != -1 ) // it's a percentage
165 bIsRel = true;
166 ::sax::Converter::convertPercent(aLineDash.DashLen, rStrValue);
168 else
170 rUnitConverter.convertMeasureToCore( aLineDash.DashLen,
171 rStrValue );
174 break;
176 case XML_TOK_DASH_DISTANCE:
178 if( rStrValue.indexOf( '%' ) != -1 ) // it's a percentage
180 bIsRel = true;
181 ::sax::Converter::convertPercent(aLineDash.Distance, rStrValue);
183 else
185 rUnitConverter.convertMeasureToCore( aLineDash.Distance,
186 rStrValue );
189 break;
190 default:
191 SAL_INFO("xmloff.style", "Unknown token at import dash style");
195 if( bIsRel )
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;
208 // Export
210 XMLDashStyleExport::XMLDashStyleExport( SvXMLExport& rExp )
211 : rExport(rExp)
215 XMLDashStyleExport::~XMLDashStyleExport()
219 bool XMLDashStyleExport::exportXML(
220 const OUString& rStrName,
221 const uno::Any& rValue )
223 bool bRet = false;
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;
235 OUString aStrValue;
236 OUStringBuffer aOut;
238 // Name
239 bool bEncoded = false;
240 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
241 rExport.EncodeStyleName( rStrName,
242 &bEncoded ) );
243 if( bEncoded )
244 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
245 rStrName );
247 // Style
248 SvXMLUnitConverter::convertEnum( aOut, aLineDash.Style, pXML_DashStyle_Enum );
249 aStrValue = aOut.makeStringAndClear();
250 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
252 // dots
253 if( aLineDash.Dots )
255 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1, OUString::number( aLineDash.Dots ) );
257 if( aLineDash.DotLen )
259 // dashes length
260 if( bIsRel )
262 ::sax::Converter::convertPercent(aOut, aLineDash.DotLen);
264 else
266 rUnitConverter.convertMeasureToXML( aOut,
267 aLineDash.DotLen );
269 aStrValue = aOut.makeStringAndClear();
270 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1_LENGTH, aStrValue );
274 // dashes
275 if( aLineDash.Dashes )
277 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2, OUString::number( aLineDash.Dashes ) );
279 if( aLineDash.DashLen )
281 // dashes length
282 if( bIsRel )
284 ::sax::Converter::convertPercent(aOut, aLineDash.DashLen);
286 else
288 rUnitConverter.convertMeasureToXML( aOut,
289 aLineDash.DashLen );
291 aStrValue = aOut.makeStringAndClear();
292 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2_LENGTH, aStrValue );
296 // distance
297 if( bIsRel )
299 ::sax::Converter::convertPercent( aOut, aLineDash.Distance );
301 else
303 rUnitConverter.convertMeasureToXML( aOut,
304 aLineDash.Distance );
306 aStrValue = aOut.makeStringAndClear();
307 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISTANCE, aStrValue );
309 // do Write
310 SvXMLElementExport rElem( rExport,
311 XML_NAMESPACE_DRAW, XML_STROKE_DASH,
312 true, false );
315 return bRet;
318 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */