LCOV - code coverage report
Current view: top level - svgio/source/svgreader - svgclippathnode.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 46 76 60.5 %
Date: 2014-04-11 Functions: 7 7 100.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/svgclippathnode.hxx>
      21             : #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
      22             : #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
      23             : #include <basegfx/matrix/b2dhommatrixtools.hxx>
      24             : #include <drawinglayer/geometry/viewinformation2d.hxx>
      25             : #include <drawinglayer/processor2d/contourextractor2d.hxx>
      26             : #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
      27             : #include <basegfx/polygon/b2dpolygontools.hxx>
      28             : 
      29             : 
      30             : 
      31             : namespace svgio
      32             : {
      33             :     namespace svgreader
      34             :     {
      35          93 :         SvgClipPathNode::SvgClipPathNode(
      36             :             SvgDocument& rDocument,
      37             :             SvgNode* pParent)
      38             :         :   SvgNode(SVGTokenClipPathNode, rDocument, pParent),
      39             :             maSvgStyleAttributes(*this),
      40             :             mpaTransform(0),
      41          93 :             maClipPathUnits(userSpaceOnUse)
      42             :         {
      43          93 :         }
      44             : 
      45         279 :         SvgClipPathNode::~SvgClipPathNode()
      46             :         {
      47          93 :             if(mpaTransform) delete mpaTransform;
      48         186 :         }
      49             : 
      50         501 :         const SvgStyleAttributes* SvgClipPathNode::getSvgStyleAttributes() const
      51             :         {
      52         501 :             return &maSvgStyleAttributes;
      53             :         }
      54             : 
      55          93 :         void SvgClipPathNode::parseAttribute(const OUString& rTokenName, SVGToken aSVGToken, const OUString& aContent)
      56             :         {
      57             :             // call parent
      58          93 :             SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
      59             : 
      60             :             // read style attributes
      61          93 :             maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent);
      62             : 
      63             :             // parse own
      64          93 :             switch(aSVGToken)
      65             :             {
      66             :                 case SVGTokenStyle:
      67             :                 {
      68           0 :                     maSvgStyleAttributes.readStyle(aContent);
      69           0 :                     break;
      70             :                 }
      71             :                 case SVGTokenTransform:
      72             :                 {
      73           0 :                     const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
      74             : 
      75           0 :                     if(!aMatrix.isIdentity())
      76             :                     {
      77           0 :                         setTransform(&aMatrix);
      78             :                     }
      79           0 :                     break;
      80             :                 }
      81             :                 case SVGTokenClipPathUnits:
      82             :                 {
      83           0 :                     if(!aContent.isEmpty())
      84             :                     {
      85           0 :                         if(aContent.match(commonStrings::aStrUserSpaceOnUse, 0))
      86             :                         {
      87           0 :                             setClipPathUnits(userSpaceOnUse);
      88             :                         }
      89           0 :                         else if(aContent.match(commonStrings::aStrObjectBoundingBox, 0))
      90             :                         {
      91           0 :                             setClipPathUnits(objectBoundingBox);
      92             :                         }
      93             :                     }
      94           0 :                     break;
      95             :                 }
      96             :                 default:
      97             :                 {
      98          93 :                     break;
      99             :                 }
     100             :             }
     101          93 :         }
     102             : 
     103         249 :         void SvgClipPathNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool bReferenced) const
     104             :         {
     105         249 :             drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
     106             : 
     107             :             // decompose children
     108         249 :             SvgNode::decomposeSvgNode(aNewTarget, bReferenced);
     109             : 
     110         249 :             if(aNewTarget.hasElements())
     111             :             {
     112         156 :                 if(getTransform())
     113             :                 {
     114             :                     // create embedding group element with transformation
     115             :                     const drawinglayer::primitive2d::Primitive2DReference xRef(
     116             :                         new drawinglayer::primitive2d::TransformPrimitive2D(
     117             :                             *getTransform(),
     118           0 :                             aNewTarget));
     119             : 
     120           0 :                     drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget, xRef);
     121             :                 }
     122             :                 else
     123             :                 {
     124             :                     // append to current target
     125         156 :                     drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget, aNewTarget);
     126             :                 }
     127         249 :             }
     128         249 :         }
     129             : 
     130         156 :         void SvgClipPathNode::apply(drawinglayer::primitive2d::Primitive2DSequence& rContent) const
     131             :         {
     132         156 :             if(rContent.hasElements() && Display_none != getDisplay())
     133             :             {
     134         156 :                 const drawinglayer::geometry::ViewInformation2D aViewInformation2D;
     135         312 :                 drawinglayer::primitive2d::Primitive2DSequence aClipTarget;
     136         312 :                 basegfx::B2DPolyPolygon aClipPolyPolygon;
     137             : 
     138             :                 // get clipPath definition as primitives
     139         156 :                 decomposeSvgNode(aClipTarget, true);
     140             : 
     141         156 :                 if(aClipTarget.hasElements())
     142             :                 {
     143             :                     // extract filled plygons as base for a mask PolyPolygon
     144         156 :                     drawinglayer::processor2d::ContourExtractor2D aExtractor(aViewInformation2D, true);
     145             : 
     146         156 :                     aExtractor.process(aClipTarget);
     147             : 
     148         156 :                     const basegfx::B2DPolyPolygonVector& rResult(aExtractor.getExtractedContour());
     149         156 :                     const sal_uInt32 nSize(rResult.size());
     150             : 
     151         156 :                     if(nSize > 1)
     152             :                     {
     153             :                         // merge to single clipPolyPolygon
     154           0 :                         aClipPolyPolygon = basegfx::tools::mergeToSinglePolyPolygon(rResult);
     155             :                     }
     156             :                     else
     157             :                     {
     158         156 :                         aClipPolyPolygon = rResult[0];
     159         156 :                     }
     160             :                 }
     161             : 
     162         156 :                 if(aClipPolyPolygon.count())
     163             :                 {
     164         156 :                     if(objectBoundingBox == getClipPathUnits())
     165             :                     {
     166             :                         // clip is object-relative, transform using content transformation
     167             :                         const basegfx::B2DRange aContentRange(
     168             :                             drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence(
     169             :                                 rContent,
     170           0 :                                 aViewInformation2D));
     171             : 
     172             :                         aClipPolyPolygon.transform(
     173             :                             basegfx::tools::createScaleTranslateB2DHomMatrix(
     174             :                                 aContentRange.getRange(),
     175           0 :                                 aContentRange.getMinimum()));
     176             :                     }
     177             : 
     178             :                     // #i124313# try to avoid creating an embedding to a MaskPrimitive2D if
     179             :                     // possible; MaskPrimitive2D processing is potentially expensive
     180         156 :                     bool bCreateEmbedding(true);
     181         156 :                     bool bAddContent(true);
     182             : 
     183         156 :                     if(basegfx::tools::isRectangle(aClipPolyPolygon))
     184             :                     {
     185             :                         // ClipRegion is a rectangle, thus it is not expensive to tell
     186             :                         // if the content is completely inside or outside of it; get ranges
     187           0 :                         const basegfx::B2DRange aClipRange(aClipPolyPolygon.getB2DRange());
     188             :                         const basegfx::B2DRange aContentRange(
     189             :                             drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence(
     190             :                                 rContent,
     191           0 :                                 aViewInformation2D));
     192             : 
     193           0 :                         if(aClipRange.isInside(aContentRange))
     194             :                         {
     195             :                             // completely contained, no need to clip at all, so no need for embedding
     196           0 :                             bCreateEmbedding = false;
     197             :                         }
     198           0 :                         else if(aClipRange.overlaps(aContentRange))
     199             :                         {
     200             :                             // overlap; embedding needed. ClipRegion can be minimized by using
     201             :                             // the intersection of the ClipRange and the ContentRange. Minimizing
     202             :                             // the ClipRegion potentially enhances further processing since
     203             :                             // usually clip operations are expensive.
     204           0 :                             basegfx::B2DRange aCommonRange(aContentRange);
     205             : 
     206           0 :                             aCommonRange.intersect(aClipRange);
     207           0 :                             aClipPolyPolygon = basegfx::B2DPolyPolygon(basegfx::tools::createPolygonFromRect(aCommonRange));
     208             :                         }
     209             :                         else
     210             :                         {
     211             :                             // not inside and no overlap -> completely outside
     212             :                             // no need for embedding, no need for content at all
     213           0 :                             bCreateEmbedding = false;
     214           0 :                             bAddContent = false;
     215             :                         }
     216             :                     }
     217             :                     else
     218             :                     {
     219             :                         // ClipRegion is not a simple rectangle, it would be possible but expensive to
     220             :                         // tell if the content needs clipping or not. It is also dependent of
     221             :                         // the content's decomposition. To do this, a processor would be needed that
     222             :                         // is capable if processing the given sequence of primitives and decide
     223             :                         // if all is inside or all is outside. Such a ClipProcessor could be written,
     224             :                         // but for now just create the embedding
     225             :                     }
     226             : 
     227         156 :                     if(bCreateEmbedding)
     228             :                     {
     229             :                         // redefine target. Use MaskPrimitive2D with created clip
     230             :                         // geometry. Using the automatically set mbIsClipPathContent at
     231             :                         // SvgStyleAttributes the clip definition is without fill, stroke,
     232             :                         // and strokeWidth and forced to black
     233             :                         const drawinglayer::primitive2d::Primitive2DReference xEmbedTransparence(
     234             :                             new drawinglayer::primitive2d::MaskPrimitive2D(
     235             :                                 aClipPolyPolygon,
     236         156 :                                 rContent));
     237             : 
     238         156 :                         rContent = drawinglayer::primitive2d::Primitive2DSequence(&xEmbedTransparence, 1);
     239             :                     }
     240             :                     else
     241             :                     {
     242           0 :                         if(!bAddContent)
     243             :                         {
     244           0 :                             rContent.realloc(0);
     245             :                         }
     246             :                     }
     247             :                 }
     248             :                 else
     249             :                 {
     250             :                     // An empty clipping path will completely clip away the element that had
     251             :                     // the ‘clip-path’ property applied. (Svg spec)
     252           0 :                     rContent.realloc(0);
     253         156 :                 }
     254             :             }
     255         156 :         }
     256             : 
     257             :     } // end of namespace svgreader
     258             : } // end of namespace svgio
     259             : 
     260             : 
     261             : // eof
     262             : 
     263             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10