The Easiest Way to Use Any Font You Wish
videos

The Easiest Way to Use Any Font You Wish

CSS 3 is on the horizon, and we’re all getting excited. Thanks to the latest browser updates, developers can begin working with time-saving new properties – such as @font-face. Unfortunately, the availability of these features is limited to a tiny fraction of our overall userbase. At least for the next year or so, we’ll need to continue utilizing the Flash and Javascript alternatives when embedding fonts.

Luckily, a new contender, Cufón, has made the process unbelievably simple. What makes it different? Rather than Flash, it uses a mixture of canvas and VML to render the fonts. In just a few minutes, I’ll demonstrate how to use any font you wish in your web applications. Excited?

Pros

  • Lightning fast!
  • 100 times more simple than siFR.
  • Up and running in a few minutes.
  • Not dependent upon a server-side language, like FLIR is.

Cons

  • It’s Javascript dependent. If disabled, the default fonts will be used.
  • The text isn’t selectable – never a good thing.
  • You can’t apply a hover state to converted elements.

Step 1: Download Cufón

Cufon homepage

Visit Cufón’s website and right-click on the “Download” button at the top. Choose “Save-As” and place it on your desktop.

Step 2: Convert a Font

Choose A Font

In order to function, we need to use the font converter utility on the website. Alternatively, you may download the source code and convert your fonts locally. For the purposes of demonstration, I’ve chosen to use an obnoxious font: “Jokerman”. Note – Windows users: you may have to copy the font from your “FONT” folder to the desktop for this to work.

If desired, also upload the italic and bold files as well.

Jokerman

Step 2b

Next, you’ll need to choose which glyphs should be included. Don’t be so quick to simply “CHOOSE ALL”. Doing so will cause the JS file size to increase dramatically. For example, we probably don’t need all of the Latin glyphs; so make sure they are left unchecked. In my case, I’ve checked the ones you see below.

Glyphs

Step 2c

Cufón allows you to designate a specific url for your file, to increase security. It’s extremely important that you ensure that you have the proper privileges to use a font. REFER HERE to review the terms. If advantageous, type in your site’s url into this box.

Since we’re just getting started, you can leave the final two sections at their default values. Accept the terms, and click “Let’s Do This”. You’ll then be presented with a download box asking you where to save the generated script. Once again, save it to your desktop for easy retrieval.

Download Script

Step 3

Folder Preview

The next step is to prepare our project. Create a new folder on your desktop, add an index.html file, and drag your two Javascript files in.

Open the index file in your favorite code editor, add the basic HTML tags, and then reference your two Javascript files just before the closing body tag (you’re free to add them to the head section as well).

<script type="text/javascript" src="js/cufon-yui.js"></script>
<script type="text/javascript" src="js/Jokerman_400.font.js"></script>

Calling the Script

Now, we need to decide what text should be replaced. Since our document is still blank, feel free to litter it with random tags and text. Let’s try to replace the default font in all the H1 tags with Jokerman.

<script type="text/javascript">
	Cufon.replace('h1');
</script>

When we call the “replace” method, we may append a string containing the tag name that we wish to replace – in our case, all H1 tags. Save the file, and view it in your browser.

replaced

Step 3b

As always, IE needs a bit more to play nicely with the others. If you view this page in IE, you’ll notice a slight flickr/delay before the font is rendered. To remedy, simply append:

<script type="text/javascript"> Cufon.now(); </script>

Step 4

Let’s imagine that you want to have more control over your selector. For instance, perhaps you don’t want to change ALL the H1 tags, but merely the ones within the header of your document. Cufón doesn’t have its own selector engine built in. This feature was omitted to keep the file size as small as possible. Though this might seem like a downfall at first, it’s actually a great idea. Considering the ubiquity of Javascript frameworks lately, there is no need to double-up. We’ll review two methods to target specific elements.

Method 1: Javascript

If you won’t be using a JS framework in your project, we’ll simply use:

Cufon.replace(document.getElementById('header').getElementsByTagName('h1'));

The code above states, “Get the element which has an id of “header”. Then, find all of the H1 tags within this element, and “replace” them with our new font.

