LibreOffice Module xmloff (master) 1
GradientStyle.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21
22#include <com/sun/star/awt/Gradient.hpp>
23
26
28#include <xmloff/xmluconv.hxx>
30#include <xmloff/xmltoken.hxx>
31#include <rtl/ustrbuf.hxx>
32#include <rtl/ustring.hxx>
33#include <sal/log.hxx>
34#include <xmloff/xmltkmap.hxx>
35#include <xmloff/xmlexp.hxx>
36#include <xmloff/xmlimp.hxx>
37#include <xmloff/xmlement.hxx>
38
39using namespace ::com::sun::star;
40using namespace ::xmloff::token;
41
43{
44 { XML_LINEAR, awt::GradientStyle_LINEAR },
45 { XML_GRADIENTSTYLE_AXIAL, awt::GradientStyle_AXIAL },
46 { XML_GRADIENTSTYLE_RADIAL, awt::GradientStyle_RADIAL },
47 { XML_GRADIENTSTYLE_ELLIPSOID, awt::GradientStyle_ELLIPTICAL },
48 { XML_GRADIENTSTYLE_SQUARE, awt::GradientStyle_SQUARE },
49 { XML_GRADIENTSTYLE_RECTANGULAR, awt::GradientStyle_RECT },
50 { XML_TOKEN_INVALID, awt::GradientStyle(0) }
51};
52
53// Import
55 SvXMLImport& rImp )
56 : rImport(rImp)
57{
58}
59
61 const uno::Reference< xml::sax::XFastAttributeList >& xAttrList,
62 uno::Any& rValue,
63 OUString& rStrName )
64{
65 OUString aDisplayName;
66
67 awt::Gradient aGradient;
68 aGradient.Style = css::awt::GradientStyle_LINEAR;
69 aGradient.StartColor = 0;
70 aGradient.EndColor = 0;
71 aGradient.Angle = 0;
72 aGradient.Border = 0;
73 aGradient.XOffset = 0;
74 aGradient.YOffset = 0;
75 aGradient.StartIntensity = 100;
76 aGradient.EndIntensity = 100;
77 aGradient.StepCount = 0;
78
79 for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
80 {
81 sal_Int32 nTmpValue(0);
82
83 switch( aIter.getToken() )
84 {
86 rStrName = aIter.toString();
87 break;
89 aDisplayName = aIter.toString();
90 break;
92 SvXMLUnitConverter::convertEnum( aGradient.Style, aIter.toView(), pXML_GradientStyle_Enum );
93 break;
94 case XML_ELEMENT(DRAW, XML_CX):
95 ::sax::Converter::convertPercent( nTmpValue, aIter.toView() );
96 aGradient.XOffset = static_cast< sal_Int16 >( nTmpValue );
97 break;
98 case XML_ELEMENT(DRAW, XML_CY):
99 ::sax::Converter::convertPercent( nTmpValue, aIter.toView() );
100 aGradient.YOffset = static_cast< sal_Int16 >( nTmpValue );
101 break;
103 ::sax::Converter::convertColor(aGradient.StartColor, aIter.toView());
104 break;
106 ::sax::Converter::convertColor(aGradient.EndColor, aIter.toView());
107 break;
109 ::sax::Converter::convertPercent( nTmpValue, aIter.toView() );
110 aGradient.StartIntensity = static_cast< sal_Int16 >( nTmpValue );
111 break;
113 ::sax::Converter::convertPercent( nTmpValue, aIter.toView() );
114 aGradient.EndIntensity = static_cast< sal_Int16 >( nTmpValue );
115 break;
117 {
118 auto const cmp12(rImport.GetODFVersion().compareTo(ODFVER_012_TEXT));
119 bool const bSuccess =
120 ::sax::Converter::convertAngle(aGradient.Angle, aIter.toView(),
121 // tdf#89475 try to detect borked OOo angles
122 (cmp12 < 0) || (cmp12 == 0
124 // also for AOO 4.x, assume there won't ever be a 4.2
126 SAL_INFO_IF(!bSuccess, "xmloff.style", "failed to import draw:angle");
127 }
128 break;
130 ::sax::Converter::convertPercent( nTmpValue, aIter.toView() );
131 aGradient.Border = static_cast< sal_Int16 >( nTmpValue );
132 break;
133
134 default:
135 XMLOFF_WARN_UNKNOWN("xmloff.style", aIter);
136 }
137 }
138
139 rValue <<= aGradient;
140
141 if( !aDisplayName.isEmpty() )
142 {
144 aDisplayName );
145 rStrName = aDisplayName;
146 }
147}
148
149// Export
150
152 SvXMLExport& rExp )
153 : rExport(rExp)
154{
155}
156
158 const OUString& rStrName,
159 const uno::Any& rValue )
160{
161 awt::Gradient aGradient;
162
163 if( rStrName.isEmpty() )
164 return;
165
166 if( !(rValue >>= aGradient) )
167 return;
168
169 OUString aStrValue;
170 OUStringBuffer aOut;
171
172 // Style
173 if( !SvXMLUnitConverter::convertEnum( aOut, aGradient.Style, pXML_GradientStyle_Enum ) )
174 return;
175
176 // Name
177 bool bEncoded = false;
179 rExport.EncodeStyleName( rStrName,
180 &bEncoded ) );
181 if( bEncoded )
183 rStrName );
184
185 aStrValue = aOut.makeStringAndClear();
187
188 // Center x/y
189 if( aGradient.Style != awt::GradientStyle_LINEAR &&
190 aGradient.Style != awt::GradientStyle_AXIAL )
191 {
192 ::sax::Converter::convertPercent(aOut, aGradient.XOffset);
193 aStrValue = aOut.makeStringAndClear();
195 ::sax::Converter::convertPercent(aOut, aGradient.YOffset);
196 aStrValue = aOut.makeStringAndClear();
198 }
199
200 // Color start
201 ::sax::Converter::convertColor(aOut, aGradient.StartColor);
202 aStrValue = aOut.makeStringAndClear();
204
205 // Color end
206 ::sax::Converter::convertColor(aOut, aGradient.EndColor);
207 aStrValue = aOut.makeStringAndClear();
209
210 // Intensity start
211 ::sax::Converter::convertPercent(aOut, aGradient.StartIntensity);
212 aStrValue = aOut.makeStringAndClear();
214
215 // Intensity end
216 ::sax::Converter::convertPercent(aOut, aGradient.EndIntensity);
217 aStrValue = aOut.makeStringAndClear();
219
220 // Angle
221 if( aGradient.Style != awt::GradientStyle_RADIAL )
222 {
224 aStrValue = aOut.makeStringAndClear();
226 }
227
228 // Border
229 ::sax::Converter::convertPercent( aOut, aGradient.Border );
230 aStrValue = aOut.makeStringAndClear();
232
233 // Do Write
235 true, false );
236}
237
238/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvXMLEnumMapEntry< awt::GradientStyle > const pXML_GradientStyle_Enum[]
void AddAttribute(sal_uInt16 nPrefix, const OUString &rName, const OUString &rValue)
Definition: xmlexp.cxx:911
SvtSaveOptions::ODFSaneDefaultVersion getSaneDefaultVersion() const
returns the deterministic version for odf export
Definition: xmlexp.cxx:2271
OUString EncodeStyleName(const OUString &rName, bool *pEncoded=nullptr) const
Definition: xmlexp.cxx:1938
static const sal_uInt16 AOO_4x
Definition: xmlimp.hxx:549
bool isGeneratorVersionOlderThan(sal_uInt16 const nOOoVersion, sal_uInt16 const nLOVersion)
depending on whether the generator version indicates LO, compare against either the given LO or given...
Definition: xmlimp.cxx:1852
OUString GetODFVersion() const
Definition: xmlimp.cxx:1864
void AddStyleDisplayName(XmlStyleFamily nFamily, const OUString &rName, const OUString &rDisplayName)
Definition: xmlimp.cxx:1389
sal_uInt16 getGeneratorVersion() const
this checks the build ID and returns
Definition: xmlimp.cxx:1845
static const sal_uInt16 LO_7x
Definition: xmlimp.hxx:561
static bool convertEnum(EnumT &rEnum, std::u16string_view rValue, const SvXMLEnumMapEntry< EnumT > *pMap)
convert string to enum using given enum map, if the enum is not found in the map, this method will re...
Definition: xmluconv.hxx:145
void exportXML(const OUString &rStrName, const css::uno::Any &rValue)
XMLGradientStyleExport(SvXMLExport &rExport)
XMLGradientStyleImport(SvXMLImport &rImport)
void importXML(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, css::uno::Any &rValue, OUString &rStrName)
static bool convertPercent(sal_Int32 &rValue, std::u16string_view rString)
static bool convertColor(sal_Int32 &rColor, std::u16string_view rValue)
static void convertAngle(OUStringBuffer &rBuffer, sal_Int16 nAngle, SvtSaveOptions::ODFSaneDefaultVersion nVersion)
constexpr OUStringLiteral ODFVER_012_TEXT
DRAW
#define SAL_INFO_IF(condition, area, stream)
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
Handling of tokens in XML:
@ XML_GRADIENTSTYLE_ELLIPSOID
Definition: xmltoken.hxx:982
@ XML_GRADIENTSTYLE_AXIAL
Definition: xmltoken.hxx:981
@ XML_GRADIENTSTYLE_SQUARE
Definition: xmltoken.hxx:985
@ XML_GRADIENTSTYLE_RECTANGULAR
Definition: xmltoken.hxx:984
@ XML_GRADIENTSTYLE_RADIAL
Definition: xmltoken.hxx:983
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:114
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97
constexpr sal_uInt16 XML_NAMESPACE_DRAW