summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorwout <wout@impinc.co.uk>2012-12-21 17:28:17 +0100
committerwout <wout@impinc.co.uk>2012-12-21 17:28:17 +0100
commit5e4f748abffe48305e3d112e683d71cbec5155b3 (patch)
tree3c283536ea906a9260946168f6ad7b3891aa4f58 /src
parentb1516b2580dbdffd5ee68c75d4eb769b52faa100 (diff)
downloadsvg.js-5e4f748abffe48305e3d112e683d71cbec5155b3.tar.gz
svg.js-5e4f748abffe48305e3d112e683d71cbec5155b3.zip
Streamlined code
Diffstat (limited to 'src')
-rw-r--r--src/arrange.js8
-rw-r--r--src/clip.js17
-rw-r--r--src/container.js2
-rw-r--r--src/defs.js15
-rw-r--r--src/doc.js8
-rw-r--r--src/element.js40
-rw-r--r--src/path.js2
-rw-r--r--src/sugar.js20
-rw-r--r--src/text.js53
9 files changed, 96 insertions, 69 deletions
diff --git a/src/arrange.js b/src/arrange.js
index d7fcb5f..b5dc41a 100644
--- a/src/arrange.js
+++ b/src/arrange.js
@@ -17,9 +17,7 @@ SVG.extend(SVG.Element, {
// send given element one step backwards
backward: function() {
- var i, p = this.mother();
-
- p.levelDefs();
+ var i, p = this.mother().levelDefs();
i = this.siblings().indexOf(this);
@@ -38,9 +36,7 @@ SVG.extend(SVG.Element, {
// send given element all the way to the back
back: function() {
- var i, p = this.mother();
-
- p.levelDefs();
+ var i, p = this.mother().levelDefs();
i = this.siblings().indexOf(this);
diff --git a/src/clip.js b/src/clip.js
index 2266adb..ddc2588 100644
--- a/src/clip.js
+++ b/src/clip.js
@@ -18,9 +18,9 @@ SVG.extend(SVG.Clip, SVG.Container);
SVG.extend(SVG.Element, {
// clip element using another element
- clip: function(block) {
+ clip: function(b) {
var p = this.mother().defs().clipPath();
- block(p);
+ b(p);
return this.clipTo(p);
},
@@ -31,3 +31,16 @@ SVG.extend(SVG.Element, {
}
});
+
+// add def-specific functions
+SVG.extend(SVG.Defs, {
+
+ // define clippath
+ clipPath: function() {
+ var e = new SVG.Clip();
+ this.add(e);
+
+ return e;
+ }
+
+}); \ No newline at end of file
diff --git a/src/container.js b/src/container.js
index a6eeb93..868b477 100644
--- a/src/container.js
+++ b/src/container.js
@@ -114,7 +114,7 @@ SVG.Container = {
e.size(v.width, v.height);
v.data != null ?
- e.data(v.data) :
+ e.plot(v.data) :
v.src != null ?
e.load(v.src) :
v.text != null ?
diff --git a/src/defs.js b/src/defs.js
index f5753e1..2ba11e8 100644
--- a/src/defs.js
+++ b/src/defs.js
@@ -7,17 +7,4 @@ SVG.Defs = function Defs() {
SVG.Defs.prototype = new SVG.Element();
// include the container object
-SVG.extend(SVG.Defs, SVG.Container);
-
-// Add def-specific functions
-SVG.extend(SVG.Defs, {
-
- // define clippath
- clipPath: function() {
- var e = new SVG.Clip();
- this.add(e);
-
- return e;
- }
-
-}); \ No newline at end of file
+SVG.extend(SVG.Defs, SVG.Container); \ No newline at end of file
diff --git a/src/doc.js b/src/doc.js
index e307d09..0fe5e21 100644
--- a/src/doc.js
+++ b/src/doc.js
@@ -2,11 +2,11 @@
SVG.Doc = function Doc(e) {
this.constructor.call(this, SVG.create('svg'));
- this.attr('xmlns', SVG.ns);
- this.attr('version', '1.1');
- this.attr('xlink', SVG.xlink, SVG.ns);
- this.defs();
+ this.
+ attr({ xmlns: SVG.ns, version: '1.1' }).
+ attr('xlink', SVG.xlink, SVG.ns).
+ defs();
if (typeof e == 'string')
e = document.getElementById(e);
diff --git a/src/element.js b/src/element.js
index 87ae198..981f9a0 100644
--- a/src/element.js
+++ b/src/element.js
@@ -2,6 +2,8 @@
SVG.Element = function Element(n) {
this.node = n;
this.attrs = {};
+
+ this._s = ('size family weight stretch variant style').split(' ');
};
// Add element-specific functions
@@ -34,18 +36,38 @@ SVG.extend(SVG.Element, {
// 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 if (this._isStyle(a)) {
+ a == 'text' ?
+ this.text(v) :
+ a == 'leading' ?
+ this[a] = v :
+ this.style[a] = v;
+
+ this.text(this.content);
+
} else {
this.attrs[a] = v;
- n != null ?
- this.node.setAttributeNS(n, a, v) :
- this.node.setAttribute(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);
}
@@ -92,6 +114,16 @@ SVG.extend(SVG.Element, {
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;
}
});
diff --git a/src/path.js b/src/path.js
index ab1aa87..5c8ca46 100644
--- a/src/path.js
+++ b/src/path.js
@@ -10,7 +10,7 @@ SVG.Path.prototype = new SVG.Shape();
SVG.extend(SVG.Path, {
// set path data
- data: function(d) {
+ plot: function(d) {
return this.attr('d', d);
}
diff --git a/src/sugar.js b/src/sugar.js
index 18f422e..b9ac577 100644
--- a/src/sugar.js
+++ b/src/sugar.js
@@ -54,6 +54,26 @@ SVG.extend(SVG.G, {
});
+// Add text-specific functions
+SVG.extend(SVG.Text, {
+
+ // set font
+ font: function(o) {
+ var a = {};
+
+ for (var k in o)
+ k == 'leading' ?
+ a[k] = o[k] :
+ k == 'anchor' ?
+ a['text-anchor'] = o[k] :
+ this._s.indexOf(k) > -1 ?
+ a['font-'+ k] = o[k] :
+ void 0;
+
+ return this.attr(a).text(this.content);
+ },
+
+});
diff --git a/src/text.js b/src/text.js
index 78675ed..e102360 100644
--- a/src/text.js
+++ b/src/text.js
@@ -2,11 +2,9 @@
SVG.Text = function Text() {
this.constructor.call(this, SVG.create('text'));
- this.style = { 'font-size': 16, 'font-family': 'Helvetica' };
+ this.style = { 'font-size': 16, 'font-family': 'Helvetica', 'text-anchor': 'start' };
this.leading = 1.2;
- this.anchor = 'start';
- this._s = ('size family weight stretch variant style').split(' ');
- this._p = ('leading anchor').split(' ');
+ this.lines = [];
};
// inherit from SVG.Element
@@ -17,47 +15,29 @@ SVG.extend(SVG.Text, {
text: function(t) {
this.content = t = t || 'text';
+ this.lines = [];
- var i,
+ var i, s,
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().
+ for (i = 0, l = a.length; i < l; i++) {
+ s = 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(a, v) {
- if (typeof a == 'object') {
- var i, s = this._s;
-
- for (i = s.length - 1; i >= 0; i--)
- if (a[s[i]] != null)
- this.style['font-' + s[i]] = a[s[i]];
-
- s = this._p;
-
- for (i = s.length - 1; i >= 0; i--)
- if (a[s[i]] != null)
- this[s[i]] = a[s[i]];
+ attr({
+ dy: this.style['font-size'] * this.leading,
+ x: (this.attr('x') || 0),
+ style: this._style()
+ });
- } else if (v != null) {
- var s = {};
- s[a] = v;
- this.font(s);
-
- } else {
- return this._p.indexOf(a) > -1 ? this[a] : this._s.indexOf(a) > -1 ? this.style['font-' + a] : void 0;
- }
+ this.node.appendChild(s.node);
+ this.lines.push(s);
+ };
- return this.text(this.content);
+ return this;
},
_style: function() {
@@ -67,8 +47,7 @@ SVG.extend(SVG.Text, {
if (this.style['font-' + s[i]] != null)
o += 'font-' + s[i] + ':' + this.style['font-' + s[i]] + ';';
- if (this.anchor != null)
- o += 'text-anchor:' + this.anchor + ';';
+ o += 'text-anchor:' + this.style['text-anchor'] + ';';
return o;
}