bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / style / GradientStyle.cxx
blob0ad8c74c904407fcffc5313b600fbf30d182d84c
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 .
20 #include <xmloff/GradientStyle.hxx>
22 #include <com/sun/star/awt/Gradient.hpp>
24 #include <sax/tools/converter.hxx>
26 #include <xmloff/attrlist.hxx>
27 #include <xmloff/nmspmap.hxx>
28 #include <xmloff/xmluconv.hxx>
29 #include <xmloff/xmlnmspe.hxx>
30 #include <xmloff/xmltoken.hxx>
31 #include <rtl/ustrbuf.hxx>
32 #include <rtl/ustring.hxx>
33 #include <tools/debug.hxx>
34 #include <xmloff/xmltkmap.hxx>
35 #include <xmloff/xmlexp.hxx>
36 #include <xmloff/xmlimp.hxx>
38 using namespace ::com::sun::star;
40 using namespace ::xmloff::token;
42 enum SvXMLTokenMapAttrs
44 XML_TOK_GRADIENT_NAME,
45 XML_TOK_GRADIENT_DISPLAY_NAME,
46 XML_TOK_GRADIENT_STYLE,
47 XML_TOK_GRADIENT_CX,
48 XML_TOK_GRADIENT_CY,
49 XML_TOK_GRADIENT_STARTCOLOR,
50 XML_TOK_GRADIENT_ENDCOLOR,
51 XML_TOK_GRADIENT_STARTINT,
52 XML_TOK_GRADIENT_ENDINT,
53 XML_TOK_GRADIENT_ANGLE,
54 XML_TOK_GRADIENT_BORDER,
55 XML_TOK_TABSTOP_END=XML_TOK_UNKNOWN
58 SvXMLEnumMapEntry const pXML_GradientStyle_Enum[] =
60 { XML_GRADIENTSTYLE_LINEAR, awt::GradientStyle_LINEAR },
61 { XML_GRADIENTSTYLE_AXIAL, awt::GradientStyle_AXIAL },
62 { XML_GRADIENTSTYLE_RADIAL, awt::GradientStyle_RADIAL },
63 { XML_GRADIENTSTYLE_ELLIPSOID, awt::GradientStyle_ELLIPTICAL },
64 { XML_GRADIENTSTYLE_SQUARE, awt::GradientStyle_SQUARE },
65 { XML_GRADIENTSTYLE_RECTANGULAR, awt::GradientStyle_RECT },
66 { XML_TOKEN_INVALID, 0 }
69 // Import
70 XMLGradientStyleImport::XMLGradientStyleImport(
71 SvXMLImport& rImp )
72 : rImport(rImp)
76 XMLGradientStyleImport::~XMLGradientStyleImport()
80 bool XMLGradientStyleImport::importXML(
81 const uno::Reference< xml::sax::XAttributeList >& xAttrList,
82 uno::Any& rValue,
83 OUString& rStrName )
85 static const SvXMLTokenMapEntry aGradientAttrTokenMap[] =
87 { XML_NAMESPACE_DRAW, XML_NAME, XML_TOK_GRADIENT_NAME },
88 { XML_NAMESPACE_DRAW, XML_DISPLAY_NAME, XML_TOK_GRADIENT_DISPLAY_NAME },
89 { XML_NAMESPACE_DRAW, XML_STYLE, XML_TOK_GRADIENT_STYLE },
90 { XML_NAMESPACE_DRAW, XML_CX, XML_TOK_GRADIENT_CX },
91 { XML_NAMESPACE_DRAW, XML_CY, XML_TOK_GRADIENT_CY },
92 { XML_NAMESPACE_DRAW, XML_START_COLOR, XML_TOK_GRADIENT_STARTCOLOR },
93 { XML_NAMESPACE_DRAW, XML_END_COLOR, XML_TOK_GRADIENT_ENDCOLOR },
94 { XML_NAMESPACE_DRAW, XML_START_INTENSITY, XML_TOK_GRADIENT_STARTINT },
95 { XML_NAMESPACE_DRAW, XML_END_INTENSITY, XML_TOK_GRADIENT_ENDINT },
96 { XML_NAMESPACE_DRAW, XML_GRADIENT_ANGLE, XML_TOK_GRADIENT_ANGLE },
97 { XML_NAMESPACE_DRAW, XML_GRADIENT_BORDER, XML_TOK_GRADIENT_BORDER },
98 XML_TOKEN_MAP_END
101 bool bHasName = false;
102 bool bHasStyle = false;
103 bool bHasStartColor = false;
104 bool bHasEndColor = false;
105 OUString aDisplayName;
107 awt::Gradient aGradient;
108 aGradient.XOffset = 0;
109 aGradient.YOffset = 0;
110 aGradient.StartIntensity = 100;
111 aGradient.EndIntensity = 100;
112 aGradient.Angle = 0;
113 aGradient.Border = 0;
115 SvXMLTokenMap aTokenMap( aGradientAttrTokenMap );
116 SvXMLNamespaceMap& rNamespaceMap = rImport.GetNamespaceMap();
118 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
119 for( sal_Int16 i=0; i < nAttrCount; i++ )
121 const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
122 OUString aStrAttrName;
123 sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName( rFullAttrName, &aStrAttrName );
124 const OUString& rStrValue = xAttrList->getValueByIndex( i );
126 sal_Int32 nTmpValue;
128 switch( aTokenMap.Get( nPrefix, aStrAttrName ) )
130 case XML_TOK_GRADIENT_NAME:
132 rStrName = rStrValue;
133 bHasName = true;
135 break;
136 case XML_TOK_GRADIENT_DISPLAY_NAME:
138 aDisplayName = rStrValue;
140 break;
141 case XML_TOK_GRADIENT_STYLE:
143 sal_uInt16 eValue;
144 if( SvXMLUnitConverter::convertEnum( eValue, rStrValue, pXML_GradientStyle_Enum ) )
146 aGradient.Style = (awt::GradientStyle) eValue;
147 bHasStyle = true;
150 break;
151 case XML_TOK_GRADIENT_CX:
152 ::sax::Converter::convertPercent( nTmpValue, rStrValue );
153 aGradient.XOffset = static_cast< sal_Int16 >( nTmpValue );
154 break;
155 case XML_TOK_GRADIENT_CY:
156 ::sax::Converter::convertPercent( nTmpValue, rStrValue );
157 aGradient.YOffset = static_cast< sal_Int16 >( nTmpValue );
158 break;
159 case XML_TOK_GRADIENT_STARTCOLOR:
161 bHasStartColor = ::sax::Converter::convertColor(
162 aGradient.StartColor, rStrValue);
164 break;
165 case XML_TOK_GRADIENT_ENDCOLOR:
167 bHasStartColor = ::sax::Converter::convertColor(
168 aGradient.EndColor, rStrValue);
170 break;
171 case XML_TOK_GRADIENT_STARTINT:
172 ::sax::Converter::convertPercent( nTmpValue, rStrValue );
173 aGradient.StartIntensity = static_cast< sal_Int16 >( nTmpValue );
174 break;
175 case XML_TOK_GRADIENT_ENDINT:
176 ::sax::Converter::convertPercent( nTmpValue, rStrValue );
177 aGradient.EndIntensity = static_cast< sal_Int16 >( nTmpValue );
178 break;
179 case XML_TOK_GRADIENT_ANGLE:
181 bool const bSuccess =
182 ::sax::Converter::convertAngle(aGradient.Angle, rStrValue);
183 SAL_INFO_IF(!bSuccess, "xmloff.style", "failed to import draw:angle");
185 break;
186 case XML_TOK_GRADIENT_BORDER:
187 ::sax::Converter::convertPercent( nTmpValue, rStrValue );
188 aGradient.Border = static_cast< sal_Int16 >( nTmpValue );
189 break;
191 default:
192 SAL_INFO("xmloff.style", "Unknown token at import gradient style");
196 rValue <<= aGradient;
198 if( !aDisplayName.isEmpty() )
200 rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_GRADIENT_ID, rStrName,
201 aDisplayName );
202 rStrName = aDisplayName;
205 bool bRet = bHasName && bHasStyle && bHasStartColor && bHasEndColor;
207 return bRet;
210 // Export
212 XMLGradientStyleExport::XMLGradientStyleExport(
213 SvXMLExport& rExp )
214 : rExport(rExp)
218 XMLGradientStyleExport::~XMLGradientStyleExport()
222 bool XMLGradientStyleExport::exportXML(
223 const OUString& rStrName,
224 const uno::Any& rValue )
226 bool bRet = false;
227 awt::Gradient aGradient;
229 if( !rStrName.isEmpty() )
231 if( rValue >>= aGradient )
233 OUString aStrValue;
234 OUStringBuffer aOut;
236 // Style
237 if( !SvXMLUnitConverter::convertEnum( aOut, aGradient.Style, pXML_GradientStyle_Enum ) )
239 bRet = false;
241 else
243 // Name
244 bool bEncoded = false;
245 OUString aStrName( rStrName );
246 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
247 rExport.EncodeStyleName( aStrName,
248 &bEncoded ) );
249 if( bEncoded )
250 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
251 aStrName );
253 aStrValue = aOut.makeStringAndClear();
254 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
256 // Center x/y
257 if( aGradient.Style != awt::GradientStyle_LINEAR &&
258 aGradient.Style != awt::GradientStyle_AXIAL )
260 ::sax::Converter::convertPercent(aOut, aGradient.XOffset);
261 aStrValue = aOut.makeStringAndClear();
262 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_CX, aStrValue );
263 ::sax::Converter::convertPercent(aOut, aGradient.YOffset);
264 aStrValue = aOut.makeStringAndClear();
265 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_CY, aStrValue );
268 // Color start
269 ::sax::Converter::convertColor(aOut, aGradient.StartColor);
270 aStrValue = aOut.makeStringAndClear();
271 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_START_COLOR, aStrValue );
273 // Color end
274 ::sax::Converter::convertColor(aOut, aGradient.EndColor);
275 aStrValue = aOut.makeStringAndClear();
276 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_END_COLOR, aStrValue );
278 // Intensity start
279 ::sax::Converter::convertPercent(aOut, aGradient.StartIntensity);
280 aStrValue = aOut.makeStringAndClear();
281 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_START_INTENSITY, aStrValue );
283 // Intensity end
284 ::sax::Converter::convertPercent(aOut, aGradient.EndIntensity);
285 aStrValue = aOut.makeStringAndClear();
286 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_END_INTENSITY, aStrValue );
288 // Angle
289 if( aGradient.Style != awt::GradientStyle_RADIAL )
291 ::sax::Converter::convertAngle(aOut, aGradient.Angle);
292 aStrValue = aOut.makeStringAndClear();
293 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_GRADIENT_ANGLE, aStrValue );
296 // Border
297 ::sax::Converter::convertPercent( aOut, aGradient.Border );
298 aStrValue = aOut.makeStringAndClear();
299 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_GRADIENT_BORDER, aStrValue );
301 // Do Write
302 SvXMLElementExport aElem( rExport, XML_NAMESPACE_DRAW, XML_GRADIENT,
303 true, false );
308 return bRet;
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */