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 <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/PolygonHairlinePrimitive2D.hxx>
28 #include <drawinglayer/primitive2d/hiddengeometryprimitive2d.hxx>
29 #include <o3tl/unit_conversion.hxx>
30 #include <svgdocument.hxx>
32 namespace svgio::svgreader
34 SvgSvgNode::SvgSvgNode(
35 SvgDocument
& rDocument
,
37 : SvgNode(SVGToken::Svg
, rDocument
, pParent
),
38 maSvgStyleAttributes(*this),
39 mbStyleAttributesInitialized(false) // #i125258#
44 void SvgSvgNode::initializeStyleAttributes()
46 if(mbStyleAttributesInitialized
)
49 // #i125258# determine if initial values need to be initialized with hard values
50 // for the case that this is the outmost SVG statement and it has no parent
51 // stale (CssStyle for svg may be defined)
52 bool bSetInitialValues(true);
56 // #i125258# no initial values when it's a SVG element embedded in SVG
57 bSetInitialValues
= false;
62 const SvgStyleAttributes
* pStyles
= getSvgStyleAttributes();
64 if(pStyles
&& pStyles
->getParentStyle())
66 // SVG has a parent style (probably CssStyle), check if fill is set there anywhere
67 // already. If yes, do not set the default fill (black)
69 const SvgStyleAttributes
* pParentStyle
= pStyles
->getParentStyle();
71 while(pParentStyle
&& !bFillSet
)
73 bFillSet
= pParentStyle
->isFillSet();
74 pParentStyle
= pParentStyle
->getParentStyle();
79 // #125258# no initial values when SVG has a parent style at which a fill
81 bSetInitialValues
= false;
88 // #i125258# only set if not yet initialized (SvgSvgNode::parseAttribute is already done,
89 // just setting may revert an already set valid value)
90 if(!maSvgStyleAttributes
.isFillSet())
92 // #i125258# initial fill is black (see SVG1.1 spec)
93 maSvgStyleAttributes
.setFill(SvgPaint(basegfx::BColor(0.0, 0.0, 0.0), true, true));
97 mbStyleAttributesInitialized
= true;
100 SvgSvgNode::~SvgSvgNode()
104 const SvgStyleAttributes
* SvgSvgNode::getSvgStyleAttributes() const
106 // #i125258# svg node can have CssStyles, too, so check for it here
107 return checkForCssStyle("svg", maSvgStyleAttributes
);
110 void SvgSvgNode::parseAttribute(const OUString
& rTokenName
, SVGToken aSVGToken
, const OUString
& aContent
)
113 SvgNode::parseAttribute(rTokenName
, aSVGToken
, aContent
);
115 // read style attributes
116 maSvgStyleAttributes
.parseStyleAttribute(aSVGToken
, aContent
);
121 case SVGToken::Style
:
123 readLocalCssStyle(aContent
);
126 case SVGToken::ViewBox
:
128 const basegfx::B2DRange
aRange(readViewBox(aContent
, *this));
130 if(!aRange
.isEmpty())
136 case SVGToken::PreserveAspectRatio
:
138 maSvgAspectRatio
= readSvgAspectRatio(aContent
);
145 if(readSingleNumber(aContent
, aNum
))
155 if(readSingleNumber(aContent
, aNum
))
161 case SVGToken::Width
:
165 if(readSingleNumber(aContent
, aNum
))
167 if(aNum
.isPositive())
174 case SVGToken::Height
:
178 if(readSingleNumber(aContent
, aNum
))
180 if(aNum
.isPositive())
187 case SVGToken::Version
:
191 if(readSingleNumber(aContent
, aNum
))
204 void SvgSvgNode::seekReferenceWidth(double& fWidth
, bool& bHasFound
) const
206 if (!getParent() || bHasFound
)
210 const SvgSvgNode
* pParentSvgSvgNode
= nullptr;
211 // enclosing svg might have relative width, need to cumulate them till they are
212 // resolved somewhere up in the node tree
213 double fPercentage(1.0);
214 for(const SvgNode
* pParent
= getParent(); pParent
&& !bHasFound
; pParent
= pParent
->getParent())
216 // dynamic_cast results Null-pointer for not SvgSvgNode and so skips them in if condition
217 pParentSvgSvgNode
= dynamic_cast< const SvgSvgNode
* >(pParent
);
218 if (pParentSvgSvgNode
)
220 if (pParentSvgSvgNode
->getViewBox())
222 // viewbox values are already in 'user unit'.
223 fWidth
= pParentSvgSvgNode
->getViewBox()->getWidth() * fPercentage
;
228 // take absolute value or cumulate percentage
229 if (pParentSvgSvgNode
->getWidth().isSet())
231 if (SvgUnit::percent
== pParentSvgSvgNode
->getWidth().getUnit())
233 fPercentage
*= pParentSvgSvgNode
->getWidth().getNumber() * 0.01;
237 fWidth
= pParentSvgSvgNode
->getWidth().solveNonPercentage(*pParentSvgSvgNode
) * fPercentage
;
240 } // not set => width=100% => factor 1, no need for else
246 void SvgSvgNode::seekReferenceHeight(double& fHeight
, bool& bHasFound
) const
248 if (!getParent() || bHasFound
)
252 const SvgSvgNode
* pParentSvgSvgNode
= nullptr;
253 // enclosing svg might have relative width and height, need to cumulate them till they are
254 // resolved somewhere up in the node tree
255 double fPercentage(1.0);
256 for(const SvgNode
* pParent
= getParent(); pParent
&& !bHasFound
; pParent
= pParent
->getParent())
258 // dynamic_cast results Null-pointer for not SvgSvgNode and so skips them in if condition
259 pParentSvgSvgNode
= dynamic_cast< const SvgSvgNode
* >(pParent
);
260 if (pParentSvgSvgNode
)
262 if (pParentSvgSvgNode
->getViewBox())
264 // viewbox values are already in 'user unit'.
265 fHeight
= pParentSvgSvgNode
->getViewBox()->getHeight() * fPercentage
;
270 // take absolute value or cumulate percentage
271 if (pParentSvgSvgNode
->getHeight().isSet())
273 if (SvgUnit::percent
== pParentSvgSvgNode
->getHeight().getUnit())
275 fPercentage
*= pParentSvgSvgNode
->getHeight().getNumber() * 0.01;
279 fHeight
= pParentSvgSvgNode
->getHeight().solveNonPercentage(*pParentSvgSvgNode
) * fPercentage
;
282 } // not set => height=100% => factor 1, no need for else
288 // ToDo: Consider attribute overflow in method decomposeSvgNode
289 void SvgSvgNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer
& rTarget
, bool bReferenced
) const
291 drawinglayer::primitive2d::Primitive2DContainer aSequence
;
293 // #i125258# check now if we need to init some style settings locally. Do not do this
294 // in the constructor, there is not yet information e.g. about existing CssStyles.
295 // Here all nodes are read and interpreted
296 const_cast< SvgSvgNode
* >(this)->initializeStyleAttributes();
298 // decompose children
299 SvgNode::decomposeSvgNode(aSequence
, bReferenced
);
301 if(!aSequence
.empty())
305 // #i122594# if width/height is not given, it's 100% (see 5.1.2 The 'svg' element in SVG1.1 spec).
306 // If it is relative, the question is to what. The previous implementation assumed relative to the
307 // local ViewBox which is implied by (4.2 Basic data types):
309 // "Note that the non-property <length> definition also allows a percentage unit identifier.
310 // The meaning of a percentage length value depends on the attribute for which the percentage
311 // length value has been specified. Two common cases are: (a) when a percentage length value
312 // represents a percentage of the viewport width or height (refer to the section that discusses
313 // units in general), and (b) when a percentage length value represents a percentage of the
314 // bounding box width or height on a given object (refer to the section that describes object
315 // bounding box units)."
317 // Comparisons with common browsers show that it's mostly interpreted relative to the viewport
318 // of the parent, and so does the new implementation.
320 // Extract known viewport data
321 // bXXXIsAbsolute tracks whether relative values could be resolved to absolute values
323 // If width or height is not provided, the default 100% is used, see SVG 1.1 section 5.1.2
324 // value 0.0 here is only to initialize variable
325 bool bWidthIsAbsolute(getWidth().isSet() && SvgUnit::percent
!= getWidth().getUnit());
326 double fW( bWidthIsAbsolute
? getWidth().solveNonPercentage(*this) : 0.0);
328 bool bHeightIsAbsolute(getHeight().isSet() && SvgUnit::percent
!= getHeight().getUnit());
329 double fH( bHeightIsAbsolute
? getHeight().solveNonPercentage(*this) : 0.0);
331 // If x or y not provided, then default 0.0 is used, see SVG 1.1 Section 5.1.2
332 bool bXIsAbsolute((getX().isSet() && SvgUnit::percent
!= getX().getUnit()) || !getX().isSet());
333 double fX( bXIsAbsolute
&& getX().isSet() ? getX().solveNonPercentage(*this) : 0.0);
335 bool bYIsAbsolute((getY().isSet() && SvgUnit::percent
!= getY().getUnit()) || !getY().isSet());
336 double fY( bYIsAbsolute
&& getY().isSet() ? getY().solveNonPercentage(*this) : 0.0);
338 if ( !bXIsAbsolute
|| !bWidthIsAbsolute
)
340 // get width of enclosing svg and resolve percentage in x and width;
341 double fWReference(0.0);
342 bool bHasFoundWidth(false);
343 seekReferenceWidth(fWReference
, bHasFoundWidth
);
348 fWReference
= getViewBox()->getWidth();
352 // Even outermost svg has not all information to resolve relative values,
353 // I use content itself as fallback to set missing values for viewport
354 // Any better idea for such ill structured svg documents?
355 const basegfx::B2DRange
aChildRange(
356 aSequence
.getB2DRange(
357 drawinglayer::geometry::ViewInformation2D()));
358 fWReference
= aChildRange
.getWidth();
361 // referenced values are already in 'user unit'
364 fX
= getX().getNumber() * 0.01 * fWReference
;
366 if (!bWidthIsAbsolute
)
368 fW
= (getWidth().isSet() ? getWidth().getNumber() *0.01 : 1.0) * fWReference
;
372 if ( !bYIsAbsolute
|| !bHeightIsAbsolute
)
374 // get height of enclosing svg and resolve percentage in y and height
375 double fHReference(0.0);
376 bool bHasFoundHeight(false);
377 seekReferenceHeight(fHReference
, bHasFoundHeight
);
378 if (!bHasFoundHeight
)
382 fHReference
= getViewBox()->getHeight();
386 // Even outermost svg has not all information to resolve relative values,
387 // I use content itself as fallback to set missing values for viewport
388 // Any better idea for such ill structured svg documents?
389 const basegfx::B2DRange
aChildRange(
390 aSequence
.getB2DRange(
391 drawinglayer::geometry::ViewInformation2D()));
392 fHReference
= aChildRange
.getHeight();
396 // referenced values are already in 'user unit'
399 fY
= getY().getNumber() * 0.01 * fHReference
;
401 if (!bHeightIsAbsolute
)
403 fH
= (getHeight().isSet() ? getHeight().getNumber() *0.01 : 1.0) * fHReference
;
409 // SVG 1.1 defines in section 7.7 that a negative value for width or height
410 // in viewBox is an error and that 0.0 disables rendering
411 if(basegfx::fTools::more(getViewBox()->getWidth(),0.0) && basegfx::fTools::more(getViewBox()->getHeight(),0.0))
413 // create target range homing x,y, width and height as calculated above
414 const basegfx::B2DRange
aTarget(fX
, fY
, fX
+ fW
, fY
+ fH
);
416 if(aTarget
.equal(*getViewBox()))
418 // no mapping needed, append
419 rTarget
.append(aSequence
);
424 // #i122610 SVG 1.1 defines in section 5.1.2 that if the attribute preserveAspectRatio is not specified,
425 // then the effect is as if a value of 'xMidYMid meet' were specified.
426 SvgAspectRatio
aRatioDefault(SvgAlign::xMidYMid
,true);
427 const SvgAspectRatio
& rRatio
= getSvgAspectRatio().isSet()? getSvgAspectRatio() : aRatioDefault
;
429 // let mapping be created from SvgAspectRatio
430 const basegfx::B2DHomMatrix
aEmbeddingTransform(
431 rRatio
.createMapping(aTarget
, *getViewBox()));
433 // prepare embedding in transformation
434 const drawinglayer::primitive2d::Primitive2DReference
xRef(
435 new drawinglayer::primitive2d::TransformPrimitive2D(
437 drawinglayer::primitive2d::Primitive2DContainer(aSequence
)));
439 if(rRatio
.isMeetOrSlice())
441 // embed in transformation
442 rTarget
.push_back(xRef
);
446 // need to embed in MaskPrimitive2D, too
447 const drawinglayer::primitive2d::Primitive2DReference
xMask(
448 new drawinglayer::primitive2d::MaskPrimitive2D(
449 basegfx::B2DPolyPolygon(basegfx::utils::createPolygonFromRect(aTarget
)),
450 drawinglayer::primitive2d::Primitive2DContainer
{ xRef
}));
452 rTarget
.push_back(xMask
);
457 else // no viewBox attribute
459 // Svg defines that a negative value is an error and that 0.0 disables rendering
460 if(basegfx::fTools::more(fW
, 0.0) && basegfx::fTools::more(fH
, 0.0))
462 if(!basegfx::fTools::equalZero(fX
) || !basegfx::fTools::equalZero(fY
))
464 // embed in transform
465 const drawinglayer::primitive2d::Primitive2DReference
xRef(
466 new drawinglayer::primitive2d::TransformPrimitive2D(
467 basegfx::utils::createTranslateB2DHomMatrix(fX
, fY
),
468 std::move(aSequence
)));
470 aSequence
= drawinglayer::primitive2d::Primitive2DContainer
{ xRef
, };
473 // embed in MaskPrimitive2D to clip
474 const drawinglayer::primitive2d::Primitive2DReference
xMask(
475 new drawinglayer::primitive2d::MaskPrimitive2D(
476 basegfx::B2DPolyPolygon(
477 basegfx::utils::createPolygonFromRect(
478 basegfx::B2DRange(fX
, fY
, fX
+ fW
, fY
+ fH
))),
479 drawinglayer::primitive2d::Primitive2DContainer(aSequence
)));
482 rTarget
.push_back(xMask
);
486 else // Outermost SVG element
488 // Svg defines that a negative value is an error and that 0.0 disables rendering
489 // isPositive() not usable because it allows 0.0 in contrast to mathematical definition of 'positive'
490 const bool bWidthInvalid(getWidth().isSet() && basegfx::fTools::lessOrEqual(getWidth().getNumber(), 0.0));
491 const bool bHeightInvalid(getHeight().isSet() && basegfx::fTools::lessOrEqual(getHeight().getNumber(), 0.0));
492 if(!bWidthInvalid
&& !bHeightInvalid
)
494 basegfx::B2DRange aSvgCanvasRange
; // viewport
495 double fW
= 0.0; // dummy values
497 if (const basegfx::B2DRange
* pBox
= getViewBox())
499 // SVG 1.1 defines in section 7.7 that a negative value for width or height
500 // in viewBox is an error and that 0.0 disables rendering
501 const double fViewBoxWidth
= pBox
->getWidth();
502 const double fViewBoxHeight
= pBox
->getHeight();
503 if(basegfx::fTools::more(fViewBoxWidth
,0.0) && basegfx::fTools::more(fViewBoxHeight
,0.0))
505 // The intrinsic aspect ratio of the svg element is given by absolute values of svg width and svg height
506 // or by the width and height of the viewBox, if svg width or svg height is relative.
507 // see SVG 1.1 section 7.12
508 bool bNeedsMapping(true);
509 const bool bWidthIsAbsolute(getWidth().isSet() && SvgUnit::percent
!= getWidth().getUnit());
510 const bool bHeightIsAbsolute(getHeight().isSet() && SvgUnit::percent
!= getHeight().getUnit());
511 const double fViewBoxRatio(fViewBoxWidth
/fViewBoxHeight
);
512 if(bWidthIsAbsolute
&& bHeightIsAbsolute
)
514 fW
= getWidth().solveNonPercentage(*this);
515 fH
= getHeight().solveNonPercentage(*this);
516 aSvgCanvasRange
= basegfx::B2DRange(0.0, 0.0, fW
, fH
);
518 else if (bWidthIsAbsolute
)
520 fW
= getWidth().solveNonPercentage(*this);
521 fH
= fW
/ fViewBoxRatio
;
522 aSvgCanvasRange
= basegfx::B2DRange(0.0, 0.0, fW
, fH
);
524 else if (bHeightIsAbsolute
)
526 fH
= getHeight().solveNonPercentage(*this);
527 fW
= fH
* fViewBoxRatio
;
528 aSvgCanvasRange
= basegfx::B2DRange(0.0, 0.0, fW
, fH
);
532 // There exists no parent to resolve relative width or height.
533 // Use child size as fallback and expand to aspect ratio given
534 // by the viewBox. No mapping.
535 // We get viewport >= content, therefore no clipping.
536 bNeedsMapping
= false;
538 const double fChildWidth(pBox
->getWidth());
539 const double fChildHeight(pBox
->getHeight());
540 const double fLeft(pBox
->getMinX());
541 const double fTop(pBox
->getMinY());
542 if ( fChildWidth
/ fViewBoxWidth
> fChildHeight
/ fViewBoxHeight
)
545 fH
= fChildWidth
/ fViewBoxRatio
;
550 fW
= fChildHeight
* fViewBoxRatio
;
552 aSvgCanvasRange
= basegfx::B2DRange(fLeft
, fTop
, fLeft
+ fW
, fTop
+ fH
);
558 // SVG 1.1 defines in section 5.1.2 that if the attribute preserveAspectRatio is not specified,
559 // then the effect is as if a value of 'xMidYMid meet' were specified.
560 SvgAspectRatio
aRatioDefault(SvgAlign::xMidYMid
, true);
561 const SvgAspectRatio
& rRatio
= getSvgAspectRatio().isSet()? getSvgAspectRatio() : aRatioDefault
;
563 basegfx::B2DHomMatrix aViewBoxMapping
= rRatio
.createMapping(aSvgCanvasRange
, *pBox
);
564 // no need to check ratio here for slice, the outermost Svg will
565 // be clipped anyways (see below)
567 // scale content to viewBox definitions
568 const drawinglayer::primitive2d::Primitive2DReference
xTransform(
569 new drawinglayer::primitive2d::TransformPrimitive2D(
571 std::move(aSequence
)));
573 aSequence
= drawinglayer::primitive2d::Primitive2DContainer
{ xTransform
};
577 else // no viewbox => no mapping
579 const bool bWidthIsAbsolute(getWidth().isSet() && SvgUnit::percent
!= getWidth().getUnit());
580 const bool bHeightIsAbsolute(getHeight().isSet() && SvgUnit::percent
!= getHeight().getUnit());
581 if (bWidthIsAbsolute
&& bHeightIsAbsolute
)
583 fW
=getWidth().solveNonPercentage(*this);
584 fH
=getHeight().solveNonPercentage(*this);
585 aSvgCanvasRange
= basegfx::B2DRange(0.0, 0.0, fW
, fH
);
589 // There exists no parent to resolve relative width or height.
590 // Use child size as fallback. We get viewport >= content, therefore no clipping.
591 const basegfx::B2DRange
aChildRange(
592 aSequence
.getB2DRange(
593 drawinglayer::geometry::ViewInformation2D()));
594 const double fChildWidth(aChildRange
.getWidth());
595 const double fChildHeight(aChildRange
.getHeight());
596 const double fChildLeft(aChildRange
.getMinX());
597 const double fChildTop(aChildRange
.getMinY());
598 fW
= bWidthIsAbsolute
? getWidth().solveNonPercentage(*this) : fChildWidth
;
599 fH
= bHeightIsAbsolute
? getHeight().solveNonPercentage(*this) : fChildHeight
;
600 const double fLeft(bWidthIsAbsolute
? 0.0 : fChildLeft
);
601 const double fTop(bHeightIsAbsolute
? 0.0 : fChildTop
);
602 aSvgCanvasRange
= basegfx::B2DRange(fLeft
, fTop
, fLeft
+fW
, fTop
+fH
);
607 // to be completely correct in Svg sense it is necessary to clip
608 // the whole content to the given canvas. I choose here to do this
609 // initially despite I found various examples of Svg files out there
610 // which have no correct values for this clipping. It's correct
611 // due to the Svg spec.
613 // different from Svg we have the possibility with primitives to get
614 // a correct bounding box for the geometry. Get it for evtl. taking action
615 const basegfx::B2DRange
aContentRange(
616 aSequence
.getB2DRange(
617 drawinglayer::geometry::ViewInformation2D()));
619 if(aSvgCanvasRange
.isInside(aContentRange
))
621 // no clip needed, but an invisible HiddenGeometryPrimitive2D
622 // to allow getting the full Svg range using the primitive mechanisms.
623 // This is needed since e.g. an SdrObject using this as graphic will
624 // create a mapping transformation to exactly map the content to its
626 const drawinglayer::primitive2d::Primitive2DReference
xLine(
627 new drawinglayer::primitive2d::PolygonHairlinePrimitive2D(
628 basegfx::utils::createPolygonFromRect(
630 basegfx::BColor(0.0, 0.0, 0.0)));
631 const drawinglayer::primitive2d::Primitive2DReference
xHidden(
632 new drawinglayer::primitive2d::HiddenGeometryPrimitive2D(
633 drawinglayer::primitive2d::Primitive2DContainer
{ xLine
}));
635 aSequence
.push_back(xHidden
);
637 else if(aSvgCanvasRange
.overlaps(aContentRange
))
639 // Clip is necessary. This will make Svg images evtl. smaller
640 // than wanted from Svg (the free space which may be around it is
641 // conform to the Svg spec), but avoids an expensive and unnecessary
642 // clip. Keep the full Svg range here to get the correct mappings
643 // to objects using this. Optimizations can be done in the processors
644 const drawinglayer::primitive2d::Primitive2DReference
xMask(
645 new drawinglayer::primitive2d::MaskPrimitive2D(
646 basegfx::B2DPolyPolygon(
647 basegfx::utils::createPolygonFromRect(
649 std::move(aSequence
)));
651 aSequence
= drawinglayer::primitive2d::Primitive2DContainer
{ xMask
};
655 // not inside, no overlap. Empty Svg
659 if(!aSequence
.empty())
661 // Another correction:
662 // If no Width/Height is set (usually done in
663 // <svg ... width="215.9mm" height="279.4mm" >) which
664 // is the case for own-Impress-exports, assume that
665 // the Units are already 100ThMM.
666 // Maybe only for own-Impress-exports, thus may need to be
667 // &&ed with getDocument().findSvgNodeById("ooo:meta_slides"),
668 // but does not need to be.
669 bool bEmbedInFinalTransformPxTo100ThMM(true);
671 if(getDocument().findSvgNodeById("ooo:meta_slides")
672 && !getWidth().isSet()
673 && !getHeight().isSet())
675 bEmbedInFinalTransformPxTo100ThMM
= false;
678 if(bEmbedInFinalTransformPxTo100ThMM
)
680 // embed in transform primitive to scale to 1/100th mm
681 // to get from Svg coordinates (px) to drawinglayer coordinates
682 constexpr double fScaleTo100thmm(o3tl::convert(1.0, o3tl::Length::px
, o3tl::Length::mm100
));
683 const basegfx::B2DHomMatrix
aTransform(
684 basegfx::utils::createScaleB2DHomMatrix(
688 const drawinglayer::primitive2d::Primitive2DReference
xTransform(
689 new drawinglayer::primitive2d::TransformPrimitive2D(
691 std::move(aSequence
)));
693 aSequence
= drawinglayer::primitive2d::Primitive2DContainer
{ xTransform
};
697 rTarget
.append(aSequence
);
703 if(!(aSequence
.empty() && !getParent() && getViewBox()))
706 // tdf#118232 No geometry, Outermost SVG element and we have a ViewBox.
707 // Create a HiddenGeometry Primitive containing an expanded
708 // hairline geometry to have the size contained
709 const drawinglayer::primitive2d::Primitive2DReference
xLine(
710 new drawinglayer::primitive2d::PolygonHairlinePrimitive2D(
711 basegfx::utils::createPolygonFromRect(
713 basegfx::BColor(0.0, 0.0, 0.0)));
714 const drawinglayer::primitive2d::Primitive2DReference
xHidden(
715 new drawinglayer::primitive2d::HiddenGeometryPrimitive2D(
716 drawinglayer::primitive2d::Primitive2DContainer
{ xLine
}));
718 rTarget
.push_back(xHidden
);
721 basegfx::B2DRange
SvgSvgNode::getCurrentViewPort() const
725 return *(getViewBox());
727 else // viewport should be given by x, y, width, and height
729 // Extract known viewport data
730 // bXXXIsAbsolute tracks whether relative values could be resolved to absolute values
733 // If width or height is not provided, the default 100% is used, see SVG 1.1 section 5.1.2
734 // value 0.0 here is only to initialize variable
735 bool bWidthIsAbsolute(getWidth().isSet() && SvgUnit::percent
!= getWidth().getUnit());
736 double fW( bWidthIsAbsolute
? getWidth().solveNonPercentage(*this) : 0.0);
737 bool bHeightIsAbsolute(getHeight().isSet() && SvgUnit::percent
!= getHeight().getUnit());
738 double fH( bHeightIsAbsolute
? getHeight().solveNonPercentage(*this) : 0.0);
740 // If x or y not provided, then default 0.0 is used, see SVG 1.1 Section 5.1.2
741 bool bXIsAbsolute((getX().isSet() && SvgUnit::percent
!= getX().getUnit()) || !getX().isSet());
742 double fX( bXIsAbsolute
&& getX().isSet() ? getX().solveNonPercentage(*this) : 0.0);
744 bool bYIsAbsolute((getY().isSet() && SvgUnit::percent
!= getY().getUnit()) || !getY().isSet());
745 double fY( bYIsAbsolute
&& getY().isSet() ? getY().solveNonPercentage(*this) : 0.0);
747 if (bXIsAbsolute
&& bYIsAbsolute
&& bWidthIsAbsolute
&& bHeightIsAbsolute
)
749 return basegfx::B2DRange(fX
, fY
, fX
+fW
, fY
+fH
);
751 else // try to resolve relative values
753 if (!bXIsAbsolute
|| !bWidthIsAbsolute
)
755 // get width of enclosing svg and resolve percentage in x and width
756 double fWReference(0.0);
757 bool bHasFoundWidth(false);
758 seekReferenceWidth(fWReference
, bHasFoundWidth
);
759 // referenced values are already in 'user unit'
760 if (!bXIsAbsolute
&& bHasFoundWidth
)
762 fX
= getX().getNumber() * 0.01 * fWReference
;
765 if (!bWidthIsAbsolute
&& bHasFoundWidth
)
767 fW
= (getWidth().isSet() ? getWidth().getNumber() *0.01 : 1.0) * fWReference
;
768 bWidthIsAbsolute
= true;
771 if (!bYIsAbsolute
|| !bHeightIsAbsolute
)
773 // get height of enclosing svg and resolve percentage in y and height
774 double fHReference(0.0);
775 bool bHasFoundHeight(false);
776 seekReferenceHeight(fHReference
, bHasFoundHeight
);
777 // referenced values are already in 'user unit'
778 if (!bYIsAbsolute
&& bHasFoundHeight
)
780 fY
= getY().getNumber() * 0.01 * fHReference
;
783 if (!bHeightIsAbsolute
&& bHasFoundHeight
)
785 fH
= (getHeight().isSet() ? getHeight().getNumber() *0.01 : 1.0) * fHReference
;
786 bHeightIsAbsolute
= true;
790 if (bXIsAbsolute
&& bYIsAbsolute
&& bWidthIsAbsolute
&& bHeightIsAbsolute
)
792 return basegfx::B2DRange(fX
, fY
, fX
+fW
, fY
+fH
);
794 else // relative values could not be resolved, there exists no fallback
796 return SvgNode::getCurrentViewPort();
802 // If width or height is not provided, the default would be 100%, see SVG 1.1 section 5.1.2
803 // But here it cannot be resolved and no fallback exists.
804 // SVG 1.1 defines in section 5.1.2 that x,y has no meaning for the outermost SVG element.
805 bool bWidthIsAbsolute(getWidth().isSet() && SvgUnit::percent
!= getWidth().getUnit());
806 bool bHeightIsAbsolute(getHeight().isSet() && SvgUnit::percent
!= getHeight().getUnit());
807 if (bWidthIsAbsolute
&& bHeightIsAbsolute
)
809 double fW( getWidth().solveNonPercentage(*this) );
810 double fH( getHeight().solveNonPercentage(*this) );
811 return basegfx::B2DRange(0.0, 0.0, fW
, fH
);
813 else // no fallback exists
815 return SvgNode::getCurrentViewPort();
818 // TODO: Is it possible to decompose and use the bounding box of the children, if even the
819 // outermost svg has no information to resolve percentage? Is it worth, how expensive is it?
824 } // end of namespace svgio
826 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */