source: josm/trunk/src/com/kitfox/svg/Use.java@ 6522

Last change on this file since 6522 was 6002, checked in by Don-vip, 13 years ago

fix #8742 - update svgsalamander to release 0.1.18+patch (fix bug SVGSALAMANDER-26) -> allow to open more SVG files

File size: 7.4 KB
RevLine 
[4256]1/*
[6002]2 * SVG Salamander
3 * Copyright (c) 2004, Mark McKay
4 * All rights reserved.
[4256]5 *
[6002]6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
[4256]9 *
[6002]10 * - Redistributions of source code must retain the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials
16 * provided with the distribution.
[4256]17 *
[6002]18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
32 * projects can be found at http://www.kitfox.com
[4256]33 *
34 * Created on January 26, 2004, 1:54 AM
35 */
36package com.kitfox.svg;
37
38import com.kitfox.svg.xml.StyleAttribute;
39import java.awt.Graphics2D;
40import java.awt.Shape;
41import java.awt.geom.AffineTransform;
42import java.awt.geom.Rectangle2D;
43import java.net.URI;
44
45/**
46 * @author Mark McKay
47 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
48 */
[6002]49public class Use extends ShapeElement
50{
51 public static final String TAG_NAME = "use";
52
[4256]53 float x = 0f;
54 float y = 0f;
55 float width = 1f;
56 float height = 1f;
[6002]57// SVGElement href = null;
58 URI href = null;
[4256]59 AffineTransform refXform;
60
[6002]61 /**
62 * Creates a new instance of LinearGradient
63 */
64 public Use()
65 {
[4256]66 }
[6002]67
68 public String getTagName()
[4256]69 {
[6002]70 return TAG_NAME;
71 }
[4256]72
[6002]73 protected void build() throws SVGException
74 {
75 super.build();
[4256]76
[6002]77 StyleAttribute sty = new StyleAttribute();
[4256]78
[6002]79 if (getPres(sty.setName("x")))
80 {
81 x = sty.getFloatValueWithUnits();
82 }
[4256]83
[6002]84 if (getPres(sty.setName("y")))
[4256]85 {
[6002]86 y = sty.getFloatValueWithUnits();
[4256]87 }
88
[6002]89 if (getPres(sty.setName("width")))
90 {
91 width = sty.getFloatValueWithUnits();
92 }
[4256]93
[6002]94 if (getPres(sty.setName("height")))
95 {
96 height = sty.getFloatValueWithUnits();
97 }
[4256]98
99 if (getPres(sty.setName("xlink:href")))
100 {
101 URI src = sty.getURIValue(getXMLBase());
[6002]102 href = src;
103// href = diagram.getUniverse().getElement(src);
[4256]104 }
[6002]105
[4256]106 //Determine use offset/scale
107 refXform = new AffineTransform();
108 refXform.translate(this.x, this.y);
109 }
[6002]110
[4256]111 public void render(Graphics2D g) throws SVGException
112 {
113 beginLayer(g);
114
115 //AffineTransform oldXform = g.getTransform();
116 AffineTransform oldXform = g.getTransform();
117 g.transform(refXform);
118
[6002]119 SVGElement ref = diagram.getUniverse().getElement(href);
[4256]120
[6002]121 if (ref == null || !(ref instanceof RenderableElement))
122 {
123 return;
124 }
125
126 RenderableElement rendEle = (RenderableElement) ref;
[4256]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 {
[6002]138 SVGElement ref = diagram.getUniverse().getElement(href);
139 if (ref instanceof ShapeElement)
[4256]140 {
[6002]141 Shape shape = ((ShapeElement) ref).getShape();
[4256]142 shape = refXform.createTransformedShape(shape);
143 shape = shapeToParent(shape);
144 return shape;
145 }
146
147 return null;
148 }
149
150 public Rectangle2D getBoundingBox() throws SVGException
151 {
[6002]152 SVGElement ref = diagram.getUniverse().getElement(href);
153 if (ref instanceof ShapeElement)
[4256]154 {
[6002]155 ShapeElement shapeEle = (ShapeElement) ref;
[4256]156 shapeEle.pushParentContext(this);
157 Rectangle2D bounds = shapeEle.getBoundingBox();
158 shapeEle.popParentContext();
[6002]159
[4256]160 bounds = refXform.createTransformedShape(bounds).getBounds2D();
161 bounds = boundsToParent(bounds);
162
163 return bounds;
164 }
165
166 return null;
167 }
168
169 /**
[6002]170 * Updates all attributes in this diagram associated with a time event. Ie,
171 * all attributes with track information.
172 *
[4256]173 * @return - true if this node has changed state as a result of the time
174 * update
175 */
176 public boolean updateTime(double curTime) throws SVGException
177 {
178// if (trackManager.getNumTracks() == 0) return false;
179 boolean changeState = super.updateTime(curTime);
180
181 //Get current values for parameters
182 StyleAttribute sty = new StyleAttribute();
183 boolean shapeChange = false;
[6002]184
[4256]185 if (getPres(sty.setName("x")))
186 {
187 float newVal = sty.getFloatValueWithUnits();
188 if (newVal != x)
189 {
190 x = newVal;
191 shapeChange = true;
192 }
193 }
194
195 if (getPres(sty.setName("y")))
196 {
197 float newVal = sty.getFloatValueWithUnits();
198 if (newVal != y)
199 {
200 y = newVal;
201 shapeChange = true;
202 }
203 }
204
205 if (getPres(sty.setName("width")))
206 {
207 float newVal = sty.getFloatValueWithUnits();
208 if (newVal != width)
209 {
210 width = newVal;
211 shapeChange = true;
212 }
213 }
214
215 if (getPres(sty.setName("height")))
216 {
217 float newVal = sty.getFloatValueWithUnits();
218 if (newVal != height)
219 {
220 height = newVal;
221 shapeChange = true;
222 }
223 }
[6002]224
[4256]225 if (getPres(sty.setName("xlink:href")))
226 {
227 URI src = sty.getURIValue(getXMLBase());
[6002]228// SVGElement newVal = diagram.getUniverse().getElement(src);
229 if (!src.equals(href))
[4256]230 {
[6002]231 href = src;
[4256]232 shapeChange = true;
233 }
234 }
[6002]235 /*
236 if (getPres(sty.setName("xlink:href")))
237 {
238 URI src = sty.getURIValue(getXMLBase());
239 href = diagram.getUniverse().getElement(src);
240 }
[4256]241
[6002]242 //Determine use offset/scale
243 refXform = new AffineTransform();
244 refXform.translate(this.x, this.y);
245 refXform.scale(this.width, this.height);
246 */
[4256]247 if (shapeChange)
248 {
249 build();
250 //Determine use offset/scale
251// refXform.setToTranslation(this.x, this.y);
252// refXform.scale(this.width, this.height);
253// return true;
254 }
[6002]255
[4256]256 return changeState || shapeChange;
257 }
258}
Note: See TracBrowser for help on using the repository browser.