mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
65 lines
1.7 KiB
JavaScript
65 lines
1.7 KiB
JavaScript
_html2canvas.Util.Font = (function () {
|
|
|
|
var fontData = {};
|
|
|
|
return function(font, fontSize, doc) {
|
|
if (fontData[font + "-" + fontSize] !== undefined) {
|
|
return fontData[font + "-" + fontSize];
|
|
}
|
|
|
|
var container = doc.createElement('div'),
|
|
img = doc.createElement('img'),
|
|
span = doc.createElement('span'),
|
|
sampleText = 'Hidden Text',
|
|
baseline,
|
|
middle,
|
|
metricsObj;
|
|
|
|
container.style.visibility = "hidden";
|
|
container.style.fontFamily = font;
|
|
container.style.fontSize = fontSize;
|
|
container.style.margin = 0;
|
|
container.style.padding = 0;
|
|
|
|
doc.body.appendChild(container);
|
|
|
|
// http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever (handtinywhite.gif)
|
|
img.src = "data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=";
|
|
img.width = 1;
|
|
img.height = 1;
|
|
|
|
img.style.margin = 0;
|
|
img.style.padding = 0;
|
|
img.style.verticalAlign = "baseline";
|
|
|
|
span.style.fontFamily = font;
|
|
span.style.fontSize = fontSize;
|
|
span.style.margin = 0;
|
|
span.style.padding = 0;
|
|
|
|
span.appendChild(doc.createTextNode(sampleText));
|
|
container.appendChild(span);
|
|
container.appendChild(img);
|
|
baseline = (img.offsetTop - span.offsetTop) + 1;
|
|
|
|
container.removeChild(span);
|
|
container.appendChild(doc.createTextNode(sampleText));
|
|
|
|
container.style.lineHeight = "normal";
|
|
img.style.verticalAlign = "super";
|
|
|
|
middle = (img.offsetTop-container.offsetTop) + 1;
|
|
metricsObj = {
|
|
baseline: baseline,
|
|
lineWidth: 1,
|
|
middle: middle
|
|
};
|
|
|
|
fontData[font + "-" + fontSize] = metricsObj;
|
|
|
|
doc.body.removeChild(container);
|
|
|
|
return metricsObj;
|
|
};
|
|
})();
|