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 <xmloff/GradientStyle.hxx>
22 #include <com/sun/star/awt/Gradient2.hpp>
24 #include <comphelper/documentconstants.hxx>
25 #include <rtl/ustrbuf.hxx>
26 #include <rtl/ustring.hxx>
27 #include <sal/log.hxx>
28 #include <sax/tools/converter.hxx>
29 #include <xmloff/xmlement.hxx>
30 #include <xmloff/xmlexp.hxx>
31 #include <xmloff/xmlimp.hxx>
32 #include <xmloff/xmlnamespace.hxx>
33 #include <xmloff/xmltoken.hxx>
34 #include <xmloff/xmluconv.hxx>
35 #include <basegfx/utils/bgradient.hxx>
36 #include <docmodel/uno/UnoGradientTools.hxx>
38 using namespace ::com::sun::star
;
39 using namespace ::xmloff::token
;
41 SvXMLEnumMapEntry
<awt::GradientStyle
> const pXML_GradientStyle_Enum
[] =
43 { XML_LINEAR
, awt::GradientStyle_LINEAR
},
44 { XML_GRADIENTSTYLE_AXIAL
, awt::GradientStyle_AXIAL
},
45 { XML_GRADIENTSTYLE_RADIAL
, awt::GradientStyle_RADIAL
},
46 { XML_GRADIENTSTYLE_ELLIPSOID
, awt::GradientStyle_ELLIPTICAL
},
47 { XML_GRADIENTSTYLE_SQUARE
, awt::GradientStyle_SQUARE
},
48 { XML_GRADIENTSTYLE_RECTANGULAR
, awt::GradientStyle_RECT
},
49 { XML_TOKEN_INVALID
, awt::GradientStyle(0) }
53 XMLGradientStyleImport::XMLGradientStyleImport(
59 void XMLGradientStyleImport::importXML(
60 const uno::Reference
< xml::sax::XFastAttributeList
>& xAttrList
,
64 OUString aDisplayName
;
66 awt::Gradient2 aGradient
;
67 aGradient
.Style
= css::awt::GradientStyle_LINEAR
;
68 aGradient
.StartColor
= 0;
69 aGradient
.EndColor
= 0;
72 aGradient
.XOffset
= 0;
73 aGradient
.YOffset
= 0;
74 aGradient
.StartIntensity
= 100;
75 aGradient
.EndIntensity
= 100;
76 aGradient
.StepCount
= 0;
78 for (auto &aIter
: sax_fastparser::castToFastAttributeList( xAttrList
))
80 sal_Int32
nTmpValue(0);
82 switch( aIter
.getToken() )
84 case XML_ELEMENT(DRAW
, XML_NAME
):
85 rStrName
= aIter
.toString();
87 case XML_ELEMENT(DRAW
, XML_DISPLAY_NAME
):
88 aDisplayName
= aIter
.toString();
90 case XML_ELEMENT(DRAW
, XML_STYLE
):
91 SvXMLUnitConverter::convertEnum( aGradient
.Style
, aIter
.toView(), pXML_GradientStyle_Enum
);
93 case XML_ELEMENT(DRAW
, XML_CX
):
94 ::sax::Converter::convertPercent( nTmpValue
, aIter
.toView() );
95 aGradient
.XOffset
= static_cast< sal_Int16
>( nTmpValue
);
97 case XML_ELEMENT(DRAW
, XML_CY
):
98 ::sax::Converter::convertPercent( nTmpValue
, aIter
.toView() );
99 aGradient
.YOffset
= static_cast< sal_Int16
>( nTmpValue
);
101 case XML_ELEMENT(DRAW
, XML_START_COLOR
):
102 ::sax::Converter::convertColor(aGradient
.StartColor
, aIter
.toView());
104 case XML_ELEMENT(DRAW
, XML_END_COLOR
):
105 ::sax::Converter::convertColor(aGradient
.EndColor
, aIter
.toView());
107 case XML_ELEMENT(DRAW
, XML_START_INTENSITY
):
108 ::sax::Converter::convertPercent( nTmpValue
, aIter
.toView() );
109 aGradient
.StartIntensity
= static_cast< sal_Int16
>( nTmpValue
);
111 case XML_ELEMENT(DRAW
, XML_END_INTENSITY
):
112 ::sax::Converter::convertPercent( nTmpValue
, aIter
.toView() );
113 aGradient
.EndIntensity
= static_cast< sal_Int16
>( nTmpValue
);
115 case XML_ELEMENT(DRAW
, XML_GRADIENT_ANGLE
):
117 auto const cmp12(m_rImport
.GetODFVersion().compareTo(ODFVER_012_TEXT
));
118 // tdf#89475 try to detect borked OOo angles
119 bool const bIsWrongOOo10thDegAngle(
122 && (m_rImport
.isGeneratorVersionOlderThan(SvXMLImport::AOO_4x
,
124 // also for AOO 4.x, assume there won't ever be a 4.2
125 || m_rImport
.getGeneratorVersion() == SvXMLImport::AOO_4x
)));
127 bool const bSuccess
= ::sax::Converter::convert10thDegAngle(
128 nAngle
, aIter
.toView(), bIsWrongOOo10thDegAngle
);
130 { // limit to valid range [0..3600[
131 nAngle
= nAngle
% 3600;
134 aGradient
.Angle
= nAngle
;
136 SAL_INFO_IF(!bSuccess
, "xmloff.style", "failed to import draw:angle");
139 case XML_ELEMENT(DRAW
, XML_BORDER
):
140 ::sax::Converter::convertPercent( nTmpValue
, aIter
.toView() );
141 aGradient
.Border
= static_cast< sal_Int16
>( nTmpValue
);
145 XMLOFF_WARN_UNKNOWN("xmloff.style", aIter
);
149 rValue
<<= aGradient
;
151 if( !aDisplayName
.isEmpty() )
153 m_rImport
.AddStyleDisplayName( XmlStyleFamily::SD_GRADIENT_ID
, rStrName
,
155 rStrName
= aDisplayName
;
159 XMLGradientStopContext::XMLGradientStopContext(
160 SvXMLImport
& rImport
, sal_Int32 nElement
,
161 const uno::Reference
< xml::sax::XFastAttributeList
>& xAttrList
,
162 std::vector
<awt::ColorStop
>& rColorStopVec
)
163 : SvXMLImportContext(rImport
)
165 if(nElement
!= XML_ELEMENT(LO_EXT
, xmloff::token::XML_GRADIENT_STOP
))
168 double fOffset
= -1.0;
170 OUString sColorValue
;
171 // First collect all attributes
172 for (auto &aIter
: sax_fastparser::castToFastAttributeList(xAttrList
))
174 switch(aIter
.getToken())
176 case XML_ELEMENT(SVG
, xmloff::token::XML_OFFSET
): // needed??
177 case XML_ELEMENT(SVG_COMPAT
, xmloff::token::XML_OFFSET
):
178 if (!::sax::Converter::convertDouble(fOffset
, aIter
.toView()))
181 case XML_ELEMENT(LO_EXT
, xmloff::token::XML_COLOR_VALUE
):
182 sColorValue
= aIter
.toString();
183 if (sColorValue
.isEmpty())
186 case XML_ELEMENT(LO_EXT
, xmloff::token::XML_COLOR_TYPE
):
187 sColorType
= aIter
.toString();
188 if (sColorType
.isEmpty())
192 XMLOFF_WARN_UNKNOWN("xmloff.style", aIter
);
196 // As of LO 7.6.0 only "rgb" is implemented.
197 if (sColorType
!= u
"rgb")
200 // Type "rgb" requires kind color-value="#rrggbb".
202 if (!::sax::Converter::convertColor(aColor
, sColorValue
))
205 // All attribute values OK. Generate ColorStop.
206 css::rendering::RGBColor aRGBColor
;
207 aRGBColor
.Red
= aColor
.GetRed() / 255.0;
208 aRGBColor
.Green
= aColor
.GetGreen() / 255.0;
209 aRGBColor
.Blue
= aColor
.GetBlue() / 255.0;
211 awt::ColorStop aColorStop
;
212 aColorStop
.StopOffset
= fOffset
;
213 aColorStop
.StopColor
= aRGBColor
;
214 rColorStopVec
.push_back(aColorStop
);
217 XMLGradientStopContext::~XMLGradientStopContext()
223 XMLGradientStyleExport::XMLGradientStyleExport(
229 void XMLGradientStyleExport::exportXML(
230 const OUString
& rStrName
,
231 const uno::Any
& rValue
)
233 if( rStrName
.isEmpty() )
236 if (!rValue
.has
<css::awt::Gradient2
>() && !rValue
.has
<css::awt::Gradient
>())
239 basegfx::BGradient aGradient
= model::gradient::getFromAny(rValue
);
241 // Export of axial gradient to OOXML produces a symmetrical linear multi-color gradient. Import
242 // does not regenerate it as 'axial' because that is not needed for MCGR. For export to ODF we
243 // try to regenerate 'axial' for to get a better compatibility with LO versions before MCGR.
244 aGradient
.tryToConvertToAxial();
246 // MCGR: For better compatibility with LO versions before MCGR, try
247 // to re-create a 'border' value based on the existing gradient stops.
248 // With MCGR we do not need 'border' anymore in quite some cases since
249 // no Start/EndColor at 0.0 resp. 1.0 is explicitly needed. Since we
250 // (unfortunately need to) internally continue to support border
251 // anyways it does no harm to fallback to use the border value - if
252 // there is an equivalent representation as this helper checks for.
253 // For exports that do not support 'border' this will be adapted as
254 // needed (see tryToApplyBorder()).
255 aGradient
.tryToRecreateBorder(nullptr);
261 if( !SvXMLUnitConverter::convertEnum( aOut
, aGradient
.GetGradientStyle(), pXML_GradientStyle_Enum
) )
265 bool bEncoded
= false;
266 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_NAME
,
267 m_rExport
.EncodeStyleName( rStrName
,
270 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DISPLAY_NAME
,
273 aStrValue
= aOut
.makeStringAndClear();
274 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_STYLE
, aStrValue
);
277 if( aGradient
.GetGradientStyle() != awt::GradientStyle_LINEAR
&&
278 aGradient
.GetGradientStyle() != awt::GradientStyle_AXIAL
)
280 ::sax::Converter::convertPercent(aOut
, aGradient
.GetXOffset());
281 aStrValue
= aOut
.makeStringAndClear();
282 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_CX
, aStrValue
);
283 ::sax::Converter::convertPercent(aOut
, aGradient
.GetYOffset());
284 aStrValue
= aOut
.makeStringAndClear();
285 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_CY
, aStrValue
);
288 // prep Start/EndColor, default black
289 basegfx::BColor aStartColor
;
290 basegfx::BColor aEndColor
;
292 if (!aGradient
.GetColorStops().empty())
294 aStartColor
= aGradient
.GetColorStops().front().getStopColor();
295 aEndColor
= aGradient
.GetColorStops().back().getStopColor();
299 ::sax::Converter::convertColor(aOut
, Color(aStartColor
));
300 aStrValue
= aOut
.makeStringAndClear();
301 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_START_COLOR
, aStrValue
);
304 ::sax::Converter::convertColor(aOut
, Color(aEndColor
));
305 aStrValue
= aOut
.makeStringAndClear();
306 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_END_COLOR
, aStrValue
);
309 ::sax::Converter::convertPercent(aOut
, aGradient
.GetStartIntens());
310 aStrValue
= aOut
.makeStringAndClear();
311 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_START_INTENSITY
, aStrValue
);
314 ::sax::Converter::convertPercent(aOut
, aGradient
.GetEndIntens());
315 aStrValue
= aOut
.makeStringAndClear();
316 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_END_INTENSITY
, aStrValue
);
319 if( aGradient
.GetGradientStyle() != awt::GradientStyle_RADIAL
)
321 // true: wrong, but backward compatible with OOo/LO < 4.4
322 // false: OFFICE-3774 tdf#89475 write valid ODF 1.2 angle; needs LO 4.4 to import
323 bool bIsWrongOOo10thDegAngle(m_rExport
.getSaneDefaultVersion() < SvtSaveOptions::ODFSVER_012
324 || m_rExport
.getSaneDefaultVersion() == SvtSaveOptions::ODFSVER_012_EXT_COMPAT
);
325 ::sax::Converter::convert10thDegAngle(aOut
, static_cast<sal_Int16
>(aGradient
.GetAngle()),
326 bIsWrongOOo10thDegAngle
);
327 aStrValue
= aOut
.makeStringAndClear();
328 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_GRADIENT_ANGLE
, aStrValue
);
332 ::sax::Converter::convertPercent( aOut
, aGradient
.GetBorder() );
333 aStrValue
= aOut
.makeStringAndClear();
334 m_rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_BORDER
, aStrValue
);
336 // ctor writes start tag. End-tag is written by destructor at block end.
337 SvXMLElementExport
aElem( m_rExport
, XML_NAMESPACE_DRAW
, XML_GRADIENT
,
340 // Write child elements <loext:gradient-stop>
341 // Do not export in standard ODF 1.3 or older.
342 if ((m_rExport
.getSaneDefaultVersion() & SvtSaveOptions::ODFSVER_EXTENDED
) == 0)
345 if (aGradient
.GetColorStops().empty())
348 double fPreviousOffset
= 0.0;
349 for (const auto& aCandidate
: aGradient
.GetColorStops())
351 // Attribute svg:offset. Make sure offsets are increasing.
352 double fOffset
= std::clamp
<double>(aCandidate
.getStopOffset(), 0.0, 1.0);
353 if (fOffset
< fPreviousOffset
)
354 fOffset
= fPreviousOffset
;
355 m_rExport
.AddAttribute(XML_NAMESPACE_SVG
, XML_OFFSET
, OUString::number(fOffset
));
356 fPreviousOffset
= fOffset
;
358 // As of LO 7.6.0 only color-type="rgb" is implemented.
359 m_rExport
.AddAttribute(XML_NAMESPACE_LO_EXT
, XML_COLOR_TYPE
, u
"rgb"_ustr
);
361 // Attribute loext:color-value, data type color, that is #rrggbb.
362 const basegfx::BColor
aDecimalColor(aCandidate
.getStopColor());
363 ::Color
aToolsColor(std::clamp
<sal_uInt8
>(std::round(aDecimalColor
.getRed() * 255.0), 0, 255),
364 std::clamp
<sal_uInt8
>(std::round(aDecimalColor
.getGreen() * 255.0), 0, 255),
365 std::clamp
<sal_uInt8
>(std::round(aDecimalColor
.getBlue() * 255.0), 0, 255));
366 m_rExport
.AddAttribute(XML_NAMESPACE_LO_EXT
, XML_COLOR_VALUE
,
367 rtl::OUStringChar('#') + aToolsColor
.AsRGBHexString());
369 // write gradient stop element
370 SvXMLElementExport
aStopElement(m_rExport
, XML_NAMESPACE_LO_EXT
, XML_GRADIENT_STOP
, true, true);
374 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */