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/polygonprimitive2d.hxx>
28 #include <drawinglayer/primitive2d/hiddengeometryprimitive2d.hxx>
29 #include <svgdocument.hxx>
35 SvgSvgNode::SvgSvgNode(
36 SvgDocument
& rDocument
,
38 : SvgNode(SVGTokenSvg
, rDocument
, pParent
),
39 maSvgStyleAttributes(*this),
46 mbStyleAttributesInitialized(false) // #i125258#
51 void SvgSvgNode::initializeStyleAttributes()
53 if(!mbStyleAttributesInitialized
)
55 // #i125258# determine if initial values need to be initialized with hard values
56 // for the case that this is the outmost SVG statement and it has no parent
57 // stale (CssStyle for svg may be defined)
58 bool bSetInitialValues(true);
62 // #i125258# no initial values when it's a SVG element embedded in SVG
63 bSetInitialValues
= false;
68 const SvgStyleAttributes
* pStyles
= getSvgStyleAttributes();
70 if(pStyles
&& pStyles
->getParentStyle())
72 // SVG has a parent style (probably CssStyle), check if fill is set there anywhere
73 // already. If yes, do not set the default fill (black)
75 const SvgStyleAttributes
* pParentStyle
= pStyles
->getParentStyle();
77 while(pParentStyle
&& !bFillSet
)
79 bFillSet
= pParentStyle
->isFillSet();
80 pParentStyle
= pParentStyle
->getParentStyle();
85 // #125258# no initial values when SVG has a parent style at which a fill
87 bSetInitialValues
= false;
94 // #i125258# only set if not yet initialized (SvgSvgNode::parseAttribute is already done,
95 // just setting may revert an already set valid value)
96 if(!maSvgStyleAttributes
.isFillSet())
98 // #i125258# initial fill is black (see SVG1.1 spec)
99 maSvgStyleAttributes
.setFill(SvgPaint(basegfx::BColor(0.0, 0.0, 0.0), true, true));
103 mbStyleAttributesInitialized
= true;
107 SvgSvgNode::~SvgSvgNode()
111 const SvgStyleAttributes
* SvgSvgNode::getSvgStyleAttributes() const
113 // #i125258# svg node can have CssStyles, too, so check for it here
114 return checkForCssStyle("svg", maSvgStyleAttributes
);
117 void SvgSvgNode::parseAttribute(const OUString
& rTokenName
, SVGToken aSVGToken
, const OUString
& aContent
)
120 SvgNode::parseAttribute(rTokenName
, aSVGToken
, aContent
);
122 // read style attributes
123 maSvgStyleAttributes
.parseStyleAttribute(aSVGToken
, aContent
, false);
130 readLocalCssStyle(aContent
);
133 case SVGTokenViewBox
:
135 const basegfx::B2DRange
aRange(readViewBox(aContent
, *this));
137 if(!aRange
.isEmpty())
143 case SVGTokenPreserveAspectRatio
:
145 maSvgAspectRatio
= readSvgAspectRatio(aContent
);
152 if(readSingleNumber(aContent
, aNum
))
162 if(readSingleNumber(aContent
, aNum
))
172 if(readSingleNumber(aContent
, aNum
))
174 if(aNum
.isPositive())
185 if(readSingleNumber(aContent
, aNum
))
187 if(aNum
.isPositive())
194 case SVGTokenVersion
:
198 if(readSingleNumber(aContent
, aNum
))
211 void SvgSvgNode::seekReferenceWidth(double& fWidth
, bool& bHasFound
) const
213 if (!getParent() || bHasFound
)
217 const SvgSvgNode
* pParentSvgSvgNode
= nullptr;
218 // enclosing svg might have relative width, need to cumulate them till they are
219 // resolved somewhere up in the node tree
220 double fPercentage(1.0);
221 for(const SvgNode
* pParent
= getParent(); pParent
&& !bHasFound
; pParent
= pParent
->getParent())
223 // dynamic_cast results Null-pointer for not SvgSvgNode and so skips them in if condition
224 pParentSvgSvgNode
= dynamic_cast< const SvgSvgNode
* >(pParent
);
225 if (pParentSvgSvgNode
)
227 if (pParentSvgSvgNode
->getViewBox())
229 // viewbox values are already in 'user unit'.
230 fWidth
= pParentSvgSvgNode
->getViewBox()->getWidth() * fPercentage
;
235 // take absolute value or cumulate percentage
236 if (pParentSvgSvgNode
->getWidth().isSet())
238 if (Unit_percent
== pParentSvgSvgNode
->getWidth().getUnit())
240 fPercentage
*= pParentSvgSvgNode
->getWidth().getNumber() * 0.01;
244 fWidth
= pParentSvgSvgNode
->getWidth().solveNonPercentage(*pParentSvgSvgNode
) * fPercentage
;
247 } // not set => width=100% => factor 1, no need for else
253 void SvgSvgNode::seekReferenceHeight(double& fHeight
, bool& bHasFound
) const
255 if (!getParent() || bHasFound
)
259 const SvgSvgNode
* pParentSvgSvgNode
= nullptr;
260 // enclosing svg might have relative width and height, need to cumulate them till they are
261 // resolved somewhere up in the node tree
262 double fPercentage(1.0);
263 for(const SvgNode
* pParent
= getParent(); pParent
&& !bHasFound
; pParent
= pParent
->getParent())
265 // dynamic_cast results Null-pointer for not SvgSvgNode and so skips them in if condition
266 pParentSvgSvgNode
= dynamic_cast< const SvgSvgNode
* >(pParent
);
267 if (pParentSvgSvgNode
)
269 if (pParentSvgSvgNode
->getViewBox())
271 // viewbox values are already in 'user unit'.
272 fHeight
= pParentSvgSvgNode
->getViewBox()->getHeight() * fPercentage
;
277 // take absolute value or cumulate percentage
278 if (pParentSvgSvgNode
->getHeight().isSet())
280 if (Unit_percent
== pParentSvgSvgNode
->getHeight().getUnit())
282 fPercentage
*= pParentSvgSvgNode
->getHeight().getNumber() * 0.01;
286 fHeight
= pParentSvgSvgNode
->getHeight().solveNonPercentage(*pParentSvgSvgNode
) * fPercentage
;
289 } // not set => height=100% => factor 1, no need for else
295 // ToDo: Consider attribute overflow in method decomposeSvgNode
296 void SvgSvgNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer
& rTarget
, bool bReferenced
) const
298 drawinglayer::primitive2d::Primitive2DContainer aSequence
;
300 // #i125258# check now if we need to init some style settings locally. Do not do this
301 // in the constructor, there is not yet information e.g. about existing CssStyles.
302 // Here all nodes are read and interpreted
303 const_cast< SvgSvgNode
* >(this)->initializeStyleAttributes();
305 // decompose children
306 SvgNode::decomposeSvgNode(aSequence
, bReferenced
);
308 if(!aSequence
.empty())
312 // #i122594# if width/height is not given, it's 100% (see 5.1.2 The 'svg' element in SVG1.1 spec).
313 // If it is relative, the question is to what. The previous implementation assumed relative to the
314 // local ViewBox which is implied by (4.2 Basic data types):
316 // "Note that the non-property <length> definition also allows a percentage unit identifier.
317 // The meaning of a percentage length value depends on the attribute for which the percentage
318 // length value has been specified. Two common cases are: (a) when a percentage length value
319 // represents a percentage of the viewport width or height (refer to the section that discusses
320 // units in general), and (b) when a percentage length value represents a percentage of the
321 // bounding box width or height on a given object (refer to the section that describes object
322 // bounding box units)."
324 // Comparisons with common browsers show that it's mostly interpreted relative to the viewport
325 // of the parent, and so does the new implementation.
327 // Extract known viewport data
328 // bXXXIsAbsolute tracks whether relative values could be resolved to absolute values
330 // If width or height is not provided, the default 100% is used, see SVG 1.1 section 5.1.2
331 // value 0.0 here is only to initialize variable
332 bool bWidthIsAbsolute(getWidth().isSet() && Unit_percent
!= getWidth().getUnit());
333 double fW( bWidthIsAbsolute
? getWidth().solveNonPercentage(*this) : 0.0);
335 bool bHeightIsAbsolute(getHeight().isSet() && Unit_percent
!= getHeight().getUnit());
336 double fH( bHeightIsAbsolute
? getHeight().solveNonPercentage(*this) : 0.0);
338 // If x or y not provided, then default 0.0 is used, see SVG 1.1 Section 5.1.2
339 bool bXIsAbsolute((getX().isSet() && Unit_percent
!= getX().getUnit()) || !getX().isSet());
340 double fX( bXIsAbsolute
&& getX().isSet() ? getX().solveNonPercentage(*this) : 0.0);
342 bool bYIsAbsolute((getY().isSet() && Unit_percent
!= getY().getUnit()) || !getY().isSet());
343 double fY( bYIsAbsolute
&& getY().isSet() ? getY().solveNonPercentage(*this) : 0.0);
345 if ( !bXIsAbsolute
|| !bWidthIsAbsolute
)
347 // get width of enclosing svg and resolve percentage in x and width;
348 double fWReference(0.0);
349 bool bHasFoundWidth(false);
350 seekReferenceWidth(fWReference
, bHasFoundWidth
);
355 fWReference
= getViewBox()->getWidth();
359 // Even outermost svg has not all information to resolve relative values,
360 // I use content itself as fallback to set missing values for viewport
361 // Any better idea for such ill structured svg documents?
362 const basegfx::B2DRange
aChildRange(
363 aSequence
.getB2DRange(
364 drawinglayer::geometry::ViewInformation2D()));
365 fWReference
= aChildRange
.getWidth();
368 // referenced values are already in 'user unit'
371 fX
= getX().getNumber() * 0.01 * fWReference
;
373 if (!bWidthIsAbsolute
)
375 fW
= (getWidth().isSet() ? getWidth().getNumber() *0.01 : 1.0) * fWReference
;
379 if ( !bYIsAbsolute
|| !bHeightIsAbsolute
)
381 // get height of enclosing svg and resolve percentage in y and height
382 double fHReference(0.0);
383 bool bHasFoundHeight(false);
384 seekReferenceHeight(fHReference
, bHasFoundHeight
);
385 if (!bHasFoundHeight
)
389 fHReference
= getViewBox()->getHeight();
393 // Even outermost svg has not all information to resolve relative values,
394 // I use content itself as fallback to set missing values for viewport
395 // Any better idea for such ill structured svg documents?
396 const basegfx::B2DRange
aChildRange(
397 aSequence
.getB2DRange(
398 drawinglayer::geometry::ViewInformation2D()));
399 fHReference
= aChildRange
.getHeight();
403 // referenced values are already in 'user unit'
406 fY
= getY().getNumber() * 0.01 * fHReference
;
408 if (!bHeightIsAbsolute
)
410 fH
= (getHeight().isSet() ? getHeight().getNumber() *0.01 : 1.0) * fHReference
;
416 // SVG 1.1 defines in section 7.7 that a negative value for width or height
417 // in viewBox is an error and that 0.0 disables rendering
418 if(basegfx::fTools::more(getViewBox()->getWidth(),0.0) && basegfx::fTools::more(getViewBox()->getHeight(),0.0))
420 // create target range homing x,y, width and height as calculated above
421 const basegfx::B2DRange
aTarget(fX
, fY
, fX
+ fW
, fY
+ fH
);
423 if(aTarget
.equal(*getViewBox()))
425 // no mapping needed, append
426 rTarget
.append(aSequence
);
431 // #i122610 SVG 1.1 defines in section 5.1.2 that if the attribute perserveAspectRatio is not specified,
432 // then the effect is as if a value of 'xMidYMid meet' were specified.
433 SvgAspectRatio
aRatioDefault(Align_xMidYMid
,true);
434 const SvgAspectRatio
& rRatio
= getSvgAspectRatio().isSet()? getSvgAspectRatio() : aRatioDefault
;
436 // let mapping be created from SvgAspectRatio
437 const basegfx::B2DHomMatrix
aEmbeddingTransform(
438 rRatio
.createMapping(aTarget
, *getViewBox()));
440 // prepare embedding in transformation
441 const drawinglayer::primitive2d::Primitive2DReference
xRef(
442 new drawinglayer::primitive2d::TransformPrimitive2D(
446 if(rRatio
.isMeetOrSlice())
448 // embed in transformation
449 rTarget
.push_back(xRef
);
453 // need to embed in MaskPrimitive2D, too
454 const drawinglayer::primitive2d::Primitive2DReference
xMask(
455 new drawinglayer::primitive2d::MaskPrimitive2D(
456 basegfx::B2DPolyPolygon(basegfx::utils::createPolygonFromRect(aTarget
)),
457 drawinglayer::primitive2d::Primitive2DContainer
{ xRef
}));
459 rTarget
.push_back(xMask
);
464 else // no viewBox attribute
466 // Svg defines that a negative value is an error and that 0.0 disables rendering
467 if(basegfx::fTools::more(fW
, 0.0) && basegfx::fTools::more(fH
, 0.0))
469 if(!basegfx::fTools::equalZero(fX
) || !basegfx::fTools::equalZero(fY
))
471 // embed in transform
472 const drawinglayer::primitive2d::Primitive2DReference
xRef(
473 new drawinglayer::primitive2d::TransformPrimitive2D(
474 basegfx::utils::createTranslateB2DHomMatrix(fX
, fY
),
477 aSequence
= drawinglayer::primitive2d::Primitive2DContainer
{ xRef
, };
480 // embed in MaskPrimitive2D to clip
481 const drawinglayer::primitive2d::Primitive2DReference
xMask(
482 new drawinglayer::primitive2d::MaskPrimitive2D(
483 basegfx::B2DPolyPolygon(
484 basegfx::utils::createPolygonFromRect(
485 basegfx::B2DRange(fX
, fY
, fX
+ fW
, fY
+ fH
))),
489 rTarget
.push_back(xMask
);
493 else // Outermost SVG element
495 // Svg defines that a negative value is an error and that 0.0 disables rendering
496 // isPositive() not usable because it allows 0.0 in contrast to mathematical definition of 'positive'
497 const bool bWidthInvalid(getWidth().isSet() && basegfx::fTools::lessOrEqual(getWidth().getNumber(), 0.0));
498 const bool bHeightInvalid(getHeight().isSet() && basegfx::fTools::lessOrEqual(getHeight().getNumber(), 0.0));
499 if(!bWidthInvalid
&& !bHeightInvalid
)
501 basegfx::B2DRange aSvgCanvasRange
; // viewport
502 double fW
= 0.0; // dummy values
504 if (const basegfx::B2DRange
* pBox
= getViewBox())
506 // SVG 1.1 defines in section 7.7 that a negative value for width or height
507 // in viewBox is an error and that 0.0 disables rendering
508 const double fViewBoxWidth
= pBox
->getWidth();
509 const double fViewBoxHeight
= pBox
->getHeight();
510 if(basegfx::fTools::more(fViewBoxWidth
,0.0) && basegfx::fTools::more(fViewBoxHeight
,0.0))
512 // The intrinsic aspect ratio of the svg element is given by absolute values of svg width and svg height
513 // or by the width and height of the viewBox, if svg width or svg height is relative.
514 // see SVG 1.1 section 7.12
515 bool bNeedsMapping(true);
516 const bool bWidthIsAbsolute(getWidth().isSet() && Unit_percent
!= getWidth().getUnit());
517 const bool bHeightIsAbsolute(getHeight().isSet() && Unit_percent
!= getHeight().getUnit());
518 const double fViewBoxRatio(fViewBoxWidth
/fViewBoxHeight
);
519 if(bWidthIsAbsolute
&& bHeightIsAbsolute
)
521 fW
= getWidth().solveNonPercentage(*this);
522 fH
= getHeight().solveNonPercentage(*this);
523 aSvgCanvasRange
= basegfx::B2DRange(0.0, 0.0, fW
, fH
);
525 else if (bWidthIsAbsolute
)
527 fW
= getWidth().solveNonPercentage(*this);
528 fH
= fW
/ fViewBoxRatio
;
529 aSvgCanvasRange
= basegfx::B2DRange(0.0, 0.0, fW
, fH
);
531 else if (bHeightIsAbsolute
)
533 fH
= getHeight().solveNonPercentage(*this);
534 fW
= fH
* fViewBoxRatio
;
535 aSvgCanvasRange
= basegfx::B2DRange(0.0, 0.0, fW
, fH
);
539 // There exists no parent to resolve relative width or height.
540 // Use child size as fallback and expand to aspect ratio given
541 // by the viewBox. No mapping.
542 // We get viewport >= content, therefore no clipping.
543 bNeedsMapping
= false;
545 const double fChildWidth(pBox
->getWidth());
546 const double fChildHeight(pBox
->getHeight());
547 const double fLeft(pBox
->getMinX());
548 const double fTop(pBox
->getMinY());
549 if ( fChildWidth
/ fViewBoxWidth
> fChildHeight
/ fViewBoxHeight
)
552 fH
= fChildWidth
/ fViewBoxRatio
;
557 fW
= fChildHeight
* fViewBoxRatio
;
559 aSvgCanvasRange
= basegfx::B2DRange(fLeft
, fTop
, fLeft
+ fW
, fTop
+ fH
);
565 // SVG 1.1 defines in section 5.1.2 that if the attribute perserveAspectRatio is not specified,
566 // then the effect is as if a value of 'xMidYMid meet' were specified.
567 SvgAspectRatio
aRatioDefault(Align_xMidYMid
,true);
568 const SvgAspectRatio
& rRatio
= getSvgAspectRatio().isSet()? getSvgAspectRatio() : aRatioDefault
;
570 basegfx::B2DHomMatrix aViewBoxMapping
;
571 aViewBoxMapping
= rRatio
.createMapping(aSvgCanvasRange
, *pBox
);
572 // no need to check ratio here for slice, the outermost Svg will
573 // be clipped anyways (see below)
575 // scale content to viewBox definitions
576 const drawinglayer::primitive2d::Primitive2DReference
xTransform(
577 new drawinglayer::primitive2d::TransformPrimitive2D(
581 aSequence
= drawinglayer::primitive2d::Primitive2DContainer
{ xTransform
};
585 else // no viewbox => no mapping
587 const bool bWidthIsAbsolute(getWidth().isSet() && Unit_percent
!= getWidth().getUnit());
588 const bool bHeightIsAbsolute(getHeight().isSet() && Unit_percent
!= getHeight().getUnit());
589 if (bWidthIsAbsolute
&& bHeightIsAbsolute
)
591 fW
=getWidth().solveNonPercentage(*this);
592 fH
=getHeight().solveNonPercentage(*this);
593 aSvgCanvasRange
= basegfx::B2DRange(0.0, 0.0, fW
, fH
);
597 // There exists no parent to resolve relative width or height.
598 // Use child size as fallback. We get viewport >= content, therefore no clipping.
599 const basegfx::B2DRange
aChildRange(
600 aSequence
.getB2DRange(
601 drawinglayer::geometry::ViewInformation2D()));
602 const double fChildWidth(aChildRange
.getWidth());
603 const double fChildHeight(aChildRange
.getHeight());
604 const double fChildLeft(aChildRange
.getMinX());
605 const double fChildTop(aChildRange
.getMinY());
606 fW
= bWidthIsAbsolute
? getWidth().solveNonPercentage(*this) : fChildWidth
;
607 fH
= bHeightIsAbsolute
? getHeight().solveNonPercentage(*this) : fChildHeight
;
608 const double fLeft(bWidthIsAbsolute
? 0.0 : fChildLeft
);
609 const double fTop(bHeightIsAbsolute
? 0.0 : fChildTop
);
610 aSvgCanvasRange
= basegfx::B2DRange(fLeft
, fTop
, fLeft
+fW
, fTop
+fH
);
615 // to be completely correct in Svg sense it is necessary to clip
616 // the whole content to the given canvas. I choose here to do this
617 // initially despite I found various examples of Svg files out there
618 // which have no correct values for this clipping. It's correct
619 // due to the Svg spec.
621 // different from Svg we have the possibility with primitives to get
622 // a correct bounding box for the geometry. Get it for evtl. taking action
623 const basegfx::B2DRange
aContentRange(
624 aSequence
.getB2DRange(
625 drawinglayer::geometry::ViewInformation2D()));
627 if(aSvgCanvasRange
.isInside(aContentRange
))
629 // no clip needed, but an invisible HiddenGeometryPrimitive2D
630 // to allow getting the full Svg range using the primitive mechanisms.
631 // This is needed since e.g. an SdrObject using this as graphic will
632 // create a mapping transformation to exactly map the content to its
634 const drawinglayer::primitive2d::Primitive2DReference
xLine(
635 new drawinglayer::primitive2d::PolygonHairlinePrimitive2D(
636 basegfx::utils::createPolygonFromRect(
638 basegfx::BColor(0.0, 0.0, 0.0)));
639 const drawinglayer::primitive2d::Primitive2DReference
xHidden(
640 new drawinglayer::primitive2d::HiddenGeometryPrimitive2D(
641 drawinglayer::primitive2d::Primitive2DContainer
{ xLine
}));
643 aSequence
.push_back(xHidden
);
645 else if(aSvgCanvasRange
.overlaps(aContentRange
))
647 // Clip is necessary. This will make Svg images evtl. smaller
648 // than wanted from Svg (the free space which may be around it is
649 // conform to the Svg spec), but avoids an expensive and unnecessary
650 // clip. Keep the full Svg range here to get the correct mappings
651 // to objects using this. Optimizations can be done in the processors
652 const drawinglayer::primitive2d::Primitive2DReference
xMask(
653 new drawinglayer::primitive2d::MaskPrimitive2D(
654 basegfx::B2DPolyPolygon(
655 basegfx::utils::createPolygonFromRect(
659 aSequence
= drawinglayer::primitive2d::Primitive2DContainer
{ xMask
};
663 // not inside, no overlap. Empty Svg
667 if(!aSequence
.empty())
669 // Another correction:
670 // If no Width/Height is set (usually done in
671 // <svg ... width="215.9mm" height="279.4mm" >) which
672 // is the case for own-Impress-exports, assume that
673 // the Units are already 100ThMM.
674 // Maybe only for own-Impress-exports, thus may need to be
675 // &&ed with getDocument().findSvgNodeById("ooo:meta_slides"),
676 // but does not need to be.
677 bool bEmbedInFinalTransformPxTo100ThMM(true);
679 if(getDocument().findSvgNodeById("ooo:meta_slides")
680 && !getWidth().isSet()
681 && !getHeight().isSet())
683 bEmbedInFinalTransformPxTo100ThMM
= false;
686 if(bEmbedInFinalTransformPxTo100ThMM
)
688 // embed in transform primitive to scale to 1/100th mm
689 // where 1 inch == 25.4 mm to get from Svg coordinates (px) to
690 // drawinglayer coordinates
691 const double fScaleTo100thmm(25.4 * 100.0 / F_SVG_PIXEL_PER_INCH
);
692 const basegfx::B2DHomMatrix
aTransform(
693 basegfx::utils::createScaleB2DHomMatrix(
697 const drawinglayer::primitive2d::Primitive2DReference
xTransform(
698 new drawinglayer::primitive2d::TransformPrimitive2D(
702 aSequence
= drawinglayer::primitive2d::Primitive2DContainer
{ xTransform
};
706 rTarget
.append(aSequence
);
712 if(aSequence
.empty() && !getParent() && getViewBox())
714 // tdf#118232 No geometry, Outermost SVG element and we have a ViewBox.
715 // Create a HiddenGeometry Primitive containing an expanded
716 // hairline geometry to have the size contained
717 const drawinglayer::primitive2d::Primitive2DReference
xLine(
718 new drawinglayer::primitive2d::PolygonHairlinePrimitive2D(
719 basegfx::utils::createPolygonFromRect(
721 basegfx::BColor(0.0, 0.0, 0.0)));
722 const drawinglayer::primitive2d::Primitive2DReference
xHidden(
723 new drawinglayer::primitive2d::HiddenGeometryPrimitive2D(
724 drawinglayer::primitive2d::Primitive2DContainer
{ xLine
}));
726 rTarget
.push_back(xHidden
);
730 const basegfx::B2DRange
SvgSvgNode::getCurrentViewPort() const
734 return *(getViewBox());
736 else // viewport should be given by x, y, width, and height
738 // Extract known viewport data
739 // bXXXIsAbsolute tracks whether relative values could be resolved to absolute values
742 // If width or height is not provided, the default 100% is used, see SVG 1.1 section 5.1.2
743 // value 0.0 here is only to initialize variable
744 bool bWidthIsAbsolute(getWidth().isSet() && Unit_percent
!= getWidth().getUnit());
745 double fW( bWidthIsAbsolute
? getWidth().solveNonPercentage(*this) : 0.0);
746 bool bHeightIsAbsolute(getHeight().isSet() && Unit_percent
!= getHeight().getUnit());
747 double fH( bHeightIsAbsolute
? getHeight().solveNonPercentage(*this) : 0.0);
749 // If x or y not provided, then default 0.0 is used, see SVG 1.1 Section 5.1.2
750 bool bXIsAbsolute((getX().isSet() && Unit_percent
!= getX().getUnit()) || !getX().isSet());
751 double fX( bXIsAbsolute
&& getX().isSet() ? getX().solveNonPercentage(*this) : 0.0);
753 bool bYIsAbsolute((getY().isSet() && Unit_percent
!= getY().getUnit()) || !getY().isSet());
754 double fY( bYIsAbsolute
&& getY().isSet() ? getY().solveNonPercentage(*this) : 0.0);
756 if (bXIsAbsolute
&& bYIsAbsolute
&& bWidthIsAbsolute
&& bHeightIsAbsolute
)
758 return basegfx::B2DRange(fX
, fY
, fX
+fW
, fY
+fH
);
760 else // try to resolve relative values
762 if (!bXIsAbsolute
|| !bWidthIsAbsolute
)
764 // get width of enclosing svg and resolve percentage in x and width
765 double fWReference(0.0);
766 bool bHasFoundWidth(false);
767 seekReferenceWidth(fWReference
, bHasFoundWidth
);
768 // referenced values are already in 'user unit'
769 if (!bXIsAbsolute
&& bHasFoundWidth
)
771 fX
= getX().getNumber() * 0.01 * fWReference
;
774 if (!bWidthIsAbsolute
&& bHasFoundWidth
)
776 fW
= (getWidth().isSet() ? getWidth().getNumber() *0.01 : 1.0) * fWReference
;
777 bWidthIsAbsolute
= true;
780 if (!bYIsAbsolute
|| !bHeightIsAbsolute
)
782 // get height of enclosing svg and resolve percentage in y and height
783 double fHReference(0.0);
784 bool bHasFoundHeight(false);
785 seekReferenceHeight(fHReference
, bHasFoundHeight
);
786 // referenced values are already in 'user unit'
787 if (!bYIsAbsolute
&& bHasFoundHeight
)
789 fY
= getY().getNumber() * 0.01 * fHReference
;
792 if (!bHeightIsAbsolute
&& bHasFoundHeight
)
794 fH
= (getHeight().isSet() ? getHeight().getNumber() *0.01 : 1.0) * fHReference
;
795 bHeightIsAbsolute
= true;
799 if (bXIsAbsolute
&& bYIsAbsolute
&& bWidthIsAbsolute
&& bHeightIsAbsolute
)
801 return basegfx::B2DRange(fX
, fY
, fX
+fW
, fY
+fH
);
803 else // relative values could not be resolved, there exists no fallback
805 return SvgNode::getCurrentViewPort();
811 // If width or height is not provided, the default would be 100%, see SVG 1.1 section 5.1.2
812 // But here it cannot be resolved and no fallback exists.
813 // SVG 1.1 defines in section 5.1.2 that x,y has no meaning for the outermost SVG element.
814 bool bWidthIsAbsolute(getWidth().isSet() && Unit_percent
!= getWidth().getUnit());
815 bool bHeightIsAbsolute(getHeight().isSet() && Unit_percent
!= getHeight().getUnit());
816 if (bWidthIsAbsolute
&& bHeightIsAbsolute
)
818 double fW( getWidth().solveNonPercentage(*this) );
819 double fH( getHeight().solveNonPercentage(*this) );
820 return basegfx::B2DRange(0.0, 0.0, fW
, fH
);
822 else // no fallback exists
824 return SvgNode::getCurrentViewPort();
827 // TODO: Is it possible to decompose and use the bounding box of the children, if even the
828 // outermost svg has no information to resolve percentage? Is it worth, how expensive is it?
833 } // end of namespace svgreader
834 } // end of namespace svgio
836 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */