Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / svgio / source / svgreader / svgmasknode.cxx
blobbc1ac3243e8578255f4e5348efb34545adc56374
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 <svgio/svgreader/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 //////////////////////////////////////////////////////////////////////////////
32 namespace svgio
34 namespace svgreader
36 SvgMaskNode::SvgMaskNode(
37 SvgDocument& rDocument,
38 SvgNode* pParent)
39 : SvgNode(SVGTokenMask, rDocument, pParent),
40 maSvgStyleAttributes(*this),
41 maX(SvgNumber(-10.0, Unit_percent, true)),
42 maY(SvgNumber(-10.0, Unit_percent, true)),
43 maWidth(SvgNumber(120.0, Unit_percent, true)),
44 maHeight(SvgNumber(120.0, Unit_percent, true)),
45 mpaTransform(0),
46 maMaskUnits(objectBoundingBox),
47 maMaskContentUnits(userSpaceOnUse)
51 SvgMaskNode::~SvgMaskNode()
53 if(mpaTransform) delete mpaTransform;
56 const SvgStyleAttributes* SvgMaskNode::getSvgStyleAttributes() const
58 return &maSvgStyleAttributes;
61 void SvgMaskNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent)
63 // call parent
64 SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
66 // read style attributes
67 maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent);
69 // parse own
70 switch(aSVGToken)
72 case SVGTokenStyle:
74 maSvgStyleAttributes.readStyle(aContent);
75 break;
77 case SVGTokenX:
79 SvgNumber aNum;
81 if(readSingleNumber(aContent, aNum))
83 setX(aNum);
85 break;
87 case SVGTokenY:
89 SvgNumber aNum;
91 if(readSingleNumber(aContent, aNum))
93 setY(aNum);
95 break;
97 case SVGTokenWidth:
99 SvgNumber aNum;
101 if(readSingleNumber(aContent, aNum))
103 if(aNum.isPositive())
105 setWidth(aNum);
108 break;
110 case SVGTokenHeight:
112 SvgNumber aNum;
114 if(readSingleNumber(aContent, aNum))
116 if(aNum.isPositive())
118 setHeight(aNum);
121 break;
123 case SVGTokenTransform:
125 const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
127 if(!aMatrix.isIdentity())
129 setTransform(&aMatrix);
131 break;
133 case SVGTokenMaskUnits:
135 if(aContent.getLength())
137 if(aContent.match(commonStrings::aStrUserSpaceOnUse, 0))
139 setMaskUnits(userSpaceOnUse);
141 else if(aContent.match(commonStrings::aStrObjectBoundingBox, 0))
143 setMaskUnits(objectBoundingBox);
146 break;
148 case SVGTokenMaskContentUnits:
150 if(aContent.getLength())
152 if(aContent.match(commonStrings::aStrUserSpaceOnUse, 0))
154 setMaskContentUnits(userSpaceOnUse);
156 else if(aContent.match(commonStrings::aStrObjectBoundingBox, 0))
158 setMaskContentUnits(objectBoundingBox);
161 break;
163 default:
165 break;
170 void SvgMaskNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool bReferenced) const
172 drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
174 // decompose childs
175 SvgNode::decomposeSvgNode(aNewTarget, bReferenced);
177 if(aNewTarget.hasElements())
179 if(getTransform())
181 // create embedding group element with transformation
182 const drawinglayer::primitive2d::Primitive2DReference xRef(
183 new drawinglayer::primitive2d::TransformPrimitive2D(
184 *getTransform(),
185 aNewTarget));
187 aNewTarget = drawinglayer::primitive2d::Primitive2DSequence(&xRef, 1);
190 // append to current target
191 drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget, aNewTarget);
195 void SvgMaskNode::apply(drawinglayer::primitive2d::Primitive2DSequence& rTarget) const
197 if(rTarget.hasElements())
199 drawinglayer::primitive2d::Primitive2DSequence aMaskTarget;
201 // get mask definition as primitives
202 decomposeSvgNode(aMaskTarget, true);
204 if(aMaskTarget.hasElements())
206 // get range of content to be masked
207 const basegfx::B2DRange aContentRange(
208 drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence(
209 rTarget,
210 drawinglayer::geometry::ViewInformation2D()));
211 const double fContentWidth(aContentRange.getWidth());
212 const double fContentHeight(aContentRange.getHeight());
214 if(fContentWidth > 0.0 && fContentHeight > 0.0)
216 // create OffscreenBufferRange
217 basegfx::B2DRange aOffscreenBufferRange;
219 if(objectBoundingBox == getMaskUnits())
221 // fractions or percentages of the bounding box of the element to which the mask is applied
222 const double fX(Unit_percent == getX().getUnit() ? getX().getNumber() * 0.01 : getX().getNumber());
223 const double fY(Unit_percent == getY().getUnit() ? getY().getNumber() * 0.01 : getY().getNumber());
224 const double fW(Unit_percent == getWidth().getUnit() ? getWidth().getNumber() * 0.01 : getWidth().getNumber());
225 const double fH(Unit_percent == getHeight().getUnit() ? getHeight().getNumber() * 0.01 : getHeight().getNumber());
227 aOffscreenBufferRange = basegfx::B2DRange(
228 aContentRange.getMinX() + (fX * fContentWidth),
229 aContentRange.getMinY() + (fY * fContentHeight),
230 aContentRange.getMinX() + ((fX + fW) * fContentWidth),
231 aContentRange.getMinY() + ((fY + fH) * fContentHeight));
233 else
235 const double fX(getX().isSet() ? getX().solve(*this, xcoordinate) : 0.0);
236 const double fY(getY().isSet() ? getY().solve(*this, ycoordinate) : 0.0);
238 aOffscreenBufferRange = basegfx::B2DRange(
241 fX + (getWidth().isSet() ? getWidth().solve(*this, xcoordinate) : 0.0),
242 fY + (getHeight().isSet() ? getHeight().solve(*this, ycoordinate) : 0.0));
245 if(objectBoundingBox == getMaskContentUnits())
247 // mask is object-relative, embed in content transformation
248 const drawinglayer::primitive2d::Primitive2DReference xTransform(
249 new drawinglayer::primitive2d::TransformPrimitive2D(
250 basegfx::tools::createScaleTranslateB2DHomMatrix(
251 aContentRange.getRange(),
252 aContentRange.getMinimum()),
253 aMaskTarget));
255 aMaskTarget = drawinglayer::primitive2d::Primitive2DSequence(&xTransform, 1);
258 // embed content to a ModifiedColorPrimitive2D since the definitions
259 // how content is used as alpha is special for Svg
261 const drawinglayer::primitive2d::Primitive2DReference xInverseMask(
262 new drawinglayer::primitive2d::ModifiedColorPrimitive2D(
263 aMaskTarget,
264 basegfx::BColorModifier(
265 basegfx::BColor(0.0, 0.0, 0.0),
266 0.5,
267 basegfx::BCOLORMODIFYMODE_LUMINANCE_TO_ALPHA)));
269 aMaskTarget = drawinglayer::primitive2d::Primitive2DSequence(&xInverseMask, 1);
272 // prepare new content
273 drawinglayer::primitive2d::Primitive2DReference xNewContent(
274 new drawinglayer::primitive2d::TransparencePrimitive2D(
275 rTarget,
276 aMaskTarget));
278 // output up to now is defined by aContentRange and mask is oriented
279 // relative to it. It is possible that aOffscreenBufferRange defines
280 // a smaller area. In that case, embed to a mask primitive
281 if(!aOffscreenBufferRange.isInside(aContentRange))
283 xNewContent = new drawinglayer::primitive2d::MaskPrimitive2D(
284 basegfx::B2DPolyPolygon(
285 basegfx::tools::createPolygonFromRect(
286 aOffscreenBufferRange)),
287 drawinglayer::primitive2d::Primitive2DSequence(&xNewContent, 1));
290 // redefine target. Use TransparencePrimitive2D with created mask
291 // geometry
292 rTarget = drawinglayer::primitive2d::Primitive2DSequence(&xNewContent, 1);
294 else
296 // content is geometrically empty
297 rTarget.realloc(0);
300 else
302 // An empty clipping path will completely clip away the element that had
303 // the ‘clip-path’ property applied. (Svg spec)
304 rTarget.realloc(0);
309 } // end of namespace svgreader
310 } // end of namespace svgio
312 //////////////////////////////////////////////////////////////////////////////
313 // eof
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */