bump product version to 7.2.5.1
[LibreOffice.git] / svgio / source / svgreader / svgsvgnode.cxx
blob57e7832ebce3f3514f25895a3e80f530568e749e
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/polygonprimitive2d.hxx>
28 #include <drawinglayer/primitive2d/hiddengeometryprimitive2d.hxx>
29 #include <svgdocument.hxx>
31 namespace svgio::svgreader
33 SvgSvgNode::SvgSvgNode(
34 SvgDocument& rDocument,
35 SvgNode* pParent)
36 : SvgNode(SVGToken::Svg, rDocument, pParent),
37 maSvgStyleAttributes(*this),
38 maSvgAspectRatio(),
39 maX(),
40 maY(),
41 maWidth(),
42 maHeight(),
43 maVersion(),
44 mbStyleAttributesInitialized(false) // #i125258#
48 // #i125258#
49 void SvgSvgNode::initializeStyleAttributes()
51 if(mbStyleAttributesInitialized)
52 return;
54 // #i125258# determine if initial values need to be initialized with hard values
55 // for the case that this is the outmost SVG statement and it has no parent
56 // stale (CssStyle for svg may be defined)
57 bool bSetInitialValues(true);
59 if(getParent())
61 // #i125258# no initial values when it's a SVG element embedded in SVG
62 bSetInitialValues = false;
65 if(bSetInitialValues)
67 const SvgStyleAttributes* pStyles = getSvgStyleAttributes();
69 if(pStyles && pStyles->getParentStyle())
71 // SVG has a parent style (probably CssStyle), check if fill is set there anywhere
72 // already. If yes, do not set the default fill (black)
73 bool bFillSet(false);
74 const SvgStyleAttributes* pParentStyle = pStyles->getParentStyle();
76 while(pParentStyle && !bFillSet)
78 bFillSet = pParentStyle->isFillSet();
79 pParentStyle = pParentStyle->getParentStyle();
82 if(bFillSet)
84 // #125258# no initial values when SVG has a parent style at which a fill
85 // is already set
86 bSetInitialValues = false;
91 if(bSetInitialValues)
93 // #i125258# only set if not yet initialized (SvgSvgNode::parseAttribute is already done,
94 // just setting may revert an already set valid value)
95 if(!maSvgStyleAttributes.isFillSet())
97 // #i125258# initial fill is black (see SVG1.1 spec)
98 maSvgStyleAttributes.setFill(SvgPaint(basegfx::BColor(0.0, 0.0, 0.0), true, true));
102 mbStyleAttributesInitialized = true;
105 SvgSvgNode::~SvgSvgNode()
109 const SvgStyleAttributes* SvgSvgNode::getSvgStyleAttributes() const
111 // #i125258# svg node can have CssStyles, too, so check for it here
112 return checkForCssStyle("svg", maSvgStyleAttributes);
115 void SvgSvgNode::parseAttribute(const OUString& rTokenName, SVGToken aSVGToken, const OUString& aContent)
117 // call parent
118 SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
120 // read style attributes
121 maSvgStyleAttributes.parseStyleAttribute(aSVGToken, aContent, false);
123 // parse own
124 switch(aSVGToken)
126 case SVGToken::Style:
128 readLocalCssStyle(aContent);
129 break;
131 case SVGToken::ViewBox:
133 const basegfx::B2DRange aRange(readViewBox(aContent, *this));
135 if(!aRange.isEmpty())
137 setViewBox(&aRange);
139 break;
141 case SVGToken::PreserveAspectRatio:
143 maSvgAspectRatio = readSvgAspectRatio(aContent);
144 break;
146 case SVGToken::X:
148 SvgNumber aNum;
150 if(readSingleNumber(aContent, aNum))
152 maX = aNum;
154 break;
156 case SVGToken::Y:
158 SvgNumber aNum;
160 if(readSingleNumber(aContent, aNum))
162 maY = aNum;
164 break;
166 case SVGToken::Width:
168 SvgNumber aNum;
170 if(readSingleNumber(aContent, aNum))
172 if(aNum.isPositive())
174 maWidth = aNum;
177 break;
179 case SVGToken::Height:
181 SvgNumber aNum;
183 if(readSingleNumber(aContent, aNum))
185 if(aNum.isPositive())
187 maHeight = aNum;
190 break;
192 case SVGToken::Version:
194 SvgNumber aNum;
196 if(readSingleNumber(aContent, aNum))
198 maVersion = aNum;
200 break;
202 default:
204 break;
209 void SvgSvgNode::seekReferenceWidth(double& fWidth, bool& bHasFound) const
211 if (!getParent() || bHasFound)
213 return;
215 const SvgSvgNode* pParentSvgSvgNode = nullptr;
216 // enclosing svg might have relative width, need to cumulate them till they are
217 // resolved somewhere up in the node tree
218 double fPercentage(1.0);
219 for(const SvgNode* pParent = getParent(); pParent && !bHasFound; pParent = pParent->getParent())
221 // dynamic_cast results Null-pointer for not SvgSvgNode and so skips them in if condition
222 pParentSvgSvgNode = dynamic_cast< const SvgSvgNode* >(pParent);
223 if (pParentSvgSvgNode)
225 if (pParentSvgSvgNode->getViewBox())
227 // viewbox values are already in 'user unit'.
228 fWidth = pParentSvgSvgNode->getViewBox()->getWidth() * fPercentage;
229 bHasFound = true;
231 else
233 // take absolute value or cumulate percentage
234 if (pParentSvgSvgNode->getWidth().isSet())
236 if (SvgUnit::percent == pParentSvgSvgNode->getWidth().getUnit())
238 fPercentage *= pParentSvgSvgNode->getWidth().getNumber() * 0.01;
240 else
242 fWidth = pParentSvgSvgNode->getWidth().solveNonPercentage(*pParentSvgSvgNode) * fPercentage;
243 bHasFound = true;
245 } // not set => width=100% => factor 1, no need for else
251 void SvgSvgNode::seekReferenceHeight(double& fHeight, bool& bHasFound) const
253 if (!getParent() || bHasFound)
255 return;
257 const SvgSvgNode* pParentSvgSvgNode = nullptr;
258 // enclosing svg might have relative width and height, need to cumulate them till they are
259 // resolved somewhere up in the node tree
260 double fPercentage(1.0);
261 for(const SvgNode* pParent = getParent(); pParent && !bHasFound; pParent = pParent->getParent())
263 // dynamic_cast results Null-pointer for not SvgSvgNode and so skips them in if condition
264 pParentSvgSvgNode = dynamic_cast< const SvgSvgNode* >(pParent);
265 if (pParentSvgSvgNode)
267 if (pParentSvgSvgNode->getViewBox())
269 // viewbox values are already in 'user unit'.
270 fHeight = pParentSvgSvgNode->getViewBox()->getHeight() * fPercentage;
271 bHasFound = true;
273 else
275 // take absolute value or cumulate percentage
276 if (pParentSvgSvgNode->getHeight().isSet())
278 if (SvgUnit::percent == pParentSvgSvgNode->getHeight().getUnit())
280 fPercentage *= pParentSvgSvgNode->getHeight().getNumber() * 0.01;
282 else
284 fHeight = pParentSvgSvgNode->getHeight().solveNonPercentage(*pParentSvgSvgNode) * fPercentage;
285 bHasFound = true;
287 } // not set => height=100% => factor 1, no need for else
293 // ToDo: Consider attribute overflow in method decomposeSvgNode
294 void SvgSvgNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer& rTarget, bool bReferenced) const
296 drawinglayer::primitive2d::Primitive2DContainer aSequence;
298 // #i125258# check now if we need to init some style settings locally. Do not do this
299 // in the constructor, there is not yet information e.g. about existing CssStyles.
300 // Here all nodes are read and interpreted
301 const_cast< SvgSvgNode* >(this)->initializeStyleAttributes();
303 // decompose children
304 SvgNode::decomposeSvgNode(aSequence, bReferenced);
306 if(!aSequence.empty())
308 if(getParent())
310 // #i122594# if width/height is not given, it's 100% (see 5.1.2 The 'svg' element in SVG1.1 spec).
311 // If it is relative, the question is to what. The previous implementation assumed relative to the
312 // local ViewBox which is implied by (4.2 Basic data types):
314 // "Note that the non-property <length> definition also allows a percentage unit identifier.
315 // The meaning of a percentage length value depends on the attribute for which the percentage
316 // length value has been specified. Two common cases are: (a) when a percentage length value
317 // represents a percentage of the viewport width or height (refer to the section that discusses
318 // units in general), and (b) when a percentage length value represents a percentage of the
319 // bounding box width or height on a given object (refer to the section that describes object
320 // bounding box units)."
322 // Comparisons with common browsers show that it's mostly interpreted relative to the viewport
323 // of the parent, and so does the new implementation.
325 // Extract known viewport data
326 // bXXXIsAbsolute tracks whether relative values could be resolved to absolute values
328 // If width or height is not provided, the default 100% is used, see SVG 1.1 section 5.1.2
329 // value 0.0 here is only to initialize variable
330 bool bWidthIsAbsolute(getWidth().isSet() && SvgUnit::percent != getWidth().getUnit());
331 double fW( bWidthIsAbsolute ? getWidth().solveNonPercentage(*this) : 0.0);
333 bool bHeightIsAbsolute(getHeight().isSet() && SvgUnit::percent != getHeight().getUnit());
334 double fH( bHeightIsAbsolute ? getHeight().solveNonPercentage(*this) : 0.0);
336 // If x or y not provided, then default 0.0 is used, see SVG 1.1 Section 5.1.2
337 bool bXIsAbsolute((getX().isSet() && SvgUnit::percent != getX().getUnit()) || !getX().isSet());
338 double fX( bXIsAbsolute && getX().isSet() ? getX().solveNonPercentage(*this) : 0.0);
340 bool bYIsAbsolute((getY().isSet() && SvgUnit::percent != getY().getUnit()) || !getY().isSet());
341 double fY( bYIsAbsolute && getY().isSet() ? getY().solveNonPercentage(*this) : 0.0);
343 if ( !bXIsAbsolute || !bWidthIsAbsolute)
345 // get width of enclosing svg and resolve percentage in x and width;
346 double fWReference(0.0);
347 bool bHasFoundWidth(false);
348 seekReferenceWidth(fWReference, bHasFoundWidth);
349 if (!bHasFoundWidth)
351 if (getViewBox())
353 fWReference = getViewBox()->getWidth();
355 else
357 // Even outermost svg has not all information to resolve relative values,
358 // I use content itself as fallback to set missing values for viewport
359 // Any better idea for such ill structured svg documents?
360 const basegfx::B2DRange aChildRange(
361 aSequence.getB2DRange(
362 drawinglayer::geometry::ViewInformation2D()));
363 fWReference = aChildRange.getWidth();
366 // referenced values are already in 'user unit'
367 if (!bXIsAbsolute)
369 fX = getX().getNumber() * 0.01 * fWReference;
371 if (!bWidthIsAbsolute)
373 fW = (getWidth().isSet() ? getWidth().getNumber() *0.01 : 1.0) * fWReference;
377 if ( !bYIsAbsolute || !bHeightIsAbsolute)
379 // get height of enclosing svg and resolve percentage in y and height
380 double fHReference(0.0);
381 bool bHasFoundHeight(false);
382 seekReferenceHeight(fHReference, bHasFoundHeight);
383 if (!bHasFoundHeight)
385 if (getViewBox())
387 fHReference = getViewBox()->getHeight();
389 else
391 // Even outermost svg has not all information to resolve relative values,
392 // I use content itself as fallback to set missing values for viewport
393 // Any better idea for such ill structured svg documents?
394 const basegfx::B2DRange aChildRange(
395 aSequence.getB2DRange(
396 drawinglayer::geometry::ViewInformation2D()));
397 fHReference = aChildRange.getHeight();
401 // referenced values are already in 'user unit'
402 if (!bYIsAbsolute)
404 fY = getY().getNumber() * 0.01 * fHReference;
406 if (!bHeightIsAbsolute)
408 fH = (getHeight().isSet() ? getHeight().getNumber() *0.01 : 1.0) * fHReference;
412 if(getViewBox())
414 // SVG 1.1 defines in section 7.7 that a negative value for width or height
415 // in viewBox is an error and that 0.0 disables rendering
416 if(basegfx::fTools::more(getViewBox()->getWidth(),0.0) && basegfx::fTools::more(getViewBox()->getHeight(),0.0))
418 // create target range homing x,y, width and height as calculated above
419 const basegfx::B2DRange aTarget(fX, fY, fX + fW, fY + fH);
421 if(aTarget.equal(*getViewBox()))
423 // no mapping needed, append
424 rTarget.append(aSequence);
426 else
428 // create mapping
429 // #i122610 SVG 1.1 defines in section 5.1.2 that if the attribute preserveAspectRatio is not specified,
430 // then the effect is as if a value of 'xMidYMid meet' were specified.
431 SvgAspectRatio aRatioDefault(SvgAlign::xMidYMid,true);
432 const SvgAspectRatio& rRatio = getSvgAspectRatio().isSet()? getSvgAspectRatio() : aRatioDefault;
434 // let mapping be created from SvgAspectRatio
435 const basegfx::B2DHomMatrix aEmbeddingTransform(
436 rRatio.createMapping(aTarget, *getViewBox()));
438 // prepare embedding in transformation
439 const drawinglayer::primitive2d::Primitive2DReference xRef(
440 new drawinglayer::primitive2d::TransformPrimitive2D(
441 aEmbeddingTransform,
442 aSequence));
444 if(rRatio.isMeetOrSlice())
446 // embed in transformation
447 rTarget.push_back(xRef);
449 else
451 // need to embed in MaskPrimitive2D, too
452 const drawinglayer::primitive2d::Primitive2DReference xMask(
453 new drawinglayer::primitive2d::MaskPrimitive2D(
454 basegfx::B2DPolyPolygon(basegfx::utils::createPolygonFromRect(aTarget)),
455 drawinglayer::primitive2d::Primitive2DContainer { xRef }));
457 rTarget.push_back(xMask);
462 else // no viewBox attribute
464 // Svg defines that a negative value is an error and that 0.0 disables rendering
465 if(basegfx::fTools::more(fW, 0.0) && basegfx::fTools::more(fH, 0.0))
467 if(!basegfx::fTools::equalZero(fX) || !basegfx::fTools::equalZero(fY))
469 // embed in transform
470 const drawinglayer::primitive2d::Primitive2DReference xRef(
471 new drawinglayer::primitive2d::TransformPrimitive2D(
472 basegfx::utils::createTranslateB2DHomMatrix(fX, fY),
473 aSequence));
475 aSequence = drawinglayer::primitive2d::Primitive2DContainer { xRef, };
478 // embed in MaskPrimitive2D to clip
479 const drawinglayer::primitive2d::Primitive2DReference xMask(
480 new drawinglayer::primitive2d::MaskPrimitive2D(
481 basegfx::B2DPolyPolygon(
482 basegfx::utils::createPolygonFromRect(
483 basegfx::B2DRange(fX, fY, fX + fW, fY + fH))),
484 aSequence));
486 // append
487 rTarget.push_back(xMask);
491 else // Outermost SVG element
493 // Svg defines that a negative value is an error and that 0.0 disables rendering
494 // isPositive() not usable because it allows 0.0 in contrast to mathematical definition of 'positive'
495 const bool bWidthInvalid(getWidth().isSet() && basegfx::fTools::lessOrEqual(getWidth().getNumber(), 0.0));
496 const bool bHeightInvalid(getHeight().isSet() && basegfx::fTools::lessOrEqual(getHeight().getNumber(), 0.0));
497 if(!bWidthInvalid && !bHeightInvalid)
499 basegfx::B2DRange aSvgCanvasRange; // viewport
500 double fW = 0.0; // dummy values
501 double fH = 0.0;
502 if (const basegfx::B2DRange* pBox = getViewBox())
504 // SVG 1.1 defines in section 7.7 that a negative value for width or height
505 // in viewBox is an error and that 0.0 disables rendering
506 const double fViewBoxWidth = pBox->getWidth();
507 const double fViewBoxHeight = pBox->getHeight();
508 if(basegfx::fTools::more(fViewBoxWidth,0.0) && basegfx::fTools::more(fViewBoxHeight,0.0))
510 // The intrinsic aspect ratio of the svg element is given by absolute values of svg width and svg height
511 // or by the width and height of the viewBox, if svg width or svg height is relative.
512 // see SVG 1.1 section 7.12
513 bool bNeedsMapping(true);
514 const bool bWidthIsAbsolute(getWidth().isSet() && SvgUnit::percent != getWidth().getUnit());
515 const bool bHeightIsAbsolute(getHeight().isSet() && SvgUnit::percent != getHeight().getUnit());
516 const double fViewBoxRatio(fViewBoxWidth/fViewBoxHeight);
517 if(bWidthIsAbsolute && bHeightIsAbsolute)
519 fW = getWidth().solveNonPercentage(*this);
520 fH = getHeight().solveNonPercentage(*this);
521 aSvgCanvasRange = basegfx::B2DRange(0.0, 0.0, fW, fH);
523 else if (bWidthIsAbsolute)
525 fW = getWidth().solveNonPercentage(*this);
526 fH = fW / fViewBoxRatio ;
527 aSvgCanvasRange = basegfx::B2DRange(0.0, 0.0, fW, fH);
529 else if (bHeightIsAbsolute)
531 fH = getHeight().solveNonPercentage(*this);
532 fW = fH * fViewBoxRatio ;
533 aSvgCanvasRange = basegfx::B2DRange(0.0, 0.0, fW, fH);
535 else
537 // There exists no parent to resolve relative width or height.
538 // Use child size as fallback and expand to aspect ratio given
539 // by the viewBox. No mapping.
540 // We get viewport >= content, therefore no clipping.
541 bNeedsMapping = false;
543 const double fChildWidth(pBox->getWidth());
544 const double fChildHeight(pBox->getHeight());
545 const double fLeft(pBox->getMinX());
546 const double fTop(pBox->getMinY());
547 if ( fChildWidth / fViewBoxWidth > fChildHeight / fViewBoxHeight )
548 { // expand y
549 fW = fChildWidth;
550 fH = fChildWidth / fViewBoxRatio;
552 else
553 { // expand x
554 fH = fChildHeight;
555 fW = fChildHeight * fViewBoxRatio;
557 aSvgCanvasRange = basegfx::B2DRange(fLeft, fTop, fLeft + fW, fTop + fH);
560 if (bNeedsMapping)
562 // create mapping
563 // SVG 1.1 defines in section 5.1.2 that if the attribute preserveAspectRatio is not specified,
564 // then the effect is as if a value of 'xMidYMid meet' were specified.
565 SvgAspectRatio aRatioDefault(SvgAlign::xMidYMid, true);
566 const SvgAspectRatio& rRatio = getSvgAspectRatio().isSet()? getSvgAspectRatio() : aRatioDefault;
568 basegfx::B2DHomMatrix aViewBoxMapping = rRatio.createMapping(aSvgCanvasRange, *pBox);
569 // no need to check ratio here for slice, the outermost Svg will
570 // be clipped anyways (see below)
572 // scale content to viewBox definitions
573 const drawinglayer::primitive2d::Primitive2DReference xTransform(
574 new drawinglayer::primitive2d::TransformPrimitive2D(
575 aViewBoxMapping,
576 aSequence));
578 aSequence = drawinglayer::primitive2d::Primitive2DContainer { xTransform };
582 else // no viewbox => no mapping
584 const bool bWidthIsAbsolute(getWidth().isSet() && SvgUnit::percent != getWidth().getUnit());
585 const bool bHeightIsAbsolute(getHeight().isSet() && SvgUnit::percent != getHeight().getUnit());
586 if (bWidthIsAbsolute && bHeightIsAbsolute)
588 fW =getWidth().solveNonPercentage(*this);
589 fH =getHeight().solveNonPercentage(*this);
590 aSvgCanvasRange = basegfx::B2DRange(0.0, 0.0, fW, fH);
592 else
594 // There exists no parent to resolve relative width or height.
595 // Use child size as fallback. We get viewport >= content, therefore no clipping.
596 const basegfx::B2DRange aChildRange(
597 aSequence.getB2DRange(
598 drawinglayer::geometry::ViewInformation2D()));
599 const double fChildWidth(aChildRange.getWidth());
600 const double fChildHeight(aChildRange.getHeight());
601 const double fChildLeft(aChildRange.getMinX());
602 const double fChildTop(aChildRange.getMinY());
603 fW = bWidthIsAbsolute ? getWidth().solveNonPercentage(*this) : fChildWidth;
604 fH = bHeightIsAbsolute ? getHeight().solveNonPercentage(*this) : fChildHeight;
605 const double fLeft(bWidthIsAbsolute ? 0.0 : fChildLeft);
606 const double fTop(bHeightIsAbsolute ? 0.0 : fChildTop);
607 aSvgCanvasRange = basegfx::B2DRange(fLeft, fTop, fLeft+fW, fTop+fH);
612 // to be completely correct in Svg sense it is necessary to clip
613 // the whole content to the given canvas. I choose here to do this
614 // initially despite I found various examples of Svg files out there
615 // which have no correct values for this clipping. It's correct
616 // due to the Svg spec.
618 // different from Svg we have the possibility with primitives to get
619 // a correct bounding box for the geometry. Get it for evtl. taking action
620 const basegfx::B2DRange aContentRange(
621 aSequence.getB2DRange(
622 drawinglayer::geometry::ViewInformation2D()));
624 if(aSvgCanvasRange.isInside(aContentRange))
626 // no clip needed, but an invisible HiddenGeometryPrimitive2D
627 // to allow getting the full Svg range using the primitive mechanisms.
628 // This is needed since e.g. an SdrObject using this as graphic will
629 // create a mapping transformation to exactly map the content to its
630 // real life size
631 const drawinglayer::primitive2d::Primitive2DReference xLine(
632 new drawinglayer::primitive2d::PolygonHairlinePrimitive2D(
633 basegfx::utils::createPolygonFromRect(
634 aSvgCanvasRange),
635 basegfx::BColor(0.0, 0.0, 0.0)));
636 const drawinglayer::primitive2d::Primitive2DReference xHidden(
637 new drawinglayer::primitive2d::HiddenGeometryPrimitive2D(
638 drawinglayer::primitive2d::Primitive2DContainer { xLine }));
640 aSequence.push_back(xHidden);
642 else if(aSvgCanvasRange.overlaps(aContentRange))
644 // Clip is necessary. This will make Svg images evtl. smaller
645 // than wanted from Svg (the free space which may be around it is
646 // conform to the Svg spec), but avoids an expensive and unnecessary
647 // clip. Keep the full Svg range here to get the correct mappings
648 // to objects using this. Optimizations can be done in the processors
649 const drawinglayer::primitive2d::Primitive2DReference xMask(
650 new drawinglayer::primitive2d::MaskPrimitive2D(
651 basegfx::B2DPolyPolygon(
652 basegfx::utils::createPolygonFromRect(
653 aSvgCanvasRange)),
654 aSequence));
656 aSequence = drawinglayer::primitive2d::Primitive2DContainer { xMask };
658 else
660 // not inside, no overlap. Empty Svg
661 aSequence.clear();
664 if(!aSequence.empty())
666 // Another correction:
667 // If no Width/Height is set (usually done in
668 // <svg ... width="215.9mm" height="279.4mm" >) which
669 // is the case for own-Impress-exports, assume that
670 // the Units are already 100ThMM.
671 // Maybe only for own-Impress-exports, thus may need to be
672 // &&ed with getDocument().findSvgNodeById("ooo:meta_slides"),
673 // but does not need to be.
674 bool bEmbedInFinalTransformPxTo100ThMM(true);
676 if(getDocument().findSvgNodeById("ooo:meta_slides")
677 && !getWidth().isSet()
678 && !getHeight().isSet())
680 bEmbedInFinalTransformPxTo100ThMM = false;
683 if(bEmbedInFinalTransformPxTo100ThMM)
685 // embed in transform primitive to scale to 1/100th mm
686 // where 1 inch == 25.4 mm to get from Svg coordinates (px) to
687 // drawinglayer coordinates
688 const double fScaleTo100thmm(25.4 * 100.0 / F_SVG_PIXEL_PER_INCH);
689 const basegfx::B2DHomMatrix aTransform(
690 basegfx::utils::createScaleB2DHomMatrix(
691 fScaleTo100thmm,
692 fScaleTo100thmm));
694 const drawinglayer::primitive2d::Primitive2DReference xTransform(
695 new drawinglayer::primitive2d::TransformPrimitive2D(
696 aTransform,
697 aSequence));
699 aSequence = drawinglayer::primitive2d::Primitive2DContainer { xTransform };
702 // append to result
703 rTarget.append(aSequence);
709 if(!(aSequence.empty() && !getParent() && getViewBox()))
710 return;
712 // tdf#118232 No geometry, Outermost SVG element and we have a ViewBox.
713 // Create a HiddenGeometry Primitive containing an expanded
714 // hairline geometry to have the size contained
715 const drawinglayer::primitive2d::Primitive2DReference xLine(
716 new drawinglayer::primitive2d::PolygonHairlinePrimitive2D(
717 basegfx::utils::createPolygonFromRect(
718 *getViewBox()),
719 basegfx::BColor(0.0, 0.0, 0.0)));
720 const drawinglayer::primitive2d::Primitive2DReference xHidden(
721 new drawinglayer::primitive2d::HiddenGeometryPrimitive2D(
722 drawinglayer::primitive2d::Primitive2DContainer { xLine }));
724 rTarget.push_back(xHidden);
727 basegfx::B2DRange SvgSvgNode::getCurrentViewPort() const
729 if(getViewBox())
731 return *(getViewBox());
733 else // viewport should be given by x, y, width, and height
735 // Extract known viewport data
736 // bXXXIsAbsolute tracks whether relative values could be resolved to absolute values
737 if (getParent())
739 // If width or height is not provided, the default 100% is used, see SVG 1.1 section 5.1.2
740 // value 0.0 here is only to initialize variable
741 bool bWidthIsAbsolute(getWidth().isSet() && SvgUnit::percent != getWidth().getUnit());
742 double fW( bWidthIsAbsolute ? getWidth().solveNonPercentage(*this) : 0.0);
743 bool bHeightIsAbsolute(getHeight().isSet() && SvgUnit::percent != getHeight().getUnit());
744 double fH( bHeightIsAbsolute ? getHeight().solveNonPercentage(*this) : 0.0);
746 // If x or y not provided, then default 0.0 is used, see SVG 1.1 Section 5.1.2
747 bool bXIsAbsolute((getX().isSet() && SvgUnit::percent != getX().getUnit()) || !getX().isSet());
748 double fX( bXIsAbsolute && getX().isSet() ? getX().solveNonPercentage(*this) : 0.0);
750 bool bYIsAbsolute((getY().isSet() && SvgUnit::percent != getY().getUnit()) || !getY().isSet());
751 double fY( bYIsAbsolute && getY().isSet() ? getY().solveNonPercentage(*this) : 0.0);
753 if (bXIsAbsolute && bYIsAbsolute && bWidthIsAbsolute && bHeightIsAbsolute)
755 return basegfx::B2DRange(fX, fY, fX+fW, fY+fH);
757 else // try to resolve relative values
759 if (!bXIsAbsolute || !bWidthIsAbsolute)
761 // get width of enclosing svg and resolve percentage in x and width
762 double fWReference(0.0);
763 bool bHasFoundWidth(false);
764 seekReferenceWidth(fWReference, bHasFoundWidth);
765 // referenced values are already in 'user unit'
766 if (!bXIsAbsolute && bHasFoundWidth)
768 fX = getX().getNumber() * 0.01 * fWReference;
769 bXIsAbsolute = true;
771 if (!bWidthIsAbsolute && bHasFoundWidth)
773 fW = (getWidth().isSet() ? getWidth().getNumber() *0.01 : 1.0) * fWReference;
774 bWidthIsAbsolute = true;
777 if (!bYIsAbsolute || !bHeightIsAbsolute)
779 // get height of enclosing svg and resolve percentage in y and height
780 double fHReference(0.0);
781 bool bHasFoundHeight(false);
782 seekReferenceHeight(fHReference, bHasFoundHeight);
783 // referenced values are already in 'user unit'
784 if (!bYIsAbsolute && bHasFoundHeight)
786 fY = getY().getNumber() * 0.01 * fHReference;
787 bYIsAbsolute = true;
789 if (!bHeightIsAbsolute && bHasFoundHeight)
791 fH = (getHeight().isSet() ? getHeight().getNumber() *0.01 : 1.0) * fHReference;
792 bHeightIsAbsolute = true;
796 if (bXIsAbsolute && bYIsAbsolute && bWidthIsAbsolute && bHeightIsAbsolute)
798 return basegfx::B2DRange(fX, fY, fX+fW, fY+fH);
800 else // relative values could not be resolved, there exists no fallback
802 return SvgNode::getCurrentViewPort();
806 else //outermost svg
808 // If width or height is not provided, the default would be 100%, see SVG 1.1 section 5.1.2
809 // But here it cannot be resolved and no fallback exists.
810 // SVG 1.1 defines in section 5.1.2 that x,y has no meaning for the outermost SVG element.
811 bool bWidthIsAbsolute(getWidth().isSet() && SvgUnit::percent != getWidth().getUnit());
812 bool bHeightIsAbsolute(getHeight().isSet() && SvgUnit::percent != getHeight().getUnit());
813 if (bWidthIsAbsolute && bHeightIsAbsolute)
815 double fW( getWidth().solveNonPercentage(*this) );
816 double fH( getHeight().solveNonPercentage(*this) );
817 return basegfx::B2DRange(0.0, 0.0, fW, fH);
819 else // no fallback exists
821 return SvgNode::getCurrentViewPort();
824 // TODO: Is it possible to decompose and use the bounding box of the children, if even the
825 // outermost svg has no information to resolve percentage? Is it worth, how expensive is it?
830 } // end of namespace svgio
832 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */