Font detection with Javascript and Flash

June 26, 2008 — 4 Comments

View the demo now

I was playing around with jquery and I wanted to see if it was possible to generate a cheat sheet of all the fonts on your system. Its also a good run through of how to load a SWF with a callback.

View a demo

The steps I used to generate this are:

  • Load SWF
  • Javascript calls to Actionscript fonts() method via ExternalInterface
  • Get the list of system fonts in Flash via Font.enumerateFonts(true)
  • Curse Flash for giving you a list of font names, where half of them are garbage.
  • For each font name, in javascript, create a test which adds a DOM element temporarily with a fallback font family set. For example, font-family: 'Apple Garamond Pro', 'Times New Roman';, where Times New Roman is your fallback.
  • Then check the actual width (via offsetWidth). If it does not match the offsetWidth of the fallback font, then we know it rendered ok. (The original idea is from http://www.lalit.org/lab/javascript-css-font-detect)

The only caveat is, if the actual font width and the fallback font width do match, then you would have a false negative. So its not bulletproof.

The actionscript to enumerate the fonts and call back out is pretty simple:

public function fonts():Array {
  return Font.enumerateFonts(true).sortOn("fontName", Array.CASEINSENSITIVE);
}

Expose the method in the constructor using ExternalInterface:

ExternalInterface.addCallback("fonts", fonts);

FontList.as

I used swfobject to load the SWF. Also be sure to enable allowScriptAccess.

The javascript to test the fonts is at: font-detect.js

You can use it by including:

 

In the end, I don’t think this is useful in practice, but I thought I would share anyway. Its on github too.

Updated: Now uses call straight to actionscript, doesn’t marshall JSON first so its much faster, and other refactoring.

4 responses to Font detection with Javascript and Flash

  1. wow, that idea is amazing! works great for me, some of the font names are garbage (as you mentioned) but it works! very very nice…

  2. Henrik Kjelsberg July 26, 2008 at 10:31 am

    Great concept! This lifts flash typography to another level. Thank you for the tip!

  3. Is your swf available to the public? I’d like to use it on my site to allow custom fonts.

Trackbacks and Pingbacks:

  1. JS获取客户端系统字体 - September 4, 2011

    [...] LinkedIn [...]

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s