Changeset 4256 in josm for trunk/src


Ignore:
Timestamp:
2011-07-18T00:05:50+02:00 (13 years ago)
Author:
bastiK
Message:

see #6560 - basic svg support, includes kitfox svgsalamander, r 98, patched

Location:
trunk/src
Files:
106 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r4255 r4256  
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
     5
     6import com.kitfox.svg.SVGDiagram;
     7import com.kitfox.svg.SVGException;
     8import com.kitfox.svg.SVGUniverse;
    59
    610import java.awt.Component;
     
    2024import java.io.InputStream;
    2125import java.net.MalformedURLException;
     26import java.net.URI;
    2227import java.net.URL;
    2328import java.util.Arrays;
     
    4247public class ImageProvider {
    4348
     49    private static SVGUniverse svgUniverse;
     50
    4451    /**
    4552     * Position of an overlay icon
     
    4855    public static enum OverlayPosition {
    4956        NORTHWEST, NORTHEAST, SOUTHWEST, SOUTHEAST
     57    }
     58
     59    public static enum ImageType {
     60        PNG, SVG
    5061    }
    5162
     
    7182     * Return an image from the specified location.
    7283     *
    73      * @param subdir The position of the directory, e.g. "layer"
    74      * @param name The icons name (without 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)
    7586     * @return The requested Image.
    7687     */
     
    8697    }
    8798
     99    /**
     100     * Shortcut for get("", name);
     101     */
     102    public static ImageIcon get(String name) {
     103        return get("", name);
     104    }
     105
    88106    public static ImageIcon getIfAvailable(String subdir, String name) {
    89107        return getIfAvailable((Collection<String>) null, null, subdir, name);
     
    111129     * or something like
    112130     *   dirs.get(i)+"/"+subdir+"/"+name+".png".
    113      * @param dirs      Directories to look.
     131     * @param dirs      Directories to look (may be null).
    114132     * @param id        An id used for caching. Id is not used for cache if name starts with http://. (URL is unique anyway.)
    115133     * @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).
    118137     * @param sanitize  If the image should be repainted to a new BufferedImage to work
    119138     *                  around certain issues.
     
    133152        if (name == null)
    134153            return null;
     154        ImageType type = name.toLowerCase().endsWith("svg") ? ImageType.SVG : ImageType.PNG;
     155
    135156        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
    138167        if (subdir == null) {
    139168            subdir = "";
     
    141170            subdir += "/";
    142171        }
    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
    156198            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
    159206            // getImageUrl() does a ton of "stat()" calls and gets expensive
    160207            // and redundant when you have a whole ton of objects. So,
     
    162209            // and don't bother to create a URL unless we're actually
    163210            // 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) {
    193243        ZipFile zipFile = null;
    194244        Image img = null;
     
    205255                try {
    206256                    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;
    212271                    }
    213                     img = Toolkit.getDefaultToolkit().createImage(buf);
    214272                } finally {
    215273                    if (is != null) {
     
    231289    }
    232290
     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
    233305    private static URL getImageUrl(String path, String name) {
    234         if (path.startsWith("resource://")) {
     306        if (path != null && path.startsWith("resource://")) {
    235307            String p = path.substring("resource://".length());
    236308            for (ClassLoader source : PluginHandler.getResourceClassLoaders()) {
     
    280352        }
    281353
     354        // Absolute path?
     355        u = getImageUrl(null, imageName);
     356        if (u != null)
     357            return u;
     358
    282359        // Try plugins and josm classloader
    283360        u = getImageUrl("resource://images/", imageName);
     
    294371                return u;
    295372        }
     373
    296374        return null;
    297     }
    298 
    299     /**
    300      * Shortcut for get("", name);
    301      */
    302     public static ImageIcon get(String name) {
    303         return get("", name);
    304375    }
    305376
     
    438509        return result;
    439510    }
     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    }
    440534}
Note: See TracChangeset for help on using the changeset viewer.