Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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 <vcl/canvastools.hxx>
28 : #include <comphelper/seqstream.hxx>
29 : #include <vcl/svapp.hxx>
30 : #include <vcl/outdev.hxx>
31 :
32 : //////////////////////////////////////////////////////////////////////////////
33 :
34 : using namespace ::com::sun::star;
35 :
36 : //////////////////////////////////////////////////////////////////////////////
37 :
38 292 : BitmapEx VCL_DLLPUBLIC convertPrimitive2DSequenceToBitmapEx(
39 : const Primitive2DSequence& rSequence,
40 : const basegfx::B2DRange& rTargetRange,
41 : const sal_uInt32 nMaximumQuadraticPixels)
42 : {
43 292 : BitmapEx aRetval;
44 :
45 292 : if(rSequence.hasElements())
46 : {
47 : // create replacement graphic from maSequence
48 : // create XPrimitive2DRenderer
49 292 : uno::Reference< uno::XComponentContext > xContext(::comphelper::getProcessComponentContext());
50 :
51 : try
52 : {
53 292 : const uno::Reference< graphic::XPrimitive2DRenderer > xPrimitive2DRenderer = graphic::Primitive2DTools::create(xContext);
54 :
55 584 : uno::Sequence< beans::PropertyValue > aViewParameters;
56 292 : geometry::RealRectangle2D aRealRect;
57 :
58 292 : aRealRect.X1 = rTargetRange.getMinX();
59 292 : aRealRect.Y1 = rTargetRange.getMinY();
60 292 : aRealRect.X2 = rTargetRange.getMaxX();
61 292 : aRealRect.Y2 = rTargetRange.getMaxY();
62 :
63 : // get system DPI
64 292 : const Size aDPI(Application::GetDefaultDevice()->LogicToPixel(Size(1, 1), MAP_INCH));
65 :
66 : const uno::Reference< rendering::XBitmap > xBitmap(
67 292 : xPrimitive2DRenderer->rasterize(
68 : rSequence,
69 : aViewParameters,
70 292 : aDPI.getWidth(),
71 292 : aDPI.getHeight(),
72 : aRealRect,
73 1168 : nMaximumQuadraticPixels));
74 :
75 292 : if(xBitmap.is())
76 : {
77 292 : const uno::Reference< rendering::XIntegerReadOnlyBitmap> xIntBmp(xBitmap, uno::UNO_QUERY_THROW);
78 :
79 292 : if(xIntBmp.is())
80 : {
81 292 : aRetval = vcl::unotools::bitmapExFromXBitmap(xIntBmp);
82 292 : }
83 292 : }
84 : }
85 0 : catch(const uno::Exception& e)
86 : {
87 : SAL_WARN( "vcl", "Got no graphic::XPrimitive2DRenderer! : " << e.Message);
88 292 : }
89 : }
90 :
91 292 : return aRetval;
92 : }
93 :
94 : //////////////////////////////////////////////////////////////////////////////
95 :
96 292 : void SvgData::ensureReplacement()
97 : {
98 292 : ensureSequenceAndRange();
99 :
100 292 : if(maReplacement.IsEmpty() && maSequence.hasElements())
101 : {
102 292 : maReplacement = convertPrimitive2DSequenceToBitmapEx(maSequence, getRange());
103 : }
104 292 : }
105 :
106 : //////////////////////////////////////////////////////////////////////////////
107 :
108 584 : void SvgData::ensureSequenceAndRange()
109 : {
110 584 : if(!maSequence.hasElements() && mnSvgDataArrayLength)
111 : {
112 : // import SVG to maSequence, also set maRange
113 292 : maRange.reset();
114 :
115 : // create stream
116 292 : const uno::Sequence< sal_Int8 > aPostData((sal_Int8*)maSvgDataArray.get(), mnSvgDataArrayLength);
117 584 : const uno::Reference< io::XInputStream > myInputStream(new comphelper::SequenceInputStream(aPostData));
118 :
119 292 : if(myInputStream.is())
120 : {
121 : // create SVG interpreter
122 292 : uno::Reference< uno::XComponentContext > xContext(::comphelper::getProcessComponentContext());
123 :
124 : try
125 : {
126 292 : const uno::Reference< graphic::XSvgParser > xSvgParser = graphic::SvgTools::create(xContext);
127 :
128 292 : maSequence = xSvgParser->getDecomposition(myInputStream, maPath);
129 : }
130 0 : catch(const uno::Exception&)
131 : {
132 : OSL_ENSURE(sal_False, "Got no graphic::XSvgParser (!)" );
133 292 : }
134 : }
135 :
136 292 : if(maSequence.hasElements())
137 : {
138 292 : const sal_Int32 nCount(maSequence.getLength());
139 292 : geometry::RealRectangle2D aRealRect;
140 292 : uno::Sequence< beans::PropertyValue > aViewParameters;
141 :
142 584 : for(sal_Int32 a(0L); a < nCount; a++)
143 : {
144 : // get reference
145 292 : const Primitive2DReference xReference(maSequence[a]);
146 :
147 292 : if(xReference.is())
148 : {
149 292 : aRealRect = xReference->getRange(aViewParameters);
150 :
151 : maRange.expand(
152 : basegfx::B2DRange(
153 : aRealRect.X1,
154 : aRealRect.Y1,
155 : aRealRect.X2,
156 292 : aRealRect.Y2));
157 : }
158 584 : }
159 292 : }
160 : }
161 584 : }
162 :
163 : //////////////////////////////////////////////////////////////////////////////
164 :
165 292 : SvgData::SvgData(const SvgDataArray& rSvgDataArray, sal_uInt32 nSvgDataArrayLength, const OUString& rPath)
166 : : maSvgDataArray(rSvgDataArray),
167 : mnSvgDataArrayLength(nSvgDataArrayLength),
168 : maPath(rPath),
169 : maRange(),
170 : maSequence(),
171 292 : maReplacement()
172 : {
173 292 : }
174 :
175 : //////////////////////////////////////////////////////////////////////////////
176 0 : SvgData::SvgData(const OUString& rPath):
177 : maSvgDataArray(),
178 : mnSvgDataArrayLength(0),
179 : maPath(rPath),
180 : maRange(),
181 : maSequence(),
182 0 : maReplacement()
183 : {
184 0 : SvFileStream rIStm(rPath, STREAM_STD_READ);
185 0 : if(rIStm.GetError())
186 0 : return;
187 0 : const sal_uInt32 nStmPos(rIStm.Tell());
188 0 : const sal_uInt32 nStmLen(rIStm.Seek(STREAM_SEEK_TO_END) - nStmPos);
189 0 : if(nStmLen)
190 : {
191 0 : SvgDataArray aNewData(new sal_uInt8[nStmLen]);
192 0 : rIStm.Seek(nStmPos);
193 0 : rIStm.Read(aNewData.get(), nStmLen);
194 :
195 0 : if(!rIStm.GetError())
196 : {
197 0 : maSvgDataArray = aNewData;
198 0 : mnSvgDataArrayLength = nStmLen;
199 0 : }
200 0 : }
201 : }
202 :
203 : //////////////////////////////////////////////////////////////////////////////
204 :
205 292 : const basegfx::B2DRange& SvgData::getRange() const
206 : {
207 292 : const_cast< SvgData* >(this)->ensureSequenceAndRange();
208 :
209 292 : return maRange;
210 : }
211 :
212 : //////////////////////////////////////////////////////////////////////////////
213 :
214 0 : const Primitive2DSequence& SvgData::getPrimitive2DSequence() const
215 : {
216 0 : const_cast< SvgData* >(this)->ensureSequenceAndRange();
217 :
218 0 : return maSequence;
219 : }
220 :
221 : //////////////////////////////////////////////////////////////////////////////
222 :
223 292 : const BitmapEx& SvgData::getReplacement() const
224 : {
225 292 : const_cast< SvgData* >(this)->ensureReplacement();
226 :
227 292 : return maReplacement;
228 465 : }
229 :
230 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|