From 5e4f748abffe48305e3d112e683d71cbec5155b3 Mon Sep 17 00:00:00 2001 From: wout Date: Fri, 21 Dec 2012 17:28:17 +0100 Subject: [PATCH] Streamlined code --- README.md | 31 ++++---- Rakefile | 2 +- dist/svg.js | 186 +++++++++++++++++++++++++++-------------------- dist/svg.min.js | 4 +- src/arrange.js | 8 +- src/clip.js | 17 ++++- src/container.js | 2 +- src/defs.js | 15 +--- src/doc.js | 8 +- src/element.js | 40 +++++++++- src/path.js | 2 +- src/sugar.js | 20 +++++ src/text.js | 53 ++++---------- 13 files changed, 218 insertions(+), 170 deletions(-) diff --git a/README.md b/README.md index 60ca2aa..e3a7caa 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Svg.js -Svg.js is a lightweight (2.5k gzipped) library for manipulating SVG. +Svg.js is a lightweight (less than 3k gzipped) library for manipulating SVG. Svg.js is licensed under the terms of the MIT License. @@ -94,7 +94,7 @@ rect.remove(); ### Text element -Text elements can be created with a single style applied. +Text elements can be created with an object as the first argument defining the 'text' content, 'x' and 'y': ```javascript var text = draw.text({ text: "svg\nto\nthe\npoint.", @@ -102,20 +102,13 @@ var text = draw.text({ y: 0 }); ``` -Styling text can be done using the 'font()' method which is both a setter and a getter: +Styling text can be done using the 'attr()': ```javascript -// get a single font property -text.font('size'); - -// set a single font property -text.font('anchor', 'start'); - -// set multiple font properties at once text.font({ - family: 'Helvetica', - size: 36, - anchor: 'middle', - leading: 1.5 + 'font-family': 'Helvetica', + 'font-size': 36, + 'text-anchor': 'middle', + 'leading': 1.5 }); ``` Changing text afterwards is also possible with the 'text()' method: @@ -124,13 +117,15 @@ text.text('Brilliant!'); ``` To get the raw text content: ```javascript -text.content; +text.attr('text'); ``` -Setting attributes like fill, opacity, x and y is the same as other elements: +The sugar.js module provides some syntax sugar specifically for this element type: ```javascript text.attr({ - fill: '#333', - x: 100 + family: 'Helvetica', + size: 144, + anchor: 'middle', + leading: 1.5 }); ``` diff --git a/Rakefile b/Rakefile index 0f44e65..2d1d0c5 100644 --- a/Rakefile +++ b/Rakefile @@ -1,7 +1,7 @@ SVGJS_VERSION = '0.1a' # all available modules in the correct loading order -ALL = %w[ svg container element group arrange clip doc defs shape rect circle ellipse path image text sugar ] +ALL = %w[ svg container element group arrange defs clip doc shape rect circle ellipse path image text sugar ] # required modules to make the library operational CORE = %w[ circle container defs doc element ellipse image path rect shape svg text ] diff --git a/dist/svg.js b/dist/svg.js index 6560668..1553e61 100644 --- a/dist/svg.js +++ b/dist/svg.js @@ -1,4 +1,4 @@ -/* svg.js 0.1a - svg container element group arrange clip doc defs shape rect circle ellipse path image text sugar - svgjs.com/license */ +/* svg.js 0.1a - svg container element group arrange defs clip doc shape rect circle ellipse path image text sugar - svgjs.com/license */ (function() { this.SVG = { @@ -134,7 +134,7 @@ 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 ? @@ -152,6 +152,8 @@ SVG.Element = function Element(n) { this.node = n; this.attrs = {}; + + this._s = ('size family weight stretch variant style').split(' '); }; // Add element-specific functions @@ -184,18 +186,38 @@ // 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); } @@ -242,6 +264,16 @@ 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; } }); @@ -274,9 +306,7 @@ // 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); @@ -295,9 +325,7 @@ // 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); @@ -309,6 +337,16 @@ }); + SVG.Defs = function Defs() { + this.constructor.call(this, SVG.create('defs')); + }; + + // inherit from SVG.Element + SVG.Defs.prototype = new SVG.Element(); + + // include the container object + SVG.extend(SVG.Defs, SVG.Container); + var clipID = 0; SVG.Clip = function Clip() { @@ -327,9 +365,9 @@ 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); }, @@ -340,16 +378,28 @@ } }); - + + // add def-specific functions + SVG.extend(SVG.Defs, { + + // define clippath + clipPath: function() { + var e = new SVG.Clip(); + this.add(e); + + return e; + } + + }); 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); @@ -363,29 +413,6 @@ // include the container object SVG.extend(SVG.Doc, SVG.Container); - SVG.Defs = function Defs() { - this.constructor.call(this, SVG.create('defs')); - }; - - // inherit from SVG.Element - 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; - } - - }); - SVG.Shape = function Shape(element) { this.constructor.call(this, element); }; @@ -482,7 +509,7 @@ SVG.extend(SVG.Path, { // set path data - data: function(d) { + plot: function(d) { return this.attr('d', d); } @@ -508,11 +535,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 @@ -523,47 +548,29 @@ 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() { @@ -573,8 +580,7 @@ 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; } @@ -654,6 +660,26 @@ }); + // 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/dist/svg.min.js b/dist/svg.min.js index 3ae8654..9b20932 100644 --- a/dist/svg.min.js +++ b/dist/svg.min.js @@ -1,2 +1,2 @@ -/* svg.js 0.1a - svg container element group arrange clip doc defs shape rect circle ellipse path image text sugar - svgjs.com/license */ -(function(){function t(){this.constructor.call(this,SVG.create("tspan"))}this.SVG={ns:"http://www.w3.org/2000/svg",xlink:"http://www.w3.org/1999/xlink",create:function(e){return document.createElementNS(this.ns,e)},extend:function(e,t){for(var n in t)e.prototype[n]=t[n]}},this.svg=function(e){return new SVG.Doc(e)},SVG.Container={add:function(e,t){return this.has(e)||(t=t==null?this.children().length:t,this.children().splice(t,0,e),this.node.insertBefore(e.node,this.node.childNodes[t]),e.parent=this),this},has:function(e){return this.children().indexOf(e)>=0},children:function(){return this._children||(this._children=[])},remove:function(e){return this.removeAt(this.children().indexOf(e))},removeAt:function(e){if(0<=e&&e1&&t.remove(this).add(this,e-1),this},front:function(){return this.mother().remove(this).add(this),this},back:function(){var e,t=this.mother();return t.levelDefs(),e=this.siblings().indexOf(this),e>1&&t.remove(this).add(this,0),this}});var e=0;SVG.Clip=function(){this.constructor.call(this,SVG.create("clipPath")),this.id="_"+e++,this.attr("id",this.id)},SVG.Clip.prototype=new SVG.Element,SVG.extend(SVG.Clip,SVG.Container),SVG.extend(SVG.Element,{clip:function(e){var t=this.mother().defs().clipPath();return e(t),this.clipTo(t)},clipTo:function(e){return this.attr("clip-path","url(#"+e.id+")")}}),SVG.Doc=function(t){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(),typeof t=="string"&&(t=document.getElementById(t)),t.appendChild(this.node)},SVG.Doc.prototype=new SVG.Element,SVG.extend(SVG.Doc,SVG.Container),SVG.Defs=function(){this.constructor.call(this,SVG.create("defs"))},SVG.Defs.prototype=new SVG.Element,SVG.extend(SVG.Defs,SVG.Container),SVG.extend(SVG.Defs,{clipPath:function(){var e=new SVG.Clip;return this.add(e),e}}),SVG.Shape=function(t){this.constructor.call(this,t)},SVG.Shape.prototype=new SVG.Element,SVG.Rect=function(){this.constructor.call(this,SVG.create("rect"))},SVG.Rect.prototype=new SVG.Shape,SVG.Circle=function(){this.constructor.call(this,SVG.create("circle"))},SVG.Circle.prototype=new SVG.Shape,SVG.extend(SVG.Circle,{move:function(e,t){return this.attrs.x=e,this.attrs.y=t,this.center()},size:function(e){return this.attr("r",e/2).center()},center:function(e,t){var n=this.attrs.r||0;return this.attr({cx:e||(this.attrs.x||0)+n,cy:t||(this.attrs.y||0)+n})}}),SVG.Ellipse=function(){this.constructor.call(this,SVG.create("ellipse"))},SVG.Ellipse.prototype=new SVG.Shape,SVG.extend(SVG.Ellipse,{move:function(e,t){return this.attrs.x=e,this.attrs.y=t,this.center()},size:function(e,t){return this.attr({rx:e/2,ry:t/2}).center()},center:function(e,t){return this.attr({cx:e||(this.attrs.x||0)+(this.attrs.rx||0),cy:t||(this.attrs.y||0)+(this.attrs.ry||0)})}}),SVG.Path=function(){this.constructor.call(this,SVG.create("path"))},SVG.Path.prototype=new SVG.Shape,SVG.extend(SVG.Path,{data:function(e){return this.attr("d",e)}}),SVG.Image=function(){this.constructor.call(this,SVG.create("image"))},SVG.Image.prototype=new SVG.Shape,SVG.extend(SVG.Image,{load:function(e){return this.attr("href",e,SVG.xlink)}}),SVG.Text=function(){this.constructor.call(this,SVG.create("text")),this.style={"font-size":16,"font-family":"Helvetica"},this.leading=1.2,this.anchor="start",this._s="size family weight stretch variant style".split(" "),this._p="leading anchor".split(" ")},SVG.Text.prototype=new SVG.Shape,SVG.extend(SVG.Text,{text:function(e){this.content=e=e||"text";var n,r=this.parentDoc(),i=e.split("\n");while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);for(n=0,l=i.length;n=0;n--)e[r[n]]!=null&&(this.style["font-"+r[n]]=e[r[n]]);r=this._p;for(n=r.length-1;n>=0;n--)e[r[n]]!=null&&(this[r[n]]=e[r[n]])}else{if(t==null)return this._p.indexOf(e)>-1?this[e]:this._s.indexOf(e)>-1?this.style["font-"+e]:void 0;var r={};r[e]=t,this.font(r)}return this.text(this.content)},_style:function(){var e,t="",n=this._s;for(e=n.length-1;e>=0;e--)this.style["font-"+n[e]]!=null&&(t+="font-"+n[e]+":"+this.style["font-"+n[e]]+";");return this.anchor!=null&&(t+="text-anchor:"+this.anchor+";"),t}}),t.prototype=new SVG.Shape,SVG.extend(t,{text:function(e){return this.node.appendChild(document.createTextNode(e)),this}}),SVG.extend(SVG.Shape,{fill:function(e){return e.color!=null&&this.attr("fill",e.color),e.opacity!=null&&this.attr("fill-opacity",e.opacity),this},stroke:function(e){e.color&&this.attr("stroke",e.color);var t="width opacity linecap linejoin miterlimit dasharray dashoffset".split(" ");for(var n=t.length-1;n>=0;n--)e[t[n]]!=null&&this.attr("stroke-"+t[n],e[t[n]]);return this}}),SVG.extend(SVG.Element,{rotate:function(e){var t=this.bbox();return e.x==null&&(e.x=t.cx),e.y==null&&(e.y=t.cy),this.transform("rotate("+(e.deg||0)+" "+e.x+" "+e.y+")",e.relative)}}),SVG.extend(SVG.G,{move:function(e,t){return this.transform("translate("+e+" "+t+")")}})}).call(this); \ No newline at end of file +/* svg.js 0.1a - svg container element group arrange defs clip doc shape rect circle ellipse path image text sugar - svgjs.com/license */ +(function(){function t(){this.constructor.call(this,SVG.create("tspan"))}this.SVG={ns:"http://www.w3.org/2000/svg",xlink:"http://www.w3.org/1999/xlink",create:function(e){return document.createElementNS(this.ns,e)},extend:function(e,t){for(var n in t)e.prototype[n]=t[n]}},this.svg=function(e){return new SVG.Doc(e)},SVG.Container={add:function(e,t){return this.has(e)||(t=t==null?this.children().length:t,this.children().splice(t,0,e),this.node.insertBefore(e.node,this.node.childNodes[t]),e.parent=this),this},has:function(e){return this.children().indexOf(e)>=0},children:function(){return this._children||(this._children=[])},remove:function(e){return this.removeAt(this.children().indexOf(e))},removeAt:function(e){if(0<=e&&e=0;r--)this.lines[r].attr(e,t);else n!=null?this.node.setAttributeNS(n,e,t):this.node.setAttribute(e,t)}return this},transform:function(e,t){var n=[],r=this.attr("transform")||"",i=r.match(/([a-z]+\([^\)]+\))/g)||[];if(t!==!0){var s=e.match(/^([A-Za-z\-]+)/)[1],t=new RegExp("^"+s);for(var o=0,r=i.length;o1&&t.remove(this).add(this,e-1),this},front:function(){return this.mother().remove(this).add(this),this},back:function(){var e,t=this.mother().levelDefs();return e=this.siblings().indexOf(this),e>1&&t.remove(this).add(this,0),this}}),SVG.Defs=function(){this.constructor.call(this,SVG.create("defs"))},SVG.Defs.prototype=new SVG.Element,SVG.extend(SVG.Defs,SVG.Container);var e=0;SVG.Clip=function(){this.constructor.call(this,SVG.create("clipPath")),this.id="_"+e++,this.attr("id",this.id)},SVG.Clip.prototype=new SVG.Element,SVG.extend(SVG.Clip,SVG.Container),SVG.extend(SVG.Element,{clip:function(e){var t=this.mother().defs().clipPath();return e(t),this.clipTo(t)},clipTo:function(e){return this.attr("clip-path","url(#"+e.id+")")}}),SVG.extend(SVG.Defs,{clipPath:function(){var e=new SVG.Clip;return this.add(e),e}}),SVG.Doc=function(t){this.constructor.call(this,SVG.create("svg")),this.attr({xmlns:SVG.ns,version:"1.1"}).attr("xlink",SVG.xlink,SVG.ns).defs(),typeof t=="string"&&(t=document.getElementById(t)),t.appendChild(this.node)},SVG.Doc.prototype=new SVG.Element,SVG.extend(SVG.Doc,SVG.Container),SVG.Shape=function(t){this.constructor.call(this,t)},SVG.Shape.prototype=new SVG.Element,SVG.Rect=function(){this.constructor.call(this,SVG.create("rect"))},SVG.Rect.prototype=new SVG.Shape,SVG.Circle=function(){this.constructor.call(this,SVG.create("circle"))},SVG.Circle.prototype=new SVG.Shape,SVG.extend(SVG.Circle,{move:function(e,t){return this.attrs.x=e,this.attrs.y=t,this.center()},size:function(e){return this.attr("r",e/2).center()},center:function(e,t){var n=this.attrs.r||0;return this.attr({cx:e||(this.attrs.x||0)+n,cy:t||(this.attrs.y||0)+n})}}),SVG.Ellipse=function(){this.constructor.call(this,SVG.create("ellipse"))},SVG.Ellipse.prototype=new SVG.Shape,SVG.extend(SVG.Ellipse,{move:function(e,t){return this.attrs.x=e,this.attrs.y=t,this.center()},size:function(e,t){return this.attr({rx:e/2,ry:t/2}).center()},center:function(e,t){return this.attr({cx:e||(this.attrs.x||0)+(this.attrs.rx||0),cy:t||(this.attrs.y||0)+(this.attrs.ry||0)})}}),SVG.Path=function(){this.constructor.call(this,SVG.create("path"))},SVG.Path.prototype=new SVG.Shape,SVG.extend(SVG.Path,{plot:function(e){return this.attr("d",e)}}),SVG.Image=function(){this.constructor.call(this,SVG.create("image"))},SVG.Image.prototype=new SVG.Shape,SVG.extend(SVG.Image,{load:function(e){return this.attr("href",e,SVG.xlink)}}),SVG.Text=function(){this.constructor.call(this,SVG.create("text")),this.style={"font-size":16,"font-family":"Helvetica","text-anchor":"start"},this.leading=1.2,this.lines=[]},SVG.Text.prototype=new SVG.Shape,SVG.extend(SVG.Text,{text:function(e){this.content=e=e||"text",this.lines=[];var n,r,i=this.parentDoc(),s=e.split("\n");while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);for(n=0,l=s.length;n=0;e--)this.style["font-"+n[e]]!=null&&(t+="font-"+n[e]+":"+this.style["font-"+n[e]]+";");return t+="text-anchor:"+this.style["text-anchor"]+";",t}}),t.prototype=new SVG.Shape,SVG.extend(t,{text:function(e){return this.node.appendChild(document.createTextNode(e)),this}}),SVG.extend(SVG.Shape,{fill:function(e){return e.color!=null&&this.attr("fill",e.color),e.opacity!=null&&this.attr("fill-opacity",e.opacity),this},stroke:function(e){e.color&&this.attr("stroke",e.color);var t="width opacity linecap linejoin miterlimit dasharray dashoffset".split(" ");for(var n=t.length-1;n>=0;n--)e[t[n]]!=null&&this.attr("stroke-"+t[n],e[t[n]]);return this}}),SVG.extend(SVG.Element,{rotate:function(e){var t=this.bbox();return e.x==null&&(e.x=t.cx),e.y==null&&(e.y=t.cy),this.transform("rotate("+(e.deg||0)+" "+e.x+" "+e.y+")",e.relative)}}),SVG.extend(SVG.G,{move:function(e,t){return this.transform("translate("+e+" "+t+")")}}),SVG.extend(SVG.Text,{font:function(e){var t={};for(var n in e)n=="leading"?t[n]=e[n]:n=="anchor"?t["text-anchor"]=e[n]:this._s.indexOf(n)>-1?t["font-"+n]=e[n]:void 0;return this.attr(t).text(this.content)}})}).call(this); \ No newline at end of file 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; } -- 2.39.5