Ignore:
Timestamp:
2013-06-11T01:01:28+02:00 (13 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/Tspan.java

    r4256 r6002  
    11/*
    2  * Stop.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, 1:56 AM
    2635 */
    27 
    2836package com.kitfox.svg;
    2937
    3038import com.kitfox.svg.xml.StyleAttribute;
    31 import java.awt.*;
    32 import java.awt.font.*;
    33 import java.awt.geom.*;
    34 import java.util.*;
    35 
    36 import com.kitfox.svg.xml.*;
    37 import org.xml.sax.*;
    38 
    39 //import org.apache.batik.ext.awt.geom.ExtendedGeneralPath;
     39import java.awt.Graphics2D;
     40import java.awt.Shape;
     41import java.awt.font.FontRenderContext;
     42import java.awt.font.GlyphMetrics;
     43import java.awt.font.GlyphVector;
     44import java.awt.geom.AffineTransform;
     45import java.awt.geom.GeneralPath;
     46import java.awt.geom.Rectangle2D;
    4047
    4148/**
     
    4350 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
    4451 */
    45 public class Tspan extends ShapeElement {
    46 
     52public class Tspan extends ShapeElement
     53{
     54
     55    public static final String TAG_NAME = "tspan";
    4756    float[] x = null;
    4857    float[] y = null;
     
    5059    float[] dy = null;
    5160    float[] rotate = null;
    52 
    5361    private String text = "";
    54 
    5562    float cursorX;
    5663    float cursorY;
    5764
    5865//    Shape tspanShape;
    59 
    60     /** Creates a new instance of Stop */
    61     public Tspan() {
    62     }
    63 
    64     public float getCursorX() { return cursorX; }
    65     public float getCursorY() { return cursorY; }
     66    /**
     67     * Creates a new instance of Stop
     68     */
     69    public Tspan()
     70    {
     71    }
     72
     73    public String getTagName()
     74    {
     75        return TAG_NAME;
     76    }
     77
     78    public float getCursorX()
     79    {
     80        return cursorX;
     81    }
     82
     83    public float getCursorY()
     84    {
     85        return cursorY;
     86    }
    6687
    6788    public void setCursorX(float cursorX)
     
    7495        this.cursorY = cursorY;
    7596    }
    76 /*
    77     public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
    78     {
    79                 //Load style string
    80         super.loaderStartElement(helper, attrs, parent);
    81 
    82         String x = attrs.getValue("x");
    83         String y = attrs.getValue("y");
    84         String dx = attrs.getValue("dx");
    85         String dy = attrs.getValue("dy");
    86         String rotate = attrs.getValue("rotate");
    87 
    88         if (x != null) this.x = XMLParseUtil.parseFloatList(x);
    89         if (y != null) this.y = XMLParseUtil.parseFloatList(y);
    90         if (dx != null) this.dx = XMLParseUtil.parseFloatList(dx);
    91         if (dy != null) this.dy = XMLParseUtil.parseFloatList(dy);
    92         if (rotate != null)
    93         {
    94             this.rotate = XMLParseUtil.parseFloatList(rotate);
    95             for (int i = 0; i < this.rotate.length; i++)
    96                 this.rotate[i] = (float)Math.toRadians(this.rotate[i]);
    97         }
    98     }
    99     */
     97    /*
     98     public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
     99     {
     100     //Load style string
     101     super.loaderStartElement(helper, attrs, parent);
     102
     103     String x = attrs.getValue("x");
     104     String y = attrs.getValue("y");
     105     String dx = attrs.getValue("dx");
     106     String dy = attrs.getValue("dy");
     107     String rotate = attrs.getValue("rotate");
     108
     109     if (x != null) this.x = XMLParseUtil.parseFloatList(x);
     110     if (y != null) this.y = XMLParseUtil.parseFloatList(y);
     111     if (dx != null) this.dx = XMLParseUtil.parseFloatList(dx);
     112     if (dy != null) this.dy = XMLParseUtil.parseFloatList(dy);
     113     if (rotate != null)
     114     {
     115     this.rotate = XMLParseUtil.parseFloatList(rotate);
     116     for (int i = 0; i < this.rotate.length; i++)
     117     this.rotate[i] = (float)Math.toRadians(this.rotate[i]);
     118     }
     119     }
     120     */
    100121
    101122    /**
     
    107128    }
    108129
    109    
    110130    protected void build() throws SVGException
    111131    {
    112132        super.build();
    113        
     133
    114134        StyleAttribute sty = new StyleAttribute();
    115        
    116         if (getPres(sty.setName("x"))) x = sty.getFloatList();
    117 
    118         if (getPres(sty.setName("y"))) y = sty.getFloatList();
    119 
    120         if (getPres(sty.setName("dx"))) dx = sty.getFloatList();
    121 
    122         if (getPres(sty.setName("dy"))) dy = sty.getFloatList();
     135
     136        if (getPres(sty.setName("x")))
     137        {
     138            x = sty.getFloatList();
     139        }
     140
     141        if (getPres(sty.setName("y")))
     142        {
     143            y = sty.getFloatList();
     144        }
     145
     146        if (getPres(sty.setName("dx")))
     147        {
     148            dx = sty.getFloatList();
     149        }
     150
     151        if (getPres(sty.setName("dy")))
     152        {
     153            dy = sty.getFloatList();
     154        }
    123155
    124156        if (getPres(sty.setName("rotate")))
     
    127159            for (int i = 0; i < this.rotate.length; i++)
    128160            {
    129                 rotate[i] = (float)Math.toRadians(this.rotate[i]);
    130             }
    131                
    132         }
    133     }
    134    
     161                rotate[i] = (float) Math.toRadians(this.rotate[i]);
     162            }
     163
     164        }
     165    }
     166
    135167    public void addShape(GeneralPath addShape) throws SVGException
    136168    {
     
    138170        {
    139171            cursorX = x[0];
     172        } else if (dx != null)
     173        {
     174            cursorX += dx[0];
     175        }
     176
     177        if (y != null)
     178        {
    140179            cursorY = y[0];
    141         }
    142         else if (dx != null)
    143         {
    144             cursorX += dx[0];
     180        } else if (dy != null)
     181        {
    145182            cursorY += dy[0];
    146183        }
    147184
    148185        StyleAttribute sty = new StyleAttribute();
    149        
     186
    150187        String fontFamily = null;
    151188        if (getStyle(sty.setName("font-family")))
     
    160197            fontSize = sty.getFloatValueWithUnits();
    161198        }
     199
     200        float letterSpacing = 0;
     201        if (getStyle(sty.setName("letter-spacing")))
     202        {
     203            letterSpacing = sty.getFloatValueWithUnits();
     204        }
     205
    162206
    163207        //Get font
     
    165209        if (font == null)
    166210        {
    167             addShapeSysFont(addShape, font, fontFamily, fontSize);
     211            addShapeSysFont(addShape, font, fontFamily, fontSize, letterSpacing);
    168212            return;
    169213        }
     
    171215        FontFace fontFace = font.getFontFace();
    172216        int ascent = fontFace.getAscent();
    173         float fontScale = fontSize / (float)ascent;
     217        float fontScale = fontSize / (float) ascent;
    174218
    175219        AffineTransform xform = new AffineTransform();
     
    184228            xform.setToTranslation(cursorX, cursorY);
    185229            xform.scale(fontScale, fontScale);
    186             if (rotate != null) xform.rotate(rotate[posPtr]);
     230            if (rotate != null)
     231            {
     232                xform.rotate(rotate[posPtr]);
     233            }
    187234
    188235            String unicode = text.substring(i, i + 1);
     
    200247                cursorX = x[posPtr];
    201248                cursorY = y[posPtr++];
    202             }
    203             else if (dx != null && posPtr < dx.length)
     249            } else if (dx != null && posPtr < dx.length)
    204250            {
    205251                cursorX += dx[posPtr];
     
    207253            }
    208254
    209             cursorX += fontScale * glyph.getHorizAdvX();
     255            cursorX += fontScale * glyph.getHorizAdvX() + letterSpacing;
    210256        }
    211257
     
    213259    }
    214260
    215     private void addShapeSysFont(GeneralPath addShape, Font font, String fontFamily, float fontSize)
    216     {
    217         java.awt.Font sysFont = new java.awt.Font(fontFamily, java.awt.Font.PLAIN, (int)fontSize);
     261    private void addShapeSysFont(GeneralPath addShape, Font font,
     262        String fontFamily, float fontSize, float letterSpacing)
     263    {
     264        java.awt.Font sysFont = new java.awt.Font(fontFamily, java.awt.Font.PLAIN, (int) fontSize);
    218265
    219266        FontRenderContext frc = new FontRenderContext(null, true, true);
     
    226273        {
    227274            xform.setToIdentity();
    228             xform.setToTranslation(cursorX, cursorY);
    229             if (rotate != null) xform.rotate(rotate[Math.min(i, rotate.length - 1)]);
     275            xform.setToTranslation(cursorX + i * letterSpacing, cursorY);
     276            if (rotate != null)
     277            {
     278                xform.rotate(rotate[Math.min(i, rotate.length - 1)]);
     279            }
    230280
    231281            String unicode = text.substring(i, i + 1);
     
    240290                cursorX = x[posPtr];
    241291                cursorY = y[posPtr++];
    242             }
    243             else if (dx != null && posPtr < dx.length)
     292            } else if (dx != null && posPtr < dx.length)
    244293            {
    245294                cursorX += dx[posPtr];
     
    255304            cursorX = x[0];
    256305            cursorY = y[0];
    257         }
    258         else if (dx != null)
     306        } else if (dx != null)
    259307        {
    260308            cursorX += dx[0];
     
    263311
    264312        StyleAttribute sty = new StyleAttribute();
    265        
     313
    266314        String fontFamily = null;
    267315        if (getPres(sty.setName("font-family")))
     
    282330        {
    283331            System.err.println("Could not load font");
    284             java.awt.Font sysFont = new java.awt.Font(fontFamily, java.awt.Font.PLAIN, (int)fontSize);
     332            java.awt.Font sysFont = new java.awt.Font(fontFamily, java.awt.Font.PLAIN, (int) fontSize);
    285333            renderSysFont(g, sysFont);
    286334            return;
     
    290338        FontFace fontFace = font.getFontFace();
    291339        int ascent = fontFace.getAscent();
    292         float fontScale = fontSize / (float)ascent;
     340        float fontScale = fontSize / (float) ascent;
    293341
    294342        AffineTransform oldXform = g.getTransform();
     
    312360            {
    313361                renderShape(g, path);
    314             }
    315             else glyph.render(g);
     362            } else
     363            {
     364                glyph.render(g);
     365            }
    316366
    317367            if (x != null && posPtr < x.length)
     
    319369                cursorX = x[posPtr];
    320370                cursorY = y[posPtr++];
    321             }
    322             else if (dx != null && posPtr < dx.length)
     371            } else if (dx != null && posPtr < dx.length)
    323372            {
    324373                cursorX += dx[posPtr];
     
    342391        renderShape(g, textShape);
    343392        Rectangle2D rect = font.getStringBounds(text, frc);
    344         cursorX += (float)rect.getWidth();
     393        cursorX += (float) rect.getWidth();
    345394    }
    346395
     
    358407
    359408    /**
    360      * Updates all attributes in this diagram associated with a time event.
    361      * Ie, all attributes with track information.
     409     * Updates all attributes in this diagram associated with a time event. Ie,
     410     * all attributes with track information.
     411     *
    362412     * @return - true if this node has changed state as a result of the time
    363413     * update
Note: See TracChangeset for help on using the changeset viewer.