Index: trunk/src/com/kitfox/svg/Text.java
===================================================================
--- trunk/src/com/kitfox/svg/Text.java	(revision 4256)
+++ trunk/src/com/kitfox/svg/Text.java	(revision 6002)
@@ -1,40 +1,52 @@
 /*
- * Stop.java
+ * SVG Salamander
+ * Copyright (c) 2004, Mark McKay
+ * All rights reserved.
  *
+ * Redistribution and use in source and binary forms, with or 
+ * without modification, are permitted provided that the following
+ * conditions are met:
  *
- *  The Salamander Project - 2D and 3D graphics libraries in Java
- *  Copyright (C) 2004 Mark McKay
+ *   - Redistributions of source code must retain the above 
+ *     copyright notice, this list of conditions and the following
+ *     disclaimer.
+ *   - Redistributions in binary form must reproduce the above
+ *     copyright notice, this list of conditions and the following
+ *     disclaimer in the documentation and/or other materials 
+ *     provided with the distribution.
  *
- *  This library is free software; you can redistribute it and/or
- *  modify it under the terms of the GNU Lesser General Public
- *  License as published by the Free Software Foundation; either
- *  version 2.1 of the License, or (at your option) any later version.
- *
- *  This library is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- *  Lesser General Public License for more details.
- *
- *  You should have received a copy of the GNU Lesser General Public
- *  License along with this library; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- *  Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
- *  projects can be found at http://www.kitfox.com
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE. 
+ * 
+ * Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
+ * projects can be found at http://www.kitfox.com
  *
  * Created on January 26, 2004, 1:56 AM
  */
-
 package com.kitfox.svg;
 
 import com.kitfox.svg.xml.StyleAttribute;
-import java.awt.*;
-import java.awt.font.*;
-import java.awt.geom.*;
-import java.util.*;
-import java.util.regex.*;
+import java.awt.Graphics2D;
+import java.awt.Shape;
+import java.awt.font.FontRenderContext;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.GeneralPath;
+import java.awt.geom.Rectangle2D;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 //import org.apache.batik.ext.awt.geom.ExtendedGeneralPath;
-
 /**
  * @author Mark McKay
@@ -43,27 +55,22 @@
 public class Text extends ShapeElement
 {
+    public static final String TAG_NAME = "text";
     
     float x = 0;
     float y = 0;
     AffineTransform transform = null;
-    
     String fontFamily;
     float fontSize;
-    
     //List of strings and tspans containing the content of this node
     LinkedList content = new LinkedList();
-    
     Shape textShape;
-    
     public static final int TXAN_START = 0;
     public static final int TXAN_MIDDLE = 1;
     public static final int TXAN_END = 2;
     int textAnchor = TXAN_START;
-    
     public static final int TXST_NORMAL = 0;
     public static final int TXST_ITALIC = 1;
     public static final int TXST_OBLIQUE = 2;
     int fontStyle;
-    
     public static final int TXWE_NORMAL = 0;
     public static final int TXWE_BOLD = 1;
@@ -80,22 +87,28 @@
     public static final int TXWE_900 = 12;
     int fontWeight;
-    
-    /** Creates a new instance of Stop */
+
+    /**
+     * Creates a new instance of Stop
+     */
     public Text()
     {
     }
-    
+
+    public String getTagName()
+    {
+        return TAG_NAME;
+    }
+
     public void appendText(String text)
     {
         content.addLast(text);
     }
