| 1 |
// http://stackoverflow.com/questions/4383226/using-jquery-to-know-when-font-face-fonts-are-loaded |
| 2 |
(function(){ |
| 3 |
function waitForWebfonts(fonts, callback) { |
| 4 |
var loadedFonts = 0; |
| 5 |
for(var i = 0, l = fonts.length; i < l; ++i) { |
| 6 |
(function(font) { |
| 7 |
var node = document.createElement('span'); |
| 8 |
// Characters that vary significantly among different fonts |
| 9 |
node.innerHTML = 'giItT1WQy@!-/#'; |
| 10 |
// Visible - so we can measure it - but not on the screen |
| 11 |
node.style.position = 'absolute'; |
| 12 |
node.style.left = '-10000px'; |
| 13 |
node.style.top = '-10000px'; |
| 14 |
// Large font size makes even subtle changes obvious |
| 15 |
node.style.fontSize = '300px'; |
| 16 |
// Reset any font properties |
| 17 |
node.style.fontFamily = 'sans-serif'; |
| 18 |
node.style.fontVariant = 'normal'; |
| 19 |
node.style.fontStyle = 'normal'; |
| 20 |
node.style.fontWeight = 'normal'; |
| 21 |
node.style.letterSpacing = '0'; |
| 22 |
document.body.appendChild(node); |
| 23 |
|
| 24 |
// Remember width with no applied web font |
| 25 |
var width = node.offsetWidth; |
| 26 |
|
| 27 |
node.style.fontFamily = font; |
| 28 |
|
| 29 |
var interval; |
| 30 |
function checkFont() { |
| 31 |
// Compare current width with original width |
| 32 |
if(node && node.offsetWidth != width) { |
| 33 |
++loadedFonts; |
| 34 |
node.parentNode.removeChild(node); |
| 35 |
node = null; |
| 36 |
} |
| 37 |
|
| 38 |
// If all fonts have been loaded |
| 39 |
if(loadedFonts >= fonts.length) { |
| 40 |
if(interval) { |
| 41 |
clearInterval(interval); |
| 42 |
} |
| 43 |
if(loadedFonts == fonts.length) { |
| 44 |
callback(); |
| 45 |
return true; |
| 46 |
} |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
if(!checkFont()) { |
| 51 |
interval = setInterval(checkFont, 50); |
| 52 |
} |
| 53 |
})(fonts[i]); |
| 54 |
} |
| 55 |
} |
| 56 |
window.waitForWebfonts = waitForWebfonts; |
| 57 |
})(); |
| 58 |
|
| 59 |
|