Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / svgio / source / svgreader / svgclippathnode.cxx
blob5c271400d5f0fedf2c9abfa9d1f887312797f3e4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
28 //////////////////////////////////////////////////////////////////////////////
30 namespace svgio
32 namespace svgreader
34 SvgClipPathNode::SvgClipPathNode(
35 SvgDocument& rDocument,
36 SvgNode* pParent)
37 : SvgNode(SVGTokenClipPathNode, rDocument, pParent),
38 maSvgStyleAttributes(*this),
39 mpaTransform(0),
40 maClipPathUnits(userSpaceOnUse)
44 SvgClipPathNode::~SvgClipPathNode()
46 if(mpaTransform) delete mpaTransform;
49 const SvgStyleAttributes* SvgClipPathNode::getSvgStyleAttributes() const
51 return &maSvgStyleAttributes;
54 void SvgClipPathNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent)
56 // call parent
57 SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
59 // read style attributes
60 maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent);
62 // parse own
63 switch(aSVGToken)
65 case SVGTokenStyle:
67 maSvgStyleAttributes.readStyle(aContent);
68 break;
70 case SVGTokenTransform:
72 const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
74 if(!aMatrix.isIdentity())
76 setTransform(&aMatrix);
78 break;
80 case SVGTokenClipPathUnits:
82 if(aContent.getLength())
84 if(aContent.match(commonStrings::aStrUserSpaceOnUse, 0))
86 setClipPathUnits(userSpaceOnUse);
88 else if(aContent.match(commonStrings::aStrObjectBoundingBox, 0))
90 setClipPathUnits(objectBoundingBox);
93 break;
95 default:
97 break;
102 void SvgClipPathNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool bReferenced) const
104 drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
106 // decompose childs
107 SvgNode::decomposeSvgNode(aNewTarget, bReferenced);
109 if(aNewTarget.hasElements())
111 if(getTransform())
113 // create embedding group element with transformation
114 const drawinglayer::primitive2d::Primitive2DReference xRef(
115 new drawinglayer::primitive2d::TransformPrimitive2D(
116 *getTransform(),
117 aNewTarget));
119 drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget, xRef);
121 else
123 // append to current target
124 drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget, aNewTarget);
129 void SvgClipPathNode::apply(drawinglayer::primitive2d::Primitive2DSequence& rContent) const
131 if(rContent.hasElements())
133 const drawinglayer::geometry::ViewInformation2D aViewInformation2D;
134 drawinglayer::primitive2d::Primitive2DSequence aClipTarget;
135 basegfx::B2DPolyPolygon aClipPolyPolygon;
137 // get clipPath definition as primitives
138 decomposeSvgNode(aClipTarget, true);
140 if(aClipTarget.hasElements())
142 // extract filled plygons as base for a mask PolyPolygon
143 drawinglayer::processor2d::ContourExtractor2D aExtractor(aViewInformation2D, true);
145 aExtractor.process(aClipTarget);
147 const basegfx::B2DPolyPolygonVector& rResult(aExtractor.getExtractedContour());
148 const sal_uInt32 nSize(rResult.size());
150 if(nSize > 1)
152 // merge to single clipPolyPolygon
153 aClipPolyPolygon = basegfx::tools::mergeToSinglePolyPolygon(rResult);
155 else
157 aClipPolyPolygon = rResult[0];
161 if(aClipPolyPolygon.count())
163 if(objectBoundingBox == getClipPathUnits())
165 // clip is object-relative, transform using content transformation
166 const basegfx::B2DRange aContentRange(
167 drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence(
168 rContent,
169 aViewInformation2D));
171 aClipPolyPolygon.transform(
172 basegfx::tools::createScaleTranslateB2DHomMatrix(
173 aContentRange.getRange(),
174 aContentRange.getMinimum()));
177 // redefine target. Use MaskPrimitive2D with created clip
178 // geometry. Using the automatically set mbIsClipPathContent at
179 // SvgStyleAttributes the clip definition is without fill, stroke,
180 // and strokeWidth and forced to black
181 const drawinglayer::primitive2d::Primitive2DReference xEmbedTransparence(
182 new drawinglayer::primitive2d::MaskPrimitive2D(
183 aClipPolyPolygon,
184 rContent));
186 rContent = drawinglayer::primitive2d::Primitive2DSequence(&xEmbedTransparence, 1);
188 else
190 // An empty clipping path will completely clip away the element that had
191 // the ‘clip-path’ property applied. (Svg spec)
192 rContent.realloc(0);
197 } // end of namespace svgreader
198 } // end of namespace svgio
200 //////////////////////////////////////////////////////////////////////////////
201 // eof
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */