LibreOffice Module svgio (master) 1
svgclippathnode.cxx
Go to the documentation of this file.
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>
28#include <o3tl/string_view.hxx>
29
30namespace svgio::svgreader
31{
33 SvgDocument& rDocument,
34 SvgNode* pParent)
35 : SvgNode(SVGToken::ClipPathNode, rDocument, pParent),
36 maSvgStyleAttributes(*this),
37 maClipPathUnits(SvgUnits::userSpaceOnUse)
38 {
39 }
40
42 {
43 }
44
46 {
48 }
49
50 void SvgClipPathNode::parseAttribute(const OUString& rTokenName, SVGToken aSVGToken, const OUString& aContent)
51 {
52 // call parent
53 SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
54
55 // read style attributes
56 maSvgStyleAttributes.parseStyleAttribute(aSVGToken, aContent);
57
58 // parse own
59 switch(aSVGToken)
60 {
61 case SVGToken::Style:
62 {
63 readLocalCssStyle(aContent);
64 break;
65 }
67 {
68 const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
69
70 if(!aMatrix.isIdentity())
71 {
72 setTransform(aMatrix);
73 }
74 break;
75 }
77 {
78 if(!aContent.isEmpty())
79 {
81 {
83 }
85 {
87 }
88 }
89 break;
90 }
91 default:
92 {
93 break;
94 }
95 }
96 }
97
99 {
101
102 // decompose children
103 SvgNode::decomposeSvgNode(aNewTarget, bReferenced);
104
105 if(aNewTarget.empty())
106 return;
107
108 if(getTransform())
109 {
110 // create embedding group element with transformation
113 *getTransform(),
114 std::move(aNewTarget)));
115
116 rTarget.push_back(xRef);
117 }
118 else
119 {
120 // append to current target
121 rTarget.append(aNewTarget);
122 }
123 }
124
127 const std::optional<basegfx::B2DHomMatrix>& pTransform) const
128 {
129 if (rContent.empty() || Display::None == getDisplay())
130 return;
131
132 const drawinglayer::geometry::ViewInformation2D aViewInformation2D;
134 basegfx::B2DPolyPolygon aClipPolyPolygon;
135
136 // get clipPath definition as primitives
137 decomposeSvgNode(aClipTarget, true);
138
139 if(!aClipTarget.empty())
140 {
141 // extract filled polygons as base for a mask PolyPolygon
142 drawinglayer::processor2d::ContourExtractor2D aExtractor(aViewInformation2D, true);
143
144 aExtractor.process(aClipTarget);
145
146 const basegfx::B2DPolyPolygonVector& rResult(aExtractor.getExtractedContour());
147 const sal_uInt32 nSize(rResult.size());
148
149 if(nSize > 1)
150 {
151 // merge to single clipPolyPolygon
152 aClipPolyPolygon = basegfx::utils::mergeToSinglePolyPolygon(rResult);
153 }
154 else if (nSize != 0)
155 {
156 aClipPolyPolygon = rResult[0];
157 }
158 }
159
160 if(aClipPolyPolygon.count())
161 {
163 {
164 // clip is object-relative, transform using content transformation
165 const basegfx::B2DRange aContentRange(rContent.getB2DRange(aViewInformation2D));
166
167 aClipPolyPolygon.transform(
169 aContentRange.getRange(),
170 aContentRange.getMinimum()));
171 }
172 else // userSpaceOnUse
173 {
174 // #i124852#
175 if(pTransform)
176 {
177 aClipPolyPolygon.transform(*pTransform);
178 }
179 }
180
181 // #i124313# try to avoid creating an embedding to a MaskPrimitive2D if
182 // possible; MaskPrimitive2D processing is potentially expensive
183 bool bCreateEmbedding(true);
184 bool bAddContent(true);
185
186 if(basegfx::utils::isRectangle(aClipPolyPolygon))
187 {
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 const basegfx::B2DRange aClipRange(aClipPolyPolygon.getB2DRange());
191 const basegfx::B2DRange aContentRange(
192 rContent.getB2DRange(
193 aViewInformation2D));
194
195 if(aClipRange.isInside(aContentRange))
196 {
197 // completely contained, no need to clip at all, so no need for embedding
198 bCreateEmbedding = false;
199 }
200 else if(aClipRange.overlaps(aContentRange))
201 {
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 basegfx::B2DRange aCommonRange(aContentRange);
207
208 aCommonRange.intersect(aClipRange);
209 aClipPolyPolygon = basegfx::B2DPolyPolygon(basegfx::utils::createPolygonFromRect(aCommonRange));
210 }
211 else
212 {
213 // not inside and no overlap -> completely outside
214 // no need for embedding, no need for content at all
215 bCreateEmbedding = false;
216 bAddContent = false;
217 }
218 }
219 else
220 {
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 }
228
229 if(bCreateEmbedding)
230 {
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
237 std::move(aClipPolyPolygon),
238 std::move(rContent)));
239
240 rContent = drawinglayer::primitive2d::Primitive2DContainer { xEmbedTransparence };
241 }
242 else
243 {
244 if(!bAddContent)
245 {
246 rContent.clear();
247 }
248 }
249 }
250 else
251 {
252 // An empty clipping path will completely clip away the element that had
253 // the clip-path property applied. (Svg spec)
254 rContent.clear();
255 }
256 }
257
258} // end of namespace svgio::svgreader
259
260/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool isIdentity() const
void transform(const basegfx::B2DHomMatrix &rMatrix)
B2DRange getB2DRange() const
sal_uInt32 count() const
B2DVector getRange() const
B2DPoint getMinimum() const
void intersect(const Range2D &rRange)
bool isInside(const Tuple2D< TYPE > &rTuple) const
bool overlaps(const Range2D &rRange) const
basegfx::B2DRange getB2DRange(const geometry::ViewInformation2D &aViewInformation) const
void process(const primitive2d::Primitive2DContainer &rSource)
const basegfx::B2DPolyPolygonVector & getExtractedContour() const
void setClipPathUnits(const SvgUnits aClipPathUnits)
SvgStyleAttributes maSvgStyleAttributes
use styles
virtual void parseAttribute(const OUString &rTokenName, SVGToken aSVGToken, const OUString &aContent) override
SvgClipPathNode(SvgDocument &rDocument, SvgNode *pParent)
virtual const SvgStyleAttributes * getSvgStyleAttributes() const override
void apply(drawinglayer::primitive2d::Primitive2DContainer &rTarget, const std::optional< basegfx::B2DHomMatrix > &pTransform) const
apply contained clipPath to given geometry #i124852# transform may be needed
virtual void decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer &rTarget, bool bReferenced) const override
SvgUnits getClipPathUnits() const
clipPathUnits content
void setTransform(const std::optional< basegfx::B2DHomMatrix > &pMatrix)
const std::optional< basegfx::B2DHomMatrix > & getTransform() const
transform content
Display getDisplay() const
Display access #i121656#.
Definition: svgnode.hxx:182
virtual void decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer &rTarget, bool bReferenced) const
Definition: svgnode.cxx:584
virtual void parseAttribute(const OUString &rTokenName, SVGToken aSVGToken, const OUString &aContent)
Definition: svgnode.cxx:534
void readLocalCssStyle(std::u16string_view aContent)
scan helper to read and interpret a local CssStyle to mpLocalCssStyle
Definition: svgnode.cxx:412
void parseStyleAttribute(SVGToken aSVGToken, const OUString &rContent)
local attribute scanner
FilterGroup & rTarget
B2DHomMatrix createScaleTranslateB2DHomMatrix(double fScaleX, double fScaleY, double fTranslateX, double fTranslateY)
B2DPolygon createPolygonFromRect(const B2DRectangle &rRect, double fRadiusX, double fRadiusY)
bool isRectangle(const B2DPolygon &rPoly)
B2DPolyPolygon mergeToSinglePolyPolygon(const B2DPolyPolygonVector &rInput)
::std::vector< B2DPolyPolygon > B2DPolyPolygonVector
std::basic_string_view< charT, traits > trim(std::basic_string_view< charT, traits > str)
bool equalsIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2)
basegfx::B2DHomMatrix readTransform(std::u16string_view rCandidate, InfoProvider const &rInfoProvider)
Definition: svgtools.cxx:871
static constexpr OUStringLiteral aStrUserSpaceOnUse
Definition: svgtools.hxx:38
static constexpr OUStringLiteral aStrObjectBoundingBox
Definition: svgtools.hxx:39