mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
Text rendering refactoring
This commit is contained in:
parent
44023015b6
commit
c6baabc99c
117
src/Parse.js
117
src/Parse.js
@ -200,51 +200,19 @@ _html2canvas.Parse = function ( images, options ) {
|
||||
}
|
||||
|
||||
function drawText(currentText, x, y, ctx){
|
||||
if (trimText(currentText).length > 0) {
|
||||
if (currentText !== null && trimText(currentText).length > 0) {
|
||||
ctx.fillText(currentText, x, y);
|
||||
numDraws+=1;
|
||||
}
|
||||
}
|
||||
|
||||
function renderText(el, textNode, stack) {
|
||||
var ctx = stack.ctx,
|
||||
function setTextVariables(ctx, el, text_decoration, color) {
|
||||
var align = false,
|
||||
font_style = getCSS(el, "fontStyle"),
|
||||
bold = getCSS(el, "fontWeight"),
|
||||
family = getCSS(el, "fontFamily"),
|
||||
size = getCSS(el, "fontSize"),
|
||||
color = getCSS(el, "color"),
|
||||
text_decoration = getCSS(el, "textDecoration"),
|
||||
text_align = getCSS(el, "textAlign"),
|
||||
letter_spacing = getCSS(el, "letterSpacing"),
|
||||
bounds,
|
||||
text,
|
||||
metrics,
|
||||
renderList,
|
||||
listLen,
|
||||
bold = getCSS(el, "fontWeight"),
|
||||
font_style = getCSS(el, "fontStyle"),
|
||||
font_variant = getCSS(el, "fontVariant"),
|
||||
align = false,
|
||||
newTextNode,
|
||||
textValue,
|
||||
textOffset = 0,
|
||||
oldTextNode,
|
||||
c,
|
||||
range,
|
||||
parent,
|
||||
wrapElement,
|
||||
backupText;
|
||||
|
||||
textNode.nodeValue = textTransform(textNode.nodeValue, getCSS(el, "textTransform"));
|
||||
text = trimText(textNode.nodeValue);
|
||||
|
||||
if (text.length>0){
|
||||
|
||||
if (text_decoration !== "none"){
|
||||
metrics = fontMetrics(family, size);
|
||||
}
|
||||
|
||||
text_align = text_align.replace(["-webkit-auto"],["auto"]);
|
||||
|
||||
renderList = (!options.letterRendering && /^(left|right|justify|auto)$/.test(text_align) && noLetterSpacing(letter_spacing)) ? textNode.nodeValue.split(/(\b| )/) : textNode.nodeValue.split("");
|
||||
font_variant = getCSS(el, "fontVariant");
|
||||
|
||||
switch(parseInt(bold, 10)){
|
||||
case 401:
|
||||
@ -258,7 +226,59 @@ _html2canvas.Parse = function ( images, options ) {
|
||||
ctx.setVariable("fillStyle", color);
|
||||
ctx.setVariable("font", font_style+ " " + font_variant + " " + bold + " " + size + " " + family);
|
||||
ctx.setVariable("textAlign", (align) ? "right" : "left");
|
||||
if (text_decoration !== "none"){
|
||||
return fontMetrics(family, size);
|
||||
}
|
||||
}
|
||||
|
||||
function renderTextDecoration(text_decoration, bounds, metrics, color) {
|
||||
switch(text_decoration) {
|
||||
case "underline":
|
||||
// Draws a line at the baseline of the font
|
||||
// TODO As some browsers display the line as more than 1px if the font-size is big, need to take that into account both in position and size
|
||||
renderRect(ctx, bounds.left, Math.round(bounds.top + metrics.baseline + metrics.lineWidth), bounds.width, 1, color);
|
||||
break;
|
||||
case "overline":
|
||||
renderRect(ctx, bounds.left, bounds.top, bounds.width, 1, color);
|
||||
break;
|
||||
case "line-through":
|
||||
// TODO try and find exact position for line-through
|
||||
renderRect(ctx, bounds.left, Math.ceil(bounds.top + metrics.middle + metrics.lineWidth), bounds.width, 1, color);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function renderText(el, textNode, stack) {
|
||||
var ctx = stack.ctx,
|
||||
color = getCSS(el, "color"),
|
||||
text_decoration = getCSS(el, "textDecoration"),
|
||||
text_align = getCSS(el, "textAlign"),
|
||||
letter_spacing = getCSS(el, "letterSpacing"),
|
||||
bounds,
|
||||
text,
|
||||
metrics,
|
||||
renderList,
|
||||
listLen,
|
||||
newTextNode,
|
||||
textValue,
|
||||
textOffset = 0,
|
||||
oldTextNode,
|
||||
c,
|
||||
range,
|
||||
parent,
|
||||
wrapElement,
|
||||
backupText;
|
||||
|
||||
textNode.nodeValue = textTransform(textNode.nodeValue, getCSS(el, "textTransform"));
|
||||
text = trimText(textNode.nodeValue);
|
||||
|
||||
if (text.length > 0){
|
||||
|
||||
text_align = text_align.replace(["-webkit-auto"],["auto"]);
|
||||
|
||||
renderList = (!options.letterRendering && /^(left|right|justify|auto)$/.test(text_align) && noLetterSpacing(letter_spacing)) ? textNode.nodeValue.split(/(\b| )/) : textNode.nodeValue.split("");
|
||||
|
||||
metrics = setTextVariables(ctx, el, text_decoration, color);
|
||||
oldTextNode = textNode;
|
||||
|
||||
for ( c=0, listLen = renderList.length; c < listLen; c+=1 ) {
|
||||
@ -311,24 +331,9 @@ _html2canvas.Parse = function ( images, options ) {
|
||||
|
||||
}
|
||||
|
||||
if (textValue !== null){
|
||||
drawText(textValue, bounds.left, bounds.bottom, ctx);
|
||||
}
|
||||
|
||||
switch(text_decoration) {
|
||||
case "underline":
|
||||
// Draws a line at the baseline of the font
|
||||
// TODO As some browsers display the line as more than 1px if the font-size is big, need to take that into account both in position and size
|
||||
renderRect(ctx, bounds.left, Math.round(bounds.top + metrics.baseline + metrics.lineWidth), bounds.width, 1, color);
|
||||
break;
|
||||
case "overline":
|
||||
renderRect(ctx, bounds.left, bounds.top, bounds.width, 1, color);
|
||||
break;
|
||||
case "line-through":
|
||||
// TODO try and find exact position for line-through
|
||||
renderRect(ctx, bounds.left, Math.ceil(bounds.top + metrics.middle + metrics.lineWidth), bounds.width, 1, color);
|
||||
break;
|
||||
}
|
||||
drawText(textValue, bounds.left, bounds.bottom, ctx);
|
||||
renderTextDecoration(text_decoration, bounds, metrics, color);
|
||||
|
||||
textOffset += renderList[c].length;
|
||||
|
||||
@ -358,8 +363,6 @@ _html2canvas.Parse = function ( images, options ) {
|
||||
return bounds;
|
||||
}
|
||||
|
||||
|
||||
|
||||
function elementIndex( el ) {
|
||||
var i = -1,
|
||||
count = 1,
|
||||
|
Loading…
Reference in New Issue
Block a user