MooTools FontChecker Plugin

By  on  

There's a very interesting piece of code on Google Code called FontAvailable which does a jQuery-based JavaScript check on a string to check whether or not your system has a specific font based upon its output width. I've ported this functionality to MooTools.

The MooTools JavaScript

var FontChecker = new Class({
	/* implements */
	Implements: [Options],
	/* options */
	options: {
		fakeFont: '__RUBBUSH_FONT__',
		testString: 'abcdefghijklmnopqrstuvwxyz'
	/* initialization */
	initialize: function(options) {
		//set 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
		/* 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 */
		/* compare widths to see check if font is available */
		return (width !== new_width);

My code follows the same logic as FontAvailable:

  • inject a hidden inline element with some garbage text and a fake font
  • record the calculated width of the element
  • set the element's font to the desired font
  • compare the widths and return whether they are the same

The Usage

/* when the dom is ready */
window.addEvent('domready',function() {
	var fc = new FontChecker();
	/* use it...or lose it? */
	$('start').addEvent('click',function() {
		$$('#font-list li').each(function(el) {
			el.setStyle('color', fc.check(el.get('text')) ? 'green' : 'red');

You may check as many fonts as you'd like using one instance by using the check() method.

The accuracy of this type of check can never be 100% because two fonts could theoretically have the same calculated width. This methods does, however, provide a fairly accurate result.

Recent Features

  • By
    CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

Incredible Demos

  • By
    Select Dropdowns, MooTools, and CSS Print

    I know I've harped on this over and over again but it's important to enhance pages for print. You can do some things using simple CSS but today's post features MooTools and jQuery. We'll be taking the options of a SELECT element and generating...

  • By
    MooTools Image Preloading with Progress Bar

    The idea of image preloading has been around since the dawn of the internet. When we didn't have all the fancy stuff we use now, we were forced to use ugly mouseover images to show dynamism. I don't think you were declared an official...


  1. Wow, I don’t have Arial? :P And ya, line 4 and 5 should be swapped.

  2. Interesting ! This could help, thanks.

  3. @Lim Chee Aun: Good call on the 4/5 swap.

  4. Nice one, though I can’t think of any practical uses right now…

    Still, fun stuff.

  5. @Koen De Groote
    I actually disagree. This plugin is super useful when you want to allow alternative font selections for your website, lets say you want the primary font to be A, but if the font is now allowed use B or C etc… with a simple manipulation of the code, you can get it done by loading different stylesheets that call the font family

    I wonder if that made any sense, I’m too tired to type :)

  6. @digit you dont need javascript to do that

    font-family: arial, “lucida console”, sans-serif

  7. @Jesus: You’re right, but this is all I could think of heh

  8. Very cool. Thanks for posting this.

  9. Alan

    Internet Explorer and Opera showed different results than FF & Chrome. Any ideas as to why?

  10. @Alan
    This script does not give 100% true results.

    It is just tries to see if the default font gives a text with different width than the new font you want to see the user haves on his computer.

    If both font have same width with the testString, wich is not that hard, the script will give false results.

    I don´t see any other way to test this dou…. In fact this is new for me.

    Congratulations DW and the guy who created it for the first time.

  11. Meh, I must agree with Jesus DeLaTorre on this one: while this is a cool trick, I don’t really see any use for it, since font selection is already handled by one line CSS. Unless you want to display a message that say “this site would look cooler if you had the font …” then what?

  12. @olivier: This is more proof of concept than anything else. For each computer I tested this on, it returned the correct result.

  13. Monkey

    No use? Why are you all talking about no use?

    You can make excellent use of it! You use this to check if you want need a font-replacer like Cufon at all, by first checking if the browser is able to render the font-itself (either since it was pre-installed, or maybe since css @font-face works in this browser).

    Refer to the excellent article

  14. Ricardo

    The real world use for this would be as follows:

    Detect if client supports font A (heavy, condensed).
    If not, detect if client supports font B (condensed).
    If true, adjust font weight to heavy.

    AFAIK, you cannot cascade weight, size etc, only font family.

  15. Jon

    Hi David,

    I’m trying to find something that would allow users of a website to type some characters into a field and then choose from a list of available fonts and see what their text looks like in that font.

    Is there anyway your code could be modified to help do this? If not any idea what I could use for this?

    I’m not a developer but a graphic designer who puts together websites using Joomla. I can usually figure things out if pointed in the right direction so any help would be much appreciated.


Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!