[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[71] | 2 | package org.openstreetmap.josm.tools;
|
---|
[17] | 3 |
|
---|
[2017] | 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[1295] | 6 | import java.awt.Component;
|
---|
[101] | 7 | import java.awt.Cursor;
|
---|
[4271] | 8 | import java.awt.Dimension;
|
---|
[17] | 9 | import java.awt.Graphics;
|
---|
[1295] | 10 | import java.awt.Graphics2D;
|
---|
[17] | 11 | import java.awt.GraphicsConfiguration;
|
---|
| 12 | import java.awt.GraphicsEnvironment;
|
---|
[79] | 13 | import java.awt.Image;
|
---|
[101] | 14 | import java.awt.Point;
|
---|
[1295] | 15 | import java.awt.RenderingHints;
|
---|
[79] | 16 | import java.awt.Toolkit;
|
---|
[17] | 17 | import java.awt.Transparency;
|
---|
| 18 | import java.awt.image.BufferedImage;
|
---|
[4403] | 19 | import java.io.ByteArrayInputStream;
|
---|
[243] | 20 | import java.io.File;
|
---|
[2990] | 21 | import java.io.IOException;
|
---|
[2889] | 22 | import java.io.InputStream;
|
---|
[243] | 23 | import java.net.MalformedURLException;
|
---|
[4256] | 24 | import java.net.URI;
|
---|
[17] | 25 | import java.net.URL;
|
---|
[991] | 26 | import java.util.Arrays;
|
---|
| 27 | import java.util.Collection;
|
---|
[17] | 28 | import java.util.HashMap;
|
---|
| 29 | import java.util.Map;
|
---|
[2889] | 30 | import java.util.zip.ZipEntry;
|
---|
| 31 | import java.util.zip.ZipFile;
|
---|
[17] | 32 | import javax.swing.Icon;
|
---|
| 33 | import javax.swing.ImageIcon;
|
---|
| 34 |
|
---|
[21] | 35 | import org.openstreetmap.josm.Main;
|
---|
[1814] | 36 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
[1711] | 37 | import org.openstreetmap.josm.io.MirroredInputStream;
|
---|
[3874] | 38 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
[4403] | 39 | import org.openstreetmap.josm.tools.Utils;
|
---|
| 40 | import org.xml.sax.Attributes;
|
---|
| 41 | import org.xml.sax.EntityResolver;
|
---|
| 42 | import org.xml.sax.InputSource;
|
---|
| 43 | import org.xml.sax.SAXException;
|
---|
| 44 | import org.xml.sax.XMLReader;
|
---|
| 45 | import org.xml.sax.helpers.DefaultHandler;
|
---|
| 46 | import org.xml.sax.helpers.XMLReaderFactory;
|
---|
[21] | 47 |
|
---|
[4403] | 48 | import com.kitfox.svg.SVGDiagram;
|
---|
| 49 | import com.kitfox.svg.SVGException;
|
---|
| 50 | import com.kitfox.svg.SVGUniverse;
|
---|
| 51 |
|
---|
[17] | 52 | /**
|
---|
[4271] | 53 | * Helper class to support the application with images.
|
---|
[17] | 54 | * @author imi
|
---|
| 55 | */
|
---|
| 56 | public class ImageProvider {
|
---|
| 57 |
|
---|
[4256] | 58 | private static SVGUniverse svgUniverse;
|
---|
| 59 |
|
---|
[1169] | 60 | /**
|
---|
| 61 | * Position of an overlay icon
|
---|
| 62 | * @author imi
|
---|
| 63 | */
|
---|
[1879] | 64 | public static enum OverlayPosition {
|
---|
| 65 | NORTHWEST, NORTHEAST, SOUTHWEST, SOUTHEAST
|
---|
| 66 | }
|
---|
[93] | 67 |
|
---|
[4256] | 68 | public static enum ImageType {
|
---|
[4261] | 69 | SVG, // scalable vector graphics
|
---|
[4271] | 70 | OTHER // everything else, e.g. png, gif (must be supported by Java)
|
---|
[4256] | 71 | }
|
---|
| 72 |
|
---|
[1169] | 73 | /**
|
---|
| 74 | * The icon cache
|
---|
| 75 | */
|
---|
[4271] | 76 | private static Map<String, ImageResource> cache = new HashMap<String, ImageResource>();
|
---|
[93] | 77 |
|
---|
[1169] | 78 | /**
|
---|
| 79 | * Return an image from the specified location.
|
---|
[2512] | 80 | *
|
---|
[4256] | 81 | * @param subdir The position of the directory, e.g. 'layer'
|
---|
| 82 | * @param name The icons name (with or without '.png' or '.svg' extension)
|
---|
[1169] | 83 | * @return The requested Image.
|
---|
| 84 | */
|
---|
| 85 | public static ImageIcon get(String subdir, String name) {
|
---|
| 86 | ImageIcon icon = getIfAvailable(subdir, name);
|
---|
| 87 | if (icon == null) {
|
---|
[4261] | 88 | String ext = name.indexOf('.') != -1 ? "" : ".???";
|
---|
[3446] | 89 | throw new NullPointerException(tr(
|
---|
[4403] | 90 | "Fatal: failed to locate image ''{0}''. This is a serious configuration problem. JOSM will stop working.",
|
---|
| 91 | name+ext));
|
---|
[1169] | 92 | }
|
---|
| 93 | return icon;
|
---|
| 94 | }
|
---|
[200] | 95 |
|
---|
[4256] | 96 | /**
|
---|
| 97 | * Shortcut for get("", name);
|
---|
| 98 | */
|
---|
| 99 | public static ImageIcon get(String name) {
|
---|
| 100 | return get("", name);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[1879] | 103 | public static ImageIcon getIfAvailable(String subdir, String name) {
|
---|
| 104 | return getIfAvailable((Collection<String>) null, null, subdir, name);
|
---|
[1169] | 105 | }
|
---|
[1879] | 106 |
|
---|
[3862] | 107 | public static ImageIcon getIfAvailable(String[] dirs, String id, String subdir, String name) {
|
---|
[1169] | 108 | return getIfAvailable(Arrays.asList(dirs), id, subdir, name);
|
---|
| 109 | }
|
---|
[991] | 110 |
|
---|
[1169] | 111 | /**
|
---|
[1879] | 112 | * Like {@link #get(String)}, but does not throw and return <code>null</code> in case of nothing
|
---|
[3365] | 113 | * is found. Use this, if the image to retrieve is optional. Nevertheless a warning will
|
---|
| 114 | * be printed on the console if the image could not be found.
|
---|
[1169] | 115 | */
|
---|
[1879] | 116 | public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name) {
|
---|
[2889] | 117 | return getIfAvailable(dirs, id, subdir, name, null);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[3862] | 120 | public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive) {
|
---|
| 121 | return getIfAvailable(dirs, id, subdir, name, archive, false);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[4271] | 124 | public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, boolean sanitize) {
|
---|
| 125 | return getIfAvailable(dirs, id, subdir, name, archive, null, sanitize);
|
---|
| 126 | }
|
---|
[4403] | 127 |
|
---|
[3365] | 128 | /**
|
---|
| 129 | * The full path of the image is either a url (starting with http://)
|
---|
| 130 | * or something like
|
---|
| 131 | * dirs.get(i)+"/"+subdir+"/"+name+".png".
|
---|
[4256] | 132 | * @param dirs Directories to look (may be null).
|
---|
[3365] | 133 | * @param id An id used for caching. Id is not used for cache if name starts with http://. (URL is unique anyway.)
|
---|
| 134 | * @param subdir Subdirectory the image lies in.
|
---|
[4256] | 135 | * @param name The name of the image. If it does not end with '.png' or '.svg',
|
---|
| 136 | * it will try both extensions.
|
---|
| 137 | * @param archive A zip file where the image is located (may be null).
|
---|
[4271] | 138 | * @param dim The dimensions of the image if it should be scaled. null if the
|
---|
[4403] | 139 | * original size of the image should be returned. The width
|
---|
[4271] | 140 | * part of the dimension can be -1. Then it will scale the width
|
---|
| 141 | * in the same way as the height. (And the other way around.)
|
---|
[3862] | 142 | * @param sanitize If the image should be repainted to a new BufferedImage to work
|
---|
| 143 | * around certain issues.
|
---|
[3365] | 144 | */
|
---|
[4271] | 145 | public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, Dimension dim, boolean sanitize) {
|
---|
| 146 | ImageResource ir = getIfAvailableImpl(dirs, id, subdir, name, archive);
|
---|
| 147 | if (ir == null)
|
---|
[3862] | 148 | return null;
|
---|
[4271] | 149 | return ir.getImageIcon(dim == null ? ImageResource.DEFAULT_DIMENSION : dim, sanitize);
|
---|
[3862] | 150 | }
|
---|
| 151 |
|
---|
[4271] | 152 | private static ImageResource getIfAvailableImpl(Collection<String> dirs, String id, String subdir, String name, File archive) {
|
---|
[1169] | 153 | if (name == null)
|
---|
| 154 | return null;
|
---|
[4261] | 155 | ImageType type = name.toLowerCase().endsWith(".svg") ? ImageType.SVG : ImageType.OTHER;
|
---|
[4256] | 156 |
|
---|
[1879] | 157 | if (name.startsWith("http://")) {
|
---|
[4256] | 158 | String url = name;
|
---|
[4271] | 159 | ImageResource ir = cache.get(url);
|
---|
| 160 | if (ir != null) return ir;
|
---|
| 161 | ir = getIfAvailableHttp(url, type);
|
---|
| 162 | if (ir != null) {
|
---|
| 163 | cache.put(url, ir);
|
---|
[4256] | 164 | }
|
---|
[4271] | 165 | return ir;
|
---|
[4403] | 166 | } else if (name.startsWith("wiki://")) {
|
---|
| 167 | ImageResource ir = cache.get(name);
|
---|
| 168 | if (ir != null) return ir;
|
---|
| 169 | ir = getIfAvailableWiki(name, type);
|
---|
| 170 | if (ir != null) {
|
---|
| 171 | cache.put(name, ir);
|
---|
| 172 | }
|
---|
| 173 | return ir;
|
---|
[1711] | 174 | }
|
---|
[4256] | 175 |
|
---|
[1814] | 176 | if (subdir == null) {
|
---|
[1169] | 177 | subdir = "";
|
---|
[1814] | 178 | } else if (!subdir.equals("")) {
|
---|
[1169] | 179 | subdir += "/";
|
---|
[1814] | 180 | }
|
---|
[4256] | 181 | String[] extensions;
|
---|
[4261] | 182 | if (name.indexOf('.') != -1) {
|
---|
[4256] | 183 | extensions = new String[] { "" };
|
---|
| 184 | } else {
|
---|
| 185 | extensions = new String[] { ".png", ".svg"};
|
---|
| 186 | }
|
---|
[4261] | 187 | final int ARCHIVE = 0, LOCAL = 1;
|
---|
| 188 | for (int place : new Integer[] { ARCHIVE, LOCAL }) {
|
---|
| 189 | for (String ext : extensions) {
|
---|
| 190 |
|
---|
| 191 | if (".svg".equals(ext)) {
|
---|
| 192 | type = ImageType.SVG;
|
---|
| 193 | } else if (".png".equals(ext)) {
|
---|
| 194 | type = ImageType.OTHER;
|
---|
[4256] | 195 | }
|
---|
[243] | 196 |
|
---|
[4261] | 197 | String full_name = subdir + name + ext;
|
---|
| 198 | String cache_name = full_name;
|
---|
| 199 | /* cache separately */
|
---|
| 200 | if (dirs != null && dirs.size() > 0) {
|
---|
| 201 | cache_name = "id:" + id + ":" + full_name;
|
---|
| 202 | if(archive != null) {
|
---|
| 203 | cache_name += ":" + archive.getName();
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
[4256] | 206 |
|
---|
[4271] | 207 | ImageResource ir = cache.get(cache_name);
|
---|
| 208 | if (ir != null) return ir;
|
---|
[4261] | 209 |
|
---|
| 210 | switch (place) {
|
---|
| 211 | case ARCHIVE:
|
---|
| 212 | if (archive != null) {
|
---|
[4271] | 213 | ir = getIfAvailableZip(full_name, archive, type);
|
---|
| 214 | if (ir != null) {
|
---|
| 215 | cache.put(cache_name, ir);
|
---|
| 216 | return ir;
|
---|
[4261] | 217 | }
|
---|
| 218 | }
|
---|
| 219 | break;
|
---|
| 220 | case LOCAL:
|
---|
| 221 | // getImageUrl() does a ton of "stat()" calls and gets expensive
|
---|
| 222 | // and redundant when you have a whole ton of objects. So,
|
---|
| 223 | // index the cache by the name of the icon we're looking for
|
---|
| 224 | // and don't bother to create a URL unless we're actually
|
---|
| 225 | // creating the image.
|
---|
| 226 | URL path = getImageUrl(full_name, dirs);
|
---|
[4403] | 227 | if (path == null) {
|
---|
[4261] | 228 | continue;
|
---|
[4403] | 229 | }
|
---|
[4271] | 230 | ir = getIfAvailableLocalURL(path, type);
|
---|
| 231 | if (ir != null) {
|
---|
| 232 | cache.put(cache_name, ir);
|
---|
| 233 | return ir;
|
---|
[4261] | 234 | }
|
---|
| 235 | break;
|
---|
[4256] | 236 | }
|
---|
[2889] | 237 | }
|
---|
[1169] | 238 | }
|
---|
[4256] | 239 | return null;
|
---|
[1169] | 240 | }
|
---|
[293] | 241 |
|
---|
[4271] | 242 | private static ImageResource getIfAvailableHttp(String url, ImageType type) {
|
---|
[4256] | 243 | try {
|
---|
| 244 | MirroredInputStream is = new MirroredInputStream(url,
|
---|
| 245 | new File(Main.pref.getPreferencesDir(), "images").toString());
|
---|
| 246 | switch (type) {
|
---|
| 247 | case SVG:
|
---|
| 248 | URI uri = getSvgUniverse().loadSVG(is, is.getFile().toURI().toURL().toString());
|
---|
[4271] | 249 | SVGDiagram svg = getSvgUniverse().getDiagram(uri);
|
---|
| 250 | return svg == null ? null : new ImageResource(svg);
|
---|
[4261] | 251 | case OTHER:
|
---|
[4271] | 252 | Image img = Toolkit.getDefaultToolkit().createImage(is.getFile().toURI().toURL());
|
---|
| 253 | return img == null ? null : new ImageResource(img, false);
|
---|
| 254 | default:
|
---|
| 255 | throw new AssertionError();
|
---|
[4255] | 256 | }
|
---|
[4256] | 257 | } catch (IOException e) {
|
---|
[4271] | 258 | return null;
|
---|
[4255] | 259 | }
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[4403] | 262 | private static ImageResource getIfAvailableWiki(String name, ImageType type) {
|
---|
| 263 | final Collection<String> defaultBaseUrls = Arrays.asList(
|
---|
| 264 | "http://wiki.openstreetmap.org/w/images/",
|
---|
| 265 | "http://upload.wikimedia.org/wikipedia/commons/",
|
---|
| 266 | "http://wiki.openstreetmap.org/wiki/File:"
|
---|
| 267 | );
|
---|
| 268 | final Collection<String> baseUrls = Main.pref.getCollection("image-provider.wiki.urls", defaultBaseUrls);
|
---|
| 269 |
|
---|
| 270 | final String fn = name.substring(name.lastIndexOf('/') + 1);
|
---|
| 271 |
|
---|
| 272 | ImageResource result = null;
|
---|
| 273 | for (String b : baseUrls) {
|
---|
| 274 | String url;
|
---|
| 275 | if (b.endsWith(":")) {
|
---|
| 276 | url = getImgUrlFromWikiInfoPage(b, fn);
|
---|
| 277 | if (url == null) {
|
---|
| 278 | continue;
|
---|
| 279 | }
|
---|
| 280 | } else {
|
---|
| 281 | final String fn_md5 = Utils.md5Hex(fn);
|
---|
| 282 | url = b + fn_md5.substring(0,1) + "/" + fn_md5.substring(0,2) + "/" + fn;
|
---|
| 283 | }
|
---|
| 284 | result = getIfAvailableHttp(url, type);
|
---|
| 285 | if (result != null) {
|
---|
| 286 | break;
|
---|
| 287 | }
|
---|
| 288 | }
|
---|
| 289 | return result;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
[4271] | 292 | private static ImageResource getIfAvailableZip(String full_name, File archive, ImageType type) {
|
---|
[4255] | 293 | ZipFile zipFile = null;
|
---|
| 294 | try
|
---|
| 295 | {
|
---|
| 296 | zipFile = new ZipFile(archive);
|
---|
| 297 | ZipEntry entry = zipFile.getEntry(full_name);
|
---|
| 298 | if(entry != null)
|
---|
| 299 | {
|
---|
| 300 | int size = (int)entry.getSize();
|
---|
| 301 | int offs = 0;
|
---|
| 302 | byte[] buf = new byte[size];
|
---|
| 303 | InputStream is = null;
|
---|
| 304 | try {
|
---|
| 305 | is = zipFile.getInputStream(entry);
|
---|
[4256] | 306 | switch (type) {
|
---|
[4261] | 307 | case SVG:
|
---|
| 308 | URI uri = getSvgUniverse().loadSVG(is, full_name);
|
---|
[4271] | 309 | SVGDiagram svg = getSvgUniverse().getDiagram(uri);
|
---|
| 310 | return svg == null ? null : new ImageResource(svg);
|
---|
[4261] | 311 | case OTHER:
|
---|
[4256] | 312 | while(size > 0)
|
---|
| 313 | {
|
---|
| 314 | int l = is.read(buf, offs, size);
|
---|
| 315 | offs += l;
|
---|
| 316 | size -= l;
|
---|
| 317 | }
|
---|
[4271] | 318 | Image img = Toolkit.getDefaultToolkit().createImage(buf);
|
---|
| 319 | return img == null ? null : new ImageResource(img, false);
|
---|
| 320 | default:
|
---|
| 321 | throw new AssertionError();
|
---|
[4255] | 322 | }
|
---|
| 323 | } finally {
|
---|
| 324 | if (is != null) {
|
---|
| 325 | is.close();
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
| 328 | }
|
---|
| 329 | } catch (Exception e) {
|
---|
| 330 | System.err.println(tr("Warning: failed to handle zip file ''{0}''. Exception was: {1}", archive.getName(), e.toString()));
|
---|
| 331 | } finally {
|
---|
| 332 | if (zipFile != null) {
|
---|
| 333 | try {
|
---|
| 334 | zipFile.close();
|
---|
| 335 | } catch (IOException ex) {
|
---|
| 336 | }
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
[4271] | 339 | return null;
|
---|
[4255] | 340 | }
|
---|
| 341 |
|
---|
[4271] | 342 | private static ImageResource getIfAvailableLocalURL(URL path, ImageType type) {
|
---|
[4256] | 343 | switch (type) {
|
---|
| 344 | case SVG:
|
---|
| 345 | URI uri = getSvgUniverse().loadSVG(path);
|
---|
[4271] | 346 | SVGDiagram svg = getSvgUniverse().getDiagram(uri);
|
---|
| 347 | return svg == null ? null : new ImageResource(svg);
|
---|
[4261] | 348 | case OTHER:
|
---|
[4271] | 349 | Image img = Toolkit.getDefaultToolkit().createImage(path);
|
---|
| 350 | return img == null ? null : new ImageResource(img, false);
|
---|
| 351 | default:
|
---|
| 352 | throw new AssertionError();
|
---|
[4256] | 353 | }
|
---|
| 354 | }
|
---|
| 355 |
|
---|
[1879] | 356 | private static URL getImageUrl(String path, String name) {
|
---|
[4256] | 357 | if (path != null && path.startsWith("resource://")) {
|
---|
[1169] | 358 | String p = path.substring("resource://".length());
|
---|
[3874] | 359 | for (ClassLoader source : PluginHandler.getResourceClassLoaders()) {
|
---|
[1169] | 360 | URL res;
|
---|
[1879] | 361 | if ((res = source.getResource(p + name)) != null)
|
---|
[1169] | 362 | return res;
|
---|
| 363 | }
|
---|
[1879] | 364 | } else {
|
---|
[1169] | 365 | try {
|
---|
| 366 | File f = new File(path, name);
|
---|
[1879] | 367 | if (f.exists())
|
---|
[1169] | 368 | return f.toURI().toURL();
|
---|
[1879] | 369 | } catch (MalformedURLException e) {
|
---|
| 370 | }
|
---|
[1169] | 371 | }
|
---|
| 372 | return null;
|
---|
| 373 | }
|
---|
[293] | 374 |
|
---|
[1879] | 375 | private static URL getImageUrl(String imageName, Collection<String> dirs) {
|
---|
| 376 | URL u = null;
|
---|
| 377 |
|
---|
[1169] | 378 | // Try passed directories first
|
---|
[1879] | 379 | if (dirs != null) {
|
---|
| 380 | for (String name : dirs) {
|
---|
| 381 | try {
|
---|
| 382 | u = getImageUrl(name, imageName);
|
---|
| 383 | if (u != null)
|
---|
| 384 | return u;
|
---|
| 385 | } catch (SecurityException e) {
|
---|
| 386 | System.out.println(tr(
|
---|
[2927] | 387 | "Warning: failed to access directory ''{0}'' for security reasons. Exception was: {1}",
|
---|
[1879] | 388 | name, e.toString()));
|
---|
| 389 | }
|
---|
| 390 |
|
---|
[1169] | 391 | }
|
---|
| 392 | }
|
---|
| 393 | // Try user-preference directory
|
---|
[1879] | 394 | String dir = Main.pref.getPreferencesDir() + "images";
|
---|
| 395 | try {
|
---|
| 396 | u = getImageUrl(dir, imageName);
|
---|
| 397 | if (u != null)
|
---|
| 398 | return u;
|
---|
| 399 | } catch (SecurityException e) {
|
---|
| 400 | System.out.println(tr(
|
---|
[2927] | 401 | "Warning: failed to access directory ''{0}'' for security reasons. Exception was: {1}", dir, e
|
---|
[1879] | 402 | .toString()));
|
---|
| 403 | }
|
---|
[243] | 404 |
|
---|
[4256] | 405 | // Absolute path?
|
---|
| 406 | u = getImageUrl(null, imageName);
|
---|
| 407 | if (u != null)
|
---|
| 408 | return u;
|
---|
| 409 |
|
---|
[1169] | 410 | // Try plugins and josm classloader
|
---|
| 411 | u = getImageUrl("resource://images/", imageName);
|
---|
[1879] | 412 | if (u != null)
|
---|
| 413 | return u;
|
---|
[991] | 414 |
|
---|
[2927] | 415 | // Try all other resource directories
|
---|
[1879] | 416 | for (String location : Main.pref.getAllPossiblePreferenceDirs()) {
|
---|
| 417 | u = getImageUrl(location + "images", imageName);
|
---|
| 418 | if (u != null)
|
---|
| 419 | return u;
|
---|
[1169] | 420 | u = getImageUrl(location, imageName);
|
---|
[1879] | 421 | if (u != null)
|
---|
| 422 | return u;
|
---|
[1169] | 423 | }
|
---|
[4256] | 424 |
|
---|
[1169] | 425 | return null;
|
---|
| 426 | }
|
---|
[17] | 427 |
|
---|
[4403] | 428 | /**
|
---|
| 429 | * Reads the wiki page on a certain file in html format in order to find the real image URL.
|
---|
| 430 | */
|
---|
| 431 | private static String getImgUrlFromWikiInfoPage(final String base, final String fn) {
|
---|
| 432 |
|
---|
| 433 | /** Quit parsing, when a certain condition is met */
|
---|
| 434 | class SAXReturnException extends SAXException {
|
---|
| 435 | private String result;
|
---|
| 436 |
|
---|
| 437 | public SAXReturnException(String result) {
|
---|
| 438 | this.result = result;
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | public String getResult() {
|
---|
| 442 | return result;
|
---|
| 443 | }
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | try {
|
---|
| 447 | final XMLReader parser = XMLReaderFactory.createXMLReader();
|
---|
| 448 | parser.setContentHandler(new DefaultHandler() {
|
---|
| 449 | @Override
|
---|
| 450 | public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
|
---|
| 451 | System.out.println();
|
---|
| 452 | if (localName.equalsIgnoreCase("img")) {
|
---|
| 453 | String val = atts.getValue("src");
|
---|
| 454 | if (val.endsWith(fn))
|
---|
| 455 | throw new SAXReturnException(val); // parsing done, quit early
|
---|
| 456 | }
|
---|
| 457 | }
|
---|
| 458 | });
|
---|
| 459 |
|
---|
| 460 | parser.setEntityResolver(new EntityResolver() {
|
---|
| 461 | public InputSource resolveEntity (String publicId, String systemId) {
|
---|
| 462 | return new InputSource(new ByteArrayInputStream(new byte[0]));
|
---|
| 463 | }
|
---|
| 464 | });
|
---|
| 465 |
|
---|
| 466 | parser.parse(new InputSource(new MirroredInputStream(
|
---|
| 467 | base + fn,
|
---|
| 468 | new File(Main.pref.getPreferencesDir(), "images").toString()
|
---|
| 469 | )));
|
---|
| 470 | } catch (SAXReturnException r) {
|
---|
| 471 | return r.getResult();
|
---|
| 472 | } catch (Exception e) {
|
---|
| 473 | System.out.println("INFO: parsing " + base + fn + " failed:\n" + e);
|
---|
| 474 | return null;
|
---|
| 475 | }
|
---|
| 476 | System.out.println("INFO: parsing " + base + fn + " failed: Unexpected content.");
|
---|
| 477 | return null;
|
---|
| 478 | }
|
---|
| 479 |
|
---|
[1169] | 480 | public static Cursor getCursor(String name, String overlay) {
|
---|
[1879] | 481 | ImageIcon img = get("cursor", name);
|
---|
[1814] | 482 | if (overlay != null) {
|
---|
[1879] | 483 | img = overlay(img, "cursor/modifier/" + overlay, OverlayPosition.SOUTHEAST);
|
---|
[1814] | 484 | }
|
---|
[1169] | 485 | Cursor c = Toolkit.getDefaultToolkit().createCustomCursor(img.getImage(),
|
---|
[1879] | 486 | name.equals("crosshair") ? new Point(10, 10) : new Point(3, 2), "Cursor");
|
---|
[1169] | 487 | return c;
|
---|
| 488 | }
|
---|
[159] | 489 |
|
---|
[1169] | 490 | /**
|
---|
[1879] | 491 | * @return an icon that represent the overlay of the two given icons. The second icon is layed
|
---|
| 492 | * on the first relative to the given position.
|
---|
[1169] | 493 | */
|
---|
| 494 | public static ImageIcon overlay(Icon ground, String overlayImage, OverlayPosition pos) {
|
---|
[3403] | 495 | return overlay(ground, ImageProvider.get(overlayImage), pos);
|
---|
| 496 | }
|
---|
| 497 |
|
---|
| 498 | public static ImageIcon overlay(Icon ground, Icon overlay, OverlayPosition pos) {
|
---|
[1879] | 499 | GraphicsConfiguration conf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
|
---|
| 500 | .getDefaultConfiguration();
|
---|
[1169] | 501 | int w = ground.getIconWidth();
|
---|
| 502 | int h = ground.getIconHeight();
|
---|
| 503 | int wo = overlay.getIconWidth();
|
---|
| 504 | int ho = overlay.getIconHeight();
|
---|
[1879] | 505 | BufferedImage img = conf.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
|
---|
[1169] | 506 | Graphics g = img.createGraphics();
|
---|
| 507 | ground.paintIcon(null, g, 0, 0);
|
---|
| 508 | int x = 0, y = 0;
|
---|
| 509 | switch (pos) {
|
---|
| 510 | case NORTHWEST:
|
---|
| 511 | x = 0;
|
---|
| 512 | y = 0;
|
---|
| 513 | break;
|
---|
| 514 | case NORTHEAST:
|
---|
[1879] | 515 | x = w - wo;
|
---|
[1169] | 516 | y = 0;
|
---|
| 517 | break;
|
---|
| 518 | case SOUTHWEST:
|
---|
| 519 | x = 0;
|
---|
[1879] | 520 | y = h - ho;
|
---|
[1169] | 521 | break;
|
---|
| 522 | case SOUTHEAST:
|
---|
[1879] | 523 | x = w - wo;
|
---|
| 524 | y = h - ho;
|
---|
[1169] | 525 | break;
|
---|
| 526 | }
|
---|
| 527 | overlay.paintIcon(null, g, x, y);
|
---|
| 528 | return new ImageIcon(img);
|
---|
| 529 | }
|
---|
| 530 |
|
---|
[1879] | 531 | /*
|
---|
| 532 | * from: http://www.jidesoft.com/blog/2008/02/29/rotate-an-icon-in-java/ License:
|
---|
| 533 | * "feel free to use"
|
---|
[1814] | 534 | */
|
---|
| 535 | final static double DEGREE_90 = 90.0 * Math.PI / 180.0;
|
---|
[1295] | 536 |
|
---|
| 537 | /**
|
---|
| 538 | * Creates a rotated version of the input image.
|
---|
[2512] | 539 | *
|
---|
[1879] | 540 | * @param c The component to get properties useful for painting, e.g. the foreground or
|
---|
| 541 | * background color.
|
---|
| 542 | * @param icon the image to be rotated.
|
---|
[1295] | 543 | * @param rotatedAngle the rotated angle, in degree, clockwise. It could be any double but we
|
---|
[1879] | 544 | * will mod it with 360 before using it.
|
---|
[2512] | 545 | *
|
---|
[1295] | 546 | * @return the image after rotating.
|
---|
| 547 | */
|
---|
| 548 | public static ImageIcon createRotatedImage(Component c, Icon icon, double rotatedAngle) {
|
---|
| 549 | // convert rotatedAngle to a value from 0 to 360
|
---|
| 550 | double originalAngle = rotatedAngle % 360;
|
---|
| 551 | if (rotatedAngle != 0 && originalAngle == 0) {
|
---|
| 552 | originalAngle = 360.0;
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | // convert originalAngle to a value from 0 to 90
|
---|
| 556 | double angle = originalAngle % 90;
|
---|
| 557 | if (originalAngle != 0.0 && angle == 0.0) {
|
---|
| 558 | angle = 90.0;
|
---|
| 559 | }
|
---|
| 560 |
|
---|
| 561 | double radian = Math.toRadians(angle);
|
---|
| 562 |
|
---|
| 563 | int iw = icon.getIconWidth();
|
---|
| 564 | int ih = icon.getIconHeight();
|
---|
| 565 | int w;
|
---|
| 566 | int h;
|
---|
| 567 |
|
---|
| 568 | if ((originalAngle >= 0 && originalAngle <= 90) || (originalAngle > 180 && originalAngle <= 270)) {
|
---|
| 569 | w = (int) (iw * Math.sin(DEGREE_90 - radian) + ih * Math.sin(radian));
|
---|
| 570 | h = (int) (iw * Math.sin(radian) + ih * Math.sin(DEGREE_90 - radian));
|
---|
[1879] | 571 | } else {
|
---|
[1295] | 572 | w = (int) (ih * Math.sin(DEGREE_90 - radian) + iw * Math.sin(radian));
|
---|
| 573 | h = (int) (ih * Math.sin(radian) + iw * Math.sin(DEGREE_90 - radian));
|
---|
| 574 | }
|
---|
| 575 | BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
|
---|
| 576 | Graphics g = image.getGraphics();
|
---|
| 577 | Graphics2D g2d = (Graphics2D) g.create();
|
---|
| 578 |
|
---|
| 579 | // calculate the center of the icon.
|
---|
| 580 | int cx = iw / 2;
|
---|
| 581 | int cy = ih / 2;
|
---|
| 582 |
|
---|
| 583 | // move the graphics center point to the center of the icon.
|
---|
[1879] | 584 | g2d.translate(w / 2, h / 2);
|
---|
[1295] | 585 |
|
---|
| 586 | // rotate the graphics about the center point of the icon
|
---|
| 587 | g2d.rotate(Math.toRadians(originalAngle));
|
---|
| 588 |
|
---|
| 589 | g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
---|
| 590 | icon.paintIcon(c, g2d, -cx, -cy);
|
---|
| 591 |
|
---|
| 592 | g2d.dispose();
|
---|
| 593 | return new ImageIcon(image);
|
---|
| 594 | }
|
---|
[1814] | 595 |
|
---|
| 596 | /**
|
---|
| 597 | * Replies the icon for an OSM primitive type
|
---|
| 598 | * @param type the type
|
---|
| 599 | * @return the icon
|
---|
| 600 | */
|
---|
| 601 | public static ImageIcon get(OsmPrimitiveType type) throws IllegalArgumentException {
|
---|
[2853] | 602 | CheckParameterUtil.ensureParameterNotNull(type, "type");
|
---|
[1879] | 603 | return get("data", type.getAPIName());
|
---|
[1814] | 604 | }
|
---|
[3862] | 605 |
|
---|
| 606 | public static BufferedImage sanitize(Image img) {
|
---|
| 607 | (new ImageIcon(img)).getImage(); // load competely
|
---|
| 608 | int width = img.getWidth(null);
|
---|
| 609 | int height = img.getHeight(null);
|
---|
| 610 | BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
---|
| 611 | result.getGraphics().drawImage(img, 0, 0, null);
|
---|
| 612 | return result;
|
---|
| 613 | }
|
---|
[4256] | 614 |
|
---|
[4271] | 615 | public static Image createImageFromSvg(SVGDiagram svg, Dimension dim) {
|
---|
| 616 | float realWidth = svg.getWidth();
|
---|
| 617 | float realHeight = svg.getHeight();
|
---|
| 618 | int width = Math.round(realWidth);
|
---|
| 619 | int height = Math.round(realHeight);
|
---|
| 620 | Double scaleX = null, scaleY = null;
|
---|
| 621 | if (dim.width != -1) {
|
---|
| 622 | width = dim.width;
|
---|
| 623 | scaleX = (double) width / realWidth;
|
---|
| 624 | if (dim.height == -1) {
|
---|
| 625 | scaleY = scaleX;
|
---|
| 626 | height = (int) Math.round(realHeight * scaleY);
|
---|
| 627 | } else {
|
---|
| 628 | height = dim.height;
|
---|
| 629 | scaleY = (double) height / realHeight;
|
---|
| 630 | }
|
---|
| 631 | } else if (dim.height != -1) {
|
---|
| 632 | height = dim.height;
|
---|
| 633 | scaleX = scaleY = (double) height / realHeight;
|
---|
| 634 | width = (int) Math.round(realWidth * scaleX);
|
---|
| 635 | }
|
---|
| 636 | Image img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
---|
[4256] | 637 | Graphics2D g = ((BufferedImage) img).createGraphics();
|
---|
[4271] | 638 | g.setClip(0, 0, width, height);
|
---|
| 639 | if (scaleX != null) {
|
---|
| 640 | g.scale(scaleX, scaleY);
|
---|
| 641 | }
|
---|
[4256] | 642 | g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
---|
| 643 | try {
|
---|
[4271] | 644 | svg.render(g);
|
---|
[4256] | 645 | } catch (SVGException ex) {
|
---|
| 646 | return null;
|
---|
| 647 | }
|
---|
| 648 | return img;
|
---|
| 649 | }
|
---|
| 650 |
|
---|
| 651 | private static SVGUniverse getSvgUniverse() {
|
---|
| 652 | if (svgUniverse == null) {
|
---|
| 653 | svgUniverse = new SVGUniverse();
|
---|
| 654 | }
|
---|
| 655 | return svgUniverse;
|
---|
| 656 | }
|
---|
[17] | 657 | }
|
---|