1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 // MARKER(update_precomp.py): autogen include statement, do not remove 23 #include "precompiled_svgio.hxx" 24 25 #include <svgio/svgreader/svgclippathnode.hxx> 26 #include <drawinglayer/primitive2d/transformprimitive2d.hxx> 27 #include <drawinglayer/primitive2d/maskprimitive2d.hxx> 28 #include <basegfx/matrix/b2dhommatrixtools.hxx> 29 #include <drawinglayer/geometry/viewinformation2d.hxx> 30 #include <drawinglayer/processor2d/contourextractor2d.hxx> 31 #include <basegfx/polygon/b2dpolypolygoncutter.hxx> 32 #include <basegfx/polygon/b2dpolygontools.hxx> 33 34 ////////////////////////////////////////////////////////////////////////////// 35 36 namespace svgio 37 { 38 namespace svgreader 39 { 40 SvgClipPathNode::SvgClipPathNode( 41 SvgDocument& rDocument, 42 SvgNode* pParent) 43 : SvgNode(SVGTokenClipPathNode, rDocument, pParent), 44 maSvgStyleAttributes(*this), 45 mpaTransform(0), 46 maClipPathUnits(userSpaceOnUse) 47 { 48 } 49 50 SvgClipPathNode::~SvgClipPathNode() 51 { 52 if(mpaTransform) delete mpaTransform; 53 } 54 55 const SvgStyleAttributes* SvgClipPathNode::getSvgStyleAttributes() const 56 { 57 return &maSvgStyleAttributes; 58 } 59 60 void SvgClipPathNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent) 61 { 62 // call parent 63 SvgNode::parseAttribute(rTokenName, aSVGToken, aContent); 64 65 // read style attributes 66 maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent); 67 68 // parse own 69 switch(aSVGToken) 70 { 71 case SVGTokenStyle: 72 { 73 maSvgStyleAttributes.readStyle(aContent); 74 break; 75 } 76 case SVGTokenTransform: 77 { 78 const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this)); 79 80 if(!aMatrix.isIdentity()) 81 { 82 setTransform(&aMatrix); 83 } 84 break; 85 } 86 case SVGTokenClipPathUnits: 87 { 88 if(aContent.getLength()) 89 { 90 if(aContent.match(commonStrings::aStrUserSpaceOnUse, 0)) 91 { 92 setClipPathUnits(userSpaceOnUse); 93 } 94 else if(aContent.match(commonStrings::aStrObjectBoundingBox, 0)) 95 { 96 setClipPathUnits(objectBoundingBox); 97 } 98 } 99 break; 100 } 101 default: 102 { 103 break; 104 } 105 } 106 } 107 108 void SvgClipPathNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool bReferenced) const 109 { 110 drawinglayer::primitive2d::Primitive2DSequence aNewTarget; 111 112 // decompose childs 113 SvgNode::decomposeSvgNode(aNewTarget, bReferenced); 114 115 if(aNewTarget.hasElements()) 116 { 117 if(getTransform()) 118 { 119 // create embedding group element with transformation 120 const drawinglayer::primitive2d::Primitive2DReference xRef( 121 new drawinglayer::primitive2d::TransformPrimitive2D( 122 *getTransform(), 123 aNewTarget)); 124 125 drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget, xRef); 126 } 127 else 128 { 129 // append to current target 130 drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget, aNewTarget); 131 } 132 } 133 } 134 135 void SvgClipPathNode::apply(drawinglayer::primitive2d::Primitive2DSequence& rContent) const 136 { 137 if(rContent.hasElements() && Display_none != getDisplay()) 138 { 139 const drawinglayer::geometry::ViewInformation2D aViewInformation2D; 140 drawinglayer::primitive2d::Primitive2DSequence aClipTarget; 141 basegfx::B2DPolyPolygon aClipPolyPolygon; 142 143 // get clipPath definition as primitives 144 decomposeSvgNode(aClipTarget, true); 145 146 if(aClipTarget.hasElements()) 147 { 148 // extract filled plygons as base for a mask PolyPolygon 149 drawinglayer::processor2d::ContourExtractor2D aExtractor(aViewInformation2D, true); 150 151 aExtractor.process(aClipTarget); 152 153 const basegfx::B2DPolyPolygonVector& rResult(aExtractor.getExtractedContour()); 154 const sal_uInt32 nSize(rResult.size()); 155 156 if(nSize > 1) 157 { 158 // merge to single clipPolyPolygon 159 aClipPolyPolygon = basegfx::tools::mergeToSinglePolyPolygon(rResult); 160 } 161 else 162 { 163 aClipPolyPolygon = rResult[0]; 164 } 165 } 166 167 if(aClipPolyPolygon.count()) 168 { 169 if(objectBoundingBox == getClipPathUnits()) 170 { 171 // clip is object-relative, transform using content transformation 172 const basegfx::B2DRange aContentRange( 173 drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence( 174 rContent, 175 aViewInformation2D)); 176 177 aClipPolyPolygon.transform( 178 basegfx::tools::createScaleTranslateB2DHomMatrix( 179 aContentRange.getRange(), 180 aContentRange.getMinimum())); 181 } 182 183 // #124313# try to avoid creating an embedding to a MaskPrimitive2D if 184 // possible; MaskPrimitive2D processing is potentially expensive 185 bool bCreateEmbedding(true); 186 bool bAddContent(true); 187 188 if(basegfx::tools::isRectangle(aClipPolyPolygon)) 189 { 190 // ClipRegion is a rectangle, thus it is not expensive to tell 191 // if the content is completely inside or outside of it; get ranges 192 const basegfx::B2DRange aClipRange(aClipPolyPolygon.getB2DRange()); 193 const basegfx::B2DRange aContentRange( 194 drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence( 195 rContent, 196 aViewInformation2D)); 197 198 if(aClipRange.isInside(aContentRange)) 199 { 200 // completely contained, no need to clip at all, so no need for embedding 201 bCreateEmbedding = false; 202 } 203 else if(aClipRange.overlaps(aContentRange)) 204 { 205 // overlap; embedding needed. ClipRegion can be minimized by using 206 // the intersection of the ClipRange and the ContentRange. Minimizing 207 // the ClipRegion potentially enhances further processing since 208 // usually clip operations are expensive. 209 basegfx::B2DRange aCommonRange(aContentRange); 210 211 aCommonRange.intersect(aClipRange); 212 aClipPolyPolygon = basegfx::B2DPolyPolygon(basegfx::tools::createPolygonFromRect(aCommonRange)); 213 } 214 else 215 { 216 // not inside and no overlap -> completely outside 217 // no need for embedding, no need for content at all 218 bCreateEmbedding = false; 219 bAddContent = false; 220 } 221 } 222 else 223 { 224 // ClipRegion is not a simple rectangle, it would be possible but expensive to 225 // tell if the content needs clipping or not. It is also dependent of 226 // the content's decomposition. To do this, a processor would be needed that 227 // is capable if processing the given sequence of primitives and decide 228 // if all is inside or all is outside. Such a ClipProcessor could be written, 229 // but for now just create the embedding 230 } 231 232 if(bCreateEmbedding) 233 { 234 // redefine target. Use MaskPrimitive2D with created clip 235 // geometry. Using the automatically set mbIsClipPathContent at 236 // SvgStyleAttributes the clip definition is without fill, stroke, 237 // and strokeWidth and forced to black 238 const drawinglayer::primitive2d::Primitive2DReference xEmbedTransparence( 239 new drawinglayer::primitive2d::MaskPrimitive2D( 240 aClipPolyPolygon, 241 rContent)); 242 243 rContent = drawinglayer::primitive2d::Primitive2DSequence(&xEmbedTransparence, 1); 244 } 245 else 246 { 247 if(!bAddContent) 248 { 249 rContent.realloc(0); 250 } 251 } 252 } 253 else 254 { 255 // An empty clipping path will completely clip away the element that had 256 // the �clip-path� property applied. (Svg spec) 257 rContent.realloc(0); 258 } 259 } 260 } 261 262 } // end of namespace svgreader 263 } // end of namespace svgio 264 265 ////////////////////////////////////////////////////////////////////////////// 266 // eof 267