LibreOffice Module svgio (master) 1
svgsvgnode.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <svgsvgnode.hxx>
30#include <svgdocument.hxx>
31
32namespace svgio::svgreader
33{
35 SvgDocument& rDocument,
36 SvgNode* pParent)
37 : SvgNode(SVGToken::Svg, rDocument, pParent),
38 maSvgStyleAttributes(*this),
39 mbStyleAttributesInitialized(false) // #i125258#
40 {
41 }
42
43 // #i125258#
45 {
47 return;
48
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);
53
54 if(getParent())
55 {
56 // #i125258# no initial values when it's a SVG element embedded in SVG
57 bSetInitialValues = false;
58 }
59
60 if(bSetInitialValues)
61 {
63
64 if(pStyles && pStyles->getParentStyle())
65 {
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->getParentStyle();
70
71 while(pParentStyle && !bFillSet)
72 {
73 bFillSet = pParentStyle->isFillSet();
74 pParentStyle = pParentStyle->getParentStyle();
75 }
76
77 if(bFillSet)
78 {
79 // #125258# no initial values when SVG has a parent style at which a fill
80 // is already set
81 bSetInitialValues = false;
82 }
83 }
84 }
85
86 if(bSetInitialValues)
87 {
88 // #i125258# only set if not yet initialized (SvgSvgNode::parseAttribute is already done,
89 // just setting may revert an already set valid value)
91 {
92 // #i125258# initial fill is black (see SVG1.1 spec)
93 maSvgStyleAttributes.setFill(SvgPaint(basegfx::BColor(0.0, 0.0, 0.0), true, true));
94 }
95 }
96
98 }
99
101 {
102 }
103
105 {
106 // #i125258# svg node can have CssStyles, too, so check for it here
108 }
109
110 void SvgSvgNode::parseAttribute(const OUString& rTokenName, SVGToken aSVGToken, const OUString& aContent)
111 {
112 // call parent
113 SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
114
115 // read style attributes
116 maSvgStyleAttributes.parseStyleAttribute(aSVGToken, aContent);
117
118 // parse own
119 switch(aSVGToken)
120 {
121 case SVGToken::Style:
122 {
123 readLocalCssStyle(aContent);
124 break;
125 }
127 {
128 const basegfx::B2DRange aRange(readViewBox(aContent, *this));
129
130 if(!aRange.isEmpty())
131 {
132 setViewBox(&aRange);
133 }
134 break;
135 }
137 {
139 break;
140 }
141 case SVGToken::X:
142 {
143 SvgNumber aNum;
144
145 if(readSingleNumber(aContent, aNum))
146 {
147 maX = aNum;
148 }
149 break;
150 }
151 case SVGToken::Y:
152 {
153 SvgNumber aNum;
154
155 if(readSingleNumber(aContent, aNum))
156 {
157 maY = aNum;
158 }
159 break;
160 }
161 case SVGToken::Width:
162 {
163 SvgNumber aNum;
164
165 if(readSingleNumber(aContent, aNum))
166 {
167 if(aNum.isPositive())
168 {
169 maWidth = aNum;
170 }
171 }
172 break;
173 }
174 case SVGToken::Height:
175 {
176 SvgNumber aNum;
177
178 if(readSingleNumber(aContent, aNum))
179 {
180 if(aNum.isPositive())
181 {
182 maHeight = aNum;
183 }
184 }
185 break;
186 }
188 {
189 SvgNumber aNum;
190
191 if(readSingleNumber(aContent, aNum))
192 {
193 maVersion = aNum;
194 }
195 break;
196 }
197 default:
198 {
199 break;
200 }
201 }
202 }
203
204 void SvgSvgNode::seekReferenceWidth(double& fWidth, bool& bHasFound) const
205 {
206 if (!getParent() || bHasFound)
207 {
208 return;
209 }
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())
215 {
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)
219 {
220 if (pParentSvgSvgNode->getViewBox())
221 {
222 // viewbox values are already in 'user unit'.
223 fWidth = pParentSvgSvgNode->getViewBox()->getWidth() * fPercentage;
224 bHasFound = true;
225 }
226 else
227 {
228 // take absolute value or cumulate percentage
229 if (pParentSvgSvgNode->getWidth().isSet())
230 {
231 if (SvgUnit::percent == pParentSvgSvgNode->getWidth().getUnit())
232 {
233 fPercentage *= pParentSvgSvgNode->getWidth().getNumber() * 0.01;
234 }
235 else
236 {
237 fWidth = pParentSvgSvgNode->getWidth().solveNonPercentage(*pParentSvgSvgNode) * fPercentage;
238 bHasFound = true;
239 }
240 } // not set => width=100% => factor 1, no need for else
241 }
242 }
243 }
244 }
245
246 void SvgSvgNode::seekReferenceHeight(double& fHeight, bool& bHasFound) const
247 {
248 if (!getParent() || bHasFound)
249 {
250 return;
251 }
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())
257 {
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)
261 {
262 if (pParentSvgSvgNode->getViewBox())
263 {
264 // viewbox values are already in 'user unit'.
265 fHeight = pParentSvgSvgNode->getViewBox()->getHeight() * fPercentage;
266 bHasFound = true;
267 }
268 else
269 {
270 // take absolute value or cumulate percentage
271 if (pParentSvgSvgNode->getHeight().isSet())
272 {
273 if (SvgUnit::percent == pParentSvgSvgNode->getHeight().getUnit())
274 {
275 fPercentage *= pParentSvgSvgNode->getHeight().getNumber() * 0.01;
276 }
277 else
278 {
279 fHeight = pParentSvgSvgNode->getHeight().solveNonPercentage(*pParentSvgSvgNode) * fPercentage;
280 bHasFound = true;
281 }
282 } // not set => height=100% => factor 1, no need for else
283 }
284 }
285 }
286 }
287
288// ToDo: Consider attribute overflow in method decomposeSvgNode
290 {
292
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();
297
298 // decompose children
299 SvgNode::decomposeSvgNode(aSequence, bReferenced);
300
301 if(!aSequence.empty())
302 {
303 if(getParent())
304 {
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):
308
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)."
316
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.
319
320 // Extract known viewport data
321 // bXXXIsAbsolute tracks whether relative values could be resolved to absolute values
322
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);
327
328 bool bHeightIsAbsolute(getHeight().isSet() && SvgUnit::percent != getHeight().getUnit());
329 double fH( bHeightIsAbsolute ? getHeight().solveNonPercentage(*this) : 0.0);
330
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);
334
335 bool bYIsAbsolute((getY().isSet() && SvgUnit::percent != getY().getUnit()) || !getY().isSet());
336 double fY( bYIsAbsolute && getY().isSet() ? getY().solveNonPercentage(*this) : 0.0);
337
338 if ( !bXIsAbsolute || !bWidthIsAbsolute)
339 {
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)
345 {
346 if (getViewBox())
347 {
348 fWReference = getViewBox()->getWidth();
349 }
350 else
351 {
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(
358 fWReference = aChildRange.getWidth();
359 }
360 }
361 // referenced values are already in 'user unit'
362 if (!bXIsAbsolute)
363 {
364 fX = getX().getNumber() * 0.01 * fWReference;
365 }
366 if (!bWidthIsAbsolute)
367 {
368 fW = (getWidth().isSet() ? getWidth().getNumber() *0.01 : 1.0) * fWReference;
369 }
370 }
371
372 if ( !bYIsAbsolute || !bHeightIsAbsolute)
373 {
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)
379 {
380 if (getViewBox())
381 {
382 fHReference = getViewBox()->getHeight();
383 }
384 else
385 {
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(
392 fHReference = aChildRange.getHeight();
393 }
394 }
395
396 // referenced values are already in 'user unit'
397 if (!bYIsAbsolute)
398 {
399 fY = getY().getNumber() * 0.01 * fHReference;
400 }
401 if (!bHeightIsAbsolute)
402 {
403 fH = (getHeight().isSet() ? getHeight().getNumber() *0.01 : 1.0) * fHReference;
404 }
405 }
406
407 if(getViewBox())
408 {
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
412 {
413 // create target range homing x,y, width and height as calculated above
414 const basegfx::B2DRange aTarget(fX, fY, fX + fW, fY + fH);
415
416 if(aTarget.equal(*getViewBox()))
417 {
418 // no mapping needed, append
419 rTarget.append(aSequence);
420 }
421 else
422 {
423 // create mapping
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;
428
429 // let mapping be created from SvgAspectRatio
430 const basegfx::B2DHomMatrix aEmbeddingTransform(
431 rRatio.createMapping(aTarget, *getViewBox()));
432
433 // prepare embedding in transformation
436 aEmbeddingTransform,
438
439 if(rRatio.isMeetOrSlice())
440 {
441 // embed in transformation
442 rTarget.push_back(xRef);
443 }
444 else
445 {
446 // need to embed in MaskPrimitive2D, too
451
452 rTarget.push_back(xMask);
453 }
454 }
455 }
456 }
457 else // no viewBox attribute
458 {
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))
461 {
463 {
464 // embed in transform
468 std::move(aSequence)));
469
471 }
472
473 // embed in MaskPrimitive2D to clip
478 basegfx::B2DRange(fX, fY, fX + fW, fY + fH))),
480
481 // append
482 rTarget.push_back(xMask);
483 }
484 }
485 }
486 else // Outermost SVG element
487 {
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)
493 {
494 basegfx::B2DRange aSvgCanvasRange; // viewport
495 double fW = 0.0; // dummy values
496 double fH = 0.0;
497 if (const basegfx::B2DRange* pBox = getViewBox())
498 {
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))
504 {
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)
513 {
514 fW = getWidth().solveNonPercentage(*this);
515 fH = getHeight().solveNonPercentage(*this);
516 aSvgCanvasRange = basegfx::B2DRange(0.0, 0.0, fW, fH);
517 }
518 else if (bWidthIsAbsolute)
519 {
520 fW = getWidth().solveNonPercentage(*this);
521 fH = fW / fViewBoxRatio ;
522 aSvgCanvasRange = basegfx::B2DRange(0.0, 0.0, fW, fH);
523 }
524 else if (bHeightIsAbsolute)
525 {
526 fH = getHeight().solveNonPercentage(*this);
527 fW = fH * fViewBoxRatio ;
528 aSvgCanvasRange = basegfx::B2DRange(0.0, 0.0, fW, fH);
529 }
530 else
531 {
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;
537
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 )
543 { // expand y
544 fW = fChildWidth;
545 fH = fChildWidth / fViewBoxRatio;
546 }
547 else
548 { // expand x
549 fH = fChildHeight;
550 fW = fChildHeight * fViewBoxRatio;
551 }
552 aSvgCanvasRange = basegfx::B2DRange(fLeft, fTop, fLeft + fW, fTop + fH);
553 }
554
555 if (bNeedsMapping)
556 {
557 // create mapping
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;
562
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)
566
567 // scale content to viewBox definitions
570 aViewBoxMapping,
571 std::move(aSequence)));
572
573 aSequence = drawinglayer::primitive2d::Primitive2DContainer { xTransform };
574 }
575 }
576 }
577 else // no viewbox => no mapping
578 {
579 const bool bWidthIsAbsolute(getWidth().isSet() && SvgUnit::percent != getWidth().getUnit());
580 const bool bHeightIsAbsolute(getHeight().isSet() && SvgUnit::percent != getHeight().getUnit());
581 if (bWidthIsAbsolute && bHeightIsAbsolute)
582 {
583 fW =getWidth().solveNonPercentage(*this);
584 fH =getHeight().solveNonPercentage(*this);
585 aSvgCanvasRange = basegfx::B2DRange(0.0, 0.0, fW, fH);
586 }
587 else
588 {
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(
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);
603 }
604
605 }
606
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.
612
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(
618
619 if(aSvgCanvasRange.isInside(aContentRange))
620 {
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
625 // real life size
629 aSvgCanvasRange),
630 basegfx::BColor(0.0, 0.0, 0.0)));
634
635 aSequence.push_back(xHidden);
636 }
637 else if(aSvgCanvasRange.overlaps(aContentRange))
638 {
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
648 aSvgCanvasRange)),
649 std::move(aSequence)));
650
652 }
653 else
654 {
655 // not inside, no overlap. Empty Svg
656 aSequence.clear();
657 }
658
659 if(!aSequence.empty())
660 {
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);
670
671 if(getDocument().findSvgNodeById("ooo:meta_slides")
672 && !getWidth().isSet()
673 && !getHeight().isSet())
674 {
675 bEmbedInFinalTransformPxTo100ThMM = false;
676 }
677
678 if(bEmbedInFinalTransformPxTo100ThMM)
679 {
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(
685 fScaleTo100thmm,
686 fScaleTo100thmm));
687
690 aTransform,
691 std::move(aSequence)));
692
693 aSequence = drawinglayer::primitive2d::Primitive2DContainer { xTransform };
694 }
695
696 // append to result
697 rTarget.append(aSequence);
698 }
699 }
700 }
701 }
702
703 if(!(aSequence.empty() && !getParent() && getViewBox()))
704 return;
705
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
712 *getViewBox()),
713 basegfx::BColor(0.0, 0.0, 0.0)));
717
718 rTarget.push_back(xHidden);
719 }
720
722 {
723 if(getViewBox())
724 {
725 return *(getViewBox());
726 }
727 else // viewport should be given by x, y, width, and height
728 {
729 // Extract known viewport data
730 // bXXXIsAbsolute tracks whether relative values could be resolved to absolute values
731 if (getParent())
732 {
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);
739
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);
743
744 bool bYIsAbsolute((getY().isSet() && SvgUnit::percent != getY().getUnit()) || !getY().isSet());
745 double fY( bYIsAbsolute && getY().isSet() ? getY().solveNonPercentage(*this) : 0.0);
746
747 if (bXIsAbsolute && bYIsAbsolute && bWidthIsAbsolute && bHeightIsAbsolute)
748 {
749 return basegfx::B2DRange(fX, fY, fX+fW, fY+fH);
750 }
751 else // try to resolve relative values
752 {
753 if (!bXIsAbsolute || !bWidthIsAbsolute)
754 {
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)
761 {
762 fX = getX().getNumber() * 0.01 * fWReference;
763 bXIsAbsolute = true;
764 }
765 if (!bWidthIsAbsolute && bHasFoundWidth)
766 {
767 fW = (getWidth().isSet() ? getWidth().getNumber() *0.01 : 1.0) * fWReference;
768 bWidthIsAbsolute = true;
769 }
770 }
771 if (!bYIsAbsolute || !bHeightIsAbsolute)
772 {
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)
779 {
780 fY = getY().getNumber() * 0.01 * fHReference;
781 bYIsAbsolute = true;
782 }
783 if (!bHeightIsAbsolute && bHasFoundHeight)
784 {
785 fH = (getHeight().isSet() ? getHeight().getNumber() *0.01 : 1.0) * fHReference;
786 bHeightIsAbsolute = true;
787 }
788 }
789
790 if (bXIsAbsolute && bYIsAbsolute && bWidthIsAbsolute && bHeightIsAbsolute)
791 {
792 return basegfx::B2DRange(fX, fY, fX+fW, fY+fH);
793 }
794 else // relative values could not be resolved, there exists no fallback
795 {
797 }
798 }
799 }
800 else //outermost svg
801 {
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)
808 {
809 double fW( getWidth().solveNonPercentage(*this) );
810 double fH( getHeight().solveNonPercentage(*this) );
811 return basegfx::B2DRange(0.0, 0.0, fW, fH);
812 }
813 else // no fallback exists
814 {
816 }
817 }
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?
820
821 }
822 }
823
824} // end of namespace svgio
825
826/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
TYPE getWidth() const
TYPE getMinX() const
TYPE getMinY() const
bool isInside(const Tuple2D< TYPE > &rTuple) const
bool isEmpty() const
bool equal(const Range2D &rRange) const
TYPE getHeight() const
bool overlaps(const Range2D &rRange) const
basegfx::B2DRange getB2DRange(const geometry::ViewInformation2D &aViewInformation) const
basegfx::B2DHomMatrix createMapping(const basegfx::B2DRange &rTarget, const basegfx::B2DRange &rSource) const
Definition: svgtools.cxx:206
virtual void decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer &rTarget, bool bReferenced) const
Definition: svgnode.cxx:584
const SvgStyleAttributes * checkForCssStyle(const SvgStyleAttributes &rOriginal) const
helper to evtl. link to css style
Definition: svgnode.cxx:335
const SvgNode * getParent() const
Definition: svgnode.hxx:158
virtual basegfx::B2DRange getCurrentViewPort() const override
InfoProvider support for %, em and ex values.
Definition: svgnode.cxx:702
virtual void parseAttribute(const OUString &rTokenName, SVGToken aSVGToken, const OUString &aContent)
Definition: svgnode.cxx:534
void readLocalCssStyle(std::u16string_view aContent)
scan helper to read and interpret a local CssStyle to mpLocalCssStyle
Definition: svgnode.cxx:412
const SvgDocument & getDocument() const
Definition: svgnode.hxx:157
SvgUnit getUnit() const
Definition: SvgNumber.hxx:90
double solveNonPercentage(const InfoProvider &rInfoProvider) const
Definition: SvgNumber.cxx:28
double getNumber() const
Definition: SvgNumber.hxx:85
const SvgStyleAttributes * getParentStyle() const
void parseStyleAttribute(SVGToken aSVGToken, const OUString &rContent)
local attribute scanner
const SvgAspectRatio & getSvgAspectRatio() const
SvgAspectRatio content.
Definition: svgsvgnode.hxx:76
void setViewBox(const basegfx::B2DRange *pViewBox)
Definition: svgsvgnode.hxx:73
virtual ~SvgSvgNode() override
Definition: svgsvgnode.cxx:100
SvgStyleAttributes maSvgStyleAttributes
use styles
Definition: svgsvgnode.hxx:31
virtual const SvgStyleAttributes * getSvgStyleAttributes() const override
Definition: svgsvgnode.cxx:104
virtual void decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer &rTarget, bool bReferenced) const override
Definition: svgsvgnode.cxx:289
SvgSvgNode(SvgDocument &rDocument, SvgNode *pParent)
Definition: svgsvgnode.cxx:34
SvgAspectRatio maSvgAspectRatio
Definition: svgsvgnode.hxx:36
const SvgNumber & getWidth() const
width content
Definition: svgsvgnode.hxx:85
void seekReferenceWidth(double &fWidth, bool &bHasFound) const
Seeks width and height of viewport, which is current before the new viewport is set.
Definition: svgsvgnode.cxx:204
const SvgNumber & getX() const
x content
Definition: svgsvgnode.hxx:79
virtual basegfx::B2DRange getCurrentViewPort() const override
InfoProvider support for % values in children.
Definition: svgsvgnode.cxx:721
const SvgNumber & getY() const
y content
Definition: svgsvgnode.hxx:82
void seekReferenceHeight(double &fHeight, bool &bHasFound) const
Definition: svgsvgnode.cxx:246
const basegfx::B2DRange * getViewBox() const
viewBox content
Definition: svgsvgnode.hxx:72
virtual void parseAttribute(const OUString &rTokenName, SVGToken aSVGToken, const OUString &aContent) override
Definition: svgsvgnode.cxx:110
const SvgNumber & getHeight() const
height content
Definition: svgsvgnode.hxx:88
bool mbStyleAttributesInitialized
#i125258# bitfield
Definition: svgsvgnode.hxx:44
FilterGroup & rTarget
bool lessOrEqual(const T &rfValA, const T &rfValB)
bool more(const T &rfValA, const T &rfValB)
bool equalZero(const T &rfVal)
B2DPolygon createPolygonFromRect(const B2DRectangle &rRect, double fRadiusX, double fRadiusY)
B2DHomMatrix createScaleB2DHomMatrix(double fScaleX, double fScaleY)
B2DHomMatrix createTranslateB2DHomMatrix(double fTranslateX, double fTranslateY)
constexpr Point convert(const Point &rPoint, o3tl::Length eFrom, o3tl::Length eTo)
bool readSingleNumber(std::u16string_view rCandidate, SvgNumber &aNum)
Definition: svgtools.cxx:1076
SvgAspectRatio readSvgAspectRatio(std::u16string_view rCandidate)
Definition: svgtools.cxx:1197
basegfx::B2DRange readViewBox(std::u16string_view rCandidate, InfoProvider const &rInfoProvider)
Definition: svgtools.cxx:808