1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
,
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 //-------------------------------------------------------------
75 //-------------------------------------------------------------
76 XMLGradientStyleImport::XMLGradientStyleImport(
82 XMLGradientStyleImport::~XMLGradientStyleImport()
86 sal_Bool
XMLGradientStyleImport::importXML(
87 const uno::Reference
< xml::sax::XAttributeList
>& xAttrList
,
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;
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
},
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
);
136 switch( aTokenMap
.Get( nPrefix
, aStrAttrName
) )
138 case XML_TOK_GRADIENT_NAME
:
140 rStrName
= rStrValue
;
144 case XML_TOK_GRADIENT_DISPLAY_NAME
:
146 aDisplayName
= rStrValue
;
149 case XML_TOK_GRADIENT_STYLE
:
152 if( SvXMLUnitConverter::convertEnum( eValue
, rStrValue
, pXML_GradientStyle_Enum
) )
154 aGradient
.Style
= (awt::GradientStyle
) eValue
;
155 bHasStyle
= sal_True
;
159 case XML_TOK_GRADIENT_CX
:
160 ::sax::Converter::convertPercent( nTmpValue
, rStrValue
);
161 aGradient
.XOffset
= static_cast< sal_Int16
>( nTmpValue
);
163 case XML_TOK_GRADIENT_CY
:
164 ::sax::Converter::convertPercent( nTmpValue
, rStrValue
);
165 aGradient
.YOffset
= static_cast< sal_Int16
>( nTmpValue
);
167 case XML_TOK_GRADIENT_STARTCOLOR
:
169 bHasStartColor
= ::sax::Converter::convertColor(
170 aGradient
.StartColor
, rStrValue
);
173 case XML_TOK_GRADIENT_ENDCOLOR
:
175 bHasStartColor
= ::sax::Converter::convertColor(
176 aGradient
.EndColor
, rStrValue
);
179 case XML_TOK_GRADIENT_STARTINT
:
180 ::sax::Converter::convertPercent( nTmpValue
, rStrValue
);
181 aGradient
.StartIntensity
= static_cast< sal_Int16
>( nTmpValue
);
183 case XML_TOK_GRADIENT_ENDINT
:
184 ::sax::Converter::convertPercent( nTmpValue
, rStrValue
);
185 aGradient
.EndIntensity
= static_cast< sal_Int16
>( nTmpValue
);
187 case XML_TOK_GRADIENT_ANGLE
:
190 ::sax::Converter::convertNumber( nValue
, rStrValue
, 0, 3600 );
191 aGradient
.Angle
= sal_Int16( nValue
);
194 case XML_TOK_GRADIENT_BORDER
:
195 ::sax::Converter::convertPercent( nTmpValue
, rStrValue
);
196 aGradient
.Border
= static_cast< sal_Int16
>( nTmpValue
);
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
,
211 rStrName
= aDisplayName
;
214 bRet
= bHasName
&& bHasStyle
&& bHasStartColor
&& bHasEndColor
;
222 //-------------------------------------------------------------
224 //-------------------------------------------------------------
226 XMLGradientStyleExport::XMLGradientStyleExport(
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
)
251 if( !SvXMLUnitConverter::convertEnum( aOut
, aGradient
.Style
, pXML_GradientStyle_Enum
) )
258 sal_Bool bEncoded
= sal_False
;
259 OUString
aStrName( rStrName
);
260 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_NAME
,
261 rExport
.EncodeStyleName( aStrName
,
264 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DISPLAY_NAME
,
268 aStrValue
= aOut
.makeStringAndClear();
269 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_STYLE
, aStrValue
);
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
);
284 ::sax::Converter::convertColor(aOut
, aGradient
.StartColor
);
285 aStrValue
= aOut
.makeStringAndClear();
286 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_START_COLOR
, aStrValue
);
289 ::sax::Converter::convertColor(aOut
, aGradient
.EndColor
);
290 aStrValue
= aOut
.makeStringAndClear();
291 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_END_COLOR
, aStrValue
);
294 ::sax::Converter::convertPercent(aOut
, aGradient
.StartIntensity
);
295 aStrValue
= aOut
.makeStringAndClear();
296 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_START_INTENSITY
, aStrValue
);
299 ::sax::Converter::convertPercent(aOut
, aGradient
.EndIntensity
);
300 aStrValue
= aOut
.makeStringAndClear();
301 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_END_INTENSITY
, aStrValue
);
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
);
312 ::sax::Converter::convertPercent( aOut
, aGradient
.Border
);
313 aStrValue
= aOut
.makeStringAndClear();
314 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_GRADIENT_BORDER
, aStrValue
);
317 SvXMLElementExport
aElem( rExport
, XML_NAMESPACE_DRAW
, XML_GRADIENT
,
318 sal_True
, sal_False
);
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */