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 :
28 : //////////////////////////////////////////////////////////////////////////////
29 :
30 : namespace svgio
31 : {
32 : namespace svgreader
33 : {
34 0 : SvgClipPathNode::SvgClipPathNode(
35 : SvgDocument& rDocument,
36 : SvgNode* pParent)
37 : : SvgNode(SVGTokenClipPathNode, rDocument, pParent),
38 : maSvgStyleAttributes(*this),
39 : mpaTransform(0),
40 0 : maClipPathUnits(userSpaceOnUse)
41 : {
42 0 : }
43 :
44 0 : SvgClipPathNode::~SvgClipPathNode()
45 : {
46 0 : if(mpaTransform) delete mpaTransform;
47 0 : }
48 :
49 0 : const SvgStyleAttributes* SvgClipPathNode::getSvgStyleAttributes() const
50 : {
51 0 : return &maSvgStyleAttributes;
52 : }
53 :
54 0 : void SvgClipPathNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent)
55 : {
56 : // call parent
57 0 : SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
58 :
59 : // read style attributes
60 0 : maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent);
61 :
62 : // parse own
63 0 : switch(aSVGToken)
64 : {
65 : case SVGTokenStyle:
66 : {
67 0 : maSvgStyleAttributes.readStyle(aContent);
68 0 : break;
69 : }
70 : case SVGTokenTransform:
71 : {
72 0 : const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
73 :
74 0 : if(!aMatrix.isIdentity())
75 : {
76 0 : setTransform(&aMatrix);
77 : }
78 0 : break;
79 : }
80 : case SVGTokenClipPathUnits:
81 : {
82 0 : if(aContent.getLength())
83 : {
84 0 : if(aContent.match(commonStrings::aStrUserSpaceOnUse, 0))
85 : {
86 0 : setClipPathUnits(userSpaceOnUse);
87 : }
88 0 : else if(aContent.match(commonStrings::aStrObjectBoundingBox, 0))
89 : {
90 0 : setClipPathUnits(objectBoundingBox);
91 : }
92 : }
93 0 : break;
94 : }
95 : default:
96 : {
97 0 : break;
98 : }
99 : }
100 0 : }
101 :
102 0 : void SvgClipPathNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool bReferenced) const
103 : {
104 0 : drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
105 :
106 : // decompose childs
107 0 : SvgNode::decomposeSvgNode(aNewTarget, bReferenced);
108 :
109 0 : if(aNewTarget.hasElements())
110 : {
111 0 : if(getTransform())
112 : {
113 : // create embedding group element with transformation
114 : const drawinglayer::primitive2d::Primitive2DReference xRef(
115 : new drawinglayer::primitive2d::TransformPrimitive2D(
116 : *getTransform(),
117 0 : aNewTarget));
118 :
119 0 : drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget, xRef);
120 : }
121 : else
122 : {
123 : // append to current target
124 0 : drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget, aNewTarget);
125 : }
126 0 : }
127 0 : }
128 :
129 0 : void SvgClipPathNode::apply(drawinglayer::primitive2d::Primitive2DSequence& rContent) const
130 : {
131 0 : if(rContent.hasElements())
132 : {
133 0 : const drawinglayer::geometry::ViewInformation2D aViewInformation2D;
134 0 : drawinglayer::primitive2d::Primitive2DSequence aClipTarget;
135 0 : basegfx::B2DPolyPolygon aClipPolyPolygon;
136 :
137 : // get clipPath definition as primitives
138 0 : decomposeSvgNode(aClipTarget, true);
139 :
140 0 : if(aClipTarget.hasElements())
141 : {
142 : // extract filled plygons as base for a mask PolyPolygon
143 0 : drawinglayer::processor2d::ContourExtractor2D aExtractor(aViewInformation2D, true);
144 :
145 0 : aExtractor.process(aClipTarget);
146 :
147 0 : const basegfx::B2DPolyPolygonVector& rResult(aExtractor.getExtractedContour());
148 0 : const sal_uInt32 nSize(rResult.size());
149 :
150 0 : if(nSize > 1)
151 : {
152 : // merge to single clipPolyPolygon
153 0 : aClipPolyPolygon = basegfx::tools::mergeToSinglePolyPolygon(rResult);
154 : }
155 : else
156 : {
157 0 : aClipPolyPolygon = rResult[0];
158 0 : }
159 : }
160 :
161 0 : if(aClipPolyPolygon.count())
162 : {
163 0 : if(objectBoundingBox == getClipPathUnits())
164 : {
165 : // clip is object-relative, transform using content transformation
166 : const basegfx::B2DRange aContentRange(
167 : drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence(
168 : rContent,
169 0 : aViewInformation2D));
170 :
171 : aClipPolyPolygon.transform(
172 : basegfx::tools::createScaleTranslateB2DHomMatrix(
173 : aContentRange.getRange(),
174 0 : aContentRange.getMinimum()));
175 : }
176 :
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 0 : rContent));
185 :
186 0 : rContent = drawinglayer::primitive2d::Primitive2DSequence(&xEmbedTransparence, 1);
187 : }
188 : else
189 : {
190 : // An empty clipping path will completely clip away the element that had
191 : // the ‘clip-path’ property applied. (Svg spec)
192 0 : rContent.realloc(0);
193 0 : }
194 : }
195 0 : }
196 :
197 : } // end of namespace svgreader
198 : } // end of namespace svgio
199 :
200 : //////////////////////////////////////////////////////////////////////////////
201 : // eof
202 :
203 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|