LibreOffice Module xmloff (master) 1
FillStyleContext.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 "FillStyleContext.hxx"
21
23
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>
30
33#include <xmloff/xmlimp.hxx>
35#include <xmloff/HatchStyle.hxx>
36#include <xmloff/ImageStyle.hxx>
38#include <xmloff/DashStyle.hxx>
40#include <xmloff/xmltkmap.hxx>
42
43#include <vector>
44
45using namespace ::com::sun::star;
46
47
48XMLGradientStyleContext::XMLGradientStyleContext( SvXMLImport& rImport, sal_Int32 ,
49 const uno::Reference< xml::sax::XFastAttributeList >& xAttrList)
50: SvXMLStyleContext(rImport)
51{
52 // start import
53 XMLGradientStyleImport aGradientStyle( GetImport() );
54 aGradientStyle.importXML( xAttrList, maAny, maStrName );
55}
56
58{
59}
60
61css::uno::Reference<css::xml::sax::XFastContextHandler> XMLGradientStyleContext::createFastChildContext(
62 sal_Int32 nElement,
63 const css::uno::Reference<css::xml::sax::XFastAttributeList>& xAttrList)
64{
65 if (nElement == XML_ELEMENT(LO_EXT, xmloff::token::XML_GRADIENT_STOP))
66 return new XMLGradientStopContext(GetImport(), nElement, xAttrList, maColorStopVec);
67
68 return nullptr;
69}
70
72{
73 // correcting invalid StopOffset values is done at the model. Therefore we import them here
74 // without any change.
75 if (!maColorStopVec.empty())
76 {
77 awt::Gradient2 aGradient;
78 maAny >>= aGradient;
79 aGradient.ColorStops = comphelper::containerToSequence(maColorStopVec);
80 maAny <<= aGradient;
81 }
82
83 uno::Reference< container::XNameContainer > xGradient( GetImport().GetGradientHelper() );
84 try
85 {
86 if(xGradient.is())
87 {
88 if( xGradient->hasByName( maStrName ) )
89 {
90 xGradient->replaceByName( maStrName, maAny );
91 }
92 else
93 {
94 xGradient->insertByName( maStrName, maAny );
95 }
96 }
97 }
98 catch( container::ElementExistException& )
99 {}
100}
101
103{
104 return true;
105}
106
107XMLHatchStyleContext::XMLHatchStyleContext( SvXMLImport& rImport, sal_Int32 /*nElement*/,
108 const uno::Reference< xml::sax::XFastAttributeList >& xAttrList)
109: SvXMLStyleContext(rImport)
110{
111 // start import
112 XMLHatchStyleImport aHatchStyle( GetImport() );
113 aHatchStyle.importXML( xAttrList, maAny, maStrName );
114}
115
117{
118}
119
121{
122 uno::Reference< container::XNameContainer > xHatch( GetImport().GetHatchHelper() );
123
124 try
125 {
126 if(xHatch.is())
127 {
128 if( xHatch->hasByName( maStrName ) )
129 {
130 xHatch->replaceByName( maStrName, maAny );
131 }
132 else
133 {
134 xHatch->insertByName( maStrName, maAny );
135 }
136 }
137 }
138 catch( container::ElementExistException& )
139 {}
140}
141
143{
144 return true;
145}
146
147
148XMLBitmapStyleContext::XMLBitmapStyleContext( SvXMLImport& rImport, sal_Int32 /*nElement*/,
149 const uno::Reference< xml::sax::XFastAttributeList >& xAttrList)
150: SvXMLStyleContext(rImport)
151{
152 // start import
153 XMLImageStyle::importXML( xAttrList, maAny, maStrName, rImport );
154}
155
157{
158}
159
160css::uno::Reference< css::xml::sax::XFastContextHandler > XMLBitmapStyleContext::createFastChildContext(
161 sal_Int32 nElement,
162 const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
163{
165 {
166 OUString sURL;
167 maAny >>= sURL;
168 if( sURL.isEmpty() && !mxBase64Stream.is() )
169 {
170 mxBase64Stream = GetImport().GetStreamForGraphicObjectURLFromBase64();
171 if( mxBase64Stream.is() )
173 }
174 }
175
176 return nullptr;
177}
178
180{
181 if (!maAny.has<uno::Reference<graphic::XGraphic>>() && mxBase64Stream.is())
182 {
183 // No graphic so far? Then see if it's inline.
184 uno::Reference<graphic::XGraphic> xGraphic = GetImport().loadGraphicFromBase64(mxBase64Stream);
185 if (xGraphic.is())
186 {
187 maAny <<= xGraphic;
188 }
189 }
190
191 if (!maAny.has<uno::Reference<graphic::XGraphic>>())
192 return;
193
194 uno::Reference<container::XNameContainer> xBitmapContainer(GetImport().GetBitmapHelper());
195
196 uno::Reference<graphic::XGraphic> xGraphic = maAny.get<uno::Reference<graphic::XGraphic>>();
197 uno::Reference<awt::XBitmap> xBitmap(xGraphic, uno::UNO_QUERY);
198
199 try
200 {
201 if (xBitmapContainer.is())
202 {
203 if (xBitmapContainer->hasByName(maStrName))
204 {
205 xBitmapContainer->replaceByName(maStrName, uno::Any(xBitmap));
206 }
207 else
208 {
209 xBitmapContainer->insertByName(maStrName, uno::Any(xBitmap));
210 }
211 }
212 }
213 catch (container::ElementExistException&)
214 {}
215}
216
218{
219 return true;
220}
221
222
223XMLTransGradientStyleContext::XMLTransGradientStyleContext( SvXMLImport& rImport, sal_Int32 /*nElement*/,
224 const uno::Reference< xml::sax::XFastAttributeList >& xAttrList)
225: SvXMLStyleContext(rImport)
226{
227 // start import
228 XMLTransGradientStyleImport aTransGradientStyle( GetImport() );
229 aTransGradientStyle.importXML( xAttrList, maAny, maStrName );
230}
231
233{
234}
235
236css::uno::Reference<css::xml::sax::XFastContextHandler> XMLTransGradientStyleContext::createFastChildContext(
237 sal_Int32 nElement,
238 const css::uno::Reference<css::xml::sax::XFastAttributeList>& xAttrList)
239{
240 if (nElement == XML_ELEMENT(LO_EXT, xmloff::token::XML_OPACITY_STOP))
241 return new XMLTransparencyStopContext(GetImport(), nElement, xAttrList, maColorStopVec);
242
243 return nullptr;
244}
245
247{
248 uno::Reference< container::XNameContainer > xTransGradient( GetImport().GetTransGradientHelper() );
249
250 // correcting invalid StopOffset values is done at the model. Therefore we import them here
251 // without any change.
252 if (!maColorStopVec.empty())
253 {
254 awt::Gradient2 aGradient;
255 maAny >>= aGradient;
256 aGradient.ColorStops = comphelper::containerToSequence(maColorStopVec);
257 maAny <<= aGradient;
258 }
259
260 try
261 {
262 if(xTransGradient.is())
263 {
264 if( xTransGradient->hasByName( maStrName ) )
265 {
266 xTransGradient->replaceByName( maStrName, maAny );
267 }
268 else
269 {
270 xTransGradient->insertByName( maStrName, maAny );
271 }
272 }
273 }
274 catch( container::ElementExistException& )
275 {}
276}
277
279{
280 return true;
281}
282
284 SvXMLImport& rImport, sal_Int32 nElement,
285 const uno::Reference< xml::sax::XFastAttributeList >& xAttrList,
286 std::vector<awt::ColorStop>& rColorStopVec)
287: SvXMLStyleContext(rImport)
288{
289 if(nElement != XML_ELEMENT(LO_EXT, xmloff::token::XML_OPACITY_STOP))
290 return;
291
292 double fOffset = -1.0;
293 css::rendering::RGBColor aRGBColor; // transparency is handled as gray color
294 for (auto &aIter : sax_fastparser::castToFastAttributeList(xAttrList))
295 {
296 switch(aIter.getToken())
297 {
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()))
301 return;
302 break;
305 {
306 double fOpacity = 1.0;
307 if (!::sax::Converter::convertDouble(fOpacity, aIter.toView()))
308 return;
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;
314 }
315 break;
316 default:
317 XMLOFF_WARN_UNKNOWN("xmloff.style", aIter);
318 }
319 }
320
321 awt::ColorStop aColorStop;
322 aColorStop.StopOffset = fOffset;
323 aColorStop.StopColor = aRGBColor;
324 rColorStopVec.push_back(aColorStop);
325}
326
328{
329}
330
331XMLMarkerStyleContext::XMLMarkerStyleContext( SvXMLImport& rImport, sal_Int32 /*nElement*/,
332 const uno::Reference< xml::sax::XFastAttributeList >& xAttrList)
333: SvXMLStyleContext(rImport)
334{
335 // start import
336 XMLMarkerStyleImport aMarkerStyle( GetImport() );
337 aMarkerStyle.importXML( xAttrList, maAny, maStrName );
338}
339
341{
342}
343
345{
346 uno::Reference< container::XNameContainer > xMarker( GetImport().GetMarkerHelper() );
347
348 try
349 {
350 if(xMarker.is())
351 {
352 if( xMarker->hasByName( maStrName ) )
353 {
354 xMarker->replaceByName( maStrName, maAny );
355 }
356 else
357 {
358 xMarker->insertByName( maStrName, maAny );
359 }
360 }
361 }
362 catch( container::ElementExistException& )
363 {}
364}
365
367{
368 return true;
369}
370
371
372XMLDashStyleContext::XMLDashStyleContext( SvXMLImport& rImport, sal_Int32 /*nElement*/,
373 const uno::Reference< xml::sax::XFastAttributeList >& xAttrList)
374: SvXMLStyleContext(rImport)
375{
376 // start import
377 XMLDashStyleImport aDashStyle( GetImport() );
378 aDashStyle.importXML( xAttrList, maAny, maStrName );
379}
380
382{
383}
384
386{
387 uno::Reference< container::XNameContainer > xDashes( GetImport().GetDashHelper() );
388
389 try
390 {
391 if(xDashes.is())
392 {
393 if( xDashes->hasByName( maStrName ) )
394 {
395 xDashes->replaceByName( maStrName, maAny );
396 }
397 else
398 {
399 xDashes->insertByName( maStrName, maAny );
400 }
401 }
402 }
403 catch( container::ElementExistException& )
404 {}
405}
406
408{
409 return true;
410}
411
412/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
virtual ~XMLBitmapStyleContext() override
XMLBitmapStyleContext(SvXMLImport &rImport, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
css::uno::Reference< css::io::XOutputStream > mxBase64Stream
virtual bool IsTransient() const override
if this method returns true, its parent styles context should not add it to its container.
virtual bool IsTransient() const override
if this method returns true, its parent styles context should not add it to its container.
XMLDashStyleContext(SvXMLImport &rImport, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
virtual ~XMLDashStyleContext() override
void importXML(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, css::uno::Any &rValue, OUString &rStrName)
Definition: DashStyle.cxx:57
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
virtual ~XMLGradientStyleContext() override
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
XMLGradientStyleContext(SvXMLImport &rImport, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
std::vector< css::awt::ColorStop > maColorStopVec
virtual bool IsTransient() const override
if this method returns true, its parent styles context should not add it to its container.
void importXML(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, css::uno::Any &rValue, OUString &rStrName)
virtual bool IsTransient() const override
if this method returns true, its parent styles context should not add it to its container.
virtual ~XMLHatchStyleContext() override
XMLHatchStyleContext(SvXMLImport &rImport, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
void importXML(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, css::uno::Any &rValue, OUString &rStrName)
Definition: HatchStyle.cxx:57
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
virtual ~XMLMarkerStyleContext() override
XMLMarkerStyleContext(SvXMLImport &rImport, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
virtual bool IsTransient() const override
if this method returns true, its parent styles context should not add it to its container.
void importXML(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, css::uno::Any &rValue, OUString &rStrName)
Definition: MarkerStyle.cxx:47
virtual bool IsTransient() const override
if this method returns true, its parent styles context should not add it to its container.
virtual ~XMLTransGradientStyleContext() override
std::vector< css::awt::ColorStop > maColorStopVec
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
XMLTransGradientStyleContext(SvXMLImport &rImport, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
void importXML(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, css::uno::Any &rValue, OUString &rStrName)
XMLTransparencyStopContext(SvXMLImport &rImport, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, std::vector< css::awt::ColorStop > &rColorStopVec)
virtual ~XMLTransparencyStopContext() override
static void convertDouble(OUStringBuffer &rBuffer, double fNumber, bool bWriteUnits, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit)
XMLOFF_DLLPUBLIC bool importXML(css::uno::Reference< css::xml::sax::XFastAttributeList > const &xAttrList, css::uno::Any &rValue, OUString &rStrName, SvXMLImport &rImport)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:114
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97