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