/src/libreoffice/svgio/source/svgreader/svgimagenode.cxx
Line | Count | Source |
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 <svgimagenode.hxx> |
21 | | #include <svgdocument.hxx> |
22 | | #include <tools/stream.hxx> |
23 | | #include <vcl/bitmap.hxx> |
24 | | #include <vcl/graph.hxx> |
25 | | #include <vcl/graphicfilter.hxx> |
26 | | #include <vcl/vectorgraphicdata.hxx> |
27 | | #include <basegfx/matrix/b2dhommatrixtools.hxx> |
28 | | #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx> |
29 | | #include <drawinglayer/primitive2d/transformprimitive2d.hxx> |
30 | | #include <drawinglayer/primitive2d/maskprimitive2d.hxx> |
31 | | #include <basegfx/polygon/b2dpolygontools.hxx> |
32 | | #include <basegfx/polygon/b2dpolygon.hxx> |
33 | | #include <sal/log.hxx> |
34 | | #include <drawinglayer/geometry/viewinformation2d.hxx> |
35 | | #include <comphelper/base64.hxx> |
36 | | #include <toolkit/helper/vclunohelper.hxx> |
37 | | |
38 | | namespace svgio::svgreader |
39 | | { |
40 | | SvgImageNode::SvgImageNode( |
41 | | SvgDocument& rDocument, |
42 | | SvgNode* pParent) |
43 | 1 | : SvgNode(SVGToken::Rect, rDocument, pParent), |
44 | 1 | maSvgStyleAttributes(*this), |
45 | 1 | maX(0), |
46 | 1 | maY(0), |
47 | 1 | maWidth(0), |
48 | 1 | maHeight(0) |
49 | 1 | { |
50 | 1 | } |
51 | | |
52 | | SvgImageNode::~SvgImageNode() |
53 | 1 | { |
54 | 1 | } |
55 | | |
56 | | const SvgStyleAttributes* SvgImageNode::getSvgStyleAttributes() const |
57 | 0 | { |
58 | 0 | return checkForCssStyle(maSvgStyleAttributes); |
59 | 0 | } |
60 | | |
61 | | void SvgImageNode::parseAttribute(SVGToken aSVGToken, const OUString& aContent) |
62 | 0 | { |
63 | | // call parent |
64 | 0 | SvgNode::parseAttribute(aSVGToken, aContent); |
65 | | |
66 | | // read style attributes |
67 | 0 | maSvgStyleAttributes.parseStyleAttribute(aSVGToken, aContent); |
68 | | |
69 | | // parse own |
70 | 0 | switch(aSVGToken) |
71 | 0 | { |
72 | 0 | case SVGToken::Style: |
73 | 0 | { |
74 | 0 | readLocalCssStyle(aContent); |
75 | 0 | break; |
76 | 0 | } |
77 | 0 | case SVGToken::PreserveAspectRatio: |
78 | 0 | { |
79 | 0 | maSvgAspectRatio = readSvgAspectRatio(aContent); |
80 | 0 | break; |
81 | 0 | } |
82 | 0 | case SVGToken::Transform: |
83 | 0 | { |
84 | 0 | const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this)); |
85 | |
|
86 | 0 | if(!aMatrix.isIdentity()) |
87 | 0 | { |
88 | 0 | setTransform(aMatrix); |
89 | 0 | } |
90 | 0 | break; |
91 | 0 | } |
92 | 0 | case SVGToken::X: |
93 | 0 | { |
94 | 0 | SvgNumber aNum; |
95 | |
|
96 | 0 | if(readSingleNumber(aContent, aNum)) |
97 | 0 | { |
98 | 0 | maX = aNum; |
99 | 0 | } |
100 | 0 | break; |
101 | 0 | } |
102 | 0 | case SVGToken::Y: |
103 | 0 | { |
104 | 0 | SvgNumber aNum; |
105 | |
|
106 | 0 | if(readSingleNumber(aContent, aNum)) |
107 | 0 | { |
108 | 0 | maY = aNum; |
109 | 0 | } |
110 | 0 | break; |
111 | 0 | } |
112 | 0 | case SVGToken::Width: |
113 | 0 | { |
114 | 0 | SvgNumber aNum; |
115 | |
|
116 | 0 | if(readSingleNumber(aContent, aNum)) |
117 | 0 | { |
118 | 0 | if(aNum.isPositive()) |
119 | 0 | { |
120 | 0 | maWidth = aNum; |
121 | 0 | } |
122 | 0 | } |
123 | 0 | break; |
124 | 0 | } |
125 | 0 | case SVGToken::Height: |
126 | 0 | { |
127 | 0 | SvgNumber aNum; |
128 | |
|
129 | 0 | if(readSingleNumber(aContent, aNum)) |
130 | 0 | { |
131 | 0 | if(aNum.isPositive()) |
132 | 0 | { |
133 | 0 | maHeight = aNum; |
134 | 0 | } |
135 | 0 | } |
136 | 0 | break; |
137 | 0 | } |
138 | 0 | case SVGToken::Href: |
139 | 0 | case SVGToken::XlinkHref: |
140 | 0 | { |
141 | 0 | const sal_Int32 nLen(aContent.getLength()); |
142 | |
|
143 | 0 | if(nLen) |
144 | 0 | { |
145 | 0 | readImageLink(aContent, maXLink, maUrl, maData); |
146 | 0 | } |
147 | 0 | break; |
148 | 0 | } |
149 | 0 | default: |
150 | 0 | { |
151 | 0 | break; |
152 | 0 | } |
153 | 0 | } |
154 | 0 | } |
155 | | |
156 | | static void extractFromGraphic( |
157 | | const Graphic& rGraphic, |
158 | | drawinglayer::primitive2d::Primitive2DContainer& rEmbedded, |
159 | | basegfx::B2DRange& rViewBox, |
160 | | Bitmap& rBitmap) |
161 | 0 | { |
162 | 0 | if(GraphicType::Bitmap == rGraphic.GetType()) |
163 | 0 | { |
164 | 0 | if(rGraphic.getVectorGraphicData()) |
165 | 0 | { |
166 | | // embedded Svg |
167 | 0 | rEmbedded = rGraphic.getVectorGraphicData()->getPrimitive2DSequence(); |
168 | | |
169 | | // fill aViewBox |
170 | 0 | rViewBox = rGraphic.getVectorGraphicData()->getRange(); |
171 | 0 | } |
172 | 0 | else |
173 | 0 | { |
174 | | // get bitmap |
175 | 0 | rBitmap = rGraphic.GetBitmap(); |
176 | 0 | } |
177 | 0 | } |
178 | 0 | else |
179 | 0 | { |
180 | | // evtl. convert to bitmap |
181 | 0 | rBitmap = rGraphic.GetBitmap(); |
182 | 0 | } |
183 | 0 | } |
184 | | |
185 | | void SvgImageNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer& rTarget, bool /*bReferenced*/) const |
186 | 0 | { |
187 | | // get size range and create path |
188 | 0 | const SvgStyleAttributes* pStyle = getSvgStyleAttributes(); |
189 | |
|
190 | 0 | if(!(pStyle && getWidth().isSet() && getHeight().isSet())) |
191 | 0 | return; |
192 | | |
193 | 0 | const double fWidth(getWidth().solve(*this, NumberType::xcoordinate)); |
194 | 0 | const double fHeight(getHeight().solve(*this, NumberType::ycoordinate)); |
195 | |
|
196 | 0 | if(fWidth <= 0.0 || fHeight <= 0.0) |
197 | 0 | return; |
198 | | |
199 | 0 | Bitmap aBitmap; |
200 | 0 | drawinglayer::primitive2d::Primitive2DContainer aNewTarget; |
201 | | |
202 | | // prepare Target and ViewBox for evtl. AspectRatio mappings |
203 | 0 | const double fX(getX().isSet() ? getX().solve(*this, NumberType::xcoordinate) : 0.0); |
204 | 0 | const double fY(getY().isSet() ? getY().solve(*this, NumberType::ycoordinate) : 0.0); |
205 | 0 | const basegfx::B2DRange aTarget(fX, fY, fX + fWidth, fY + fHeight); |
206 | 0 | basegfx::B2DRange aViewBox(aTarget); |
207 | |
|
208 | 0 | if(!maData.isEmpty()) |
209 | 0 | { |
210 | | // use embedded base64 encoded data |
211 | 0 | css::uno::Sequence< sal_Int8 > aPass; |
212 | 0 | ::comphelper::Base64::decode(aPass, maData); |
213 | |
|
214 | 0 | if(aPass.hasElements()) |
215 | 0 | { |
216 | 0 | SvMemoryStream aStream(aPass.getArray(), aPass.getLength(), StreamMode::READ); |
217 | 0 | Graphic aGraphic; |
218 | |
|
219 | 0 | if(ERRCODE_NONE == GraphicFilter::GetGraphicFilter().ImportGraphic( |
220 | 0 | aGraphic, |
221 | 0 | u"", |
222 | 0 | aStream)) |
223 | 0 | { |
224 | 0 | extractFromGraphic(aGraphic, aNewTarget, aViewBox, aBitmap); |
225 | 0 | } |
226 | 0 | } |
227 | 0 | } |
228 | 0 | else if(!maUrl.isEmpty()) |
229 | 0 | { |
230 | 0 | const OUString aAbsUrl |
231 | 0 | = resolveImageUrl(getDocument().getAbsolutePath(), maUrl); |
232 | |
|
233 | 0 | if (!aAbsUrl.isEmpty()) |
234 | 0 | { |
235 | 0 | SvFileStream aStream(aAbsUrl, StreamMode::STD_READ); |
236 | 0 | Graphic aGraphic; |
237 | |
|
238 | 0 | if(ERRCODE_NONE == GraphicFilter::GetGraphicFilter().ImportGraphic( |
239 | 0 | aGraphic, |
240 | 0 | aAbsUrl, |
241 | 0 | aStream)) |
242 | 0 | { |
243 | 0 | extractFromGraphic(aGraphic, aNewTarget, aViewBox, aBitmap); |
244 | 0 | } |
245 | 0 | } |
246 | 0 | } |
247 | 0 | else if(!maXLink.isEmpty()) |
248 | 0 | { |
249 | 0 | const SvgNode* pXLink = getDocument().findSvgNodeById(maXLink); |
250 | |
|
251 | 0 | if(pXLink && Display::None != pXLink->getDisplay()) |
252 | 0 | { |
253 | 0 | pXLink->decomposeSvgNode(aNewTarget, true); |
254 | |
|
255 | 0 | if(!aNewTarget.empty()) |
256 | 0 | { |
257 | 0 | aViewBox = aNewTarget.getB2DRange(drawinglayer::geometry::ViewInformation2D()); |
258 | 0 | } |
259 | 0 | } |
260 | 0 | } |
261 | |
|
262 | 0 | if(!aBitmap.IsEmpty() && 0 != aBitmap.GetSizePixel().Width() && 0 != aBitmap.GetSizePixel().Height()) |
263 | 0 | { |
264 | | // calculate centered unit size |
265 | 0 | const double fAspectRatio = static_cast<double>(aBitmap.GetSizePixel().Width()) / static_cast<double>(aBitmap.GetSizePixel().Height()); |
266 | |
|
267 | 0 | if (basegfx::fTools::equalZero(fAspectRatio)) |
268 | 0 | { |
269 | | // use unit range |
270 | 0 | aViewBox = basegfx::B2DRange(0.0, 0.0, 1.0, 1.0); |
271 | 0 | } |
272 | 0 | else if (fAspectRatio > 0.0) |
273 | 0 | { |
274 | | // width bigger height |
275 | 0 | const double fHalfHeight((1.0 / fAspectRatio) * 0.5); |
276 | 0 | aViewBox = basegfx::B2DRange( |
277 | 0 | 0.0, |
278 | 0 | 0.5 - fHalfHeight, |
279 | 0 | 1.0, |
280 | 0 | 0.5 + fHalfHeight); |
281 | 0 | } |
282 | 0 | else |
283 | 0 | { |
284 | | // height bigger width |
285 | 0 | const double fHalfWidth(fAspectRatio * 0.5); |
286 | 0 | aViewBox = basegfx::B2DRange( |
287 | 0 | 0.5 - fHalfWidth, |
288 | 0 | 0.0, |
289 | 0 | 0.5 + fHalfWidth, |
290 | 0 | 1.0); |
291 | 0 | } |
292 | | |
293 | | // create content from created bitmap, use calculated unit range size |
294 | | // as transformation to map the picture data correctly |
295 | 0 | aNewTarget.resize(1); |
296 | 0 | aNewTarget[0] = new drawinglayer::primitive2d::BitmapPrimitive2D( |
297 | 0 | aBitmap, |
298 | 0 | basegfx::utils::createScaleTranslateB2DHomMatrix( |
299 | 0 | aViewBox.getRange(), |
300 | 0 | aViewBox.getMinimum())); |
301 | 0 | } |
302 | |
|
303 | 0 | if(aNewTarget.empty()) |
304 | 0 | return; |
305 | | |
306 | | // create mapping |
307 | 0 | const SvgAspectRatio& rRatio = maSvgAspectRatio; |
308 | | |
309 | | // even when ratio is not set, use the defaults |
310 | | // let mapping be created from SvgAspectRatio |
311 | 0 | const basegfx::B2DHomMatrix aEmbeddingTransform(rRatio.createMapping(aTarget, aViewBox)); |
312 | |
|
313 | 0 | if(!aEmbeddingTransform.isIdentity()) |
314 | 0 | { |
315 | 0 | const drawinglayer::primitive2d::Primitive2DReference xRef( |
316 | 0 | new drawinglayer::primitive2d::TransformPrimitive2D( |
317 | 0 | aEmbeddingTransform, |
318 | 0 | std::move(aNewTarget))); |
319 | |
|
320 | 0 | aNewTarget = drawinglayer::primitive2d::Primitive2DContainer { xRef }; |
321 | 0 | } |
322 | |
|
323 | 0 | if(!rRatio.isMeetOrSlice()) |
324 | 0 | { |
325 | | // need to embed in MaskPrimitive2D to ensure clipping |
326 | 0 | const drawinglayer::primitive2d::Primitive2DReference xMask( |
327 | 0 | new drawinglayer::primitive2d::MaskPrimitive2D( |
328 | 0 | basegfx::B2DPolyPolygon( |
329 | 0 | basegfx::utils::createPolygonFromRect(aTarget)), |
330 | 0 | std::move(aNewTarget))); |
331 | |
|
332 | 0 | aNewTarget = drawinglayer::primitive2d::Primitive2DContainer { xMask }; |
333 | 0 | } |
334 | | |
335 | | // embed and add to rTarget, take local extra-transform into account |
336 | 0 | pStyle->add_postProcess(rTarget, std::move(aNewTarget), getTransform()); |
337 | 0 | } |
338 | | |
339 | | } // end of namespace svgio::svgreader |
340 | | |
341 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |