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/Text.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 import java.util.regex.*;
     39import java.awt.Graphics2D;
     40import java.awt.Shape;
     41import java.awt.font.FontRenderContext;
     42import java.awt.geom.AffineTransform;
     43import java.awt.geom.GeneralPath;
     44import java.awt.geom.Rectangle2D;
     45import java.util.Iterator;
     46import java.util.LinkedList;
     47import java.util.regex.Matcher;
     48import java.util.regex.Pattern;
    3649
    3750//import org.apache.batik.ext.awt.geom.ExtendedGeneralPath;
    38 
    3951/**
    4052 * @author Mark McKay
     
    4355public class Text extends ShapeElement
    4456{
     57    public static final String TAG_NAME = "text";
    4558   
    4659    float x = 0;
    4760    float y = 0;
    4861    AffineTransform transform = null;
    49    
    5062    String fontFamily;
    5163    float fontSize;
    52    
    5364    //List of strings and tspans containing the content of this node
    5465    LinkedList content = new LinkedList();
    55    
    5666    Shape textShape;
    57    
    5867    public static final int TXAN_START = 0;
    5968    public static final int TXAN_MIDDLE = 1;
    6069    public static final int TXAN_END = 2;
    6170    int textAnchor = TXAN_START;
    62    
    6371    public static final int TXST_NORMAL = 0;
    6472    public static final int TXST_ITALIC = 1;
    6573    public static final int TXST_OBLIQUE = 2;
    6674    int fontStyle;
    67    
    6875    public static final int TXWE_NORMAL = 0;
    6976    public static final int TXWE_BOLD = 1;
     
    8087    public static final int TXWE_900 = 12;
    8188    int fontWeight;
    82    
    83     /** Creates a new instance of Stop */
     89
     90    /**
     91     * Creates a new instance of Stop
     92     */
    8493    public Text()
    8594    {
    8695    }
    87    
     96
     97    public String getTagName()
     98    {
     99        return TAG_NAME;
     100    }
     101
    88102    public void appendText(String text)
    89103    {
    90104        content.addLast(text);
    91105    }
    92    
     106
    93107    public void appendTspan(Tspan tspan) throws SVGElementException
    94108    {
    95109        super.loaderAddChild(null, tspan);
    96110        content.addLast(tspan);
    97 //        tspan.setParent(this);
    98     }
    99    
     111    }
     112
    100113    /**
    101114     * Discard cached information
     
    105118        build();
    106119    }
    107    
     120
    108121    public java.util.List getContent()
    109122    {
    110123        return content;
    111124    }
    112 /*
    113     public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
    114     {
    115                 //Load style string
    116         super.loaderStartElement(helper, attrs, parent);
    117  
    118         String x = attrs.getValue("x");
    119         String y = attrs.getValue("y");
    120         //String transform = attrs.getValue("transform");
    121  
    122         this.x = XMLParseUtil.parseFloat(x);
    123         this.y = XMLParseUtil.parseFloat(y);
    124     }
    125  */
     125
    126126    /**
    127127     * Called after the start element but before the end element to indicate
     
    131131    {
    132132        super.loaderAddChild(helper, child);
    133        
     133
    134134        content.addLast(child);
    135135    }
    136    
     136
    137137    /**
    138138     * Called during load process to add text scanned within a tag
     
    141141    {
    142142        Matcher matchWs = Pattern.compile("\\s*").matcher(text);
    143         if (!matchWs.matches()) content.addLast(text);
    144     }
    145    
     143        if (!matchWs.matches())
     144        {
     145            content.addLast(text);
     146        }
     147    }
     148
    146149    public void build() throws SVGException
    147150    {
    148151        super.build();
    149        
     152
    150153        StyleAttribute sty = new StyleAttribute();
    151        
    152         if (getPres(sty.setName("x"))) x = sty.getFloatValueWithUnits();
    153        
    154         if (getPres(sty.setName("y"))) y = sty.getFloatValueWithUnits();
    155        
    156         if (getStyle(sty.setName("font-family"))) fontFamily = sty.getStringValue();
    157         else fontFamily = "Sans Serif";
    158        
    159         if (getStyle(sty.setName("font-size"))) fontSize = sty.getFloatValueWithUnits();
    160         else fontSize = 12f;
    161        
     154
     155        if (getPres(sty.setName("x")))
     156        {
     157            x = sty.getFloatValueWithUnits();
     158        }
     159
     160        if (getPres(sty.setName("y")))
     161        {
     162            y = sty.getFloatValueWithUnits();
     163        }
     164
     165        if (getStyle(sty.setName("font-family")))
     166        {
     167            fontFamily = sty.getStringValue();
     168        } else
     169        {
     170            fontFamily = "Sans Serif";
     171        }
     172
     173        if (getStyle(sty.setName("font-size")))
     174        {
     175            fontSize = sty.getFloatValueWithUnits();
     176        } else
     177        {
     178            fontSize = 12f;
     179        }
     180
    162181        if (getStyle(sty.setName("font-style")))
    163182        {
     
    166185            {
    167186                fontStyle = TXST_NORMAL;
    168             }
    169             else if ("italic".equals(s))
     187            } else if ("italic".equals(s))
    170188            {
    171189                fontStyle = TXST_ITALIC;
    172             }
    173             else if ("oblique".equals(s))
     190            } else if ("oblique".equals(s))
    174191            {
    175192                fontStyle = TXST_OBLIQUE;
    176193            }
    177         }
    178         else fontStyle = TXST_NORMAL;
    179        
     194        } else
     195        {
     196            fontStyle = TXST_NORMAL;
     197        }
     198
    180199        if (getStyle(sty.setName("font-weight")))
    181200        {
     
    184203            {
    185204                fontWeight = TXWE_NORMAL;
    186             }
    187             else if ("bold".equals(s))
     205            } else if ("bold".equals(s))
    188206            {
    189207                fontWeight = TXWE_BOLD;
    190208            }
    191         }
    192         else fontWeight = TXWE_BOLD;
    193        
     209        } else
     210        {
     211            fontWeight = TXWE_BOLD;
     212        }
     213
    194214        if (getStyle(sty.setName("text-anchor")))
    195215        {
    196216            String s = sty.getStringValue();
    197             if (s.equals("middle")) textAnchor = TXAN_MIDDLE;
    198             else if (s.equals("end")) textAnchor = TXAN_END;
    199             else textAnchor = TXAN_START;
    200         }
    201         else textAnchor = TXAN_START;
    202        
     217            if (s.equals("middle"))
     218            {
     219                textAnchor = TXAN_MIDDLE;
     220            } else if (s.equals("end"))
     221            {
     222                textAnchor = TXAN_END;
     223            } else
     224            {
     225                textAnchor = TXAN_START;
     226            }
     227        } else
     228        {
     229            textAnchor = TXAN_START;
     230        }
     231
    203232        //text anchor
    204233        //text-decoration
    205234        //text-rendering
    206        
     235
    207236        buildFont();
    208237    }
    209    
     238
    210239    protected void buildFont() throws SVGException
    211240    {
     
    232261                break;
    233262        }
    234            
     263
    235264        //Get font
    236265        Font font = diagram.getUniverse().getFont(fontFamily);
     
    238267        {
    239268//            System.err.println("Could not load font");
    240            
    241             java.awt.Font sysFont = new java.awt.Font(fontFamily, style | weight, (int)fontSize);
     269
     270            java.awt.Font sysFont = new java.awt.Font(fontFamily, style | weight, (int) fontSize);
    242271            buildSysFont(sysFont);
    243272            return;
    244273        }
    245        
     274
    246275//        font = new java.awt.Font(font.getFamily(), style | weight, font.getSize());
    247        
     276
    248277//        Area textArea = new Area();
    249278        GeneralPath textPath = new GeneralPath();
    250279        textShape = textPath;
    251        
     280
    252281        float cursorX = x, cursorY = y;
    253        
     282
    254283        FontFace fontFace = font.getFontFace();
    255284        //int unitsPerEm = fontFace.getUnitsPerEm();
    256285        int ascent = fontFace.getAscent();
    257         float fontScale = fontSize / (float)ascent;
    258        
     286        float fontScale = fontSize / (float) ascent;
     287
    259288//        AffineTransform oldXform = g.getTransform();
    260289        AffineTransform xform = new AffineTransform();
    261        
     290
    262291        for (Iterator it = content.iterator(); it.hasNext();)
    263292        {
    264293            Object obj = it.next();
    265            
     294
    266295            if (obj instanceof String)
    267296            {
    268                 String text = (String)obj;
    269                
     297                String text = (String) obj;
     298                if (text != null)
     299                {
     300                    text = text.trim();
     301                }
     302
    270303                strokeWidthScalar = 1f / fontScale;
    271                
     304
    272305                for (int i = 0; i < text.length(); i++)
    273306                {
     
    276309                    xform.scale(fontScale, fontScale);
    277310//                    g.transform(xform);
    278                    
     311
    279312                    String unicode = text.substring(i, i + 1);
    280313                    MissingGlyph glyph = font.getGlyph(unicode);
    281                    
     314
    282315                    Shape path = glyph.getPath();
    283316                    if (path != null)
     
    287320                    }
    288321//                    else glyph.render(g);
    289                    
     322
    290323                    cursorX += fontScale * glyph.getHorizAdvX();
    291                    
     324
    292325//                    g.setTransform(oldXform);
    293326                }
    294                
     327
    295328                strokeWidthScalar = 1f;
    296             }
    297             else if (obj instanceof Tspan)
    298             {
    299                 Tspan tspan = (Tspan)obj;
    300                
     329            } else if (obj instanceof Tspan)
     330            {
     331                Tspan tspan = (Tspan) obj;
     332
    301333                xform.setToIdentity();
    302334                xform.setToTranslation(cursorX, cursorY);
     
    304336//                tspan.setCursorX(cursorX);
    305337//                tspan.setCursorY(cursorY);
    306                
     338
    307339                Shape tspanShape = tspan.getShape();
    308340                tspanShape = xform.createTransformedShape(tspanShape);
     
    312344//                cursorY = tspan.getCursorY();
    313345            }
    314            
    315         }
    316        
     346
     347        }
     348
    317349        switch (textAnchor)
    318350        {
     
    333365        }
    334366    }
    335    
     367
    336368    private void buildSysFont(java.awt.Font font) throws SVGException
    337369    {
    338370        GeneralPath textPath = new GeneralPath();
    339371        textShape = textPath;
    340        
     372
    341373        float cursorX = x, cursorY = y;
    342        
     374
    343375//        FontMetrics fm = g.getFontMetrics(font);
    344376        FontRenderContext frc = new FontRenderContext(null, true, true);
    345        
     377
    346378//        FontFace fontFace = font.getFontFace();
    347379        //int unitsPerEm = fontFace.getUnitsPerEm();
    348380//        int ascent = fm.getAscent();
    349381//        float fontScale = fontSize / (float)ascent;
    350        
     382
    351383//        AffineTransform oldXform = g.getTransform();
    352384        AffineTransform xform = new AffineTransform();
    353        
     385
    354386        for (Iterator it = content.iterator(); it.hasNext();)
    355387        {
    356388            Object obj = it.next();
    357            
     389
    358390            if (obj instanceof String)
    359391            {
    360                 String text = (String)obj;
    361                
     392                String text = (String) obj;
     393
    362394                Shape textShape = font.createGlyphVector(frc, text).getOutline(cursorX, cursorY);
    363395                textPath.append(textShape, false);
    364396//                renderShape(g, textShape);
    365397//                g.drawString(text, cursorX, cursorY);
    366                
     398
    367399                Rectangle2D rect = font.getStringBounds(text, frc);
    368                 cursorX += (float)rect.getWidth();
    369             }
    370             else if (obj instanceof Tspan)
     400                cursorX += (float) rect.getWidth();
     401            } else if (obj instanceof Tspan)
    371402            {
    372403                /*
    373                 Tspan tspan = (Tspan)obj;
     404                 Tspan tspan = (Tspan)obj;
    374405                 
    375                 xform.setToIdentity();
    376                 xform.setToTranslation(cursorX, cursorY);
     406                 xform.setToIdentity();
     407                 xform.setToTranslation(cursorX, cursorY);
    377408                 
    378                 Shape tspanShape = tspan.getShape();
    379                 tspanShape = xform.createTransformedShape(tspanShape);
    380                 textArea.add(new Area(tspanShape));
     409                 Shape tspanShape = tspan.getShape();
     410                 tspanShape = xform.createTransformedShape(tspanShape);
     411                 textArea.add(new Area(tspanShape));
    381412                 
    382                 cursorX += tspanShape.getBounds2D().getWidth();
     413                 cursorX += tspanShape.getBounds2D().getWidth();
    383414                 */
    384                
    385                
    386                 Tspan tspan = (Tspan)obj;
     415
     416
     417                Tspan tspan = (Tspan) obj;
    387418                tspan.setCursorX(cursorX);
    388419                tspan.setCursorY(cursorY);
     
    390421                cursorX = tspan.getCursorX();
    391422                cursorY = tspan.getCursorY();
    392                
    393             }
    394         }
    395        
     423
     424            }
     425        }
     426
    396427        switch (textAnchor)
    397428        {
     
    412443        }
    413444    }
    414    
    415    
     445
    416446    public void render(Graphics2D g) throws SVGException
    417447    {
     
    420450        finishLayer(g);
    421451    }
    422    
     452
    423453    public Shape getShape()
    424454    {
    425455        return shapeToParent(textShape);
    426456    }
    427    
     457
    428458    public Rectangle2D getBoundingBox() throws SVGException
    429459    {
    430460        return boundsToParent(includeStrokeInBounds(textShape.getBounds2D()));
    431461    }
    432    
     462
    433463    /**
    434      * Updates all attributes in this diagram associated with a time event.
    435      * Ie, all attributes with track information.
     464     * Updates all attributes in this diagram associated with a time event. Ie,
     465     * all attributes with track information.
     466     *
    436467     * @return - true if this node has changed state as a result of the time
    437468     * update
     
    441472//        if (trackManager.getNumTracks() == 0) return false;
    442473        boolean changeState = super.updateTime(curTime);
    443        
     474
    444475        //Get current values for parameters
    445476        StyleAttribute sty = new StyleAttribute();
    446477        boolean shapeChange = false;
    447        
     478
    448479        if (getPres(sty.setName("x")))
    449480        {
     
    455486            }
    456487        }
    457        
     488
    458489        if (getPres(sty.setName("y")))
    459490        {
     
    465496            }
    466497        }
    467        
     498
    468499        if (getPres(sty.setName("font-family")))
    469500        {
     
    475506            }
    476507        }
    477        
     508
    478509        if (getPres(sty.setName("font-size")))
    479510        {
     
    485516            }
    486517        }
    487        
    488        
     518
     519
    489520        if (getStyle(sty.setName("font-style")))
    490521        {
     
    494525            {
    495526                newVal = TXST_NORMAL;
    496             }
    497             else if ("italic".equals(s))
     527            } else if ("italic".equals(s))
    498528            {
    499529                newVal = TXST_ITALIC;
    500             }
    501             else if ("oblique".equals(s))
     530            } else if ("oblique".equals(s))
    502531            {
    503532                newVal = TXST_OBLIQUE;
     
    509538            }
    510539        }
    511        
     540
    512541        if (getStyle(sty.setName("font-weight")))
    513542        {
     
    517546            {
    518547                newVal = TXWE_NORMAL;
    519             }
    520             else if ("bold".equals(s))
     548            } else if ("bold".equals(s))
    521549            {
    522550                newVal = TXWE_BOLD;
     
    528556            }
    529557        }
    530        
     558
    531559        if (shapeChange)
    532560        {
     
    535563//            return true;
    536564        }
    537        
     565
    538566        return changeState || shapeChange;
    539567    }
Note: See TracChangeset for help on using the changeset viewer.