From 1ae730fec364f70d4541f38930929607dc2a31ac Mon Sep 17 00:00:00 2001 From: wout Date: Sun, 23 Dec 2012 11:52:07 +0100 Subject: [PATCH] Fixed a bug in safari prevending correct dy on tspans --- dist/svg.js | 51 +++++++++++++++++++++++++++++++++++------------- dist/svg.min.js | 4 ++-- src/arrange.js | 10 +++++----- src/clip.js | 2 +- src/container.js | 24 ++++++++++++++++++++++- src/doc.js | 9 +++++++-- src/element.js | 5 ----- 7 files changed, 75 insertions(+), 30 deletions(-) diff --git a/dist/svg.js b/dist/svg.js index 8626879..7be4b13 100644 --- a/dist/svg.js +++ b/dist/svg.js @@ -1,4 +1,4 @@ -/* svg.js v0.1-12-g0967c92 - svg container element group arrange defs clip gradient doc shape rect circle ellipse path image text sugar - svgjs.com/license */ +/* svg.js v0.1-13-g7039458 - svg container element group arrange defs clip gradient doc shape rect circle ellipse path image text sugar - svgjs.com/license */ (function() { this.SVG = { @@ -60,7 +60,7 @@ return this._defs; }, - levelDefs: function() { + level: function() { var d = this.defs(); this.remove(d).add(d, 0); @@ -119,6 +119,28 @@ gradient: function(t, b) { return this.defs().gradient(t, b); + }, + + // hack for safari preventing text to be rendered in one line, + // basically it sets the position of the svg node to absolute + // when the dom is loaded, and resets it to relative a few ms later. + stage: function() { + if (document.readyState !== 'complete') { + var r, e = this; + + r = function() { + if (document.readyState === 'complete') { + e.node.style.position = 'absolute'; + setTimeout(function() { e.node.style.position = 'relative'; }, 5); + } else { + setTimeout(r, 10); + } + }; + + r(); + } + + return this; } }; @@ -152,11 +174,6 @@ parentDoc: function() { return this._parent(SVG.Doc); }, - - // get parent svg wrapper - mother: function() { - return this.parentDoc(); - }, // set svg element attribute attr: function(a, v, n) { @@ -267,20 +284,20 @@ // get all siblings, including me siblings: function() { - return this.mother().children(); + return this.parent.children(); }, // send given element one step forwards forward: function() { var i = this.siblings().indexOf(this); - this.mother().remove(this).add(this, i + 1); + this.parent.remove(this).add(this, i + 1); return this; }, // send given element one step backwards backward: function() { - var i, p = this.mother().levelDefs(); + var i, p = this.parent.level(); i = this.siblings().indexOf(this); @@ -292,14 +309,14 @@ // send given element all the way to the front front: function() { - this.mother().remove(this).add(this); + this.parent.remove(this).add(this); return this; }, // send given element all the way to the back back: function() { - var i, p = this.mother().levelDefs(); + var i, p = this.parent.level(); i = this.siblings().indexOf(this); @@ -340,7 +357,7 @@ // clip element using another element clip: function(b) { - var p = this.mother().defs().clip(); + var p = this.parent.defs().clip(); b(p); return this.clipTo(p); @@ -486,12 +503,15 @@ // set this. - attr({ xmlns: SVG.ns, version: '1.1', style: 'position:absolute;', x: 0, y: 0 }). + attr({ xmlns: SVG.ns, version: '1.1', style: 'position:relative;' }). attr('xlink', SVG.xlink, SVG.ns). size(e.offsetWidth, e.offsetHeight). defs(); e.appendChild(this.node); + + // ensure + this.stage(); }; // inherit from SVG.Element @@ -499,6 +519,9 @@ // include the container object SVG.extend(SVG.Doc, SVG.Container); + + + SVG.Shape = function Shape(element) { this.constructor.call(this, element); diff --git a/dist/svg.min.js b/dist/svg.min.js index c9f2268..cbec6df 100644 --- a/dist/svg.min.js +++ b/dist/svg.min.js @@ -1,2 +1,2 @@ -/* svg.js v0.1-12-g0967c92 - svg container element group arrange defs clip gradient doc shape rect circle ellipse path image text sugar - svgjs.com/license */ -function svg(e){return new SVG.Doc(e)}(function(){function n(){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]}},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);this._isStyle(e)&&(e=="text"?this.text(t):e=="leading"?this[e]=t:this.style[e]=t,this.text(this.content))}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="svgjs_clip_"+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().clip();return e(t),this.clipTo(t)},clipTo:function(e){return this.attr("clip-path","url(#"+e.id+")")}}),SVG.extend(SVG.Defs,{clip:function(){var e=new SVG.Clip;return this.add(e),e}});var t=0;SVG.Gradient=function(n){this.constructor.call(this,SVG.create(n+"Gradient")),this.id="svgjs_grad_"+t++,this.type=n,this.attr("id",this.id)},SVG.Gradient.prototype=new SVG.Element,SVG.extend(SVG.Gradient,SVG.Container),SVG.extend(SVG.Gradient,{from:function(e,t){return this.type=="radial"?this.attr({fx:e+"%",fy:t+"%"}):this.attr({x1:e+"%",y1:t+"%"})},to:function(e,t){return this.type=="radial"?this.attr({cx:e+"%",cy:t+"%"}):this.attr({x2:e+"%",y2:t+"%"})},radius:function(e){return this.type=="radial"?this.attr({r:e+"%"}):this},at:function(e){var t=new SVG.Stop(e);return this.add(t),t},update:function(e){while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);return e(this),this},fill:function(){return"url(#"+this.id+")"}}),SVG.extend(SVG.Defs,{gradient:function(e,t){var n=new SVG.Gradient(e);return this.add(n),t(n),n}}),SVG.Stop=function(t){this.constructor.call(this,SVG.create("stop")),this.update(t)},SVG.Stop.prototype=new SVG.Element,SVG.extend(SVG.Stop,{update:function(e){var t="",n=["opacity","color"];for(var r=n.length-1;r>=0;r--)e[n[r]]!=null&&(t+="stop-"+n[r]+":"+e[n[r]]+";");return this.attr({offset:(e.offset!=null?e.offset:this.attr("offset")||0)+"%",style:t})}}),SVG.Doc=function(t){this.constructor.call(this,SVG.create("svg")),typeof t=="string"&&(t=document.getElementById(t)),this.attr({xmlns:SVG.ns,version:"1.1",style:"position:absolute;",x:0,y:0}).attr("xlink",SVG.xlink,SVG.ns).size(t.offsetWidth,t.offsetHeight).defs(),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("xlink: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 t,r,i=this._style(),s=this.parentDoc(),o=e.split("\n");while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);for(t=0,l=o.length;t=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}}),n.prototype=new SVG.Shape,SVG.extend(n,{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 typeof e=="number"&&(e={deg:e}),this.transform("rotate("+(e.deg||0)+" "+(e.x==null?t.cx:e.x)+" "+(e.y==null?t.cx: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 +/* svg.js v0.1-13-g7039458 - svg container element group arrange defs clip gradient doc shape rect circle ellipse path image text sugar - svgjs.com/license */ +function svg(e){return new SVG.Doc(e)}(function(){function n(){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]}},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);this._isStyle(e)&&(e=="text"?this.text(t):e=="leading"?this[e]=t:this.style[e]=t,this.text(this.content))}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.parent.remove(this).add(this),this},back:function(){var e,t=this.parent.level();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="svgjs_clip_"+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.parent.defs().clip();return e(t),this.clipTo(t)},clipTo:function(e){return this.attr("clip-path","url(#"+e.id+")")}}),SVG.extend(SVG.Defs,{clip:function(){var e=new SVG.Clip;return this.add(e),e}});var t=0;SVG.Gradient=function(n){this.constructor.call(this,SVG.create(n+"Gradient")),this.id="svgjs_grad_"+t++,this.type=n,this.attr("id",this.id)},SVG.Gradient.prototype=new SVG.Element,SVG.extend(SVG.Gradient,SVG.Container),SVG.extend(SVG.Gradient,{from:function(e,t){return this.type=="radial"?this.attr({fx:e+"%",fy:t+"%"}):this.attr({x1:e+"%",y1:t+"%"})},to:function(e,t){return this.type=="radial"?this.attr({cx:e+"%",cy:t+"%"}):this.attr({x2:e+"%",y2:t+"%"})},radius:function(e){return this.type=="radial"?this.attr({r:e+"%"}):this},at:function(e){var t=new SVG.Stop(e);return this.add(t),t},update:function(e){while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);return e(this),this},fill:function(){return"url(#"+this.id+")"}}),SVG.extend(SVG.Defs,{gradient:function(e,t){var n=new SVG.Gradient(e);return this.add(n),t(n),n}}),SVG.Stop=function(t){this.constructor.call(this,SVG.create("stop")),this.update(t)},SVG.Stop.prototype=new SVG.Element,SVG.extend(SVG.Stop,{update:function(e){var t="",n=["opacity","color"];for(var r=n.length-1;r>=0;r--)e[n[r]]!=null&&(t+="stop-"+n[r]+":"+e[n[r]]+";");return this.attr({offset:(e.offset!=null?e.offset:this.attr("offset")||0)+"%",style:t})}}),SVG.Doc=function(t){this.constructor.call(this,SVG.create("svg")),typeof t=="string"&&(t=document.getElementById(t)),this.attr({xmlns:SVG.ns,version:"1.1",style:"position:relative;"}).attr("xlink",SVG.xlink,SVG.ns).size(t.offsetWidth,t.offsetHeight).defs(),t.appendChild(this.node),this.stage()},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("xlink: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 t,r,i=this._style(),s=this.parentDoc(),o=e.split("\n");while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);for(t=0,l=o.length;t=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}}),n.prototype=new SVG.Shape,SVG.extend(n,{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 typeof e=="number"&&(e={deg:e}),this.transform("rotate("+(e.deg||0)+" "+(e.x==null?t.cx:e.x)+" "+(e.y==null?t.cx: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 b5dc41a..001da75 100644 --- a/src/arrange.js +++ b/src/arrange.js @@ -4,20 +4,20 @@ SVG.extend(SVG.Element, { // get all siblings, including me siblings: function() { - return this.mother().children(); + return this.parent.children(); }, // send given element one step forwards forward: function() { var i = this.siblings().indexOf(this); - this.mother().remove(this).add(this, i + 1); + this.parent.remove(this).add(this, i + 1); return this; }, // send given element one step backwards backward: function() { - var i, p = this.mother().levelDefs(); + var i, p = this.parent.level(); i = this.siblings().indexOf(this); @@ -29,14 +29,14 @@ SVG.extend(SVG.Element, { // send given element all the way to the front front: function() { - this.mother().remove(this).add(this); + this.parent.remove(this).add(this); return this; }, // send given element all the way to the back back: function() { - var i, p = this.mother().levelDefs(); + var i, p = this.parent.level(); i = this.siblings().indexOf(this); diff --git a/src/clip.js b/src/clip.js index f4fba45..9b106b4 100644 --- a/src/clip.js +++ b/src/clip.js @@ -19,7 +19,7 @@ SVG.extend(SVG.Element, { // clip element using another element clip: function(b) { - var p = this.mother().defs().clip(); + var p = this.parent.defs().clip(); b(p); return this.clipTo(p); diff --git a/src/container.js b/src/container.js index f45bd6c..c5658ee 100644 --- a/src/container.js +++ b/src/container.js @@ -44,7 +44,7 @@ SVG.Container = { return this._defs; }, - levelDefs: function() { + level: function() { var d = this.defs(); this.remove(d).add(d, 0); @@ -103,6 +103,28 @@ SVG.Container = { gradient: function(t, b) { return this.defs().gradient(t, b); + }, + + // hack for safari preventing text to be rendered in one line, + // basically it sets the position of the svg node to absolute + // when the dom is loaded, and resets it to relative a few ms later. + stage: function() { + if (document.readyState !== 'complete') { + var r, e = this; + + r = function() { + if (document.readyState === 'complete') { + e.node.style.position = 'absolute'; + setTimeout(function() { e.node.style.position = 'relative'; }, 5); + } else { + setTimeout(r, 10); + } + }; + + r(); + } + + return this; } }; \ No newline at end of file diff --git a/src/doc.js b/src/doc.js index ead1116..0322e86 100644 --- a/src/doc.js +++ b/src/doc.js @@ -8,16 +8,21 @@ SVG.Doc = function Doc(e) { // set this. - attr({ xmlns: SVG.ns, version: '1.1' }). + attr({ xmlns: SVG.ns, version: '1.1', style: 'position:relative;' }). attr('xlink', SVG.xlink, SVG.ns). size(e.offsetWidth, e.offsetHeight). defs(); e.appendChild(this.node); + + // ensure + this.stage(); }; // inherit from SVG.Element SVG.Doc.prototype = new SVG.Element(); // include the container object -SVG.extend(SVG.Doc, SVG.Container); \ No newline at end of file +SVG.extend(SVG.Doc, SVG.Container); + + diff --git a/src/element.js b/src/element.js index 5664f46..ff3ad37 100644 --- a/src/element.js +++ b/src/element.js @@ -28,11 +28,6 @@ SVG.extend(SVG.Element, { parentDoc: function() { return this._parent(SVG.Doc); }, - - // get parent svg wrapper - mother: function() { - return this.parentDoc(); - }, // set svg element attribute attr: function(a, v, n) { -- 2.39.5