用 Batik 写入 unicode 字符不起作用
我正在用 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 XX
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 OutputStreamWriter 编写文档并将字符编码显式指定为支持 unicode 的内容:
Try writing the document with an OutputStreamWriter and specifying the character encoding explicitly to something that supports unicode: