Ignore:
Timestamp:
2014-10-30T11:39:47+01:00 (11 years ago)
Author:
stoecker
Message:

update SVG code to current SVN (fix line endings), see #10479

File:
1 edited

Legend:

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

    r6002 r7676  
    4444import java.awt.geom.AffineTransform;
    4545import java.awt.geom.GeneralPath;
     46import java.awt.geom.Point2D;
    4647import java.awt.geom.Rectangle2D;
    4748
     
    6061    float[] rotate = null;
    6162    private String text = "";
    62     float cursorX;
    63     float cursorY;
     63//    float cursorX;
     64//    float cursorY;
    6465
    6566//    Shape tspanShape;
     
    7677    }
    7778
    78     public float getCursorX()
    79     {
    80         return cursorX;
    81     }
    82 
    83     public float getCursorY()
    84     {
    85         return cursorY;
    86     }
    87 
    88     public void setCursorX(float cursorX)
    89     {
    90         this.cursorX = cursorX;
    91     }
    92 
    93     public void setCursorY(float cursorY)
    94     {
    95         this.cursorY = cursorY;
    96     }
     79//    public float getCursorX()
     80//    {
     81//        return cursorX;
     82//    }
     83//
     84//    public float getCursorY()
     85//    {
     86//        return cursorY;
     87//    }
     88//
     89//    public void setCursorX(float cursorX)
     90//    {
     91//        this.cursorX = cursorX;
     92//    }
     93//
     94//    public void setCursorY(float cursorY)
     95//    {
     96//        this.cursorY = cursorY;
     97//    }
    9798    /*
    9899     public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
     
    165166    }
    166167
    167     public void addShape(GeneralPath addShape) throws SVGException
    168     {
    169         if (x != null)
    170         {
    171             cursorX = x[0];
    172         } else if (dx != null)
    173         {
    174             cursorX += dx[0];
    175         }
    176 
    177         if (y != null)
    178         {
    179             cursorY = y[0];
    180         } else if (dy != null)
    181         {
    182             cursorY += dy[0];
    183         }
     168    public void appendToShape(GeneralPath addShape, Point2D cursor) throws SVGException
     169    {
     170//        if (x != null)
     171//        {
     172//            cursorX = x[0];
     173//        } else if (dx != null)
     174//        {
     175//            cursorX += dx[0];
     176//        }
     177//
     178//        if (y != null)
     179//        {
     180//            cursorY = y[0];
     181//        } else if (dy != null)
     182//        {
     183//            cursorY += dy[0];
     184//        }
    184185
    185186        StyleAttribute sty = new StyleAttribute();
     
    209210        if (font == null)
    210211        {
    211             addShapeSysFont(addShape, font, fontFamily, fontSize, letterSpacing);
     212            addShapeSysFont(addShape, font, fontFamily, fontSize, letterSpacing, cursor);
    212213            return;
    213214        }
     
    221222        strokeWidthScalar = 1f / fontScale;
    222223
    223         int posPtr = 1;
    224 
    225         for (int i = 0; i < text.length(); i++)
    226         {
     224        float cursorX = (float)cursor.getX();
     225        float cursorY = (float)cursor.getY();
     226   
     227//        int i = 0;
     228
     229        String drawText = this.text;
     230        drawText = drawText.trim();
     231        for (int i = 0; i < drawText.length(); i++)
     232        {
     233            if (x != null && i < x.length)
     234            {
     235                cursorX = x[i];
     236            } else if (dx != null && i < dx.length)
     237            {
     238                cursorX += dx[i];
     239            }
     240           
     241            if (y != null && i < y.length)
     242            {
     243                cursorY = y[i];
     244            } else if (dy != null && i < dy.length)
     245            {
     246                cursorY += dy[i];
     247            }
     248  //          i++;
     249           
    227250            xform.setToIdentity();
    228251            xform.setToTranslation(cursorX, cursorY);
     
    230253            if (rotate != null)
    231254            {
    232                 xform.rotate(rotate[posPtr]);
    233             }
    234 
    235             String unicode = text.substring(i, i + 1);
     255                xform.rotate(rotate[i]);
     256            }
     257
     258            String unicode = drawText.substring(i, i + 1);
    236259            MissingGlyph glyph = font.getGlyph(unicode);
    237260
     
    243266            }
    244267
    245             if (x != null && posPtr < x.length)
    246             {
    247                 cursorX = x[posPtr];
    248                 cursorY = y[posPtr++];
    249             } else if (dx != null && posPtr < dx.length)
    250             {
    251                 cursorX += dx[posPtr];
    252                 cursorY += dy[posPtr++];
    253             }
    254 
    255268            cursorX += fontScale * glyph.getHorizAdvX() + letterSpacing;
    256269        }
    257270
     271        //Save final draw point so calling method knows where to begin next
     272        // text draw
     273        cursor.setLocation(cursorX, cursorY);
    258274        strokeWidthScalar = 1f;
    259275    }
    260276
    261277    private void addShapeSysFont(GeneralPath addShape, Font font,
    262         String fontFamily, float fontSize, float letterSpacing)
    263     {
     278        String fontFamily, float fontSize, float letterSpacing, Point2D cursor)
     279    {
     280
    264281        java.awt.Font sysFont = new java.awt.Font(fontFamily, java.awt.Font.PLAIN, (int) fontSize);
    265282
    266283        FontRenderContext frc = new FontRenderContext(null, true, true);
    267         GlyphVector textVector = sysFont.createGlyphVector(frc, text);
     284        String renderText = this.text.trim();
    268285
    269286        AffineTransform xform = new AffineTransform();
    270287
    271         int posPtr = 1;
    272         for (int i = 0; i < text.length(); i++)
    273         {
     288        float cursorX = (float)cursor.getX();
     289        float cursorY = (float)cursor.getY();
     290//        int i = 0;
     291        for (int i = 0; i < renderText.length(); i++)
     292        {
     293            if (x != null && i < x.length)
     294            {
     295                cursorX = x[i];
     296            } else if (dx != null && i < dx.length)
     297            {
     298                cursorX += dx[i];
     299            }
     300
     301            if (y != null && i < y.length)
     302            {
     303                cursorY = y[i];
     304            } else if (dy != null && i < dy.length)
     305            {
     306                cursorY += dy[i];
     307            }
     308//            i++;
     309           
    274310            xform.setToIdentity();
    275             xform.setToTranslation(cursorX + i * letterSpacing, cursorY);
     311            xform.setToTranslation(cursorX, cursorY);
    276312            if (rotate != null)
    277313            {
     
    279315            }
    280316
    281             String unicode = text.substring(i, i + 1);
    282             Shape glyphOutline = textVector.getGlyphOutline(i);
    283             GlyphMetrics glyphMetrics = textVector.getGlyphMetrics(i);
     317//            String unicode = renderText.substring(i, i + 1);
     318            GlyphVector textVector = sysFont.createGlyphVector(frc, renderText.substring(i, i + 1));
     319            Shape glyphOutline = textVector.getGlyphOutline(0);
     320            GlyphMetrics glyphMetrics = textVector.getGlyphMetrics(0);
    284321
    285322            glyphOutline = xform.createTransformedShape(glyphOutline);
    286323            addShape.append(glyphOutline, false);
    287324
    288             if (x != null && posPtr < x.length)
    289             {
    290                 cursorX = x[posPtr];
    291                 cursorY = y[posPtr++];
    292             } else if (dx != null && posPtr < dx.length)
    293             {
    294                 cursorX += dx[posPtr];
    295                 cursorY += dy[posPtr++];
    296             }
    297         }
     325
     326//            cursorX += fontScale * glyph.getHorizAdvX() + letterSpacing;
     327            cursorX += glyphMetrics.getAdvance() + letterSpacing;
     328        }
     329       
     330        cursor.setLocation(cursorX, cursorY);
    298331    }
    299332
    300333    public void render(Graphics2D g) throws SVGException
    301334    {
     335        float cursorX = 0;
     336        float cursorY = 0;
     337   
    302338        if (x != null)
    303339        {
     
    385421    protected void renderSysFont(Graphics2D g, java.awt.Font font) throws SVGException
    386422    {
     423        float cursorX = 0;
     424        float cursorY = 0;
     425   
    387426        int posPtr = 1;
    388427        FontRenderContext frc = g.getFontRenderContext();
Note: See TracChangeset for help on using the changeset viewer.