【发布时间】:2014-07-15 15:34:50
【问题描述】:
我正在尝试编写一个RaphaelJS 函数,它将获取Raphael paper 实例中的现有文本节点并将它们转换为路径。
目标是复制文本在页面上的位置、大小和属性,但使用路径而不是文本呈现。我最初无法使用 Raphael paper.print() 函数呈现文本,因为文本是动态更新的,并且需要基于“文本”的属性来执行此操作。将现有文本节点转换为路径将作为过程中的“最后”步骤(在文本修改完成之后)发生。
我这样做是为了消除安装字体以便以后查看或处理 SVG 的需要。
我面临的挑战是:
文本节点可能包含带有
x和dy定义的tspan。创建的路径必须与每个 childNode 字母 (tspan) 完美对齐。获取文本节点的实际位置数据,以及每个tspan。这就是我遇到麻烦的地方,希望有更多经验的人可以帮助我。由于笔划宽度和其他属性会影响定位/bbox 值,我不确定为文本获取正确定位数据的最有效方法是什么。
到目前为止我所做的尝试:
我的代码的简单分解。
我编写了一个自定义属性函数 textFormat,它以交错的形式格式化文本。此函数解析文本节点,将其拆分为每个字母添加一个新行\n 字符,并调整位置以使其看起来交错。
textToPaths 函数是一个纸函数,它应该循环遍历纸节点,并使用 Raphael paper.print() 函数将所有找到的文本节点转换为路径。这是我遇到问题的功能。
View the Complete JSFiddle Example Here
问题代码
我不确定如何获得准确且一致的x 和y 值以传递给paper.print() 函数。现在,我正在使用getBoundingClientRect(),但它仍然处于关闭状态并且出现偏差。我的假设是笔划宽度会影响 x 和 y 计算。
//Loop through each tspan and print the path for each.
var i,
children = node.node.childNodes,
len = children.length;
for (i = 0; i < len; i++) {
var tspan = children[i],
tspanText = tspan.innerHTML,
x = tspan.getBoundingClientRect().left - node.node.getBoundingClientRect().left, //How do I get the correct x value?
y = tspan.getBoundingClientRect().top - node.node.getBoundingClientRect().top; //How do I get the correcy y value?
var path = paper.print(x, y, tspanText, font, fontSize),
attrs = node.attrs;
delete attrs.x;
delete attrs.y;
path.attr(attrs);
path.attr('fill', '#ff0000'); //Red, for testing purposes.
}
完整代码 View the JSFiddle Example
//Register Cufon Font
var paper = Raphael(document.getElementById('paper'), '600', '600');
var text1 = paper.text(100, 100, 'abc').attr({fill: 'none',stroke: '#000000',"stroke-width": '12',"stroke-miterlimit": '1',"font-family" : "Lobster", "font-size": '30px','stroke-opacity': '1'});
var text2 = paper.text(100, 100, 'abc').attr({fill: 'none',stroke: '#ffffff',"stroke-width": '8',"stroke-miterlimit": '1',"font-family" : "Lobster", "font-size": '30px','stroke-opacity': '1'});
var text3 = paper.text(100, 100, 'abc').attr({fill: '#000000',stroke: '#ffffff',"stroke-width": '0',"stroke-miterlimit": '1',"font-family" : "Lobster", "font-size": '30px','stroke-opacity': '1'});
var text = paper.set(text1, text2, text3);
text.attr('textFormat', 'stagger');
/* paper.textToPaths
* Description: Converts all text nodes to paths within a paper.
*
* Example: paper.textToPaths();
*/
(function(R) {
R.fn.textToPaths = function() {
var paper = this;
//Loop all nodes in the paper.
for (var node = paper.bottom; node != null; node = node.next ) {
if ( node.node.style.display === 'none' || node.type !== "text" || node.attrs.opacity == "0") continue; //skip non-text and hidden nodes.
//Get the font config for this text node.
var text = node.attr('text'),
fontFamily = node.attr('font-family'),
fontSize = parseInt(node.attr('font-size')),
fontWeight = node.attr('font-weight'),
font = paper.getFont(fontFamily, fontWeight);
//Loop through each tspan and print the path for each.
var i,
children = node.node.childNodes,
len = children.length;
for (i = 0; i < len; i++) {
var tspan = children[i],
tspanText = tspan.innerHTML,
x = tspan.getBoundingClientRect().left - node.node.getBoundingClientRect().left, //How do I get the correct x value?
y = tspan.getBoundingClientRect().top - node.node.getBoundingClientRect().top; //How do I get the correcy y value?
var path = paper.print(x, y, tspanText, font, fontSize),
attrs = node.attrs;
delete attrs.x;
delete attrs.y;
path.attr(attrs);
path.attr('fill', '#ff0000'); //Red, for testing purposes.
}
}
};
})(window.Raphael);
textToPaths = function() {
//Run textToPaths
paper.textToPaths();
};
/* Custom Element Attribute: textFormat
* Description: Formats a text element to either staggered or normal text.
*
* Example: element.attr('textFormat, 'stagger');
*/
paper.customAttributes.textFormat = function( value ) {
// Sets the SVG dy attribute, which Raphael doesn't control
var selector = Raphael.svg ? 'tspan' : 'v:textpath',
has = "hasOwnProperty",
$node = $(this.node),
text = $node.text(),
$tspans = $node.find(selector);
console.log('format');
switch(value)
{
case 'stagger' :
var stagger = function(el) {
var R = Raphael,
letters = '',
newline = '\n';
for (var c=0; c < text.length; c++) {
var letter = text[c],
append = '';
if(c < text.length - 1)
append = newline;
letters += letter+append;
}
el.attr('text', letters);
var children = el.node.childNodes;
var i,
a = el.attrs,
node = el.node,
len = children.length,
letterOffset = 0,
tspan,
tspanHeight,
tspanWidth,
tspanX,
prevTspan,
prevTspanRight = 0,
tspanDiff = 0,
tspanTemp,
fontSize,
leading = 1.2,
tempText;
for (i = 0; i < len; i++) {
tspan = children[i];
tspanHeight = tspan.getComputedTextLength();
tspanWidth = tspan.getComputedTextLength();
tspanX = tspan.getAttribute('x'),
prevTspanRight = tspan.getBoundingClientRect().right
if(tspanX !== null)
{
tspanDiff = tspanDiff + prevTspanRight - tspan.getBoundingClientRect().left;
var setX = parseInt(tspanX) + parseInt(tspanDiff);
tspan.setAttribute('x', setX);
tspan.setAttribute('dy', 15);
}
prevTspan = tspan;
}
}
stagger(this);
break;
case 'normal' :
this.attr('text', text);
break;
default :
this.attr('text', text);
break;
}
eve("raphael.attr.textFormat." + this.id, this, value);
// change no default Raphael attributes
return {};
};
staggerText = function() {
//Run textToPaths
text.attr('textFormat', 'stagger');
};
如果有人能帮我解决这个问题,我将不胜感激。谢谢!
【问题讨论】:
-
为什么不使用嵌入式网络字体呢?
-
我试图消除以后安装字体的需要。您不能在 SVG 中嵌入字体信息,因此我需要将文本字形转换为路径;)
-
当然可以,网络字体可以作为数据 URI 嵌入。这是一个例子:xn--dahlstrm-t4a.net/svg/fonts/webfont-datauri.svg
-
那是使用嵌入的字体,这很好,但是像 ImageMagick 这样的服务器端工具不支持 SVG 中的字体,这又导致了同样的问题。我选择消除对渲染字体的要求,句号。我想确保它看起来 100% 相同,而不依赖于不同的字体渲染引擎。我可以保证的唯一方法是使用绝对路径。
标签: javascript svg raphael