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
54 XMLGradientStyleImport::XMLGradientStyleImport(
55  SvXMLImport& rImp )
56  : rImport(rImp)
57 {
58 }
59 
60 XMLGradientStyleImport::~XMLGradientStyleImport()
61 {
62 }
63 
64 void XMLGradientStyleImport::importXML(
65  const uno::Reference< xml::sax::XFastAttributeList >& xAttrList,
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  const OUString aStrValue = aIter.toString();
82 
83  sal_Int32 nTmpValue;
84 
85  switch( aIter.getToken() )
86  {
87  case XML_ELEMENT(DRAW, XML_NAME):
88  rStrName = aStrValue;
89  break;
90  case XML_ELEMENT(DRAW, XML_DISPLAY_NAME):
91  aDisplayName = aStrValue;
92  break;
93  case XML_ELEMENT(DRAW, XML_STYLE):
94  SvXMLUnitConverter::convertEnum( aGradient.Style, aStrValue, pXML_GradientStyle_Enum );
95  break;
96  case XML_ELEMENT(DRAW, XML_CX):
97  ::sax::Converter::convertPercent( nTmpValue, aStrValue );
98  aGradient.XOffset = static_cast< sal_Int16 >( nTmpValue );
99  break;
100  case XML_ELEMENT(DRAW, XML_CY):
101  ::sax::Converter::convertPercent( nTmpValue, aStrValue );
102  aGradient.YOffset = static_cast< sal_Int16 >( nTmpValue );
103  break;
104  case XML_ELEMENT(DRAW, XML_START_COLOR):
105  ::sax::Converter::convertColor(aGradient.StartColor, aStrValue);
106  break;
107  case XML_ELEMENT(DRAW, XML_END_COLOR):
108  ::sax::Converter::convertColor(aGradient.EndColor, aStrValue);
109  break;
110  case XML_ELEMENT(DRAW, XML_START_INTENSITY):
111  ::sax::Converter::convertPercent( nTmpValue, aStrValue );
112  aGradient.StartIntensity = static_cast< sal_Int16 >( nTmpValue );
113  break;
114  case XML_ELEMENT(DRAW, XML_END_INTENSITY):
115  ::sax::Converter::convertPercent( nTmpValue, aStrValue );
116  aGradient.EndIntensity = static_cast< sal_Int16 >( nTmpValue );
117  break;
118  case XML_ELEMENT(DRAW, XML_GRADIENT_ANGLE):
119  {
120  auto const cmp12(rImport.GetODFVersion().compareTo(ODFVER_012_TEXT));
121  bool const bSuccess =
122  ::sax::Converter::convertAngle(aGradient.Angle, aStrValue,
123  // tdf#89475 try to detect borked OOo angles
124  (cmp12 < 0) || (cmp12 == 0
125  && (rImport.isGeneratorVersionOlderThan(SvXMLImport::AOO_4x, SvXMLImport::LO_7x)
126  // also for AOO 4.x, assume there won't ever be a 4.2
127  || rImport.getGeneratorVersion() == SvXMLImport::AOO_4x)));
128  SAL_INFO_IF(!bSuccess, "xmloff.style", "failed to import draw:angle");
129  }
130  break;
131  case XML_ELEMENT(DRAW, XML_BORDER):
132  ::sax::Converter::convertPercent( nTmpValue, aStrValue );
133  aGradient.Border = static_cast< sal_Int16 >( nTmpValue );
134  break;
135 
136  default:
137  XMLOFF_WARN_UNKNOWN("xmloff.style", aIter);
138  }
139  }
140 
141  rValue <<= aGradient;
142 
143  if( !aDisplayName.isEmpty() )
144  {
145  rImport.AddStyleDisplayName( XmlStyleFamily::SD_GRADIENT_ID, rStrName,
146  aDisplayName );
147  rStrName = aDisplayName;
148  }
149 }
150 
151 // Export
152 
153 XMLGradientStyleExport::XMLGradientStyleExport(
154  SvXMLExport& rExp )
155  : rExport(rExp)
156 {
157 }
158 
159 XMLGradientStyleExport::~XMLGradientStyleExport()
160 {
161 }
162 
163 void XMLGradientStyleExport::exportXML(
164  const OUString& rStrName,
165  const uno::Any& rValue )
166 {
167  awt::Gradient aGradient;
168 
169  if( rStrName.isEmpty() )
170  return;
171 
172  if( !(rValue >>= aGradient) )
173  return;
174 
175  OUString aStrValue;
176  OUStringBuffer aOut;
177 
178  // Style
179  if( !SvXMLUnitConverter::convertEnum( aOut, aGradient.Style, pXML_GradientStyle_Enum ) )
180  return;
181 
182  // Name
183  bool bEncoded = false;
185  rExport.EncodeStyleName( rStrName,
186  &bEncoded ) );
187  if( bEncoded )
189  rStrName );
190 
191  aStrValue = aOut.makeStringAndClear();
193 
194  // Center x/y
195  if( aGradient.Style != awt::GradientStyle_LINEAR &&
196  aGradient.Style != awt::GradientStyle_AXIAL )
197  {
198  ::sax::Converter::convertPercent(aOut, aGradient.XOffset);
199  aStrValue = aOut.makeStringAndClear();
201  ::sax::Converter::convertPercent(aOut, aGradient.YOffset);
202  aStrValue = aOut.makeStringAndClear();
204  }
205 
206  // Color start
207  ::sax::Converter::convertColor(aOut, aGradient.StartColor);
208  aStrValue = aOut.makeStringAndClear();
210 
211  // Color end
212  ::sax::Converter::convertColor(aOut, aGradient.EndColor);
213  aStrValue = aOut.makeStringAndClear();
215 
216  // Intensity start
217  ::sax::Converter::convertPercent(aOut, aGradient.StartIntensity);
218  aStrValue = aOut.makeStringAndClear();
220 
221  // Intensity end
222  ::sax::Converter::convertPercent(aOut, aGradient.EndIntensity);
223  aStrValue = aOut.makeStringAndClear();
225 
226  // Angle
227  if( aGradient.Style != awt::GradientStyle_RADIAL )
228  {
230  aStrValue = aOut.makeStringAndClear();
232  }
233 
234  // Border
235  ::sax::Converter::convertPercent( aOut, aGradient.Border );
236  aStrValue = aOut.makeStringAndClear();
238 
239  // Do Write
241  true, false );
242 }
243 
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvXMLEnumMapEntry< awt::GradientStyle > const pXML_GradientStyle_Enum[]
static bool convertEnum(EnumT &rEnum, const OUString &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:128
the SvXMLTypeConverter converts values of various types from their internal representation to the tex...
Definition: xmluconv.hxx:77
#define SAL_INFO_IF(condition, area, stream)
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
static const sal_uInt16 LO_7x
Definition: xmlimp.hxx:555
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:131
void AddAttribute(sal_uInt16 nPrefix, const char *pName, const OUString &rValue)
Definition: xmlexp.cxx:909
static const sal_uInt16 AOO_4x
Definition: xmlimp.hxx:544
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:2297
OUString EncodeStyleName(const OUString &rName, bool *pEncoded=nullptr) const
Definition: xmlexp.cxx:1954
Map an XMLTokenEnum to an enum value.
Definition: ximpshap.hxx:40
static bool convertColor(sal_Int32 &rColor, const OUString &rValue)
Handling of tokens in XML:
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:94
css::uno::Any const SvXMLExport & rExport
Definition: ImageStyle.hxx:38
static bool convertPercent(sal_Int32 &rValue, const OUString &rString)