source: josm/trunk/src/com/kitfox/svg/ImageSVG.java@ 6129

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

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

File size: 10.8 KB
Line 
1/*
2 * SVG Salamander
3 * Copyright (c) 2004, Mark McKay
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
9 *
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.
17 *
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
33 *
34 * Created on February 20, 2004, 10:00 PM
35 */
36package com.kitfox.svg;
37
38import com.kitfox.svg.app.data.Handler;
39import com.kitfox.svg.xml.StyleAttribute;
40import java.awt.AlphaComposite;
41import java.awt.Composite;
42import java.awt.Graphics2D;
43import java.awt.geom.AffineTransform;
44import java.awt.geom.Point2D;
45import java.awt.geom.Rectangle2D;
46import java.awt.image.BufferedImage;
47import java.net.URI;
48import java.net.URL;
49import java.util.List;
50import java.util.logging.Level;
51import java.util.logging.Logger;
52
53/**
54 * Implements an image.
55 *
56 * @author Mark McKay
57 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
58 */
59public class ImageSVG extends RenderableElement
60{
61 public static final String TAG_NAME = "image";
62
63 float x = 0f;
64 float y = 0f;
65 float width = 0f;
66 float height = 0f;
67// BufferedImage href = null;
68 URL imageSrc = null;
69 AffineTransform xform;
70 Rectangle2D bounds;
71
72 /**
73 * Creates a new instance of Font
74 */
75 public ImageSVG()
76 {
77 }
78
79 public String getTagName()
80 {
81 return TAG_NAME;
82 }
83
84 protected void build() throws SVGException
85 {
86 super.build();
87
88 StyleAttribute sty = new StyleAttribute();
89
90 if (getPres(sty.setName("x")))
91 {
92 x = sty.getFloatValueWithUnits();
93 }
94
95 if (getPres(sty.setName("y")))
96 {
97 y = sty.getFloatValueWithUnits();
98 }
99
100 if (getPres(sty.setName("width")))
101 {
102 width = sty.getFloatValueWithUnits();
103 }
104
105 if (getPres(sty.setName("height")))
106 {
107 height = sty.getFloatValueWithUnits();
108 }
109
110 try
111 {
112 if (getPres(sty.setName("xlink:href")))
113 {
114 URI src = sty.getURIValue(getXMLBase());
115 if ("data".equals(src.getScheme()))
116 {
117 imageSrc = new URL(null, src.toASCIIString(), new Handler());
118 } else
119 {
120 try
121 {
122 imageSrc = src.toURL();
123 } catch (Exception e)
124 {
125 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
126 "Could not parse xlink:href", e);
127// e.printStackTrace();
128 imageSrc = null;
129 }
130 }
131 }
132 } catch (Exception e)
133 {
134 throw new SVGException(e);
135 }
136
137 diagram.getUniverse().registerImage(imageSrc);
138
139 //Set widths if not set
140 BufferedImage img = diagram.getUniverse().getImage(imageSrc);
141 if (img == null)
142 {
143 xform = new AffineTransform();
144 bounds = new Rectangle2D.Float();
145 return;
146 }
147
148 if (width == 0)
149 {
150 width = img.getWidth();
151 }
152 if (height == 0)
153 {
154 height = img.getHeight();
155 }
156
157 //Determine image xform
158 xform = new AffineTransform();
159// xform.setToScale(this.width / img.getWidth(), this.height / img.getHeight());
160// xform.translate(this.x, this.y);
161 xform.translate(this.x, this.y);
162 xform.scale(this.width / img.getWidth(), this.height / img.getHeight());
163
164 bounds = new Rectangle2D.Float(this.x, this.y, this.width, this.height);
165 }
166
167 public float getX()
168 {
169 return x;
170 }
171
172 public float getY()
173 {
174 return y;
175 }
176
177 public float getWidth()
178 {
179 return width;
180 }
181
182 public float getHeight()
183 {
184 return height;
185 }
186
187 void pick(Point2D point, boolean boundingBox, List retVec) throws SVGException
188 {
189 if (getBoundingBox().contains(point))
190 {
191 retVec.add(getPath(null));
192 }
193 }
194
195 void pick(Rectangle2D pickArea, AffineTransform ltw, boolean boundingBox, List retVec) throws SVGException
196 {
197 if (ltw.createTransformedShape(getBoundingBox()).intersects(pickArea))
198 {
199 retVec.add(getPath(null));
200 }
201 }
202
203 public void render(Graphics2D g) throws SVGException
204 {
205 StyleAttribute styleAttrib = new StyleAttribute();
206 if (getStyle(styleAttrib.setName("visibility")))
207 {
208 if (!styleAttrib.getStringValue().equals("visible"))
209 {
210 return;
211 }
212 }
213
214 beginLayer(g);
215
216 float opacity = 1f;
217 if (getStyle(styleAttrib.setName("opacity")))
218 {
219 opacity = styleAttrib.getRatioValue();
220 }
221
222 if (opacity <= 0)
223 {
224 return;
225 }
226
227 Composite oldComp = null;
228
229 if (opacity < 1)
230 {
231 oldComp = g.getComposite();
232 Composite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity);
233 g.setComposite(comp);
234 }
235
236 BufferedImage img = diagram.getUniverse().getImage(imageSrc);
237 if (img == null)
238 {
239 return;
240 }
241
242 AffineTransform curXform = g.getTransform();
243 g.transform(xform);
244
245 g.drawImage(img, 0, 0, null);
246
247 g.setTransform(curXform);
248 if (oldComp != null)
249 {
250 g.setComposite(oldComp);
251 }
252
253 finishLayer(g);
254 }
255
256 public Rectangle2D getBoundingBox()
257 {
258 return boundsToParent(bounds);
259 }
260
261 /**
262 * Updates all attributes in this diagram associated with a time event. Ie,
263 * all attributes with track information.
264 *
265 * @return - true if this node has changed state as a result of the time
266 * update
267 */
268 public boolean updateTime(double curTime) throws SVGException
269 {
270// if (trackManager.getNumTracks() == 0) return false;
271 boolean changeState = super.updateTime(curTime);
272
273 //Get current values for parameters
274 StyleAttribute sty = new StyleAttribute();
275 boolean shapeChange = false;
276
277 if (getPres(sty.setName("x")))
278 {
279 float newVal = sty.getFloatValueWithUnits();
280 if (newVal != x)
281 {
282 x = newVal;
283 shapeChange = true;
284 }
285 }
286
287 if (getPres(sty.setName("y")))
288 {
289 float newVal = sty.getFloatValueWithUnits();
290 if (newVal != y)
291 {
292 y = newVal;
293 shapeChange = true;
294 }
295 }
296
297 if (getPres(sty.setName("width")))
298 {
299 float newVal = sty.getFloatValueWithUnits();
300 if (newVal != width)
301 {
302 width = newVal;
303 shapeChange = true;
304 }
305 }
306
307 if (getPres(sty.setName("height")))
308 {
309 float newVal = sty.getFloatValueWithUnits();
310 if (newVal != height)
311 {
312 height = newVal;
313 shapeChange = true;
314 }
315 }
316
317 try
318 {
319 if (getPres(sty.setName("xlink:href")))
320 {
321 URI src = sty.getURIValue(getXMLBase());
322
323 URL newVal;
324 if ("data".equals(src.getScheme()))
325 {
326 newVal = new URL(null, src.toASCIIString(), new Handler());
327 } else
328 {
329 newVal = src.toURL();
330 }
331
332 if (!newVal.equals(imageSrc))
333 {
334 imageSrc = newVal;
335 shapeChange = true;
336 }
337 }
338 } catch (IllegalArgumentException ie)
339 {
340 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
341 "Image provided with illegal value for href: \""
342 + sty.getStringValue() + '"', ie);
343 } catch (Exception e)
344 {
345 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
346 "Could not parse xlink:href", e);
347 }
348
349
350 if (shapeChange)
351 {
352 build();
353// diagram.getUniverse().registerImage(imageSrc);
354//
355// //Set widths if not set
356// BufferedImage img = diagram.getUniverse().getImage(imageSrc);
357// if (img == null)
358// {
359// xform = new AffineTransform();
360// bounds = new Rectangle2D.Float();
361// }
362// else
363// {
364// if (width == 0) width = img.getWidth();
365// if (height == 0) height = img.getHeight();
366//
367// //Determine image xform
368// xform = new AffineTransform();
369//// xform.setToScale(this.width / img.getWidth(), this.height / img.getHeight());
370//// xform.translate(this.x, this.y);
371// xform.translate(this.x, this.y);
372// xform.scale(this.width / img.getWidth(), this.height / img.getHeight());
373//
374// bounds = new Rectangle2D.Float(this.x, this.y, this.width, this.height);
375// }
376//
377// return true;
378 }
379
380 return changeState || shapeChange;
381 }
382}
Note: See TracBrowser for help on using the repository browser.