Mohsen
Mohsen

Reputation: 65835

Using Cufon and @font-face together

I think this is time of using custom fonts in web widely! The best and easiest way to add custom fonts to your website is Cufon. This is easy to use and really cross-browser. It even support IE5.5 !!! Anyway it force client to download a massive script for the font and also user can't use "find" and "zoom" functions in her browser properly. On the other hand @font-face is widely supported now days and it's more semantic. Here they spoke about @font-face browser support and other issues.

My question is how to use @font-face and Cufon together and deliver custom fonts to more clients with the best performance. We should not load Cufon's custom scripts when we can load custom font via @font-face and Also we should not load custom font in client side when we prefer using cufon in targeted client. (Think we have system/browser that supports @font-face but we preffer using Cufon).

I'm asking you to write an script that based on @font-face support and other factors like developer prefers and so loads Cufon or custom font. here is a great @font-face detector:

/*!
 * isFontFaceSupported - v0.9 - 12/19/2009
 * http://paulirish.com/2009/font-face-feature-detection/
 * 
 * Copyright (c) 2009 Paul Irish
 * MIT license
 */

var isFontFaceSupported = (function(){


    var fontret,
        fontfaceCheckDelay = 100;

        // IE supports EOT and has had EOT support since IE 5.
        // This is a proprietary standard (ATOW) and thus this off-spec,
        // proprietary test for it is acceptable. 
    if (!(!/*@cc_on@if(@_jscript_version>=5)!@end@*/0)) fontret = true;

    else {

    // Create variables for dedicated @font-face test
      var doc = document, docElement = doc.documentElement, 
          st  = doc.createElement('style'),
          spn = doc.createElement('span'),
          wid, nwid, body = doc.body,
          callback, isCallbackCalled;

      // The following is a font, only containing the - character. Thanks Ethan Dunham.
      st.textContent = "@font-face{font-family:testfont;src:url(data:font/opentype;base64,T1RUTwALAIAAAwAwQ0ZGIMA92IQAAAVAAAAAyUZGVE1VeVesAAAGLAAAABxHREVGADAABAAABgwAAAAgT1MvMlBHT5sAAAEgAAAAYGNtYXAATQPNAAAD1AAAAUpoZWFk8QMKmwAAALwAAAA2aGhlYQS/BDgAAAD0AAAAJGhtdHgHKQAAAAAGSAAAAAxtYXhwAANQAAAAARgAAAAGbmFtZR8kCUMAAAGAAAACUnBvc3T/uAAyAAAFIAAAACAAAQAAAAEAQVTDUm9fDzz1AAsD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AAAAgAC3////h/9UAAQAAAAAAAAAAAQYAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAAAAAAAA/7UAMgAAAAAAAAAAAAAAAAAAAAAAAAAAAQAEBAABAQEDUEkAAQIAAQAu+BAA+BsB+BwC+B0D+BgEWQwDi/eH+dP4CgUcAIwPHAAAEBwAkREcAB4cAKsSAAMCAAEAPQA/AEFHZW5lcmF0ZWQgaW4gMjAwOSBieSBGb250TGFiIFN0dWRpby4gQ29weXJpZ2h0IGluZm8gcGVuZGluZy5QSVBJAAAAAAEADgADAQECAxQODvb3h/cXAfeHBPnT9xf90wYO+IgU+WoVHgoDliX/DAmLDAr3Fwr3FwwMHgoG/wwSAAAAAAEAAAAOAAAAGAAAAAAAAgABAAEAAgABAAQAAAACAAAAAAABAAAAAMbULpkAAAAAx1KUiQAAAADHUpSJAfQAAAH0AAADQQAA)}";
      doc.getElementsByTagName('head')[0].appendChild(st);


      spn.setAttribute('style','font:99px _,serif;position:absolute;visibility:hidden'); 

      if  (!body){
        body = docElement.appendChild(doc.createElement('fontface'));
      } 

      // the data-uri'd font only has the - character
      spn.innerHTML = '-------';
      spn.id        = 'fonttest';

      body.appendChild(spn);
      wid = spn.offsetWidth;

      spn.style.font = '99px testfont,_,serif';

      // needed for the CSSFontFaceRule false positives (ff3, chrome, op9)
      fontret = wid !== spn.offsetWidth;

      var delayedCheck = function(){
        if (isCallbackCalled) return;
        fontret = wid !== spn.offsetWidth;
        callback && (isCallbackCalled = true) && callback(fontret);
      }

      addEventListener('load',delayedCheck,false);
      setTimeout(delayedCheck,fontfaceCheckDelay);
    }

    function ret(){  return fontret || wid !== spn.offsetWidth; };

    // allow for a callback
    ret.ready = function(fn){
      (isCallbackCalled || fontret) ? fn(fontret) : (callback = fn);
    }  

    return ret;
})();

I want a function like this:

if(isFontFaceSupported()==true && weWantFontFaceHere==true){ 
//add @font-face to the CSS and prevent Cufon
}
else{
//use Cufon
}

My question is how to this part:

add @font-face to the CSS 

Sorry for bad English.

Upvotes: 1

Views: 840

Answers (2)

Kevin C.
Kevin C.

Reputation: 2527

Using http://yepnopejs.com/ + http://www.modernizr.com/ (I recommend using the custom builder at http://modernizr.github.com/Modernizr/2.0-beta/) you can test for @font-face compatibility, and conditionally load either a CSS file with the @font-face rules, or load Cufon. This will accomplish exactly what you're looking for.

Unfortunately Cufon doesn't work in IE when loaded asynchronously. But since it supports @font-face, you wouldn't need Cufon. See https://github.com/sorccu/cufon/issues/171 for more information.

Upvotes: 2

Tom Claus
Tom Claus

Reputation: 1291

jQuery has a function fontAvailable where you can check if the fontface font is loaded:

$(document).ready(function() {
    if(!$.fontAvailable('FontFaceFontName')) {
        // Your Cufon Replace Code
    }
});

Upvotes: 3

Related Questions