diff options
author | wout <wout@impinc.co.uk> | 2012-12-29 18:12:53 +0100 |
---|---|---|
committer | wout <wout@impinc.co.uk> | 2012-12-29 18:12:53 +0100 |
commit | 68367e494fcab754ae4833e64804a824032cf31c (patch) | |
tree | c9d5d9fe5995ccbbe819f29752c1133c7415882d /src/text.js | |
parent | 52ed3ba98715ed3c63e74ff29a9892972a7bec19 (diff) | |
download | svg.js-68367e494fcab754ae4833e64804a824032cf31c.tar.gz svg.js-68367e494fcab754ae4833e64804a824032cf31c.zip |
Code refactoring
Diffstat (limited to 'src/text.js')
-rw-r--r-- | src/text.js | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/src/text.js b/src/text.js index 301373c..3fe3f0b 100644 --- a/src/text.js +++ b/src/text.js @@ -1,10 +1,14 @@ +// 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; - this.lines = []; }; // inherit from SVG.Element @@ -14,6 +18,7 @@ SVG.Text.prototype = new SVG.Shape(); SVG.extend(SVG.Text, { text: function(t) { + // update the content this.content = t = t || 'text'; this.lines = []; @@ -23,31 +28,37 @@ SVG.extend(SVG.Text, { 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.attr('x') || 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 = '', s = this._s; + var i, o = ''; - for (i = s.length - 1; i >= 0; i--) - if (this.style['font-' + s[i]] != null) - o += 'font-' + s[i] + ':' + this.style['font-' + s[i]] + ';'; + 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'] + ';'; |