Changeset 4256 in josm
- Timestamp:
- 2011-07-18T00:05:50+02:00 (13 years ago)
- Location:
- trunk
- Files:
-
- 109 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/ImageProvider.java
r4255 r4256 3 3 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 6 import com.kitfox.svg.SVGDiagram; 7 import com.kitfox.svg.SVGException; 8 import com.kitfox.svg.SVGUniverse; 5 9 6 10 import java.awt.Component; … … 20 24 import java.io.InputStream; 21 25 import java.net.MalformedURLException; 26 import java.net.URI; 22 27 import java.net.URL; 23 28 import java.util.Arrays; … … 42 47 public class ImageProvider { 43 48 49 private static SVGUniverse svgUniverse; 50 44 51 /** 45 52 * Position of an overlay icon … … 48 55 public static enum OverlayPosition { 49 56 NORTHWEST, NORTHEAST, SOUTHWEST, SOUTHEAST 57 } 58 59 public static enum ImageType { 60 PNG, SVG 50 61 } 51 62 … … 71 82 * Return an image from the specified location. 72 83 * 73 * @param subdir The position of the directory, e.g. "layer"74 * @param name The icons name (with out the ending of ".png")84 * @param subdir The position of the directory, e.g. 'layer' 85 * @param name The icons name (with or without '.png' or '.svg' extension) 75 86 * @return The requested Image. 76 87 */ … … 86 97 } 87 98 99 /** 100 * Shortcut for get("", name); 101 */ 102 public static ImageIcon get(String name) { 103 return get("", name); 104 } 105 88 106 public static ImageIcon getIfAvailable(String subdir, String name) { 89 107 return getIfAvailable((Collection<String>) null, null, subdir, name); … … 111 129 * or something like 112 130 * dirs.get(i)+"/"+subdir+"/"+name+".png". 113 * @param dirs Directories to look .131 * @param dirs Directories to look (may be null). 114 132 * @param id An id used for caching. Id is not used for cache if name starts with http://. (URL is unique anyway.) 115 133 * @param subdir Subdirectory the image lies in. 116 * @param name The name of the image. If it contains no '.', a png extension is added. 117 * @param archive A zip file where the image is located. 134 * @param name The name of the image. If it does not end with '.png' or '.svg', 135 * it will try both extensions. 136 * @param archive A zip file where the image is located (may be null). 118 137 * @param sanitize If the image should be repainted to a new BufferedImage to work 119 138 * around certain issues. … … 133 152 if (name == null) 134 153 return null; 154 ImageType type = name.toLowerCase().endsWith("svg") ? ImageType.SVG : ImageType.PNG; 155 135 156 if (name.startsWith("http://")) { 136 return getIfAvailableHttp(name); 137 } 157 String url = name; 158 ImageWrapper iw = cache.get(url); 159 if (iw != null) return iw; 160 iw = getIfAvailableHttp(url, type); 161 if (iw != null) { 162 cache.put(url, iw); 163 } 164 return iw; 165 } 166 138 167 if (subdir == null) { 139 168 subdir = ""; … … 141 170 subdir += "/"; 142 171 } 143 String ext = name.indexOf('.') != -1 ? "" : ".png"; 144 String full_name = subdir + name + ext; 145 String cache_name = full_name; 146 /* cache separately */ 147 if (dirs != null && dirs.size() > 0) { 148 cache_name = "id:" + id + ":" + full_name; 149 if(archive != null) { 150 cache_name += ":" + archive.getName(); 151 } 152 } 153 154 ImageWrapper iw = cache.get(cache_name); 155 if (iw == null) { 172 String[] extensions; 173 if (name.toLowerCase().endsWith(".png") | name.toLowerCase().endsWith(".svg")) { 174 extensions = new String[] { "" }; 175 } else { 176 extensions = new String[] { ".png", ".svg"}; 177 } 178 for (String ext : extensions) { 179 if (".svg".equals(ext)) { 180 type = ImageType.SVG; 181 } else if (".png".equals(ext)) { 182 type = ImageType.PNG; 183 } 184 185 String full_name = subdir + name + ext; 186 String cache_name = full_name; 187 /* cache separately */ 188 if (dirs != null && dirs.size() > 0) { 189 cache_name = "id:" + id + ":" + full_name; 190 if(archive != null) { 191 cache_name += ":" + archive.getName(); 192 } 193 } 194 195 ImageWrapper iw = cache.get(cache_name); 196 if (iw != null) return iw; 197 156 198 if (archive != null) { 157 iw = getIfAvailableZip(full_name, archive); 158 } 199 iw = getIfAvailableZip(full_name, archive, type); 200 if (iw != null) { 201 cache.put(cache_name, iw); 202 return iw; 203 } 204 } 205 159 206 // getImageUrl() does a ton of "stat()" calls and gets expensive 160 207 // and redundant when you have a whole ton of objects. So, … … 162 209 // and don't bother to create a URL unless we're actually 163 210 // creating the image. 164 if (iw == null) { 165 URL path = getImageUrl(full_name, dirs); 166 if (path == null) 167 return null; 168 Image img = Toolkit.getDefaultToolkit().createImage(path); 169 iw = new ImageWrapper(img, false); 170 } 171 cache.put(cache_name, iw); 172 } 173 174 return iw; 175 } 176 177 private static ImageWrapper getIfAvailableHttp(String url) { 178 ImageWrapper iw = cache.get(url); 179 if (iw == null) { 180 try { 181 MirroredInputStream is = new MirroredInputStream(url, new File(Main.pref.getPreferencesDir(), 182 "images").toString()); 183 Image img = Toolkit.getDefaultToolkit().createImage(is.getFile().toURI().toURL()); 184 iw = new ImageWrapper(img, false); 185 cache.put(url, iw); 186 } catch (IOException e) { 187 } 188 } 189 return iw; 190 } 191 192 private static ImageWrapper getIfAvailableZip(String full_name, File archive) { 211 URL path = getImageUrl(full_name, dirs); 212 if (path == null) 213 continue; 214 iw = getIfAvailableLocalURL(path, type); 215 if (iw != null) { 216 cache.put(cache_name, iw); 217 return iw; 218 } 219 } 220 return null; 221 } 222 223 private static ImageWrapper getIfAvailableHttp(String url, ImageType type) { 224 Image img = null; 225 try { 226 MirroredInputStream is = new MirroredInputStream(url, 227 new File(Main.pref.getPreferencesDir(), "images").toString()); 228 switch (type) { 229 case PNG: 230 img = Toolkit.getDefaultToolkit().createImage(is.getFile().toURI().toURL()); 231 break; 232 case SVG: 233 URI uri = getSvgUniverse().loadSVG(is, is.getFile().toURI().toURL().toString()); 234 img = createImageFromSvgUri(uri); 235 break; 236 } 237 } catch (IOException e) { 238 } 239 return img == null ? null : new ImageWrapper(img, false); 240 } 241 242 private static ImageWrapper getIfAvailableZip(String full_name, File archive, ImageType type) { 193 243 ZipFile zipFile = null; 194 244 Image img = null; … … 205 255 try { 206 256 is = zipFile.getInputStream(entry); 207 while(size > 0) 208 { 209 int l = is.read(buf, offs, size); 210 offs += l; 211 size -= l; 257 switch (type) { 258 case PNG: 259 while(size > 0) 260 { 261 int l = is.read(buf, offs, size); 262 offs += l; 263 size -= l; 264 } 265 img = Toolkit.getDefaultToolkit().createImage(buf); 266 break; 267 case SVG: 268 URI uri = getSvgUniverse().loadSVG(is, full_name); 269 img = createImageFromSvgUri(uri); 270 break; 212 271 } 213 img = Toolkit.getDefaultToolkit().createImage(buf);214 272 } finally { 215 273 if (is != null) { … … 231 289 } 232 290 291 private static ImageWrapper getIfAvailableLocalURL(URL path, ImageType type) { 292 Image img = null; 293 switch (type) { 294 case PNG: 295 img = Toolkit.getDefaultToolkit().createImage(path); 296 break; 297 case SVG: 298 URI uri = getSvgUniverse().loadSVG(path); 299 img = createImageFromSvgUri(uri); 300 break; 301 } 302 return img == null ? null : new ImageWrapper(img, false); 303 } 304 233 305 private static URL getImageUrl(String path, String name) { 234 if (path .startsWith("resource://")) {306 if (path != null && path.startsWith("resource://")) { 235 307 String p = path.substring("resource://".length()); 236 308 for (ClassLoader source : PluginHandler.getResourceClassLoaders()) { … … 280 352 } 281 353 354 // Absolute path? 355 u = getImageUrl(null, imageName); 356 if (u != null) 357 return u; 358 282 359 // Try plugins and josm classloader 283 360 u = getImageUrl("resource://images/", imageName); … … 294 371 return u; 295 372 } 373 296 374 return null; 297 }298 299 /**300 * Shortcut for get("", name);301 */302 public static ImageIcon get(String name) {303 return get("", name);304 375 } 305 376 … … 438 509 return result; 439 510 } 511 512 private static Image createImageFromSvgUri(URI uri) { 513 SVGDiagram dia = getSvgUniverse().getDiagram(uri); 514 int w = (int)dia.getWidth(); 515 int h = (int)dia.getHeight(); 516 Image img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 517 Graphics2D g = ((BufferedImage) img).createGraphics(); 518 g.setClip(0, 0, w, h); 519 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 520 try { 521 dia.render(g); 522 } catch (SVGException ex) { 523 return null; 524 } 525 return img; 526 } 527 528 private static SVGUniverse getSvgUniverse() { 529 if (svgUniverse == null) { 530 svgUniverse = new SVGUniverse(); 531 } 532 return svgUniverse; 533 } 440 534 }
Note:
See TracChangeset
for help on using the changeset viewer.