Ignore:
Timestamp:
2017-02-02T00:08:08+01:00 (7 years ago)
Author:
Don-vip
Message:

see #14319 - update to latest version of svgSalamander (2017-01-07, patched)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/com/kitfox/svg/app/beans/SVGIcon.java

    r10787 r11525  
    44 * All rights reserved.
    55 *
    6  * Redistribution and use in source and binary forms, with or 
     6 * Redistribution and use in source and binary forms, with or
    77 * without modification, are permitted provided that the following
    88 * conditions are met:
    99 *
    10  *   - Redistributions of source code must retain the above 
     10 *   - Redistributions of source code must retain the above
    1111 *     copyright notice, this list of conditions and the following
    1212 *     disclaimer.
    1313 *   - Redistributions in binary form must reproduce the above
    1414 *     copyright notice, this list of conditions and the following
    15  *     disclaimer in the documentation and/or other materials 
     15 *     disclaimer in the documentation and/or other materials
    1616 *     provided with the distribution.
    1717 *
     
    2727 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    2828 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    29  * OF THE POSSIBILITY OF SUCH DAMAGE. 
    30  * 
     29 * OF THE POSSIBILITY OF SUCH DAMAGE.
     30 *
    3131 * Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
    3232 * projects can be found at http://www.kitfox.com
     
    3737package com.kitfox.svg.app.beans;
    3838
    39 import com.kitfox.svg.SVGCache;
    40 import com.kitfox.svg.SVGDiagram;
    41 import com.kitfox.svg.SVGException;
    42 import com.kitfox.svg.SVGUniverse;
    4339import java.awt.Component;
    4440import java.awt.Dimension;
     
    5450import java.beans.PropertyChangeSupport;
    5551import java.net.URI;
     52
    5653import javax.swing.ImageIcon;
    5754
     55import com.kitfox.svg.SVGCache;
     56import com.kitfox.svg.SVGDiagram;
     57import com.kitfox.svg.SVGException;
     58import com.kitfox.svg.SVGUniverse;
    5859
    5960/**
     
    6667
    6768    public static final String PROP_AUTOSIZE = "PROP_AUTOSIZE";
    68    
     69
    6970    private final PropertyChangeSupport changes = new PropertyChangeSupport(this);
    70    
     71
    7172    SVGUniverse svgUniverse = SVGCache.getSVGUniverse();
    7273    public static final int INTERP_NEAREST_NEIGHBOR = 0;
    7374    public static final int INTERP_BILINEAR = 1;
    7475    public static final int INTERP_BICUBIC = 2;
    75    
     76
    7677    private boolean antiAlias;
    7778    private int interpolation = INTERP_NEAREST_NEIGHBOR;
    7879    private boolean clipToViewbox;
    79    
     80
    8081    URI svgURI;
    81    
     82
    8283//    private boolean scaleToFit;
    8384    AffineTransform scaleXform = new AffineTransform();
     
    8990    public static final int AUTOSIZE_STRETCH = 4;
    9091    private int autosize = AUTOSIZE_NONE;
    91    
     92
    9293    Dimension preferredSize;
    93    
     94
    9495    /** Creates a new instance of SVGIcon */
    9596    public SVGIcon()
    9697    {
    9798    }
    98    
     99
    99100    public void addPropertyChangeListener(PropertyChangeListener p)
    100101    {
    101102        changes.addPropertyChangeListener(p);
    102103    }
    103    
     104
    104105    public void removePropertyChangeListener(PropertyChangeListener p)
    105106    {
    106107        changes.removePropertyChangeListener(p);
    107108    }
    108    
     109
     110    @Override
    109111    public Image getImage()
    110112    {
     
    117119     * @return height of this icon
    118120     */
    119     public int getIconHeight()
     121    public int getIconHeightIgnoreAutosize()
    120122    {
    121123        if (preferredSize != null &&
    122                 (autosize == AUTOSIZE_VERT || autosize == AUTOSIZE_STRETCH 
     124                (autosize == AUTOSIZE_VERT || autosize == AUTOSIZE_STRETCH
    123125                || autosize == AUTOSIZE_BESTFIT))
    124126        {
    125127            return preferredSize.height;
    126128        }
    127        
     129
    128130        SVGDiagram diagram = svgUniverse.getDiagram(svgURI);
    129131        if (diagram == null)
     
    133135        return (int)diagram.getHeight();
    134136    }
    135    
     137
    136138    /**
    137139     * @return width of this icon
    138140     */
    139     public int getIconWidth()
     141    public int getIconWidthIgnoreAutosize()
    140142    {
    141143        if (preferredSize != null &&
    142                 (autosize == AUTOSIZE_HORIZ || autosize == AUTOSIZE_STRETCH 
     144                (autosize == AUTOSIZE_HORIZ || autosize == AUTOSIZE_STRETCH
    143145                || autosize == AUTOSIZE_BESTFIT))
    144146        {
    145147            return preferredSize.width;
    146148        }
    147        
     149
    148150        SVGDiagram diagram = svgUniverse.getDiagram(svgURI);
    149151        if (diagram == null)
     
    153155        return (int)diagram.getWidth();
    154156    }
    155    
     157
     158    private boolean isAutoSizeBestFitUseFixedHeight(final int iconWidthIgnoreAutosize, final int iconHeightIgnoreAutosize,
     159            final SVGDiagram diagram)
     160    {
     161        return iconHeightIgnoreAutosize/diagram.getHeight() < iconWidthIgnoreAutosize/diagram.getWidth();
     162    }
     163
     164    @Override
     165    public int getIconWidth()
     166    {
     167        final int iconWidthIgnoreAutosize = getIconWidthIgnoreAutosize();
     168        final int iconHeightIgnoreAutosize = getIconHeightIgnoreAutosize();
     169        final SVGDiagram diagram = svgUniverse.getDiagram(svgURI);
     170        if (preferredSize != null && (autosize == AUTOSIZE_VERT ||
     171                                     (autosize == AUTOSIZE_BESTFIT && isAutoSizeBestFitUseFixedHeight(iconWidthIgnoreAutosize, iconHeightIgnoreAutosize, diagram))))
     172        {
     173            final double aspectRatio = diagram.getHeight()/diagram.getWidth();
     174            return (int)(iconHeightIgnoreAutosize / aspectRatio);
     175        }
     176        else
     177        {
     178            return iconWidthIgnoreAutosize;
     179        }
     180    }
     181
     182    @Override
     183    public int getIconHeight()
     184    {
     185        final int iconWidthIgnoreAutosize = getIconWidthIgnoreAutosize();
     186        final int iconHeightIgnoreAutosize = getIconHeightIgnoreAutosize();
     187        final SVGDiagram diagram = svgUniverse.getDiagram(svgURI);
     188        if (preferredSize != null && (autosize == AUTOSIZE_HORIZ ||
     189                                      (autosize == AUTOSIZE_BESTFIT && !isAutoSizeBestFitUseFixedHeight(iconWidthIgnoreAutosize, iconHeightIgnoreAutosize, diagram))))
     190        {
     191            final double aspectRatio = diagram.getHeight()/diagram.getWidth();
     192            return (int)(iconWidthIgnoreAutosize * aspectRatio);
     193        }
     194        else
     195        {
     196            return iconHeightIgnoreAutosize;
     197        }
     198    }
     199
    156200    /**
    157201     * Draws the icon to the specified component.
     
    161205     * @param y - Y coordinate to draw icon
    162206     */
     207    @Override
    163208    public void paintIcon(Component comp, Graphics gg, int x, int y)
    164209    {
    165         //Copy graphics object so that 
     210        //Copy graphics object so that
    166211        Graphics2D g = (Graphics2D)gg.create();
    167212        paintIcon(comp, g, x, y);
    168213        g.dispose();
    169214    }
    170    
     215
    171216    private void paintIcon(Component comp, Graphics2D g, int x, int y)
    172217    {
    173218        Object oldAliasHint = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
    174219        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antiAlias ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
    175        
     220
    176221        Object oldInterpolationHint = g.getRenderingHint(RenderingHints.KEY_INTERPOLATION);
    177222        switch (interpolation)
     
    187232                break;
    188233        }
    189        
    190        
     234
     235
    191236        SVGDiagram diagram = svgUniverse.getDiagram(svgURI);
    192237        if (diagram == null)
     
    194239            return;
    195240        }
    196        
     241
    197242        g.translate(x, y);
    198243        diagram.setIgnoringClipHeuristic(!clipToViewbox);
     
    201246            g.setClip(new Rectangle2D.Float(0, 0, diagram.getWidth(), diagram.getHeight()));
    202247        }
    203        
    204        
     248
     249
    205250        if (autosize == AUTOSIZE_NONE)
    206251        {
     
    217262            return;
    218263        }
    219        
    220         final int width = getIconWidth();
    221         final int height = getIconHeight();
     264
     265        final int width = getIconWidthIgnoreAutosize();
     266        final int height = getIconHeightIgnoreAutosize();
    222267//        int width = getWidth();
    223268//        int height = getHeight();
    224        
     269
    225270        if (width == 0 || height == 0)
    226271        {
    227272            return;
    228273        }
    229        
     274
    230275//        if (width == 0 || height == 0)
    231276//        {
     
    236281//            return;
    237282//        }
    238        
     283
    239284//        g.setClip(0, 0, width, height);
    240        
    241        
     285
     286
    242287//        final Rectangle2D.Double rect = new Rectangle2D.Double();
    243288//        diagram.getViewRect(rect);
    244 //       
     289//
    245290//        scaleXform.setToScale(width / rect.width, height / rect.height);
    246291        double diaWidth = diagram.getWidth();
    247292        double diaHeight = diagram.getHeight();
    248        
     293
    249294        double scaleW = 1;
    250295        double scaleH = 1;
    251296        if (autosize == AUTOSIZE_BESTFIT)
    252297        {
    253             scaleW = scaleH = (height / diaHeight < width / diaWidth) 
     298            scaleW = scaleH = (height / diaHeight < width / diaWidth)
    254299                    ? height / diaHeight : width / diaWidth;
    255300        }
     
    268313        }
    269314        scaleXform.setToScale(scaleW, scaleH);
    270        
     315
    271316        AffineTransform oldXform = g.getTransform();
    272317        g.transform(scaleXform);
    273        
     318
    274319        try
    275320        {
     
    280325            throw new RuntimeException(e);
    281326        }
    282        
     327
    283328        g.setTransform(oldXform);
    284        
    285        
     329
     330
    286331        g.translate(-x, -y);
    287        
     332
    288333        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAliasHint);
    289334        if (oldInterpolationHint != null)
     
    292337        }
    293338    }
    294    
     339
    295340    /**
    296341     * @return the universe this icon draws it's SVGDiagrams from
     
    300345        return svgUniverse;
    301346    }
    302    
     347
    303348    public void setSvgUniverse(SVGUniverse svgUniverse)
    304349    {
     
    307352        changes.firePropertyChange("svgUniverse", old, svgUniverse);
    308353    }
    309    
     354
    310355    /**
    311356     * @return the uni of the document being displayed by this icon
     
    315360        return svgURI;
    316361    }
    317    
     362
    318363    /**
    319364     * Loads an SVG document from a URI.
     
    324369        URI old = this.svgURI;
    325370        this.svgURI = svgURI;
    326        
     371
    327372        SVGDiagram diagram = svgUniverse.getDiagram(svgURI);
    328373        if (diagram != null)
     
    335380            diagram.setDeviceViewport(new Rectangle(0, 0, size.width, size.height));
    336381        }
    337        
     382
    338383        changes.firePropertyChange("svgURI", old, svgURI);
    339384    }
    340    
     385
    341386    /**
    342387     * Loads an SVG document from the classpath.  This function is equivilant to
     
    347392    {
    348393        URI old = this.svgURI;
    349        
     394
    350395        try
    351396        {
    352397            svgURI = new URI(getClass().getResource(resourcePath).toString());
    353398            changes.firePropertyChange("svgURI", old, svgURI);
    354            
     399
    355400            SVGDiagram diagram = svgUniverse.getDiagram(svgURI);
    356401            if (diagram != null)
     
    358403                diagram.setDeviceViewport(new Rectangle(0, 0, preferredSize.width, preferredSize.height));
    359404            }
    360            
     405
    361406        }
    362407        catch (Exception e)
     
    365410        }
    366411    }
    367    
    368     /**
    369      * If this SVG document has a viewbox, if scaleToFit is set, will scale the viewbox to match the
    370      * preferred size of this icon
    371      * @deprecated
    372      * @return
    373      */
    374     public boolean isScaleToFit()
    375     {
    376         return autosize == AUTOSIZE_STRETCH;
    377     }
    378    
    379     /**
    380      * @deprecated
    381      * @return
    382      */
    383     public void setScaleToFit(boolean scaleToFit)
    384     {
    385         setAutosize(AUTOSIZE_STRETCH);
    386 //        boolean old = this.scaleToFit;
    387 //        this.scaleToFit = scaleToFit;
    388 //        firePropertyChange("scaleToFit", old, scaleToFit);
    389     }
    390    
     412
    391413    public Dimension getPreferredSize()
    392414    {
     
    400422            }
    401423        }
    402        
     424
    403425        return new Dimension(preferredSize);
    404426    }
    405    
     427
    406428    public void setPreferredSize(Dimension preferredSize)
    407429    {
    408430        Dimension old = this.preferredSize;
    409431        this.preferredSize = preferredSize;
    410        
     432
    411433        SVGDiagram diagram = svgUniverse.getDiagram(svgURI);
    412434        if (diagram != null)
     
    414436            diagram.setDeviceViewport(new Rectangle(0, 0, preferredSize.width, preferredSize.height));
    415437        }
    416        
     438
    417439        changes.firePropertyChange("preferredSize", old, preferredSize);
    418440    }
    419    
    420    
     441
    421442    /**
    422443     * @return true if antiAliasing is turned on.
    423      * @deprecated
    424      */
    425     public boolean getUseAntiAlias()
    426     {
    427         return getAntiAlias();
    428     }
    429    
    430     /**
    431      * @param antiAlias true to use antiAliasing.
    432      * @deprecated
    433      */
    434     public void setUseAntiAlias(boolean antiAlias)
    435     {
    436         setAntiAlias(antiAlias);
    437     }
    438    
    439     /**
    440      * @return true if antiAliasing is turned on.
    441444     */
    442445    public boolean getAntiAlias()
     
    444447        return antiAlias;
    445448    }
    446    
     449
    447450    /**
    448451     * @param antiAlias true to use antiAliasing.
     
    454457        changes.firePropertyChange("antiAlias", old, antiAlias);
    455458    }
    456    
     459
    457460    /**
    458461     * @return interpolation used in rescaling images
     
    462465        return interpolation;
    463466    }
    464    
     467
    465468    /**
    466469     * @param interpolation Interpolation value used in rescaling images.
     
    476479        changes.firePropertyChange("interpolation", old, interpolation);
    477480    }
    478    
     481
    479482    /**
    480483     * clipToViewbox will set a clip box equivilant to the SVG's viewbox before
     
    485488        return clipToViewbox;
    486489    }
    487    
     490
    488491    public void setClipToViewbox(boolean clipToViewbox)
    489492    {
     
    508511        changes.firePropertyChange(PROP_AUTOSIZE, oldAutosize, autosize);
    509512    }
    510        
     513
    511514}
Note: See TracChangeset for help on using the changeset viewer.