xref: /trunk/main/xmloff/source/style/DashStyle.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 #include <com/sun/star/drawing/DashStyle.hpp>
31 #include <com/sun/star/drawing/LineDash.hpp>
32 #include "xmloff/DashStyle.hxx"
33 #include <xmloff/attrlist.hxx>
34 #include <xmloff/nmspmap.hxx>
35 #include <xmloff/xmluconv.hxx>
36 #include "xmloff/xmlnmspe.hxx"
37 #include <xmloff/xmltoken.hxx>
38 #include <xmloff/xmlexp.hxx>
39 #include <xmloff/xmlimp.hxx>
40 #include <rtl/ustrbuf.hxx>
41 #include <rtl/ustring.hxx>
42 #include <tools/debug.hxx>
43 #include <xmloff/xmltkmap.hxx>
44 
45 using namespace ::com::sun::star;
46 using ::rtl::OUString;
47 using ::rtl::OUStringBuffer;
48 
49 using namespace ::xmloff::token;
50 
51 enum SvXMLTokenMapAttrs
52 {
53     XML_TOK_DASH_NAME,
54     XML_TOK_DASH_DISPLAY_NAME,
55     XML_TOK_DASH_STYLE,
56     XML_TOK_DASH_DOTS1,
57     XML_TOK_DASH_DOTS1LEN,
58     XML_TOK_DASH_DOTS2,
59     XML_TOK_DASH_DOTS2LEN,
60     XML_TOK_DASH_DISTANCE,
61     XML_TOK_DASH_END=XML_TOK_UNKNOWN
62 };
63 
64 static __FAR_DATA SvXMLTokenMapEntry aDashStyleAttrTokenMap[] =
65 {
66     { XML_NAMESPACE_DRAW, XML_NAME,             XML_TOK_DASH_NAME },
67     { XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,     XML_TOK_DASH_DISPLAY_NAME },
68     { XML_NAMESPACE_DRAW, XML_STYLE,            XML_TOK_DASH_STYLE },
69     { XML_NAMESPACE_DRAW, XML_DOTS1,            XML_TOK_DASH_DOTS1 },
70     { XML_NAMESPACE_DRAW, XML_DOTS1_LENGTH,     XML_TOK_DASH_DOTS1LEN },
71     { XML_NAMESPACE_DRAW, XML_DOTS2,            XML_TOK_DASH_DOTS2 },
72     { XML_NAMESPACE_DRAW, XML_DOTS2_LENGTH,     XML_TOK_DASH_DOTS2LEN },
73     { XML_NAMESPACE_DRAW, XML_DISTANCE,         XML_TOK_DASH_DISTANCE },
74     XML_TOKEN_MAP_END
75 };
76 
77 SvXMLEnumMapEntry __READONLY_DATA pXML_DashStyle_Enum[] =
78 {
79     { XML_RECT,         drawing::DashStyle_RECT },
80     { XML_ROUND,        drawing::DashStyle_ROUND },
81     { XML_RECT,         drawing::DashStyle_RECTRELATIVE },
82     { XML_ROUND,        drawing::DashStyle_ROUNDRELATIVE },
83     { XML_TOKEN_INVALID, 0 }
84 };
85 
86 //-------------------------------------------------------------
87 // Import
88 //-------------------------------------------------------------
89 
90 XMLDashStyleImport::XMLDashStyleImport( SvXMLImport& rImp )
91     : rImport(rImp)
92 {
93 }
94 
95 XMLDashStyleImport::~XMLDashStyleImport()
96 {
97 }
98 
99 sal_Bool XMLDashStyleImport::importXML(
100     const uno::Reference< xml::sax::XAttributeList >& xAttrList,
101     uno::Any& rValue,
102     OUString& rStrName )
103 {
104     drawing::LineDash aLineDash;
105     aLineDash.Style = drawing::DashStyle_RECT;
106     aLineDash.Dots = 0;
107     aLineDash.DotLen = 0;
108     aLineDash.Dashes = 0;
109     aLineDash.DashLen = 0;
110     aLineDash.Distance = 20;
111     OUString aDisplayName;
112 
113     sal_Bool bIsRel = sal_False;
114 
115     SvXMLNamespaceMap& rNamespaceMap = rImport.GetNamespaceMap();
116     SvXMLUnitConverter& rUnitConverter = rImport.GetMM100UnitConverter();
117 
118     SvXMLTokenMap aTokenMap( aDashStyleAttrTokenMap );
119 
120     sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
121     for( sal_Int16 i=0; i < nAttrCount; i++ )
122     {
123         const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
124         OUString aStrAttrName;
125         sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName( rFullAttrName, &aStrAttrName );
126         const OUString& rStrValue = xAttrList->getValueByIndex( i );
127 
128         switch( aTokenMap.Get( nPrefix, aStrAttrName ) )
129         {
130         case XML_TOK_DASH_NAME:
131             {
132                 rStrName = rStrValue;
133             }
134             break;
135         case XML_TOK_DASH_DISPLAY_NAME:
136             {
137                 aDisplayName = rStrValue;
138             }
139             break;
140         case XML_TOK_DASH_STYLE:
141             {
142                 sal_uInt16 eValue;
143                 if( rUnitConverter.convertEnum( eValue, rStrValue, pXML_DashStyle_Enum ) )
144                 {
145                     aLineDash.Style = (drawing::DashStyle) eValue;
146                 }
147             }
148             break;
149         case XML_TOK_DASH_DOTS1:
150             aLineDash.Dots = (sal_Int16)rStrValue.toInt32();
151             break;
152 
153         case XML_TOK_DASH_DOTS1LEN:
154             {
155                 if( rStrValue.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
156                 {
157                     bIsRel = sal_True;
158                     rUnitConverter.convertPercent( aLineDash.DotLen, rStrValue );
159                 }
160                 else
161                 {
162                     rUnitConverter.convertMeasure( aLineDash.DotLen, rStrValue );
163                 }
164             }
165             break;
166 
167         case XML_TOK_DASH_DOTS2:
168             aLineDash.Dashes = (sal_Int16)rStrValue.toInt32();
169             break;
170 
171         case XML_TOK_DASH_DOTS2LEN:
172             {
173                 if( rStrValue.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
174                 {
175                     bIsRel = sal_True;
176                     rUnitConverter.convertPercent( aLineDash.DashLen, rStrValue );
177                 }
178                 else
179                 {
180                     rUnitConverter.convertMeasure( aLineDash.DashLen, rStrValue );
181                 }
182             }
183             break;
184 
185         case XML_TOK_DASH_DISTANCE:
186             {
187                 if( rStrValue.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
188                 {
189                     bIsRel = sal_True;
190                     rUnitConverter.convertPercent( aLineDash.Distance, rStrValue );
191                 }
192                 else
193                 {
194                     rUnitConverter.convertMeasure( aLineDash.Distance, rStrValue );
195                 }
196             }
197             break;
198         default:
199             DBG_WARNING( "Unknown token at import gradient style" );
200         }
201     }
202 
203     if( bIsRel )
204         aLineDash.Style = aLineDash.Style == drawing::DashStyle_RECT ? drawing::DashStyle_RECTRELATIVE : drawing::DashStyle_ROUNDRELATIVE;
205 
206     rValue <<= aLineDash;
207 
208     if( aDisplayName.getLength() )
209     {
210         rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_STROKE_DASH_ID,
211                                      rStrName, aDisplayName );
212         rStrName = aDisplayName;
213     }
214 
215     return sal_True;
216 }
217 
218 
219 //-------------------------------------------------------------
220 // Export
221 //-------------------------------------------------------------
222 
223 #ifndef SVX_LIGHT
224 
225 XMLDashStyleExport::XMLDashStyleExport( SvXMLExport& rExp )
226     : rExport(rExp)
227 {
228 }
229 
230 XMLDashStyleExport::~XMLDashStyleExport()
231 {
232 }
233 
234 sal_Bool XMLDashStyleExport::exportXML(
235     const OUString& rStrName,
236     const uno::Any& rValue )
237 {
238     sal_Bool bRet = sal_False;
239 
240     SvXMLUnitConverter rUnitConverter = rExport.GetMM100UnitConverter();
241 
242     drawing::LineDash aLineDash;
243 
244     if( rStrName.getLength() )
245     {
246         if( rValue >>= aLineDash )
247         {
248             sal_Bool bIsRel = aLineDash.Style == drawing::DashStyle_RECTRELATIVE || aLineDash.Style == drawing::DashStyle_ROUNDRELATIVE;
249 
250             OUString aStrValue;
251             OUStringBuffer aOut;
252 
253             // Name
254             sal_Bool bEncoded = sal_False;
255             rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
256                                   rExport.EncodeStyleName( rStrName,
257                                                            &bEncoded ) );
258             if( bEncoded )
259                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
260                                       rStrName );
261 
262             // Style
263             rUnitConverter.convertEnum( aOut, aLineDash.Style, pXML_DashStyle_Enum );
264             aStrValue = aOut.makeStringAndClear();
265             rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
266 
267 
268             // dots
269             if( aLineDash.Dots )
270             {
271                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1, OUString::valueOf( (sal_Int32)aLineDash.Dots ) );
272 
273                 if( aLineDash.DotLen )
274                 {
275                     // dashes length
276                     if( bIsRel )
277                     {
278                         rUnitConverter.convertPercent( aOut, aLineDash.DotLen );
279                     }
280                     else
281                     {
282                         rUnitConverter.convertMeasure( aOut, aLineDash.DotLen );
283                     }
284                     aStrValue = aOut.makeStringAndClear();
285                     rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1_LENGTH, aStrValue );
286                 }
287             }
288 
289             // dashes
290             if( aLineDash.Dashes )
291             {
292                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2, OUString::valueOf( (sal_Int32)aLineDash.Dashes ) );
293 
294                 if( aLineDash.DashLen )
295                 {
296                     // dashes length
297                     if( bIsRel )
298                     {
299                         rUnitConverter.convertPercent( aOut, aLineDash.DashLen );
300                     }
301                     else
302                     {
303                         rUnitConverter.convertMeasure( aOut, aLineDash.DashLen );
304                     }
305                     aStrValue = aOut.makeStringAndClear();
306                     rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2_LENGTH, aStrValue );
307                 }
308             }
309 
310             // distance
311             if( bIsRel )
312             {
313                 rUnitConverter.convertPercent( aOut, aLineDash.Distance );
314             }
315             else
316             {
317                 rUnitConverter.convertMeasure( aOut, aLineDash.Distance );
318             }
319             aStrValue = aOut.makeStringAndClear();
320             rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISTANCE, aStrValue );
321 
322 
323             // do Write
324             SvXMLElementExport rElem( rExport,
325                                       XML_NAMESPACE_DRAW, XML_STROKE_DASH,
326                                       sal_True, sal_False );
327         }
328     }
329     return bRet;
330 }
331 
332 #endif // #ifndef SVX_LIGHT
333