source: josm/trunk/src/com/kitfox/svg/Use.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: 7.6 KB
Line 
1/*
2 * LinearGradient.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, 1:54 AM
26 */
27
28package com.kitfox.svg;
29
30import com.kitfox.svg.xml.StyleAttribute;
31import java.awt.Graphics2D;
32import java.awt.Shape;
33import java.awt.geom.AffineTransform;
34import java.awt.geom.Rectangle2D;
35import java.net.URI;
36
37/**
38 * @author Mark McKay
39 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
40 */
41public class Use extends ShapeElement {
42
43 float x = 0f;
44 float y = 0f;
45 float width = 1f;
46 float height = 1f;
47
48 SVGElement href = null;
49
50 AffineTransform refXform;
51
52 /** Creates a new instance of LinearGradient */
53 public Use() {
54 }
55/*
56 public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
57 {
58 //Load style string
59 super.loaderStartElement(helper, attrs, parent);
60
61 String x = attrs.getValue("x");
62 String y = attrs.getValue("y");
63 String width = attrs.getValue("width");
64 String height = attrs.getValue("height");
65 String href = attrs.getValue("xlink:href");
66
67 if (x != null) this.x = (float)XMLParseUtil.parseRatio(x);
68 if (y != null) this.y = (float)XMLParseUtil.parseRatio(y);
69 if (width != null) this.width = (float)XMLParseUtil.parseRatio(width);
70 if (height != null) this.height = (float)XMLParseUtil.parseRatio(height);
71
72
73 if (href != null)
74 {
75 try {
76 URI src = getXMLBase().resolve(href);
77 this.href = helper.universe.getElement(src);
78 }
79 catch (Exception e)
80 {
81 e.printStackTrace();
82 }
83 }
84
85 //Determine use offset/scale
86 refXform = new AffineTransform();
87 refXform.translate(this.x, this.y);
88 refXform.scale(this.width, this.height);
89 }
90*/
91 protected void build() throws SVGException
92 {
93 super.build();
94
95 StyleAttribute sty = new StyleAttribute();
96
97 if (getPres(sty.setName("x"))) x = sty.getFloatValueWithUnits();
98
99 if (getPres(sty.setName("y"))) y = sty.getFloatValueWithUnits();
100
101 if (getPres(sty.setName("width"))) width = sty.getFloatValueWithUnits();
102
103 if (getPres(sty.setName("height"))) height = sty.getFloatValueWithUnits();
104
105 if (getPres(sty.setName("xlink:href")))
106 {
107 URI src = sty.getURIValue(getXMLBase());
108 href = diagram.getUniverse().getElement(src);
109 }
110
111 //Determine use offset/scale
112 refXform = new AffineTransform();
113 refXform.translate(this.x, this.y);
114 }
115
116 public void render(Graphics2D g) throws SVGException
117 {
118 beginLayer(g);
119
120 //AffineTransform oldXform = g.getTransform();
121 AffineTransform oldXform = g.getTransform();
122 g.transform(refXform);
123
124 if (href == null || !(href instanceof RenderableElement)) return;
125
126 RenderableElement rendEle = (RenderableElement)href;
127 rendEle.pushParentContext(this);
128 rendEle.render(g);
129 rendEle.popParentContext();
130
131 g.setTransform(oldXform);
132
133 finishLayer(g);
134 }
135
136 public Shape getShape()
137 {
138 if (href instanceof ShapeElement)
139 {
140 Shape shape = ((ShapeElement)href).getShape();
141 shape = refXform.createTransformedShape(shape);
142 shape = shapeToParent(shape);
143 return shape;
144 }
145
146 return null;
147 }
148
149 public Rectangle2D getBoundingBox() throws SVGException
150 {
151 if (href instanceof ShapeElement)
152 {
153 ShapeElement shapeEle = (ShapeElement)href;
154 shapeEle.pushParentContext(this);
155 Rectangle2D bounds = shapeEle.getBoundingBox();
156 shapeEle.popParentContext();
157
158 bounds = refXform.createTransformedShape(bounds).getBounds2D();
159 bounds = boundsToParent(bounds);
160
161 return bounds;
162 }
163
164 return null;
165 }
166
167 /**
168 * Updates all attributes in this diagram associated with a time event.
169 * Ie, all attributes with track information.
170 * @return - true if this node has changed state as a result of the time
171 * update
172 */
173 public boolean updateTime(double curTime) throws SVGException
174 {
175// if (trackManager.getNumTracks() == 0) return false;
176 boolean changeState = super.updateTime(curTime);
177
178 //Get current values for parameters
179 StyleAttribute sty = new StyleAttribute();
180 boolean shapeChange = false;
181
182 if (getPres(sty.setName("x")))
183 {
184 float newVal = sty.getFloatValueWithUnits();
185 if (newVal != x)
186 {
187 x = newVal;
188 shapeChange = true;
189 }
190 }
191
192 if (getPres(sty.setName("y")))
193 {
194 float newVal = sty.getFloatValueWithUnits();
195 if (newVal != y)
196 {
197 y = newVal;
198 shapeChange = true;
199 }
200 }
201
202 if (getPres(sty.setName("width")))
203 {
204 float newVal = sty.getFloatValueWithUnits();
205 if (newVal != width)
206 {
207 width = newVal;
208 shapeChange = true;
209 }
210 }
211
212 if (getPres(sty.setName("height")))
213 {
214 float newVal = sty.getFloatValueWithUnits();
215 if (newVal != height)
216 {
217 height = newVal;
218 shapeChange = true;
219 }
220 }
221
222 if (getPres(sty.setName("xlink:href")))
223 {
224 URI src = sty.getURIValue(getXMLBase());
225 SVGElement newVal = diagram.getUniverse().getElement(src);
226 if (newVal != href)
227 {
228 href = newVal;
229 shapeChange = true;
230 }
231 }
232/*
233 if (getPres(sty.setName("xlink:href")))
234 {
235 URI src = sty.getURIValue(getXMLBase());
236 href = diagram.getUniverse().getElement(src);
237 }
238
239 //Determine use offset/scale
240 refXform = new AffineTransform();
241 refXform.translate(this.x, this.y);
242 refXform.scale(this.width, this.height);
243*/
244 if (shapeChange)
245 {
246 build();
247 //Determine use offset/scale
248// refXform.setToTranslation(this.x, this.y);
249// refXform.scale(this.width, this.height);
250// return true;
251 }
252
253 return changeState || shapeChange;
254 }
255}
Note: See TracBrowser for help on using the repository browser.