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     {
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 
48         SvgLineNode::~SvgLineNode()
49         {
50             if(mpaTransform) delete mpaTransform;
51         }
52 
53         const SvgStyleAttributes* SvgLineNode::getSvgStyleAttributes() const
54         {
55             static rtl::OUString aClassStr(rtl::OUString::createFromAscii("line"));
56             maSvgStyleAttributes.checkForCssStyle(aClassStr);
57 
58             return &maSvgStyleAttributes;
59         }
60 
61         void SvgLineNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent)
62         {
63             // call parent
64             SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
65 
66             // read style attributes
67             maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent);
68 
69             // parse own
70             switch(aSVGToken)
71             {
72                 case SVGTokenStyle:
73                 {
74                     maSvgStyleAttributes.readStyle(aContent);
75                     break;
76                 }
77                 case SVGTokenX1:
78                 {
79                     SvgNumber aNum;
80 
81                     if(readSingleNumber(aContent, aNum))
82                     {
83                         setX1(aNum);
84                     }
85                     break;
86                 }
87                 case SVGTokenY1:
88                 {
89                     SvgNumber aNum;
90 
91                     if(readSingleNumber(aContent, aNum))
92                     {
93                         setY1(aNum);
94                     }
95                     break;
96                 }
97                 case SVGTokenX2:
98                 {
99                     SvgNumber aNum;
100 
101                     if(readSingleNumber(aContent, aNum))
102                     {
103                         setX2(aNum);
104                     }
105                     break;
106                 }
107                 case SVGTokenY2:
108                 {
109                     SvgNumber aNum;
110 
111                     if(readSingleNumber(aContent, aNum))
112                     {
113                         setY2(aNum);
114                     }
115                     break;
116                 }
117                 case SVGTokenTransform:
118                 {
119                     const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
120 
121                     if(!aMatrix.isIdentity())
122                     {
123                         setTransform(&aMatrix);
124                     }
125                     break;
126                 }
127             }
128         }
129 
130         void SvgLineNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool bReferenced) const
131         {
132             const SvgStyleAttributes* pStyle = getSvgStyleAttributes();
133 
134             if(pStyle)
135             {
136                 const basegfx::B2DPoint X(
137                     getX1().isSet() ? getX1().solve(*this, xcoordinate) : 0.0,
138                     getY1().isSet() ? getY1().solve(*this, ycoordinate) : 0.0);
139                 const basegfx::B2DPoint Y(
140                     getX2().isSet() ? getX2().solve(*this, xcoordinate) : 0.0,
141                     getY2().isSet() ? getY2().solve(*this, ycoordinate) : 0.0);
142 
143                 if(!X.equal(Y))
144                 {
145                     basegfx::B2DPolygon aPath;
146 
147                     aPath.append(X);
148                     aPath.append(Y);
149 
150                     drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
151 
152                     pStyle->add_path(basegfx::B2DPolyPolygon(aPath), aNewTarget);
153 
154                     if(aNewTarget.hasElements())
155                     {
156                         pStyle->add_postProcess(rTarget, aNewTarget, getTransform());
157                     }
158                 }
159             }
160         }
161     } // end of namespace svgreader
162 } // end of namespace svgio
163 
164 //////////////////////////////////////////////////////////////////////////////
165 // eof
166