summaryrefslogtreecommitdiffstats
path: root/src/element.js
blob: 33813c450bffca3bea799b17cecf88c92405eb45 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
SVG.Element = function Element(n) {
  this.node = n;
  this.attrs = {};
  this.trans = {
    x:        0,
    y:        0,
    scaleX:   1,
    scaleY:   1,
    rotation: 0,
    skewX:    0,
    skewY:    0
  };
  
  this._s = ('size family weight stretch variant style').split(' ');
};

// Add element-specific functions
SVG.extend(SVG.Element, {
  
  // move element to given x and y values
  move: function(x, y) {
    return this.attr({ x: x, y: y });
  },
  
  // set element size to given width and height
  size: function(w, h) {
    return this.attr({ width: w, height: h });
  },
  
  // remove element
  remove: function() {
    return this.parent != null ? this.parent.remove(this) : void 0;
  },
  
  // get parent document
  parentDoc: function() {
    return this._parent(SVG.Doc);
  },
  
  // set svg element attribute
  attr: function(a, v, n) {
    if (arguments.length < 2) {
      if (typeof a == 'object')
        for (v in a) this.attr(v, a[v]);
        
      else if (this._isStyle(a))
        return a == 'text' ?
                 this.content :
               a == 'leading' ?
                 this[a] :
                 this.style[a];
      
      else
        return this.attrs[a];
    
    } else {
      this.attrs[a] = v;
      if (a == 'x' && this._isText())
        for (var i = this.lines.length - 1; i >= 0; i--)
          this.lines[i].attr(a, v);
      else
        n != null ?
          this.node.setAttributeNS(n, a, v) :
          this.node.setAttribute(a, v);
      
      if (this._isStyle(a)) {
        a == 'text' ?
          this.text(v) :
        a == 'leading' ?
          this[a] = v :
          this.style[a] = v;
      
        this.text(this.content);
      }
    }
    
    return this;
  },
  
  transform: function(o) {
    // act as a getter if the first argument is a string
    if (typeof o === 'string')
      return this.trans[o];
      
    // ... otherwise continue as a setter
    var k,
        t = [],
        b = this.bbox(),
        s = this.attr('transform') || '',
        l = s.match(/[a-z]+\([^\)]+\)/g) || [];
    
    // merge values
    for (k in o)
      if (o[k] != null)
        this.trans[k] = o[k];
    
    // alias current transformations
    o = this.trans;
    
    // add rotate
    if (o.rotation != 0)
      t.push('rotate(' + o.rotation + ',' + (o.cx != null ? o.cx : b.cx) + ',' + (o.cy != null ? o.cy : b.cy) + ')');
    
    // add scale
    if (o.scaleX != 1 && o.scaleY != 1)
      t.push('scale(' + o.sx + ',' + o.sy + ')');
    
    // add skew on x axis
    if (o.skewX != 0)
      t.push('skewX(' + x.skewX + ')');
    
    // add skew on y axis
    if (o.skewY != 0)
      t.push('skewY(' + x.skewY + ')')
    
    // add translate
    if (o.x != 0 && o.y != 0)
      t.push('translate(' + o.x + ',' + o.y + ')');
    
    // add only te required transformations
    return this.attr('transform', t.join(' '));
  },
  
  // get bounding box
  bbox: function() {
    // actual bounding box
    var b = this.node.getBBox();
    
    return {
      // include translations on x an y
      x:      b.x + this.trans.x,
      y:      b.x + this.trans.y,
      // add the center
      cx:     b.x + this.trans.x + b.width / 2,
      cy:     b.x + this.trans.y + b.height / 2,
      // plain width and height
      width:  b.width,
      height: b.height
    };
  },
  
  // private: find svg parent
  _parent: function(pt) {
    var e = this;

    while (e != null && !(e instanceof pt))
      e = e.parent;

    return e;
  },
  
  // private: is this text style
  _isStyle: function(a) {
    return typeof a == 'string' && this._isText() ? (/^font|text|leading/).test(a) : false;
  },
  
  // private: element type tester
  _isText: function() {
    return this instanceof SVG.Text;
  }
  
});