bump product version to 7.2.5.1
[LibreOffice.git] / svgio / source / svgreader / svgmasknode.cxx
blob7f3ab9a772ca6f5e278241d019dc917ccffe0397
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>
30 namespace svgio::svgreader
32 SvgMaskNode::SvgMaskNode(
33 SvgDocument& rDocument,
34 SvgNode* pParent)
35 : SvgNode(SVGToken::Mask, rDocument, pParent),
36 maSvgStyleAttributes(*this),
37 maX(SvgNumber(-10.0, SvgUnit::percent, true)),
38 maY(SvgNumber(-10.0, SvgUnit::percent, true)),
39 maWidth(SvgNumber(120.0, SvgUnit::percent, true)),
40 maHeight(SvgNumber(120.0, SvgUnit::percent, true)),
41 maMaskUnits(SvgUnits::objectBoundingBox),
42 maMaskContentUnits(SvgUnits::userSpaceOnUse)
46 SvgMaskNode::~SvgMaskNode()
50 const SvgStyleAttributes* SvgMaskNode::getSvgStyleAttributes() const
52 return &maSvgStyleAttributes;
55 void SvgMaskNode::parseAttribute(const OUString& rTokenName, SVGToken aSVGToken, const OUString& aContent)
57 // call parent
58 SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
60 // read style attributes
61 maSvgStyleAttributes.parseStyleAttribute(aSVGToken, aContent, false);
63 // parse own
64 switch(aSVGToken)
66 case SVGToken::Style:
68 readLocalCssStyle(aContent);
69 break;
71 case SVGToken::X:
73 SvgNumber aNum;
75 if(readSingleNumber(aContent, aNum))
77 maX = aNum;
79 break;
81 case SVGToken::Y:
83 SvgNumber aNum;
85 if(readSingleNumber(aContent, aNum))
87 maY = aNum;
89 break;
91 case SVGToken::Width:
93 SvgNumber aNum;
95 if(readSingleNumber(aContent, aNum))
97 if(aNum.isPositive())
99 maWidth = aNum;
102 break;
104 case SVGToken::Height:
106 SvgNumber aNum;
108 if(readSingleNumber(aContent, aNum))
110 if(aNum.isPositive())
112 maHeight = aNum;
115 break;
117 case SVGToken::Transform:
119 const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
121 if(!aMatrix.isIdentity())
123 setTransform(&aMatrix);
125 break;
127 case SVGToken::MaskUnits:
129 if(!aContent.isEmpty())
131 if(aContent.match(commonStrings::aStrUserSpaceOnUse))
133 setMaskUnits(SvgUnits::userSpaceOnUse);
135 else if(aContent.match(commonStrings::aStrObjectBoundingBox))
137 setMaskUnits(SvgUnits::objectBoundingBox);
140 break;
142 case SVGToken::MaskContentUnits:
144 if(!aContent.isEmpty())
146 if(aContent.match(commonStrings::aStrUserSpaceOnUse))
148 setMaskContentUnits(SvgUnits::userSpaceOnUse);
150 else if(aContent.match(commonStrings::aStrObjectBoundingBox))
152 setMaskContentUnits(SvgUnits::objectBoundingBox);
155 break;
157 default:
159 break;
164 void SvgMaskNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer& rTarget, bool bReferenced) const
166 drawinglayer::primitive2d::Primitive2DContainer aNewTarget;
168 // decompose children
169 SvgNode::decomposeSvgNode(aNewTarget, bReferenced);
171 if(aNewTarget.empty())
172 return;
174 if(getTransform())
176 // create embedding group element with transformation
177 const drawinglayer::primitive2d::Primitive2DReference xRef(
178 new drawinglayer::primitive2d::TransformPrimitive2D(
179 *getTransform(),
180 aNewTarget));
182 aNewTarget = drawinglayer::primitive2d::Primitive2DContainer { xRef };
185 // append to current target
186 rTarget.append(aNewTarget);
189 void SvgMaskNode::apply(
190 drawinglayer::primitive2d::Primitive2DContainer& rTarget,
191 const basegfx::B2DHomMatrix* pTransform) const
193 if(rTarget.empty() || Display::None == getDisplay())
194 return;
196 drawinglayer::primitive2d::Primitive2DContainer aMaskTarget;
198 // get mask definition as primitives
199 decomposeSvgNode(aMaskTarget, true);
201 if(!aMaskTarget.empty())
203 // get range of content to be masked
204 const basegfx::B2DRange aContentRange(
205 rTarget.getB2DRange(
206 drawinglayer::geometry::ViewInformation2D()));
207 const double fContentWidth(aContentRange.getWidth());
208 const double fContentHeight(aContentRange.getHeight());
210 if(fContentWidth > 0.0 && fContentHeight > 0.0)
212 // create OffscreenBufferRange
213 basegfx::B2DRange aOffscreenBufferRange;
215 if (SvgUnits::objectBoundingBox == maMaskUnits)
217 // fractions or percentages of the bounding box of the element to which the mask is applied
218 const double fX(SvgUnit::percent == getX().getUnit() ? getX().getNumber() * 0.01 : getX().getNumber());
219 const double fY(SvgUnit::percent == getY().getUnit() ? getY().getNumber() * 0.01 : getY().getNumber());
220 const double fW(SvgUnit::percent == getWidth().getUnit() ? getWidth().getNumber() * 0.01 : getWidth().getNumber());
221 const double fH(SvgUnit::percent == getHeight().getUnit() ? getHeight().getNumber() * 0.01 : getHeight().getNumber());
223 aOffscreenBufferRange = basegfx::B2DRange(
224 aContentRange.getMinX() + (fX * fContentWidth),
225 aContentRange.getMinY() + (fY * fContentHeight),
226 aContentRange.getMinX() + ((fX + fW) * fContentWidth),
227 aContentRange.getMinY() + ((fY + fH) * fContentHeight));
229 else
231 const double fX(getX().isSet() ? getX().solve(*this, NumberType::xcoordinate) : 0.0);
232 const double fY(getY().isSet() ? getY().solve(*this, NumberType::ycoordinate) : 0.0);
234 aOffscreenBufferRange = basegfx::B2DRange(
237 fX + (getWidth().isSet() ? getWidth().solve(*this, NumberType::xcoordinate) : 0.0),
238 fY + (getHeight().isSet() ? getHeight().solve(*this, NumberType::ycoordinate) : 0.0));
241 if (SvgUnits::objectBoundingBox == maMaskContentUnits)
243 // mask is object-relative, embed in content transformation
244 const drawinglayer::primitive2d::Primitive2DReference xTransform(
245 new drawinglayer::primitive2d::TransformPrimitive2D(
246 basegfx::utils::createScaleTranslateB2DHomMatrix(
247 aContentRange.getRange(),
248 aContentRange.getMinimum()),
249 aMaskTarget));
251 aMaskTarget = drawinglayer::primitive2d::Primitive2DContainer { xTransform };
253 else // userSpaceOnUse
255 // #i124852#
256 if(pTransform)
258 const drawinglayer::primitive2d::Primitive2DReference xTransform(
259 new drawinglayer::primitive2d::TransformPrimitive2D(
260 *pTransform,
261 aMaskTarget));
263 aMaskTarget = drawinglayer::primitive2d::Primitive2DContainer { xTransform };
267 // embed content to a ModifiedColorPrimitive2D since the definitions
268 // how content is used as alpha is special for Svg
270 const drawinglayer::primitive2d::Primitive2DReference xInverseMask(
271 new drawinglayer::primitive2d::ModifiedColorPrimitive2D(
272 aMaskTarget,
273 std::make_shared<basegfx::BColorModifier_luminance_to_alpha>()));
275 aMaskTarget = drawinglayer::primitive2d::Primitive2DContainer { xInverseMask };
278 // prepare new content
279 drawinglayer::primitive2d::Primitive2DReference xNewContent(
280 new drawinglayer::primitive2d::TransparencePrimitive2D(
281 rTarget,
282 aMaskTarget));
284 // output up to now is defined by aContentRange and mask is oriented
285 // relative to it. It is possible that aOffscreenBufferRange defines
286 // a smaller area. In that case, embed to a mask primitive
287 if(!aOffscreenBufferRange.isInside(aContentRange))
289 xNewContent = new drawinglayer::primitive2d::MaskPrimitive2D(
290 basegfx::B2DPolyPolygon(
291 basegfx::utils::createPolygonFromRect(
292 aOffscreenBufferRange)),
293 drawinglayer::primitive2d::Primitive2DContainer { xNewContent });
296 // redefine target. Use TransparencePrimitive2D with created mask
297 // geometry
298 rTarget = drawinglayer::primitive2d::Primitive2DContainer { xNewContent };
300 else
302 // content is geometrically empty
303 rTarget.clear();
306 else
308 // An empty clipping path will completely clip away the element that had
309 // the clip-path property applied. (Svg spec)
310 rTarget.clear();
314 } // end of namespace svgio::svgreader
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */