gpt4 book ai didi

java - 从命令行生成 SVG 失败,但使用 NetBeans 生成成功

转载 作者:行者123 更新时间:2023-12-01 14:58:37 26 4
gpt4 key购买 nike

我有一个奇怪的问题。我目前正在尝试生成 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?感谢您的任何意见。

最佳答案

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

关于java - 从命令行生成 SVG 失败,但使用 NetBeans 生成成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14002676/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com