LCOV - code coverage report
Current view: top level - svgio/source/svgreader - svgclippathnode.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 48 78 61.5 %
Date: 2014-11-03 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             : namespace svgio
      30             : {
      31             :     namespace svgreader
      32             :     {
      33          93 :         SvgClipPathNode::SvgClipPathNode(
      34             :             SvgDocument& rDocument,
      35             :             SvgNode* pParent)
      36             :         :   SvgNode(SVGTokenClipPathNode, rDocument, pParent),
      37             :             maSvgStyleAttributes(*this),
      38             :             mpaTransform(0),
      39          93 :             maClipPathUnits(userSpaceOnUse)
      40             :         {
      41          93 :         }
      42             : 
      43         279 :         SvgClipPathNode::~SvgClipPathNode()
      44             :         {
      45          93 :             if(mpaTransform) delete mpaTransform;
      46         186 :         }
      47             : 
      48         687 :         const SvgStyleAttributes* SvgClipPathNode::getSvgStyleAttributes() const
      49             :         {
      50         687 :             return &maSvgStyleAttributes;
      51             :         }
      52             : 
      53          93 :         void SvgClipPathNode::parseAttribute(const OUString& rTokenName, SVGToken aSVGToken, const OUString& aContent)
      54             :         {
      55             :             // call parent
      56          93 :             SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
      57             : 
      58             :             // read style attributes
      59          93 :             maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent, false);
      60             : 
      61             :             // parse own
      62          93 :             switch(aSVGToken)
      63             :             {
      64             :                 case SVGTokenStyle:
      65             :                 {
      66           0 :                     readLocalCssStyle(aContent);
      67           0 :                     break;
      68             :                 }
      69             :                 case SVGTokenTransform:
      70             :                 {
      71           0 :                     const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
      72             : 
      73           0 :                     if(!aMatrix.isIdentity())
      74             :                     {
      75           0 :                         setTransform(&aMatrix);
      76             :                     }
      77           0 :                     break;
      78             :                 }
      79             :                 case SVGTokenClipPathUnits:
      80             :                 {
      81           0 :                     if(!aContent.isEmpty())
      82             :                     {
      83           0 :                         if(aContent.match(commonStrings::aStrUserSpaceOnUse, 0))
      84             :                         {
      85           0 :                             setClipPathUnits(userSpaceOnUse);
      86             :                         }
      87           0 :                         else if(aContent.match(commonStrings::aStrObjectBoundingBox, 0))
      88             :                         {
      89           0 :                             setClipPathUnits(objectBoundingBox);
      90             :                         }
      91             :                     }
      92           0 :                     break;
      93             :                 }
      94             :                 default:
      95             :                 {
      96          93 :                     break;
      97             :                 }
      98             :             }
      99          93 :         }
     100             : 
     101         249 :         void SvgClipPathNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool bReferenced) const
     102             :         {
     103         249 :             drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
     104             : 
     105             :             // decompose children
     106         249 :             SvgNode::decomposeSvgNode(aNewTarget, bReferenced);
     107             : 
     108         249 :             if(aNewTarget.hasElements())
     109             :             {
     110         156 :                 if(getTransform())
     111             :                 {
     112             :                     // create embedding group element with transformation
     113             :                     const drawinglayer::primitive2d::Primitive2DReference xRef(
     114             :                         new drawinglayer::primitive2d::TransformPrimitive2D(
     115             :                             *getTransform(),
     116           0 :                             aNewTarget));
     117             : 
     118           0 :                     drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget, xRef);
     119             :                 }
     120             :                 else
     121             :                 {
     122             :                     // append to current target
     123         156 :                     drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget, aNewTarget);
     124             :                 }
     125         249 :             }
     126         249 :         }
     127             : 
     128         156 :         void SvgClipPathNode::apply(
     129             :             drawinglayer::primitive2d::Primitive2DSequence& rContent,
     130             :             const basegfx::B2DHomMatrix* pTransform) 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 polygons 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             :                     else // userSpaceOnUse
     178             :                     {
     179             :                         // #i124852#
     180         156 :                         if(pTransform)
     181             :                         {
     182          25 :                             aClipPolyPolygon.transform(*pTransform);
     183             :                         }
     184             :                     }
     185             : 
     186             :                     // #i124313# try to avoid creating an embedding to a MaskPrimitive2D if
     187             :                     // possible; MaskPrimitive2D processing is potentially expensive
     188         156 :                     bool bCreateEmbedding(true);
     189         156 :                     bool bAddContent(true);
     190             : 
     191         156 :                     if(basegfx::tools::isRectangle(aClipPolyPolygon))
     192             :                     {
     193             :                         // ClipRegion is a rectangle, thus it is not expensive to tell
     194             :                         // if the content is completely inside or outside of it; get ranges
     195           0 :                         const basegfx::B2DRange aClipRange(aClipPolyPolygon.getB2DRange());
     196             :                         const basegfx::B2DRange aContentRange(
     197             :                             drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence(
     198             :                                 rContent,
     199           0 :                                 aViewInformation2D));
     200             : 
     201           0 :                         if(aClipRange.isInside(aContentRange))
     202             :                         {
     203             :                             // completely contained, no need to clip at all, so no need for embedding
     204           0 :                             bCreateEmbedding = false;
     205             :                         }
     206           0 :                         else if(aClipRange.overlaps(aContentRange))
     207             :                         {
     208             :                             // overlap; embedding needed. ClipRegion can be minimized by using
     209             :                             // the intersection of the ClipRange and the ContentRange. Minimizing
     210             :                             // the ClipRegion potentially enhances further processing since
     211             :                             // usually clip operations are expensive.
     212           0 :                             basegfx::B2DRange aCommonRange(aContentRange);
     213             : 
     214           0 :                             aCommonRange.intersect(aClipRange);
     215           0 :                             aClipPolyPolygon = basegfx::B2DPolyPolygon(basegfx::tools::createPolygonFromRect(aCommonRange));
     216             :                         }
     217             :                         else
     218             :                         {
     219             :                             // not inside and no overlap -> completely outside
     220             :                             // no need for embedding, no need for content at all
     221           0 :                             bCreateEmbedding = false;
     222           0 :                             bAddContent = false;
     223             :                         }
     224             :                     }
     225             :                     else
     226             :                     {
     227             :                         // ClipRegion is not a simple rectangle, it would be possible but expensive to
     228             :                         // tell if the content needs clipping or not. It is also dependent of
     229             :                         // the content's decomposition. To do this, a processor would be needed that
     230             :                         // is capable if processing the given sequence of primitives and decide
     231             :                         // if all is inside or all is outside. Such a ClipProcessor could be written,
     232             :                         // but for now just create the embedding
     233             :                     }
     234             : 
     235         156 :                     if(bCreateEmbedding)
     236             :                     {
     237             :                         // redefine target. Use MaskPrimitive2D with created clip
     238             :                         // geometry. Using the automatically set mbIsClipPathContent at
     239             :                         // SvgStyleAttributes the clip definition is without fill, stroke,
     240             :                         // and strokeWidth and forced to black
     241             :                         const drawinglayer::primitive2d::Primitive2DReference xEmbedTransparence(
     242             :                             new drawinglayer::primitive2d::MaskPrimitive2D(
     243             :                                 aClipPolyPolygon,
     244         156 :                                 rContent));
     245             : 
     246         156 :                         rContent = drawinglayer::primitive2d::Primitive2DSequence(&xEmbedTransparence, 1);
     247             :                     }
     248             :                     else
     249             :                     {
     250           0 :                         if(!bAddContent)
     251             :                         {
     252           0 :                             rContent.realloc(0);
     253             :                         }
     254             :                     }
     255             :                 }
     256             :                 else
     257             :                 {
     258             :                     // An empty clipping path will completely clip away the element that had
     259             :                     // the clip-path property applied. (Svg spec)
     260           0 :                     rContent.realloc(0);
     261         156 :                 }
     262             :             }
     263         156 :         }
     264             : 
     265             :     } // end of namespace svgreader
     266             : } // end of namespace svgio
     267             : 
     268             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10