Method 2: jQuery

To piggyback off of jQuery’s selector engine, we only need to import jQuery before Cufón.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="js/cufon-yui.js"></script>
<script type="text/javascript" src="js/Jokerman_400.font.js"></script>
Cufon.replace('#header h1');

It’s as simple as that! Please note that you MUST import jQuery BEFORE your Cufón script in order for this method to work.

Complete

Believe it or not, you’re finished! With just a few lines of simple code, you’re free to use any font you wish! Just make sure you have permission and are compliant with type foundries’ licensing.

The main concern from the perspective of the type foundry appears to be that the typeface script generated by Cufón could be used to reverse engineer the very typeface itself.
-Cameron Moll

What are your thoughts? Have a better method that I’m not familiar with?

  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.


Tags: Videos
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • nalu

    thank you! it’s easy to follow the tutorial plus with image.. nice!

  • http://www.gotlockerz.com Jose D

    is it possible to add Cufón to a CSS or PHP file instead of HTML ? if so, how? thanks for the tutorial!! +

    • Mark

      You gotta be kidding….

      • gus

        so… it doesnt work on php files??? why?

      • http://www.graphicdesignme.com Michelle

        No need to be snarky. People come to this site to learn.
        Yes you can add it to a PHP file.

  • Stefan

    Thanks for the tutorial. It is the best I found on the web!

  • Pingback: Kako postaviti bilo koji font na web sajt? Rešenje – Cufon! | Milan Stojanov homepage!

  • Pingback: The Official jQuery Podcast

  • http://www.spincentral.org Brandon Elfring

    Thank you so much. Wonderful tutorial!

  • http://shipsandtripstravel.com Gregg

    Great Tutorial!
    I used Cufon on a travel agency site for dynamic text on a feature rotator and specials: http://shipsandtripstravel.com
    i also used multiple fonts on the same page for the testimonials page: http://shipsandtripstravel.com/about/testimonials.html

  • http://www.hyperlinksolution.com/ Kris Acharya

    Awesome!!
    Very useful resource and beautifully presented..
    I really appreciate your effort.

  • http://www.shojaru.com Stalin

    Thanks a lot man! You rocks! ;)

  • Pingback: Cufon Fonts | Frugal Theme | WordPress Theme Blog

  • http://www.vinodsobale.com vinod

    I have tried a lot & yet no luck.

    Here’s the code:

    cufon workshop

    Cufon.replace(‘strong’);

    Hello! Welcome to cufon workshop

    I don’t know what’s wrong. Could anyone please help?

  • http://allisonwashko.tumblr.com allison

    i’ve tried multiple times and my font will not change! \:

  • Pingback: 6 ways to use any fonts on your site! | Hot Scripts Blog

  • Pingback: 6 ways to use any fonts on your site! | Articulos Webmasters

  • Pingback: 6 Killer Text Replacement Tricks to Improve your Typography

  • Pingback: 尝试抄袭完成一个自用的主题-2 - ~囧囧~

  • http://www.teenpublish.com teenpublish

    Question,
    does Cufon CACHE your images somewhere? Or does it render a new every time the person’s page is loaded? So no caching right? (Flir caches server side, and since Cufon is client side, does it still cache anywhere on the server at all? I’m not familiar with Canvas and VML), please do let me know.. thanks!

  • http://www.teenpublish.com teenpublish

    If someone can answer the previous question I asked.. that would be dope.. because I just installed Cufon and now I’m a bit concerned about the server side caching aspect.. I dont want it to keep on serving new images if its going to suck up more bandwidth compared to say FLIR which keeps all the PHP generated images in a cache’d directory. I googled if Cufon is cached server side but I’m a tad bit confused on this issue, some helpful advice would suffice =)

    • William Richardson

      Cufon is rendered with Javascript on the html5 canvas element (or other similar vector element in IE), so no image file is ever created or used. Your server doesn’t have to lift a finger besides actually serving up the Cufon javascript files. The user’s browser does all the work.

  • http://www.tech-plazaa.com fahim

    hey anybody help me line hieght is not working in cufon font replacement
    any body know to change it

  • http://wikicomputacao.blogspot.com sergioviniciuss

    excellent tutorial! =)
    it’s a fast way to change the fonts in our websites =) nice one!

  • Tufan

    Free Cufon font library: http://www.cufonfonts.com/

  • OLga

    It does not work in IE :(

  • http://amtype.com mating

    thank you! it’s easy to follow the tutorial plus with image.. nice!

  • Pingback: Cufón resolve o problema da topografia para a web | Blog da Carambola Digital

  • http://ninna.myplace-online.info NiNNa

    10x, I will use it

  • Jessica

    Thank you SO much for this straight-forward tutorial! I’m a newbie to Expression Engine and web design in general, but I was able to follow along just fine. Excellent work!

  • http://www.immense.net Jason Cross

    Nice to see someone else rocking e-texteditor

  • http://www.buzzscript.com Buzz Script

    This was a useful tutorial. Thank you!

  • Pingback: A Web Designer's Guide to Font Replacement Methods | Webdesigntuts+

  • Pingback: A Web Designer’s Guide to Font Replacement Methods | cssWOW:: CSS Gallery

  • Dee

    Typekit anyone? I haven’t used all these methods but there’s a good discussion of Cufon, @font-face and Typekit here: http://www.zeldman.com/2010/03/22/my-lovehate-affair-with-typekit/

  • abcgi

    For hover states:

    Cufon.replace(‘element’, {hover: ‘true’});

    • luai

      thank for this great tuts.
      why it dosn’t work with arabic font’s ?

  • K

    I tried to use it with “text-decoration: underline;” and it doesn’t work. It simply doubles the underline which looks pretty messy. In IE8 the underline doesn’t get rendered at all. Looks like we have to life with a lot of restrictions.

  • icute

    Its handy! love the tutorial and ya voice..thanks for this informative tut and it works like a flash..

  • http://www.sagive.co.il Sagive

    Jeez This is the best tutorial on Cufun yet!
    gona be real helpfull when building arabic or hebrew website
    on wordpress..

    10x a lot

  • http://brettbowlin.com Brett

    Looks like Cufon is anti-aliased unlike @font-face

  • Pingback: Use custom fonts with Cufón | Ayaz Ahmed

  • Pingback: Common Cross Browser Fonts and Cufon | deanographics.com

  • Pingback: TOKGOO » Cufon 实现网页字体渲染

  • luai

    thank for this great tuts.
    why it dosn’t work with arabic font’s ?

  • KennyP

    This is about as clear as mud to me. I put my code in the exact same places but it did nothing.

  • Pingback: Cufon 实现网页字体渲染 | Alan's World

  • Pingback: How to Achieve Cross-Browser @font-face Support | blog.designs4u.nl

  • Pingback: Come out of web-safe font use any font for your website « Web Creatives

  • Jeff

    hi guys? I’m new to this cufon replacement. I’m trying “Lifestyle Rounded M54″ font that I download in the Dafont.com. after that i embed the JS file in the sample web like Jeffrey Way’s tutorial and reload it then poof! its not working. can somebody help me with this guys! here is my code.

    Untitled Document

    Cufon.replace(‘h1′);

    My H1 Tag Goes Right Here

    .

  • Jeff

    hi guys? I’m new to this cufon replacement. I’m trying “Lifestyle Rounded M54″ font that I download in the Dafont.com. after that i embed the JS file in the sample web like Jeffrey Way’s tutorial and reload it then poof! its not working. can somebody help me with this guys! here is my code.

    Cufon.replace(‘h1′);

    My H1 Tag Goes Right Here

  • Jeff

    Cufon.replace(‘h1′);

    My H1 Tag Goes Right Here

  • Jeff

    sorry guys for the 4 time posting my connection is so slow
    here is the link of the code

    http://img225.imageshack.us/i/samplext.jpg/

    i need you knowledge in this matter
    thanks

  • http://www.coleruddick.com Cole

    As someone who’s new to web fonts, your walk-through was really helpful! I’m interested to see where this leaves us once CSS 3 is mainstream. Thanks again for your work on this tutorial.

  • http://readmehow.com readmehow

    ohh great tut.
    thanks man very much.. keep moving forword