-
Notifications
You must be signed in to change notification settings - Fork 6
/
isFontLoaded.js
66 lines (47 loc) · 2.1 KB
/
isFontLoaded.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// by harold kyle, based off of paul irish's work.
//call the function to Firebug Console
//isThisFontLoaded("Greta Text","Times New Roman",function(bool){console.debug('here');})
var isThisFontLoaded = (function(font,fallback,fn){
var fontret,
fontfaceCheckDelay = 5000;
// IE supports EOT and has had EOT support since IE 5.
// This is a proprietary standard (ATOW) and thus this off-spec,
// proprietary test for it is acceptable.
if (!(!/*@cc_on@if(@_jscript_version>=5)!@end@*/0)) fontret = true;
else {
// Create variables for dedicated @font-face test
var doc = document, docElement = doc.documentElement,
st = doc.createElement('style'),
spn = doc.createElement('span'),
wid, nwid, body = doc.body,
callback, isCallbackCalled;
// The following is a font, only containing the - character. Thanks Ethan Dunham.
doc.getElementsByTagName('head')[0].appendChild(st);
spn.setAttribute('style','font:99px _,'+fallback+';position:absolute;visibility:hidden');
if (!body){
body = docElement.appendChild(doc.createElement('fontface'));
}
// the data-uri'd font only has the - character
spn.innerHTML = '-------';
spn.id = 'fonttest';
body.appendChild(spn);
wid = spn.offsetWidth*spn.offsetHeight;
spn.style.font = '99px '+font+',_,'+fallback;
// needed for the CSSFontFaceRule false positives (ff3, chrome, op9)
fontret = wid !== spn.offsetWidth*spn.offsetHeight;
var delayedCheck = function(){
if (isCallbackCalled) return;
fontret = wid !== spn.offsetWidth*spn.offsetHeight;
callback && (isCallbackCalled = true) && callback(fontret);
}
addEventListener('load',delayedCheck,false);
setTimeout(delayedCheck,fontfaceCheckDelay);
}
function ret(){ return fontret || wid !== spn.offsetWidth*spn.offsetHeight; };
// allow for a callback
if(ret()&&(typeof fn === 'function')){
(isCallbackCalled || fontret) ? fn(fontret) : (callback = fn);
}
return ret();
});
/* end font_face */