From: wout Date: Thu, 20 Dec 2012 20:31:17 +0000 (+0100) Subject: Added font element X-Git-Tag: 0.1~7 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=675e347a10372a10ecdfd4fa6624c062f1ee7102;p=svg.js.git Added font element --- diff --git a/.gitignore b/.gitignore index 77fa516..3d6d0dd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .DS_Store public/ bleed/ -obsolete/ \ No newline at end of file +obsolete/ +test/ \ No newline at end of file diff --git a/Rakefile b/Rakefile index 493430c..0f44e65 100644 --- a/Rakefile +++ b/Rakefile @@ -1,10 +1,10 @@ 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 sugar ] +ALL = %w[ svg container element group arrange clip doc defs 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 ] +CORE = %w[ circle container defs doc element ellipse image path rect shape svg text ] # optional modules OPTIONAL = %w[ clip group arrange sugar ] @@ -92,10 +92,10 @@ end desc "Concatenate source files to build svg.js" task :concat, [:modules] do |task, args| modules = args[:modules].to_s.split(':') - to_add, to_exclude = modules.partition {|m| m.sub!(/^(-)?(.+)/, 'src/\2.js'); !$1 } + toattrsdd, to_exclude = modules.partition {|m| m.sub!(/^(-)?(.+)/, 'src/\2.js'); !$1 } Rake::Task['dist/svg.js']. - remove_prerequisites(to_exclude).enhance(to_add). + remove_prerequisites(to_exclude).enhance(toattrsdd). invoke end diff --git a/dist/svg.js b/dist/svg.js index 52a3384..b338b90 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 sugar - svgjs.com/license */ +/* svg.js 0.1a - svg container element group arrange clip doc defs shape rect circle ellipse path image text sugar - svgjs.com/license */ (function() { this.SVG = { @@ -121,6 +121,10 @@ 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) @@ -129,11 +133,13 @@ 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); @@ -408,25 +414,23 @@ 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)) + }); } }); @@ -445,24 +449,23 @@ 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))) + }); } }); @@ -480,8 +483,7 @@ // set path data data: function(d) { - this.attr('d', d); - return this; + return this.attr('d', d); } }); @@ -491,17 +493,98 @@ }; // 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.attr('href', u, SVG.xlink); + } + + }); + + 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; } diff --git a/dist/svg.min.js b/dist/svg.min.js index 7bffff3..ee726d9 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 sugar - svgjs.com/license */ -(function(){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(),this},size:function(e){return this.attr("r",e/2),this.center(),this},center:function(e,t){var n=this.attrs.r||0;this.attr("cx",e||(this.attrs.x||0)+n),this.attr("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(),this},size:function(e,t){return this.attr("rx",e/2),this.attr("ry",t/2),this.center(),this},center:function(e,t){this.attr("cx",e||(this.attrs.x||0)+(this.attrs.rx||0)),this.attr("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),this}}),SVG.Image=function(){this.constructor.call(this,SVG.create("image"))},SVG.Image.prototype=new SVG.Element,SVG.extend(SVG.Image,SVG.Container),SVG.extend(SVG.Image,{load:function(e){return this.attr("href",e,SVG.xlink),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 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},this.leading=1.2},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;t--)e[n[t]]!=null&&(this.style["font-"+n[t]]=e[n[t]]);n="leading kerning anchor".split(" ");for(t=n.length-1;t>=0;t--)e[n[t]]!=null&&(this[n[t]]=e[n[t]]);return this.text(this.content)},_style:function(){var e,t="",n="size family weight stretch variant style".split(" ");for(e=n.length-1;e>=0;e--)this.style["font-"+n[e]]!=null&&(t+="font-"+n[e]+":"+this.style["font-"+n[e]]+";");n="leading kerning anchor".split(" ");for(e=n.length-1;e>=0;e--)this[n[e]]!=null&&(t+=n[e]+":"+this[n[e]]+";");return 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 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