Tuesday, July 7, 2009

Embedding your font with @font-face

Last night I found out about @font-face, which allows web authors to embed a font into a web page. The only problem here is the browser comparability.
After much research overnight (and some freak outs) I found this jQuery plugin called fontAvailable. It basically creates a dummy span element with a fake font, and then tests it against the input front from the parameter; it checks the width and height for changes.
So after embedding the font with @font-face, the font should act as though the font is installed on the client’s computer.
The tools that I have used to achieve a lightweight embedded fonts are Cufon, jQuery, fontAvailable, and @font-face.

The HTML
<link rel="stylesheet" type="text/css" href="css/style.css" >
<script type="text/javascript" src="js/lib/jquery/jquery-1.3.2.js"> </script>
<script type="text/javascript" src="js/lib/jquery/plugins/jquery.fontavailable-1.1.js"> </script>

Note: The Cufon library is dynamically loaded with jQuery

The CSS
@font-face {
font-family: Kartika;
src: url(kartika.ttf) format('truetype');
}


The JavaScript
if (!$.fontAvailable('Kartika')) {
// load the cufon library
$.getScript('js/lib/cufon/cufon.js', function() {
// load the font
$.getScript('js/lib/cufon/Kartika_400.font.js', function() {
Cufon.replace('#nav ul li a');
});
});
}

It’s important to have the font file loaded in the callback after loading the Cufon library, and same thing goes for applying the fonts.

Edit:
So it turns out that dynamically loading the javascripts doesn’t work in Internet Explorer (haven't tested it in others) so it’s best to load the Cufon library and it’s font files with the script tags.

10 comments:

Juan Ramón said...

Hello, I am testing cufon loading dinamically scripts with nested calls,as you. But I have a problem with explorer. I see normal text!!

Thank you!!

SamerZiadeh said...

Hey, at the time of writing I was still experimenting with this technique. I am still using that technique on the site I'm building, and I'm getting the same problem that you've mentioned.

I'm going to read over the Cufon documentation to see what's going on.

I know that @font-face, although it's been supported by Internet Explorer, IE only likes specific font format to be embedded which is EOT.

I will update the post as soon as I figure out a solution :/

Juan Ramón said...

ok!! But I need use only one technique for all browsers!
Thank you for your response!!

SamerZiadeh said...

The Cufon library should be working by itself.
I'm also waiting on Paul Irish to publish his method, he's working on it.
I posted this as I was building the website because I needed an immediate solution for now.
I believe @font-face in IE only works with embeddable font types (eot) so it's pretty limited on that end.
But Cufon should do the trick. I will look over my code and hacks and see what can be done to fix it, and update the post.
Post a comment if you find a better solution :)

Juan Ramón said...

I have comproved if you load cufon library in the head of document(script /script), and load the fonts with $.getScript() in Explorer work good.

SamerZiadeh said...

Try loading your Cufon library in your HTML document instead of dynamically loading it with jQuery.
I haven't gotten around yet to further test it yet. I got held up with adding another feature to the site, and procrastination :/

SamerZiadeh said...

I updated the post, see the edited part.
Let me know if anyone finds a better solution :)

Juan Ramón said...

Is good! But it would be better to find why dont work in IE, and is more flexible for web 2.0 to call fonts only when you need them!!

SamerZiadeh said...

Yeah that's for sure, I hate the fact that I have to load them in the document.

I haven't had time to test that, but I think the script can be dynamically loaded with JavaScript in everything except for IE. So you can add the script to load in the document if it's IE and then load dynamically if @font-face doesn't work.

I hope I get back on track soon so I can test out these theories. If you have time to test out let me know what turns out

Juan Ramón said...

I have wrote a comment in the cufon google groups account!

This is the link if you need more!

http://groups.google.es/group/cufon/browse_thread/thread/e5f55fd975093a36