aboutsummaryrefslogtreecommitdiffstats
path: root/src/text.js
blob: ecd8811495a7492f8134d752380ae351ffb7540c (plain)
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
// 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();

SVG.extend(SVG.Text, {
  // Set the text content
  text: function(text) {
    /* update the content */
    this.content = text = text || 'text';
    this.lines = [];
    
    var index, length, tspan,
        style   = this._style(),
        parent  = this.doc(),
        lines   = text.split("\n"),
        size    = this.style['font-size'];
    
    /* remove existing child nodes */
    while (this.node.hasChildNodes())
      this.node.removeChild(this.node.lastChild);
    
    /* build new lines */
    for (index = 0, length = lines.length; index < length; index++) {
      /* create new tspan and set attributes */
      tspan = new TSpan().
        text(lines[index]).
        attr({
          dy:     size * this.leading - (index == 0 ? size * 0.3 : 0),
          x:      (this.attrs.x || 0),
          style:  style
        });
      
      /* add new tspan */
      this.node.appendChild(tspan.node);
      this.lines.push(tspan);
    };
    
    /* set style */
    return this.attr('style', style);
  },
  
  // Build style based on _styleAttr
  _style: function() {
    var index, style = '';
    
    for (index = _styleAttr.length - 1; index >= 0; index--)
      if (this.style['font-' + _styleAttr[index]] != null)
        style += 'font-' + _styleAttr[index] + ':' + this.style['font-' + _styleAttr[index]] + ';';
    
    style += 'text-anchor:' + this.style['text-anchor'] + ';';
      
    return style;
  }
  
});


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, {
  // Set text content
  text: function(text) {
    this.node.appendChild(document.createTextNode(text));
    
    return this;
  }
  
});