1*ddde725dSArmin Le Grand /**************************************************************
2*ddde725dSArmin Le Grand  *
3*ddde725dSArmin Le Grand  * Licensed to the Apache Software Foundation (ASF) under one
4*ddde725dSArmin Le Grand  * or more contributor license agreements.  See the NOTICE file
5*ddde725dSArmin Le Grand  * distributed with this work for additional information
6*ddde725dSArmin Le Grand  * regarding copyright ownership.  The ASF licenses this file
7*ddde725dSArmin Le Grand  * to you under the Apache License, Version 2.0 (the
8*ddde725dSArmin Le Grand  * "License"); you may not use this file except in compliance
9*ddde725dSArmin Le Grand  * with the License.  You may obtain a copy of the License at
10*ddde725dSArmin Le Grand  *
11*ddde725dSArmin Le Grand  *   http://www.apache.org/licenses/LICENSE-2.0
12*ddde725dSArmin Le Grand  *
13*ddde725dSArmin Le Grand  * Unless required by applicable law or agreed to in writing,
14*ddde725dSArmin Le Grand  * software distributed under the License is distributed on an
15*ddde725dSArmin Le Grand  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ddde725dSArmin Le Grand  * KIND, either express or implied.  See the License for the
17*ddde725dSArmin Le Grand  * specific language governing permissions and limitations
18*ddde725dSArmin Le Grand  * under the License.
19*ddde725dSArmin Le Grand  *
20*ddde725dSArmin Le Grand  *************************************************************/
21*ddde725dSArmin Le Grand 
22*ddde725dSArmin Le Grand // MARKER(update_precomp.py): autogen include statement, do not remove
23*ddde725dSArmin Le Grand #include "precompiled_svgio.hxx"
24*ddde725dSArmin Le Grand 
25*ddde725dSArmin Le Grand #include <svgio/svgreader/svgmasknode.hxx>
26*ddde725dSArmin Le Grand #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
27*ddde725dSArmin Le Grand #include <drawinglayer/primitive2d/transparenceprimitive2d.hxx>
28*ddde725dSArmin Le Grand #include <basegfx/matrix/b2dhommatrixtools.hxx>
29*ddde725dSArmin Le Grand #include <drawinglayer/geometry/viewinformation2d.hxx>
30*ddde725dSArmin Le Grand #include <drawinglayer/primitive2d/modifiedcolorprimitive2d.hxx>
31*ddde725dSArmin Le Grand #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
32*ddde725dSArmin Le Grand #include <basegfx/polygon/b2dpolygontools.hxx>
33*ddde725dSArmin Le Grand #include <basegfx/polygon/b2dpolygon.hxx>
34*ddde725dSArmin Le Grand 
35*ddde725dSArmin Le Grand //////////////////////////////////////////////////////////////////////////////
36*ddde725dSArmin Le Grand 
37*ddde725dSArmin Le Grand namespace svgio
38*ddde725dSArmin Le Grand {
39*ddde725dSArmin Le Grand     namespace svgreader
40*ddde725dSArmin Le Grand     {
41*ddde725dSArmin Le Grand         SvgMaskNode::SvgMaskNode(
42*ddde725dSArmin Le Grand             SvgDocument& rDocument,
43*ddde725dSArmin Le Grand             SvgNode* pParent)
44*ddde725dSArmin Le Grand         :   SvgNode(SVGTokenMask, rDocument, pParent),
45*ddde725dSArmin Le Grand             maSvgStyleAttributes(*this),
46*ddde725dSArmin Le Grand             maX(SvgNumber(-10.0, Unit_percent, true)),
47*ddde725dSArmin Le Grand             maY(SvgNumber(-10.0, Unit_percent, true)),
48*ddde725dSArmin Le Grand             maWidth(SvgNumber(120.0, Unit_percent, true)),
49*ddde725dSArmin Le Grand             maHeight(SvgNumber(120.0, Unit_percent, true)),
50*ddde725dSArmin Le Grand             mpaTransform(0),
51*ddde725dSArmin Le Grand             maMaskUnits(objectBoundingBox),
52*ddde725dSArmin Le Grand             maMaskContentUnits(userSpaceOnUse)
53*ddde725dSArmin Le Grand         {
54*ddde725dSArmin Le Grand         }
55*ddde725dSArmin Le Grand 
56*ddde725dSArmin Le Grand         SvgMaskNode::~SvgMaskNode()
57*ddde725dSArmin Le Grand         {
58*ddde725dSArmin Le Grand             if(mpaTransform) delete mpaTransform;
59*ddde725dSArmin Le Grand         }
60*ddde725dSArmin Le Grand 
61*ddde725dSArmin Le Grand         const SvgStyleAttributes* SvgMaskNode::getSvgStyleAttributes() const
62*ddde725dSArmin Le Grand         {
63*ddde725dSArmin Le Grand             static rtl::OUString aClassStr(rtl::OUString::createFromAscii("mask"));
64*ddde725dSArmin Le Grand             maSvgStyleAttributes.checkForCssStyle(aClassStr);
65*ddde725dSArmin Le Grand 
66*ddde725dSArmin Le Grand             return &maSvgStyleAttributes;
67*ddde725dSArmin Le Grand         }
68*ddde725dSArmin Le Grand 
69*ddde725dSArmin Le Grand         void SvgMaskNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent)
70*ddde725dSArmin Le Grand         {
71*ddde725dSArmin Le Grand             // call parent
72*ddde725dSArmin Le Grand             SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
73*ddde725dSArmin Le Grand 
74*ddde725dSArmin Le Grand             // read style attributes
75*ddde725dSArmin Le Grand             maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent);
76*ddde725dSArmin Le Grand 
77*ddde725dSArmin Le Grand             // parse own
78*ddde725dSArmin Le Grand             switch(aSVGToken)
79*ddde725dSArmin Le Grand             {
80*ddde725dSArmin Le Grand                 case SVGTokenStyle:
81*ddde725dSArmin Le Grand                 {
82*ddde725dSArmin Le Grand                     maSvgStyleAttributes.readStyle(aContent);
83*ddde725dSArmin Le Grand                     break;
84*ddde725dSArmin Le Grand                 }
85*ddde725dSArmin Le Grand                 case SVGTokenX:
86*ddde725dSArmin Le Grand                 {
87*ddde725dSArmin Le Grand                     SvgNumber aNum;
88*ddde725dSArmin Le Grand 
89*ddde725dSArmin Le Grand                     if(readSingleNumber(aContent, aNum))
90*ddde725dSArmin Le Grand                     {
91*ddde725dSArmin Le Grand                         setX(aNum);
92*ddde725dSArmin Le Grand                     }
93*ddde725dSArmin Le Grand                     break;
94*ddde725dSArmin Le Grand                 }
95*ddde725dSArmin Le Grand                 case SVGTokenY:
96*ddde725dSArmin Le Grand                 {
97*ddde725dSArmin Le Grand                     SvgNumber aNum;
98*ddde725dSArmin Le Grand 
99*ddde725dSArmin Le Grand                     if(readSingleNumber(aContent, aNum))
100*ddde725dSArmin Le Grand                     {
101*ddde725dSArmin Le Grand                         setY(aNum);
102*ddde725dSArmin Le Grand                     }
103*ddde725dSArmin Le Grand                     break;
104*ddde725dSArmin Le Grand                 }
105*ddde725dSArmin Le Grand                 case SVGTokenWidth:
106*ddde725dSArmin Le Grand                 {
107*ddde725dSArmin Le Grand                     SvgNumber aNum;
108*ddde725dSArmin Le Grand 
109*ddde725dSArmin Le Grand                     if(readSingleNumber(aContent, aNum))
110*ddde725dSArmin Le Grand                     {
111*ddde725dSArmin Le Grand                         if(aNum.isPositive())
112*ddde725dSArmin Le Grand                         {
113*ddde725dSArmin Le Grand                             setWidth(aNum);
114*ddde725dSArmin Le Grand                         }
115*ddde725dSArmin Le Grand                     }
116*ddde725dSArmin Le Grand                     break;
117*ddde725dSArmin Le Grand                 }
118*ddde725dSArmin Le Grand                 case SVGTokenHeight:
119*ddde725dSArmin Le Grand                 {
120*ddde725dSArmin Le Grand                     SvgNumber aNum;
121*ddde725dSArmin Le Grand 
122*ddde725dSArmin Le Grand                     if(readSingleNumber(aContent, aNum))
123*ddde725dSArmin Le Grand                     {
124*ddde725dSArmin Le Grand                         if(aNum.isPositive())
125*ddde725dSArmin Le Grand                         {
126*ddde725dSArmin Le Grand                             setHeight(aNum);
127*ddde725dSArmin Le Grand                         }
128*ddde725dSArmin Le Grand                     }
129*ddde725dSArmin Le Grand                     break;
130*ddde725dSArmin Le Grand                 }
131*ddde725dSArmin Le Grand                 case SVGTokenTransform:
132*ddde725dSArmin Le Grand                 {
133*ddde725dSArmin Le Grand                     const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
134*ddde725dSArmin Le Grand 
135*ddde725dSArmin Le Grand                     if(!aMatrix.isIdentity())
136*ddde725dSArmin Le Grand                     {
137*ddde725dSArmin Le Grand                         setTransform(&aMatrix);
138*ddde725dSArmin Le Grand                     }
139*ddde725dSArmin Le Grand                     break;
140*ddde725dSArmin Le Grand                 }
141*ddde725dSArmin Le Grand                 case SVGTokenMaskUnits:
142*ddde725dSArmin Le Grand                 {
143*ddde725dSArmin Le Grand                     if(aContent.getLength())
144*ddde725dSArmin Le Grand                     {
145*ddde725dSArmin Le Grand                         if(aContent.match(commonStrings::aStrUserSpaceOnUse, 0))
146*ddde725dSArmin Le Grand                         {
147*ddde725dSArmin Le Grand                             setMaskUnits(userSpaceOnUse);
148*ddde725dSArmin Le Grand                         }
149*ddde725dSArmin Le Grand                         else if(aContent.match(commonStrings::aStrObjectBoundingBox, 0))
150*ddde725dSArmin Le Grand                         {
151*ddde725dSArmin Le Grand                             setMaskUnits(objectBoundingBox);
152*ddde725dSArmin Le Grand                         }
153*ddde725dSArmin Le Grand                     }
154*ddde725dSArmin Le Grand                     break;
155*ddde725dSArmin Le Grand                 }
156*ddde725dSArmin Le Grand                 case SVGTokenMaskContentUnits:
157*ddde725dSArmin Le Grand                 {
158*ddde725dSArmin Le Grand                     if(aContent.getLength())
159*ddde725dSArmin Le Grand                     {
160*ddde725dSArmin Le Grand                         if(aContent.match(commonStrings::aStrUserSpaceOnUse, 0))
161*ddde725dSArmin Le Grand                         {
162*ddde725dSArmin Le Grand                             setMaskContentUnits(userSpaceOnUse);
163*ddde725dSArmin Le Grand                         }
164*ddde725dSArmin Le Grand                         else if(aContent.match(commonStrings::aStrObjectBoundingBox, 0))
165*ddde725dSArmin Le Grand                         {
166*ddde725dSArmin Le Grand                             setMaskContentUnits(objectBoundingBox);
167*ddde725dSArmin Le Grand                         }
168*ddde725dSArmin Le Grand                     }
169*ddde725dSArmin Le Grand                     break;
170*ddde725dSArmin Le Grand                 }
171*ddde725dSArmin Le Grand             }
172*ddde725dSArmin Le Grand         }
173*ddde725dSArmin Le Grand 
174*ddde725dSArmin Le Grand         void SvgMaskNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool bReferenced) const
175*ddde725dSArmin Le Grand         {
176*ddde725dSArmin Le Grand             drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
177*ddde725dSArmin Le Grand 
178*ddde725dSArmin Le Grand             // decompose childs
179*ddde725dSArmin Le Grand             SvgNode::decomposeSvgNode(aNewTarget, bReferenced);
180*ddde725dSArmin Le Grand 
181*ddde725dSArmin Le Grand             if(aNewTarget.hasElements())
182*ddde725dSArmin Le Grand             {
183*ddde725dSArmin Le Grand                 if(getTransform())
184*ddde725dSArmin Le Grand                 {
185*ddde725dSArmin Le Grand                     // create embedding group element with transformation
186*ddde725dSArmin Le Grand                     const drawinglayer::primitive2d::Primitive2DReference xRef(
187*ddde725dSArmin Le Grand                         new drawinglayer::primitive2d::TransformPrimitive2D(
188*ddde725dSArmin Le Grand                             *getTransform(),
189*ddde725dSArmin Le Grand                             aNewTarget));
190*ddde725dSArmin Le Grand 
191*ddde725dSArmin Le Grand                     aNewTarget = drawinglayer::primitive2d::Primitive2DSequence(&xRef, 1);
192*ddde725dSArmin Le Grand                 }
193*ddde725dSArmin Le Grand 
194*ddde725dSArmin Le Grand                 // append to current target
195*ddde725dSArmin Le Grand                 drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget, aNewTarget);
196*ddde725dSArmin Le Grand             }
197*ddde725dSArmin Le Grand         }
198*ddde725dSArmin Le Grand 
199*ddde725dSArmin Le Grand         void SvgMaskNode::apply(drawinglayer::primitive2d::Primitive2DSequence& rTarget) const
200*ddde725dSArmin Le Grand         {
201*ddde725dSArmin Le Grand             if(rTarget.hasElements())
202*ddde725dSArmin Le Grand             {
203*ddde725dSArmin Le Grand                 drawinglayer::primitive2d::Primitive2DSequence aMaskTarget;
204*ddde725dSArmin Le Grand 
205*ddde725dSArmin Le Grand                 // get mask definition as primitives
206*ddde725dSArmin Le Grand                 decomposeSvgNode(aMaskTarget, true);
207*ddde725dSArmin Le Grand 
208*ddde725dSArmin Le Grand                 if(aMaskTarget.hasElements())
209*ddde725dSArmin Le Grand                 {
210*ddde725dSArmin Le Grand                     // get range of content to be masked
211*ddde725dSArmin Le Grand                     const basegfx::B2DRange aContentRange(
212*ddde725dSArmin Le Grand                         drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence(
213*ddde725dSArmin Le Grand                             rTarget,
214*ddde725dSArmin Le Grand                             drawinglayer::geometry::ViewInformation2D()));
215*ddde725dSArmin Le Grand                     const double fContentWidth(aContentRange.getWidth());
216*ddde725dSArmin Le Grand                     const double fContentHeight(aContentRange.getHeight());
217*ddde725dSArmin Le Grand 
218*ddde725dSArmin Le Grand                     if(fContentWidth > 0.0 && fContentHeight > 0.0)
219*ddde725dSArmin Le Grand                     {
220*ddde725dSArmin Le Grand                         // create OffscreenBufferRange
221*ddde725dSArmin Le Grand                         basegfx::B2DRange aOffscreenBufferRange;
222*ddde725dSArmin Le Grand 
223*ddde725dSArmin Le Grand                         if(objectBoundingBox == getMaskUnits())
224*ddde725dSArmin Le Grand                         {
225*ddde725dSArmin Le Grand                             // fractions or percentages of the bounding box of the element to which the mask is applied
226*ddde725dSArmin Le Grand                             const double fX(Unit_percent == getX().getUnit() ? getX().getNumber() * 0.01 : getX().getNumber());
227*ddde725dSArmin Le Grand                             const double fY(Unit_percent == getY().getUnit() ? getY().getNumber() * 0.01 : getY().getNumber());
228*ddde725dSArmin Le Grand                             const double fW(Unit_percent == getWidth().getUnit() ? getWidth().getNumber() * 0.01 : getWidth().getNumber());
229*ddde725dSArmin Le Grand                             const double fH(Unit_percent == getHeight().getUnit() ? getHeight().getNumber() * 0.01 : getHeight().getNumber());
230*ddde725dSArmin Le Grand 
231*ddde725dSArmin Le Grand                             aOffscreenBufferRange = basegfx::B2DRange(
232*ddde725dSArmin Le Grand                                 aContentRange.getMinX() + (fX * fContentWidth),
233*ddde725dSArmin Le Grand                                 aContentRange.getMinY() + (fY * fContentHeight),
234*ddde725dSArmin Le Grand                                 aContentRange.getMinX() + ((fX + fW) * fContentWidth),
235*ddde725dSArmin Le Grand                                 aContentRange.getMinY() + ((fY + fH) * fContentHeight));
236*ddde725dSArmin Le Grand                         }
237*ddde725dSArmin Le Grand                         else
238*ddde725dSArmin Le Grand                         {
239*ddde725dSArmin Le Grand                             const double fX(getX().isSet() ? getX().solve(*this, xcoordinate) : 0.0);
240*ddde725dSArmin Le Grand                             const double fY(getY().isSet() ? getY().solve(*this, ycoordinate) : 0.0);
241*ddde725dSArmin Le Grand 
242*ddde725dSArmin Le Grand                             aOffscreenBufferRange = basegfx::B2DRange(
243*ddde725dSArmin Le Grand                                 fX,
244*ddde725dSArmin Le Grand                                 fY,
245*ddde725dSArmin Le Grand                                 fX + (getWidth().isSet() ? getWidth().solve(*this, xcoordinate) : 0.0),
246*ddde725dSArmin Le Grand                                 fY + (getHeight().isSet() ? getHeight().solve(*this, ycoordinate) : 0.0));
247*ddde725dSArmin Le Grand                         }
248*ddde725dSArmin Le Grand 
249*ddde725dSArmin Le Grand                         if(objectBoundingBox == getMaskContentUnits())
250*ddde725dSArmin Le Grand                         {
251*ddde725dSArmin Le Grand                             // mask is object-relative, embed in content transformation
252*ddde725dSArmin Le Grand                             const drawinglayer::primitive2d::Primitive2DReference xTransform(
253*ddde725dSArmin Le Grand                                 new drawinglayer::primitive2d::TransformPrimitive2D(
254*ddde725dSArmin Le Grand                                     basegfx::tools::createScaleTranslateB2DHomMatrix(
255*ddde725dSArmin Le Grand                                         aContentRange.getRange(),
256*ddde725dSArmin Le Grand                                         aContentRange.getMinimum()),
257*ddde725dSArmin Le Grand                                     aMaskTarget));
258*ddde725dSArmin Le Grand 
259*ddde725dSArmin Le Grand                             aMaskTarget = drawinglayer::primitive2d::Primitive2DSequence(&xTransform, 1);
260*ddde725dSArmin Le Grand                         }
261*ddde725dSArmin Le Grand 
262*ddde725dSArmin Le Grand                         // embed content to a ModifiedColorPrimitive2D since the definitions
263*ddde725dSArmin Le Grand                         // how content is used as alpha is special for Svg
264*ddde725dSArmin Le Grand                         {
265*ddde725dSArmin Le Grand                             const drawinglayer::primitive2d::Primitive2DReference xInverseMask(
266*ddde725dSArmin Le Grand                                 new drawinglayer::primitive2d::ModifiedColorPrimitive2D(
267*ddde725dSArmin Le Grand                                     aMaskTarget,
268*ddde725dSArmin Le Grand                                     basegfx::BColorModifier(
269*ddde725dSArmin Le Grand                                         basegfx::BColor(0.0, 0.0, 0.0),
270*ddde725dSArmin Le Grand                                         0.5,
271*ddde725dSArmin Le Grand                                         basegfx::BCOLORMODIFYMODE_LUMINANCE_TO_ALPHA)));
272*ddde725dSArmin Le Grand 
273*ddde725dSArmin Le Grand                             aMaskTarget = drawinglayer::primitive2d::Primitive2DSequence(&xInverseMask, 1);
274*ddde725dSArmin Le Grand                         }
275*ddde725dSArmin Le Grand 
276*ddde725dSArmin Le Grand                         // prepare new content
277*ddde725dSArmin Le Grand                         drawinglayer::primitive2d::Primitive2DReference xNewContent(
278*ddde725dSArmin Le Grand                             new drawinglayer::primitive2d::TransparencePrimitive2D(
279*ddde725dSArmin Le Grand                                 rTarget,
280*ddde725dSArmin Le Grand                                 aMaskTarget));
281*ddde725dSArmin Le Grand 
282*ddde725dSArmin Le Grand                         // output up to now is defined by aContentRange and mask is oriented
283*ddde725dSArmin Le Grand                         // relative to it. It is possible that aOffscreenBufferRange defines
284*ddde725dSArmin Le Grand                         // a smaller area. In that case, embed to a mask primitive
285*ddde725dSArmin Le Grand                         if(!aOffscreenBufferRange.isInside(aContentRange))
286*ddde725dSArmin Le Grand                         {
287*ddde725dSArmin Le Grand                             xNewContent = new drawinglayer::primitive2d::MaskPrimitive2D(
288*ddde725dSArmin Le Grand                                 basegfx::B2DPolyPolygon(
289*ddde725dSArmin Le Grand                                     basegfx::tools::createPolygonFromRect(
290*ddde725dSArmin Le Grand                                         aOffscreenBufferRange)),
291*ddde725dSArmin Le Grand                                 drawinglayer::primitive2d::Primitive2DSequence(&xNewContent, 1));
292*ddde725dSArmin Le Grand                         }
293*ddde725dSArmin Le Grand 
294*ddde725dSArmin Le Grand                         // redefine target. Use TransparencePrimitive2D with created mask
295*ddde725dSArmin Le Grand                         // geometry
296*ddde725dSArmin Le Grand                         rTarget = drawinglayer::primitive2d::Primitive2DSequence(&xNewContent, 1);
297*ddde725dSArmin Le Grand                     }
298*ddde725dSArmin Le Grand                     else
299*ddde725dSArmin Le Grand                     {
300*ddde725dSArmin Le Grand                         // content is geometrically empty
301*ddde725dSArmin Le Grand                         rTarget.realloc(0);
302*ddde725dSArmin Le Grand                     }
303*ddde725dSArmin Le Grand                 }
304*ddde725dSArmin Le Grand                 else
305*ddde725dSArmin Le Grand                 {
306*ddde725dSArmin Le Grand                     // An empty clipping path will completely clip away the element that had
307*ddde725dSArmin Le Grand                     // the �clip-path� property applied. (Svg spec)
308*ddde725dSArmin Le Grand                     rTarget.realloc(0);
309*ddde725dSArmin Le Grand                 }
310*ddde725dSArmin Le Grand             }
311*ddde725dSArmin Le Grand         }
312*ddde725dSArmin Le Grand 
313*ddde725dSArmin Le Grand     } // end of namespace svgreader
314*ddde725dSArmin Le Grand } // end of namespace svgio
315*ddde725dSArmin Le Grand 
316*ddde725dSArmin Le Grand //////////////////////////////////////////////////////////////////////////////
317*ddde725dSArmin Le Grand // eof
318