1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_svgio.hxx"
24 
25 #include <svgio/svgreader/svglinenode.hxx>
26 #include <basegfx/polygon/b2dpolygon.hxx>
27 #include <basegfx/polygon/b2dpolygontools.hxx>
28 
29 //////////////////////////////////////////////////////////////////////////////
30 
31 namespace svgio
32 {
33     namespace svgreader
34     {
SvgLineNode(SvgDocument & rDocument,SvgNode * pParent)35         SvgLineNode::SvgLineNode(
36             SvgDocument& rDocument,
37             SvgNode* pParent)
38         :   SvgNode(SVGTokenLine, rDocument, pParent),
39             maSvgStyleAttributes(*this),
40             maX1(0),
41             maY1(0),
42             maX2(0),
43             maY2(0),
44             mpaTransform(0)
45         {
46         }
47 
~SvgLineNode()48         SvgLineNode::~SvgLineNode()
49         {
50             if(mpaTransform) delete mpaTransform;
51         }
52 
getSvgStyleAttributes() const53         const SvgStyleAttributes* SvgLineNode::getSvgStyleAttributes() const
54         {
55             static rtl::OUString aClassStr(rtl::OUString::createFromAscii("line"));
56 
57             return checkForCssStyle(aClassStr, maSvgStyleAttributes);
58         }
59 
parseAttribute(const rtl::OUString & rTokenName,SVGToken aSVGToken,const rtl::OUString & aContent)60         void SvgLineNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent)
61         {
62             // call parent
63             SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
64 
65             // read style attributes
66             maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent);
67 
68             // parse own
69             switch(aSVGToken)
70             {
71                 case SVGTokenStyle:
72                 {
73                     maSvgStyleAttributes.readStyle(aContent);
74                     break;
75                 }
76                 case SVGTokenX1:
77                 {
78                     SvgNumber aNum;
79 
80                     if(readSingleNumber(aContent, aNum))
81                     {
82                         setX1(aNum);
83                     }
84                     break;
85                 }
86                 case SVGTokenY1:
87                 {
88                     SvgNumber aNum;
89 
90                     if(readSingleNumber(aContent, aNum))
91                     {
92                         setY1(aNum);
93                     }
94                     break;
95                 }
96                 case SVGTokenX2:
97                 {
98                     SvgNumber aNum;
99 
100                     if(readSingleNumber(aContent, aNum))
101                     {
102                         setX2(aNum);
103                     }
104                     break;
105                 }
106                 case SVGTokenY2:
107                 {
108                     SvgNumber aNum;
109 
110                     if(readSingleNumber(aContent, aNum))
111                     {
112                         setY2(aNum);
113                     }
114                     break;
115                 }
116                 case SVGTokenTransform:
117                 {
118                     const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
119 
120                     if(!aMatrix.isIdentity())
121                     {
122                         setTransform(&aMatrix);
123                     }
124                     break;
125                 }
126                 default:
127                 {
128                     break;
129                 }
130             }
131         }
132 
decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence & rTarget,bool) const133         void SvgLineNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool /*bReferenced*/) const
134         {
135             const SvgStyleAttributes* pStyle = getSvgStyleAttributes();
136 
137             if(pStyle)
138             {
139                 const basegfx::B2DPoint X(
140                     getX1().isSet() ? getX1().solve(*this, xcoordinate) : 0.0,
141                     getY1().isSet() ? getY1().solve(*this, ycoordinate) : 0.0);
142                 const basegfx::B2DPoint Y(
143                     getX2().isSet() ? getX2().solve(*this, xcoordinate) : 0.0,
144                     getY2().isSet() ? getY2().solve(*this, ycoordinate) : 0.0);
145 
146                 if(!X.equal(Y))
147                 {
148                     basegfx::B2DPolygon aPath;
149 
150                     aPath.append(X);
151                     aPath.append(Y);
152 
153                     drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
154 
155                     pStyle->add_path(basegfx::B2DPolyPolygon(aPath), aNewTarget, 0);
156 
157                     if(aNewTarget.hasElements())
158                     {
159                         pStyle->add_postProcess(rTarget, aNewTarget, getTransform());
160                     }
161                 }
162             }
163         }
164     } // end of namespace svgreader
165 } // end of namespace svgio
166 
167 //////////////////////////////////////////////////////////////////////////////
168 // eof
169