xref: /AOO41X/main/xmloff/source/style/TransGradientStyle.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 "TransGradientStyle.hxx"
31*cdf0e10cSrcweir #include <com/sun/star/awt/Gradient.hpp>
32*cdf0e10cSrcweir #include <xmloff/attrlist.hxx>
33*cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
34*cdf0e10cSrcweir #include <xmloff/xmluconv.hxx>
35*cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
36*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
37*cdf0e10cSrcweir #include <rtl/ustring.hxx>
38*cdf0e10cSrcweir #include <tools/debug.hxx>
39*cdf0e10cSrcweir #include <xmloff/xmltkmap.hxx>
40*cdf0e10cSrcweir #include <xmloff/xmlexp.hxx>
41*cdf0e10cSrcweir #include <xmloff/xmlimp.hxx>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir using namespace ::com::sun::star;
45*cdf0e10cSrcweir using ::rtl::OUString;
46*cdf0e10cSrcweir using ::rtl::OUStringBuffer;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir using namespace ::xmloff::token;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir enum SvXMLTokenMapAttrs
51*cdf0e10cSrcweir {
52*cdf0e10cSrcweir 	XML_TOK_GRADIENT_NAME,
53*cdf0e10cSrcweir 	XML_TOK_GRADIENT_DISPLAY_NAME,
54*cdf0e10cSrcweir 	XML_TOK_GRADIENT_STYLE,
55*cdf0e10cSrcweir 	XML_TOK_GRADIENT_CX,
56*cdf0e10cSrcweir 	XML_TOK_GRADIENT_CY,
57*cdf0e10cSrcweir 	XML_TOK_GRADIENT_START,
58*cdf0e10cSrcweir 	XML_TOK_GRADIENT_END,
59*cdf0e10cSrcweir 	XML_TOK_GRADIENT_ANGLE,
60*cdf0e10cSrcweir 	XML_TOK_GRADIENT_BORDER,
61*cdf0e10cSrcweir 	XML_TOK_TABSTOP_END=XML_TOK_UNKNOWN
62*cdf0e10cSrcweir };
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir SvXMLEnumMapEntry __READONLY_DATA pXML_GradientStyle_Enum[] =
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir 	{ XML_GRADIENTSTYLE_LINEAR,		    awt::GradientStyle_LINEAR },
68*cdf0e10cSrcweir 	{ XML_GRADIENTSTYLE_AXIAL,			awt::GradientStyle_AXIAL },
69*cdf0e10cSrcweir 	{ XML_GRADIENTSTYLE_RADIAL,		    awt::GradientStyle_RADIAL },
70*cdf0e10cSrcweir 	{ XML_GRADIENTSTYLE_ELLIPSOID,		awt::GradientStyle_ELLIPTICAL },
71*cdf0e10cSrcweir 	{ XML_GRADIENTSTYLE_SQUARE,		    awt::GradientStyle_SQUARE },
72*cdf0e10cSrcweir 	{ XML_GRADIENTSTYLE_RECTANGULAR,	awt::GradientStyle_RECT },
73*cdf0e10cSrcweir 	{ XML_TOKEN_INVALID,                0 }
74*cdf0e10cSrcweir };
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir //-------------------------------------------------------------
78*cdf0e10cSrcweir // Import
79*cdf0e10cSrcweir //-------------------------------------------------------------
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir XMLTransGradientStyleImport::XMLTransGradientStyleImport( SvXMLImport& rImp )
82*cdf0e10cSrcweir     : rImport(rImp)
83*cdf0e10cSrcweir {
84*cdf0e10cSrcweir }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir XMLTransGradientStyleImport::~XMLTransGradientStyleImport()
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir sal_Bool XMLTransGradientStyleImport::importXML(
91*cdf0e10cSrcweir     const uno::Reference< xml::sax::XAttributeList >& xAttrList,
92*cdf0e10cSrcweir     uno::Any& rValue,
93*cdf0e10cSrcweir     OUString& rStrName )
94*cdf0e10cSrcweir {
95*cdf0e10cSrcweir 	sal_Bool bRet           = sal_False;
96*cdf0e10cSrcweir 	sal_Bool bHasName       = sal_False;
97*cdf0e10cSrcweir 	sal_Bool bHasStyle      = sal_False;
98*cdf0e10cSrcweir 	OUString aDisplayName;
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 	awt::Gradient aGradient;
101*cdf0e10cSrcweir 	aGradient.XOffset = 0;
102*cdf0e10cSrcweir 	aGradient.YOffset = 0;
103*cdf0e10cSrcweir 	aGradient.StartIntensity = 100;
104*cdf0e10cSrcweir 	aGradient.EndIntensity = 100;
105*cdf0e10cSrcweir 	aGradient.Angle = 0;
106*cdf0e10cSrcweir 	aGradient.Border = 0;
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir     {
109*cdf0e10cSrcweir         static __FAR_DATA SvXMLTokenMapEntry aTrGradientAttrTokenMap[] =
110*cdf0e10cSrcweir {
111*cdf0e10cSrcweir 	{ XML_NAMESPACE_DRAW, XML_NAME, XML_TOK_GRADIENT_NAME },
112*cdf0e10cSrcweir 	{ XML_NAMESPACE_DRAW, XML_DISPLAY_NAME, XML_TOK_GRADIENT_DISPLAY_NAME },
113*cdf0e10cSrcweir 	{ XML_NAMESPACE_DRAW, XML_STYLE, XML_TOK_GRADIENT_STYLE },
114*cdf0e10cSrcweir 	{ XML_NAMESPACE_DRAW, XML_CX, XML_TOK_GRADIENT_CX },
115*cdf0e10cSrcweir 	{ XML_NAMESPACE_DRAW, XML_CY, XML_TOK_GRADIENT_CY },
116*cdf0e10cSrcweir 	{ XML_NAMESPACE_DRAW, XML_START, XML_TOK_GRADIENT_START },
117*cdf0e10cSrcweir 	{ XML_NAMESPACE_DRAW, XML_END, XML_TOK_GRADIENT_END },
118*cdf0e10cSrcweir 	{ XML_NAMESPACE_DRAW, XML_GRADIENT_ANGLE, XML_TOK_GRADIENT_ANGLE },
119*cdf0e10cSrcweir 	{ XML_NAMESPACE_DRAW, XML_GRADIENT_BORDER, XML_TOK_GRADIENT_BORDER },
120*cdf0e10cSrcweir 	XML_TOKEN_MAP_END
121*cdf0e10cSrcweir };
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 	SvXMLTokenMap aTokenMap( aTrGradientAttrTokenMap );
124*cdf0e10cSrcweir     SvXMLNamespaceMap& rNamespaceMap = rImport.GetNamespaceMap();
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 	sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
127*cdf0e10cSrcweir 	for( sal_Int16 i=0; i < nAttrCount; i++ )
128*cdf0e10cSrcweir 	{
129*cdf0e10cSrcweir 		const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
130*cdf0e10cSrcweir 		OUString aStrAttrName;
131*cdf0e10cSrcweir 		sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName( rFullAttrName, &aStrAttrName );
132*cdf0e10cSrcweir 		const OUString& rStrValue = xAttrList->getValueByIndex( i );
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 		sal_Int32 nTmpValue;
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 		switch( aTokenMap.Get( nPrefix, aStrAttrName ) )
137*cdf0e10cSrcweir 		{
138*cdf0e10cSrcweir 		case XML_TOK_GRADIENT_NAME:
139*cdf0e10cSrcweir 			{
140*cdf0e10cSrcweir 				rStrName = rStrValue;
141*cdf0e10cSrcweir 				bHasName = sal_True;
142*cdf0e10cSrcweir 			}
143*cdf0e10cSrcweir 			break;
144*cdf0e10cSrcweir 		case XML_TOK_GRADIENT_DISPLAY_NAME:
145*cdf0e10cSrcweir 			{
146*cdf0e10cSrcweir 				aDisplayName = rStrValue;
147*cdf0e10cSrcweir 			}
148*cdf0e10cSrcweir 			break;
149*cdf0e10cSrcweir 		case XML_TOK_GRADIENT_STYLE:
150*cdf0e10cSrcweir 			{
151*cdf0e10cSrcweir 				sal_uInt16 eValue;
152*cdf0e10cSrcweir 				if( SvXMLUnitConverter::convertEnum( eValue, rStrValue, pXML_GradientStyle_Enum ) )
153*cdf0e10cSrcweir 				{
154*cdf0e10cSrcweir 					aGradient.Style = (awt::GradientStyle) eValue;
155*cdf0e10cSrcweir 					bHasStyle = sal_True;
156*cdf0e10cSrcweir 				}
157*cdf0e10cSrcweir 			}
158*cdf0e10cSrcweir 			break;
159*cdf0e10cSrcweir 		case XML_TOK_GRADIENT_CX:
160*cdf0e10cSrcweir 			SvXMLUnitConverter::convertPercent( nTmpValue, rStrValue );
161*cdf0e10cSrcweir 			aGradient.XOffset = sal::static_int_cast< sal_Int16 >(nTmpValue);
162*cdf0e10cSrcweir 			break;
163*cdf0e10cSrcweir 		case XML_TOK_GRADIENT_CY:
164*cdf0e10cSrcweir 			SvXMLUnitConverter::convertPercent( nTmpValue, rStrValue );
165*cdf0e10cSrcweir 			aGradient.YOffset = sal::static_int_cast< sal_Int16 >(nTmpValue);
166*cdf0e10cSrcweir 			break;
167*cdf0e10cSrcweir 		case XML_TOK_GRADIENT_START:
168*cdf0e10cSrcweir 			{
169*cdf0e10cSrcweir 				sal_Int32 aStartTransparency;
170*cdf0e10cSrcweir 				SvXMLUnitConverter::convertPercent( aStartTransparency, rStrValue );
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 				sal_uInt8 n = sal::static_int_cast< sal_uInt8 >(
173*cdf0e10cSrcweir                     ( (100 - aStartTransparency) * 255 ) / 100 );
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 				Color aColor( n, n, n );
176*cdf0e10cSrcweir 				aGradient.StartColor = (sal_Int32)( aColor.GetColor() );
177*cdf0e10cSrcweir 			}
178*cdf0e10cSrcweir 			break;
179*cdf0e10cSrcweir 		case XML_TOK_GRADIENT_END:
180*cdf0e10cSrcweir 			{
181*cdf0e10cSrcweir 				sal_Int32 aEndTransparency;
182*cdf0e10cSrcweir 				SvXMLUnitConverter::convertPercent( aEndTransparency, rStrValue );
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 				sal_uInt8 n = sal::static_int_cast< sal_uInt8 >(
185*cdf0e10cSrcweir                     ( (100 - aEndTransparency) * 255 ) / 100 );
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 				Color aColor( n, n, n );
188*cdf0e10cSrcweir 				aGradient.EndColor = (sal_Int32)( aColor.GetColor() );
189*cdf0e10cSrcweir 			}
190*cdf0e10cSrcweir 			break;
191*cdf0e10cSrcweir 		case XML_TOK_GRADIENT_ANGLE:
192*cdf0e10cSrcweir 			{
193*cdf0e10cSrcweir 				sal_Int32 nValue;
194*cdf0e10cSrcweir 				SvXMLUnitConverter::convertNumber( nValue, rStrValue, 0, 3600 );
195*cdf0e10cSrcweir 				aGradient.Angle = sal_Int16( nValue );
196*cdf0e10cSrcweir 			}
197*cdf0e10cSrcweir 			break;
198*cdf0e10cSrcweir 		case XML_TOK_GRADIENT_BORDER:
199*cdf0e10cSrcweir 			SvXMLUnitConverter::convertPercent( nTmpValue, rStrValue );
200*cdf0e10cSrcweir 			aGradient.Border = sal::static_int_cast< sal_Int16 >(nTmpValue);
201*cdf0e10cSrcweir 			break;
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir 		default:
204*cdf0e10cSrcweir 			DBG_WARNING( "Unknown token at import transparency gradient style" )
205*cdf0e10cSrcweir 			;
206*cdf0e10cSrcweir 		}
207*cdf0e10cSrcweir 	}
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir 	rValue <<= aGradient;
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 	if( aDisplayName.getLength() )
212*cdf0e10cSrcweir 	{
213*cdf0e10cSrcweir 		rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_GRADIENT_ID, rStrName,
214*cdf0e10cSrcweir 									 aDisplayName );
215*cdf0e10cSrcweir 		rStrName = aDisplayName;
216*cdf0e10cSrcweir 	}
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 	bRet = bHasName && bHasStyle;
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir     }
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir 	return bRet;
223*cdf0e10cSrcweir }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir //-------------------------------------------------------------
227*cdf0e10cSrcweir // Export
228*cdf0e10cSrcweir //-------------------------------------------------------------
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir #ifndef SVX_LIGHT
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir XMLTransGradientStyleExport::XMLTransGradientStyleExport( SvXMLExport& rExp )
233*cdf0e10cSrcweir     : rExport(rExp)
234*cdf0e10cSrcweir {
235*cdf0e10cSrcweir }
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir XMLTransGradientStyleExport::~XMLTransGradientStyleExport()
238*cdf0e10cSrcweir {
239*cdf0e10cSrcweir }
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir sal_Bool XMLTransGradientStyleExport::exportXML(
243*cdf0e10cSrcweir     const OUString& rStrName,
244*cdf0e10cSrcweir     const uno::Any& rValue )
245*cdf0e10cSrcweir {
246*cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
247*cdf0e10cSrcweir 	awt::Gradient aGradient;
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir 	if( rStrName.getLength() )
250*cdf0e10cSrcweir 	{
251*cdf0e10cSrcweir 		if( rValue >>= aGradient )
252*cdf0e10cSrcweir 		{
253*cdf0e10cSrcweir 			OUString aStrValue;
254*cdf0e10cSrcweir 			OUStringBuffer aOut;
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir 			// Style
257*cdf0e10cSrcweir 			if( !SvXMLUnitConverter::convertEnum( aOut, aGradient.Style, pXML_GradientStyle_Enum ) )
258*cdf0e10cSrcweir 			{
259*cdf0e10cSrcweir 				bRet = sal_False;
260*cdf0e10cSrcweir 			}
261*cdf0e10cSrcweir 			else
262*cdf0e10cSrcweir 			{
263*cdf0e10cSrcweir 				// Name
264*cdf0e10cSrcweir 				sal_Bool bEncoded = sal_False;
265*cdf0e10cSrcweir 				rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
266*cdf0e10cSrcweir 									  rExport.EncodeStyleName( rStrName,
267*cdf0e10cSrcweir 										 					   &bEncoded ) );
268*cdf0e10cSrcweir 				if( bEncoded )
269*cdf0e10cSrcweir 					rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
270*cdf0e10cSrcweir 									  	  rStrName );
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir 				aStrValue = aOut.makeStringAndClear();
273*cdf0e10cSrcweir 				rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir 				// Center x/y
276*cdf0e10cSrcweir 				if( aGradient.Style != awt::GradientStyle_LINEAR &&
277*cdf0e10cSrcweir 					aGradient.Style != awt::GradientStyle_AXIAL   )
278*cdf0e10cSrcweir 				{
279*cdf0e10cSrcweir 					SvXMLUnitConverter::convertPercent( aOut, aGradient.XOffset );
280*cdf0e10cSrcweir 					aStrValue = aOut.makeStringAndClear();
281*cdf0e10cSrcweir 					rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_CX, aStrValue );
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir 					SvXMLUnitConverter::convertPercent( aOut, aGradient.YOffset );
284*cdf0e10cSrcweir 					aStrValue = aOut.makeStringAndClear();
285*cdf0e10cSrcweir 					rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_CY, aStrValue );
286*cdf0e10cSrcweir 				}
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir 				Color aColor;
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir 				// Transparency start
292*cdf0e10cSrcweir 				aColor.SetColor( aGradient.StartColor );
293*cdf0e10cSrcweir 				sal_Int32 aStartValue = 100 - (sal_Int32)(((aColor.GetRed() + 1) * 100) / 255);
294*cdf0e10cSrcweir 				SvXMLUnitConverter::convertPercent( aOut, aStartValue );
295*cdf0e10cSrcweir 				aStrValue = aOut.makeStringAndClear();
296*cdf0e10cSrcweir 				rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_START, aStrValue );
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir 				// Transparency end
299*cdf0e10cSrcweir 				aColor.SetColor( aGradient.EndColor );
300*cdf0e10cSrcweir 				sal_Int32 aEndValue = 100 - (sal_Int32)(((aColor.GetRed() + 1) * 100) / 255);
301*cdf0e10cSrcweir 				SvXMLUnitConverter::convertPercent( aOut, aEndValue );
302*cdf0e10cSrcweir 				aStrValue = aOut.makeStringAndClear();
303*cdf0e10cSrcweir 				rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_END, aStrValue );
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir 				// Angle
306*cdf0e10cSrcweir 				if( aGradient.Style != awt::GradientStyle_RADIAL )
307*cdf0e10cSrcweir 				{
308*cdf0e10cSrcweir 					SvXMLUnitConverter::convertNumber( aOut, sal_Int32( aGradient.Angle ) );
309*cdf0e10cSrcweir 					aStrValue = aOut.makeStringAndClear();
310*cdf0e10cSrcweir 					rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_GRADIENT_ANGLE, aStrValue );
311*cdf0e10cSrcweir 				}
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir 				// Border
314*cdf0e10cSrcweir 				SvXMLUnitConverter::convertPercent( aOut, aGradient.Border );
315*cdf0e10cSrcweir 				aStrValue = aOut.makeStringAndClear();
316*cdf0e10cSrcweir 				rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_GRADIENT_BORDER, aStrValue );
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir 				// Do Write
319*cdf0e10cSrcweir 				SvXMLElementExport rElem( rExport,
320*cdf0e10cSrcweir 										  XML_NAMESPACE_DRAW, XML_OPACITY,
321*cdf0e10cSrcweir 										  sal_True, sal_False );
322*cdf0e10cSrcweir 			}
323*cdf0e10cSrcweir 		}
324*cdf0e10cSrcweir 	}
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir 	return bRet;
327*cdf0e10cSrcweir }
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir #endif // #ifndef SVX_LIGHT
330