LibreOffice Module xmloff (master) 1
TransGradientStyle.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 <rtl/ustrbuf.hxx>
31#include <rtl/ustring.hxx>
32#include <sal/log.hxx>
33#include <tools/color.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;
40
41using namespace ::xmloff::token;
42
44{
45 { XML_LINEAR, awt::GradientStyle_LINEAR },
46 { XML_GRADIENTSTYLE_AXIAL, awt::GradientStyle_AXIAL },
47 { XML_GRADIENTSTYLE_RADIAL, awt::GradientStyle_RADIAL },
48 { XML_GRADIENTSTYLE_ELLIPSOID, awt::GradientStyle_ELLIPTICAL },
49 { XML_GRADIENTSTYLE_SQUARE, awt::GradientStyle_SQUARE },
50 { XML_GRADIENTSTYLE_RECTANGULAR, awt::GradientStyle_RECT },
51 { XML_TOKEN_INVALID, awt::GradientStyle(0) }
52};
53
54// Import
55
57 : rImport(rImp)
58{
59}
60
62 const uno::Reference< xml::sax::XFastAttributeList >& xAttrList,
63 uno::Any& rValue,
64 OUString& rStrName )
65{
66 OUString aDisplayName;
67
68 awt::Gradient aGradient;
69 aGradient.XOffset = 0;
70 aGradient.YOffset = 0;
71 aGradient.StartIntensity = 100;
72 aGradient.EndIntensity = 100;
73 aGradient.Angle = 0;
74 aGradient.Border = 0;
75
76 for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
77 {
78 sal_Int32 nTmpValue;
79
80 switch( aIter.getToken() )
81 {
83 {
84 rStrName = aIter.toString();
85 }
86 break;
88 {
89 aDisplayName = aIter.toString();
90 }
91 break;
93 {
94 SvXMLUnitConverter::convertEnum( aGradient.Style, aIter.toView(), pXML_GradientStyle_Enum );
95 }
96 break;
97 case XML_ELEMENT(DRAW, XML_CX):
98 ::sax::Converter::convertPercent( nTmpValue, aIter.toView() );
99 aGradient.XOffset = sal::static_int_cast< sal_Int16 >(nTmpValue);
100 break;
101 case XML_ELEMENT(DRAW, XML_CY):
102 ::sax::Converter::convertPercent( nTmpValue, aIter.toView() );
103 aGradient.YOffset = sal::static_int_cast< sal_Int16 >(nTmpValue);
104 break;
106 {
107 sal_Int32 aStartTransparency;
108 ::sax::Converter::convertPercent( aStartTransparency, aIter.toView() );
109
110 sal_uInt8 n = sal::static_int_cast< sal_uInt8 >(
111 ( (100 - aStartTransparency) * 255 ) / 100 );
112
113 Color aColor( n, n, n );
114 aGradient.StartColor = static_cast<sal_Int32>( aColor );
115 }
116 break;
117 case XML_ELEMENT(DRAW, XML_END):
118 {
119 sal_Int32 aEndTransparency;
120 ::sax::Converter::convertPercent( aEndTransparency, aIter.toView() );
121
122 sal_uInt8 n = sal::static_int_cast< sal_uInt8 >(
123 ( (100 - aEndTransparency) * 255 ) / 100 );
124
125 Color aColor( n, n, n );
126 aGradient.EndColor = static_cast<sal_Int32>( aColor );
127 }
128 break;
130 {
131 auto const cmp12(rImport.GetODFVersion().compareTo(ODFVER_012_TEXT));
132 bool const bSuccess =
133 ::sax::Converter::convertAngle(aGradient.Angle, aIter.toView(),
134 // tdf#89475 try to detect borked OOo angles
135 (cmp12 < 0) || (cmp12 == 0
137 // also for AOO 4.x, assume there won't ever be a 4.2
139 SAL_INFO_IF(!bSuccess, "xmloff.style", "failed to import draw:angle");
140 }
141 break;
143 ::sax::Converter::convertPercent( nTmpValue, aIter.toView() );
144 aGradient.Border = sal::static_int_cast< sal_Int16 >(nTmpValue);
145 break;
146
147 default:
148 XMLOFF_WARN_UNKNOWN("xmloff.style", aIter);
149 }
150 }
151
152 rValue <<= aGradient;
153
154 if( !aDisplayName.isEmpty() )
155 {
157 aDisplayName );
158 rStrName = aDisplayName;
159 }
160}
161
162// Export
163
165 : rExport(rExp)
166{
167}
168
170 const OUString& rStrName,
171 const uno::Any& rValue )
172{
173 awt::Gradient aGradient;
174
175 if( rStrName.isEmpty() )
176 return;
177
178 if( !(rValue >>= aGradient) )
179 return;
180
181 OUString aStrValue;
182 OUStringBuffer aOut;
183
184 // Style
185 if( !SvXMLUnitConverter::convertEnum( aOut, aGradient.Style, pXML_GradientStyle_Enum ) )
186 return;
187
188 // Name
189 bool bEncoded = false;
191 rExport.EncodeStyleName( rStrName,
192 &bEncoded ) );
193 if( bEncoded )
195 rStrName );
196
197 aStrValue = aOut.makeStringAndClear();
199
200 // Center x/y
201 if( aGradient.Style != awt::GradientStyle_LINEAR &&
202 aGradient.Style != awt::GradientStyle_AXIAL )
203 {
204 ::sax::Converter::convertPercent(aOut, aGradient.XOffset);
205 aStrValue = aOut.makeStringAndClear();
207
208 ::sax::Converter::convertPercent(aOut, aGradient.YOffset);
209 aStrValue = aOut.makeStringAndClear();
211 }
212
213 // Transparency start
214 Color aColor(ColorTransparency, aGradient.StartColor);
215 sal_Int32 aStartValue = 100 - static_cast<sal_Int32>(((aColor.GetRed() + 1) * 100) / 255);
216 ::sax::Converter::convertPercent( aOut, aStartValue );
217 aStrValue = aOut.makeStringAndClear();
219
220 // Transparency end
221 aColor = Color(ColorTransparency, aGradient.EndColor);
222 sal_Int32 aEndValue = 100 - static_cast<sal_Int32>(((aColor.GetRed() + 1) * 100) / 255);
223 ::sax::Converter::convertPercent( aOut, aEndValue );
224 aStrValue = aOut.makeStringAndClear();
226
227 // Angle
228 if( aGradient.Style != awt::GradientStyle_RADIAL )
229 {
231 aStrValue = aOut.makeStringAndClear();
233 }
234
235 // Border
236 ::sax::Converter::convertPercent( aOut, aGradient.Border );
237 aStrValue = aOut.makeStringAndClear();
239
240 // Do Write
243 true, false );
244}
245
246/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvXMLEnumMapEntry< awt::GradientStyle > const pXML_GradientStyle_Enum[]
sal_uInt8 GetRed() const
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:550
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:1854
OUString GetODFVersion() const
Definition: xmlimp.cxx:1866
void AddStyleDisplayName(XmlStyleFamily nFamily, const OUString &rName, const OUString &rDisplayName)
Definition: xmlimp.cxx:1391
sal_uInt16 getGeneratorVersion() const
this checks the build ID and returns
Definition: xmlimp.cxx:1847
static const sal_uInt16 LO_7x
Definition: xmlimp.hxx:562
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)
XMLTransGradientStyleExport(SvXMLExport &rExport)
void importXML(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, css::uno::Any &rValue, OUString &rStrName)
XMLTransGradientStyleImport(SvXMLImport &rImport)
static bool convertPercent(sal_Int32 &rValue, std::u16string_view rString)
static void convertAngle(OUStringBuffer &rBuffer, sal_Int16 nAngle, SvtSaveOptions::ODFSaneDefaultVersion nVersion)
ColorTransparency
constexpr OUStringLiteral ODFVER_012_TEXT
DRAW
sal_Int64 n
#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:983
@ XML_GRADIENTSTYLE_AXIAL
Definition: xmltoken.hxx:982
@ XML_GRADIENTSTYLE_SQUARE
Definition: xmltoken.hxx:986
@ XML_GRADIENTSTYLE_RECTANGULAR
Definition: xmltoken.hxx:985
@ XML_GRADIENTSTYLE_RADIAL
Definition: xmltoken.hxx:984
unsigned char sal_uInt8
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:114
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:98
constexpr sal_uInt16 XML_NAMESPACE_DRAW