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 "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
,
49 XML_TOK_GRADIENT_START
,
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 }
69 XMLTransGradientStyleImport::XMLTransGradientStyleImport( SvXMLImport
& rImp
)
74 XMLTransGradientStyleImport::~XMLTransGradientStyleImport()
78 bool XMLTransGradientStyleImport::importXML(
79 const uno::Reference
< xml::sax::XAttributeList
>& xAttrList
,
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;
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
},
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
);
124 switch( aTokenMap
.Get( nPrefix
, aStrAttrName
) )
126 case XML_TOK_GRADIENT_NAME
:
128 rStrName
= rStrValue
;
132 case XML_TOK_GRADIENT_DISPLAY_NAME
:
134 aDisplayName
= rStrValue
;
137 case XML_TOK_GRADIENT_STYLE
:
140 if( SvXMLUnitConverter::convertEnum( eValue
, rStrValue
, pXML_GradientStyle_Enum
) )
142 aGradient
.Style
= (awt::GradientStyle
) eValue
;
147 case XML_TOK_GRADIENT_CX
:
148 ::sax::Converter::convertPercent( nTmpValue
, rStrValue
);
149 aGradient
.XOffset
= sal::static_int_cast
< sal_Int16
>(nTmpValue
);
151 case XML_TOK_GRADIENT_CY
:
152 ::sax::Converter::convertPercent( nTmpValue
, rStrValue
);
153 aGradient
.YOffset
= sal::static_int_cast
< sal_Int16
>(nTmpValue
);
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() );
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() );
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
= sal::static_int_cast
< sal_Int16
>(nTmpValue
);
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
,
202 rStrName
= aDisplayName
;
205 bRet
= bHasName
&& bHasStyle
;
214 XMLTransGradientStyleExport::XMLTransGradientStyleExport( SvXMLExport
& rExp
)
219 XMLTransGradientStyleExport::~XMLTransGradientStyleExport()
223 bool XMLTransGradientStyleExport::exportXML(
224 const OUString
& rStrName
,
225 const uno::Any
& rValue
)
228 awt::Gradient aGradient
;
230 if( !rStrName
.isEmpty() )
232 if( rValue
>>= aGradient
)
238 if( !SvXMLUnitConverter::convertEnum( aOut
, aGradient
.Style
, pXML_GradientStyle_Enum
) )
245 bool bEncoded
= false;
246 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_NAME
,
247 rExport
.EncodeStyleName( rStrName
,
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
);
264 ::sax::Converter::convertPercent(aOut
, aGradient
.YOffset
);
265 aStrValue
= aOut
.makeStringAndClear();
266 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_CY
, aStrValue
);
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
);
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
);
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
);
294 ::sax::Converter::convertPercent( aOut
, aGradient
.Border
);
295 aStrValue
= aOut
.makeStringAndClear();
296 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_GRADIENT_BORDER
, aStrValue
);
299 SvXMLElementExport
rElem( rExport
,
300 XML_NAMESPACE_DRAW
, XML_OPACITY
,
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */