Ignore:
Timestamp:
2013-06-11T01:01:28+02:00 (11 years ago)
Author:
Don-vip
Message:

fix #8742 - update svgsalamander to release 0.1.18+patch (fix bug SVGSALAMANDER-26) -> allow to open more SVG files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/com/kitfox/svg/Gradient.java

    r4256 r6002  
    11/*
    2  * Gradient.java
     2 * SVG Salamander
     3 * Copyright (c) 2004, Mark McKay
     4 * All rights reserved.
    35 *
     6 * Redistribution and use in source and binary forms, with or
     7 * without modification, are permitted provided that the following
     8 * conditions are met:
    49 *
    5  *  The Salamander Project - 2D and 3D graphics libraries in Java
    6  *  Copyright (C) 2004 Mark McKay
     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.
    717 *
    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
     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
    2433 *
    2534 * Created on January 26, 2004, 3:25 AM
    2635 */
    27 
    2836package com.kitfox.svg;
    2937
    30 import java.net.*;
    31 import java.util.*;
    32 import java.awt.geom.*;
    33 import java.awt.*;
    34 
    35 import com.kitfox.svg.xml.*;
     38import com.kitfox.svg.xml.StyleAttribute;
     39import java.awt.Color;
     40import java.awt.geom.AffineTransform;
     41import java.net.URI;
     42import java.util.ArrayList;
     43import java.util.Iterator;
     44import java.util.logging.Level;
     45import java.util.logging.Logger;
    3646
    3747/**
     
    4151abstract public class Gradient extends FillElement
    4252{
    43 
     53    public static final String TAG_NAME = "gradient";
     54   
    4455    public static final int SM_PAD = 0;
    4556    public static final int SM_REPEAT = 1;
    4657    public static final int SM_REFLECT = 2;
    47 
    4858    int spreadMethod = SM_PAD;
    49 
    5059    public static final int GU_OBJECT_BOUNDING_BOX = 0;
    5160    public static final int GU_USER_SPACE_ON_USE = 1;
    52 
    5361    protected int gradientUnits = GU_OBJECT_BOUNDING_BOX;
    54 
    5562    //Either this gradient contains a list of stops, or it will take it's
    5663    // stops from the referenced gradient
    5764    ArrayList stops = new ArrayList();
    5865    URI stopRef = null;
    59 //    Gradient stopRef = null;
    60 
    6166    protected AffineTransform gradientTransform = null;
    62 
     67   
    6368    //Cache arrays of stop values here
    6469    float[] stopFractions;
    6570    Color[] stopColors;
    6671
    67     /** Creates a new instance of Gradient */
    68     public Gradient() {
    69     }
    70    
     72    /**
     73     * Creates a new instance of Gradient
     74     */
     75    public Gradient()
     76    {
     77    }
     78
     79    public String getTagName()
     80    {
     81        return TAG_NAME;
     82    }
     83
    7184    /**
    7285     * Called after the start element but before the end element to indicate
     
    7790        super.loaderAddChild(helper, child);
    7891
    79         if (!(child instanceof Stop)) return;
    80         appendStop((Stop)child);
     92        if (!(child instanceof Stop))
     93        {
     94            return;
     95        }
     96        appendStop((Stop) child);
    8197    }
    8298
     
    84100    {
    85101        super.build();
    86        
     102
    87103        StyleAttribute sty = new StyleAttribute();
    88104        String strn;
    89        
     105
    90106        if (getPres(sty.setName("spreadMethod")))
    91107        {
    92108            strn = sty.getStringValue().toLowerCase();
    93             if (strn.equals("repeat")) spreadMethod = SM_REPEAT;
    94             else if (strn.equals("reflect")) spreadMethod = SM_REFLECT;
    95             else spreadMethod = SM_PAD;
     109            if (strn.equals("repeat"))
     110            {
     111                spreadMethod = SM_REPEAT;
     112            } else if (strn.equals("reflect"))
     113            {
     114                spreadMethod = SM_REFLECT;
     115            } else
     116            {
     117                spreadMethod = SM_PAD;
     118            }
    96119        }
    97120
     
    99122        {
    100123            strn = sty.getStringValue().toLowerCase();
    101             if (strn.equals("userspaceonuse")) gradientUnits = GU_USER_SPACE_ON_USE;
    102             else gradientUnits = GU_OBJECT_BOUNDING_BOX;
    103         }
    104 
    105         if (getPres(sty.setName("gradientTransform"))) gradientTransform = parseTransform(sty.getStringValue());
     124            if (strn.equals("userspaceonuse"))
     125            {
     126                gradientUnits = GU_USER_SPACE_ON_USE;
     127            } else
     128            {
     129                gradientUnits = GU_OBJECT_BOUNDING_BOX;
     130            }
     131        }
     132
     133        if (getPres(sty.setName("gradientTransform")))
     134        {
     135            gradientTransform = parseTransform(sty.getStringValue());
     136        }
    106137        //If we still don't have one, set it to identity
    107         if (gradientTransform == null) gradientTransform = new AffineTransform();
    108 
    109        
     138        if (gradientTransform == null)
     139        {
     140            gradientTransform = new AffineTransform();
     141        }
     142
     143
    110144        //Check to see if we're using our own stops or referencing someone else's
    111145        if (getPres(sty.setName("xlink:href")))
    112146        {
    113             try {
     147            try
     148            {
    114149                stopRef = sty.getURIValue(getXMLBase());
    115150//System.err.println("Gradient: " + sty.getStringValue() + ", " + getXMLBase() + ", " + src);
    116151//                URI src = getXMLBase().resolve(href);
    117152//                stopRef = (Gradient)diagram.getUniverse().getElement(src);
    118             }
    119             catch (Exception e)
     153            } catch (Exception e)
    120154            {
    121155                throw new SVGException("Could not resolve relative URL in Gradient: " + sty.getStringValue() + ", " + getXMLBase(), e);
     
    123157        }
    124158    }
    125    
     159
    126160    public float[] getStopFractions()
    127161    {
    128162        if (stopRef != null)
    129163        {
    130             Gradient grad = (Gradient)diagram.getUniverse().getElement(stopRef);
     164            Gradient grad = (Gradient) diagram.getUniverse().getElement(stopRef);
    131165            return grad.getStopFractions();
    132166        }
    133167
    134         if (stopFractions != null) return stopFractions;
     168        if (stopFractions != null)
     169        {
     170            return stopFractions;
     171        }
    135172
    136173        stopFractions = new float[stops.size()];
     
    138175        for (Iterator it = stops.iterator(); it.hasNext();)
    139176        {
    140             Stop stop = (Stop)it.next();
     177            Stop stop = (Stop) it.next();
    141178            float val = stop.offset;
    142             if (idx != 0 && val < stopFractions[idx - 1]) val = stopFractions[idx - 1];
     179            if (idx != 0 && val < stopFractions[idx - 1])
     180            {
     181                val = stopFractions[idx - 1];
     182            }
    143183            stopFractions[idx++] = val;
    144184        }
     
    151191        if (stopRef != null)
    152192        {
    153             Gradient grad = (Gradient)diagram.getUniverse().getElement(stopRef);
     193            Gradient grad = (Gradient) diagram.getUniverse().getElement(stopRef);
    154194            return grad.getStopColors();
    155195        }
    156196
    157         if (stopColors != null) return stopColors;
     197        if (stopColors != null)
     198        {
     199            return stopColors;
     200        }
    158201
    159202        stopColors = new Color[stops.size()];
     
    161204        for (Iterator it = stops.iterator(); it.hasNext();)
    162205        {
    163             Stop stop = (Stop)it.next();
     206            Stop stop = (Stop) it.next();
    164207            int stopColorVal = stop.color.getRGB();
    165             Color stopColor = new Color((stopColorVal >> 16) & 0xff, (stopColorVal >> 8) & 0xff, stopColorVal & 0xff, clamp((int)(stop.opacity * 255), 0, 255));
     208            Color stopColor = new Color((stopColorVal >> 16) & 0xff, (stopColorVal >> 8) & 0xff, stopColorVal & 0xff, clamp((int) (stop.opacity * 255), 0, 255));
    166209            stopColors[idx++] = stopColor;
    167210        }
     
    169212        return stopColors;
    170213    }
    171    
     214
    172215    public void setStops(Color[] colors, float[] fractions)
    173216    {
     
    176219            throw new IllegalArgumentException();
    177220        }
    178        
     221
    179222        this.stopColors = colors;
    180223        this.stopFractions = fractions;
    181224        stopRef = null;
    182225    }
    183    
     226
    184227    private int clamp(int val, int min, int max)
    185228    {
    186         if (val < min) return min;
    187         if (val > max) return max;
     229        if (val < min)
     230        {
     231            return min;
     232        }
     233        if (val > max)
     234        {
     235            return max;
     236        }
    188237        return val;
    189238    }
    190    
     239
    191240    public void setStopRef(URI grad)
    192241    {
     
    200249
    201250    /**
    202      * Updates all attributes in this diagram associated with a time event.
    203      * Ie, all attributes with track information.
     251     * Updates all attributes in this diagram associated with a time event. Ie,
     252     * all attributes with track information.
     253     *
    204254     * @return - true if this node has changed state as a result of the time
    205255     * update
     
    214264        boolean shapeChange = false;
    215265        String strn;
    216        
     266
    217267
    218268        if (getPres(sty.setName("spreadMethod")))
     
    220270            int newVal;
    221271            strn = sty.getStringValue().toLowerCase();
    222             if (strn.equals("repeat")) newVal = SM_REPEAT;
    223             else if (strn.equals("reflect")) newVal = SM_REFLECT;
    224             else newVal = SM_PAD;
     272            if (strn.equals("repeat"))
     273            {
     274                newVal = SM_REPEAT;
     275            } else if (strn.equals("reflect"))
     276            {
     277                newVal = SM_REFLECT;
     278            } else
     279            {
     280                newVal = SM_PAD;
     281            }
    225282            if (spreadMethod != newVal)
    226283            {
     
    229286            }
    230287        }
    231        
     288
    232289        if (getPres(sty.setName("gradientUnits")))
    233290        {
    234291            int newVal;
    235292            strn = sty.getStringValue().toLowerCase();
    236             if (strn.equals("userspaceonuse")) newVal = GU_USER_SPACE_ON_USE;
    237             else newVal = GU_OBJECT_BOUNDING_BOX;
     293            if (strn.equals("userspaceonuse"))
     294            {
     295                newVal = GU_USER_SPACE_ON_USE;
     296            } else
     297            {
     298                newVal = GU_OBJECT_BOUNDING_BOX;
     299            }
    238300            if (newVal != gradientUnits)
    239301            {
     
    253315        }
    254316
    255        
     317
    256318        //Check to see if we're using our own stops or referencing someone else's
    257319        if (getPres(sty.setName("xlink:href")))
    258320        {
    259             try {
     321            try
     322            {
    260323                URI newVal = sty.getURIValue(getXMLBase());
    261324                if ((newVal == null && stopRef != null) || !newVal.equals(stopRef))
     
    264327                    stateChange = true;
    265328                }
    266             }
    267             catch (Exception e)
    268             {
    269                 e.printStackTrace();
    270             }
    271         }
    272        
     329            } catch (Exception e)
     330            {
     331                Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
     332                    "Could not parse xlink:href", e);
     333            }
     334        }
     335
    273336        //Check stops, if any
    274337        for (Iterator it = stops.iterator(); it.hasNext();)
    275338        {
    276             Stop stop = (Stop)it.next();
     339            Stop stop = (Stop) it.next();
    277340            if (stop.updateTime(curTime))
    278341            {
     
    282345            }
    283346        }
    284        
     347
    285348        return stateChange;
    286349    }
    287 
    288350}
Note: See TracChangeset for help on using the changeset viewer.