FontChecker
FontChecker is a highly inventive JavaScript-based font availability checker.
Download Debut Article Example Usage
Plugin Code (Version 1.0)
var FontChecker = new Class({
/* implements */
Implements: [Options],
/* options */
options: {
fakeFont: '__RUBBUSH_FONT__',
testString: 'abcdefghijklmnopqrstuvwxyz'
},
/* initialization */
initialize: function(options) {
//set options
this.setOptions(options);
},
/* a method that does whatever you want */
check: function(desiredFont) {
/* create a hidden element */
var element = new Element('span',{
styles: {
visibility: 'hidden',
'font-family': this.options.fakeFont
},
html: this.options.testString
}).inject(document.body);
/* apply a fake font, get width */
var width = element.getCoordinates().width;
/* apply desired font */
element.setStyle('font-family', desiredFont);
var new_width = element.getCoordinates().width;
/* take our temp element out of the DOM */
element.dispose();
/* same width = most likely has font */
return (width !== new_width);
}
});
/* usage */
window.addEvent('domready',function() {
var fc = new FontChecker();
var hasFont = fc.check('Arial');
});
Options
- fakeFont: (defaults to '__RUBBUSH_FONT__') the fake font to use as a base/default.
- testString: (defaults to "abcdefghijklmnopqrstuvwxyz") the string to base the test on.
Code Revisions & Bug Fixes
None.
