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 <svgio/svgreader/svgimagenode.hxx>
21 #include <svgio/svgreader/svgdocument.hxx>
22 #include <sax/tools/converter.hxx>
23 #include <tools/stream.hxx>
24 #include <vcl/bitmapex.hxx>
25 #include <vcl/graphicfilter.hxx>
26 #include <basegfx/matrix/b2dhommatrixtools.hxx>
27 #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
28 #include <drawinglayer/primitive2d/groupprimitive2d.hxx>
29 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
30 #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
31 #include <basegfx/polygon/b2dpolygontools.hxx>
32 #include <basegfx/polygon/b2dpolygon.hxx>
33 #include <rtl/uri.hxx>
34 #include <drawinglayer/geometry/viewinformation2d.hxx>
40 SvgImageNode::SvgImageNode(
41 SvgDocument
& rDocument
,
43 : SvgNode(SVGTokenRect
, rDocument
, pParent
),
44 maSvgStyleAttributes(*this),
58 SvgImageNode::~SvgImageNode()
60 if(mpaTransform
) delete mpaTransform
;
63 const SvgStyleAttributes
* SvgImageNode::getSvgStyleAttributes() const
65 return checkForCssStyle(OUString("image"), maSvgStyleAttributes
);
68 void SvgImageNode::parseAttribute(const OUString
& rTokenName
, SVGToken aSVGToken
, const OUString
& aContent
)
71 SvgNode::parseAttribute(rTokenName
, aSVGToken
, aContent
);
73 // read style attributes
74 maSvgStyleAttributes
.parseStyleAttribute(rTokenName
, aSVGToken
, aContent
, false);
81 readLocalCssStyle(aContent
);
84 case SVGTokenPreserveAspectRatio
:
86 setSvgAspectRatio(readSvgAspectRatio(aContent
));
89 case SVGTokenTransform
:
91 const basegfx::B2DHomMatrix
aMatrix(readTransform(aContent
, *this));
93 if(!aMatrix
.isIdentity())
95 setTransform(&aMatrix
);
103 if(readSingleNumber(aContent
, aNum
))
113 if(readSingleNumber(aContent
, aNum
))
123 if(readSingleNumber(aContent
, aNum
))
125 if(aNum
.isPositive())
136 if(readSingleNumber(aContent
, aNum
))
138 if(aNum
.isPositive())
145 case SVGTokenXlinkHref
:
147 const sal_Int32
nLen(aContent
.getLength());
151 readImageLink(aContent
, maXLink
, maUrl
, maMimeType
, maData
);
162 void extractFromGraphic(
163 const Graphic
& rGraphic
,
164 drawinglayer::primitive2d::Primitive2DSequence
& rEmbedded
,
165 basegfx::B2DRange
& rViewBox
,
168 if(GRAPHIC_BITMAP
== rGraphic
.GetType())
170 if(rGraphic
.getSvgData().get())
173 rEmbedded
= rGraphic
.getSvgData()->getPrimitive2DSequence();
176 rViewBox
= rGraphic
.getSvgData()->getRange();
181 rBitmapEx
= rGraphic
.GetBitmapEx();
186 // evtl. convert to bitmap
187 rBitmapEx
= rGraphic
.GetBitmapEx();
191 void SvgImageNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence
& rTarget
, bool /*bReferenced*/) const
193 // get size range and create path
194 const SvgStyleAttributes
* pStyle
= getSvgStyleAttributes();
196 if(pStyle
&& getWidth().isSet() && getHeight().isSet())
198 const double fWidth(getWidth().solve(*this, xcoordinate
));
199 const double fHeight(getHeight().solve(*this, ycoordinate
));
201 if(fWidth
> 0.0 && fHeight
> 0.0)
204 drawinglayer::primitive2d::Primitive2DSequence aNewTarget
;
206 // prepare Target and ViewBox for evtl. AspectRatio mappings
207 const double fX(getX().isSet() ? getX().solve(*this, xcoordinate
) : 0.0);
208 const double fY(getY().isSet() ? getY().solve(*this, ycoordinate
) : 0.0);
209 const basegfx::B2DRange
aTarget(fX
, fY
, fX
+ fWidth
, fY
+ fHeight
);
210 basegfx::B2DRange
aViewBox(aTarget
);
212 if(!maMimeType
.isEmpty() && !maData
.isEmpty())
214 // use embedded base64 encoded data
215 ::com::sun::star::uno::Sequence
< sal_Int8
> aPass
;
216 ::sax::Converter::decodeBase64(aPass
, maData
);
218 if(aPass
.hasElements())
220 SvMemoryStream
aStream(aPass
.getArray(), aPass
.getLength(), StreamMode::READ
);
223 if(GRFILTER_OK
== GraphicFilter::GetGraphicFilter().ImportGraphic(
228 extractFromGraphic(aGraphic
, aNewTarget
, aViewBox
, aBitmapEx
);
232 else if(!maUrl
.isEmpty())
234 const OUString
& rPath
= getDocument().getAbsolutePath();
237 aAbsUrl
= rtl::Uri::convertRelToAbs(rPath
, maUrl
);
238 } catch (rtl::MalformedUriException
& e
) {
241 "caught rtl::MalformedUriException \""
242 << e
.getMessage() << "\"");
245 if (!aAbsUrl
.isEmpty())
247 SvFileStream
aStream(aAbsUrl
, STREAM_STD_READ
);
250 if(GRFILTER_OK
== GraphicFilter::GetGraphicFilter().ImportGraphic(
255 extractFromGraphic(aGraphic
, aNewTarget
, aViewBox
, aBitmapEx
);
259 else if(!maXLink
.isEmpty())
261 const SvgNode
* mpXLink
= getDocument().findSvgNodeById(maXLink
);
263 if(mpXLink
&& Display_none
!= mpXLink
->getDisplay())
265 mpXLink
->decomposeSvgNode(aNewTarget
, true);
267 if(aNewTarget
.hasElements())
269 aViewBox
= drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence(
271 drawinglayer::geometry::ViewInformation2D());
276 if(!aBitmapEx
.IsEmpty())
278 // create content from created bitmap
279 aNewTarget
.realloc(1);
280 aNewTarget
[0] = new drawinglayer::primitive2d::BitmapPrimitive2D(
282 basegfx::B2DHomMatrix());
284 // fill aViewBox. No size set yet, use unit size
285 aViewBox
= basegfx::B2DRange(0.0, 0.0, 1.0, 1.0);
288 if(aNewTarget
.hasElements())
290 if(aTarget
.equal(aViewBox
))
292 // just add to rTarget
293 drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget
, aNewTarget
);
298 const SvgAspectRatio
& rRatio
= getSvgAspectRatio();
302 // let mapping be created from SvgAspectRatio
303 const basegfx::B2DHomMatrix
aEmbeddingTransform(rRatio
.createMapping(aTarget
, aViewBox
));
305 if(!aEmbeddingTransform
.isIdentity())
307 const drawinglayer::primitive2d::Primitive2DReference
xRef(
308 new drawinglayer::primitive2d::TransformPrimitive2D(
312 aNewTarget
= drawinglayer::primitive2d::Primitive2DSequence(&xRef
, 1);
315 if(!rRatio
.isMeetOrSlice())
317 // need to embed in MaskPrimitive2D to ensure clipping
318 const drawinglayer::primitive2d::Primitive2DReference
xMask(
319 new drawinglayer::primitive2d::MaskPrimitive2D(
320 basegfx::B2DPolyPolygon(
321 basegfx::tools::createPolygonFromRect(aTarget
)),
324 aNewTarget
= drawinglayer::primitive2d::Primitive2DSequence(&xMask
, 1);
329 // choose default mapping
330 const basegfx::B2DHomMatrix
aEmbeddingTransform(SvgAspectRatio::createLinearMapping(aTarget
, aViewBox
));
332 if(!aEmbeddingTransform
.isIdentity())
334 const drawinglayer::primitive2d::Primitive2DReference
xRef(
335 new drawinglayer::primitive2d::TransformPrimitive2D(
339 aNewTarget
= drawinglayer::primitive2d::Primitive2DSequence(&xRef
, 1);
343 // embed and add to rTarget, take local extra-transform into account
344 pStyle
->add_postProcess(rTarget
, aNewTarget
, getTransform());
351 } // end of namespace svgreader
352 } // end of namespace svgio
354 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */