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