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 <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
)
43 if(!rSequence
.empty())
45 // create replacement graphic from maSequence
46 // create XPrimitive2DRenderer
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();
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
),
70 nMaximumQuadraticPixels
));
74 const uno::Reference
< rendering::XIntegerReadOnlyBitmap
> xIntBmp(xBitmap
, uno::UNO_QUERY_THROW
);
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());
96 std::deque
<uno::Reference
<graphic::XPrimitive2D
>> const& rSequence
)
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();
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
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
++)
153 const css::uno::Reference
< css::graphic::XPrimitive2D
> xReference(maSequence
[a
]);
157 aRealRect
= xReference
->getRange(aViewParameters
);
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());
180 return std::make_pair(State::PARSED
, maSvgDataArray
.getLength() + mNestedBitmapSize
);
184 SvgData::SvgData(const SvgDataArray
& rSvgDataArray
, const OUString
& rPath
)
185 : maSvgDataArray(rSvgDataArray
),
190 , mNestedBitmapSize(0)
194 SvgData::SvgData(const OUString
& rPath
):
200 , mNestedBitmapSize(0)
202 SvFileStream
rIStm(rPath
, StreamMode::STD_READ
);
205 const sal_uInt32
nStmLen(rIStm
.remainingSize());
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();
225 const std::deque
< css::uno::Reference
< css::graphic::XPrimitive2D
> >& SvgData::getPrimitive2DSequence() const
227 const_cast< SvgData
* >(this)->ensureSequenceAndRange();
232 const BitmapEx
& SvgData::getReplacement() const
234 const_cast< SvgData
* >(this)->ensureReplacement();
236 return maReplacement
;
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */