/src/libreoffice/svgio/source/svgreader/svgclippathnode.cxx
Line | Count | Source |
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 <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 | | #include <o3tl/string_view.hxx> |
29 | | |
30 | | namespace svgio::svgreader |
31 | | { |
32 | | SvgClipPathNode::SvgClipPathNode( |
33 | | SvgDocument& rDocument, |
34 | | SvgNode* pParent) |
35 | 0 | : SvgNode(SVGToken::ClipPathNode, rDocument, pParent), |
36 | 0 | maSvgStyleAttributes(*this), |
37 | 0 | maClipPathUnits(SvgUnits::userSpaceOnUse) |
38 | 0 | { |
39 | 0 | } |
40 | | |
41 | | SvgClipPathNode::~SvgClipPathNode() |
42 | 0 | { |
43 | 0 | } |
44 | | |
45 | | const SvgStyleAttributes* SvgClipPathNode::getSvgStyleAttributes() const |
46 | 0 | { |
47 | 0 | return &maSvgStyleAttributes; |
48 | 0 | } |
49 | | |
50 | | void SvgClipPathNode::parseAttribute(SVGToken aSVGToken, const OUString& aContent) |
51 | 0 | { |
52 | | // call parent |
53 | 0 | SvgNode::parseAttribute(aSVGToken, aContent); |
54 | | |
55 | | // read style attributes |
56 | 0 | maSvgStyleAttributes.parseStyleAttribute(aSVGToken, aContent); |
57 | | |
58 | | // parse own |
59 | 0 | switch(aSVGToken) |
60 | 0 | { |
61 | 0 | case SVGToken::Style: |
62 | 0 | { |
63 | 0 | readLocalCssStyle(aContent); |
64 | 0 | break; |
65 | 0 | } |
66 | 0 | case SVGToken::Transform: |
67 | 0 | { |
68 | 0 | const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this)); |
69 | |
|
70 | 0 | if(!aMatrix.isIdentity()) |
71 | 0 | { |
72 | 0 | setTransform(aMatrix); |
73 | 0 | } |
74 | 0 | break; |
75 | 0 | } |
76 | 0 | case SVGToken::ClipPathUnits: |
77 | 0 | { |
78 | 0 | if(!aContent.isEmpty()) |
79 | 0 | { |
80 | 0 | if(o3tl::equalsIgnoreAsciiCase(o3tl::trim(aContent), commonStrings::aStrUserSpaceOnUse)) |
81 | 0 | { |
82 | 0 | setClipPathUnits(SvgUnits::userSpaceOnUse); |
83 | 0 | } |
84 | 0 | else if(o3tl::equalsIgnoreAsciiCase(o3tl::trim(aContent), commonStrings::aStrObjectBoundingBox)) |
85 | 0 | { |
86 | 0 | setClipPathUnits(SvgUnits::objectBoundingBox); |
87 | 0 | } |
88 | 0 | } |
89 | 0 | break; |
90 | 0 | } |
91 | 0 | default: |
92 | 0 | { |
93 | 0 | break; |
94 | 0 | } |
95 | 0 | } |
96 | 0 | } |
97 | | |
98 | | void SvgClipPathNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer& rTarget, bool bReferenced) const |
99 | 0 | { |
100 | 0 | drawinglayer::primitive2d::Primitive2DContainer aNewTarget; |
101 | | |
102 | | // decompose children |
103 | 0 | SvgNode::decomposeSvgNode(aNewTarget, bReferenced); |
104 | |
|
105 | 0 | if(aNewTarget.empty()) |
106 | 0 | return; |
107 | | |
108 | 0 | if(getTransform()) |
109 | 0 | { |
110 | | // create embedding group element with transformation |
111 | 0 | const drawinglayer::primitive2d::Primitive2DReference xRef( |
112 | 0 | new drawinglayer::primitive2d::TransformPrimitive2D( |
113 | 0 | *getTransform(), |
114 | 0 | std::move(aNewTarget))); |
115 | |
|
116 | 0 | rTarget.push_back(xRef); |
117 | 0 | } |
118 | 0 | else |
119 | 0 | { |
120 | | // append to current target |
121 | 0 | rTarget.append(std::move(aNewTarget)); |
122 | 0 | } |
123 | 0 | } |
124 | | |
125 | | void SvgClipPathNode::apply( |
126 | | drawinglayer::primitive2d::Primitive2DContainer& rContent, |
127 | | const std::optional<basegfx::B2DHomMatrix>& pTransform) const |
128 | 0 | { |
129 | 0 | if (rContent.empty() || Display::None == getDisplay()) |
130 | 0 | return; |
131 | | |
132 | 0 | const drawinglayer::geometry::ViewInformation2D aViewInformation2D; |
133 | 0 | drawinglayer::primitive2d::Primitive2DContainer aClipTarget; |
134 | 0 | basegfx::B2DPolyPolygon aClipPolyPolygon; |
135 | | |
136 | | // get clipPath definition as primitives |
137 | 0 | decomposeSvgNode(aClipTarget, true); |
138 | |
|
139 | 0 | if(!aClipTarget.empty()) |
140 | 0 | { |
141 | | // extract filled polygons as base for a mask PolyPolygon |
142 | 0 | drawinglayer::processor2d::ContourExtractor2D aExtractor(aViewInformation2D, true); |
143 | |
|
144 | 0 | aExtractor.process(aClipTarget); |
145 | |
|
146 | 0 | const basegfx::B2DPolyPolygonVector& rResult(aExtractor.getExtractedContour()); |
147 | 0 | const sal_uInt32 nSize(rResult.size()); |
148 | |
|
149 | 0 | if(nSize > 1) |
150 | 0 | { |
151 | | // merge to single clipPolyPolygon |
152 | 0 | aClipPolyPolygon = basegfx::utils::mergeToSinglePolyPolygon(rResult); |
153 | 0 | } |
154 | 0 | else if (nSize != 0) |
155 | 0 | { |
156 | 0 | aClipPolyPolygon = rResult[0]; |
157 | 0 | } |
158 | 0 | } |
159 | |
|
160 | 0 | if(aClipPolyPolygon.count()) |
161 | 0 | { |
162 | 0 | if (SvgUnits::objectBoundingBox == getClipPathUnits()) |
163 | 0 | { |
164 | | // clip is object-relative, transform using content transformation |
165 | 0 | const basegfx::B2DRange aContentRange(rContent.getB2DRange(aViewInformation2D)); |
166 | |
|
167 | 0 | aClipPolyPolygon.transform( |
168 | 0 | basegfx::utils::createScaleTranslateB2DHomMatrix( |
169 | 0 | aContentRange.getRange(), |
170 | 0 | aContentRange.getMinimum())); |
171 | 0 | } |
172 | 0 | else // userSpaceOnUse |
173 | 0 | { |
174 | | // #i124852# |
175 | 0 | if(pTransform) |
176 | 0 | { |
177 | 0 | aClipPolyPolygon.transform(*pTransform); |
178 | 0 | } |
179 | 0 | } |
180 | | |
181 | | // #i124313# try to avoid creating an embedding to a MaskPrimitive2D if |
182 | | // possible; MaskPrimitive2D processing is potentially expensive |
183 | 0 | bool bCreateEmbedding(true); |
184 | 0 | bool bAddContent(true); |
185 | |
|
186 | 0 | if(basegfx::utils::isRectangle(aClipPolyPolygon)) |
187 | 0 | { |
188 | | // ClipRegion is a rectangle, thus it is not expensive to tell |
189 | | // if the content is completely inside or outside of it; get ranges |
190 | 0 | const basegfx::B2DRange aClipRange(aClipPolyPolygon.getB2DRange()); |
191 | 0 | const basegfx::B2DRange aContentRange( |
192 | 0 | rContent.getB2DRange( |
193 | 0 | aViewInformation2D)); |
194 | |
|
195 | 0 | if(aClipRange.isInside(aContentRange)) |
196 | 0 | { |
197 | | // completely contained, no need to clip at all, so no need for embedding |
198 | 0 | bCreateEmbedding = false; |
199 | 0 | } |
200 | 0 | else if(aClipRange.overlaps(aContentRange)) |
201 | 0 | { |
202 | | // overlap; embedding needed. ClipRegion can be minimized by using |
203 | | // the intersection of the ClipRange and the ContentRange. Minimizing |
204 | | // the ClipRegion potentially enhances further processing since |
205 | | // usually clip operations are expensive. |
206 | 0 | basegfx::B2DRange aCommonRange(aContentRange); |
207 | |
|
208 | 0 | aCommonRange.intersect(aClipRange); |
209 | 0 | aClipPolyPolygon = basegfx::B2DPolyPolygon(basegfx::utils::createPolygonFromRect(aCommonRange)); |
210 | 0 | } |
211 | 0 | else |
212 | 0 | { |
213 | | // not inside and no overlap -> completely outside |
214 | | // no need for embedding, no need for content at all |
215 | 0 | bCreateEmbedding = false; |
216 | 0 | bAddContent = false; |
217 | 0 | } |
218 | 0 | } |
219 | 0 | else |
220 | 0 | { |
221 | | // ClipRegion is not a simple rectangle, it would be possible but expensive to |
222 | | // tell if the content needs clipping or not. It is also dependent of |
223 | | // the content's decomposition. To do this, a processor would be needed that |
224 | | // is capable if processing the given sequence of primitives and decide |
225 | | // if all is inside or all is outside. Such a ClipProcessor could be written, |
226 | | // but for now just create the embedding |
227 | 0 | } |
228 | |
|
229 | 0 | if(bCreateEmbedding) |
230 | 0 | { |
231 | | // redefine target. Use MaskPrimitive2D with created clip |
232 | | // geometry. Using the automatically set mbIsClipPathContent at |
233 | | // SvgStyleAttributes the clip definition is without fill, stroke, |
234 | | // and strokeWidth and forced to black |
235 | 0 | rContent = drawinglayer::primitive2d::Primitive2DContainer { |
236 | 0 | new drawinglayer::primitive2d::MaskPrimitive2D( |
237 | 0 | std::move(aClipPolyPolygon), |
238 | 0 | std::move(rContent)) |
239 | 0 | }; |
240 | 0 | } |
241 | 0 | else |
242 | 0 | { |
243 | 0 | if(!bAddContent) |
244 | 0 | { |
245 | 0 | rContent.clear(); |
246 | 0 | } |
247 | 0 | } |
248 | 0 | } |
249 | 0 | else |
250 | 0 | { |
251 | | // An empty clipping path will completely clip away the element that had |
252 | | // the clip-path property applied. (Svg spec) |
253 | 0 | rContent.clear(); |
254 | 0 | } |
255 | 0 | } |
256 | | |
257 | | } // end of namespace svgio::svgreader |
258 | | |
259 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |