Changeset 6002 in josm for trunk/src/com/kitfox/svg/ImageSVG.java
- Timestamp:
- 2013-06-11T01:01:28+02:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/kitfox/svg/ImageSVG.java
r4256 r6002 1 1 /* 2 * Font.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 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 24 33 * 25 34 * Created on February 20, 2004, 10:00 PM 26 35 */ 27 28 36 package com.kitfox.svg; 29 37 30 38 import com.kitfox.svg.app.data.Handler; 31 import com.kitfox.svg.xml.*; 32 33 import java.awt.*; 34 import java.awt.geom.*; 35 import java.awt.image.*; 36 import java.net.*; 39 import com.kitfox.svg.xml.StyleAttribute; 40 import java.awt.AlphaComposite; 41 import java.awt.Composite; 42 import java.awt.Graphics2D; 43 import java.awt.geom.AffineTransform; 44 import java.awt.geom.Point2D; 45 import java.awt.geom.Rectangle2D; 46 import java.awt.image.BufferedImage; 47 import java.net.URI; 48 import java.net.URL; 37 49 import java.util.List; 50 import java.util.logging.Level; 51 import java.util.logging.Logger; 38 52 39 53 /** 40 * Implements an embedded font. 41 * 42 * SVG specification: http://www.w3.org/TR/SVG/fonts.html 54 * Implements an image. 43 55 * 44 56 * @author Mark McKay … … 47 59 public class ImageSVG extends RenderableElement 48 60 { 61 public static final String TAG_NAME = "image"; 62 49 63 float x = 0f; 50 64 float y = 0f; 51 65 float width = 0f; 52 66 float height = 0f; 53 54 67 // BufferedImage href = null; 55 68 URL imageSrc = null; 56 57 69 AffineTransform xform; 58 70 Rectangle2D bounds; 59 71 60 /** Creates a new instance of Font */ 72 /** 73 * Creates a new instance of Font 74 */ 61 75 public ImageSVG() 62 76 { 63 77 } 64 78 79 public String getTagName() 80 { 81 return TAG_NAME; 82 } 83 65 84 protected void build() throws SVGException 66 85 { 67 86 super.build(); 68 87 69 88 StyleAttribute sty = new StyleAttribute(); 70 71 if (getPres(sty.setName("x"))) x = sty.getFloatValueWithUnits(); 72 73 if (getPres(sty.setName("y"))) y = sty.getFloatValueWithUnits(); 74 75 if (getPres(sty.setName("width"))) width = sty.getFloatValueWithUnits(); 76 77 if (getPres(sty.setName("height"))) height = sty.getFloatValueWithUnits(); 78 79 try { 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 { 80 112 if (getPres(sty.setName("xlink:href"))) 81 113 { … … 84 116 { 85 117 imageSrc = new URL(null, src.toASCIIString(), new Handler()); 86 } 87 else88 {89 try{118 } else 119 { 120 try 121 { 90 122 imageSrc = src.toURL(); 91 } 92 catch (Exception e) 123 } catch (Exception e) 93 124 { 94 e.printStackTrace(); 125 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, 126 "Could not parse xlink:href", e); 127 // e.printStackTrace(); 95 128 imageSrc = null; 96 129 } 97 130 } 98 131 } 99 } 100 catch (Exception e) 132 } catch (Exception e) 101 133 { 102 134 throw new SVGException(e); … … 104 136 105 137 diagram.getUniverse().registerImage(imageSrc); 106 138 107 139 //Set widths if not set 108 140 BufferedImage img = diagram.getUniverse().getImage(imageSrc); … … 113 145 return; 114 146 } 115 116 if (width == 0) width = img.getWidth(); 117 if (height == 0) height = img.getHeight(); 118 147 148 if (width == 0) 149 { 150 width = img.getWidth(); 151 } 152 if (height == 0) 153 { 154 height = img.getHeight(); 155 } 156 119 157 //Determine image xform 120 158 xform = new AffineTransform(); … … 123 161 xform.translate(this.x, this.y); 124 162 xform.scale(this.width / img.getWidth(), this.height / img.getHeight()); 125 163 126 164 bounds = new Rectangle2D.Float(this.x, this.y, this.width, this.height); 127 165 } 128 129 130 131 public float getX() { return x; } 132 public float getY() { return y; } 133 public float getWidth() { return width; } 134 public float getHeight() { return height; } 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 } 135 186 136 187 void pick(Point2D point, boolean boundingBox, List retVec) throws SVGException … … 155 206 if (getStyle(styleAttrib.setName("visibility"))) 156 207 { 157 if (!styleAttrib.getStringValue().equals("visible")) return; 158 } 159 208 if (!styleAttrib.getStringValue().equals("visible")) 209 { 210 return; 211 } 212 } 213 160 214 beginLayer(g); 161 215 162 216 float opacity = 1f; 163 217 if (getStyle(styleAttrib.setName("opacity"))) … … 165 219 opacity = styleAttrib.getRatioValue(); 166 220 } 167 168 if (opacity <= 0) return; 221 222 if (opacity <= 0) 223 { 224 return; 225 } 169 226 170 227 Composite oldComp = null; 171 228 172 229 if (opacity < 1) 173 230 { … … 176 233 g.setComposite(comp); 177 234 } 178 235 179 236 BufferedImage img = diagram.getUniverse().getImage(imageSrc); 180 if (img == null) return; 181 237 if (img == null) 238 { 239 return; 240 } 241 182 242 AffineTransform curXform = g.getTransform(); 183 243 g.transform(xform); 184 244 185 245 g.drawImage(img, 0, 0, null); 186 246 187 247 g.setTransform(curXform); 188 if (oldComp != null) g.setComposite(oldComp); 189 248 if (oldComp != null) 249 { 250 g.setComposite(oldComp); 251 } 252 190 253 finishLayer(g); 191 254 } 192 255 193 256 public Rectangle2D getBoundingBox() 194 257 { … … 197 260 198 261 /** 199 * Updates all attributes in this diagram associated with a time event. 200 * Ie, all attributes with track information. 262 * Updates all attributes in this diagram associated with a time event. Ie, 263 * all attributes with track information. 264 * 201 265 * @return - true if this node has changed state as a result of the time 202 266 * update … … 210 274 StyleAttribute sty = new StyleAttribute(); 211 275 boolean shapeChange = false; 212 276 213 277 if (getPres(sty.setName("x"))) 214 278 { … … 220 284 } 221 285 } 222 286 223 287 if (getPres(sty.setName("y"))) 224 288 { … … 230 294 } 231 295 } 232 296 233 297 if (getPres(sty.setName("width"))) 234 298 { … … 240 304 } 241 305 } 242 306 243 307 if (getPres(sty.setName("height"))) 244 308 { … … 250 314 } 251 315 } 252 253 try { 316 317 try 318 { 254 319 if (getPres(sty.setName("xlink:href"))) 255 320 { 256 321 URI src = sty.getURIValue(getXMLBase()); 257 URL newVal = src.toURL(); 258 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 259 332 if (!newVal.equals(imageSrc)) 260 333 { … … 263 336 } 264 337 } 265 } 266 catch (IllegalArgumentException ie) 267 { 268 new Exception("Image provided with illegal value for href: \"" + sty.getStringValue() + '"', ie).printStackTrace(); 269 } 270 catch (Exception e) 271 { 272 e.printStackTrace(); 273 } 274 275 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 276 350 if (shapeChange) 277 351 { … … 303 377 // return true; 304 378 } 305 379 306 380 return changeState || shapeChange; 307 381 }
Note:
See TracChangeset
for help on using the changeset viewer.