summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorwout <wout@impinc.co.uk>2012-12-20 21:31:17 +0100
committerwout <wout@impinc.co.uk>2012-12-20 21:31:17 +0100
commit675e347a10372a10ecdfd4fa6624c062f1ee7102 (patch)
treed36b605512862178deb07a1d58a11dc4e794921b /src
parent7b5c91ba593fc673ec6d4a8d7dac4b26b1fac52f (diff)
downloadsvg.js-675e347a10372a10ecdfd4fa6624c062f1ee7102.tar.gz
svg.js-675e347a10372a10ecdfd4fa6624c062f1ee7102.zip
Added font element
Diffstat (limited to 'src')
-rw-r--r--src/circle.js16
-rw-r--r--src/container.js16
-rw-r--r--src/ellipse.js19
-rw-r--r--src/image.js8
-rw-r--r--src/path.js3
-rw-r--r--src/text.js85
6 files changed, 115 insertions, 32 deletions
diff --git a/src/circle.js b/src/circle.js
index 1fe95e5..d1f2cdc 100644
--- a/src/circle.js
+++ b/src/circle.js
@@ -13,25 +13,23 @@ SVG.extend(SVG.Circle, {
move: function(x, y) {
this.attrs.x = x;
this.attrs.y = y;
- this.center();
-
- return this;
+
+ return this.center();
},
// custom size function (no height value here!)
size: function(w) {
- this.attr('r', w / 2);
- this.center();
-
- return this;
+ return this.attr('r', w / 2).center();
},
// position element by its center
center: function(x, y) {
var r = this.attrs.r || 0;
- this.attr('cx', x || ((this.attrs.x || 0) + r));
- this.attr('cy', y || ((this.attrs.y || 0) + r));
+ return this.attr({
+ cx: (x || ((this.attrs.x || 0) + r)),
+ cy: (y || ((this.attrs.y || 0) + r))
+ });
}
}); \ No newline at end of file
diff --git a/src/container.js b/src/container.js
index d24fe17..a6eeb93 100644
--- a/src/container.js
+++ b/src/container.js
@@ -101,6 +101,10 @@ SVG.Container = {
return this.place(new SVG.Image(), v);
},
+ text: function(v) {
+ return this.place(new SVG.Text(), v);
+ },
+
place: function(e, v) {
if (v != null) {
if (v.x != null && v.y != null)
@@ -109,11 +113,13 @@ SVG.Container = {
if (v.width != null && v.height != null)
e.size(v.width, v.height);
- if (v.data != null)
- e.data(v.data);
-
- if (v.src != null)
- e.load(v.src);
+ v.data != null ?
+ e.data(v.data) :
+ v.src != null ?
+ e.load(v.src) :
+ v.text != null ?
+ e.text(v.text) :
+ void 0;
}
this.add(e);
diff --git a/src/ellipse.js b/src/ellipse.js
index adc1fd6..d897969 100644
--- a/src/ellipse.js
+++ b/src/ellipse.js
@@ -13,24 +13,23 @@ SVG.extend(SVG.Ellipse, {
move: function(x, y) {
this.attrs.x = x;
this.attrs.y = y;
- this.center();
-
- return this;
+
+ return this.center();
},
// custom size function
size: function(w, h) {
- this.attr('rx', w / 2);
- this.attr('ry', h / 2);
- this.center();
-
- return this;
+ return this.
+ attr({ rx: w / 2, ry: h / 2 }).
+ center();
},
// position element by its center
center: function(x, y) {
- this.attr('cx', x || ((this.attrs.x || 0) + (this.attrs.rx || 0)));
- this.attr('cy', y || ((this.attrs.y || 0) + (this.attrs.ry || 0)));
+ return this.attr({
+ cx: (x || ((this.attrs.x || 0) + (this.attrs.rx || 0))),
+ cy: (y || ((this.attrs.y || 0) + (this.attrs.ry || 0)))
+ });
}
});
diff --git a/src/image.js b/src/image.js
index 107fef4..a1d21c0 100644
--- a/src/image.js
+++ b/src/image.js
@@ -4,18 +4,14 @@ SVG.Image = function Image() {
};
// inherit from SVG.Element
-SVG.Image.prototype = new SVG.Element();
-
-// include the container object
-SVG.extend(SVG.Image, SVG.Container);
+SVG.Image.prototype = new SVG.Shape();
// Add image-specific functions
SVG.extend(SVG.Image, {
// (re)load image
load: function(u) {
- this.attr('href', u, SVG.xlink);
- return this;
+ return this.attr('href', u, SVG.xlink);
}
}); \ No newline at end of file
diff --git a/src/path.js b/src/path.js
index 60ec9ef..ab1aa87 100644
--- a/src/path.js
+++ b/src/path.js
@@ -11,8 +11,7 @@ SVG.extend(SVG.Path, {
// set path data
data: function(d) {
- this.attr('d', d);
- return this;
+ return this.attr('d', d);
}
}); \ No newline at end of file
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