【问题标题】:Writing unicode characters with Batik doesn't work用蜡染写 unicode 字符不起作用
【发布时间】:2009-10-29 10:43:33
【问题描述】:

我正在用 Batik 编写一个用于多语言图像的项目。因此我需要像“sigma”或“alpha”这样的符号。我必须将字符写为文本 - 而不是多边形或字形 - 因为它必须由我的项目再次编写。

如果我在我的 SVGDocument 中写入一个 unicode 字符,它会在调试器中正确显示,但如果我写入 SVG,总会有一个 ?,或者对于普通字母,例如 A?A结果。

我认为是作者的问题,但我不知道如何解决。我知道 SVG 有一些解决方案,例如使用带有 &#XXXσ 的 unicode,但我不能给节点那个字符串,它会以正确的形式写入。

下面是简短且易于理解的代码示例:

enter code here
import java.io.File;
import java.io.FileWriter;
import java.net.URI;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.dom.util.DOMUtilities;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Document;
import org.w3c.dom.Text;
public static void main(String args[]) throws Exception
{
  /* Read Document
   */
  URI source = new URI("file:D:/foo.svg");
  //If there is no Parser:'parser' = null
  String parser = XMLResourceDescriptor.getXMLParserClassName();
  //for right interpretation
  SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
  String sourceUri = source.toString();
  /* add Textnode
   */
  Document doc = f.createSVGDocument(sourceUri);
  String textWithUni = "\u0041";
  Text textNode = doc.createTextNode(textWithUni);
  doc.appendChild(textNode);
  /*write
   */
  File output = new File("newFoo.svg");
  FileWriter fw = new FileWriter(output);
  DOMUtilities.writeDocument(doc, fw);
  fw.flush();
  fw.close();
}

【问题讨论】:

    标签: java unicode batik


    【解决方案1】:

    尝试使用OutputStreamWriter 编写文档,并将字符编码明确指定为支持 unicode 的内容:

        OutputStream fileOutputStream = new FileOutputStream(svgFile);
        Writer svgWriter = new OutputStreamWriter(fileOutputStream, "UTF-8"); 
    

    【讨论】:

    • 感谢它现在可以工作了,它在其中写入了 unicode 符号。太棒了!如果有一个解决方案,我如何在 svg 中写出像 XX 这样的想法,我也会感兴趣。但是非常感谢您的快速回答
    • 卢克,你应该接受答案,给茶机器人的功劳。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 2012-01-02
    • 2019-05-20
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多