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