summaryrefslogtreecommitdiff
path: root/svgio/source/svgreader/svgsvgnode.cxx
blob: 43fa092cd2c163dc11a742591ab9807762c994fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <svgsvgnode.hxx>
#include <drawinglayer/geometry/viewinformation2d.hxx>
#include <drawinglayer/primitive2d/transformprimitive2d.hxx>
#include <drawinglayer/primitive2d/maskprimitive2d.hxx>
#include <basegfx/polygon/b2dpolygontools.hxx>
#include <basegfx/polygon/b2dpolygon.hxx>
#include <basegfx/matrix/b2dhommatrixtools.hxx>
#include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
#include <drawinglayer/primitive2d/hiddengeometryprimitive2d.hxx>

namespace svgio
{
    namespace svgreader
    {
        SvgSvgNode::SvgSvgNode(
            SvgDocument& rDocument,
            SvgNode* pParent)
        :   SvgNode(SVGTokenSvg, rDocument, pParent),
            maSvgStyleAttributes(*this),
            mpViewBox(nullptr),
            maSvgAspectRatio(),
            maX(),
            maY(),
            maWidth(),
            maHeight(),
            maVersion(),
            mbStyleAttributesInitialized(false) // #i125258#
        {
        }

        // #i125258#
        void SvgSvgNode::initializeStyleAttributes()
        {
            if(!mbStyleAttributesInitialized)
            {
                // #i125258# determine if initial values need to be initialized with hard values
                // for the case that this is the outmost SVG statement and it has no parent
                // stale (CssStyle for svg may be defined)
                bool bSetInitialValues(true);

                if(getParent())
                {
                    // #i125258# no initial values when it's a SVG element embedded in SVG
                    bSetInitialValues = false;
                }

                if(bSetInitialValues)
                {
                    const SvgStyleAttributes* pStyles = getSvgStyleAttributes();

                    if(pStyles && pStyles->getParentStyle())
                    {
                        // SVG has a parent style (probably CssStyle), check if fill is set there anywhere
                        // already. If yes, do not set the default fill (black)
                        bool bFillSet(false);
                        const SvgStyleAttributes* pParentStyle = pStyles->getParentStyle();

                        while(pParentStyle && !bFillSet)
                        {
                            bFillSet = pParentStyle->isFillSet();
                            pParentStyle = pParentStyle->getParentStyle();
                        }

                        if(bFillSet)
                        {
                            // #125258# no initial values when SVG has a parent style at which a fill
                            // is already set
                            bSetInitialValues = false;
                        }
                    }
                }

                if(bSetInitialValues)
                {
                    // #i125258# only set if not yet initialized (SvgSvgNode::parseAttribute is already done,
                    // just setting may revert an already set valid value)
                    if(!maSvgStyleAttributes.isFillSet())
                    {
                        // #i125258# initial fill is black (see SVG1.1 spec)
                        maSvgStyleAttributes.setFill(SvgPaint(basegfx::BColor(0.0, 0.0, 0.0), true, true));
                    }
                }

                mbStyleAttributesInitialized = true;
            }
        }

        SvgSvgNode::~SvgSvgNode()
        {
        }

        const SvgStyleAttributes* SvgSvgNode::getSvgStyleAttributes() const
        {
            // #i125258# svg node can have CssStyles, too, so check for it here
            return checkForCssStyle("svg", maSvgStyleAttributes);
        }

        void SvgSvgNode::parseAttribute(const OUString& rTokenName, SVGToken aSVGToken, const OUString& aContent)
        {
            // call parent
            SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);

            // read style attributes
            maSvgStyleAttributes.parseStyleAttribute(aSVGToken, aContent, false);

            // parse own
            switch(aSVGToken)
            {
                case SVGTokenStyle:
                {
                    readLocalCssStyle(aContent);
                    break;
                }
                case SVGTokenViewBox:
                {
                    const basegfx::B2DRange aRange(readViewBox(aContent, *this));

                    if(!aRange.isEmpty())
                    {
                        setViewBox(&aRange);
                    }
                    break;
                }
                case SVGTokenPreserveAspectRatio:
                {
                    maSvgAspectRatio = readSvgAspectRatio(aContent);
                    break;
                }
                case SVGTokenX:
                {
                    SvgNumber aNum;

                    if(readSingleNumber(aContent, aNum))
                    {
                        maX = aNum;
                    }
                    break;
                }
                case SVGTokenY:
                {
                    SvgNumber aNum;

                    if(readSingleNumber(aContent, aNum))
                    {
                        maY = aNum;
                    }
                    break;
                }
                case SVGTokenWidth:
                {
                    SvgNumber aNum;

                    if(readSingleNumber(aContent, aNum))
                    {
                        if(aNum.isPositive())
                        {
                            maWidth = aNum;
                        }
                    }
                    break;
                }
                case SVGTokenHeight:
                {
                    SvgNumber aNum;

                    if(readSingleNumber(aContent, aNum))
                    {
                        if(aNum.isPositive())
                        {
                            maHeight = aNum;
                        }
                    }
                    break;
                }
                case SVGTokenVersion:
                {
                    SvgNumber aNum;

                    if(readSingleNumber(aContent, aNum))
                    {
                        maVersion = aNum;
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
        }

        void SvgSvgNode::seekReferenceWidth(double& fWidth, bool& bHasFound) const
        {
            if (!getParent() || bHasFound)
            {
                return;
            }
            const SvgSvgNode* pParentSvgSvgNode = nullptr;
            // enclosing svg might have relative width, need to cumulate them till they are
            // resolved somewhere up in the node tree
            double fPercentage(1.0);
            for(const SvgNode* pParent = getParent(); pParent && !bHasFound; pParent = pParent->getParent())
            {
                // dynamic_cast results Null-pointer for not SvgSvgNode and so skips them in if condition
                pParentSvgSvgNode = dynamic_cast< const SvgSvgNode* >(pParent);
                if (pParentSvgSvgNode)
                {
                    if (pParentSvgSvgNode->getViewBox())
                    {
                        // viewbox values are already in 'user unit'.
                        fWidth = pParentSvgSvgNode->getViewBox()->getWidth() * fPercentage;
                        bHasFound = true;
                    }
                    else
                    {
                        // take absolute value or cumulate percentage
                        if (pParentSvgSvgNode->getWidth().isSet())
                        {
                            if (Unit_percent == pParentSvgSvgNode->getWidth().getUnit())
                            {
                                fPercentage *= pParentSvgSvgNode->getWidth().getNumber() * 0.01;
                            }
                            else
                            {
                                fWidth = pParentSvgSvgNode->getWidth().solveNonPercentage(*pParentSvgSvgNode) * fPercentage;
                                bHasFound = true;
                            }
                        } // not set => width=100% => factor 1, no need for else
                    }
                }
            }
        }

        void SvgSvgNode::seekReferenceHeight(double& fHeight, bool& bHasFound) const
        {
            if (!getParent() || bHasFound)
            {
                return;
            }
            const SvgSvgNode* pParentSvgSvgNode = nullptr;
            // enclosing svg might have relative width and height, need to cumulate them till they are
            // resolved somewhere up in the node tree
            double fPercentage(1.0);
            for(const SvgNode* pParent = getParent(); pParent && !bHasFound; pParent = pParent->getParent())
            {
                // dynamic_cast results Null-pointer for not SvgSvgNode and so skips them in if condition
                pParentSvgSvgNode = dynamic_cast< const SvgSvgNode* >(pParent);
                if (pParentSvgSvgNode)
                {
                    if (pParentSvgSvgNode->getViewBox())
                    {
                        // viewbox values are already in 'user unit'.
                        fHeight = pParentSvgSvgNode->getViewBox()->getHeight() * fPercentage;
                        bHasFound = true;
                    }
                    else
                    {
                        // take absolute value or cumulate percentage
                        if (pParentSvgSvgNode->getHeight().isSet())
                        {
                            if (Unit_percent == pParentSvgSvgNode->getHeight().getUnit())
                            {
                                fPercentage *= pParentSvgSvgNode->getHeight().getNumber() * 0.01;
                            }
                            else
                            {
                                fHeight = pParentSvgSvgNode->getHeight().solveNonPercentage(*pParentSvgSvgNode) * fPercentage;
                                bHasFound = true;
                            }
                        } // not set => height=100% => factor 1, no need for else
                    }
                }
            }
        }

// ToDo: Consider attribute overflow in method decomposeSvgNode
        void SvgSvgNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer& rTarget, bool bReferenced) const
        {
            drawinglayer::primitive2d::Primitive2DContainer aSequence;

            // #i125258# check now if we need to init some style settings locally. Do not do this
            // in the constructor, there is not yet information e.g. about existing CssStyles.
            // Here all nodes are read and interpreted
            const_cast< SvgSvgNode* >(this)->initializeStyleAttributes();

            // decompose children
            SvgNode::decomposeSvgNode(aSequence, bReferenced);

            if(!aSequence.empty())
            {
                if(getParent())
                {
                    // #i122594# if width/height is not given, it's 100% (see 5.1.2 The 'svg' element in SVG1.1 spec).
                    // If it is relative, the question is to what. The previous implementatin assumed relative to the
                    // local ViewBox which is implied by (4.2 Basic data types):

                    // "Note that the non-property <length> definition also allows a percentage unit identifier.
                    // The meaning of a percentage length value depends on the attribute for which the percentage
                    // length value has been specified. Two common cases are: (a) when a percentage length value
                    // represents a percentage of the viewport width or height (refer to the section that discusses
                    // units in general), and (b) when a percentage length value represents a percentage of the
                    // bounding box width or height on a given object (refer to the section that describes object
                    // bounding box units)."

                    // Comparisons with common browsers show that it's mostly interpreted relative to the viewport
                    // of the parent, and so does the new implementation.

                    // Extract known viewport data
                    // bXXXIsAbsolute tracks whether relative values could be resolved to absolute values

                    // If width or height is not provided, the default 100% is used, see SVG 1.1 section 5.1.2
                    // value 0.0 here is only to initialize variable
                    bool bWidthIsAbsolute(getWidth().isSet() && Unit_percent != getWidth().getUnit());
                    double fW( bWidthIsAbsolute ? getWidth().solveNonPercentage(*this) : 0.0);

                    bool bHeightIsAbsolute(getHeight().isSet() && Unit_percent != getHeight().getUnit());
                    double fH( bHeightIsAbsolute ? getHeight().solveNonPercentage(*this) : 0.0);

                    // If x or y not provided, then default 0.0 is used, see SVG 1.1 Section 5.1.2
                    bool bXIsAbsolute((getX().isSet() && Unit_percent != getX().getUnit()) || !getX().isSet());
                    double fX( bXIsAbsolute && getX().isSet() ? getX().solveNonPercentage(*this) : 0.0);

                    bool bYIsAbsolute((getY().isSet() && Unit_percent != getY().getUnit()) || !getY().isSet());
                    double fY( bYIsAbsolute && getY().isSet() ? getY().solveNonPercentage(*this) : 0.0);

                    if ( !bXIsAbsolute || !bWidthIsAbsolute)
                    {
                        // get width of enclosing svg and resolve percentage in x and width;
                        double fWReference(0.0);
                        bool bHasFoundWidth(false);
                        seekReferenceWidth(fWReference, bHasFoundWidth);
                        if (!bHasFoundWidth)
                        {
                            // Even outermost svg has not all information to resolve relative values,
                            // I use content itself as fallback to set missing values for viewport
                            // Any better idea for such ill structured svg documents?
                            const basegfx::B2DRange aChildRange(
                                        aSequence.getB2DRange(
                                            drawinglayer::geometry::ViewInformation2D()));
                            fWReference = aChildRange.getWidth();
                        }
                        // referenced values are already in 'user unit'
                        if (!bXIsAbsolute)
                        {
                            fX = getX().getNumber() * 0.01 * fWReference;
                        }
                        if (!bWidthIsAbsolute)
                        {
                            fW = (getWidth().isSet() ? getWidth().getNumber() *0.01 : 1.0) * fWReference;
                        }
                    }

                    if ( !bYIsAbsolute || !bHeightIsAbsolute)
                    {
                        // get height of enclosing svg and resolve percentage in y and height
                        double fHReference(0.0);
                        bool bHasFoundHeight(false);
                        seekReferenceHeight(fHReference, bHasFoundHeight);
                        if (!bHasFoundHeight)
                        {
                            // Even outermost svg has not all information to resolve relative values,
                            // I use content itself as fallback to set missing values for viewport
                            // Any better idea for such ill structured svg documents?
                            const basegfx::B2DRange aChildRange(
                                    aSequence.getB2DRange(
                                        drawinglayer::geometry::ViewInformation2D()));
                            fHReference = aChildRange.getHeight();
                        }

                        // referenced values are already in 'user unit'
                        if (!bYIsAbsolute)
                        {
                            fY = getY().getNumber() * 0.01 * fHReference;
                        }
                        if (!bHeightIsAbsolute)
                        {
                            fH = (getHeight().isSet() ? getHeight().getNumber() *0.01 : 1.0) * fHReference;
                        }
                    }

                    if(getViewBox())
                    {
                        // SVG 1.1 defines in section 7.7 that a negative value for width or height
                        // in viewBox is an error and that 0.0 disables rendering
                        if(basegfx::fTools::more(getViewBox()->getWidth(),0.0) && basegfx::fTools::more(getViewBox()->getHeight(),0.0))
                        {
                            // create target range homing x,y, width and height as calculated above
                            const basegfx::B2DRange aTarget(fX, fY, fX + fW, fY + fH);

                            if(aTarget.equal(*getViewBox()))
                            {
                                // no mapping needed, append
                                rTarget.append(aSequence);
                            }
                            else
                            {
                                // create mapping
                                // #i122610 SVG 1.1 defines in section 5.1.2 that if the attribute perserveAspectRatio is not specified,
                                // then the effect is as if a value of 'xMidYMid meet' were specified.
                                SvgAspectRatio aRatioDefault(Align_xMidYMid,true);
                                const SvgAspectRatio& rRatio = getSvgAspectRatio().isSet()? getSvgAspectRatio() : aRatioDefault;

                                // let mapping be created from SvgAspectRatio
                                const basegfx::B2DHomMatrix aEmbeddingTransform(
                                    rRatio.createMapping(aTarget, *getViewBox()));

                                // prepare embedding in transformation
                                const drawinglayer::primitive2d::Primitive2DReference xRef(
                                    new drawinglayer::primitive2d::TransformPrimitive2D(
                                        aEmbeddingTransform,
                                        aSequence));

                                if(rRatio.isMeetOrSlice())
                                {
                                    // embed in transformation
                                    rTarget.push_back(xRef);
                                }
                                else
                                {
                                    // need to embed in MaskPrimitive2D, too
                                    const drawinglayer::primitive2d::Primitive2DReference xMask(
                                        new drawinglayer::primitive2d::MaskPrimitive2D(
                                            basegfx::B2DPolyPolygon(basegfx::utils::createPolygonFromRect(aTarget)),
                                            drawinglayer::primitive2d::Primitive2DContainer { xRef }));

                                    rTarget.push_back(xMask);
                                }
                            }
                        }
                    }
                    else // no viewBox attribute
                    {
                        // Svg defines that a negative value is an error and that 0.0 disables rendering
                        if(basegfx::fTools::more(fW, 0.0) && basegfx::fTools::more(fH, 0.0))
                        {
                            if(!basegfx::fTools::equalZero(fX) || !basegfx::fTools::equalZero(fY))
                            {
                                // embed in transform
                                const drawinglayer::primitive2d::Primitive2DReference xRef(
                                    new drawinglayer::primitive2d::TransformPrimitive2D(
                                        basegfx::utils::createTranslateB2DHomMatrix(fX, fY),
                                        aSequence));

                                aSequence = drawinglayer::primitive2d::Primitive2DContainer { xRef, };
                            }

                            // embed in MaskPrimitive2D to clip
                            const drawinglayer::primitive2d::Primitive2DReference xMask(
                                new drawinglayer::primitive2d::MaskPrimitive2D(
                                    basegfx::B2DPolyPolygon(
                                        basegfx::utils::createPolygonFromRect(
                                            basegfx::B2DRange(fX, fY, fX + fW, fY + fH))),
                                    aSequence));

                            // append
                            rTarget.push_back(xMask);
                        }
                    }
                }
                else // Outermost SVG element
                {
                    // Svg defines that a negative value is an error and that 0.0 disables rendering
                    // isPositive() not usable because it allows 0.0 in contrast to mathematical definition of 'positive'
                    const bool bWidthInvalid(getWidth().isSet() && basegfx::fTools::lessOrEqual(getWidth().getNumber(), 0.0));
                    const bool bHeightInvalid(getHeight().isSet() && basegfx::fTools::lessOrEqual(getHeight().getNumber(), 0.0));
                    if(!bWidthInvalid && !bHeightInvalid)
                    {
                        basegfx::B2DRange aSvgCanvasRange; // viewport
                        double fW = 0.0; // dummy values
                        double fH = 0.0;
                        if(getViewBox())
                        {
                            // SVG 1.1 defines in section 7.7 that a negative value for width or height
                            // in viewBox is an error and that 0.0 disables rendering
                            const double fViewBoxWidth = getViewBox()->getWidth();
                            const double fViewBoxHeight = getViewBox()->getHeight();
                            if(basegfx::fTools::more(fViewBoxWidth,0.0) && basegfx::fTools::more(fViewBoxHeight,0.0))
                            {
                                // The intrinsic aspect ratio of the svg element is given by absolute values of svg width and svg height
                                // or by the width and height of the viewBox, if svg width or svg height is relative.
                                // see SVG 1.1 section 7.12
                                bool bNeedsMapping(true);
                                const bool bWidthIsAbsolute(getWidth().isSet() && Unit_percent != getWidth().getUnit());
                                const bool bHeightIsAbsolute(getHeight().isSet() && Unit_percent != getHeight().getUnit());
                                const double fViewBoxRatio(fViewBoxWidth/fViewBoxHeight);
                                if(bWidthIsAbsolute && bHeightIsAbsolute)
                                {
                                    fW = getWidth().solveNonPercentage(*this);
                                    fH = getHeight().solveNonPercentage(*this);
                                    aSvgCanvasRange = basegfx::B2DRange(0.0, 0.0, fW, fH);
                                }
                                else if (bWidthIsAbsolute)
                                {
                                    fW = getWidth().solveNonPercentage(*this);
                                    fH = fW / fViewBoxRatio ;
                                    aSvgCanvasRange = basegfx::B2DRange(0.0, 0.0, fW, fH);
                                }
                                else if (bHeightIsAbsolute)
                                {
                                    fH = getHeight().solveNonPercentage(*this);
                                    fW = fH * fViewBoxRatio ;
                                    aSvgCanvasRange = basegfx::B2DRange(0.0, 0.0, fW, fH);
                                }
                                else
                                {
                                    // There exists no parent to resolve relative width or height.
                                    // Use child size as fallback and expand to aspect ratio given
                                    // by the viewBox. No mapping.
                                    // We get viewport >= content, therefore no clipping.
                                    bNeedsMapping = false;
                                    const basegfx::B2DRange aChildRange(
                                        aSequence.getB2DRange(
                                            drawinglayer::geometry::ViewInformation2D()));
                                    const double fChildWidth(aChildRange.getWidth());
                                    const double fChildHeight(aChildRange.getHeight());
                                    const double fLeft(aChildRange.getMinX());
                                    const double fTop(aChildRange.getMinY());
                                    if ( fChildWidth / fViewBoxWidth > fChildHeight / fViewBoxHeight )
                                    {  // expand y
                                        fW = fChildWidth;
                                        fH = fChildWidth / fViewBoxRatio;
                                    }
                                    else
                                    {  // expand x
                                        fH = fChildHeight;
                                        fW = fChildHeight * fViewBoxRatio;
                                    }
                                    aSvgCanvasRange = basegfx::B2DRange(fLeft, fTop, fLeft + fW, fTop + fH);
                                }


                                if (bNeedsMapping)
                                {
                                    // create mapping
                                    // SVG 1.1 defines in section 5.1.2 that if the attribute perserveAspectRatio is not specified,
                                    // then the effect is as if a value of 'xMidYMid meet' were specified.
                                    SvgAspectRatio aRatioDefault(Align_xMidYMid,true);
                                    const SvgAspectRatio& rRatio = getSvgAspectRatio().isSet()? getSvgAspectRatio() : aRatioDefault;

                                    basegfx::B2DHomMatrix aViewBoxMapping;
                                    aViewBoxMapping = rRatio.createMapping(aSvgCanvasRange, *getViewBox());
                                    // no need to check ratio here for slice, the outermost Svg will
                                    // be clipped anyways (see below)

                                    // scale content to viewBox definitions
                                    const drawinglayer::primitive2d::Primitive2DReference xTransform(
                                        new drawinglayer::primitive2d::TransformPrimitive2D(
                                            aViewBoxMapping,
                                            aSequence));

                                    aSequence = drawinglayer::primitive2d::Primitive2DContainer { xTransform };
                                }
                            }
                        }
                        else // no viewbox => no mapping
                        {
                            const bool bWidthIsAbsolute(getWidth().isSet() && Unit_percent != getWidth().getUnit());
                            const bool bHeightIsAbsolute(getHeight().isSet() && Unit_percent != getHeight().getUnit());
                            if (bWidthIsAbsolute && bHeightIsAbsolute)
                            {
                                fW =getWidth().solveNonPercentage(*this);
                                fH =getHeight().solveNonPercentage(*this);
                                aSvgCanvasRange = basegfx::B2DRange(0.0, 0.0, fW, fH);
                            }
                            else
                            {
                                // There exists no parent to resolve relative width or height.
                                // Use child size as fallback. We get viewport >= content, therefore no clipping.
                                const basegfx::B2DRange aChildRange(
                                     aSequence.getB2DRange(
                                         drawinglayer::geometry::ViewInformation2D()));
                                const double fChildWidth(aChildRange.getWidth());
                                const double fChildHeight(aChildRange.getHeight());
                                const double fChildLeft(aChildRange.getMinX());
                                const double fChildTop(aChildRange.getMinY());
                                fW = bWidthIsAbsolute ? getWidth().solveNonPercentage(*this) : fChildWidth;
                                fH = bHeightIsAbsolute ? getHeight().solveNonPercentage(*this) : fChildHeight;
                                const double fLeft(bWidthIsAbsolute ? 0.0 : fChildLeft);
                                const double fTop(bHeightIsAbsolute ? 0.0 : fChildTop);
                                aSvgCanvasRange = basegfx::B2DRange(fLeft, fTop, fLeft+fW, fTop+fH);
                            }

                        }

                        // to be completely correct in Svg sense it is necessary to clip
                        // the whole content to the given canvas. I choose here to do this
                        // initially despite I found various examples of Svg files out there
                        // which have no correct values for this clipping. It's correct
                        // due to the Svg spec.

                        // different from Svg we have the possibility with primitives to get
                        // a correct bounding box for the geometry. Get it for evtl. taking action
                        const basegfx::B2DRange aContentRange(
                            aSequence.getB2DRange(
                                drawinglayer::geometry::ViewInformation2D()));

                        if(aSvgCanvasRange.isInside(aContentRange))
                        {
                            // no clip needed, but an invisible HiddenGeometryPrimitive2D
                            // to allow getting the full Svg range using the primitive mechanisms.
                            // This is needed since e.g. an SdrObject using this as graphic will
                            // create a mapping transformation to exactly map the content to its
                            // real life size
                            const drawinglayer::primitive2d::Primitive2DReference xLine(
                                new drawinglayer::primitive2d::PolygonHairlinePrimitive2D(
                                    basegfx::utils::createPolygonFromRect(
                                        aSvgCanvasRange),
                                    basegfx::BColor(0.0, 0.0, 0.0)));
                            const drawinglayer::primitive2d::Primitive2DReference xHidden(
                                new drawinglayer::primitive2d::HiddenGeometryPrimitive2D(
                                    drawinglayer::primitive2d::Primitive2DContainer { xLine }));

                            aSequence.push_back(xHidden);
                        }
                        else if(aSvgCanvasRange.overlaps(aContentRange))
                        {
                            // Clip is necessary. This will make Svg images evtl. smaller
                            // than wanted from Svg (the free space which may be around it is
                            // conform to the Svg spec), but avoids an expensive and unnecessary
                            // clip. Keep the full Svg range here to get the correct mappings
                            // to objects using this. Optimizations can be done in the processors
                            const drawinglayer::primitive2d::Primitive2DReference xMask(
                                new drawinglayer::primitive2d::MaskPrimitive2D(
                                    basegfx::B2DPolyPolygon(
                                        basegfx::utils::createPolygonFromRect(
                                            aSvgCanvasRange)),
                                    aSequence));

                            aSequence = drawinglayer::primitive2d::Primitive2DContainer { xMask };
                        }
                        else
                        {
                            // not inside, no overlap. Empty Svg
                            aSequence.clear();
                        }

                        if(!aSequence.empty())
                        {
                            // embed in transform primitive to scale to 1/100th mm
                            // where 1 inch == 25.4 mm to get from Svg coordinates (px) to
                            // drawinglayer coordinates
                            const double fScaleTo100thmm(25.4 * 100.0 / F_SVG_PIXEL_PER_INCH);
                            const basegfx::B2DHomMatrix aTransform(
                                basegfx::utils::createScaleB2DHomMatrix(
                                    fScaleTo100thmm,
                                    fScaleTo100thmm));

                            const drawinglayer::primitive2d::Primitive2DReference xTransform(
                                new drawinglayer::primitive2d::TransformPrimitive2D(
                                    aTransform,
                                    aSequence));

                            aSequence = drawinglayer::primitive2d::Primitive2DContainer { xTransform };

                            // append to result
                            rTarget.append(aSequence);
                        }
                    }
                }
            }
        }

        const basegfx::B2DRange SvgSvgNode::getCurrentViewPort() const
        {
            if(getViewBox())
            {
                return *(getViewBox());
            }
            else // viewport should be given by x, y, width, and height
            {
                // Extract known viewport data
                // bXXXIsAbsolute tracks whether relative values could be resolved to absolute values
                if (getParent())
                    {
                    // If width or height is not provided, the default 100% is used, see SVG 1.1 section 5.1.2
                    // value 0.0 here is only to initialize variable
                    bool bWidthIsAbsolute(getWidth().isSet() && Unit_percent != getWidth().getUnit());
                    double fW( bWidthIsAbsolute ? getWidth().solveNonPercentage(*this) : 0.0);
                    bool bHeightIsAbsolute(getHeight().isSet() && Unit_percent != getHeight().getUnit());
                    double fH( bHeightIsAbsolute ? getHeight().solveNonPercentage(*this) : 0.0);

                    // If x or y not provided, then default 0.0 is used, see SVG 1.1 Section 5.1.2
                    bool bXIsAbsolute((getX().isSet() && Unit_percent != getX().getUnit()) || !getX().isSet());
                    double fX( bXIsAbsolute && getX().isSet() ? getX().solveNonPercentage(*this) : 0.0);

                    bool bYIsAbsolute((getY().isSet() && Unit_percent != getY().getUnit()) || !getY().isSet());
                    double fY( bYIsAbsolute && getY().isSet() ? getY().solveNonPercentage(*this) : 0.0);

                    if (bXIsAbsolute && bYIsAbsolute && bWidthIsAbsolute && bHeightIsAbsolute)
                    {
                        return basegfx::B2DRange(fX, fY, fX+fW, fY+fH);
                    }
                    else // try to resolve relative values
                    {
                        if (!bXIsAbsolute || !bWidthIsAbsolute)
                        {
                            // get width of enclosing svg and resolve percentage in x and width
                            double fWReference(0.0);
                            bool bHasFoundWidth(false);
                            seekReferenceWidth(fWReference, bHasFoundWidth);
                            // referenced values are already in 'user unit'
                            if (!bXIsAbsolute && bHasFoundWidth)
                            {
                                fX = getX().getNumber() * 0.01 * fWReference;
                                bXIsAbsolute = true;
                            }
                            if (!bWidthIsAbsolute && bHasFoundWidth)
                            {
                                fW = (getWidth().isSet() ? getWidth().getNumber() *0.01 : 1.0) * fWReference;
                                bWidthIsAbsolute = true;
                            }
                        }
                        if (!bYIsAbsolute || !bHeightIsAbsolute)
                        {
                            // get height of enclosing svg and resolve percentage in y and height
                            double fHReference(0.0);
                            bool bHasFoundHeight(false);
                            seekReferenceHeight(fHReference, bHasFoundHeight);
                            // referenced values are already in 'user unit'
                            if (!bYIsAbsolute && bHasFoundHeight)
                            {
                                fY = getY().getNumber() * 0.01 * fHReference;
                                bYIsAbsolute = true;
                            }
                            if (!bHeightIsAbsolute && bHasFoundHeight)
                            {
                                fH = (getHeight().isSet() ? getHeight().getNumber() *0.01 : 1.0) * fHReference;
                                bHeightIsAbsolute = true;
                            }
                        }

                        if (bXIsAbsolute && bYIsAbsolute && bWidthIsAbsolute && bHeightIsAbsolute)
                        {
                            return basegfx::B2DRange(fX, fY, fX+fW, fY+fH);
                        }
                        else // relative values could not be resolved, there exists no fallback
                        {
                            return SvgNode::getCurrentViewPort();
                        }
                    }
                }
                else //outermost svg
                {
                    // If width or height is not provided, the default would be 100%, see SVG 1.1 section 5.1.2
                    // But here it cannot be resolved and no fallback exists.
                    // SVG 1.1 defines in section 5.1.2 that x,y has no meaning for the outermost SVG element.
                    bool bWidthIsAbsolute(getWidth().isSet() && Unit_percent != getWidth().getUnit());
                    bool bHeightIsAbsolute(getHeight().isSet() && Unit_percent != getHeight().getUnit());
                    if (bWidthIsAbsolute && bHeightIsAbsolute)
                    {
                        double fW( getWidth().solveNonPercentage(*this) );
                        double fH( getHeight().solveNonPercentage(*this) );
                        return basegfx::B2DRange(0.0, 0.0, fW, fH);
                    }
                    else // no fallback exists
                    {
                            return SvgNode::getCurrentViewPort();
                    }
                }
// TODO: Is it possible to decompose and use the bounding box of the children, if even the
//       outermost svg has no information to resolve percentage? Is it worth, how expensive is it?

            }
        }

    } // end of namespace svgreader
} // end of namespace svgio

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */