Skip to content

Commit

Permalink
much more robust @font-face testing
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Feb 13, 2010
1 parent 2034abe commit 06ccec1
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions modernizr.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ window.Modernizr = (function(window,doc,undefined){
return ('WebSocket' in window);
};


// http://css-tricks.com/rgba-browser-support/
tests[rgba] = function() {
// Set an rgba() color and check the returned value

Expand Down Expand Up @@ -471,14 +473,16 @@ window.Modernizr = (function(window,doc,undefined){
// Create variables for dedicated @font-face test
var st = doc.createElement('style'),
spn = doc.createElement('span'),
wid, nwid, isFakeBody = false, body = doc.body,
size, isFakeBody = false, body = doc.body,
callback, isCallbackCalled;

// The following is a font-face + glyph definition for the . character:
st.textContent = "@font-face{font-family:testfont;src:url('data:font/ttf;base64,AAEAAAAMAIAAAwBAT1MvMliohmwAAADMAAAAVmNtYXCp5qrBAAABJAAAANhjdnQgACICiAAAAfwAAAAEZ2FzcP//AAMAAAIAAAAACGdseWYv5OZoAAACCAAAANxoZWFk69bnvwAAAuQAAAA2aGhlYQUJAt8AAAMcAAAAJGhtdHgGDgC4AAADQAAAABRsb2NhAIQAwgAAA1QAAAAMbWF4cABVANgAAANgAAAAIG5hbWUgXduAAAADgAAABPVwb3N03NkzmgAACHgAAAA4AAECBAEsAAUAAAKZAswAAACPApkCzAAAAesAMwEJAAACAAMDAAAAAAAAgAACbwAAAAoAAAAAAAAAAFBmRWQAAAAgqS8DM/8zAFwDMwDNAAAABQAAAAAAAAAAAAMAAAADAAAAHAABAAAAAABGAAMAAQAAAK4ABAAqAAAABgAEAAEAAgAuqQD//wAAAC6pAP///9ZXAwAAAAAAAAACAAAABgBoAAAAAAAvAAEAAAAAAAAAAAAAAAAAAAABAAIAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AHQAIABvAGYAIABKAHUAcgBhAEwAaQBnAGgAdABoAHQAdABwADoALwAvAHMAYwByAGkAcAB0AHMALgBzAGkAbAAuAG8AcgBnAC8ATwBGAEwAAAAAAgAAAAAAAP+BADMAAAAAAAAAAAAAAAAAAAAAAAAAAAAFAAAAAQACAQIAEQt6ZXJva2F5YWhsaQ==')}";
doc.getElementsByTagName('head')[0].appendChild(st);


// we don't use `serif` and we don't use `monospace`
// http://github.com/Modernizr/Modernizr/issues/closed#issue/39
// http://neugierig.org/software/chromium/notes/2009/09/monospace-fonts-workaround.html
spn.setAttribute('style','font:99px _,arial,helvetica;position:absolute;visibility:hidden');

if (!body){
Expand All @@ -491,14 +495,15 @@ window.Modernizr = (function(window,doc,undefined){
spn.id = 'fonttest';

body.appendChild(spn);
wid = spn.offsetWidth;
spn.style.font = '99px testfont,_,serif';
size = spn.offsetWidth*spn.offsetHeight;
spn.style.font = '99px testfont,_,arial,helvetica';

// needed for the CSSFontFaceRule false positives (ff3, chrome, op9)
fontret = wid !== spn.offsetWidth;
fontret = size !== spn.offsetWidth*spn.offsetHeight;

var delayedCheck = function(){
fontret = ret[fontface] = wid !== spn.offsetWidth;
function delayedCheck(){
if (!body.parentNode) return;
fontret = ret[fontface] = size !== spn.offsetWidth*spn.offsetHeight;
docElement.className = docElement.className.replace(/(no-)?font.*?\b/,'') + (fontret ? ' ' : ' no-') + fontface;
}

Expand All @@ -508,7 +513,8 @@ window.Modernizr = (function(window,doc,undefined){
delayedCheck();
callback && (isCallbackCalled = true) && callback(fontret);
setTimeout(function(){
isFakeBody && body.parentNode.removeChild(body);
if (!isFakeBody) body = spn;
body.parentNode.removeChild(body);
st.parentNode.removeChild(st);
}, 50);
},false);
Expand All @@ -519,7 +525,7 @@ window.Modernizr = (function(window,doc,undefined){
(isCallbackCalled || fontret) ? fn(fontret) : (callback = fn);
}

return function(){ return fontret || wid !== spn.offsetWidth; };
return function(){ return fontret || size !== spn.offsetWidth; };

})();

Expand Down Expand Up @@ -595,7 +601,7 @@ window.Modernizr = (function(window,doc,undefined){
// technique courtesy of Jonathan Neal
tests[flash] = function(){
try {
return !!navigator.plugins['Shockwave Flash'] || !!(new ActiveXObject('ShockwaveFlash.ShockwaveFlash'));
return !!navigator.plugins['Shockwave Flash'] || !!(new ActiveXObject('ShockwaveFlash.ShockwaveFlash'));
}
catch(e) {
return false;
Expand Down

0 comments on commit 06ccec1

Please sign in to comment.