bump product version to 7.2.5.1
[LibreOffice.git] / svgio / source / svgreader / svgimagenode.cxx
blobfeaf5933ad5305627b8b9b223f9c9c898c0613d2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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,
41 SvgNode* pParent)
42 : SvgNode(SVGToken::Rect, rDocument, pParent),
43 maSvgStyleAttributes(*this),
44 maSvgAspectRatio(),
45 maX(0),
46 maY(0),
47 maWidth(0),
48 maHeight(0),
49 maXLink(),
50 maUrl(),
51 maMimeType(),
52 maData()
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)
67 // call parent
68 SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
70 // read style attributes
71 maSvgStyleAttributes.parseStyleAttribute(aSVGToken, aContent, false);
73 // parse own
74 switch(aSVGToken)
76 case SVGToken::Style:
78 readLocalCssStyle(aContent);
79 break;
81 case SVGToken::PreserveAspectRatio:
83 maSvgAspectRatio = readSvgAspectRatio(aContent);
84 break;
86 case SVGToken::Transform:
88 const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
90 if(!aMatrix.isIdentity())
92 setTransform(&aMatrix);
94 break;
96 case SVGToken::X:
98 SvgNumber aNum;
100 if(readSingleNumber(aContent, aNum))
102 maX = aNum;
104 break;
106 case SVGToken::Y:
108 SvgNumber aNum;
110 if(readSingleNumber(aContent, aNum))
112 maY = aNum;
114 break;
116 case SVGToken::Width:
118 SvgNumber aNum;
120 if(readSingleNumber(aContent, aNum))
122 if(aNum.isPositive())
124 maWidth = aNum;
127 break;
129 case SVGToken::Height:
131 SvgNumber aNum;
133 if(readSingleNumber(aContent, aNum))
135 if(aNum.isPositive())
137 maHeight = aNum;
140 break;
142 case SVGToken::XlinkHref:
144 const sal_Int32 nLen(aContent.getLength());
146 if(nLen)
148 readImageLink(aContent, maXLink, maUrl, maMimeType, maData);
150 break;
152 default:
154 break;
159 static void extractFromGraphic(
160 const Graphic& rGraphic,
161 drawinglayer::primitive2d::Primitive2DContainer& rEmbedded,
162 basegfx::B2DRange& rViewBox,
163 BitmapEx& rBitmapEx)
165 if(GraphicType::Bitmap == rGraphic.GetType())
167 if(rGraphic.getVectorGraphicData())
169 // embedded Svg
170 rEmbedded = rGraphic.getVectorGraphicData()->getPrimitive2DSequence();
172 // fill aViewBox
173 rViewBox = rGraphic.getVectorGraphicData()->getRange();
175 else
177 // get bitmap
178 rBitmapEx = rGraphic.GetBitmapEx();
181 else
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()))
194 return;
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)
200 return;
202 BitmapEx aBitmapEx;
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);
220 Graphic aGraphic;
222 if(ERRCODE_NONE == GraphicFilter::GetGraphicFilter().ImportGraphic(
223 aGraphic,
224 OUString(),
225 aStream))
227 extractFromGraphic(aGraphic, aNewTarget, aViewBox, aBitmapEx);
231 else if(!maUrl.isEmpty())
233 const OUString& rPath = getDocument().getAbsolutePath();
234 OUString aAbsUrl;
235 try {
236 aAbsUrl = rtl::Uri::convertRelToAbs(rPath, maUrl);
237 } catch (rtl::MalformedUriException & e) {
238 SAL_WARN(
239 "svg",
240 "caught rtl::MalformedUriException \""
241 << e.getMessage() << "\"");
244 if (!aAbsUrl.isEmpty() && rPath != aAbsUrl)
246 SvFileStream aStream(aAbsUrl, StreamMode::STD_READ);
247 Graphic aGraphic;
249 if(ERRCODE_NONE == GraphicFilter::GetGraphicFilter().ImportGraphic(
250 aGraphic,
251 aAbsUrl,
252 aStream))
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))
280 // use unit range
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(
288 0.0,
289 0.5 - fHalfHeight,
290 1.0,
291 0.5 + fHalfHeight);
293 else
295 // height bigger width
296 const double fHalfWidth(fAspectRatio * 0.5);
297 aViewBox = basegfx::B2DRange(
298 0.5 - fHalfWidth,
299 0.0,
300 0.5 + fHalfWidth,
301 1.0);
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(
310 aViewBox.getRange(),
311 aViewBox.getMinimum()));
314 if(aNewTarget.empty())
315 return;
317 if(aTarget.equal(aViewBox))
319 // just add to rTarget
320 rTarget.append(aNewTarget);
322 else
324 // create mapping
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(
335 aEmbeddingTransform,
336 aNewTarget));
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)),
348 aNewTarget));
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: */