Skip to content

Instantly share code, notes, and snippets.

@lsmith
Created May 26, 2009 16:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsmith/118155 to your computer and use it in GitHub Desktop.
Save lsmith/118155 to your computer and use it in GitHub Desktop.
function testFont(name) {
name = name.replace(/['"<>]/g,'');
var body = document.body,
test = document.createElement('div'),
installed = false,
template =
'<b style="display:inline !important; width:auto !important; font:normal 10px/1 \'X\',sans-serif !important">ii</b>'+
'<b style="display:inline !important; width:auto !important; font:normal 10px/1 \'X\',monospace !important">ii</b>',
ab;
if (name) {
test.innerHTML = template.replace(/X/g, name);
test.style.cssText = 'position: absolute; visibility: hidden; display: block !important';
body.insertBefore(test, body.firstChild);
ab = test.getElementsByTagName('b');
installed = ab[0].offsetWidth === ab[1].offsetWidth;
body.removeChild(test);
}
return installed;
}
@davidcalhoun
Copy link

Strange, I seem to be getting off by 1px errors testing for HelveticaNeue-CondensedBold on iOS 5, but it works fine on Chrome on OSX.

I increased the font size to 1000px and the sizes are still off by 1px. I changed the character from ii to ww and then the test works fine. Just a heads up, not sure what's going on.

@lsmith
Copy link
Author

lsmith commented Mar 25, 2012

Meaning the font is installed, but is reporting that it isn't because the two <b>s are reporting different dims?

@davidcalhoun
Copy link

That's right, that's what seems to be happening, just on iOS 5.

EDIT: I'll see if I can make a reduced test case. It might possibly be something I'm doing in my CSS that messes it up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment