0

我有一个奇怪的问题。我目前正在尝试使用 svg 生成,我使用以下代码生成 svg 文件:

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

但是,如果我键入 Ruccc 而不是 Rúccc,那么无论是从命令行还是从浏览器,一切都很好。我想问题在于 ú 的匈牙利字符的存在。如何在 Java 中使用 svg 生成处理匈牙利字符,以便从命令行正确生成我的 svg?感谢您的任何意见。

4

2 回答 2

1

使用PrintWriter的 2 参数形式并指定允许匈牙利语输出的适当字符集。

于 2012-12-22T13:35:05.080 回答
0

似乎是字符编码问题。当您从命令行开始时尝试设置编码-Dfile.encoding=...(用您需要的编码替换点)

于 2012-12-22T12:12:20.730 回答