Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / vcl / source / gdi / svgdata.cxx
blobd15e56aa494a8d67d69bcea9b2b49992c82f42af
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 <tools/stream.hxx>
21 #include <vcl/svgdata.hxx>
22 #include <comphelper/processfactory.hxx>
23 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
24 #include <com/sun/star/graphic/SvgTools.hpp>
25 #include <com/sun/star/graphic/Primitive2DTools.hpp>
26 #include <com/sun/star/rendering/XIntegerReadOnlyBitmap.hpp>
27 #include <com/sun/star/util/XAccounting.hpp>
28 #include <vcl/canvastools.hxx>
29 #include <comphelper/seqstream.hxx>
30 #include <comphelper/sequence.hxx>
31 #include <vcl/svapp.hxx>
32 #include <vcl/outdev.hxx>
34 using namespace ::com::sun::star;
36 BitmapEx convertPrimitive2DSequenceToBitmapEx(
37 const std::deque< css::uno::Reference< css::graphic::XPrimitive2D > >& rSequence,
38 const basegfx::B2DRange& rTargetRange,
39 const sal_uInt32 nMaximumQuadraticPixels)
41 BitmapEx aRetval;
43 if(!rSequence.empty())
45 // create replacement graphic from maSequence
46 // create XPrimitive2DRenderer
47 try
49 uno::Reference< uno::XComponentContext > xContext(::comphelper::getProcessComponentContext());
50 const uno::Reference< graphic::XPrimitive2DRenderer > xPrimitive2DRenderer = graphic::Primitive2DTools::create(xContext);
52 uno::Sequence< beans::PropertyValue > aViewParameters;
53 geometry::RealRectangle2D aRealRect;
55 aRealRect.X1 = rTargetRange.getMinX();
56 aRealRect.Y1 = rTargetRange.getMinY();
57 aRealRect.X2 = rTargetRange.getMaxX();
58 aRealRect.Y2 = rTargetRange.getMaxY();
60 // get system DPI
61 const Size aDPI(Application::GetDefaultDevice()->LogicToPixel(Size(1, 1), MapUnit::MapInch));
63 const uno::Reference< rendering::XBitmap > xBitmap(
64 xPrimitive2DRenderer->rasterize(
65 comphelper::containerToSequence(rSequence),
66 aViewParameters,
67 aDPI.getWidth(),
68 aDPI.getHeight(),
69 aRealRect,
70 nMaximumQuadraticPixels));
72 if(xBitmap.is())
74 const uno::Reference< rendering::XIntegerReadOnlyBitmap> xIntBmp(xBitmap, uno::UNO_QUERY_THROW);
76 if(xIntBmp.is())
78 aRetval = vcl::unotools::bitmapExFromXBitmap(xIntBmp);
82 catch (const uno::Exception& e)
84 SAL_WARN("vcl", "Got no graphic::XPrimitive2DRenderer! : " << e.Message);
86 catch (const std::exception& e)
88 SAL_WARN("vcl", "Got no graphic::XPrimitive2DRenderer! : " << e.what());
92 return aRetval;
95 size_t estimateSize(
96 std::deque<uno::Reference<graphic::XPrimitive2D>> const& rSequence)
98 size_t nRet(0);
99 for (auto& it : rSequence)
101 uno::Reference<util::XAccounting> const xAcc(it, uno::UNO_QUERY);
102 assert(xAcc.is()); // we expect only BasePrimitive2D from SVG parser
103 nRet += xAcc->estimateUsage();
105 return nRet;
108 void SvgData::ensureReplacement()
110 ensureSequenceAndRange();
112 if(maReplacement.IsEmpty() && !maSequence.empty())
114 maReplacement = convertPrimitive2DSequenceToBitmapEx(maSequence, getRange());
118 void SvgData::ensureSequenceAndRange()
120 if(maSequence.empty() && maSvgDataArray.hasElements())
122 // import SVG to maSequence, also set maRange
123 maRange.reset();
125 // create stream
126 const uno::Reference< io::XInputStream > myInputStream(new comphelper::SequenceInputStream(maSvgDataArray));
128 if(myInputStream.is())
130 // create SVG interpreter
133 uno::Reference<uno::XComponentContext> xContext(::comphelper::getProcessComponentContext());
134 const uno::Reference< graphic::XSvgParser > xSvgParser = graphic::SvgTools::create(xContext);
136 maSequence = comphelper::sequenceToContainer<std::deque<css::uno::Reference< css::graphic::XPrimitive2D >>>(xSvgParser->getDecomposition(myInputStream, maPath));
138 catch(const uno::Exception&)
140 OSL_ENSURE(false, "Got no graphic::XSvgParser (!)" );
144 if(!maSequence.empty())
146 const sal_Int32 nCount(maSequence.size());
147 geometry::RealRectangle2D aRealRect;
148 uno::Sequence< beans::PropertyValue > aViewParameters;
150 for(sal_Int32 a(0); a < nCount; a++)
152 // get reference
153 const css::uno::Reference< css::graphic::XPrimitive2D > xReference(maSequence[a]);
155 if(xReference.is())
157 aRealRect = xReference->getRange(aViewParameters);
159 maRange.expand(
160 basegfx::B2DRange(
161 aRealRect.X1,
162 aRealRect.Y1,
163 aRealRect.X2,
164 aRealRect.Y2));
168 mNestedBitmapSize = estimateSize(maSequence);
172 auto SvgData::getSizeBytes() -> std::pair<State, size_t>
174 if (maSequence.empty() && maSvgDataArray.hasElements())
176 return std::make_pair(State::UNPARSED, maSvgDataArray.getLength());
178 else
180 return std::make_pair(State::PARSED, maSvgDataArray.getLength() + mNestedBitmapSize);
184 SvgData::SvgData(const SvgDataArray& rSvgDataArray, const OUString& rPath)
185 : maSvgDataArray(rSvgDataArray),
186 maPath(rPath),
187 maRange(),
188 maSequence(),
189 maReplacement()
190 , mNestedBitmapSize(0)
194 SvgData::SvgData(const OUString& rPath):
195 maSvgDataArray(),
196 maPath(rPath),
197 maRange(),
198 maSequence(),
199 maReplacement()
200 , mNestedBitmapSize(0)
202 SvFileStream rIStm(rPath, StreamMode::STD_READ);
203 if(rIStm.GetError())
204 return;
205 const sal_uInt32 nStmLen(rIStm.remainingSize());
206 if (nStmLen)
208 maSvgDataArray.realloc(nStmLen);
209 rIStm.ReadBytes(maSvgDataArray.begin(), nStmLen);
211 if (rIStm.GetError())
213 maSvgDataArray = SvgDataArray();
218 const basegfx::B2DRange& SvgData::getRange() const
220 const_cast< SvgData* >(this)->ensureSequenceAndRange();
222 return maRange;
225 const std::deque< css::uno::Reference< css::graphic::XPrimitive2D > >& SvgData::getPrimitive2DSequence() const
227 const_cast< SvgData* >(this)->ensureSequenceAndRange();
229 return maSequence;
232 const BitmapEx& SvgData::getReplacement() const
234 const_cast< SvgData* >(this)->ensureReplacement();
236 return maReplacement;
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */