source: josm/trunk/src/com/kitfox/svg/Path.java@ 4256

Last change on this file since 4256 was 4256, checked in by bastiK, 13 years ago

see #6560 - basic svg support, includes kitfox svgsalamander, r 98, patched

File size: 4.6 KB
Line 
1/*
2 * Rect.java
3 *
4 *
5 * The Salamander Project - 2D and 3D graphics libraries in Java
6 * Copyright (C) 2004 Mark McKay
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
23 * projects can be found at http://www.kitfox.com
24 *
25 * Created on January 26, 2004, 5:25 PM
26 */
27
28package com.kitfox.svg;
29
30import com.kitfox.svg.xml.StyleAttribute;
31import java.awt.*;
32import java.awt.geom.*;
33
34import com.kitfox.svg.pathcmd.*;
35import com.kitfox.svg.xml.*;
36import org.xml.sax.*;
37
38//import org.apache.batik.ext.awt.geom.ExtendedGeneralPath;
39
40/**
41 * @author Mark McKay
42 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
43 */
44public class Path extends ShapeElement {
45
46// PathCommand[] commands = null;
47
48 int fillRule = GeneralPath.WIND_NON_ZERO;
49 String d = "";
50// ExtendedGeneralPath path;
51 GeneralPath path;
52
53 /** Creates a new instance of Rect */
54 public Path() {
55 }
56
57 /*
58 public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
59 {
60 //Load style string
61 super.loaderStartElement(helper, attrs, parent);
62
63 StyleAttribute styleAttrib = getStyle("fill-rule");
64 String fillRule = (styleAttrib == null) ? "nonzero" : styleAttrib.getStringValue();
65
66 String d = attrs.getValue("d");
67 path = buildPath(d, fillRule.equals("evenodd") ? GeneralPath.WIND_EVEN_ODD : GeneralPath.WIND_NON_ZERO);
68 }
69 */
70
71 protected void build() throws SVGException
72 {
73 super.build();
74
75 StyleAttribute sty = new StyleAttribute();
76
77
78 String fillRuleStrn = (getStyle(sty.setName("fill-rule"))) ? sty.getStringValue() : "nonzero";
79 fillRule = fillRuleStrn.equals("evenodd") ? GeneralPath.WIND_EVEN_ODD : GeneralPath.WIND_NON_ZERO;
80
81// String d = "";
82 if (getPres(sty.setName("d"))) d = sty.getStringValue();
83
84//System.err.println(d);
85
86 path = buildPath(d, fillRule);
87
88//System.err.println(d);
89 }
90
91 public void render(Graphics2D g) throws SVGException
92 {
93 beginLayer(g);
94 renderShape(g, path);
95 finishLayer(g);
96 }
97
98 public Shape getShape()
99 {
100 return shapeToParent(path);
101 }
102
103 public Rectangle2D getBoundingBox() throws SVGException
104 {
105 return boundsToParent(includeStrokeInBounds(path.getBounds2D()));
106 }
107
108 /**
109 * Updates all attributes in this diagram associated with a time event.
110 * Ie, all attributes with track information.
111 * @return - true if this node has changed state as a result of the time
112 * update
113 */
114 public boolean updateTime(double curTime) throws SVGException
115 {
116// if (trackManager.getNumTracks() == 0) return false;
117 boolean changeState = super.updateTime(curTime);
118
119 //Get current values for parameters
120 StyleAttribute sty = new StyleAttribute();
121 boolean shapeChange = false;
122
123 if (getStyle(sty.setName("fill-rule")))
124 {
125 int newVal = sty.getStringValue().equals("evenodd")
126 ? GeneralPath.WIND_EVEN_ODD
127 : GeneralPath.WIND_NON_ZERO;
128 if (newVal != fillRule)
129 {
130 fillRule = newVal;
131 changeState = true;
132 }
133 }
134
135 if (getPres(sty.setName("d")))
136 {
137 String newVal = sty.getStringValue();
138 if (!newVal.equals(d))
139 {
140 d = newVal;
141 shapeChange = true;
142 }
143 }
144
145 if (shapeChange)
146 {
147 build();
148// path = buildPath(d, fillRule);
149// return true;
150 }
151
152 return changeState || shapeChange;
153 }
154}
Note: See TracBrowser for help on using the repository browser.