1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
,
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(SVGToken aSVGToken
, const OUString
& aContent
)
59 SvgNode::parseAttribute(aSVGToken
, aContent
);
61 // read style attributes
62 maSvgStyleAttributes
.parseStyleAttribute(aSVGToken
, aContent
);
69 readLocalCssStyle(aContent
);
76 if(readSingleNumber(aContent
, aNum
))
86 if(readSingleNumber(aContent
, aNum
))
96 if(readSingleNumber(aContent
, aNum
))
105 case SVGToken::Height
:
109 if(readSingleNumber(aContent
, aNum
))
111 if(aNum
.isPositive())
118 case SVGToken::Transform
:
120 const basegfx::B2DHomMatrix
aMatrix(readTransform(aContent
, *this));
122 if(!aMatrix
.isIdentity())
124 setTransform(aMatrix
);
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
);
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
);
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())
177 // create embedding group element with transformation
178 aNewTarget
= drawinglayer::primitive2d::Primitive2DContainer
{
179 new drawinglayer::primitive2d::TransformPrimitive2D(
181 std::move(aNewTarget
))
185 // append to current target
186 rTarget
.append(aNewTarget
);
189 void SvgMaskNode::apply(
190 drawinglayer::primitive2d::Primitive2DContainer
& rTarget
,
191 const std::optional
<basegfx::B2DHomMatrix
>& pTransform
) const
193 if(rTarget
.empty() || Display::None
== getDisplay())
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(
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
));
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 aMaskTarget
= drawinglayer::primitive2d::Primitive2DContainer
{
245 new drawinglayer::primitive2d::TransformPrimitive2D(
246 basegfx::utils::createScaleTranslateB2DHomMatrix(
247 aContentRange
.getRange(),
248 aContentRange
.getMinimum()),
249 std::move(aMaskTarget
))
252 else // userSpaceOnUse
257 aMaskTarget
= drawinglayer::primitive2d::Primitive2DContainer
{
258 new drawinglayer::primitive2d::TransformPrimitive2D(
260 std::move(aMaskTarget
))
265 // embed content to a ModifiedColorPrimitive2D since the definitions
266 // how content is used as alpha is special for Svg
268 aMaskTarget
= drawinglayer::primitive2d::Primitive2DContainer
{
269 new drawinglayer::primitive2d::ModifiedColorPrimitive2D(
270 std::move(aMaskTarget
),
271 std::make_shared
<basegfx::BColorModifier_luminance_to_alpha
>())
275 // prepare new content
276 drawinglayer::primitive2d::Primitive2DReference
xNewContent(
277 new drawinglayer::primitive2d::TransparencePrimitive2D(
279 std::move(aMaskTarget
)));
281 // output up to now is defined by aContentRange and mask is oriented
282 // relative to it. It is possible that aOffscreenBufferRange defines
283 // a smaller area. In that case, embed to a mask primitive
284 if(!aOffscreenBufferRange
.isInside(aContentRange
))
286 xNewContent
= new drawinglayer::primitive2d::MaskPrimitive2D(
287 basegfx::B2DPolyPolygon(
288 basegfx::utils::createPolygonFromRect(
289 aOffscreenBufferRange
)),
290 drawinglayer::primitive2d::Primitive2DContainer
{ xNewContent
});
293 // redefine target. Use TransparencePrimitive2D with created mask
295 rTarget
= drawinglayer::primitive2d::Primitive2DContainer
{ xNewContent
};
299 // content is geometrically empty
305 // An empty clipping path will completely clip away the element that had
306 // the clip-path property applied. (Svg spec)
311 } // end of namespace svgio::svgreader
313 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */