2012-12-22 124 views
0

我有一個奇怪的問題。目前,我想要自己離開與SVG生成和我用下面的代碼來生成一個SVG文件:SVG生成從命令行失敗,但NetBeans成功執行

public class SVGGenerator { 

    private static void drawSquare(Graphics2D g2d) { 
     g2d.setPaint(Color.blue); 
     g2d.setFont(new Font(g2d.getFont().getName(), g2d.getFont().getStyle(), 60)); 
     g2d.drawString("Ruccc", 200, 64); 
     g2d.setFont(new Font(g2d.getFont().getName(), g2d.getFont().getStyle(), 120)); 
     AffineTransform fontAT = new AffineTransform(); 

     // get the current font 
     Font theFont = g2d.getFont(); 

     // Derive a new font using a rotatation transform 
     fontAT.rotate(Math.PI/2); 
     Font theDerivedFont = theFont.deriveFont(fontAT); 

     // set the derived font in the Graphics2D context 
     g2d.setFont(theDerivedFont); 

     // Render a string using the derived font 
     g2d.setPaint(Color.red); 
     g2d.drawString("bloody", 200, 64); 
     g2d.setPaint(Color.green); 
     // put the original font back 
     g2d.setFont(new Font(g2d.getFont().getName(), g2d.getFont().getStyle(), 60)); 
     g2d.drawString("Ruccc", 200, 440); 
    } 

    public static void GenerateSVG() 
    { 
     DOMImplementation dom = GenericDOMImplementation.getDOMImplementation(); 
     Document doc = dom.createDocument(null, "svg", null); 
     SVGGraphics2D generator = new SVGGraphics2D(doc); 
     drawSquare(generator); 
     // Write the generated SVG document to a file 
     try { 
       FileWriter file = new FileWriter("c://Development//out.svg"); 
       PrintWriter writer = new PrintWriter(file); 
       generator.stream(writer); 
       writer.close(); 
      } catch (IOException ioe) { 
       System.err.println("IO problem: " + ioe.toString()); 
      } 
    }  
} 

如果我跑我與NetBeans程序,它會生成以下內容:

<?xml version="1.0"?> 
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN' 
      'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'> 
<svg fill-opacity="1" xmlns:xlink="http://www.w3.org/1999/xlink" color-rendering="auto" color-interpolation="auto" stroke="black" text-rendering="auto" stroke-linecap="square" stroke-miterlimit="10" stroke-opacity="1" shape-rendering="auto" fill="black" stroke-dasharray="none" font-weight="normal" stroke-width="1" xmlns="http://www.w3.org/2000/svg" font-family="&apos;Dialog&apos;" font-style="normal" stroke-linejoin="miter" font-size="12" stroke-dashoffset="0" image-rendering="auto" 
><!--Generated by the Batik Graphics2D SVG Generator--><defs id="genericDefs" 
    /><g 
    ><g fill="blue" font-size="60" stroke="blue" 
    ><text x="200" xml:space="preserve" y="64" stroke="none" 
     >Rúccc</text 
     ><text x="200" font-size="120" y="64" transform="matrix(0,1,-1,0,264,-136)" fill="red" stroke="none" xml:space="preserve" 
     >bloody</text 
    ></g 
    ><g fill="lime" font-size="60" stroke="lime" 
    ><text x="200" xml:space="preserve" y="440" stroke="none" 
     >Rúccc</text 
    ></g 
    ></g 
></svg 
> 

這在瀏覽器中正確顯示。但是,如果我用CMD運行它,該文件的內容如下:

<?xml version="1.0"?> 
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN' 
      'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'> 
<svg fill-opacity="1" xmlns:xlink="http://www.w3.org/1999/xlink" color-rendering="auto" color-interpolation="auto" stroke="black" text-rendering="auto" stroke-linecap="square" stroke-miterlimit="10" stroke-opacity="1" shape-rendering="auto" fill="black" stroke-dasharray="none" font-weight="normal" stroke-width="1" xmlns="http://www.w3.org/2000/svg" font-family="&apos;Dialog&apos;" font-style="normal" stroke-linejoin="miter" font-size="12" stroke-dashoffset="0" image-rendering="auto" 
><!--Generated by the Batik Graphics2D SVG Generator--><defs id="genericDefs" 
    /><g 
    ><g fill="blue" font-size="60" stroke="blue" 
    ><text x="200" xml:space="preserve" y="64" stroke="none" 
     >Rúccc</text 
     ><text x="200" font-size="120" y="64" transform="matrix(0,1,-1,0,264,-136)" fill="red" stroke="none" xml:space="preserve" 
     >bloody</text 
    ></g 
    ><g fill="lime" font-size="60" stroke="lime" 
    ><text x="200" xml:space="preserve" y="440" stroke="none" 
     >Rúccc</text 
    ></g 
    ></g 
></svg 
> 

在這種情況下,瀏覽器給我下面的錯誤:

XML5617: Illegal XML character. 
out.svg, line 9 character 9 

但是,如果,而不是Rúccc我輸入Ruccc,從命令行和瀏覽器都可以正常工作。我想這個問題是匈牙利人ú的存在。我如何在Java中使用svg generation處理匈牙利字符,以便我的svg能夠通過命令行正確生成?感謝您的任何意見。

回答

1

使用的PrintWriter 2參數形式(由你所需要的編碼更換點),並指定相應的字符集,允許匈牙利輸出。

0

似乎是一個字符編碼問題。嘗試當你從-Dfile.encoding=...從命令行啓動設置編碼

相關問題