Update ooo320-m1
[ooovba.git] / xmloff / source / style / DashStyle.cxx
blob072f6c3f4ef07a5ef4ea5b4fe6e6767e2ad3c9f8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DashStyle.cxx,v $
10 * $Revision: 1.17 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
33 #include <com/sun/star/drawing/DashStyle.hpp>
34 #include <com/sun/star/drawing/LineDash.hpp>
35 #include "DashStyle.hxx"
36 #include <xmloff/attrlist.hxx>
37 #include <xmloff/nmspmap.hxx>
38 #include <xmloff/xmluconv.hxx>
39 #include "xmlnmspe.hxx"
40 #include <xmloff/xmltoken.hxx>
41 #include <xmloff/xmlexp.hxx>
42 #include <xmloff/xmlimp.hxx>
43 #include <rtl/ustrbuf.hxx>
44 #include <rtl/ustring.hxx>
45 #include <tools/debug.hxx>
46 #include <xmloff/xmltkmap.hxx>
48 using namespace ::com::sun::star;
49 using ::rtl::OUString;
50 using ::rtl::OUStringBuffer;
52 using namespace ::xmloff::token;
54 enum SvXMLTokenMapAttrs
56 XML_TOK_DASH_NAME,
57 XML_TOK_DASH_DISPLAY_NAME,
58 XML_TOK_DASH_STYLE,
59 XML_TOK_DASH_DOTS1,
60 XML_TOK_DASH_DOTS1LEN,
61 XML_TOK_DASH_DOTS2,
62 XML_TOK_DASH_DOTS2LEN,
63 XML_TOK_DASH_DISTANCE,
64 XML_TOK_DASH_END=XML_TOK_UNKNOWN
67 static __FAR_DATA SvXMLTokenMapEntry aDashStyleAttrTokenMap[] =
69 { XML_NAMESPACE_DRAW, XML_NAME, XML_TOK_DASH_NAME },
70 { XML_NAMESPACE_DRAW, XML_DISPLAY_NAME, XML_TOK_DASH_DISPLAY_NAME },
71 { XML_NAMESPACE_DRAW, XML_STYLE, XML_TOK_DASH_STYLE },
72 { XML_NAMESPACE_DRAW, XML_DOTS1, XML_TOK_DASH_DOTS1 },
73 { XML_NAMESPACE_DRAW, XML_DOTS1_LENGTH, XML_TOK_DASH_DOTS1LEN },
74 { XML_NAMESPACE_DRAW, XML_DOTS2, XML_TOK_DASH_DOTS2 },
75 { XML_NAMESPACE_DRAW, XML_DOTS2_LENGTH, XML_TOK_DASH_DOTS2LEN },
76 { XML_NAMESPACE_DRAW, XML_DISTANCE, XML_TOK_DASH_DISTANCE },
77 XML_TOKEN_MAP_END
80 SvXMLEnumMapEntry __READONLY_DATA pXML_DashStyle_Enum[] =
82 { XML_RECT, drawing::DashStyle_RECT },
83 { XML_ROUND, drawing::DashStyle_ROUND },
84 { XML_RECT, drawing::DashStyle_RECTRELATIVE },
85 { XML_ROUND, drawing::DashStyle_ROUNDRELATIVE },
86 { XML_TOKEN_INVALID, 0 }
89 //-------------------------------------------------------------
90 // Import
91 //-------------------------------------------------------------
93 XMLDashStyleImport::XMLDashStyleImport( SvXMLImport& rImp )
94 : rImport(rImp)
98 XMLDashStyleImport::~XMLDashStyleImport()
102 sal_Bool XMLDashStyleImport::importXML(
103 const uno::Reference< xml::sax::XAttributeList >& xAttrList,
104 uno::Any& rValue,
105 OUString& rStrName )
107 drawing::LineDash aLineDash;
108 aLineDash.Style = drawing::DashStyle_RECT;
109 aLineDash.Dots = 0;
110 aLineDash.DotLen = 0;
111 aLineDash.Dashes = 0;
112 aLineDash.DashLen = 0;
113 aLineDash.Distance = 20;
114 OUString aDisplayName;
116 sal_Bool bIsRel = sal_False;
118 SvXMLNamespaceMap& rNamespaceMap = rImport.GetNamespaceMap();
119 SvXMLUnitConverter& rUnitConverter = rImport.GetMM100UnitConverter();
121 SvXMLTokenMap aTokenMap( aDashStyleAttrTokenMap );
123 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
124 for( sal_Int16 i=0; i < nAttrCount; i++ )
126 const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
127 OUString aStrAttrName;
128 sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName( rFullAttrName, &aStrAttrName );
129 const OUString& rStrValue = xAttrList->getValueByIndex( i );
131 switch( aTokenMap.Get( nPrefix, aStrAttrName ) )
133 case XML_TOK_DASH_NAME:
135 rStrName = rStrValue;
137 break;
138 case XML_TOK_DASH_DISPLAY_NAME:
140 aDisplayName = rStrValue;
142 break;
143 case XML_TOK_DASH_STYLE:
145 sal_uInt16 eValue;
146 if( rUnitConverter.convertEnum( eValue, rStrValue, pXML_DashStyle_Enum ) )
148 aLineDash.Style = (drawing::DashStyle) eValue;
151 break;
152 case XML_TOK_DASH_DOTS1:
153 aLineDash.Dots = (sal_Int16)rStrValue.toInt32();
154 break;
156 case XML_TOK_DASH_DOTS1LEN:
158 if( rStrValue.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
160 bIsRel = sal_True;
161 rUnitConverter.convertPercent( aLineDash.DotLen, rStrValue );
163 else
165 rUnitConverter.convertMeasure( aLineDash.DotLen, rStrValue );
168 break;
170 case XML_TOK_DASH_DOTS2:
171 aLineDash.Dashes = (sal_Int16)rStrValue.toInt32();
172 break;
174 case XML_TOK_DASH_DOTS2LEN:
176 if( rStrValue.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
178 bIsRel = sal_True;
179 rUnitConverter.convertPercent( aLineDash.DashLen, rStrValue );
181 else
183 rUnitConverter.convertMeasure( aLineDash.DashLen, rStrValue );
186 break;
188 case XML_TOK_DASH_DISTANCE:
190 if( rStrValue.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
192 bIsRel = sal_True;
193 rUnitConverter.convertPercent( aLineDash.Distance, rStrValue );
195 else
197 rUnitConverter.convertMeasure( aLineDash.Distance, rStrValue );
200 break;
201 default:
202 DBG_WARNING( "Unknown token at import gradient style" );
206 if( bIsRel )
207 aLineDash.Style = aLineDash.Style == drawing::DashStyle_RECT ? drawing::DashStyle_RECTRELATIVE : drawing::DashStyle_ROUNDRELATIVE;
209 rValue <<= aLineDash;
211 if( aDisplayName.getLength() )
213 rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_STROKE_DASH_ID,
214 rStrName, aDisplayName );
215 rStrName = aDisplayName;
218 return sal_True;
222 //-------------------------------------------------------------
223 // Export
224 //-------------------------------------------------------------
226 #ifndef SVX_LIGHT
228 XMLDashStyleExport::XMLDashStyleExport( SvXMLExport& rExp )
229 : rExport(rExp)
233 XMLDashStyleExport::~XMLDashStyleExport()
237 sal_Bool XMLDashStyleExport::exportXML(
238 const OUString& rStrName,
239 const uno::Any& rValue )
241 sal_Bool bRet = sal_False;
243 SvXMLUnitConverter rUnitConverter = rExport.GetMM100UnitConverter();
245 drawing::LineDash aLineDash;
247 if( rStrName.getLength() )
249 if( rValue >>= aLineDash )
251 sal_Bool bIsRel = aLineDash.Style == drawing::DashStyle_RECTRELATIVE || aLineDash.Style == drawing::DashStyle_ROUNDRELATIVE;
253 OUString aStrValue;
254 OUStringBuffer aOut;
256 // Name
257 sal_Bool bEncoded = sal_False;
258 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
259 rExport.EncodeStyleName( rStrName,
260 &bEncoded ) );
261 if( bEncoded )
262 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
263 rStrName );
265 // Style
266 rUnitConverter.convertEnum( aOut, aLineDash.Style, pXML_DashStyle_Enum );
267 aStrValue = aOut.makeStringAndClear();
268 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
271 // dots
272 if( aLineDash.Dots )
274 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1, OUString::valueOf( (sal_Int32)aLineDash.Dots ) );
276 if( aLineDash.DotLen )
278 // dashes length
279 if( bIsRel )
281 rUnitConverter.convertPercent( aOut, aLineDash.DotLen );
283 else
285 rUnitConverter.convertMeasure( aOut, aLineDash.DotLen );
287 aStrValue = aOut.makeStringAndClear();
288 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1_LENGTH, aStrValue );
292 // dashes
293 if( aLineDash.Dashes )
295 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2, OUString::valueOf( (sal_Int32)aLineDash.Dashes ) );
297 if( aLineDash.DashLen )
299 // dashes length
300 if( bIsRel )
302 rUnitConverter.convertPercent( aOut, aLineDash.DashLen );
304 else
306 rUnitConverter.convertMeasure( aOut, aLineDash.DashLen );
308 aStrValue = aOut.makeStringAndClear();
309 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2_LENGTH, aStrValue );
313 // distance
314 if( bIsRel )
316 rUnitConverter.convertPercent( aOut, aLineDash.Distance );
318 else
320 rUnitConverter.convertMeasure( aOut, aLineDash.Distance );
322 aStrValue = aOut.makeStringAndClear();
323 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISTANCE, aStrValue );
326 // do Write
327 SvXMLElementExport rElem( rExport,
328 XML_NAMESPACE_DRAW, XML_STROKE_DASH,
329 sal_True, sal_False );
332 return bRet;
335 #endif // #ifndef SVX_LIGHT