带有Unicode字符的SVG文本元素 [英] SVG text element with Unicode characters
问题描述
首先 - 对问题的简短描述。我编写了一个JavaScript函数来将一些文本标签放在SVG画布中。这是:
First - a short description of a problem. I write a JavaScript function to put some text label's in SVG canvas. Here it is:
function set_label(x,y,title)
{
var newTitle = document.createElementNS(svgNS,"text");
newTitle.setAttributeNS(null,"x",x);
newTitle.setAttributeNS(null,"y",y);
newTitle.setAttributeNS(null,"font-size","17px");
newTitle.setAttributeNS(null,"font-family","WinGreek");
var textNode = document.createTextNode(title);
newTitle.appendChild(textNode);
document.getElementById("viewport").appendChild(newTitle);
}
由于我的项目积分,我必须使用希腊字体。所以,我调用函数:
I must use Greek font due to my project points. So, I call function:
set_label (10,50,'Qitos')
这将显示Ktitos标签 - 没问题。
this will display Ktitos label - no problem.
但是 - 当我尝试这样打电话时:
But - when i try to call like this:
set_label (10,50,'QamÚraj')
甚至更糟:
set_label (10,50,'Θαρσανδαλα')
这必须显示Θαρσανδαλα标题形成全部来自特殊字符 - 问题累积 - utf-8符号看起来像我写的 - 用代码:(
this must display Θαρσανδαλα title formed all from special characters - a problem accrue - utf-8 symbol appear like i write it - with code :(
在其他类似问题的一些研究之后,我发现如果我将UTF-8代码转换为ESC序列,就像\ U00B0 - 这必须解决这个问题,但是...
- 我不知道如何做到这一点和
- 如果特殊字符在字符串的中间如何做 - 如第二个例子
After some research here in other similar question's, i find that if I convert UTF-8 code to ESC sequence, like \U00B0 - this must solve that problem, but... - I cant figure it how to do this and - how to do that if special character is in middle of string - like second example
推荐答案
这很简单:只需将您想要的实际Unicode字符放入文档中,将文档保存为UTF-8,并指定文档使用UTF-8字符集。
It's really easy: just put the actual Unicode characters you want into your document, save the document as UTF-8, and specify that the document is using the UTF-8 character set.
以下是我网站上的实例,显示您可以使用这些字符:
Here's a live example on my website showing that you can use these characters:
- 在页面标题中。
- 直接在你的文字中。
- 内部JavaScript字符串。
请注意,我已使用UTF-8编码保存此文件,注意文件的顶部有:
Note that I've saved this file with a UTF-8 encoding and notice that the top of the file has:
<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
关于我的网站关闭的可能性,这是示例的内容:
On the off chance that my site is down, here's the content of the example:
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head>
<meta http-equiv="content-type"
content="application/xhtml+xml; charset=utf-8"/>
<title>Θαρσανδαλα</title>
<style type="text/css" media="screen">
body { background:#eee; margin:0 }
svg { display:block; border:1px solid #ccc; margin:2em auto;
width:300px; height:200px; background:#fff }
svg text { text-anchor:middle }
</style>
</head><body>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" baseProfile="full"
viewBox="-150 -100 300 200">
<text>Inline: Θαρσανδαλα</text>
</svg>
<script type="text/javascript"><![CDATA[
var svg = document.getElementsByTagName('svg')[0];
createOn(svg,'text',{y:20,"font-size":"17px"},"Generated: Θαρσανδαλα");
function createOn(root,name,attrs,text){
var doc=root.ownerDocument, svg=root;
while (svg.tagName!='svg') svg=svg.parentNode;
var svgNS = svg.getAttribute('xmlns');
var el = doc.createElementNS(svgNS,name);
for (var attr in attrs){
if (attrs.hasOwnProperty(attr)){
var parts = attr.split(':');
if (parts[1]) el.setAttributeNS(
svg.getAttribute('xmlns:'+parts[0]), parts[1], attrs[attr]
);
else el.setAttributeNS(null,attr,attrs[attr]);
}
}
if (text) el.appendChild(document.createTextNode(text));
return root.appendChild(el);
}
]]></script>
</body></html>
这篇关于带有Unicode字符的SVG文本元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!