LCOV - code coverage report
Current view: top level - libreoffice/svgio/source/svgreader - svgsvgnode.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 112 0.0 %
Date: 2012-12-27 Functions: 0 7 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 <svgio/svgreader/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             : 
      30             : //////////////////////////////////////////////////////////////////////////////
      31             : 
      32             : namespace svgio
      33             : {
      34             :     namespace svgreader
      35             :     {
      36           0 :         SvgSvgNode::SvgSvgNode(
      37             :             SvgDocument& rDocument,
      38             :             SvgNode* pParent)
      39             :         :   SvgNode(SVGTokenSvg, rDocument, pParent),
      40             :             maSvgStyleAttributes(*this),
      41             :             mpViewBox(0),
      42             :             maSvgAspectRatio(),
      43             :             maX(),
      44             :             maY(),
      45             :             maWidth(),
      46             :             maHeight(),
      47           0 :             maVersion()
      48             :         {
      49           0 :             if(!getParent())
      50             :             {
      51             :                 // initial fill is black
      52           0 :                 maSvgStyleAttributes.setFill(SvgPaint(basegfx::BColor(0.0, 0.0, 0.0), true, true));
      53             :             }
      54           0 :         }
      55             : 
      56           0 :         SvgSvgNode::~SvgSvgNode()
      57             :         {
      58           0 :             if(mpViewBox) delete mpViewBox;
      59           0 :         }
      60             : 
      61           0 :         const SvgStyleAttributes* SvgSvgNode::getSvgStyleAttributes() const
      62             :         {
      63           0 :             return &maSvgStyleAttributes;
      64             :         }
      65             : 
      66           0 :         void SvgSvgNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent)
      67             :         {
      68             :             // call parent
      69           0 :             SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
      70             : 
      71             :             // read style attributes
      72           0 :             maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent);
      73             : 
      74             :             // parse own
      75           0 :             switch(aSVGToken)
      76             :             {
      77             :                 case SVGTokenStyle:
      78             :                 {
      79           0 :                     maSvgStyleAttributes.readStyle(aContent);
      80           0 :                     break;
      81             :                 }
      82             :                 case SVGTokenViewBox:
      83             :                 {
      84           0 :                     const basegfx::B2DRange aRange(readViewBox(aContent, *this));
      85             : 
      86           0 :                     if(!aRange.isEmpty())
      87             :                     {
      88           0 :                         setViewBox(&aRange);
      89             :                     }
      90             :                     break;
      91             :                 }
      92             :                 case SVGTokenPreserveAspectRatio:
      93             :                 {
      94           0 :                     setSvgAspectRatio(readSvgAspectRatio(aContent));
      95           0 :                     break;
      96             :                 }
      97             :                 case SVGTokenX:
      98             :                 {
      99           0 :                     SvgNumber aNum;
     100             : 
     101           0 :                     if(readSingleNumber(aContent, aNum))
     102             :                     {
     103           0 :                         setX(aNum);
     104             :                     }
     105             :                     break;
     106             :                 }
     107             :                 case SVGTokenY:
     108             :                 {
     109           0 :                     SvgNumber aNum;
     110             : 
     111           0 :                     if(readSingleNumber(aContent, aNum))
     112             :                     {
     113           0 :                         setY(aNum);
     114             :                     }
     115             :                     break;
     116             :                 }
     117             :                 case SVGTokenWidth:
     118             :                 {
     119           0 :                     SvgNumber aNum;
     120             : 
     121           0 :                     if(readSingleNumber(aContent, aNum))
     122             :                     {
     123           0 :                         if(aNum.isPositive())
     124             :                         {
     125           0 :                             setWidth(aNum);
     126             :                         }
     127             :                     }
     128             :                     break;
     129             :                 }
     130             :                 case SVGTokenHeight:
     131             :                 {
     132           0 :                     SvgNumber aNum;
     133             : 
     134           0 :                     if(readSingleNumber(aContent, aNum))
     135             :                     {
     136           0 :                         if(aNum.isPositive())
     137             :                         {
     138           0 :                             setHeight(aNum);
     139             :                         }
     140             :                     }
     141             :                     break;
     142             :                 }
     143             :                 case SVGTokenVersion:
     144             :                 {
     145           0 :                     SvgNumber aNum;
     146             : 
     147           0 :                     if(readSingleNumber(aContent, aNum))
     148             :                     {
     149           0 :                         setVersion(aNum);
     150             :                     }
     151             :                     break;
     152             :                 }
     153             :                 default:
     154             :                 {
     155           0 :                     break;
     156             :                 }
     157             :             }
     158           0 :         }
     159             : 
     160           0 :         void SvgSvgNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool bReferenced) const
     161             :         {
     162           0 :             drawinglayer::primitive2d::Primitive2DSequence aSequence;
     163             : 
     164             :             // decompose childs
     165           0 :             SvgNode::decomposeSvgNode(aSequence, bReferenced);
     166             : 
     167           0 :             if(aSequence.hasElements())
     168             :             {
     169           0 :                 if(getParent())
     170             :                 {
     171           0 :                     if(getViewBox())
     172             :                     {
     173             :                         // Svg defines that with no width or no height the viewBox content is empty,
     174             :                         // so both need to exist
     175           0 :                         if(!basegfx::fTools::equalZero(getViewBox()->getWidth()) && !basegfx::fTools::equalZero(getViewBox()->getHeight()))
     176             :                         {
     177             :                             // create target range homing x,y, width and height as given
     178           0 :                             const double fX(getX().isSet() ? getX().solve(*this, xcoordinate) : 0.0);
     179           0 :                             const double fY(getY().isSet() ? getY().solve(*this, ycoordinate) : 0.0);
     180           0 :                             const double fW(getWidth().isSet() ? getWidth().solve(*this, xcoordinate) : getViewBox()->getWidth());
     181           0 :                             const double fH(getHeight().isSet() ? getHeight().solve(*this, ycoordinate) : getViewBox()->getHeight());
     182           0 :                             const basegfx::B2DRange aTarget(fX, fY, fX + fW, fY + fH);
     183             : 
     184           0 :                             if(aTarget.equal(*getViewBox()))
     185             :                             {
     186             :                                 // no mapping needed, append
     187           0 :                                 drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget, aSequence);
     188             :                             }
     189             :                             else
     190             :                             {
     191             :                                 // create mapping
     192           0 :                                 const SvgAspectRatio& rRatio = getSvgAspectRatio();
     193             : 
     194           0 :                                 if(rRatio.isSet())
     195             :                                 {
     196             :                                     // let mapping be created from SvgAspectRatio
     197             :                                     const basegfx::B2DHomMatrix aEmbeddingTransform(
     198           0 :                                         rRatio.createMapping(aTarget, *getViewBox()));
     199             : 
     200             :                                     // prepare embedding in transformation
     201             :                                     const drawinglayer::primitive2d::Primitive2DReference xRef(
     202             :                                         new drawinglayer::primitive2d::TransformPrimitive2D(
     203             :                                             aEmbeddingTransform,
     204           0 :                                             aSequence));
     205             : 
     206           0 :                                     if(rRatio.isMeetOrSlice())
     207             :                                     {
     208             :                                         // embed in transformation
     209           0 :                                         drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget, xRef);
     210             :                                     }
     211             :                                     else
     212             :                                     {
     213             :                                         // need to embed in MaskPrimitive2D, too
     214             :                                         const drawinglayer::primitive2d::Primitive2DReference xMask(
     215             :                                             new drawinglayer::primitive2d::MaskPrimitive2D(
     216             :                                                 basegfx::B2DPolyPolygon(basegfx::tools::createPolygonFromRect(aTarget)),
     217           0 :                                                 drawinglayer::primitive2d::Primitive2DSequence(&xRef, 1)));
     218             : 
     219           0 :                                         drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget, xMask);
     220           0 :                                     }
     221             :                                 }
     222             :                                 else
     223             :                                 {
     224             :                                     // choose default mapping
     225             :                                     const basegfx::B2DHomMatrix aEmbeddingTransform(
     226             :                                         rRatio.createLinearMapping(
     227           0 :                                             aTarget, *getViewBox()));
     228             : 
     229             :                                     // embed in transformation
     230             :                                     const drawinglayer::primitive2d::Primitive2DReference xTransform(
     231             :                                         new drawinglayer::primitive2d::TransformPrimitive2D(
     232             :                                             aEmbeddingTransform,
     233           0 :                                             aSequence));
     234             : 
     235           0 :                                     drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget, xTransform);
     236             :                                 }
     237             :                             }
     238             :                         }
     239             :                     }
     240             :                     else
     241             :                     {
     242             :                         // check if we have a size
     243           0 :                         const double fW(getWidth().isSet() ? getWidth().solve(*this, xcoordinate) : 0.0);
     244           0 :                         const double fH(getHeight().isSet() ? getHeight().solve(*this, ycoordinate) : 0.0);
     245             : 
     246             :                         // Svg defines that a negative value is an error and that 0.0 disables rendering
     247           0 :                         if(basegfx::fTools::more(fW, 0.0) && basegfx::fTools::more(fH, 0.0))
     248             :                         {
     249             :                             // check if we have a x,y position
     250           0 :                             const double fX(getX().isSet() ? getX().solve(*this, xcoordinate) : 0.0);
     251           0 :                             const double fY(getY().isSet() ? getY().solve(*this, ycoordinate) : 0.0);
     252             : 
     253           0 :                             if(!basegfx::fTools::equalZero(fX) || !basegfx::fTools::equalZero(fY))
     254             :                             {
     255             :                                 // embed in transform
     256             :                                 const drawinglayer::primitive2d::Primitive2DReference xRef(
     257             :                                     new drawinglayer::primitive2d::TransformPrimitive2D(
     258             :                                         basegfx::tools::createTranslateB2DHomMatrix(fX, fY),
     259           0 :                                         aSequence));
     260             : 
     261           0 :                                 aSequence = drawinglayer::primitive2d::Primitive2DSequence(&xRef, 1);
     262             :                             }
     263             : 
     264             :                             // embed in MaskPrimitive2D to clip
     265             :                             const drawinglayer::primitive2d::Primitive2DReference xMask(
     266             :                                 new drawinglayer::primitive2d::MaskPrimitive2D(
     267             :                                     basegfx::B2DPolyPolygon(
     268             :                                         basegfx::tools::createPolygonFromRect(
     269             :                                             basegfx::B2DRange(fX, fY, fX + fW, fY + fH))),
     270           0 :                                     aSequence));
     271             : 
     272             :                             // append
     273           0 :                             drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget, xMask);
     274             :                         }
     275             :                     }
     276             :                 }
     277             :                 else
     278             :                 {
     279             :                     // Outermost SVG element; create target range homing width and height as given.
     280             :                     // SVG defines that x,y has no meanig for the outermost SVG element. Use a fallback
     281             :                     // width and height of din A 4 (21 x 29,7 cm)
     282           0 :                     double fW(getWidth().isSet() ? getWidth().solve(*this, xcoordinate) : (210.0 * 3.543307));
     283           0 :                     double fH(getHeight().isSet() ? getHeight().solve(*this, ycoordinate) : (297.0 * 3.543307));
     284             : 
     285             :                     // Svg defines that a negative value is an error and that 0.0 disables rendering
     286           0 :                     if(basegfx::fTools::more(fW, 0.0) && basegfx::fTools::more(fH, 0.0))
     287             :                     {
     288           0 :                         const basegfx::B2DRange aSvgCanvasRange(0.0, 0.0, fW, fH);
     289             : 
     290           0 :                         if(getViewBox())
     291             :                         {
     292           0 :                             if(!basegfx::fTools::equalZero(getViewBox()->getWidth()) && !basegfx::fTools::equalZero(getViewBox()->getHeight()))
     293             :                             {
     294             :                                 // create mapping
     295           0 :                                 const SvgAspectRatio& rRatio = getSvgAspectRatio();
     296           0 :                                 basegfx::B2DHomMatrix aViewBoxMapping;
     297             : 
     298           0 :                                 if(rRatio.isSet())
     299             :                                 {
     300             :                                     // let mapping be created from SvgAspectRatio
     301           0 :                                     aViewBoxMapping = rRatio.createMapping(aSvgCanvasRange, *getViewBox());
     302             : 
     303             :                                     // no need to check ratio here for slice, the outermost Svg will
     304             :                                     // be clipped anyways (see below)
     305             :                                 }
     306             :                                 else
     307             :                                 {
     308             :                                     // choose default mapping
     309           0 :                                     aViewBoxMapping = rRatio.createLinearMapping(aSvgCanvasRange, *getViewBox());
     310             :                                 }
     311             : 
     312             :                                 // scale content to viewBox definitions
     313             :                                 const drawinglayer::primitive2d::Primitive2DReference xTransform(
     314             :                                     new drawinglayer::primitive2d::TransformPrimitive2D(
     315             :                                         aViewBoxMapping,
     316           0 :                                         aSequence));
     317             : 
     318           0 :                                 aSequence = drawinglayer::primitive2d::Primitive2DSequence(&xTransform, 1);
     319             :                             }
     320             :                         }
     321             : 
     322             :                         // to be completely correct in Svg sense it is necessary to clip
     323             :                         // the whole content to the given canvas. I choose here to do this
     324             :                         // initially despite I found various examples of Svg files out there
     325             :                         // which have no correct values for this clipping. It's correct
     326             :                         // due to the Svg spec.
     327           0 :                         bool bDoCorrectCanvasClipping(true);
     328             : 
     329           0 :                         if(bDoCorrectCanvasClipping)
     330             :                         {
     331             :                             // different from Svg we have the possibility with primitives to get
     332             :                             // a correct bounding box for the geometry. Get it for evtl. taking action
     333             :                             const basegfx::B2DRange aContentRange(
     334             :                                 drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence(
     335             :                                     aSequence,
     336           0 :                                     drawinglayer::geometry::ViewInformation2D()));
     337             : 
     338           0 :                             if(aSvgCanvasRange.isInside(aContentRange))
     339             :                             {
     340             :                                 // no clip needed, but an invisible HiddenGeometryPrimitive2D
     341             :                                 // to allow getting the full Svg range using the primitive mechanisms.
     342             :                                 // This is needed since e.g. an SdrObject using this as graphic will
     343             :                                 // create a mapping transformation to exactly map the content to it's
     344             :                                 // real life size
     345             :                                 const drawinglayer::primitive2d::Primitive2DReference xLine(
     346             :                                     new drawinglayer::primitive2d::PolygonHairlinePrimitive2D(
     347             :                                         basegfx::tools::createPolygonFromRect(
     348             :                                             aSvgCanvasRange),
     349           0 :                                         basegfx::BColor(0.0, 0.0, 0.0)));
     350             :                                 const drawinglayer::primitive2d::Primitive2DReference xHidden(
     351             :                                     new drawinglayer::primitive2d::HiddenGeometryPrimitive2D(
     352           0 :                                         drawinglayer::primitive2d::Primitive2DSequence(&xLine, 1)));
     353             : 
     354           0 :                                 drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(aSequence, xHidden);
     355             :                             }
     356           0 :                             else if(aSvgCanvasRange.overlaps(aContentRange))
     357             :                             {
     358             :                                 // Clip is necessary. This will make Svg images evtl. smaller
     359             :                                 // than wanted from Svg (the free space which may be around it is
     360             :                                 // conform to the Svg spec), but avoids an expensive and unneccessary
     361             :                                 // clip. Keep the full Svg range here to get the correct mappings
     362             :                                 // to objects using this. Optimizations can be done in the processors
     363             :                                 const drawinglayer::primitive2d::Primitive2DReference xMask(
     364             :                                     new drawinglayer::primitive2d::MaskPrimitive2D(
     365             :                                         basegfx::B2DPolyPolygon(
     366             :                                             basegfx::tools::createPolygonFromRect(
     367             :                                                 aSvgCanvasRange)),
     368           0 :                                         aSequence));
     369             : 
     370           0 :                                 aSequence = drawinglayer::primitive2d::Primitive2DSequence(&xMask, 1);
     371             :                             }
     372             :                             else
     373             :                             {
     374             :                                 // not inside, no overlap. Empty Svg
     375           0 :                                 aSequence.realloc(0);
     376             :                             }
     377             :                         }
     378             : 
     379           0 :                         if(aSequence.hasElements())
     380             :                         {
     381             :                             // embed in transform primitive to scale to 1/100th mm
     382             :                             // where 1 mm == 3.543307 px to get from Svg coordinates to
     383             :                             // drawinglayer ones
     384           0 :                             const double fScaleTo100thmm(100.0 / 3.543307);
     385             :                             const basegfx::B2DHomMatrix aTransform(
     386             :                                 basegfx::tools::createScaleB2DHomMatrix(
     387             :                                     fScaleTo100thmm,
     388           0 :                                     fScaleTo100thmm));
     389             : 
     390             :                             const drawinglayer::primitive2d::Primitive2DReference xTransform(
     391             :                                 new drawinglayer::primitive2d::TransformPrimitive2D(
     392             :                                     aTransform,
     393           0 :                                     aSequence));
     394             : 
     395           0 :                             aSequence = drawinglayer::primitive2d::Primitive2DSequence(&xTransform, 1);
     396             : 
     397             :                             // append to result
     398           0 :                             drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget, aSequence);
     399             :                         }
     400             :                     }
     401             :                 }
     402           0 :             }
     403           0 :         }
     404             : 
     405           0 :         const basegfx::B2DRange* SvgSvgNode::getCurrentViewPort() const
     406             :         {
     407           0 :             if(getViewBox())
     408             :             {
     409           0 :                 return getViewBox();
     410             :             }
     411             :             else
     412             :             {
     413           0 :                 return SvgNode::getCurrentViewPort();
     414             :             }
     415             :         }
     416             : 
     417             :     } // end of namespace svgreader
     418             : } // end of namespace svgio
     419             : 
     420             : //////////////////////////////////////////////////////////////////////////////
     421             : // eof
     422             : 
     423             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10