bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / style / TransGradientStyle.cxx
blob7b1ca15151936f21e7e08169d7852b003eb6cbf1
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 "TransGradientStyle.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 <rtl/ustrbuf.hxx>
31 #include <rtl/ustring.hxx>
32 #include <tools/color.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_START,
50 XML_TOK_GRADIENT_END,
51 XML_TOK_GRADIENT_ANGLE,
52 XML_TOK_GRADIENT_BORDER,
53 XML_TOK_TABSTOP_END=XML_TOK_UNKNOWN
56 SvXMLEnumMapEntry const pXML_GradientStyle_Enum[] =
58 { XML_GRADIENTSTYLE_LINEAR, awt::GradientStyle_LINEAR },
59 { XML_GRADIENTSTYLE_AXIAL, awt::GradientStyle_AXIAL },
60 { XML_GRADIENTSTYLE_RADIAL, awt::GradientStyle_RADIAL },
61 { XML_GRADIENTSTYLE_ELLIPSOID, awt::GradientStyle_ELLIPTICAL },
62 { XML_GRADIENTSTYLE_SQUARE, awt::GradientStyle_SQUARE },
63 { XML_GRADIENTSTYLE_RECTANGULAR, awt::GradientStyle_RECT },
64 { XML_TOKEN_INVALID, 0 }
67 // Import
69 XMLTransGradientStyleImport::XMLTransGradientStyleImport( SvXMLImport& rImp )
70 : rImport(rImp)
74 XMLTransGradientStyleImport::~XMLTransGradientStyleImport()
78 bool XMLTransGradientStyleImport::importXML(
79 const uno::Reference< xml::sax::XAttributeList >& xAttrList,
80 uno::Any& rValue,
81 OUString& rStrName )
83 bool bRet = false;
84 bool bHasName = false;
85 bool bHasStyle = false;
86 OUString aDisplayName;
88 awt::Gradient aGradient;
89 aGradient.XOffset = 0;
90 aGradient.YOffset = 0;
91 aGradient.StartIntensity = 100;
92 aGradient.EndIntensity = 100;
93 aGradient.Angle = 0;
94 aGradient.Border = 0;
97 static const SvXMLTokenMapEntry aTrGradientAttrTokenMap[] =
99 { XML_NAMESPACE_DRAW, XML_NAME, XML_TOK_GRADIENT_NAME },
100 { XML_NAMESPACE_DRAW, XML_DISPLAY_NAME, XML_TOK_GRADIENT_DISPLAY_NAME },
101 { XML_NAMESPACE_DRAW, XML_STYLE, XML_TOK_GRADIENT_STYLE },
102 { XML_NAMESPACE_DRAW, XML_CX, XML_TOK_GRADIENT_CX },
103 { XML_NAMESPACE_DRAW, XML_CY, XML_TOK_GRADIENT_CY },
104 { XML_NAMESPACE_DRAW, XML_START, XML_TOK_GRADIENT_START },
105 { XML_NAMESPACE_DRAW, XML_END, XML_TOK_GRADIENT_END },
106 { XML_NAMESPACE_DRAW, XML_GRADIENT_ANGLE, XML_TOK_GRADIENT_ANGLE },
107 { XML_NAMESPACE_DRAW, XML_GRADIENT_BORDER, XML_TOK_GRADIENT_BORDER },
108 XML_TOKEN_MAP_END
111 SvXMLTokenMap aTokenMap( aTrGradientAttrTokenMap );
112 SvXMLNamespaceMap& rNamespaceMap = rImport.GetNamespaceMap();
114 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
115 for( sal_Int16 i=0; i < nAttrCount; i++ )
117 const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
118 OUString aStrAttrName;
119 sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName( rFullAttrName, &aStrAttrName );
120 const OUString& rStrValue = xAttrList->getValueByIndex( i );
122 sal_Int32 nTmpValue;
124 switch( aTokenMap.Get( nPrefix, aStrAttrName ) )
126 case XML_TOK_GRADIENT_NAME:
128 rStrName = rStrValue;
129 bHasName = true;
131 break;
132 case XML_TOK_GRADIENT_DISPLAY_NAME:
134 aDisplayName = rStrValue;
136 break;
137 case XML_TOK_GRADIENT_STYLE:
139 sal_uInt16 eValue;
140 if( SvXMLUnitConverter::convertEnum( eValue, rStrValue, pXML_GradientStyle_Enum ) )
142 aGradient.Style = (awt::GradientStyle) eValue;
143 bHasStyle = true;
146 break;
147 case XML_TOK_GRADIENT_CX:
148 ::sax::Converter::convertPercent( nTmpValue, rStrValue );
149 aGradient.XOffset = sal::static_int_cast< sal_Int16 >(nTmpValue);
150 break;
151 case XML_TOK_GRADIENT_CY:
152 ::sax::Converter::convertPercent( nTmpValue, rStrValue );
153 aGradient.YOffset = sal::static_int_cast< sal_Int16 >(nTmpValue);
154 break;
155 case XML_TOK_GRADIENT_START:
157 sal_Int32 aStartTransparency;
158 ::sax::Converter::convertPercent( aStartTransparency, rStrValue );
160 sal_uInt8 n = sal::static_int_cast< sal_uInt8 >(
161 ( (100 - aStartTransparency) * 255 ) / 100 );
163 Color aColor( n, n, n );
164 aGradient.StartColor = (sal_Int32)( aColor.GetColor() );
166 break;
167 case XML_TOK_GRADIENT_END:
169 sal_Int32 aEndTransparency;
170 ::sax::Converter::convertPercent( aEndTransparency, rStrValue );
172 sal_uInt8 n = sal::static_int_cast< sal_uInt8 >(
173 ( (100 - aEndTransparency) * 255 ) / 100 );
175 Color aColor( n, n, n );
176 aGradient.EndColor = (sal_Int32)( aColor.GetColor() );
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 = sal::static_int_cast< sal_Int16 >(nTmpValue);
189 break;
191 default:
192 SAL_INFO("xmloff.style", "Unknown token at import transparency 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 bRet = bHasName && bHasStyle;
209 return bRet;
212 // Export
214 XMLTransGradientStyleExport::XMLTransGradientStyleExport( SvXMLExport& rExp )
215 : rExport(rExp)
219 XMLTransGradientStyleExport::~XMLTransGradientStyleExport()
223 bool XMLTransGradientStyleExport::exportXML(
224 const OUString& rStrName,
225 const uno::Any& rValue )
227 bool bRet = false;
228 awt::Gradient aGradient;
230 if( !rStrName.isEmpty() )
232 if( rValue >>= aGradient )
234 OUString aStrValue;
235 OUStringBuffer aOut;
237 // Style
238 if( !SvXMLUnitConverter::convertEnum( aOut, aGradient.Style, pXML_GradientStyle_Enum ) )
240 bRet = false;
242 else
244 // Name
245 bool bEncoded = false;
246 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
247 rExport.EncodeStyleName( rStrName,
248 &bEncoded ) );
249 if( bEncoded )
250 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
251 rStrName );
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 );
264 ::sax::Converter::convertPercent(aOut, aGradient.YOffset);
265 aStrValue = aOut.makeStringAndClear();
266 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_CY, aStrValue );
269 Color aColor;
271 // Transparency start
272 aColor.SetColor( aGradient.StartColor );
273 sal_Int32 aStartValue = 100 - (sal_Int32)(((aColor.GetRed() + 1) * 100) / 255);
274 ::sax::Converter::convertPercent( aOut, aStartValue );
275 aStrValue = aOut.makeStringAndClear();
276 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_START, aStrValue );
278 // Transparency end
279 aColor.SetColor( aGradient.EndColor );
280 sal_Int32 aEndValue = 100 - (sal_Int32)(((aColor.GetRed() + 1) * 100) / 255);
281 ::sax::Converter::convertPercent( aOut, aEndValue );
282 aStrValue = aOut.makeStringAndClear();
283 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_END, aStrValue );
285 // Angle
286 if( aGradient.Style != awt::GradientStyle_RADIAL )
288 ::sax::Converter::convertAngle(aOut, aGradient.Angle);
289 aStrValue = aOut.makeStringAndClear();
290 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_GRADIENT_ANGLE, aStrValue );
293 // Border
294 ::sax::Converter::convertPercent( aOut, aGradient.Border );
295 aStrValue = aOut.makeStringAndClear();
296 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_GRADIENT_BORDER, aStrValue );
298 // Do Write
299 SvXMLElementExport rElem( rExport,
300 XML_NAMESPACE_DRAW, XML_OPACITY,
301 true, false );
306 return bRet;
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */