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 <svgio/svgreader/svgsvgnode.hxx>
21 #include <drawinglayer/geometry/viewinformation2d.hxx>
22 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
23 #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
24 #include <basegfx/polygon/b2dpolygontools.hxx>
25 #include <basegfx/polygon/b2dpolygon.hxx>
26 #include <basegfx/matrix/b2dhommatrixtools.hxx>
27 #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
28 #include <drawinglayer/primitive2d/hiddengeometryprimitive2d.hxx>
30 //////////////////////////////////////////////////////////////////////////////
36 SvgSvgNode::SvgSvgNode(
37 SvgDocument
& rDocument
,
39 : SvgNode(SVGTokenSvg
, rDocument
, pParent
),
40 maSvgStyleAttributes(*this),
51 // initial fill is black
52 maSvgStyleAttributes
.setFill(SvgPaint(basegfx::BColor(0.0, 0.0, 0.0), true, true));
56 SvgSvgNode::~SvgSvgNode()
58 if(mpViewBox
) delete mpViewBox
;
61 const SvgStyleAttributes
* SvgSvgNode::getSvgStyleAttributes() const
63 return &maSvgStyleAttributes
;
66 void SvgSvgNode::parseAttribute(const OUString
& rTokenName
, SVGToken aSVGToken
, const OUString
& aContent
)
69 SvgNode::parseAttribute(rTokenName
, aSVGToken
, aContent
);
71 // read style attributes
72 maSvgStyleAttributes
.parseStyleAttribute(rTokenName
, aSVGToken
, aContent
);
79 maSvgStyleAttributes
.readStyle(aContent
);
84 const basegfx::B2DRange
aRange(readViewBox(aContent
, *this));
92 case SVGTokenPreserveAspectRatio
:
94 setSvgAspectRatio(readSvgAspectRatio(aContent
));
101 if(readSingleNumber(aContent
, aNum
))
111 if(readSingleNumber(aContent
, aNum
))
121 if(readSingleNumber(aContent
, aNum
))
123 if(aNum
.isPositive())
134 if(readSingleNumber(aContent
, aNum
))
136 if(aNum
.isPositive())
143 case SVGTokenVersion
:
147 if(readSingleNumber(aContent
, aNum
))
160 void SvgSvgNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence
& rTarget
, bool bReferenced
) const
162 drawinglayer::primitive2d::Primitive2DSequence aSequence
;
165 SvgNode::decomposeSvgNode(aSequence
, bReferenced
);
167 if(aSequence
.hasElements())
173 // Svg defines that with no width or no height the viewBox content is empty,
174 // so both need to exist
175 if(!basegfx::fTools::equalZero(getViewBox()->getWidth()) && !basegfx::fTools::equalZero(getViewBox()->getHeight()))
177 // create target range homing x,y, width and height as given
178 const double fX(getX().isSet() ? getX().solve(*this, xcoordinate
) : 0.0);
179 const double fY(getY().isSet() ? getY().solve(*this, ycoordinate
) : 0.0);
180 const double fW(getWidth().isSet() ? getWidth().solve(*this, xcoordinate
) : getViewBox()->getWidth());
181 const double fH(getHeight().isSet() ? getHeight().solve(*this, ycoordinate
) : getViewBox()->getHeight());
182 const basegfx::B2DRange
aTarget(fX
, fY
, fX
+ fW
, fY
+ fH
);
184 if(aTarget
.equal(*getViewBox()))
186 // no mapping needed, append
187 drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget
, aSequence
);
192 const SvgAspectRatio
& rRatio
= getSvgAspectRatio();
196 // let mapping be created from SvgAspectRatio
197 const basegfx::B2DHomMatrix
aEmbeddingTransform(
198 rRatio
.createMapping(aTarget
, *getViewBox()));
200 // prepare embedding in transformation
201 const drawinglayer::primitive2d::Primitive2DReference
xRef(
202 new drawinglayer::primitive2d::TransformPrimitive2D(
206 if(rRatio
.isMeetOrSlice())
208 // embed in transformation
209 drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget
, xRef
);
213 // need to embed in MaskPrimitive2D, too
214 const drawinglayer::primitive2d::Primitive2DReference
xMask(
215 new drawinglayer::primitive2d::MaskPrimitive2D(
216 basegfx::B2DPolyPolygon(basegfx::tools::createPolygonFromRect(aTarget
)),
217 drawinglayer::primitive2d::Primitive2DSequence(&xRef
, 1)));
219 drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget
, xMask
);
224 // choose default mapping
225 const basegfx::B2DHomMatrix
aEmbeddingTransform(
226 rRatio
.createLinearMapping(
227 aTarget
, *getViewBox()));
229 // embed in transformation
230 const drawinglayer::primitive2d::Primitive2DReference
xTransform(
231 new drawinglayer::primitive2d::TransformPrimitive2D(
235 drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget
, xTransform
);
242 // check if we have a size
243 const double fW(getWidth().isSet() ? getWidth().solve(*this, xcoordinate
) : 0.0);
244 const double fH(getHeight().isSet() ? getHeight().solve(*this, ycoordinate
) : 0.0);
246 // Svg defines that a negative value is an error and that 0.0 disables rendering
247 if(basegfx::fTools::more(fW
, 0.0) && basegfx::fTools::more(fH
, 0.0))
249 // check if we have a x,y position
250 const double fX(getX().isSet() ? getX().solve(*this, xcoordinate
) : 0.0);
251 const double fY(getY().isSet() ? getY().solve(*this, ycoordinate
) : 0.0);
253 if(!basegfx::fTools::equalZero(fX
) || !basegfx::fTools::equalZero(fY
))
255 // embed in transform
256 const drawinglayer::primitive2d::Primitive2DReference
xRef(
257 new drawinglayer::primitive2d::TransformPrimitive2D(
258 basegfx::tools::createTranslateB2DHomMatrix(fX
, fY
),
261 aSequence
= drawinglayer::primitive2d::Primitive2DSequence(&xRef
, 1);
264 // embed in MaskPrimitive2D to clip
265 const drawinglayer::primitive2d::Primitive2DReference
xMask(
266 new drawinglayer::primitive2d::MaskPrimitive2D(
267 basegfx::B2DPolyPolygon(
268 basegfx::tools::createPolygonFromRect(
269 basegfx::B2DRange(fX
, fY
, fX
+ fW
, fY
+ fH
))),
273 drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget
, xMask
);
279 // Outermost SVG element; create target range homing width and height as given.
280 // SVG defines that x,y has no meanig for the outermost SVG element. Use a fallback
281 // width and height of din A 4 (21 x 29,7 cm)
282 double fW(getWidth().isSet() ? getWidth().solve(*this, xcoordinate
) : (210.0 * 3.543307));
283 double fH(getHeight().isSet() ? getHeight().solve(*this, ycoordinate
) : (297.0 * 3.543307));
285 // Svg defines that a negative value is an error and that 0.0 disables rendering
286 if(basegfx::fTools::more(fW
, 0.0) && basegfx::fTools::more(fH
, 0.0))
288 const basegfx::B2DRange
aSvgCanvasRange(0.0, 0.0, fW
, fH
);
292 if(!basegfx::fTools::equalZero(getViewBox()->getWidth()) && !basegfx::fTools::equalZero(getViewBox()->getHeight()))
295 const SvgAspectRatio
& rRatio
= getSvgAspectRatio();
296 basegfx::B2DHomMatrix aViewBoxMapping
;
300 // let mapping be created from SvgAspectRatio
301 aViewBoxMapping
= rRatio
.createMapping(aSvgCanvasRange
, *getViewBox());
303 // no need to check ratio here for slice, the outermost Svg will
304 // be clipped anyways (see below)
308 // choose default mapping
309 aViewBoxMapping
= rRatio
.createLinearMapping(aSvgCanvasRange
, *getViewBox());
312 // scale content to viewBox definitions
313 const drawinglayer::primitive2d::Primitive2DReference
xTransform(
314 new drawinglayer::primitive2d::TransformPrimitive2D(
318 aSequence
= drawinglayer::primitive2d::Primitive2DSequence(&xTransform
, 1);
322 // to be completely correct in Svg sense it is necessary to clip
323 // the whole content to the given canvas. I choose here to do this
324 // initially despite I found various examples of Svg files out there
325 // which have no correct values for this clipping. It's correct
326 // due to the Svg spec.
327 bool bDoCorrectCanvasClipping(true);
329 if(bDoCorrectCanvasClipping
)
331 // different from Svg we have the possibility with primitives to get
332 // a correct bounding box for the geometry. Get it for evtl. taking action
333 const basegfx::B2DRange
aContentRange(
334 drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence(
336 drawinglayer::geometry::ViewInformation2D()));
338 if(aSvgCanvasRange
.isInside(aContentRange
))
340 // no clip needed, but an invisible HiddenGeometryPrimitive2D
341 // to allow getting the full Svg range using the primitive mechanisms.
342 // This is needed since e.g. an SdrObject using this as graphic will
343 // create a mapping transformation to exactly map the content to it's
345 const drawinglayer::primitive2d::Primitive2DReference
xLine(
346 new drawinglayer::primitive2d::PolygonHairlinePrimitive2D(
347 basegfx::tools::createPolygonFromRect(
349 basegfx::BColor(0.0, 0.0, 0.0)));
350 const drawinglayer::primitive2d::Primitive2DReference
xHidden(
351 new drawinglayer::primitive2d::HiddenGeometryPrimitive2D(
352 drawinglayer::primitive2d::Primitive2DSequence(&xLine
, 1)));
354 drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(aSequence
, xHidden
);
356 else if(aSvgCanvasRange
.overlaps(aContentRange
))
358 // Clip is necessary. This will make Svg images evtl. smaller
359 // than wanted from Svg (the free space which may be around it is
360 // conform to the Svg spec), but avoids an expensive and unneccessary
361 // clip. Keep the full Svg range here to get the correct mappings
362 // to objects using this. Optimizations can be done in the processors
363 const drawinglayer::primitive2d::Primitive2DReference
xMask(
364 new drawinglayer::primitive2d::MaskPrimitive2D(
365 basegfx::B2DPolyPolygon(
366 basegfx::tools::createPolygonFromRect(
370 aSequence
= drawinglayer::primitive2d::Primitive2DSequence(&xMask
, 1);
374 // not inside, no overlap. Empty Svg
375 aSequence
.realloc(0);
379 if(aSequence
.hasElements())
381 // embed in transform primitive to scale to 1/100th mm
382 // where 1 mm == 3.543307 px to get from Svg coordinates to
384 const double fScaleTo100thmm(100.0 / 3.543307);
385 const basegfx::B2DHomMatrix
aTransform(
386 basegfx::tools::createScaleB2DHomMatrix(
390 const drawinglayer::primitive2d::Primitive2DReference
xTransform(
391 new drawinglayer::primitive2d::TransformPrimitive2D(
395 aSequence
= drawinglayer::primitive2d::Primitive2DSequence(&xTransform
, 1);
398 drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget
, aSequence
);
405 const basegfx::B2DRange
* SvgSvgNode::getCurrentViewPort() const
413 return SvgNode::getCurrentViewPort();
417 } // end of namespace svgreader
418 } // end of namespace svgio
420 //////////////////////////////////////////////////////////////////////////////
423 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */