~ubuntu-branches/debian/squeeze/latexdraw/squeeze

« back to all changes in this revision

Viewing changes to latexDraw/parsers/svg/elements/SVGPathElement.java

  • Committer: Bazaar Package Importer
  • Author(s): Stuart Prescott
  • Date: 2009-05-03 23:49:35 UTC
  • Revision ID: james.westby@ubuntu.com-20090503234935-cls7n48x018g0vk2
Tags: upstream-2.0.2+1
ImportĀ upstreamĀ versionĀ 2.0.2+1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package latexDraw.parsers.svg.elements;
 
2
 
 
3
import latexDraw.parsers.svg.MalformedSVGDocument;
 
4
import latexDraw.parsers.svg.SVGAttributes;
 
5
import latexDraw.parsers.svg.SVGDocument;
 
6
import latexDraw.parsers.svg.SVGElements;
 
7
import latexDraw.parsers.svg.elements.path.*;
 
8
 
 
9
import org.w3c.dom.Node;
 
10
 
 
11
/**
 
12
 * Defines the SVG tag <code>path</code>.<br>
 
13
 *<br>
 
14
 * This file is part of LaTeXDraw.<br>
 
15
 * Copyright (c) 2005-2008 Arnaud BLOUIN<br>
 
16
 *<br>
 
17
 *  LaTeXDraw is free software; you can redistribute it and/or modify
 
18
 *  it under the terms of the GNU General Public License as published by
 
19
 *  the Free Software Foundation; either version 2 of the License, or
 
20
 *  (at your option) any later version.<br>
 
21
 *<br>
 
22
 *  LaTeXDraw is distributed without any warranty; without even the 
 
23
 *  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
 
24
 *  PURPOSE. See the GNU General Public License for more details.<br>
 
25
 *<br>
 
26
 * 10/06/07<br>
 
27
 * @author Arnaud BLOUIN<br>
 
28
 * @version 0.1<br>
 
29
 * @since 0.1
 
30
 */
 
31
public class SVGPathElement extends SVGElement
 
32
{
 
33
        /**
 
34
         * See {@link SVGElement#SVGElement(Node, SVGElement)}.
 
35
         * @throws MalformedSVGDocument If the element is not well formed.
 
36
         */
 
37
        public SVGPathElement(Node n, SVGElement p) throws MalformedSVGDocument
 
38
        {
 
39
                super(n, p);
 
40
        }
 
41
 
 
42
        
 
43
        
 
44
        /**
 
45
         * Creates an empty SVG path element.
 
46
         * @param owner The owner document.
 
47
         * @since 0.1
 
48
         */
 
49
        public SVGPathElement(SVGDocument owner)
 
50
        {
 
51
                super(owner);
 
52
                
 
53
                setAttribute(SVGAttributes.SVG_D, "");//$NON-NLS-1$
 
54
                setNodeName(SVGElements.SVG_PATH);
 
55
        }
 
56
        
 
57
        
 
58
 
 
59
        /**
 
60
         * @return True if the path is a unique line.
 
61
         * @since 0.1
 
62
         */
 
63
        public boolean isLine()
 
64
        {
 
65
                SVGPathSegList segList = getSegList();
 
66
                
 
67
                return segList.size()==2 && segList.firstElement() instanceof SVGPathSegMoveto && 
 
68
                                segList.elementAt(1) instanceof SVGPathSegLineto ; 
 
69
        }
 
70
        
 
71
        
 
72
        
 
73
        
 
74
        /**
 
75
         * @return True if the path is composed of lines and has a 'close path' segment at the end.
 
76
         * @since 0.1
 
77
         */
 
78
        public boolean isLines()
 
79
        {
 
80
                SVGPathSegList segList = getSegList();
 
81
                
 
82
                if(segList.size()<3 || !(segList.firstElement() instanceof SVGPathSegMoveto))
 
83
                        return false;
 
84
                
 
85
                boolean ok = true;
 
86
                int i, size;
 
87
                
 
88
                for(i=1, size=segList.size()-1; i<size && ok; i++)
 
89
                        if(!(segList.elementAt(i) instanceof SVGPathSegLineto))
 
90
                                ok = false;
 
91
                
 
92
                return ok;
 
93
        }
 
94
        
 
95
        
 
96
        
 
97
        /**
 
98
         * @return True if the path is composed of lines and has a 'close path' segment at the end.
 
99
         * @since 0.1
 
100
         */
 
101
        public boolean isPolygon()
 
102
        {
 
103
                SVGPathSegList segList = getSegList();
 
104
                
 
105
                if(segList.isEmpty() || !(segList.firstElement() instanceof SVGPathSegMoveto))
 
106
                        return false;
 
107
                
 
108
                boolean ok = true;
 
109
                int i, size;
 
110
                
 
111
                for(i=1, size=segList.size()-1; i<size && ok; i++)
 
112
                        if(!(segList.elementAt(i) instanceof SVGPathSegLineto))
 
113
                                ok = false;
 
114
                
 
115
                if(!(segList.lastElement() instanceof SVGPathSegClosePath))
 
116
                        ok = false;
 
117
                
 
118
                return ok;
 
119
        }
 
120
        
 
121
        
 
122
        
 
123
        /**
 
124
         * The definition of the outline of a shape.
 
125
         * @return The path data (in postscript) from the segList attribute.
 
126
         * @since 0.1
 
127
         */
 
128
        public String getPathData()
 
129
        {
 
130
                return getAttribute(getUsablePrefix()+SVGAttributes.SVG_D);
 
131
        }
 
132
 
 
133
 
 
134
        @Override
 
135
        public boolean checkAttributes()
 
136
        {
 
137
                return getPathData()!=null;
 
138
        }
 
139
 
 
140
 
 
141
 
 
142
        @Override
 
143
        public boolean enableRendering()
 
144
        {
 
145
                return getPathData().length()>0;
 
146
        }
 
147
 
 
148
 
 
149
        /**
 
150
         * @return the segList.
 
151
         * @since 0.1
 
152
         */
 
153
        public SVGPathSegList getSegList()
 
154
        {
 
155
                return new SVGPathSegList(getPathData());
 
156
        }
 
157
 
 
158
 
 
159
 
 
160
        /**
 
161
         * Sets the path data.
 
162
         * @param path The path to set.
 
163
         * @since 2.0.0
 
164
         */
 
165
        public void setPathData(SVGPathSegList path)
 
166
        {
 
167
                if(path!=null)
 
168
                        setAttribute(SVGAttributes.SVG_D, path.toString());
 
169
        }
 
170
}