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 0 : SvgClipPathNode::SvgClipPathNode(
36 : SvgDocument& rDocument,
37 : SvgNode* pParent)
38 : : SvgNode(SVGTokenClipPathNode, rDocument, pParent),
39 : maSvgStyleAttributes(*this),
40 : mpaTransform(0),
41 0 : maClipPathUnits(userSpaceOnUse)
42 : {
43 0 : }
44 :
45 0 : SvgClipPathNode::~SvgClipPathNode()
46 : {
47 0 : if(mpaTransform) delete mpaTransform;
48 0 : }
49 :
50 0 : const SvgStyleAttributes* SvgClipPathNode::getSvgStyleAttributes() const
51 : {
52 0 : return &maSvgStyleAttributes;
53 : }
54 :
55 0 : void SvgClipPathNode::parseAttribute(const OUString& rTokenName, SVGToken aSVGToken, const OUString& aContent)
56 : {
57 : // call parent
58 0 : SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
59 :
60 : // read style attributes
61 0 : maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent);
62 :
63 : // parse own
64 0 : 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 0 : break;
99 : }
100 : }
101 0 : }
102 :
103 0 : void SvgClipPathNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool bReferenced) const
104 : {
105 0 : drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
106 :
107 : // decompose children
108 0 : SvgNode::decomposeSvgNode(aNewTarget, bReferenced);
109 :
110 0 : if(aNewTarget.hasElements())
111 : {
112 0 : 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 0 : drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget, aNewTarget);
126 : }
127 0 : }
128 0 : }
129 :
130 0 : void SvgClipPathNode::apply(drawinglayer::primitive2d::Primitive2DSequence& rContent) const
131 : {
132 0 : if(rContent.hasElements() && Display_none != getDisplay())
133 : {
134 0 : const drawinglayer::geometry::ViewInformation2D aViewInformation2D;
135 0 : drawinglayer::primitive2d::Primitive2DSequence aClipTarget;
136 0 : basegfx::B2DPolyPolygon aClipPolyPolygon;
137 :
138 : // get clipPath definition as primitives
139 0 : decomposeSvgNode(aClipTarget, true);
140 :
141 0 : if(aClipTarget.hasElements())
142 : {
143 : // extract filled plygons as base for a mask PolyPolygon
144 0 : drawinglayer::processor2d::ContourExtractor2D aExtractor(aViewInformation2D, true);
145 :
146 0 : aExtractor.process(aClipTarget);
147 :
148 0 : const basegfx::B2DPolyPolygonVector& rResult(aExtractor.getExtractedContour());
149 0 : const sal_uInt32 nSize(rResult.size());
150 :
151 0 : if(nSize > 1)
152 : {
153 : // merge to single clipPolyPolygon
154 0 : aClipPolyPolygon = basegfx::tools::mergeToSinglePolyPolygon(rResult);
155 : }
156 : else
157 : {
158 0 : aClipPolyPolygon = rResult[0];
159 0 : }
160 : }
161 :
162 0 : if(aClipPolyPolygon.count())
163 : {
164 0 : 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 0 : bool bCreateEmbedding(true);
181 0 : bool bAddContent(true);
182 :
183 0 : 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 0 : 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 0 : rContent));
237 :
238 0 : 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 0 : }
254 : }
255 0 : }
256 :
257 : } // end of namespace svgreader
258 : } // end of namespace svgio
259 :
260 :
261 : // eof
262 :
263 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|