1*63bba73cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*63bba73cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*63bba73cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*63bba73cSAndrew Rist  * distributed with this work for additional information
6*63bba73cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*63bba73cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*63bba73cSAndrew Rist  * "License"); you may not use this file except in compliance
9*63bba73cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*63bba73cSAndrew Rist  *
11*63bba73cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*63bba73cSAndrew Rist  *
13*63bba73cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*63bba73cSAndrew Rist  * software distributed under the License is distributed on an
15*63bba73cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*63bba73cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*63bba73cSAndrew Rist  * specific language governing permissions and limitations
18*63bba73cSAndrew Rist  * under the License.
19*63bba73cSAndrew Rist  *
20*63bba73cSAndrew Rist  *************************************************************/
21*63bba73cSAndrew Rist 
22*63bba73cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
26cdf0e10cSrcweir #include "TransGradientStyle.hxx"
27cdf0e10cSrcweir #include <com/sun/star/awt/Gradient.hpp>
28cdf0e10cSrcweir #include <xmloff/attrlist.hxx>
29cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
30cdf0e10cSrcweir #include <xmloff/xmluconv.hxx>
31cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
32cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
33cdf0e10cSrcweir #include <rtl/ustring.hxx>
34cdf0e10cSrcweir #include <tools/debug.hxx>
35cdf0e10cSrcweir #include <xmloff/xmltkmap.hxx>
36cdf0e10cSrcweir #include <xmloff/xmlexp.hxx>
37cdf0e10cSrcweir #include <xmloff/xmlimp.hxx>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using namespace ::com::sun::star;
41cdf0e10cSrcweir using ::rtl::OUString;
42cdf0e10cSrcweir using ::rtl::OUStringBuffer;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir using namespace ::xmloff::token;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir enum SvXMLTokenMapAttrs
47cdf0e10cSrcweir {
48cdf0e10cSrcweir 	XML_TOK_GRADIENT_NAME,
49cdf0e10cSrcweir 	XML_TOK_GRADIENT_DISPLAY_NAME,
50cdf0e10cSrcweir 	XML_TOK_GRADIENT_STYLE,
51cdf0e10cSrcweir 	XML_TOK_GRADIENT_CX,
52cdf0e10cSrcweir 	XML_TOK_GRADIENT_CY,
53cdf0e10cSrcweir 	XML_TOK_GRADIENT_START,
54cdf0e10cSrcweir 	XML_TOK_GRADIENT_END,
55cdf0e10cSrcweir 	XML_TOK_GRADIENT_ANGLE,
56cdf0e10cSrcweir 	XML_TOK_GRADIENT_BORDER,
57cdf0e10cSrcweir 	XML_TOK_TABSTOP_END=XML_TOK_UNKNOWN
58cdf0e10cSrcweir };
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 
61cdf0e10cSrcweir SvXMLEnumMapEntry __READONLY_DATA pXML_GradientStyle_Enum[] =
62cdf0e10cSrcweir {
63cdf0e10cSrcweir 	{ XML_GRADIENTSTYLE_LINEAR,		    awt::GradientStyle_LINEAR },
64cdf0e10cSrcweir 	{ XML_GRADIENTSTYLE_AXIAL,			awt::GradientStyle_AXIAL },
65cdf0e10cSrcweir 	{ XML_GRADIENTSTYLE_RADIAL,		    awt::GradientStyle_RADIAL },
66cdf0e10cSrcweir 	{ XML_GRADIENTSTYLE_ELLIPSOID,		awt::GradientStyle_ELLIPTICAL },
67cdf0e10cSrcweir 	{ XML_GRADIENTSTYLE_SQUARE,		    awt::GradientStyle_SQUARE },
68cdf0e10cSrcweir 	{ XML_GRADIENTSTYLE_RECTANGULAR,	awt::GradientStyle_RECT },
69cdf0e10cSrcweir 	{ XML_TOKEN_INVALID,                0 }
70cdf0e10cSrcweir };
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 
73cdf0e10cSrcweir //-------------------------------------------------------------
74cdf0e10cSrcweir // Import
75cdf0e10cSrcweir //-------------------------------------------------------------
76cdf0e10cSrcweir 
XMLTransGradientStyleImport(SvXMLImport & rImp)77cdf0e10cSrcweir XMLTransGradientStyleImport::XMLTransGradientStyleImport( SvXMLImport& rImp )
78cdf0e10cSrcweir     : rImport(rImp)
79cdf0e10cSrcweir {
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
~XMLTransGradientStyleImport()82cdf0e10cSrcweir XMLTransGradientStyleImport::~XMLTransGradientStyleImport()
83cdf0e10cSrcweir {
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
importXML(const uno::Reference<xml::sax::XAttributeList> & xAttrList,uno::Any & rValue,OUString & rStrName)86cdf0e10cSrcweir sal_Bool XMLTransGradientStyleImport::importXML(
87cdf0e10cSrcweir     const uno::Reference< xml::sax::XAttributeList >& xAttrList,
88cdf0e10cSrcweir     uno::Any& rValue,
89cdf0e10cSrcweir     OUString& rStrName )
90cdf0e10cSrcweir {
91cdf0e10cSrcweir 	sal_Bool bRet           = sal_False;
92cdf0e10cSrcweir 	sal_Bool bHasName       = sal_False;
93cdf0e10cSrcweir 	sal_Bool bHasStyle      = sal_False;
94cdf0e10cSrcweir 	OUString aDisplayName;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 	awt::Gradient aGradient;
97cdf0e10cSrcweir 	aGradient.XOffset = 0;
98cdf0e10cSrcweir 	aGradient.YOffset = 0;
99cdf0e10cSrcweir 	aGradient.StartIntensity = 100;
100cdf0e10cSrcweir 	aGradient.EndIntensity = 100;
101cdf0e10cSrcweir 	aGradient.Angle = 0;
102cdf0e10cSrcweir 	aGradient.Border = 0;
103cdf0e10cSrcweir 
104cdf0e10cSrcweir     {
105cdf0e10cSrcweir         static __FAR_DATA SvXMLTokenMapEntry aTrGradientAttrTokenMap[] =
106cdf0e10cSrcweir {
107cdf0e10cSrcweir 	{ XML_NAMESPACE_DRAW, XML_NAME, XML_TOK_GRADIENT_NAME },
108cdf0e10cSrcweir 	{ XML_NAMESPACE_DRAW, XML_DISPLAY_NAME, XML_TOK_GRADIENT_DISPLAY_NAME },
109cdf0e10cSrcweir 	{ XML_NAMESPACE_DRAW, XML_STYLE, XML_TOK_GRADIENT_STYLE },
110cdf0e10cSrcweir 	{ XML_NAMESPACE_DRAW, XML_CX, XML_TOK_GRADIENT_CX },
111cdf0e10cSrcweir 	{ XML_NAMESPACE_DRAW, XML_CY, XML_TOK_GRADIENT_CY },
112cdf0e10cSrcweir 	{ XML_NAMESPACE_DRAW, XML_START, XML_TOK_GRADIENT_START },
113cdf0e10cSrcweir 	{ XML_NAMESPACE_DRAW, XML_END, XML_TOK_GRADIENT_END },
114cdf0e10cSrcweir 	{ XML_NAMESPACE_DRAW, XML_GRADIENT_ANGLE, XML_TOK_GRADIENT_ANGLE },
115cdf0e10cSrcweir 	{ XML_NAMESPACE_DRAW, XML_GRADIENT_BORDER, XML_TOK_GRADIENT_BORDER },
116cdf0e10cSrcweir 	XML_TOKEN_MAP_END
117cdf0e10cSrcweir };
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 	SvXMLTokenMap aTokenMap( aTrGradientAttrTokenMap );
120cdf0e10cSrcweir     SvXMLNamespaceMap& rNamespaceMap = rImport.GetNamespaceMap();
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 	sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
123cdf0e10cSrcweir 	for( sal_Int16 i=0; i < nAttrCount; i++ )
124cdf0e10cSrcweir 	{
125cdf0e10cSrcweir 		const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
126cdf0e10cSrcweir 		OUString aStrAttrName;
127cdf0e10cSrcweir 		sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName( rFullAttrName, &aStrAttrName );
128cdf0e10cSrcweir 		const OUString& rStrValue = xAttrList->getValueByIndex( i );
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 		sal_Int32 nTmpValue;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 		switch( aTokenMap.Get( nPrefix, aStrAttrName ) )
133cdf0e10cSrcweir 		{
134cdf0e10cSrcweir 		case XML_TOK_GRADIENT_NAME:
135cdf0e10cSrcweir 			{
136cdf0e10cSrcweir 				rStrName = rStrValue;
137cdf0e10cSrcweir 				bHasName = sal_True;
138cdf0e10cSrcweir 			}
139cdf0e10cSrcweir 			break;
140cdf0e10cSrcweir 		case XML_TOK_GRADIENT_DISPLAY_NAME:
141cdf0e10cSrcweir 			{
142cdf0e10cSrcweir 				aDisplayName = rStrValue;
143cdf0e10cSrcweir 			}
144cdf0e10cSrcweir 			break;
145cdf0e10cSrcweir 		case XML_TOK_GRADIENT_STYLE:
146cdf0e10cSrcweir 			{
147cdf0e10cSrcweir 				sal_uInt16 eValue;
148cdf0e10cSrcweir 				if( SvXMLUnitConverter::convertEnum( eValue, rStrValue, pXML_GradientStyle_Enum ) )
149cdf0e10cSrcweir 				{
150cdf0e10cSrcweir 					aGradient.Style = (awt::GradientStyle) eValue;
151cdf0e10cSrcweir 					bHasStyle = sal_True;
152cdf0e10cSrcweir 				}
153cdf0e10cSrcweir 			}
154cdf0e10cSrcweir 			break;
155cdf0e10cSrcweir 		case XML_TOK_GRADIENT_CX:
156cdf0e10cSrcweir 			SvXMLUnitConverter::convertPercent( nTmpValue, rStrValue );
157cdf0e10cSrcweir 			aGradient.XOffset = sal::static_int_cast< sal_Int16 >(nTmpValue);
158cdf0e10cSrcweir 			break;
159cdf0e10cSrcweir 		case XML_TOK_GRADIENT_CY:
160cdf0e10cSrcweir 			SvXMLUnitConverter::convertPercent( nTmpValue, rStrValue );
161cdf0e10cSrcweir 			aGradient.YOffset = sal::static_int_cast< sal_Int16 >(nTmpValue);
162cdf0e10cSrcweir 			break;
163cdf0e10cSrcweir 		case XML_TOK_GRADIENT_START:
164cdf0e10cSrcweir 			{
165cdf0e10cSrcweir 				sal_Int32 aStartTransparency;
166cdf0e10cSrcweir 				SvXMLUnitConverter::convertPercent( aStartTransparency, rStrValue );
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 				sal_uInt8 n = sal::static_int_cast< sal_uInt8 >(
169cdf0e10cSrcweir                     ( (100 - aStartTransparency) * 255 ) / 100 );
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 				Color aColor( n, n, n );
172cdf0e10cSrcweir 				aGradient.StartColor = (sal_Int32)( aColor.GetColor() );
173cdf0e10cSrcweir 			}
174cdf0e10cSrcweir 			break;
175cdf0e10cSrcweir 		case XML_TOK_GRADIENT_END:
176cdf0e10cSrcweir 			{
177cdf0e10cSrcweir 				sal_Int32 aEndTransparency;
178cdf0e10cSrcweir 				SvXMLUnitConverter::convertPercent( aEndTransparency, rStrValue );
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 				sal_uInt8 n = sal::static_int_cast< sal_uInt8 >(
181cdf0e10cSrcweir                     ( (100 - aEndTransparency) * 255 ) / 100 );
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 				Color aColor( n, n, n );
184cdf0e10cSrcweir 				aGradient.EndColor = (sal_Int32)( aColor.GetColor() );
185cdf0e10cSrcweir 			}
186cdf0e10cSrcweir 			break;
187cdf0e10cSrcweir 		case XML_TOK_GRADIENT_ANGLE:
188cdf0e10cSrcweir 			{
189cdf0e10cSrcweir 				sal_Int32 nValue;
190cdf0e10cSrcweir 				SvXMLUnitConverter::convertNumber( nValue, rStrValue, 0, 3600 );
191cdf0e10cSrcweir 				aGradient.Angle = sal_Int16( nValue );
192cdf0e10cSrcweir 			}
193cdf0e10cSrcweir 			break;
194cdf0e10cSrcweir 		case XML_TOK_GRADIENT_BORDER:
195cdf0e10cSrcweir 			SvXMLUnitConverter::convertPercent( nTmpValue, rStrValue );
196cdf0e10cSrcweir 			aGradient.Border = sal::static_int_cast< sal_Int16 >(nTmpValue);
197cdf0e10cSrcweir 			break;
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 		default:
200cdf0e10cSrcweir 			DBG_WARNING( "Unknown token at import transparency gradient style" )
201cdf0e10cSrcweir 			;
202cdf0e10cSrcweir 		}
203cdf0e10cSrcweir 	}
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 	rValue <<= aGradient;
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 	if( aDisplayName.getLength() )
208cdf0e10cSrcweir 	{
209cdf0e10cSrcweir 		rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_GRADIENT_ID, rStrName,
210cdf0e10cSrcweir 									 aDisplayName );
211cdf0e10cSrcweir 		rStrName = aDisplayName;
212cdf0e10cSrcweir 	}
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 	bRet = bHasName && bHasStyle;
215cdf0e10cSrcweir 
216cdf0e10cSrcweir     }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 	return bRet;
219cdf0e10cSrcweir }
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 
222cdf0e10cSrcweir //-------------------------------------------------------------
223cdf0e10cSrcweir // Export
224cdf0e10cSrcweir //-------------------------------------------------------------
225cdf0e10cSrcweir 
226cdf0e10cSrcweir #ifndef SVX_LIGHT
227cdf0e10cSrcweir 
XMLTransGradientStyleExport(SvXMLExport & rExp)228cdf0e10cSrcweir XMLTransGradientStyleExport::XMLTransGradientStyleExport( SvXMLExport& rExp )
229cdf0e10cSrcweir     : rExport(rExp)
230cdf0e10cSrcweir {
231cdf0e10cSrcweir }
232cdf0e10cSrcweir 
~XMLTransGradientStyleExport()233cdf0e10cSrcweir XMLTransGradientStyleExport::~XMLTransGradientStyleExport()
234cdf0e10cSrcweir {
235cdf0e10cSrcweir }
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 
exportXML(const OUString & rStrName,const uno::Any & rValue)238cdf0e10cSrcweir sal_Bool XMLTransGradientStyleExport::exportXML(
239cdf0e10cSrcweir     const OUString& rStrName,
240cdf0e10cSrcweir     const uno::Any& rValue )
241cdf0e10cSrcweir {
242cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
243cdf0e10cSrcweir 	awt::Gradient aGradient;
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 	if( rStrName.getLength() )
246cdf0e10cSrcweir 	{
247cdf0e10cSrcweir 		if( rValue >>= aGradient )
248cdf0e10cSrcweir 		{
249cdf0e10cSrcweir 			OUString aStrValue;
250cdf0e10cSrcweir 			OUStringBuffer aOut;
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 			// Style
253cdf0e10cSrcweir 			if( !SvXMLUnitConverter::convertEnum( aOut, aGradient.Style, pXML_GradientStyle_Enum ) )
254cdf0e10cSrcweir 			{
255cdf0e10cSrcweir 				bRet = sal_False;
256cdf0e10cSrcweir 			}
257cdf0e10cSrcweir 			else
258cdf0e10cSrcweir 			{
259cdf0e10cSrcweir 				// Name
260cdf0e10cSrcweir 				sal_Bool bEncoded = sal_False;
261cdf0e10cSrcweir 				rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
262cdf0e10cSrcweir 									  rExport.EncodeStyleName( rStrName,
263cdf0e10cSrcweir 										 					   &bEncoded ) );
264cdf0e10cSrcweir 				if( bEncoded )
265cdf0e10cSrcweir 					rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
266cdf0e10cSrcweir 									  	  rStrName );
267cdf0e10cSrcweir 
268cdf0e10cSrcweir 				aStrValue = aOut.makeStringAndClear();
269cdf0e10cSrcweir 				rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
270cdf0e10cSrcweir 
271cdf0e10cSrcweir 				// Center x/y
272cdf0e10cSrcweir 				if( aGradient.Style != awt::GradientStyle_LINEAR &&
273cdf0e10cSrcweir 					aGradient.Style != awt::GradientStyle_AXIAL   )
274cdf0e10cSrcweir 				{
275cdf0e10cSrcweir 					SvXMLUnitConverter::convertPercent( aOut, aGradient.XOffset );
276cdf0e10cSrcweir 					aStrValue = aOut.makeStringAndClear();
277cdf0e10cSrcweir 					rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_CX, aStrValue );
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 					SvXMLUnitConverter::convertPercent( aOut, aGradient.YOffset );
280cdf0e10cSrcweir 					aStrValue = aOut.makeStringAndClear();
281cdf0e10cSrcweir 					rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_CY, aStrValue );
282cdf0e10cSrcweir 				}
283cdf0e10cSrcweir 
284cdf0e10cSrcweir 
285cdf0e10cSrcweir 				Color aColor;
286cdf0e10cSrcweir 
287cdf0e10cSrcweir 				// Transparency start
288cdf0e10cSrcweir 				aColor.SetColor( aGradient.StartColor );
289cdf0e10cSrcweir 				sal_Int32 aStartValue = 100 - (sal_Int32)(((aColor.GetRed() + 1) * 100) / 255);
290cdf0e10cSrcweir 				SvXMLUnitConverter::convertPercent( aOut, aStartValue );
291cdf0e10cSrcweir 				aStrValue = aOut.makeStringAndClear();
292cdf0e10cSrcweir 				rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_START, aStrValue );
293cdf0e10cSrcweir 
294cdf0e10cSrcweir 				// Transparency end
295cdf0e10cSrcweir 				aColor.SetColor( aGradient.EndColor );
296cdf0e10cSrcweir 				sal_Int32 aEndValue = 100 - (sal_Int32)(((aColor.GetRed() + 1) * 100) / 255);
297cdf0e10cSrcweir 				SvXMLUnitConverter::convertPercent( aOut, aEndValue );
298cdf0e10cSrcweir 				aStrValue = aOut.makeStringAndClear();
299cdf0e10cSrcweir 				rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_END, aStrValue );
300cdf0e10cSrcweir 
301cdf0e10cSrcweir 				// Angle
302cdf0e10cSrcweir 				if( aGradient.Style != awt::GradientStyle_RADIAL )
303cdf0e10cSrcweir 				{
304cdf0e10cSrcweir 					SvXMLUnitConverter::convertNumber( aOut, sal_Int32( aGradient.Angle ) );
305cdf0e10cSrcweir 					aStrValue = aOut.makeStringAndClear();
306cdf0e10cSrcweir 					rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_GRADIENT_ANGLE, aStrValue );
307cdf0e10cSrcweir 				}
308cdf0e10cSrcweir 
309cdf0e10cSrcweir 				// Border
310cdf0e10cSrcweir 				SvXMLUnitConverter::convertPercent( aOut, aGradient.Border );
311cdf0e10cSrcweir 				aStrValue = aOut.makeStringAndClear();
312cdf0e10cSrcweir 				rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_GRADIENT_BORDER, aStrValue );
313cdf0e10cSrcweir 
314cdf0e10cSrcweir 				// Do Write
315cdf0e10cSrcweir 				SvXMLElementExport rElem( rExport,
316cdf0e10cSrcweir 										  XML_NAMESPACE_DRAW, XML_OPACITY,
317cdf0e10cSrcweir 										  sal_True, sal_False );
318cdf0e10cSrcweir 			}
319cdf0e10cSrcweir 		}
320cdf0e10cSrcweir 	}
321cdf0e10cSrcweir 
322cdf0e10cSrcweir 	return bRet;
323cdf0e10cSrcweir }
324cdf0e10cSrcweir 
325cdf0e10cSrcweir #endif // #ifndef SVX_LIGHT
326