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 "FillStyleContext.hxx"
22 #include <TransGradientStyle.hxx>
24 #include <com/sun/star/awt/ColorStop.hpp>
25 #include <com/sun/star/awt/Gradient2.hpp>
26 #include <com/sun/star/awt/XBitmap.hpp>
27 #include <com/sun/star/container/XNameContainer.hpp>
28 #include <com/sun/star/graphic/XGraphic.hpp>
29 #include <com/sun/star/rendering/RGBColor.hpp>
31 #include <comphelper/sequence.hxx>
32 #include <sax/tools/converter.hxx>
33 #include <xmloff/xmlimp.hxx>
34 #include <xmloff/GradientStyle.hxx>
35 #include <xmloff/HatchStyle.hxx>
36 #include <xmloff/ImageStyle.hxx>
37 #include <xmloff/MarkerStyle.hxx>
38 #include <xmloff/DashStyle.hxx>
39 #include <xmloff/xmlnamespace.hxx>
40 #include <xmloff/xmltkmap.hxx>
41 #include <xmloff/XMLBase64ImportContext.hxx>
45 using namespace ::com::sun::star
;
48 XMLGradientStyleContext::XMLGradientStyleContext( SvXMLImport
& rImport
, sal_Int32
,
49 const uno::Reference
< xml::sax::XFastAttributeList
>& xAttrList
)
50 : SvXMLStyleContext(rImport
)
53 XMLGradientStyleImport
aGradientStyle( GetImport() );
54 aGradientStyle
.importXML( xAttrList
, maAny
, maStrName
);
57 XMLGradientStyleContext::~XMLGradientStyleContext()
61 css::uno::Reference
<css::xml::sax::XFastContextHandler
> XMLGradientStyleContext::createFastChildContext(
63 const css::uno::Reference
<css::xml::sax::XFastAttributeList
>& xAttrList
)
65 if (nElement
== XML_ELEMENT(LO_EXT
, xmloff::token::XML_GRADIENT_STOP
))
66 return new XMLGradientStopContext(GetImport(), nElement
, xAttrList
, maColorStopVec
);
71 void XMLGradientStyleContext::endFastElement(sal_Int32
)
73 // correcting invalid StopOffset values is done at the model. Therefore we import them here
74 // without any change.
75 if (!maColorStopVec
.empty())
77 awt::Gradient2 aGradient
;
79 aGradient
.ColorStops
= comphelper::containerToSequence(maColorStopVec
);
83 uno::Reference
< container::XNameContainer
> xGradient( GetImport().GetGradientHelper() );
88 if( xGradient
->hasByName( maStrName
) )
90 xGradient
->replaceByName( maStrName
, maAny
);
94 xGradient
->insertByName( maStrName
, maAny
);
98 catch( container::ElementExistException
& )
102 bool XMLGradientStyleContext::IsTransient() const
107 XMLHatchStyleContext::XMLHatchStyleContext( SvXMLImport
& rImport
, sal_Int32
/*nElement*/,
108 const uno::Reference
< xml::sax::XFastAttributeList
>& xAttrList
)
109 : SvXMLStyleContext(rImport
)
112 XMLHatchStyleImport
aHatchStyle( GetImport() );
113 aHatchStyle
.importXML( xAttrList
, maAny
, maStrName
);
116 XMLHatchStyleContext::~XMLHatchStyleContext()
120 void XMLHatchStyleContext::endFastElement(sal_Int32
)
122 uno::Reference
< container::XNameContainer
> xHatch( GetImport().GetHatchHelper() );
128 if( xHatch
->hasByName( maStrName
) )
130 xHatch
->replaceByName( maStrName
, maAny
);
134 xHatch
->insertByName( maStrName
, maAny
);
138 catch( container::ElementExistException
& )
142 bool XMLHatchStyleContext::IsTransient() const
148 XMLBitmapStyleContext::XMLBitmapStyleContext( SvXMLImport
& rImport
, sal_Int32
/*nElement*/,
149 const uno::Reference
< xml::sax::XFastAttributeList
>& xAttrList
)
150 : SvXMLStyleContext(rImport
)
153 XMLImageStyle::importXML( xAttrList
, maAny
, maStrName
, rImport
);
156 XMLBitmapStyleContext::~XMLBitmapStyleContext()
160 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLBitmapStyleContext::createFastChildContext(
162 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& )
164 if( nElement
== XML_ELEMENT(OFFICE
, xmloff::token::XML_BINARY_DATA
) )
168 if( sURL
.isEmpty() && !mxBase64Stream
.is() )
170 mxBase64Stream
= GetImport().GetStreamForGraphicObjectURLFromBase64();
171 if( mxBase64Stream
.is() )
172 return new XMLBase64ImportContext( GetImport(), mxBase64Stream
);
179 void XMLBitmapStyleContext::endFastElement(sal_Int32
)
181 if (!maAny
.has
<uno::Reference
<graphic::XGraphic
>>() && mxBase64Stream
.is())
183 // No graphic so far? Then see if it's inline.
184 uno::Reference
<graphic::XGraphic
> xGraphic
= GetImport().loadGraphicFromBase64(mxBase64Stream
);
191 if (!maAny
.has
<uno::Reference
<graphic::XGraphic
>>())
194 uno::Reference
<container::XNameContainer
> xBitmapContainer(GetImport().GetBitmapHelper());
196 uno::Reference
<graphic::XGraphic
> xGraphic
= maAny
.get
<uno::Reference
<graphic::XGraphic
>>();
197 uno::Reference
<awt::XBitmap
> xBitmap(xGraphic
, uno::UNO_QUERY
);
201 if (xBitmapContainer
.is())
203 if (xBitmapContainer
->hasByName(maStrName
))
205 xBitmapContainer
->replaceByName(maStrName
, uno::Any(xBitmap
));
209 xBitmapContainer
->insertByName(maStrName
, uno::Any(xBitmap
));
213 catch (container::ElementExistException
&)
217 bool XMLBitmapStyleContext::IsTransient() const
223 XMLTransGradientStyleContext::XMLTransGradientStyleContext( SvXMLImport
& rImport
, sal_Int32
/*nElement*/,
224 const uno::Reference
< xml::sax::XFastAttributeList
>& xAttrList
)
225 : SvXMLStyleContext(rImport
)
228 XMLTransGradientStyleImport
aTransGradientStyle( GetImport() );
229 aTransGradientStyle
.importXML( xAttrList
, maAny
, maStrName
);
232 XMLTransGradientStyleContext::~XMLTransGradientStyleContext()
236 css::uno::Reference
<css::xml::sax::XFastContextHandler
> XMLTransGradientStyleContext::createFastChildContext(
238 const css::uno::Reference
<css::xml::sax::XFastAttributeList
>& xAttrList
)
240 if (nElement
== XML_ELEMENT(LO_EXT
, xmloff::token::XML_OPACITY_STOP
))
241 return new XMLTransparencyStopContext(GetImport(), nElement
, xAttrList
, maColorStopVec
);
246 void XMLTransGradientStyleContext::endFastElement(sal_Int32
)
248 uno::Reference
< container::XNameContainer
> xTransGradient( GetImport().GetTransGradientHelper() );
250 // correcting invalid StopOffset values is done at the model. Therefore we import them here
251 // without any change.
252 if (!maColorStopVec
.empty())
254 awt::Gradient2 aGradient
;
256 aGradient
.ColorStops
= comphelper::containerToSequence(maColorStopVec
);
262 if(xTransGradient
.is())
264 if( xTransGradient
->hasByName( maStrName
) )
266 xTransGradient
->replaceByName( maStrName
, maAny
);
270 xTransGradient
->insertByName( maStrName
, maAny
);
274 catch( container::ElementExistException
& )
278 bool XMLTransGradientStyleContext::IsTransient() const
283 XMLTransparencyStopContext::XMLTransparencyStopContext(
284 SvXMLImport
& rImport
, sal_Int32 nElement
,
285 const uno::Reference
< xml::sax::XFastAttributeList
>& xAttrList
,
286 std::vector
<awt::ColorStop
>& rColorStopVec
)
287 : SvXMLStyleContext(rImport
)
289 if(nElement
!= XML_ELEMENT(LO_EXT
, xmloff::token::XML_OPACITY_STOP
))
292 double fOffset
= -1.0;
293 css::rendering::RGBColor aRGBColor
; // transparency is handled as gray color
294 for (auto &aIter
: sax_fastparser::castToFastAttributeList(xAttrList
))
296 switch(aIter
.getToken())
298 case XML_ELEMENT(SVG
, xmloff::token::XML_OFFSET
): // needed??
299 case XML_ELEMENT(SVG_COMPAT
, xmloff::token::XML_OFFSET
):
300 if (!::sax::Converter::convertDouble(fOffset
, aIter
.toView()))
303 case XML_ELEMENT(SVG
, xmloff::token::XML_STOP_OPACITY
):
304 case XML_ELEMENT(SVG_COMPAT
, xmloff::token::XML_STOP_OPACITY
):
306 double fOpacity
= 1.0;
307 if (!::sax::Converter::convertDouble(fOpacity
, aIter
.toView()))
309 // Transparency is gray, full transparent is (1|1|1).
310 double fGrayComponent
= std::clamp
<double>(1.0 - fOpacity
, 0.0, 1.0);
311 aRGBColor
.Red
= fGrayComponent
;
312 aRGBColor
.Green
= fGrayComponent
;
313 aRGBColor
.Blue
= fGrayComponent
;
317 XMLOFF_WARN_UNKNOWN("xmloff.style", aIter
);
321 awt::ColorStop aColorStop
;
322 aColorStop
.StopOffset
= fOffset
;
323 aColorStop
.StopColor
= aRGBColor
;
324 rColorStopVec
.push_back(aColorStop
);
327 XMLTransparencyStopContext::~XMLTransparencyStopContext()
331 XMLMarkerStyleContext::XMLMarkerStyleContext( SvXMLImport
& rImport
, sal_Int32
/*nElement*/,
332 const uno::Reference
< xml::sax::XFastAttributeList
>& xAttrList
)
333 : SvXMLStyleContext(rImport
)
336 XMLMarkerStyleImport
aMarkerStyle( GetImport() );
337 aMarkerStyle
.importXML( xAttrList
, maAny
, maStrName
);
340 XMLMarkerStyleContext::~XMLMarkerStyleContext()
344 void XMLMarkerStyleContext::endFastElement(sal_Int32
)
346 uno::Reference
< container::XNameContainer
> xMarker( GetImport().GetMarkerHelper() );
352 if( xMarker
->hasByName( maStrName
) )
354 xMarker
->replaceByName( maStrName
, maAny
);
358 xMarker
->insertByName( maStrName
, maAny
);
362 catch( container::ElementExistException
& )
366 bool XMLMarkerStyleContext::IsTransient() const
372 XMLDashStyleContext::XMLDashStyleContext( SvXMLImport
& rImport
, sal_Int32
/*nElement*/,
373 const uno::Reference
< xml::sax::XFastAttributeList
>& xAttrList
)
374 : SvXMLStyleContext(rImport
)
377 XMLDashStyleImport
aDashStyle( GetImport() );
378 aDashStyle
.importXML( xAttrList
, maAny
, maStrName
);
381 XMLDashStyleContext::~XMLDashStyleContext()
385 void XMLDashStyleContext::endFastElement(sal_Int32
)
387 uno::Reference
< container::XNameContainer
> xDashes( GetImport().GetDashHelper() );
393 if( xDashes
->hasByName( maStrName
) )
395 xDashes
->replaceByName( maStrName
, maAny
);
399 xDashes
->insertByName( maStrName
, maAny
);
403 catch( container::ElementExistException
& )
407 bool XMLDashStyleContext::IsTransient() const
412 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */