fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / xmloff / source / style / DashStyle.cxx
blob20ae5d287852190575af29346f44fefb7b185542
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 .
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
45 XML_TOK_DASH_NAME,
46 XML_TOK_DASH_DISPLAY_NAME,
47 XML_TOK_DASH_STYLE,
48 XML_TOK_DASH_DOTS1,
49 XML_TOK_DASH_DOTS1LEN,
50 XML_TOK_DASH_DOTS2,
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 },
66 XML_TOKEN_MAP_END
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 //-------------------------------------------------------------
79 // Import
80 //-------------------------------------------------------------
82 XMLDashStyleImport::XMLDashStyleImport( SvXMLImport& rImp )
83 : rImport(rImp)
87 XMLDashStyleImport::~XMLDashStyleImport()
91 sal_Bool XMLDashStyleImport::importXML(
92 const uno::Reference< xml::sax::XAttributeList >& xAttrList,
93 uno::Any& rValue,
94 OUString& rStrName )
96 drawing::LineDash aLineDash;
97 aLineDash.Style = drawing::DashStyle_RECT;
98 aLineDash.Dots = 0;
99 aLineDash.DotLen = 0;
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;
126 break;
127 case XML_TOK_DASH_DISPLAY_NAME:
129 aDisplayName = rStrValue;
131 break;
132 case XML_TOK_DASH_STYLE:
134 sal_uInt16 eValue;
135 if( rUnitConverter.convertEnum( eValue, rStrValue, pXML_DashStyle_Enum ) )
137 aLineDash.Style = (drawing::DashStyle) eValue;
140 break;
141 case XML_TOK_DASH_DOTS1:
142 aLineDash.Dots = (sal_Int16)rStrValue.toInt32();
143 break;
145 case XML_TOK_DASH_DOTS1LEN:
147 if( rStrValue.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
149 bIsRel = sal_True;
150 ::sax::Converter::convertPercent(aLineDash.DotLen, rStrValue);
152 else
154 rUnitConverter.convertMeasureToCore( aLineDash.DotLen,
155 rStrValue );
158 break;
160 case XML_TOK_DASH_DOTS2:
161 aLineDash.Dashes = (sal_Int16)rStrValue.toInt32();
162 break;
164 case XML_TOK_DASH_DOTS2LEN:
166 if( rStrValue.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
168 bIsRel = sal_True;
169 ::sax::Converter::convertPercent(aLineDash.DashLen, rStrValue);
171 else
173 rUnitConverter.convertMeasureToCore( aLineDash.DashLen,
174 rStrValue );
177 break;
179 case XML_TOK_DASH_DISTANCE:
181 if( rStrValue.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
183 bIsRel = sal_True;
184 ::sax::Converter::convertPercent(aLineDash.Distance, rStrValue);
186 else
188 rUnitConverter.convertMeasureToCore( aLineDash.Distance,
189 rStrValue );
192 break;
193 default:
194 DBG_WARNING( "Unknown token at import gradient style" );
198 if( bIsRel )
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;
210 return sal_True;
214 //-------------------------------------------------------------
215 // Export
216 //-------------------------------------------------------------
218 XMLDashStyleExport::XMLDashStyleExport( SvXMLExport& rExp )
219 : rExport(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;
243 OUString aStrValue;
244 OUStringBuffer aOut;
246 // Name
247 sal_Bool bEncoded = sal_False;
248 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
249 rExport.EncodeStyleName( rStrName,
250 &bEncoded ) );
251 if( bEncoded )
252 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
253 rStrName );
255 // Style
256 rUnitConverter.convertEnum( aOut, aLineDash.Style, pXML_DashStyle_Enum );
257 aStrValue = aOut.makeStringAndClear();
258 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
261 // dots
262 if( aLineDash.Dots )
264 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1, OUString::valueOf( (sal_Int32)aLineDash.Dots ) );
266 if( aLineDash.DotLen )
268 // dashes length
269 if( bIsRel )
271 ::sax::Converter::convertPercent(aOut, aLineDash.DotLen);
273 else
275 rUnitConverter.convertMeasureToXML( aOut,
276 aLineDash.DotLen );
278 aStrValue = aOut.makeStringAndClear();
279 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1_LENGTH, aStrValue );
283 // dashes
284 if( aLineDash.Dashes )
286 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2, OUString::valueOf( (sal_Int32)aLineDash.Dashes ) );
288 if( aLineDash.DashLen )
290 // dashes length
291 if( bIsRel )
293 ::sax::Converter::convertPercent(aOut, aLineDash.DashLen);
295 else
297 rUnitConverter.convertMeasureToXML( aOut,
298 aLineDash.DashLen );
300 aStrValue = aOut.makeStringAndClear();
301 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2_LENGTH, aStrValue );
305 // distance
306 if( bIsRel )
308 ::sax::Converter::convertPercent( aOut, aLineDash.Distance );
310 else
312 rUnitConverter.convertMeasureToXML( aOut,
313 aLineDash.Distance );
315 aStrValue = aOut.makeStringAndClear();
316 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISTANCE, aStrValue );
319 // do Write
320 SvXMLElementExport rElem( rExport,
321 XML_NAMESPACE_DRAW, XML_STROKE_DASH,
322 sal_True, sal_False );
325 return bRet;
328 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */