Skip to content

Instantly share code, notes, and snippets.

@jblanche
Created July 3, 2009 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jblanche/140171 to your computer and use it in GitHub Desktop.
Save jblanche/140171 to your computer and use it in GitHub Desktop.
/* fontAvailable Prototype Plugin, v1.0
*
* Copyright (c) 2009, Jonathan Blanchet
* Licensed under the MIT License
* http://github.com/jblanche/prototype-fontavailable/tree/master
*
* Based on jQuery fontAvailable plugin:
* http://code.google.com/p/jquery-fontavailable/
* and Edward H. Trager and gCSSCapabilities plugin :
* http://eyegene.ophthy.med.umich.edu/fontface/javascript/gCSSCapabilities.js
*/
(function($) {
var element;
$.fontAvailable = function(fontName) {
var width, height;
// prepare element, and append to DOM
if(!element) {
element = $( document.createElement( 'span' ))
.css( 'visibility', 'hidden' )
.css( 'position', 'absolute' )
.css( 'top', '-10000px' )
.css( 'left', '-10000px' )
.html( 'abcdefghijklmnopqrstuvwxyz' )
.appendTo( document.body );
}
// get the width/height of element after applying a fake font
width = element
.css('font-family', '__FAKEFONT__')
.width();
height = element.height();
// set test font
element.css('font-family', fontName);
return width !== element.width() || height !== element.height();
}
$.fontFaceAvailable = function(){
var n=document.styleSheets.length;
if(n){
for(var i=0;i<n;i++){
if( _checkStyleSheetforFontFaceRule( document.styleSheets[i] )){
self.supportsWebFonts=true;
break;
}
}
}
$.checkForFontFaceRule = function (stylesheet) {
var n=stylesheet.cssRules.length;
if(!n) return false;
for(var i=0;i<n;i++){
if(stylesheet.cssRules[i].type===self.FONT_FACE_RULE){
var text = stylesheet.cssRules[i].cssText;
text = text.replace(/^\s*@font-face\s*{/,"");
text = text.replace(/}/,"");
alert(text);
var vals = text.split(";");
for(var j=0;j<vals.length;j++){
var pair = vals[j].split(":");
if(pair[0].match(/font-family/)){
pair[1] = pair[1].replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
if(pair[1].length){
return true;
}
}
}
}
}
return false;
}
$.isCufonNeeded = function(fontName) {
!fontAvailable(fontName) && !fontFaceAvailable() ;
}
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment