calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / svgio / source / svgreader / svgmasknode.cxx
blob71531637f565a6a18bd6294da222171949bbb715
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 <svgmasknode.hxx>
21 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
22 #include <drawinglayer/primitive2d/transparenceprimitive2d.hxx>
23 #include <basegfx/matrix/b2dhommatrixtools.hxx>
24 #include <drawinglayer/geometry/viewinformation2d.hxx>
25 #include <drawinglayer/primitive2d/modifiedcolorprimitive2d.hxx>
26 #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
27 #include <basegfx/polygon/b2dpolygontools.hxx>
28 #include <basegfx/polygon/b2dpolygon.hxx>
29 #include <o3tl/string_view.hxx>
31 namespace svgio::svgreader
33 SvgMaskNode::SvgMaskNode(
34 SvgDocument& rDocument,
35 SvgNode* pParent)
36 : SvgNode(SVGToken::Mask, rDocument, pParent),
37 maSvgStyleAttributes(*this),
38 maX(SvgNumber(-10.0, SvgUnit::percent, true)),
39 maY(SvgNumber(-10.0, SvgUnit::percent, true)),
40 maWidth(SvgNumber(120.0, SvgUnit::percent, true)),
41 maHeight(SvgNumber(120.0, SvgUnit::percent, true)),
42 maMaskUnits(SvgUnits::objectBoundingBox),
43 maMaskContentUnits(SvgUnits::userSpaceOnUse)
47 SvgMaskNode::~SvgMaskNode()
51 const SvgStyleAttributes* SvgMaskNode::getSvgStyleAttributes() const
53 return &maSvgStyleAttributes;
56 void SvgMaskNode::parseAttribute(const OUString& rTokenName, SVGToken aSVGToken, const OUString& aContent)
58 // call parent
59 SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
61 // read style attributes
62 maSvgStyleAttributes.parseStyleAttribute(aSVGToken, aContent);
64 // parse own
65 switch(aSVGToken)
67 case SVGToken::Style:
69 readLocalCssStyle(aContent);
70 break;
72 case SVGToken::X:
74 SvgNumber aNum;
76 if(readSingleNumber(aContent, aNum))
78 maX = aNum;
80 break;
82 case SVGToken::Y:
84 SvgNumber aNum;
86 if(readSingleNumber(aContent, aNum))
88 maY = aNum;
90 break;
92 case SVGToken::Width:
94 SvgNumber aNum;
96 if(readSingleNumber(aContent, aNum))
98 if(aNum.isPositive())
100 maWidth = aNum;
103 break;
105 case SVGToken::Height:
107 SvgNumber aNum;
109 if(readSingleNumber(aContent, aNum))
111 if(aNum.isPositive())
113 maHeight = aNum;
116 break;
118 case SVGToken::Transform:
120 const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
122 if(!aMatrix.isIdentity())
124 setTransform(aMatrix);
126 break;
128 case SVGToken::MaskUnits:
130 if(!aContent.isEmpty())
132 if(o3tl::equalsIgnoreAsciiCase(o3tl::trim(aContent), commonStrings::aStrUserSpaceOnUse))
134 setMaskUnits(SvgUnits::userSpaceOnUse);
136 else if(o3tl::equalsIgnoreAsciiCase(o3tl::trim(aContent), commonStrings::aStrObjectBoundingBox))
138 setMaskUnits(SvgUnits::objectBoundingBox);
141 break;
143 case SVGToken::MaskContentUnits:
145 if(!aContent.isEmpty())
147 if(o3tl::equalsIgnoreAsciiCase(o3tl::trim(aContent), commonStrings::aStrUserSpaceOnUse))
149 setMaskContentUnits(SvgUnits::userSpaceOnUse);
151 else if(o3tl::equalsIgnoreAsciiCase(o3tl::trim(aContent), commonStrings::aStrObjectBoundingBox))
153 setMaskContentUnits(SvgUnits::objectBoundingBox);
156 break;
158 default:
160 break;
165 void SvgMaskNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer& rTarget, bool bReferenced) const
167 drawinglayer::primitive2d::Primitive2DContainer aNewTarget;
169 // decompose children
170 SvgNode::decomposeSvgNode(aNewTarget, bReferenced);
172 if(aNewTarget.empty())
173 return;
175 if(getTransform())
177 // create embedding group element with transformation
178 drawinglayer::primitive2d::Primitive2DReference xRef(
179 new drawinglayer::primitive2d::TransformPrimitive2D(
180 *getTransform(),
181 std::move(aNewTarget)));
183 aNewTarget = drawinglayer::primitive2d::Primitive2DContainer { xRef };
186 // append to current target
187 rTarget.append(aNewTarget);
190 void SvgMaskNode::apply(
191 drawinglayer::primitive2d::Primitive2DContainer& rTarget,
192 const std::optional<basegfx::B2DHomMatrix>& pTransform) const
194 if(rTarget.empty() || Display::None == getDisplay())
195 return;
197 drawinglayer::primitive2d::Primitive2DContainer aMaskTarget;
199 // get mask definition as primitives
200 decomposeSvgNode(aMaskTarget, true);
202 if(!aMaskTarget.empty())
204 // get range of content to be masked
205 const basegfx::B2DRange aContentRange(
206 rTarget.getB2DRange(
207 drawinglayer::geometry::ViewInformation2D()));
208 const double fContentWidth(aContentRange.getWidth());
209 const double fContentHeight(aContentRange.getHeight());
211 if(fContentWidth > 0.0 && fContentHeight > 0.0)
213 // create OffscreenBufferRange
214 basegfx::B2DRange aOffscreenBufferRange;
216 if (SvgUnits::objectBoundingBox == maMaskUnits)
218 // fractions or percentages of the bounding box of the element to which the mask is applied
219 const double fX(SvgUnit::percent == getX().getUnit() ? getX().getNumber() * 0.01 : getX().getNumber());
220 const double fY(SvgUnit::percent == getY().getUnit() ? getY().getNumber() * 0.01 : getY().getNumber());
221 const double fW(SvgUnit::percent == getWidth().getUnit() ? getWidth().getNumber() * 0.01 : getWidth().getNumber());
222 const double fH(SvgUnit::percent == getHeight().getUnit() ? getHeight().getNumber() * 0.01 : getHeight().getNumber());
224 aOffscreenBufferRange = basegfx::B2DRange(
225 aContentRange.getMinX() + (fX * fContentWidth),
226 aContentRange.getMinY() + (fY * fContentHeight),
227 aContentRange.getMinX() + ((fX + fW) * fContentWidth),
228 aContentRange.getMinY() + ((fY + fH) * fContentHeight));
230 else
232 const double fX(getX().isSet() ? getX().solve(*this, NumberType::xcoordinate) : 0.0);
233 const double fY(getY().isSet() ? getY().solve(*this, NumberType::ycoordinate) : 0.0);
235 aOffscreenBufferRange = basegfx::B2DRange(
238 fX + (getWidth().isSet() ? getWidth().solve(*this, NumberType::xcoordinate) : 0.0),
239 fY + (getHeight().isSet() ? getHeight().solve(*this, NumberType::ycoordinate) : 0.0));
242 if (SvgUnits::objectBoundingBox == maMaskContentUnits)
244 // mask is object-relative, embed in content transformation
245 drawinglayer::primitive2d::Primitive2DReference xTransform(
246 new drawinglayer::primitive2d::TransformPrimitive2D(
247 basegfx::utils::createScaleTranslateB2DHomMatrix(
248 aContentRange.getRange(),
249 aContentRange.getMinimum()),
250 std::move(aMaskTarget)));
252 aMaskTarget = drawinglayer::primitive2d::Primitive2DContainer { xTransform };
254 else // userSpaceOnUse
256 // #i124852#
257 if(pTransform)
259 drawinglayer::primitive2d::Primitive2DReference xTransform(
260 new drawinglayer::primitive2d::TransformPrimitive2D(
261 *pTransform,
262 std::move(aMaskTarget)));
264 aMaskTarget = drawinglayer::primitive2d::Primitive2DContainer { xTransform };
268 // embed content to a ModifiedColorPrimitive2D since the definitions
269 // how content is used as alpha is special for Svg
271 drawinglayer::primitive2d::Primitive2DReference xInverseMask(
272 new drawinglayer::primitive2d::ModifiedColorPrimitive2D(
273 std::move(aMaskTarget),
274 std::make_shared<basegfx::BColorModifier_luminance_to_alpha>()));
276 aMaskTarget = drawinglayer::primitive2d::Primitive2DContainer { xInverseMask };
279 // prepare new content
280 drawinglayer::primitive2d::Primitive2DReference xNewContent(
281 new drawinglayer::primitive2d::TransparencePrimitive2D(
282 std::move(rTarget),
283 std::move(aMaskTarget)));
285 // output up to now is defined by aContentRange and mask is oriented
286 // relative to it. It is possible that aOffscreenBufferRange defines
287 // a smaller area. In that case, embed to a mask primitive
288 if(!aOffscreenBufferRange.isInside(aContentRange))
290 xNewContent = new drawinglayer::primitive2d::MaskPrimitive2D(
291 basegfx::B2DPolyPolygon(
292 basegfx::utils::createPolygonFromRect(
293 aOffscreenBufferRange)),
294 drawinglayer::primitive2d::Primitive2DContainer { xNewContent });
297 // redefine target. Use TransparencePrimitive2D with created mask
298 // geometry
299 rTarget = drawinglayer::primitive2d::Primitive2DContainer { xNewContent };
301 else
303 // content is geometrically empty
304 rTarget.clear();
307 else
309 // An empty clipping path will completely clip away the element that had
310 // the clip-path property applied. (Svg spec)
311 rTarget.clear();
315 } // end of namespace svgio::svgreader
317 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */