bump product version to 4.1.6.2
[LibreOffice.git] / xmloff / source / style / GradientStyle.cxx
blobbc200d8ef6a2a267ec23cdc7ce67f73eaac3d630
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "xmloff/GradientStyle.hxx"
23 #include <com/sun/star/awt/Gradient.hpp>
25 #include <sax/tools/converter.hxx>
27 #include <xmloff/attrlist.hxx>
28 #include <xmloff/nmspmap.hxx>
29 #include <xmloff/xmluconv.hxx>
30 #include "xmloff/xmlnmspe.hxx"
31 #include <xmloff/xmltoken.hxx>
32 #include <rtl/ustrbuf.hxx>
33 #include <rtl/ustring.hxx>
34 #include <tools/debug.hxx>
35 #include <xmloff/xmltkmap.hxx>
36 #include <xmloff/xmlexp.hxx>
37 #include <xmloff/xmlimp.hxx>
41 using namespace ::com::sun::star;
43 using namespace ::xmloff::token;
45 enum SvXMLTokenMapAttrs
47 XML_TOK_GRADIENT_NAME,
48 XML_TOK_GRADIENT_DISPLAY_NAME,
49 XML_TOK_GRADIENT_STYLE,
50 XML_TOK_GRADIENT_CX,
51 XML_TOK_GRADIENT_CY,
52 XML_TOK_GRADIENT_STARTCOLOR,
53 XML_TOK_GRADIENT_ENDCOLOR,
54 XML_TOK_GRADIENT_STARTINT,
55 XML_TOK_GRADIENT_ENDINT,
56 XML_TOK_GRADIENT_ANGLE,
57 XML_TOK_GRADIENT_BORDER,
58 XML_TOK_TABSTOP_END=XML_TOK_UNKNOWN
62 SvXMLEnumMapEntry const pXML_GradientStyle_Enum[] =
64 { XML_GRADIENTSTYLE_LINEAR, awt::GradientStyle_LINEAR },
65 { XML_GRADIENTSTYLE_AXIAL, awt::GradientStyle_AXIAL },
66 { XML_GRADIENTSTYLE_RADIAL, awt::GradientStyle_RADIAL },
67 { XML_GRADIENTSTYLE_ELLIPSOID, awt::GradientStyle_ELLIPTICAL },
68 { XML_GRADIENTSTYLE_SQUARE, awt::GradientStyle_SQUARE },
69 { XML_GRADIENTSTYLE_RECTANGULAR, awt::GradientStyle_RECT },
70 { XML_TOKEN_INVALID, 0 }
73 //-------------------------------------------------------------
74 // Import
75 //-------------------------------------------------------------
76 XMLGradientStyleImport::XMLGradientStyleImport(
77 SvXMLImport& rImp )
78 : rImport(rImp)
82 XMLGradientStyleImport::~XMLGradientStyleImport()
86 sal_Bool XMLGradientStyleImport::importXML(
87 const uno::Reference< xml::sax::XAttributeList >& xAttrList,
88 uno::Any& rValue,
89 OUString& rStrName )
91 sal_Bool bRet = sal_False;
92 sal_Bool bHasName = sal_False;
93 sal_Bool bHasStyle = sal_False;
94 sal_Bool bHasStartColor = sal_False;
95 sal_Bool bHasEndColor = sal_False;
96 OUString aDisplayName;
98 awt::Gradient aGradient;
99 aGradient.XOffset = 0;
100 aGradient.YOffset = 0;
101 aGradient.StartIntensity = 100;
102 aGradient.EndIntensity = 100;
103 aGradient.Angle = 0;
104 aGradient.Border = 0;
107 static SvXMLTokenMapEntry aGradientAttrTokenMap[] =
109 { XML_NAMESPACE_DRAW, XML_NAME, XML_TOK_GRADIENT_NAME },
110 { XML_NAMESPACE_DRAW, XML_DISPLAY_NAME, XML_TOK_GRADIENT_DISPLAY_NAME },
111 { XML_NAMESPACE_DRAW, XML_STYLE, XML_TOK_GRADIENT_STYLE },
112 { XML_NAMESPACE_DRAW, XML_CX, XML_TOK_GRADIENT_CX },
113 { XML_NAMESPACE_DRAW, XML_CY, XML_TOK_GRADIENT_CY },
114 { XML_NAMESPACE_DRAW, XML_START_COLOR, XML_TOK_GRADIENT_STARTCOLOR },
115 { XML_NAMESPACE_DRAW, XML_END_COLOR, XML_TOK_GRADIENT_ENDCOLOR },
116 { XML_NAMESPACE_DRAW, XML_START_INTENSITY, XML_TOK_GRADIENT_STARTINT },
117 { XML_NAMESPACE_DRAW, XML_END_INTENSITY, XML_TOK_GRADIENT_ENDINT },
118 { XML_NAMESPACE_DRAW, XML_GRADIENT_ANGLE, XML_TOK_GRADIENT_ANGLE },
119 { XML_NAMESPACE_DRAW, XML_GRADIENT_BORDER, XML_TOK_GRADIENT_BORDER },
120 XML_TOKEN_MAP_END
123 SvXMLTokenMap aTokenMap( aGradientAttrTokenMap );
124 SvXMLNamespaceMap& rNamespaceMap = rImport.GetNamespaceMap();
126 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
127 for( sal_Int16 i=0; i < nAttrCount; i++ )
129 const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
130 OUString aStrAttrName;
131 sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName( rFullAttrName, &aStrAttrName );
132 const OUString& rStrValue = xAttrList->getValueByIndex( i );
134 sal_Int32 nTmpValue;
136 switch( aTokenMap.Get( nPrefix, aStrAttrName ) )
138 case XML_TOK_GRADIENT_NAME:
140 rStrName = rStrValue;
141 bHasName = sal_True;
143 break;
144 case XML_TOK_GRADIENT_DISPLAY_NAME:
146 aDisplayName = rStrValue;
148 break;
149 case XML_TOK_GRADIENT_STYLE:
151 sal_uInt16 eValue;
152 if( SvXMLUnitConverter::convertEnum( eValue, rStrValue, pXML_GradientStyle_Enum ) )
154 aGradient.Style = (awt::GradientStyle) eValue;
155 bHasStyle = sal_True;
158 break;
159 case XML_TOK_GRADIENT_CX:
160 ::sax::Converter::convertPercent( nTmpValue, rStrValue );
161 aGradient.XOffset = static_cast< sal_Int16 >( nTmpValue );
162 break;
163 case XML_TOK_GRADIENT_CY:
164 ::sax::Converter::convertPercent( nTmpValue, rStrValue );
165 aGradient.YOffset = static_cast< sal_Int16 >( nTmpValue );
166 break;
167 case XML_TOK_GRADIENT_STARTCOLOR:
169 bHasStartColor = ::sax::Converter::convertColor(
170 aGradient.StartColor, rStrValue);
172 break;
173 case XML_TOK_GRADIENT_ENDCOLOR:
175 bHasStartColor = ::sax::Converter::convertColor(
176 aGradient.EndColor, rStrValue);
178 break;
179 case XML_TOK_GRADIENT_STARTINT:
180 ::sax::Converter::convertPercent( nTmpValue, rStrValue );
181 aGradient.StartIntensity = static_cast< sal_Int16 >( nTmpValue );
182 break;
183 case XML_TOK_GRADIENT_ENDINT:
184 ::sax::Converter::convertPercent( nTmpValue, rStrValue );
185 aGradient.EndIntensity = static_cast< sal_Int16 >( nTmpValue );
186 break;
187 case XML_TOK_GRADIENT_ANGLE:
189 sal_Int32 nValue;
190 ::sax::Converter::convertNumber( nValue, rStrValue, 0, 3600 );
191 aGradient.Angle = sal_Int16( nValue );
193 break;
194 case XML_TOK_GRADIENT_BORDER:
195 ::sax::Converter::convertPercent( nTmpValue, rStrValue );
196 aGradient.Border = static_cast< sal_Int16 >( nTmpValue );
197 break;
199 default:
200 DBG_WARNING( "Unknown token at import gradient style" )
205 rValue <<= aGradient;
207 if( !aDisplayName.isEmpty() )
209 rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_GRADIENT_ID, rStrName,
210 aDisplayName );
211 rStrName = aDisplayName;
214 bRet = bHasName && bHasStyle && bHasStartColor && bHasEndColor;
218 return bRet;
222 //-------------------------------------------------------------
223 // Export
224 //-------------------------------------------------------------
226 XMLGradientStyleExport::XMLGradientStyleExport(
227 SvXMLExport& rExp )
228 : rExport(rExp)
232 XMLGradientStyleExport::~XMLGradientStyleExport()
236 sal_Bool XMLGradientStyleExport::exportXML(
237 const OUString& rStrName,
238 const uno::Any& rValue )
240 sal_Bool bRet = sal_False;
241 awt::Gradient aGradient;
243 if( !rStrName.isEmpty() )
245 if( rValue >>= aGradient )
247 OUString aStrValue;
248 OUStringBuffer aOut;
250 // Style
251 if( !SvXMLUnitConverter::convertEnum( aOut, aGradient.Style, pXML_GradientStyle_Enum ) )
253 bRet = sal_False;
255 else
257 // Name
258 sal_Bool bEncoded = sal_False;
259 OUString aStrName( rStrName );
260 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
261 rExport.EncodeStyleName( aStrName,
262 &bEncoded ) );
263 if( bEncoded )
264 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
265 aStrName );
268 aStrValue = aOut.makeStringAndClear();
269 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
271 // Center x/y
272 if( aGradient.Style != awt::GradientStyle_LINEAR &&
273 aGradient.Style != awt::GradientStyle_AXIAL )
275 ::sax::Converter::convertPercent(aOut, aGradient.XOffset);
276 aStrValue = aOut.makeStringAndClear();
277 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_CX, aStrValue );
278 ::sax::Converter::convertPercent(aOut, aGradient.YOffset);
279 aStrValue = aOut.makeStringAndClear();
280 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_CY, aStrValue );
283 // Color start
284 ::sax::Converter::convertColor(aOut, aGradient.StartColor);
285 aStrValue = aOut.makeStringAndClear();
286 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_START_COLOR, aStrValue );
288 // Color end
289 ::sax::Converter::convertColor(aOut, aGradient.EndColor);
290 aStrValue = aOut.makeStringAndClear();
291 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_END_COLOR, aStrValue );
293 // Intensity start
294 ::sax::Converter::convertPercent(aOut, aGradient.StartIntensity);
295 aStrValue = aOut.makeStringAndClear();
296 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_START_INTENSITY, aStrValue );
298 // Intensity end
299 ::sax::Converter::convertPercent(aOut, aGradient.EndIntensity);
300 aStrValue = aOut.makeStringAndClear();
301 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_END_INTENSITY, aStrValue );
303 // Angle
304 if( aGradient.Style != awt::GradientStyle_RADIAL )
306 ::sax::Converter::convertNumber(aOut, sal_Int32(aGradient.Angle));
307 aStrValue = aOut.makeStringAndClear();
308 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_GRADIENT_ANGLE, aStrValue );
311 // Border
312 ::sax::Converter::convertPercent( aOut, aGradient.Border );
313 aStrValue = aOut.makeStringAndClear();
314 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_GRADIENT_BORDER, aStrValue );
316 // Do Write
317 SvXMLElementExport aElem( rExport, XML_NAMESPACE_DRAW, XML_GRADIENT,
318 sal_True, sal_False );
323 return bRet;
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */