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