xmlsecurity: fix --without-system-nss build
[LibreOffice.git] / svgio / source / svgreader / svgsvgnode.cxx
blobe1c94e7b556aae1affe02ce76645f4d85aa2aa17
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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,
36 SvgNode* pParent)
37 : SvgNode(SVGToken::Svg, rDocument, pParent),
38 maSvgStyleAttributes(*this),
39 mbStyleAttributesInitialized(false) // #i125258#
43 // #i125258#
44 void SvgSvgNode::initializeStyleAttributes()
46 if(mbStyleAttributesInitialized)
47 return;
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);
54 if(getParent())
56 // #i125258# no initial values when it's a SVG element embedded in SVG
57 bSetInitialValues = false;
60 if(bSetInitialValues)
62 const SvgStyleAttributes* pStyles = getSvgStyleAttributes();
64 if(pStyles && pStyles->getCssStyleOrParentStyle())
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)
68 bool bFillSet(false);
69 const SvgStyleAttributes* pParentStyle = pStyles->getCssStyleOrParentStyle();
71 while(pParentStyle && !bFillSet)
73 bFillSet = pParentStyle->isFillSet();
74 pParentStyle = pParentStyle->getCssStyleOrParentStyle();
77 if(bFillSet)
79 // #125258# no initial values when SVG has a parent style at which a fill
80 // is already set
81 bSetInitialValues = false;
86 if(bSetInitialValues)
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(maSvgStyleAttributes);
110 void SvgSvgNode::parseAttribute(SVGToken aSVGToken, const OUString& aContent)
112 // call parent
113 SvgNode::parseAttribute(aSVGToken, aContent);
115 // read style attributes
116 maSvgStyleAttributes.parseStyleAttribute(aSVGToken, aContent);
118 // parse own
119 switch(aSVGToken)
121 case SVGToken::Style:
123 readLocalCssStyle(aContent);
124 break;
126 case SVGToken::ViewBox:
128 const basegfx::B2DRange aRange(readViewBox(aContent, *this));
130 if(!aRange.isEmpty())
132 setViewBox(&aRange);
134 break;
136 case SVGToken::PreserveAspectRatio:
138 maSvgAspectRatio = readSvgAspectRatio(aContent);
139 break;
141 case SVGToken::X:
143 SvgNumber aNum;
145 if(readSingleNumber(aContent, aNum))
147 maX = aNum;
149 break;
151 case SVGToken::Y:
153 SvgNumber aNum;
155 if(readSingleNumber(aContent, aNum))
157 maY = aNum;
159 break;
161 case SVGToken::Width:
163 SvgNumber aNum;
165 if(readSingleNumber(aContent, aNum))
167 if(aNum.isPositive())
169 maWidth = aNum;
172 break;
174 case SVGToken::Height:
176 SvgNumber aNum;
178 if(readSingleNumber(aContent, aNum))
180 if(aNum.isPositive())
182 maHeight = aNum;
185 break;
187 case SVGToken::Version:
189 SvgNumber aNum;
191 if(readSingleNumber(aContent, aNum))
193 maVersion = aNum;
195 break;
197 default:
199 break;
204 void SvgSvgNode::seekReferenceWidth(double& fWidth, bool& bHasFound) const
206 if (!getParent() || bHasFound)
208 return;
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;
224 bHasFound = true;
226 else
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;
235 else
237 fWidth = pParentSvgSvgNode->getWidth().solveNonPercentage(*pParentSvgSvgNode) * fPercentage;
238 bHasFound = true;
240 } // not set => width=100% => factor 1, no need for else
246 void SvgSvgNode::seekReferenceHeight(double& fHeight, bool& bHasFound) const
248 if (!getParent() || bHasFound)
250 return;
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;
266 bHasFound = true;
268 else
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;
277 else
279 fHeight = pParentSvgSvgNode->getHeight().solveNonPercentage(*pParentSvgSvgNode) * fPercentage;
280 bHasFound = true;
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())
303 if(getParent())
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);
344 if (!bHasFoundWidth)
346 if (getViewBox())
348 fWReference = getViewBox()->getWidth();
350 else
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'
362 if (!bXIsAbsolute)
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)
380 if (getViewBox())
382 fHReference = getViewBox()->getHeight();
384 else
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'
397 if (!bYIsAbsolute)
399 fY = getY().getNumber() * 0.01 * fHReference;
401 if (!bHeightIsAbsolute)
403 fH = (getHeight().isSet() ? getHeight().getNumber() *0.01 : 1.0) * fHReference;
407 if(getViewBox())
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 (getViewBox()->getWidth() > 0.0 && getViewBox()->getHeight() > 0.0 && !basegfx::fTools::equalZero(getViewBox()->getWidth()) && !basegfx::fTools::equalZero(getViewBox()->getHeight()))
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);
421 else
423 // create mapping
424 const SvgAspectRatio& rRatio = getSvgAspectRatio();
426 // let mapping be created from SvgAspectRatio
427 const basegfx::B2DHomMatrix aEmbeddingTransform(
428 rRatio.createMapping(aTarget, *getViewBox()));
430 // prepare embedding in transformation
431 const drawinglayer::primitive2d::Primitive2DReference xRef(
432 new drawinglayer::primitive2d::TransformPrimitive2D(
433 aEmbeddingTransform,
434 drawinglayer::primitive2d::Primitive2DContainer(aSequence)));
436 if(rRatio.isMeetOrSlice())
438 // embed in transformation
439 rTarget.push_back(xRef);
441 else
443 // need to embed in MaskPrimitive2D, too
444 const drawinglayer::primitive2d::Primitive2DReference xMask(
445 new drawinglayer::primitive2d::MaskPrimitive2D(
446 basegfx::B2DPolyPolygon(basegfx::utils::createPolygonFromRect(aTarget)),
447 drawinglayer::primitive2d::Primitive2DContainer { xRef }));
449 rTarget.push_back(xMask);
454 else // no viewBox attribute
456 // Svg defines that a negative value is an error and that 0.0 disables rendering
457 if (fW > 0.0 && fH > 0.0 && !basegfx::fTools::equalZero(fW) && !basegfx::fTools::equalZero(fH))
459 if(!basegfx::fTools::equalZero(fX) || !basegfx::fTools::equalZero(fY))
461 // embed in transform
462 const drawinglayer::primitive2d::Primitive2DReference xRef(
463 new drawinglayer::primitive2d::TransformPrimitive2D(
464 basegfx::utils::createTranslateB2DHomMatrix(fX, fY),
465 std::move(aSequence)));
467 aSequence = drawinglayer::primitive2d::Primitive2DContainer { xRef, };
470 // embed in MaskPrimitive2D to clip
471 const drawinglayer::primitive2d::Primitive2DReference xMask(
472 new drawinglayer::primitive2d::MaskPrimitive2D(
473 basegfx::B2DPolyPolygon(
474 basegfx::utils::createPolygonFromRect(
475 basegfx::B2DRange(fX, fY, fX + fW, fY + fH))),
476 drawinglayer::primitive2d::Primitive2DContainer(aSequence)));
478 // append
479 rTarget.push_back(xMask);
483 else // Outermost SVG element
485 // Svg defines that a negative value is an error and that 0.0 disables rendering
486 // isPositive() not usable because it allows 0.0 in contrast to mathematical definition of 'positive'
487 const bool bWidthInvalid(getWidth().isSet() && getWidth().getNumber() <= 0.0);
488 const bool bHeightInvalid(getHeight().isSet() && getHeight().getNumber() <= 0.0);
489 if(!bWidthInvalid && !bHeightInvalid)
491 basegfx::B2DRange aSvgCanvasRange; // viewport
492 if (const basegfx::B2DRange* pBox = getViewBox())
494 // SVG 1.1 defines in section 7.7 that a negative value for width or height
495 // in viewBox is an error and that 0.0 disables rendering
496 const double fViewBoxWidth = pBox->getWidth();
497 const double fViewBoxHeight = pBox->getHeight();
498 if (fViewBoxWidth > 0.0 && fViewBoxHeight > 0.0 && !basegfx::fTools::equalZero(fViewBoxWidth) && !basegfx::fTools::equalZero(fViewBoxHeight))
500 // The intrinsic aspect ratio of the svg element is given by absolute values of svg width and svg height
501 // or by the width and height of the viewBox, if svg width or svg height is relative.
502 // see SVG 1.1 section 7.12
503 bool bNeedsMapping(true);
504 const bool bWidthIsAbsolute(getWidth().isSet() && SvgUnit::percent != getWidth().getUnit());
505 const bool bHeightIsAbsolute(getHeight().isSet() && SvgUnit::percent != getHeight().getUnit());
506 const double fViewBoxRatio(fViewBoxWidth/fViewBoxHeight);
507 if(bWidthIsAbsolute && bHeightIsAbsolute)
509 double fW = getWidth().solveNonPercentage(*this);
510 double fH = getHeight().solveNonPercentage(*this);
511 aSvgCanvasRange = basegfx::B2DRange(0.0, 0.0, fW, fH);
513 else if (bWidthIsAbsolute)
515 double fW = getWidth().solveNonPercentage(*this);
516 double fH = fW / fViewBoxRatio;
517 aSvgCanvasRange = basegfx::B2DRange(0.0, 0.0, fW, fH);
519 else if (bHeightIsAbsolute)
521 double fH = getHeight().solveNonPercentage(*this);
522 double fW = fH * fViewBoxRatio;
523 aSvgCanvasRange = basegfx::B2DRange(0.0, 0.0, fW, fH);
525 else
527 // There exists no parent to resolve relative width or height.
528 // Use child size as fallback and expand to aspect ratio given
529 // by the viewBox. No mapping.
530 // We get viewport >= content, therefore no clipping.
531 bNeedsMapping = false;
533 const double fChildWidth(pBox->getWidth());
534 const double fChildHeight(pBox->getHeight());
535 const double fLeft(pBox->getMinX());
536 const double fTop(pBox->getMinY());
537 double fW, fH;
538 if ( fChildWidth / fViewBoxWidth > fChildHeight / fViewBoxHeight )
539 { // expand y
540 fW = fChildWidth;
541 fH = fChildWidth / fViewBoxRatio;
543 else
544 { // expand x
545 fH = fChildHeight;
546 fW = fChildHeight * fViewBoxRatio;
548 aSvgCanvasRange = basegfx::B2DRange(fLeft, fTop, fLeft + fW, fTop + fH);
551 if (bNeedsMapping)
553 // create mapping
554 // SVG 1.1 defines in section 5.1.2 that if the attribute preserveAspectRatio is not specified,
555 // then the effect is as if a value of 'xMidYMid meet' were specified.
556 SvgAspectRatio aRatioDefault(SvgAlign::xMidYMid, true);
557 const SvgAspectRatio& rRatio = getSvgAspectRatio().isSet()? getSvgAspectRatio() : aRatioDefault;
559 basegfx::B2DHomMatrix aViewBoxMapping = rRatio.createMapping(aSvgCanvasRange, *pBox);
560 // no need to check ratio here for slice, the outermost Svg will
561 // be clipped anyways (see below)
563 // scale content to viewBox definitions
564 const drawinglayer::primitive2d::Primitive2DReference xTransform(
565 new drawinglayer::primitive2d::TransformPrimitive2D(
566 aViewBoxMapping,
567 std::move(aSequence)));
569 aSequence = drawinglayer::primitive2d::Primitive2DContainer { xTransform };
573 else // no viewbox => no mapping
575 const bool bWidthIsAbsolute(getWidth().isSet() && SvgUnit::percent != getWidth().getUnit());
576 const bool bHeightIsAbsolute(getHeight().isSet() && SvgUnit::percent != getHeight().getUnit());
577 if (bWidthIsAbsolute && bHeightIsAbsolute)
579 double fW = getWidth().solveNonPercentage(*this);
580 double fH = getHeight().solveNonPercentage(*this);
581 aSvgCanvasRange = basegfx::B2DRange(0.0, 0.0, fW, fH);
583 else
585 // There exists no parent to resolve relative width or height.
586 // Use child size as fallback. We get viewport >= content, therefore no clipping.
587 const basegfx::B2DRange aChildRange(
588 aSequence.getB2DRange(
589 drawinglayer::geometry::ViewInformation2D()));
590 const double fChildWidth(aChildRange.getWidth());
591 const double fChildHeight(aChildRange.getHeight());
592 const double fChildLeft(aChildRange.getMinX());
593 const double fChildTop(aChildRange.getMinY());
594 double fW = bWidthIsAbsolute ? getWidth().solveNonPercentage(*this) : fChildWidth;
595 double fH = bHeightIsAbsolute ? getHeight().solveNonPercentage(*this) : fChildHeight;
596 const double fLeft(bWidthIsAbsolute ? 0.0 : fChildLeft);
597 const double fTop(bHeightIsAbsolute ? 0.0 : fChildTop);
598 aSvgCanvasRange = basegfx::B2DRange(fLeft, fTop, fLeft+fW, fTop+fH);
603 // to be completely correct in Svg sense it is necessary to clip
604 // the whole content to the given canvas. I choose here to do this
605 // initially despite I found various examples of Svg files out there
606 // which have no correct values for this clipping. It's correct
607 // due to the Svg spec.
609 // different from Svg we have the possibility with primitives to get
610 // a correct bounding box for the geometry. Get it for evtl. taking action
611 const basegfx::B2DRange aContentRange(
612 aSequence.getB2DRange(
613 drawinglayer::geometry::ViewInformation2D()));
615 if(aSvgCanvasRange.isInside(aContentRange))
617 // no clip needed, but an invisible HiddenGeometryPrimitive2D
618 // to allow getting the full Svg range using the primitive mechanisms.
619 // This is needed since e.g. an SdrObject using this as graphic will
620 // create a mapping transformation to exactly map the content to its
621 // real life size
622 const drawinglayer::primitive2d::Primitive2DReference xLine(
623 new drawinglayer::primitive2d::PolygonHairlinePrimitive2D(
624 basegfx::utils::createPolygonFromRect(
625 aSvgCanvasRange),
626 basegfx::BColor(0.0, 0.0, 0.0)));
627 const drawinglayer::primitive2d::Primitive2DReference xHidden(
628 new drawinglayer::primitive2d::HiddenGeometryPrimitive2D(
629 drawinglayer::primitive2d::Primitive2DContainer { xLine }));
631 aSequence.push_back(xHidden);
633 else if(aSvgCanvasRange.overlaps(aContentRange))
635 // Clip is necessary. This will make Svg images evtl. smaller
636 // than wanted from Svg (the free space which may be around it is
637 // conform to the Svg spec), but avoids an expensive and unnecessary
638 // clip. Keep the full Svg range here to get the correct mappings
639 // to objects using this. Optimizations can be done in the processors
640 const drawinglayer::primitive2d::Primitive2DReference xMask(
641 new drawinglayer::primitive2d::MaskPrimitive2D(
642 basegfx::B2DPolyPolygon(
643 basegfx::utils::createPolygonFromRect(
644 aSvgCanvasRange)),
645 std::move(aSequence)));
647 aSequence = drawinglayer::primitive2d::Primitive2DContainer { xMask };
649 else
651 // not inside, no overlap. Empty Svg
652 aSequence.clear();
655 if(!aSequence.empty())
657 // Another correction:
658 // If no Width/Height is set (usually done in
659 // <svg ... width="215.9mm" height="279.4mm" >) which
660 // is the case for own-Impress-exports, assume that
661 // the Units are already 100ThMM.
662 // Maybe only for own-Impress-exports, thus may need to be
663 // &&ed with getDocument().findSvgNodeById("ooo:meta_slides"),
664 // but does not need to be.
665 bool bEmbedInFinalTransformPxTo100ThMM(true);
667 if(getDocument().findSvgNodeById(u"ooo:meta_slides"_ustr)
668 && !getWidth().isSet()
669 && !getHeight().isSet())
671 bEmbedInFinalTransformPxTo100ThMM = false;
674 if(bEmbedInFinalTransformPxTo100ThMM)
676 // embed in transform primitive to scale to 1/100th mm
677 // to get from Svg coordinates (px) to drawinglayer coordinates
678 constexpr double fScaleTo100thmm(o3tl::convert(1.0, o3tl::Length::px, o3tl::Length::mm100));
679 const basegfx::B2DHomMatrix aTransform(
680 basegfx::utils::createScaleB2DHomMatrix(
681 fScaleTo100thmm,
682 fScaleTo100thmm));
684 const drawinglayer::primitive2d::Primitive2DReference xTransform(
685 new drawinglayer::primitive2d::TransformPrimitive2D(
686 aTransform,
687 std::move(aSequence)));
689 aSequence = drawinglayer::primitive2d::Primitive2DContainer { xTransform };
692 // append to result
693 rTarget.append(aSequence);
699 if(!(aSequence.empty() && !getParent() && getViewBox()))
700 return;
702 // tdf#118232 No geometry, Outermost SVG element and we have a ViewBox.
703 // Create a HiddenGeometry Primitive containing an expanded
704 // hairline geometry to have the size contained
705 const drawinglayer::primitive2d::Primitive2DReference xLine(
706 new drawinglayer::primitive2d::PolygonHairlinePrimitive2D(
707 basegfx::utils::createPolygonFromRect(
708 *getViewBox()),
709 basegfx::BColor(0.0, 0.0, 0.0)));
710 const drawinglayer::primitive2d::Primitive2DReference xHidden(
711 new drawinglayer::primitive2d::HiddenGeometryPrimitive2D(
712 drawinglayer::primitive2d::Primitive2DContainer { xLine }));
714 rTarget.push_back(xHidden);
717 basegfx::B2DRange SvgSvgNode::getCurrentViewPort() const
719 if(getViewBox())
721 return *(getViewBox());
723 else // viewport should be given by x, y, width, and height
725 // Extract known viewport data
726 // bXXXIsAbsolute tracks whether relative values could be resolved to absolute values
727 if (getParent())
729 // If width or height is not provided, the default 100% is used, see SVG 1.1 section 5.1.2
730 // value 0.0 here is only to initialize variable
731 bool bWidthIsAbsolute(getWidth().isSet() && SvgUnit::percent != getWidth().getUnit());
732 double fW( bWidthIsAbsolute ? getWidth().solveNonPercentage(*this) : 0.0);
733 bool bHeightIsAbsolute(getHeight().isSet() && SvgUnit::percent != getHeight().getUnit());
734 double fH( bHeightIsAbsolute ? getHeight().solveNonPercentage(*this) : 0.0);
736 // If x or y not provided, then default 0.0 is used, see SVG 1.1 Section 5.1.2
737 bool bXIsAbsolute((getX().isSet() && SvgUnit::percent != getX().getUnit()) || !getX().isSet());
738 double fX( bXIsAbsolute && getX().isSet() ? getX().solveNonPercentage(*this) : 0.0);
740 bool bYIsAbsolute((getY().isSet() && SvgUnit::percent != getY().getUnit()) || !getY().isSet());
741 double fY( bYIsAbsolute && getY().isSet() ? getY().solveNonPercentage(*this) : 0.0);
743 if (bXIsAbsolute && bYIsAbsolute && bWidthIsAbsolute && bHeightIsAbsolute)
745 return basegfx::B2DRange(fX, fY, fX+fW, fY+fH);
747 else // try to resolve relative values
749 if (!bXIsAbsolute || !bWidthIsAbsolute)
751 // get width of enclosing svg and resolve percentage in x and width
752 double fWReference(0.0);
753 bool bHasFoundWidth(false);
754 seekReferenceWidth(fWReference, bHasFoundWidth);
755 // referenced values are already in 'user unit'
756 if (!bXIsAbsolute && bHasFoundWidth)
758 fX = getX().getNumber() * 0.01 * fWReference;
759 bXIsAbsolute = true;
761 if (!bWidthIsAbsolute && bHasFoundWidth)
763 fW = (getWidth().isSet() ? getWidth().getNumber() *0.01 : 1.0) * fWReference;
764 bWidthIsAbsolute = true;
767 if (!bYIsAbsolute || !bHeightIsAbsolute)
769 // get height of enclosing svg and resolve percentage in y and height
770 double fHReference(0.0);
771 bool bHasFoundHeight(false);
772 seekReferenceHeight(fHReference, bHasFoundHeight);
773 // referenced values are already in 'user unit'
774 if (!bYIsAbsolute && bHasFoundHeight)
776 fY = getY().getNumber() * 0.01 * fHReference;
777 bYIsAbsolute = true;
779 if (!bHeightIsAbsolute && bHasFoundHeight)
781 fH = (getHeight().isSet() ? getHeight().getNumber() *0.01 : 1.0) * fHReference;
782 bHeightIsAbsolute = true;
786 if (bXIsAbsolute && bYIsAbsolute && bWidthIsAbsolute && bHeightIsAbsolute)
788 return basegfx::B2DRange(fX, fY, fX+fW, fY+fH);
790 else // relative values could not be resolved, there exists no fallback
792 return SvgNode::getCurrentViewPort();
796 else //outermost svg
798 // If width or height is not provided, the default would be 100%, see SVG 1.1 section 5.1.2
799 // But here it cannot be resolved and no fallback exists.
800 // SVG 1.1 defines in section 5.1.2 that x,y has no meaning for the outermost SVG element.
801 bool bWidthIsAbsolute(getWidth().isSet() && SvgUnit::percent != getWidth().getUnit());
802 bool bHeightIsAbsolute(getHeight().isSet() && SvgUnit::percent != getHeight().getUnit());
803 if (bWidthIsAbsolute && bHeightIsAbsolute)
805 double fW( getWidth().solveNonPercentage(*this) );
806 double fH( getHeight().solveNonPercentage(*this) );
807 return basegfx::B2DRange(0.0, 0.0, fW, fH);
809 else // no fallback exists
811 return SvgNode::getCurrentViewPort();
814 // TODO: Is it possible to decompose and use the bounding box of the children, if even the
815 // outermost svg has no information to resolve percentage? Is it worth, how expensive is it?
820 } // end of namespace svgio
822 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */