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
		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();
		/* 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
    Responsive and Infinitely Scalable JS Animations

    Back in late 2012 it was not easy to find open source projects using requestAnimationFrame() - this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...

  • By
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

Incredible Demos

Discussion

  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 http://kilianvalkhof.com/2009/css-xhtml/combining-cufon-and-font-face/

  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.

    Cheers
    Jon

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