Use spaces to compare font size to detect when the @font-face rule works, this seems to be more accurate

This commit is contained in:
Vivien Nicolas 2011-06-16 01:30:47 +02:00
parent 97e8a56323
commit 509d608a3a

View File

@ -131,12 +131,20 @@ Font.prototype = {
// ready // ready
var debug = false; var debug = false;
if (debug) {
var name = document.createElement("font");
name.setAttribute("style", "position: absolute; left: 20px; top: " +
(100 * fontCount + 60) + "px");
name.innerHTML = fontName;
document.body.appendChild(name);
}
var canvas = document.createElement("canvas"); var canvas = document.createElement("canvas");
var style = "border: 1px solid black; position:absolute; top: " + var style = "border: 1px solid black; position:absolute; top: " +
(debug ? (80 * fontCount) : "-200") + "px; left: 100px;"; (debug ? (100 * fontCount) : "-200") + "px; left: 2px; width: 340px; height: 100px";
canvas.setAttribute("style", style); canvas.setAttribute("style", style);
canvas.setAttribute("width", 100); canvas.setAttribute("width", 340);
canvas.setAttribute("heigth", 70); canvas.setAttribute("heigth", 100);
document.body.appendChild(canvas); document.body.appendChild(canvas);
// Retrieve font charset // Retrieve font charset
@ -146,16 +154,21 @@ Font.prototype = {
while (count-- && charset.length <= 30) while (count-- && charset.length <= 30)
charset = charset.concat(charset.slice()); charset = charset.concat(charset.slice());
// Get the font size canvas think it will be // Get the font size canvas think it will be for 'spaces'
var ctx = canvas.getContext("2d"); var ctx = canvas.getContext("2d");
var testString = ""; var testString = " ";
for (var i = 0; i < charset.length; i++) {
var unicode = new Number("0x" + GlyphsUnicode[charset[i]]); // When debugging use the characters provided by the charsets to visually
if (!unicode) // see what's happening
error("Unicode for " + charset[i] + " is has not been found in the glyphs list"); if (debug) {
testString += String.fromCharCode(unicode); for (var i = 0; i < charset.length; i++) {
var unicode = new Number("0x" + GlyphsUnicode[charset[i]]);
if (!unicode)
error("Unicode for " + charset[i] + " is has not been found in the glyphs list");
testString += String.fromCharCode(unicode);
}
} }
ctx.font = "20px " + fontName + ", Symbol"; ctx.font = "bold italic 20px " + fontName + ", Symbol, Arial";
var textWidth = ctx.mozMeasureText(testString); var textWidth = ctx.mozMeasureText(testString);
if (debug) if (debug)
@ -163,7 +176,7 @@ Font.prototype = {
var start = Date.now(); var start = Date.now();
var interval = window.setInterval(function(self) { var interval = window.setInterval(function(self) {
ctx.font = "20px " + fontName + ", Symbol"; ctx.font = "bold italic 20px " + fontName + ", Symbol, Arial";
// For some reasons the font has not loaded, so mark it loaded for the // For some reasons the font has not loaded, so mark it loaded for the
// page to proceed but cry // page to proceed but cry