tdf#163228 Enable vertical text options for Mongolian script
[LibreOffice.git] / svgio / source / svgreader / svgclippathnode.cxx
blob954ce19a78b8bbf27bc2407486864020b28f9d39
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 <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>
30 namespace svgio::svgreader
32 SvgClipPathNode::SvgClipPathNode(
33 SvgDocument& rDocument,
34 SvgNode* pParent)
35 : SvgNode(SVGToken::ClipPathNode, rDocument, pParent),
36 maSvgStyleAttributes(*this),
37 maClipPathUnits(SvgUnits::userSpaceOnUse)
41 SvgClipPathNode::~SvgClipPathNode()
45 const SvgStyleAttributes* SvgClipPathNode::getSvgStyleAttributes() const
47 return &maSvgStyleAttributes;
50 void SvgClipPathNode::parseAttribute(SVGToken aSVGToken, const OUString& aContent)
52 // call parent
53 SvgNode::parseAttribute(aSVGToken, aContent);
55 // read style attributes
56 maSvgStyleAttributes.parseStyleAttribute(aSVGToken, aContent);
58 // parse own
59 switch(aSVGToken)
61 case SVGToken::Style:
63 readLocalCssStyle(aContent);
64 break;
66 case SVGToken::Transform:
68 const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
70 if(!aMatrix.isIdentity())
72 setTransform(aMatrix);
74 break;
76 case SVGToken::ClipPathUnits:
78 if(!aContent.isEmpty())
80 if(o3tl::equalsIgnoreAsciiCase(o3tl::trim(aContent), commonStrings::aStrUserSpaceOnUse))
82 setClipPathUnits(SvgUnits::userSpaceOnUse);
84 else if(o3tl::equalsIgnoreAsciiCase(o3tl::trim(aContent), commonStrings::aStrObjectBoundingBox))
86 setClipPathUnits(SvgUnits::objectBoundingBox);
89 break;
91 default:
93 break;
98 void SvgClipPathNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer& rTarget, bool bReferenced) const
100 drawinglayer::primitive2d::Primitive2DContainer aNewTarget;
102 // decompose children
103 SvgNode::decomposeSvgNode(aNewTarget, bReferenced);
105 if(aNewTarget.empty())
106 return;
108 if(getTransform())
110 // create embedding group element with transformation
111 const drawinglayer::primitive2d::Primitive2DReference xRef(
112 new drawinglayer::primitive2d::TransformPrimitive2D(
113 *getTransform(),
114 std::move(aNewTarget)));
116 rTarget.push_back(xRef);
118 else
120 // append to current target
121 rTarget.append(std::move(aNewTarget));
125 void SvgClipPathNode::apply(
126 drawinglayer::primitive2d::Primitive2DContainer& rContent,
127 const std::optional<basegfx::B2DHomMatrix>& pTransform) const
129 if (rContent.empty() || Display::None == getDisplay())
130 return;
132 const drawinglayer::geometry::ViewInformation2D aViewInformation2D;
133 drawinglayer::primitive2d::Primitive2DContainer aClipTarget;
134 basegfx::B2DPolyPolygon aClipPolyPolygon;
136 // get clipPath definition as primitives
137 decomposeSvgNode(aClipTarget, true);
139 if(!aClipTarget.empty())
141 // extract filled polygons as base for a mask PolyPolygon
142 drawinglayer::processor2d::ContourExtractor2D aExtractor(aViewInformation2D, true);
144 aExtractor.process(aClipTarget);
146 const basegfx::B2DPolyPolygonVector& rResult(aExtractor.getExtractedContour());
147 const sal_uInt32 nSize(rResult.size());
149 if(nSize > 1)
151 // merge to single clipPolyPolygon
152 aClipPolyPolygon = basegfx::utils::mergeToSinglePolyPolygon(rResult);
154 else if (nSize != 0)
156 aClipPolyPolygon = rResult[0];
160 if(aClipPolyPolygon.count())
162 if (SvgUnits::objectBoundingBox == getClipPathUnits())
164 // clip is object-relative, transform using content transformation
165 const basegfx::B2DRange aContentRange(rContent.getB2DRange(aViewInformation2D));
167 aClipPolyPolygon.transform(
168 basegfx::utils::createScaleTranslateB2DHomMatrix(
169 aContentRange.getRange(),
170 aContentRange.getMinimum()));
172 else // userSpaceOnUse
174 // #i124852#
175 if(pTransform)
177 aClipPolyPolygon.transform(*pTransform);
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);
186 if(basegfx::utils::isRectangle(aClipPolyPolygon))
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));
195 if(aClipRange.isInside(aContentRange))
197 // completely contained, no need to clip at all, so no need for embedding
198 bCreateEmbedding = false;
200 else if(aClipRange.overlaps(aContentRange))
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);
208 aCommonRange.intersect(aClipRange);
209 aClipPolyPolygon = basegfx::B2DPolyPolygon(basegfx::utils::createPolygonFromRect(aCommonRange));
211 else
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;
219 else
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
229 if(bCreateEmbedding)
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 rContent = drawinglayer::primitive2d::Primitive2DContainer {
236 new drawinglayer::primitive2d::MaskPrimitive2D(
237 std::move(aClipPolyPolygon),
238 std::move(rContent))
241 else
243 if(!bAddContent)
245 rContent.clear();
249 else
251 // An empty clipping path will completely clip away the element that had
252 // the clip-path property applied. (Svg spec)
253 rContent.clear();
257 } // end of namespace svgio::svgreader
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */