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 
20 #include <xmloff/GradientStyle.hxx>
21 
22 #include <com/sun/star/awt/Gradient.hpp>
23 
24 #include <sax/tools/converter.hxx>
26 
27 #include <xmloff/namespacemap.hxx>
28 #include <xmloff/xmluconv.hxx>
29 #include <xmloff/xmlnamespace.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 
39 using namespace ::com::sun::star;
40 using 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 {
62 }
63 
65  const uno::Reference< xml::sax::XFastAttributeList >& xAttrList,
66  uno::Any& rValue,
67  OUString& rStrName )
68 {
69  OUString aDisplayName;
70 
71  awt::Gradient aGradient;
72  aGradient.XOffset = 0;
73  aGradient.YOffset = 0;
74  aGradient.StartIntensity = 100;
75  aGradient.EndIntensity = 100;
76  aGradient.Angle = 0;
77  aGradient.Border = 0;
78 
79  for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
80  {
81  sal_Int32 nTmpValue;
82 
83  switch( aIter.getToken() )
84  {
85  case XML_ELEMENT(DRAW, XML_NAME):
86  rStrName = aIter.toString();
87  break;
89  aDisplayName = aIter.toString();
90  break;
91  case XML_ELEMENT(DRAW, XML_STYLE):
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.toString());
104  break;
106  ::sax::Converter::convertColor(aGradient.EndColor, aIter.toString());
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(u"" 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;
129  case XML_ELEMENT(DRAW, XML_BORDER):
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 {
159 }
160 
162  const OUString& rStrName,
163  const uno::Any& rValue )
164 {
165  awt::Gradient aGradient;
166 
167  if( rStrName.isEmpty() )
168  return;
169 
170  if( !(rValue >>= aGradient) )
171  return;
172 
173  OUString aStrValue;
174  OUStringBuffer aOut;
175 
176  // Style
177  if( !SvXMLUnitConverter::convertEnum( aOut, aGradient.Style, pXML_GradientStyle_Enum ) )
178  return;
179 
180  // Name
181  bool bEncoded = false;
183  rExport.EncodeStyleName( rStrName,
184  &bEncoded ) );
185  if( bEncoded )
187  rStrName );
188 
189  aStrValue = aOut.makeStringAndClear();
191 
192  // Center x/y
193  if( aGradient.Style != awt::GradientStyle_LINEAR &&
194  aGradient.Style != awt::GradientStyle_AXIAL )
195  {
196  ::sax::Converter::convertPercent(aOut, aGradient.XOffset);
197  aStrValue = aOut.makeStringAndClear();
199  ::sax::Converter::convertPercent(aOut, aGradient.YOffset);
200  aStrValue = aOut.makeStringAndClear();
202  }
203 
204  // Color start
205  ::sax::Converter::convertColor(aOut, aGradient.StartColor);
206  aStrValue = aOut.makeStringAndClear();
208 
209  // Color end
210  ::sax::Converter::convertColor(aOut, aGradient.EndColor);
211  aStrValue = aOut.makeStringAndClear();
213 
214  // Intensity start
215  ::sax::Converter::convertPercent(aOut, aGradient.StartIntensity);
216  aStrValue = aOut.makeStringAndClear();
218 
219  // Intensity end
220  ::sax::Converter::convertPercent(aOut, aGradient.EndIntensity);
221  aStrValue = aOut.makeStringAndClear();
223 
224  // Angle
225  if( aGradient.Style != awt::GradientStyle_RADIAL )
226  {
228  aStrValue = aOut.makeStringAndClear();
230  }
231 
232  // Border
233  ::sax::Converter::convertPercent( aOut, aGradient.Border );
234  aStrValue = aOut.makeStringAndClear();
236 
237  // Do Write
239  true, false );
240 }
241 
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvXMLEnumMapEntry< awt::GradientStyle > const pXML_GradientStyle_Enum[]
void exportXML(const OUString &rStrName, const css::uno::Any &rValue)
#define SAL_INFO_IF(condition, area, stream)
#define ODFVER_012_TEXT
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
static const sal_uInt16 LO_7x
Definition: xmlimp.hxx:560
sal_uInt16 getGeneratorVersion() const
this checks the build ID and returns
Definition: xmlimp.cxx:1834
DRAW
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:113
void AddAttribute(sal_uInt16 nPrefix, const char *pName, const OUString &rValue)
Definition: xmlexp.cxx:938
static const sal_uInt16 AOO_4x
Definition: xmlimp.hxx:548
static void convertAngle(OUStringBuffer &rBuffer, sal_Int16 nAngle, SvtSaveOptions::ODFSaneDefaultVersion nVersion)
constexpr sal_uInt16 XML_NAMESPACE_DRAW
SvtSaveOptions::ODFSaneDefaultVersion getSaneDefaultVersion() const
returns the deterministic version for odf export
Definition: xmlexp.cxx:2323
float u
static bool convertColor(sal_Int32 &rColor, std::u16string_view rValue)
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)
OUString EncodeStyleName(const OUString &rName, bool *pEncoded=nullptr) const
Definition: xmlexp.cxx:1981
Map an XMLTokenEnum to an enum value.
Definition: ximpshap.hxx:39
OUString GetODFVersion() const
Definition: xmlimp.cxx:1853
XMLGradientStyleExport(SvXMLExport &rExport)
Handling of tokens in XML:
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97
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 AddStyleDisplayName(XmlStyleFamily nFamily, const OUString &rName, const OUString &rDisplayName)
Definition: xmlimp.cxx:1375
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:1841