1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
// list font style attributes as they should be applied to style
var _styleAttr = ['size', 'family', 'weight', 'stretch', 'variant', 'style'];
SVG.Text = function Text() {
this.constructor.call(this, SVG.create('text'));
// define default style
this.style = { 'font-size': 16, 'font-family': 'Helvetica', 'text-anchor': 'start' };
this.leading = 1.2;
};
// inherit from SVG.Element
SVG.Text.prototype = new SVG.Shape();
// Add image-specific functions
SVG.extend(SVG.Text, {
text: function(t) {
// update the content
this.content = t = t || 'text';
this.lines = [];
var i, n,
s = this._style(),
p = this.parentDoc(),
a = t.split("\n"),
f = this.style['font-size'];
// remove existing child nodes
while (this.node.hasChildNodes())
this.node.removeChild(this.node.lastChild);
// build new lines
for (i = 0, l = a.length; i < l; i++) {
// create new tspan and set attributes
n = new TSpan().
text(a[i]).
attr({
dy: f * this.leading - (i == 0 ? f * 0.3 : 0),
x: (this.attrs.x || 0),
style: s
});
// add new tspan
this.node.appendChild(n.node);
this.lines.push(n);
};
// set style
return this.attr('style', s);
},
// build style based on _styleAttr
_style: function() {
var i, o = '';
for (i = _styleAttr.length - 1; i >= 0; i--)
if (this.style['font-' + _styleAttr[i]] != null)
o += 'font-' + _styleAttr[i] + ':' + this.style['font-' + _styleAttr[i]] + ';';
o += 'text-anchor:' + this.style['text-anchor'] + ';';
return o;
}
});
function TSpan() {
this.constructor.call(this, SVG.create('tspan'));
};
// inherit from SVG.Shape
TSpan.prototype = new SVG.Shape();
// include the container object
SVG.extend(TSpan, {
text: function(t) {
this.node.appendChild(document.createTextNode(t));
return this;
}
});
|