用 Batik 写入 unicode 字符不起作用

发布于 2024-08-09 11:27:12 字数 1428 浏览 8 评论 0原文

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

如果我在 SVGDocument 中写入 unicode 字符,它会在调试器中正确显示,但如果我写入 SVG,则始终会出现 ?,或者对于普通字母,例如 A, ?A 结果。

我认为这是我的作家的问题,但我不知道如何解决。我知道 SVG 有一些解决方案,例如将 unicode 与 &#XXXσ 结合使用,但我无法为节点提供该字符串,它会写入以正确的形式。

这是简短且希望可以理解的代码示例:

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();
}

I am writing a project with Batik, which is for multi-language image. Therefore I need signs like "sigma" or "alpha". I have to write the character as text - not as a polygon or as a glyph - because it has to be written by my project again.

If I write a unicode character in my SVGDocument it is shown correctly in the debugger, but if I write to SVG there is always a ?, or for normal-Letter such as A, ?A as a result.

I think is a problem from my writer but I don't know how to fix it. I know that there are some solutions from SVG like using unicode with &#XXX or σ but I can't give the Node that String and it will written in the correct form.

Here is short and hopefully understandable code example:

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×眷恋的温暖 2024-08-16 11:27:12

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

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

Try writing the document with an OutputStreamWriter and specifying the character encoding explicitly to something that supports unicode:

    OutputStream fileOutputStream = new FileOutputStream(svgFile);
    Writer svgWriter = new OutputStreamWriter(fileOutputStream, "UTF-8"); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文