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/PatternSVG.java

    r4256 r6002  
    11/*
    2  * Gradient.java
    3  *
    4  *
    5  *  The Salamander Project - 2D and 3D graphics libraries in Java
    6  *  Copyright (C) 2004 Mark McKay
    7  *
    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
     2 * SVG Salamander
     3 * Copyright (c) 2004, Mark McKay
     4 * All rights reserved.
     5 *
     6 * Redistribution and use in source and binary forms, with or
     7 * without modification, are permitted provided that the following
     8 * conditions are met:
     9 *
     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.
     17 *
     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
     38import com.kitfox.svg.pattern.PatternPaint;
    3039import com.kitfox.svg.xml.StyleAttribute;
    31 import java.net.*;
    32 import java.util.*;
    33 import java.awt.geom.*;
    34 import java.awt.*;
    35 import java.awt.image.*;
    36 
    37 import com.kitfox.svg.pattern.*;
    38 import com.kitfox.svg.xml.*;
    39 import org.xml.sax.*;
     40import java.awt.Graphics2D;
     41import java.awt.Paint;
     42import java.awt.RenderingHints;
     43import java.awt.TexturePaint;
     44import java.awt.geom.AffineTransform;
     45import java.awt.geom.Point2D;
     46import java.awt.geom.Rectangle2D;
     47import java.awt.image.BufferedImage;
     48import java.net.URI;
     49import java.util.Iterator;
     50import java.util.logging.Level;
     51import java.util.logging.Logger;
    4052
    4153/**
     
    4355 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
    4456 */
    45 public class PatternSVG extends FillElement {
    46 
     57public class PatternSVG extends FillElement
     58{
     59    public static final String TAG_NAME = "pattern";
     60   
    4761    public static final int GU_OBJECT_BOUNDING_BOX = 0;
    4862    public static final int GU_USER_SPACE_ON_USE = 1;
    49 
    5063    int gradientUnits = GU_OBJECT_BOUNDING_BOX;
    51 
    5264    float x;
    5365    float y;
    5466    float width;
    5567    float height;
    56 
    5768    AffineTransform patternXform = new AffineTransform();
    5869    Rectangle2D.Float viewBox;
    59 
    6070    Paint texPaint;
    6171
    62     /** Creates a new instance of Gradient */
    63     public PatternSVG() {
    64     }
    65 /*
    66     public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
    67     {
    68                 //Load style string
    69         super.loaderStartElement(helper, attrs, parent);
    70 
    71         String href = attrs.getValue("xlink:href");
    72         //If we have a link to another pattern, initialize ourselves with it's values
    73         if (href != null)
    74         {
    75 //System.err.println("Gradient.loaderStartElement() href '" + href + "'");
    76             try {
    77                 URI src = getXMLBase().resolve(href);
    78 //                URL url = srcUrl.toURL();
    79 //                URL url = new URL(helper.docRoot, href);
    80                 PatternSVG patSrc = (PatternSVG)helper.universe.getElement(src);
    81 
    82                 gradientUnits = patSrc.gradientUnits;
    83                 x = patSrc.x;
    84                 y = patSrc.y;
    85                 width = patSrc.width;
    86                 height = patSrc.height;
    87                 viewBox = patSrc.viewBox;
    88                 patternXform.setTransform(patSrc.patternXform);
    89                 members.addAll(patSrc.members);
    90             }
    91             catch (Exception e)
    92             {
    93                 e.printStackTrace();
    94             }
    95         }
    96 
    97 
    98         String gradientUnits = attrs.getValue("gradientUnits");
    99 
    100         if (gradientUnits != null)
    101         {
    102             if (gradientUnits.toLowerCase().equals("userspaceonuse")) this.gradientUnits = GU_USER_SPACE_ON_USE;
    103             else this.gradientUnits = GU_OBJECT_BOUNDING_BOX;
    104         }
    105 
    106         String patternTransform = attrs.getValue("patternTransform");
    107         if (patternTransform != null)
    108         {
    109             patternXform = parseTransform(patternTransform);
    110         }
    111 
    112         String x = attrs.getValue("x");
    113         String y = attrs.getValue("y");
    114         String width = attrs.getValue("width");
    115         String height = attrs.getValue("height");
    116 
    117         if (x != null) this.x = XMLParseUtil.parseFloat(x);
    118         if (y != null) this.y = XMLParseUtil.parseFloat(y);
    119         if (width != null) this.width = XMLParseUtil.parseFloat(width);
    120         if (height != null) this.height = XMLParseUtil.parseFloat(height);
    121 
    122         String viewBoxStrn = attrs.getValue("viewBox");
    123         if (viewBoxStrn != null)
    124         {
    125             float[] dim = XMLParseUtil.parseFloatList(viewBoxStrn);
    126             viewBox = new Rectangle2D.Float(dim[0], dim[1], dim[2], dim[3]);
    127         }
    128     }
    129   */ 
     72    /**
     73     * Creates a new instance of Gradient
     74     */
     75    public PatternSVG()
     76    {
     77    }
     78
     79    public String getTagName()
     80    {
     81        return TAG_NAME;
     82    }
     83
    13084    /**
    13185     * Called after the start element but before the end element to indicate
     
    13589    {
    13690        super.loaderAddChild(helper, child);
    137 
    138 //        members.add(child);
    139     }
    140    
     91    }
     92
    14193    protected void build() throws SVGException
    14294    {
    14395        super.build();
    144        
     96
    14597        StyleAttribute sty = new StyleAttribute();
    146        
    147                 //Load style string
     98
     99        //Load style string
    148100        String href = null;
    149         if (getPres(sty.setName("xlink:href"))) href = sty.getStringValue();
     101        if (getPres(sty.setName("xlink:href")))
     102        {
     103            href = sty.getStringValue();
     104        }
    150105        //String href = attrs.getValue("xlink:href");
    151106        //If we have a link to another pattern, initialize ourselves with it's values
     
    153108        {
    154109//System.err.println("Gradient.loaderStartElement() href '" + href + "'");
    155             try {
     110            try
     111            {
    156112                URI src = getXMLBase().resolve(href);
    157                 PatternSVG patSrc = (PatternSVG)diagram.getUniverse().getElement(src);
     113                PatternSVG patSrc = (PatternSVG) diagram.getUniverse().getElement(src);
    158114
    159115                gradientUnits = patSrc.gradientUnits;
     
    165121                patternXform.setTransform(patSrc.patternXform);
    166122                children.addAll(patSrc.children);
     123            } catch (Exception e)
     124            {
     125                Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
     126                    "Could not parse xlink:href", e);
    167127            }
    168             catch (Exception e)
    169             {
    170                 e.printStackTrace();
    171             }
    172128        }
    173129
    174130        String gradientUnits = "";
    175         if (getPres(sty.setName("gradientUnits"))) gradientUnits = sty.getStringValue().toLowerCase();
    176         if (gradientUnits.equals("userspaceonuse")) this.gradientUnits = GU_USER_SPACE_ON_USE;
    177         else this.gradientUnits = GU_OBJECT_BOUNDING_BOX;
     131        if (getPres(sty.setName("gradientUnits")))
     132        {
     133            gradientUnits = sty.getStringValue().toLowerCase();
     134        }
     135        if (gradientUnits.equals("userspaceonuse"))
     136        {
     137            this.gradientUnits = GU_USER_SPACE_ON_USE;
     138        } else
     139        {
     140            this.gradientUnits = GU_OBJECT_BOUNDING_BOX;
     141        }
    178142
    179143        String patternTransform = "";
    180         if (getPres(sty.setName("patternTransform"))) patternTransform = sty.getStringValue();
     144        if (getPres(sty.setName("patternTransform")))
     145        {
     146            patternTransform = sty.getStringValue();
     147        }
    181148        patternXform = parseTransform(patternTransform);
    182149
    183        
    184         if (getPres(sty.setName("x"))) x = sty.getFloatValueWithUnits();
    185        
    186         if (getPres(sty.setName("y"))) y = sty.getFloatValueWithUnits();
    187        
    188         if (getPres(sty.setName("width"))) width = sty.getFloatValueWithUnits();
    189        
    190         if (getPres(sty.setName("height"))) height = sty.getFloatValueWithUnits();
    191        
     150
     151        if (getPres(sty.setName("x")))
     152        {
     153            x = sty.getFloatValueWithUnits();
     154        }
     155
     156        if (getPres(sty.setName("y")))
     157        {
     158            y = sty.getFloatValueWithUnits();
     159        }
     160
     161        if (getPres(sty.setName("width")))
     162        {
     163            width = sty.getFloatValueWithUnits();
     164        }
     165
     166        if (getPres(sty.setName("height")))
     167        {
     168            height = sty.getFloatValueWithUnits();
     169        }
     170
    192171        if (getPres(sty.setName("viewBox")))
    193172        {
     
    195174            viewBox = new Rectangle2D.Float(dim[0], dim[1], dim[2], dim[3]);
    196175        }
    197            
     176
    198177        preparePattern();
    199178    }
    200    
    201 /*
    202     public void loaderEndElement(SVGLoaderHelper helper)
    203     {
    204         build();
    205     }
    206     */
    207 
     179
     180    /*
     181     public void loaderEndElement(SVGLoaderHelper helper)
     182     {
     183     build();
     184     }
     185     */
    208186    protected void preparePattern() throws SVGException
    209187    {
    210188        //For now, treat all fills as UserSpaceOnUse.  Otherwise, we'll need
    211189        // a different paint for every object.
    212         int tileWidth = (int)width;
    213         int tileHeight = (int)height;
     190        int tileWidth = (int) width;
     191        int tileHeight = (int) height;
    214192
    215193        float stretchX = 1f, stretchY = 1f;
     
    217195        {
    218196            //Scale our source tile so that we can have nice sampling from it.
    219             float xlateX = (float)patternXform.getTranslateX();
    220             float xlateY = (float)patternXform.getTranslateY();
     197            float xlateX = (float) patternXform.getTranslateX();
     198            float xlateY = (float) patternXform.getTranslateY();
    221199
    222200            Point2D.Float pt = new Point2D.Float(), pt2 = new Point2D.Float();
     
    226204            pt2.x -= xlateX;
    227205            pt2.y -= xlateY;
    228             stretchX = (float)Math.sqrt(pt2.x * pt2.x + pt2.y * pt2.y) * 1.5f / width;
     206            stretchX = (float) Math.sqrt(pt2.x * pt2.x + pt2.y * pt2.y) * 1.5f / width;
    229207
    230208            pt.setLocation(height, 0);
     
    232210            pt2.x -= xlateX;
    233211            pt2.y -= xlateY;
    234             stretchY = (float)Math.sqrt(pt2.x * pt2.x + pt2.y * pt2.y) * 1.5f / height;
     212            stretchY = (float) Math.sqrt(pt2.x * pt2.x + pt2.y * pt2.y) * 1.5f / height;
    235213
    236214            tileWidth *= stretchX;
     
    238216        }
    239217
    240         if (tileWidth == 0 || tileHeight == 0) 
     218        if (tileWidth == 0 || tileHeight == 0)
    241219        {
    242220            //Use defaults if tile has degenerate size
    243221            return;
    244222        }
    245        
     223
    246224        BufferedImage buf = new BufferedImage(tileWidth, tileHeight, BufferedImage.TYPE_INT_ARGB);
    247225        Graphics2D g = buf.createGraphics();
     
    251229        for (Iterator it = children.iterator(); it.hasNext();)
    252230        {
    253             SVGElement ele = (SVGElement)it.next();
     231            SVGElement ele = (SVGElement) it.next();
    254232            if (ele instanceof RenderableElement)
    255233            {
     
    259237                {
    260238                    xform.translate(-x, -y);
    261                 }
    262                 else
     239                } else
    263240                {
    264241                    xform.scale(tileWidth / viewBox.width, tileHeight / viewBox.height);
     
    267244
    268245                g.setTransform(xform);
    269                 ((RenderableElement)ele).render(g);
     246                ((RenderableElement) ele).render(g);
    270247            }
    271248        }
     
    280257        {
    281258            texPaint = new TexturePaint(buf, new Rectangle2D.Float(x, y, width, height));
    282         }
    283         else
     259        } else
    284260        {
    285261            patternXform.scale(1 / stretchX, 1 / stretchY);
     
    294270
    295271    /**
    296      * Updates all attributes in this diagram associated with a time event.
    297      * Ie, all attributes with track information.
     272     * Updates all attributes in this diagram associated with a time event. Ie,
     273     * all attributes with track information.
     274     *
    298275     * @return - true if this node has changed state as a result of the time
    299276     * update
Note: See TracChangeset for help on using the changeset viewer.