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