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

« back to all changes in this revision

Viewing changes to latexDraw/generators/svg/LPolygonSVGGenerator.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.generators.svg;
 
2
 
 
3
import java.awt.geom.Point2D;
 
4
import java.util.Vector;
 
5
 
 
6
import latexDraw.figures.LaTeXDrawPolygon;
 
7
import latexDraw.parsers.svg.SVGAttributes;
 
8
import latexDraw.parsers.svg.SVGDocument;
 
9
import latexDraw.parsers.svg.elements.*;
 
10
import latexDraw.parsers.svg.elements.path.SVGPathSegLineto;
 
11
import latexDraw.parsers.svg.elements.path.SVGPathSegList;
 
12
import latexDraw.psTricks.PSTricksConstants;
 
13
import latexDraw.util.LaTeXDrawNamespace;
 
14
import latexDraw.util.LaTeXDrawPoint2D;
 
15
 
 
16
/**
 
17
 * Defines a SVG generator for a polygon.<br>
 
18
 *<br>
 
19
 * This file is part of LaTeXDraw.<br>
 
20
 * Copyright (c) 2005-2008 Arnaud BLOUIN<br>
 
21
 *<br>
 
22
 *  LaTeXDraw is free software; you can redistribute it and/or modify
 
23
 *  it under the terms of the GNU General Public License as published by
 
24
 *  the Free Software Foundation; either version 2 of the License, or
 
25
 *  (at your option) any later version.<br>
 
26
 *<br>
 
27
 *  LaTeXDraw is distributed without any warranty; without even the 
 
28
 *  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
 
29
 *  PURPOSE. See the GNU General Public License for more details.<br>
 
30
 *<br>
 
31
 * 11/11/07<br>
 
32
 * @author Arnaud BLOUIN<br>
 
33
 * @version 0.1<br>
 
34
 */
 
35
public class LPolygonSVGGenerator extends LShapeSVGGenerator
 
36
{
 
37
 
 
38
        public LPolygonSVGGenerator(LaTeXDrawPolygon f)
 
39
        {
 
40
                super(f);
 
41
        }
 
42
 
 
43
        
 
44
        
 
45
        public LPolygonSVGGenerator(SVGPathElement elt)
 
46
        {
 
47
                super(new LaTeXDrawPolygon(true));
 
48
                
 
49
                if(elt==null || !elt.isPolygon())
 
50
                        throw new IllegalArgumentException();
 
51
                
 
52
                SVGPathSegList segs = elt.getSegList();
 
53
                LaTeXDrawPolygon pol = (LaTeXDrawPolygon)getShape();
 
54
                int i, size;
 
55
                
 
56
                for(i=0, size=segs.size()-1; i<size; i++)
 
57
                        pol.addPoint(new LaTeXDrawPoint2D(((SVGPathSegLineto)segs.elementAt(i)).getPoint()));
 
58
                
 
59
                pol.updateShape();
 
60
                pol.updateBorders();
 
61
                setSVGParameters(elt);
 
62
                applyTransformations(elt);
 
63
        }
 
64
        
 
65
        
 
66
        
 
67
        /**
 
68
         * Creates a polygon from an SVG polygon element.
 
69
         * @param elt The source element.
 
70
         * @since 2.0.0
 
71
         */
 
72
        public LPolygonSVGGenerator(SVGPolygonElement elt)
 
73
        {
 
74
                this(new LaTeXDrawPolygon(true));
 
75
                
 
76
                LaTeXDrawPolygon pol = (LaTeXDrawPolygon)getShape();
 
77
                
 
78
                setSVGPolygonParameters(elt);
 
79
                applyTransformations(elt);
 
80
                pol.updateShape();
 
81
                pol.updateBorders();
 
82
        }
 
83
        
 
84
        
 
85
        
 
86
        
 
87
        public LPolygonSVGGenerator(SVGGElement elt)
 
88
        {
 
89
                this(elt, true);
 
90
        }
 
91
        
 
92
        
 
93
        
 
94
        
 
95
        /**
 
96
         * Creates a polygon from a latexdraw-SVG element.
 
97
         * @param elt The source element.
 
98
         * @since 2.0.0
 
99
         */
 
100
        public LPolygonSVGGenerator(SVGGElement elt, boolean withTransformation)
 
101
        {
 
102
                this(new LaTeXDrawPolygon(true));
 
103
 
 
104
                setNumber(elt);
 
105
                SVGElement elt2 = getLaTeXDrawElement(elt, null);
 
106
                
 
107
                if(elt==null || elt2==null || !(elt2 instanceof SVGPolygonElement))
 
108
                        throw new IllegalArgumentException();
 
109
                
 
110
                SVGPolygonElement main = (SVGPolygonElement)elt2;
 
111
                setSVGLatexdrawParameters(elt);
 
112
                setSVGParameters(main);
 
113
                setSVGPolygonParameters(main);
 
114
                
 
115
                ((LaTeXDrawPolygon)getShape()).updateBorders();
 
116
                setSVGShadowParameters(getLaTeXDrawElement(elt, LaTeXDrawNamespace.XML_TYPE_SHADOW));
 
117
                setSVGDbleBordersParameters(getLaTeXDrawElement(elt, LaTeXDrawNamespace.XML_TYPE_DBLE_BORDERS));
 
118
                
 
119
                if(withTransformation)
 
120
                        applyTransformations(elt);
 
121
        }
 
122
        
 
123
        
 
124
        
 
125
        private void setSVGPolygonParameters(SVGPolygonElement e)
 
126
        {
 
127
                setSVGParameters(e);
 
128
                Vector<Point2D> ptsPol = e.getPoints2D();
 
129
                LaTeXDrawPolygon p = (LaTeXDrawPolygon)getShape();
 
130
                
 
131
                if(ptsPol==null)
 
132
                        throw new IllegalArgumentException();
 
133
                
 
134
                for(Point2D pt : ptsPol)
 
135
                        p.addPoint(new LaTeXDrawPoint2D(pt));
 
136
        }
 
137
        
 
138
        
 
139
        
 
140
        
 
141
        
 
142
        @Override
 
143
        public SVGElement toSVG(SVGDocument doc)
 
144
        {
 
145
                if(doc==null)
 
146
                        throw new IllegalArgumentException();
 
147
                
 
148
                SVGElement root = new SVGGElement(doc);
 
149
                SVGPolygonElement elt;
 
150
                String points = "";//$NON-NLS-1$
 
151
                LaTeXDrawPolygon p = (LaTeXDrawPolygon)shape;
 
152
                
 
153
        root.setAttribute(LaTeXDrawNamespace.LATEXDRAW_NAMESPACE+':'+LaTeXDrawNamespace.XML_TYPE, LaTeXDrawNamespace.XML_TYPE_POLYGON);
 
154
        root.setAttribute(SVGAttributes.SVG_ID, getSVGID());
 
155
        
 
156
        for(LaTeXDrawPoint2D pt : p.getPoints())
 
157
           points+=pt.x + "," + pt.y + " ";//$NON-NLS-1$//$NON-NLS-2$
 
158
        
 
159
        if(shape.hasShadow())
 
160
        {
 
161
                SVGPolygonElement shad = new SVGPolygonElement(doc);
 
162
                
 
163
                shad.setAttribute(SVGAttributes.SVG_POINTS, points);
 
164
                setSVGShadowAttributes(shad, true);
 
165
                root.appendChild(shad);
 
166
        }
 
167
        
 
168
        if(shape.hasShadow() && !shape.getLineStyle().equals(PSTricksConstants.LINE_NONE_STYLE))
 
169
        {// The background of the borders must be filled is there is a shadow.
 
170
                elt = new SVGPolygonElement(doc);
 
171
                elt.setAttribute(SVGAttributes.SVG_POINTS, points);
 
172
                setSVGBorderBackground(elt, root);
 
173
        }
 
174
        
 
175
        elt = new SVGPolygonElement(doc);
 
176
        elt.setAttribute(SVGAttributes.SVG_POINTS, points);
 
177
        root.appendChild(elt);
 
178
        setSVGAttributes(doc, elt, true);
 
179
        setSVGRotationAttribute(root);
 
180
        
 
181
        if(shape.hasDoubleBoundary())
 
182
        {
 
183
                SVGPolygonElement dblBord = new SVGPolygonElement(doc);
 
184
                
 
185
                dblBord.setAttribute(SVGAttributes.SVG_POINTS, points);
 
186
                setSVGDoubleBordersAttributes(dblBord);
 
187
                root.appendChild(dblBord);
 
188
        }
 
189
        
 
190
        return root;
 
191
        }
 
192
}