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 <svgimagenode.hxx>
21 #include <svgdocument.hxx>
22 #include <tools/stream.hxx>
23 #include <vcl/bitmapex.hxx>
24 #include <vcl/graphicfilter.hxx>
25 #include <basegfx/matrix/b2dhommatrixtools.hxx>
26 #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
27 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
28 #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
29 #include <basegfx/polygon/b2dpolygontools.hxx>
30 #include <basegfx/polygon/b2dpolygon.hxx>
31 #include <rtl/uri.hxx>
32 #include <sal/log.hxx>
33 #include <drawinglayer/geometry/viewinformation2d.hxx>
34 #include <comphelper/base64.hxx>
35 #include <toolkit/helper/vclunohelper.hxx>
37 namespace svgio::svgreader
39 SvgImageNode::SvgImageNode(
40 SvgDocument
& rDocument
,
42 : SvgNode(SVGToken::Rect
, rDocument
, pParent
),
43 maSvgStyleAttributes(*this),
56 SvgImageNode::~SvgImageNode()
60 const SvgStyleAttributes
* SvgImageNode::getSvgStyleAttributes() const
62 return checkForCssStyle("image", maSvgStyleAttributes
);
65 void SvgImageNode::parseAttribute(const OUString
& rTokenName
, SVGToken aSVGToken
, const OUString
& aContent
)
68 SvgNode::parseAttribute(rTokenName
, aSVGToken
, aContent
);
70 // read style attributes
71 maSvgStyleAttributes
.parseStyleAttribute(aSVGToken
, aContent
, false);
78 readLocalCssStyle(aContent
);
81 case SVGToken::PreserveAspectRatio
:
83 maSvgAspectRatio
= readSvgAspectRatio(aContent
);
86 case SVGToken::Transform
:
88 const basegfx::B2DHomMatrix
aMatrix(readTransform(aContent
, *this));
90 if(!aMatrix
.isIdentity())
92 setTransform(&aMatrix
);
100 if(readSingleNumber(aContent
, aNum
))
110 if(readSingleNumber(aContent
, aNum
))
116 case SVGToken::Width
:
120 if(readSingleNumber(aContent
, aNum
))
122 if(aNum
.isPositive())
129 case SVGToken::Height
:
133 if(readSingleNumber(aContent
, aNum
))
135 if(aNum
.isPositive())
142 case SVGToken::XlinkHref
:
144 const sal_Int32
nLen(aContent
.getLength());
148 readImageLink(aContent
, maXLink
, maUrl
, maMimeType
, maData
);
159 static void extractFromGraphic(
160 const Graphic
& rGraphic
,
161 drawinglayer::primitive2d::Primitive2DContainer
& rEmbedded
,
162 basegfx::B2DRange
& rViewBox
,
165 if(GraphicType::Bitmap
== rGraphic
.GetType())
167 if(rGraphic
.getVectorGraphicData())
170 rEmbedded
= rGraphic
.getVectorGraphicData()->getPrimitive2DSequence();
173 rViewBox
= rGraphic
.getVectorGraphicData()->getRange();
178 rBitmapEx
= rGraphic
.GetBitmapEx();
183 // evtl. convert to bitmap
184 rBitmapEx
= rGraphic
.GetBitmapEx();
188 void SvgImageNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer
& rTarget
, bool /*bReferenced*/) const
190 // get size range and create path
191 const SvgStyleAttributes
* pStyle
= getSvgStyleAttributes();
193 if(!(pStyle
&& getWidth().isSet() && getHeight().isSet()))
196 const double fWidth(getWidth().solve(*this, NumberType::xcoordinate
));
197 const double fHeight(getHeight().solve(*this, NumberType::ycoordinate
));
199 if(fWidth
<= 0.0 || fHeight
<= 0.0)
203 drawinglayer::primitive2d::Primitive2DContainer aNewTarget
;
205 // prepare Target and ViewBox for evtl. AspectRatio mappings
206 const double fX(getX().isSet() ? getX().solve(*this, NumberType::xcoordinate
) : 0.0);
207 const double fY(getY().isSet() ? getY().solve(*this, NumberType::ycoordinate
) : 0.0);
208 const basegfx::B2DRange
aTarget(fX
, fY
, fX
+ fWidth
, fY
+ fHeight
);
209 basegfx::B2DRange
aViewBox(aTarget
);
211 if(!maMimeType
.isEmpty() && !maData
.isEmpty())
213 // use embedded base64 encoded data
214 css::uno::Sequence
< sal_Int8
> aPass
;
215 ::comphelper::Base64::decode(aPass
, maData
);
217 if(aPass
.hasElements())
219 SvMemoryStream
aStream(aPass
.getArray(), aPass
.getLength(), StreamMode::READ
);
222 if(ERRCODE_NONE
== GraphicFilter::GetGraphicFilter().ImportGraphic(
227 extractFromGraphic(aGraphic
, aNewTarget
, aViewBox
, aBitmapEx
);
231 else if(!maUrl
.isEmpty())
233 const OUString
& rPath
= getDocument().getAbsolutePath();
236 aAbsUrl
= rtl::Uri::convertRelToAbs(rPath
, maUrl
);
237 } catch (rtl::MalformedUriException
& e
) {
240 "caught rtl::MalformedUriException \""
241 << e
.getMessage() << "\"");
244 if (!aAbsUrl
.isEmpty() && rPath
!= aAbsUrl
)
246 SvFileStream
aStream(aAbsUrl
, StreamMode::STD_READ
);
249 if(ERRCODE_NONE
== GraphicFilter::GetGraphicFilter().ImportGraphic(
254 extractFromGraphic(aGraphic
, aNewTarget
, aViewBox
, aBitmapEx
);
258 else if(!maXLink
.isEmpty())
260 const SvgNode
* pXLink
= getDocument().findSvgNodeById(maXLink
);
262 if(pXLink
&& Display::None
!= pXLink
->getDisplay())
264 pXLink
->decomposeSvgNode(aNewTarget
, true);
266 if(!aNewTarget
.empty())
268 aViewBox
= aNewTarget
.getB2DRange(drawinglayer::geometry::ViewInformation2D());
273 if(!aBitmapEx
.IsEmpty() && 0 != aBitmapEx
.GetSizePixel().Width() && 0 != aBitmapEx
.GetSizePixel().Height())
275 // calculate centered unit size
276 const double fAspectRatio
= static_cast<double>(aBitmapEx
.GetSizePixel().Width()) / static_cast<double>(aBitmapEx
.GetSizePixel().Height());
278 if(basegfx::fTools::equal(fAspectRatio
, 0.0))
281 aViewBox
= basegfx::B2DRange(0.0, 0.0, 1.0, 1.0);
283 else if(basegfx::fTools::more(fAspectRatio
, 0.0))
285 // width bigger height
286 const double fHalfHeight((1.0 / fAspectRatio
) * 0.5);
287 aViewBox
= basegfx::B2DRange(
295 // height bigger width
296 const double fHalfWidth(fAspectRatio
* 0.5);
297 aViewBox
= basegfx::B2DRange(
304 // create content from created bitmap, use calculated unit range size
305 // as transformation to map the picture data correctly
306 aNewTarget
.resize(1);
307 aNewTarget
[0] = new drawinglayer::primitive2d::BitmapPrimitive2D(
308 VCLUnoHelper::CreateVCLXBitmap(aBitmapEx
),
309 basegfx::utils::createScaleTranslateB2DHomMatrix(
311 aViewBox
.getMinimum()));
314 if(aNewTarget
.empty())
317 if(aTarget
.equal(aViewBox
))
319 // just add to rTarget
320 rTarget
.append(aNewTarget
);
325 const SvgAspectRatio
& rRatio
= maSvgAspectRatio
;
327 // even when ratio is not set, use the defaults
328 // let mapping be created from SvgAspectRatio
329 const basegfx::B2DHomMatrix
aEmbeddingTransform(rRatio
.createMapping(aTarget
, aViewBox
));
331 if(!aEmbeddingTransform
.isIdentity())
333 const drawinglayer::primitive2d::Primitive2DReference
xRef(
334 new drawinglayer::primitive2d::TransformPrimitive2D(
338 aNewTarget
= drawinglayer::primitive2d::Primitive2DContainer
{ xRef
};
341 if(!rRatio
.isMeetOrSlice())
343 // need to embed in MaskPrimitive2D to ensure clipping
344 const drawinglayer::primitive2d::Primitive2DReference
xMask(
345 new drawinglayer::primitive2d::MaskPrimitive2D(
346 basegfx::B2DPolyPolygon(
347 basegfx::utils::createPolygonFromRect(aTarget
)),
350 aNewTarget
= drawinglayer::primitive2d::Primitive2DContainer
{ xMask
};
353 // embed and add to rTarget, take local extra-transform into account
354 pStyle
->add_postProcess(rTarget
, aNewTarget
, getTransform());
358 } // end of namespace svgio::svgreader
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */