1
package latexDraw.generators.svg;
3
import java.awt.geom.Point2D;
4
import java.util.Vector;
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;
17
* Defines a SVG generator for a polygon.<br>
19
* This file is part of LaTeXDraw.<br>
20
* Copyright (c) 2005-2008 Arnaud BLOUIN<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>
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>
32
* @author Arnaud BLOUIN<br>
35
public class LPolygonSVGGenerator extends LShapeSVGGenerator
38
public LPolygonSVGGenerator(LaTeXDrawPolygon f)
45
public LPolygonSVGGenerator(SVGPathElement elt)
47
super(new LaTeXDrawPolygon(true));
49
if(elt==null || !elt.isPolygon())
50
throw new IllegalArgumentException();
52
SVGPathSegList segs = elt.getSegList();
53
LaTeXDrawPolygon pol = (LaTeXDrawPolygon)getShape();
56
for(i=0, size=segs.size()-1; i<size; i++)
57
pol.addPoint(new LaTeXDrawPoint2D(((SVGPathSegLineto)segs.elementAt(i)).getPoint()));
61
setSVGParameters(elt);
62
applyTransformations(elt);
68
* Creates a polygon from an SVG polygon element.
69
* @param elt The source element.
72
public LPolygonSVGGenerator(SVGPolygonElement elt)
74
this(new LaTeXDrawPolygon(true));
76
LaTeXDrawPolygon pol = (LaTeXDrawPolygon)getShape();
78
setSVGPolygonParameters(elt);
79
applyTransformations(elt);
87
public LPolygonSVGGenerator(SVGGElement elt)
96
* Creates a polygon from a latexdraw-SVG element.
97
* @param elt The source element.
100
public LPolygonSVGGenerator(SVGGElement elt, boolean withTransformation)
102
this(new LaTeXDrawPolygon(true));
105
SVGElement elt2 = getLaTeXDrawElement(elt, null);
107
if(elt==null || elt2==null || !(elt2 instanceof SVGPolygonElement))
108
throw new IllegalArgumentException();
110
SVGPolygonElement main = (SVGPolygonElement)elt2;
111
setSVGLatexdrawParameters(elt);
112
setSVGParameters(main);
113
setSVGPolygonParameters(main);
115
((LaTeXDrawPolygon)getShape()).updateBorders();
116
setSVGShadowParameters(getLaTeXDrawElement(elt, LaTeXDrawNamespace.XML_TYPE_SHADOW));
117
setSVGDbleBordersParameters(getLaTeXDrawElement(elt, LaTeXDrawNamespace.XML_TYPE_DBLE_BORDERS));
119
if(withTransformation)
120
applyTransformations(elt);
125
private void setSVGPolygonParameters(SVGPolygonElement e)
128
Vector<Point2D> ptsPol = e.getPoints2D();
129
LaTeXDrawPolygon p = (LaTeXDrawPolygon)getShape();
132
throw new IllegalArgumentException();
134
for(Point2D pt : ptsPol)
135
p.addPoint(new LaTeXDrawPoint2D(pt));
143
public SVGElement toSVG(SVGDocument doc)
146
throw new IllegalArgumentException();
148
SVGElement root = new SVGGElement(doc);
149
SVGPolygonElement elt;
150
String points = "";//$NON-NLS-1$
151
LaTeXDrawPolygon p = (LaTeXDrawPolygon)shape;
153
root.setAttribute(LaTeXDrawNamespace.LATEXDRAW_NAMESPACE+':'+LaTeXDrawNamespace.XML_TYPE, LaTeXDrawNamespace.XML_TYPE_POLYGON);
154
root.setAttribute(SVGAttributes.SVG_ID, getSVGID());
156
for(LaTeXDrawPoint2D pt : p.getPoints())
157
points+=pt.x + "," + pt.y + " ";//$NON-NLS-1$//$NON-NLS-2$
159
if(shape.hasShadow())
161
SVGPolygonElement shad = new SVGPolygonElement(doc);
163
shad.setAttribute(SVGAttributes.SVG_POINTS, points);
164
setSVGShadowAttributes(shad, true);
165
root.appendChild(shad);
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);
175
elt = new SVGPolygonElement(doc);
176
elt.setAttribute(SVGAttributes.SVG_POINTS, points);
177
root.appendChild(elt);
178
setSVGAttributes(doc, elt, true);
179
setSVGRotationAttribute(root);
181
if(shape.hasDoubleBoundary())
183
SVGPolygonElement dblBord = new SVGPolygonElement(doc);
185
dblBord.setAttribute(SVGAttributes.SVG_POINTS, points);
186
setSVGDoubleBordersAttributes(dblBord);
187
root.appendChild(dblBord);