-    
+
     public void appendTspan(Tspan tspan) throws SVGElementException
     {
         super.loaderAddChild(null, tspan);
         content.addLast(tspan);
-//        tspan.setParent(this);
-    }
-    
+    }
+
     /**
      * Discard cached information
@@ -105,23 +118,10 @@
         build();
     }
-    
+
     public java.util.List getContent()
     {
         return content;
     }
-/*
-    public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
-    {
-                //Load style string
-        super.loaderStartElement(helper, attrs, parent);
- 
-        String x = attrs.getValue("x");
-        String y = attrs.getValue("y");
-        //String transform = attrs.getValue("transform");
- 
-        this.x = XMLParseUtil.parseFloat(x);
-        this.y = XMLParseUtil.parseFloat(y);
-    }
- */
+
     /**
      * Called after the start element but before the end element to indicate
@@ -131,8 +131,8 @@
     {
         super.loaderAddChild(helper, child);
-        
+
         content.addLast(child);
     }
-    
+
     /**
      * Called during load process to add text scanned within a tag
@@ -141,23 +141,42 @@
     {
         Matcher matchWs = Pattern.compile("\\s*").matcher(text);
-        if (!matchWs.matches()) content.addLast(text);
-    }
-    
+        if (!matchWs.matches())
+        {
+            content.addLast(text);
+        }
+    }
+
     public void build() throws SVGException
     {
         super.build();
-        
+
         StyleAttribute sty = new StyleAttribute();
-        
-        if (getPres(sty.setName("x"))) x = sty.getFloatValueWithUnits();
-        
-        if (getPres(sty.setName("y"))) y = sty.getFloatValueWithUnits();
-        
-        if (getStyle(sty.setName("font-family"))) fontFamily = sty.getStringValue();
-        else fontFamily = "Sans Serif";
-        
-        if (getStyle(sty.setName("font-size"))) fontSize = sty.getFloatValueWithUnits();
-        else fontSize = 12f;
-        
+
+        if (getPres(sty.setName("x")))
+        {
+            x = sty.getFloatValueWithUnits();
+        }
+
+        if (getPres(sty.setName("y")))
+        {
+            y = sty.getFloatValueWithUnits();
+        }
+
+        if (getStyle(sty.setName("font-family")))
+        {
+            fontFamily = sty.getStringValue();
+        } else
+        {
+            fontFamily = "Sans Serif";
+        }
+
+        if (getStyle(sty.setName("font-size")))
+        {
+            fontSize = sty.getFloatValueWithUnits();
+        } else
+        {
+            fontSize = 12f;
+        }
+
         if (getStyle(sty.setName("font-style")))
         {
@@ -166,16 +185,16 @@
             {
                 fontStyle = TXST_NORMAL;
-            }
-            else if ("italic".equals(s))
+            } else if ("italic".equals(s))
             {
                 fontStyle = TXST_ITALIC;
-            }
-            else if ("oblique".equals(s))
+            } else if ("oblique".equals(s))
             {
                 fontStyle = TXST_OBLIQUE;
             }
-        }
-        else fontStyle = TXST_NORMAL;
-        
+        } else
+        {
+            fontStyle = TXST_NORMAL;
+        }
+
         if (getStyle(sty.setName("font-weight")))
         {
@@ -184,28 +203,38 @@
             {
                 fontWeight = TXWE_NORMAL;
-            }
-            else if ("bold".equals(s))
+            } else if ("bold".equals(s))
             {
                 fontWeight = TXWE_BOLD;
             }
-        }
-        else fontWeight = TXWE_BOLD;
-        
+        } else
+        {
+            fontWeight = TXWE_BOLD;
+        }
+
         if (getStyle(sty.setName("text-anchor")))
         {
             String s = sty.getStringValue();
-            if (s.equals("middle")) textAnchor = TXAN_MIDDLE;
-            else if (s.equals("end")) textAnchor = TXAN_END;
-            else textAnchor = TXAN_START;
-        }
-        else textAnchor = TXAN_START;
-        
+            if (s.equals("middle"))
+            {
+                textAnchor = TXAN_MIDDLE;
+            } else if (s.equals("end"))
+            {
+                textAnchor = TXAN_END;
+            } else
+            {
+                textAnchor = TXAN_START;
+            }
+        } else
+        {
+            textAnchor = TXAN_START;
+        }
+
         //text anchor
         //text-decoration
         //text-rendering
-        
+
         buildFont();
     }
-    
+
     protected void buildFont() throws SVGException
     {
@@ -232,5 +261,5 @@
                 break;
         }
-            
+
         //Get font
         Font font = diagram.getUniverse().getFont(fontFamily);
@@ -238,36 +267,40 @@
         {
 //            System.err.println("Could not load font");
-            
-            java.awt.Font sysFont = new java.awt.Font(fontFamily, style | weight, (int)fontSize);
+
+            java.awt.Font sysFont = new java.awt.Font(fontFamily, style | weight, (int) fontSize);
             buildSysFont(sysFont);
             return;
         }
-        
+
 //        font = new java.awt.Font(font.getFamily(), style | weight, font.getSize());
-        
+
 //        Area textArea = new Area();
         GeneralPath textPath = new GeneralPath();
         textShape = textPath;
-        
+
         float cursorX = x, cursorY = y;
-        
+
         FontFace fontFace = font.getFontFace();
         //int unitsPerEm = fontFace.getUnitsPerEm();
         int ascent = fontFace.getAscent();
-        float fontScale = fontSize / (float)ascent;
-        
+        float fontScale = fontSize / (float) ascent;
+
 //        AffineTransform oldXform = g.getTransform();
         AffineTransform xform = new AffineTransform();
-        
+
         for (Iterator it = content.iterator(); it.hasNext();)
         {
             Object obj = it.next();
-            
+
             if (obj instanceof String)
             {
-                String text = (String)obj;
-                
+                String text = (String) obj;
+                if (text != null)
+                {
+                    text = text.trim();
+                }
+
                 strokeWidthScalar = 1f / fontScale;
-                
+
                 for (int i = 0; i < text.length(); i++)
                 {
@@ -276,8 +309,8 @@
                     xform.scale(fontScale, fontScale);
 //                    g.transform(xform);
-                    
+
                     String unicode = text.substring(i, i + 1);
                     MissingGlyph glyph = font.getGlyph(unicode);
-                    
+
                     Shape path = glyph.getPath();
                     if (path != null)
@@ -287,16 +320,15 @@
                     }
 //                    else glyph.render(g);
-                    
+
                     cursorX += fontScale * glyph.getHorizAdvX();
-                    
+
 //                    g.setTransform(oldXform);
                 }
-                
+
                 strokeWidthScalar = 1f;
-            }
-            else if (obj instanceof Tspan)
-            {
-                Tspan tspan = (Tspan)obj;
-                
+            } else if (obj instanceof Tspan)
+            {
+                Tspan tspan = (Tspan) obj;
+
                 xform.setToIdentity();
                 xform.setToTranslation(cursorX, cursorY);
@@ -304,5 +336,5 @@
 //                tspan.setCursorX(cursorX);
 //                tspan.setCursorY(cursorY);
-                
+
                 Shape tspanShape = tspan.getShape();
                 tspanShape = xform.createTransformedShape(tspanShape);
@@ -312,7 +344,7 @@
 //                cursorY = tspan.getCursorY();
             }
-            
-        }
-        
+
+        }
+
         switch (textAnchor)
         {
@@ -333,56 +365,55 @@
         }
     }
-    
+
     private void buildSysFont(java.awt.Font font) throws SVGException
     {
         GeneralPath textPath = new GeneralPath();
         textShape = textPath;
-        
+
         float cursorX = x, cursorY = y;
-        
+
 //        FontMetrics fm = g.getFontMetrics(font);
         FontRenderContext frc = new FontRenderContext(null, true, true);
-        
+
 //        FontFace fontFace = font.getFontFace();
         //int unitsPerEm = fontFace.getUnitsPerEm();
 //        int ascent = fm.getAscent();
 //        float fontScale = fontSize / (float)ascent;
-        
+
 //        AffineTransform oldXform = g.getTransform();
         AffineTransform xform = new AffineTransform();
-        
+
         for (Iterator it = content.iterator(); it.hasNext();)
         {
             Object obj = it.next();
-            
+
             if (obj instanceof String)
             {
-                String text = (String)obj;
-                
+                String text = (String) obj;
+
                 Shape textShape = font.createGlyphVector(frc, text).getOutline(cursorX, cursorY);
                 textPath.append(textShape, false);
 //                renderShape(g, textShape);
 //                g.drawString(text, cursorX, cursorY);
-                
+
                 Rectangle2D rect = font.getStringBounds(text, frc);
-                cursorX += (float)rect.getWidth();
-            }
-            else if (obj instanceof Tspan)
+                cursorX += (float) rect.getWidth();
+            } else if (obj instanceof Tspan)
             {
                 /*
-                Tspan tspan = (Tspan)obj;
+                 Tspan tspan = (Tspan)obj;
                  
-                xform.setToIdentity();
-                xform.setToTranslation(cursorX, cursorY);
+                 xform.setToIdentity();
+                 xform.setToTranslation(cursorX, cursorY);
                  
-                Shape tspanShape = tspan.getShape();
-                tspanShape = xform.createTransformedShape(tspanShape);
-                textArea.add(new Area(tspanShape));
+                 Shape tspanShape = tspan.getShape();
+                 tspanShape = xform.createTransformedShape(tspanShape);
+                 textArea.add(new Area(tspanShape));
                  
-                cursorX += tspanShape.getBounds2D().getWidth();
+                 cursorX += tspanShape.getBounds2D().getWidth();
                  */
-                
-                
-                Tspan tspan = (Tspan)obj;
+
+
+                Tspan tspan = (Tspan) obj;
                 tspan.setCursorX(cursorX);
                 tspan.setCursorY(cursorY);
@@ -390,8 +421,8 @@
                 cursorX = tspan.getCursorX();
                 cursorY = tspan.getCursorY();
-                
-            }
-        }
-        
+
+            }
+        }
+
         switch (textAnchor)
         {
@@ -412,6 +443,5 @@
         }
     }
-    
-    
+
     public void render(Graphics2D g) throws SVGException
     {
@@ -420,18 +450,19 @@
         finishLayer(g);
     }
-    
+
     public Shape getShape()
     {
         return shapeToParent(textShape);
     }
-    
+
     public Rectangle2D getBoundingBox() throws SVGException
     {
         return boundsToParent(includeStrokeInBounds(textShape.getBounds2D()));
     }
-    
+
     /**
-     * Updates all attributes in this diagram associated with a time event.
-     * Ie, all attributes with track information.
+     * Updates all attributes in this diagram associated with a time event. Ie,
+     * all attributes with track information.
+     *
      * @return - true if this node has changed state as a result of the time
      * update
@@ -441,9 +472,9 @@
 //        if (trackManager.getNumTracks() == 0) return false;
         boolean changeState = super.updateTime(curTime);
-        
+
         //Get current values for parameters
         StyleAttribute sty = new StyleAttribute();
         boolean shapeChange = false;
-        
+
         if (getPres(sty.setName("x")))
         {
@@ -455,5 +486,5 @@
             }
         }
-        
+
         if (getPres(sty.setName("y")))
         {
@@ -465,5 +496,5 @@
             }
         }
-        
+
         if (getPres(sty.setName("font-family")))
         {
@@ -475,5 +506,5 @@
             }
         }
-        
+
         if (getPres(sty.setName("font-size")))
         {
@@ -485,6 +516,6 @@
             }
         }
-        
-        
+
+
         if (getStyle(sty.setName("font-style")))
         {
@@ -494,10 +525,8 @@
             {
                 newVal = TXST_NORMAL;
-            }
-            else if ("italic".equals(s))
+            } else if ("italic".equals(s))
             {
                 newVal = TXST_ITALIC;
-            }
-            else if ("oblique".equals(s))
+            } else if ("oblique".equals(s))
             {
                 newVal = TXST_OBLIQUE;
@@ -509,5 +538,5 @@
             }
         }
-        
+
         if (getStyle(sty.setName("font-weight")))
         {
@@ -517,6 +546,5 @@
             {
                 newVal = TXWE_NORMAL;
-            }
-            else if ("bold".equals(s))
+            } else if ("bold".equals(s))
             {
                 newVal = TXWE_BOLD;
@@ -528,5 +556,5 @@
             }
         }
-        
+
         if (shapeChange)
         {
@@ -535,5 +563,5 @@
 //            return true;
         }
-        
+
         return changeState || shapeChange;
     }
