minor fix for text positioning

This commit is contained in:
Niklas von Hertzen 2011-07-18 19:21:55 +03:00
parent f8012224f9
commit 7ac9042f33
3 changed files with 14 additions and 9 deletions

View File

@ -1005,16 +1005,17 @@ html2canvas.prototype.newText = function(el,textNode,ctx){
text_align = text_align.replace(["-webkit-auto"],["auto"]) text_align = text_align.replace(["-webkit-auto"],["auto"])
if (this.opts.letterRendering == false && /^(left|right|justify|center|auto)$/.test(text_align) && /^(normal|none)$/.test(letter_spacing)){ if (this.opts.letterRendering == false && /^(left|right|justify|auto)$/.test(text_align) && /^(normal|none)$/.test(letter_spacing)){
// this.setContextVariable(ctx,"textAlign",text_align); // this.setContextVariable(ctx,"textAlign",text_align);
renderWords = true; renderWords = true;
renderList = textNode.nodeValue.split(/\b/); renderList = textNode.nodeValue.split(/(\b| )/);
}else{ }else{
this.setContextVariable(ctx,"textAlign","left"); // this.setContextVariable(ctx,"textAlign","left");
renderList = textNode.nodeValue.split(""); renderList = textNode.nodeValue.split("");
} }
this.setContextVariable(ctx,"fillStyle",color); this.setContextVariable(ctx,"fillStyle",color);
this.setContextVariable(ctx,"font",font); this.setContextVariable(ctx,"font",font);
@ -1024,6 +1025,8 @@ html2canvas.prototype.newText = function(el,textNode,ctx){
var oldTextNode = textNode; var oldTextNode = textNode;
for(var c=0;c<renderList.length;c++){ for(var c=0;c<renderList.length;c++){
// TODO only do the splitting for non-range prints
var newTextNode = oldTextNode.splitText(renderList[c].length); var newTextNode = oldTextNode.splitText(renderList[c].length);

View File

@ -33,9 +33,8 @@ c.arguments[1],c.arguments[2]):c.name=="drawImage"?b.ctx.drawImage(c.arguments[0
html2canvas.prototype.sortQueue=function(a){if(!this.opts.reorderZ||!this.needReorder)return a;var b=0;this.each(a,function(a,c){if(b<c.zIndex.length)b=c.zIndex.length});var c=0;this.each(a,function(d,e){for(var f=a.length.toString().length-c.toString().length;b>e.zIndex.length;)e.zIndex+="0";for(e.zIndex+=c;b+f+c.toString().length>e.zIndex.length;)e.zIndex+="0";c++});return a=a.sort(function(a,b){return a.zIndex<b.zIndex?-1:a.zIndex>b.zIndex?1:0})}; html2canvas.prototype.sortQueue=function(a){if(!this.opts.reorderZ||!this.needReorder)return a;var b=0;this.each(a,function(a,c){if(b<c.zIndex.length)b=c.zIndex.length});var c=0;this.each(a,function(d,e){for(var f=a.length.toString().length-c.toString().length;b>e.zIndex.length;)e.zIndex+="0";for(e.zIndex+=c;b+f+c.toString().length>e.zIndex.length;)e.zIndex+="0";c++});return a=a.sort(function(a,b){return a.zIndex<b.zIndex?-1:a.zIndex>b.zIndex?1:0})};
html2canvas.prototype.setContextVariable=function(a,b,c){a.storage?a.storage.push({type:"variable",name:b,arguments:c}):a[b]=c}; html2canvas.prototype.setContextVariable=function(a,b,c){a.storage?a.storage.push({type:"variable",name:b,arguments:c}):a[b]=c};
html2canvas.prototype.newText=function(a,b,c){var d=this.getCSS(a,"font-family"),e=this.getCSS(a,"font-size"),f=this.getCSS(a,"color"),g=this.getCSS(a,"font-weight"),k=this.getCSS(a,"font-style"),i=this.getCSS(a,"font-variant"),j=this.getCSS(a,"text-decoration"),h=this.getCSS(a,"text-align"),l=this.getCSS(a,"letter-spacing");b.nodeValue=this.textTransform(b.nodeValue,this.getCSS(a,"text-transform"));if(this.trim(b.nodeValue).length>0){switch(g){case "401":g="bold"}if(j!="none")var m=this.fontMetrics(d, html2canvas.prototype.newText=function(a,b,c){var d=this.getCSS(a,"font-family"),e=this.getCSS(a,"font-size"),f=this.getCSS(a,"color"),g=this.getCSS(a,"font-weight"),k=this.getCSS(a,"font-style"),i=this.getCSS(a,"font-variant"),j=this.getCSS(a,"text-decoration"),h=this.getCSS(a,"text-align"),l=this.getCSS(a,"letter-spacing");b.nodeValue=this.textTransform(b.nodeValue,this.getCSS(a,"text-transform"));if(this.trim(b.nodeValue).length>0){switch(g){case "401":g="bold"}if(j!="none")var m=this.fontMetrics(d,
e);a=i+" "+g+" "+k+" "+e+" "+d;h=h.replace(["-webkit-auto"],["auto"]);this.opts.letterRendering==!1&&/^(left|right|justify|center|auto)$/.test(h)&&/^(normal|none)$/.test(l)?h=b.nodeValue.split(/\b/):(this.setContextVariable(c,"textAlign","left"),h=b.nodeValue.split(""));this.setContextVariable(c,"fillStyle",f);this.setContextVariable(c,"font",a);for(l=0;l<h.length;l++){a=b.splitText(h[l].length);this.support.rangeBounds?(document.createRange?(d=document.createRange(),d.selectNode(b)):d=document.body.createTextRange(), e);a=i+" "+g+" "+k+" "+e+" "+d;h=h.replace(["-webkit-auto"],["auto"]);h=this.opts.letterRendering==!1&&/^(left|right|justify|auto)$/.test(h)&&/^(normal|none)$/.test(l)?b.nodeValue.split(/(\b| )/):b.nodeValue.split("");this.setContextVariable(c,"fillStyle",f);this.setContextVariable(c,"font",a);for(l=0;l<h.length;l++){a=b.splitText(h[l].length);this.support.rangeBounds?(document.createRange?(d=document.createRange(),d.selectNode(b)):d=document.body.createTextRange(),d=d.getBoundingClientRect()?d.getBoundingClientRect():
d=d.getBoundingClientRect()?d.getBoundingClientRect():{}):(e=b.parentNode,g=document.createElement("wrapper"),k=b.cloneNode(!0),g.appendChild(b.cloneNode(!0)),e.replaceChild(g,b),d=this.getBounds(g),e.replaceChild(k,g));this.printText(b.nodeValue,d.left,d.bottom,c);switch(j){case "underline":this.newRect(c,d.left,Math.round(d.top+m.baseline+m.lineWidth),d.width,1,f);break;case "overline":this.newRect(c,d.left,d.top,d.width,1,f);break;case "line-through":this.newRect(c,d.left,Math.ceil(d.top+m.middle+ {}):(e=b.parentNode,g=document.createElement("wrapper"),k=b.cloneNode(!0),g.appendChild(b.cloneNode(!0)),e.replaceChild(g,b),d=this.getBounds(g),e.replaceChild(k,g));this.printText(b.nodeValue,d.left,d.bottom,c);switch(j){case "underline":this.newRect(c,d.left,Math.round(d.top+m.baseline+m.lineWidth),d.width,1,f);break;case "overline":this.newRect(c,d.left,d.top,d.width,1,f);break;case "line-through":this.newRect(c,d.left,Math.ceil(d.top+m.middle+m.lineWidth),d.width,1,f)}b=a}}};
m.lineWidth),d.width,1,f)}b=a}}};
html2canvas.prototype.fontMetrics=function(a,b){var c=this.fontData.indexOf(a+"-"+b);if(c>-1)return this.fontData[c+1];c=document.createElement("div");document.getElementsByTagName("body")[0].appendChild(c);$(c).css({visibility:"hidden",fontFamily:a,fontSize:b,margin:0,padding:0});var d=document.createElement("img");d.src="http://html2canvas.hertzen.com/images/8.jpg";d.width=1;d.height=1;$(d).css({margin:0,padding:0});var e=document.createElement("span");$(e).css({fontFamily:a,fontSize:b,margin:0, html2canvas.prototype.fontMetrics=function(a,b){var c=this.fontData.indexOf(a+"-"+b);if(c>-1)return this.fontData[c+1];c=document.createElement("div");document.getElementsByTagName("body")[0].appendChild(c);$(c).css({visibility:"hidden",fontFamily:a,fontSize:b,margin:0,padding:0});var d=document.createElement("img");d.src="http://html2canvas.hertzen.com/images/8.jpg";d.width=1;d.height=1;$(d).css({margin:0,padding:0});var e=document.createElement("span");$(e).css({fontFamily:a,fontSize:b,margin:0,
padding:0});e.appendChild(document.createTextNode("Hidden Text"));c.appendChild(e);c.appendChild(d);var f=d.offsetTop-e.offsetTop+1;c.removeChild(e);c.appendChild(document.createTextNode("Hidden Text"));$(c).css("line-height","normal");$(d).css("vertical-align","super");d={baseline:f,lineWidth:1,middle:d.offsetTop-c.offsetTop+1};this.fontData.push(a+"-"+b);this.fontData.push(d);$(c).remove();return d}; padding:0});e.appendChild(document.createTextNode("Hidden Text"));c.appendChild(e);c.appendChild(d);var f=d.offsetTop-e.offsetTop+1;c.removeChild(e);c.appendChild(document.createTextNode("Hidden Text"));$(c).css("line-height","normal");$(d).css("vertical-align","super");d={baseline:f,lineWidth:1,middle:d.offsetTop-c.offsetTop+1};this.fontData.push(a+"-"+b);this.fontData.push(d);$(c).remove();return d};
html2canvas.prototype.textTransform=function(a,b){switch(b){case "lowercase":return a.toLowerCase();case "capitalize":return a.replace(/(^|\s|:|-|\(|\))([a-z])/g,function(a,b,e){return b+e.toUpperCase()});case "uppercase":return a.toUpperCase();default:return a}};html2canvas.prototype.trim=function(a){return a.replace(/^\s*/,"").replace(/\s*$/,"")};html2canvas.prototype.parseElement=function(a,b){var c=this;this.each(a.children,function(a,e){c.parsing(e,b)});this.Renderer(this.contextStacks);this.finish()}; html2canvas.prototype.textTransform=function(a,b){switch(b){case "lowercase":return a.toLowerCase();case "capitalize":return a.replace(/(^|\s|:|-|\(|\))([a-z])/g,function(a,b,e){return b+e.toUpperCase()});case "uppercase":return a.toUpperCase();default:return a}};html2canvas.prototype.trim=function(a){return a.replace(/^\s*/,"").replace(/\s*$/,"")};html2canvas.prototype.parseElement=function(a,b){var c=this;this.each(a.children,function(a,e){c.parsing(e,b)});this.Renderer(this.contextStacks);this.finish()};

View File

@ -40,16 +40,17 @@ html2canvas.prototype.newText = function(el,textNode,ctx){
text_align = text_align.replace(["-webkit-auto"],["auto"]) text_align = text_align.replace(["-webkit-auto"],["auto"])
if (this.opts.letterRendering == false && /^(left|right|justify|center|auto)$/.test(text_align) && /^(normal|none)$/.test(letter_spacing)){ if (this.opts.letterRendering == false && /^(left|right|justify|auto)$/.test(text_align) && /^(normal|none)$/.test(letter_spacing)){
// this.setContextVariable(ctx,"textAlign",text_align); // this.setContextVariable(ctx,"textAlign",text_align);
renderWords = true; renderWords = true;
renderList = textNode.nodeValue.split(/\b/); renderList = textNode.nodeValue.split(/(\b| )/);
}else{ }else{
this.setContextVariable(ctx,"textAlign","left"); // this.setContextVariable(ctx,"textAlign","left");
renderList = textNode.nodeValue.split(""); renderList = textNode.nodeValue.split("");
} }
this.setContextVariable(ctx,"fillStyle",color); this.setContextVariable(ctx,"fillStyle",color);
this.setContextVariable(ctx,"font",font); this.setContextVariable(ctx,"font",font);
@ -59,6 +60,8 @@ html2canvas.prototype.newText = function(el,textNode,ctx){
var oldTextNode = textNode; var oldTextNode = textNode;
for(var c=0;c<renderList.length;c++){ for(var c=0;c<renderList.length;c++){
// TODO only do the splitting for non-range prints
var newTextNode = oldTextNode.splitText(renderList[c].length); var newTextNode = oldTextNode.splitText(renderList[c].length);