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 .
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
,
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 }
70 XMLGradientStyleImport::XMLGradientStyleImport(
76 XMLGradientStyleImport::~XMLGradientStyleImport()
80 bool XMLGradientStyleImport::importXML(
81 const uno::Reference
< xml::sax::XAttributeList
>& xAttrList
,
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
},
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;
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
);
128 switch( aTokenMap
.Get( nPrefix
, aStrAttrName
) )
130 case XML_TOK_GRADIENT_NAME
:
132 rStrName
= rStrValue
;
136 case XML_TOK_GRADIENT_DISPLAY_NAME
:
138 aDisplayName
= rStrValue
;
141 case XML_TOK_GRADIENT_STYLE
:
144 if( SvXMLUnitConverter::convertEnum( eValue
, rStrValue
, pXML_GradientStyle_Enum
) )
146 aGradient
.Style
= (awt::GradientStyle
) eValue
;
151 case XML_TOK_GRADIENT_CX
:
152 ::sax::Converter::convertPercent( nTmpValue
, rStrValue
);
153 aGradient
.XOffset
= static_cast< sal_Int16
>( nTmpValue
);
155 case XML_TOK_GRADIENT_CY
:
156 ::sax::Converter::convertPercent( nTmpValue
, rStrValue
);
157 aGradient
.YOffset
= static_cast< sal_Int16
>( nTmpValue
);
159 case XML_TOK_GRADIENT_STARTCOLOR
:
161 bHasStartColor
= ::sax::Converter::convertColor(
162 aGradient
.StartColor
, rStrValue
);
165 case XML_TOK_GRADIENT_ENDCOLOR
:
167 bHasStartColor
= ::sax::Converter::convertColor(
168 aGradient
.EndColor
, rStrValue
);
171 case XML_TOK_GRADIENT_STARTINT
:
172 ::sax::Converter::convertPercent( nTmpValue
, rStrValue
);
173 aGradient
.StartIntensity
= static_cast< sal_Int16
>( nTmpValue
);
175 case XML_TOK_GRADIENT_ENDINT
:
176 ::sax::Converter::convertPercent( nTmpValue
, rStrValue
);
177 aGradient
.EndIntensity
= static_cast< sal_Int16
>( nTmpValue
);
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");
186 case XML_TOK_GRADIENT_BORDER
:
187 ::sax::Converter::convertPercent( nTmpValue
, rStrValue
);
188 aGradient
.Border
= static_cast< sal_Int16
>( nTmpValue
);
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
,
202 rStrName
= aDisplayName
;
205 bool bRet
= bHasName
&& bHasStyle
&& bHasStartColor
&& bHasEndColor
;
212 XMLGradientStyleExport::XMLGradientStyleExport(
218 XMLGradientStyleExport::~XMLGradientStyleExport()
222 bool XMLGradientStyleExport::exportXML(
223 const OUString
& rStrName
,
224 const uno::Any
& rValue
)
227 awt::Gradient aGradient
;
229 if( !rStrName
.isEmpty() )
231 if( rValue
>>= aGradient
)
237 if( !SvXMLUnitConverter::convertEnum( aOut
, aGradient
.Style
, pXML_GradientStyle_Enum
) )
244 bool bEncoded
= false;
245 OUString
aStrName( rStrName
);
246 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_NAME
,
247 rExport
.EncodeStyleName( aStrName
,
250 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DISPLAY_NAME
,
253 aStrValue
= aOut
.makeStringAndClear();
254 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_STYLE
, aStrValue
);
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
);
269 ::sax::Converter::convertColor(aOut
, aGradient
.StartColor
);
270 aStrValue
= aOut
.makeStringAndClear();
271 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_START_COLOR
, aStrValue
);
274 ::sax::Converter::convertColor(aOut
, aGradient
.EndColor
);
275 aStrValue
= aOut
.makeStringAndClear();
276 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_END_COLOR
, aStrValue
);
279 ::sax::Converter::convertPercent(aOut
, aGradient
.StartIntensity
);
280 aStrValue
= aOut
.makeStringAndClear();
281 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_START_INTENSITY
, aStrValue
);
284 ::sax::Converter::convertPercent(aOut
, aGradient
.EndIntensity
);
285 aStrValue
= aOut
.makeStringAndClear();
286 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_END_INTENSITY
, aStrValue
);
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
);
297 ::sax::Converter::convertPercent( aOut
, aGradient
.Border
);
298 aStrValue
= aOut
.makeStringAndClear();
299 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_GRADIENT_BORDER
, aStrValue
);
302 SvXMLElementExport
aElem( rExport
, XML_NAMESPACE_DRAW
, XML_GRADIENT
,
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */