diff options
author | wout <wout@impinc.co.uk> | 2012-12-20 21:31:17 +0100 |
---|---|---|
committer | wout <wout@impinc.co.uk> | 2012-12-20 21:31:17 +0100 |
commit | 675e347a10372a10ecdfd4fa6624c062f1ee7102 (patch) | |
tree | d36b605512862178deb07a1d58a11dc4e794921b /src/text.js | |
parent | 7b5c91ba593fc673ec6d4a8d7dac4b26b1fac52f (diff) | |
download | svg.js-675e347a10372a10ecdfd4fa6624c062f1ee7102.tar.gz svg.js-675e347a10372a10ecdfd4fa6624c062f1ee7102.zip |
Added font element
Diffstat (limited to 'src/text.js')
-rw-r--r-- | src/text.js | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/text.js b/src/text.js new file mode 100644 index 0000000..3b50216 --- /dev/null +++ b/src/text.js @@ -0,0 +1,85 @@ + +SVG.Text = function Text() { + this.constructor.call(this, SVG.create('text')); + + this.style = { 'font-size': 16 }; + 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) { + this.content = t = t || 'text'; + + var i, + p = this.parentDoc(), + a = t.split("\n"); + + while (this.node.hasChildNodes()) + this.node.removeChild(this.node.lastChild); + + for (i = 0, l = a.length; i < l; i++) + this.node.appendChild(new TSpan(). + text(a[i]). + attr('style', this._style()). + attr({ dy: this.style['font-size'] * this.leading, x: (this.attr('x') || 0) }).node ); + + return this; + }, + + font: function(o) { + var i, a = ('size family weight stretch variant style').split(' '); + + for (i = a.length - 1; i >= 0; i--) + if (o[a[i]] != null) + this.style['font-' + a[i]] = o[a[i]]; + + a = ('leading kerning anchor').split(' '); + + for (i = a.length - 1; i >= 0; i--) + if (o[a[i]] != null) + this[a[i]] = o[a[i]]; + + return this.text(this.content); + }, + + _style: function() { + var i, s = '', a = ('size family weight stretch variant style').split(' '); + + for (i = a.length - 1; i >= 0; i--) + if (this.style['font-' + a[i]] != null) + s += 'font-' + a[i] + ':' + this.style['font-' + a[i]] + ';'; + + a = ('leading kerning anchor').split(' '); + + for (i = a.length - 1; i >= 0; i--) + if (this[a[i]] != null) + s += a[i] + ':' + this[a[i]] + ';'; + + return s; + } + +}); + + +function TSpan() { + this.constructor.call(this, SVG.create('tspan')); +}; + +// inherit from SVG.Element +TSpan.prototype = new SVG.Shape(); + +// include the container object +SVG.extend(TSpan, { + + text: function(t) { + this.node.appendChild(document.createTextNode(t)); + + return this; + } + +});
\ No newline at end of file |