一起使用Cufon和@ font-face

时间:2011-05-01 07:48:04

标签: javascript html css html5

我认为现在是在网络上广泛使用自定义字体的时候了!向您的网站添加自定义字体的最佳和最简单的方法是Cufon。这很容易使用,真的是跨浏览器。它甚至支持IE5.5!无论如何,它迫使客户端下载字体的大量脚本,用户也无法在浏览器中正确使用“查找”和“缩放”功能。 另一方面,@ font-face现在被广泛支持,它更具语义性。 Here他们谈到了@ font-face浏览器支持和其他问题。

我的问题是如何将@ font-face和Cufon结合使用,并为更多具有最佳性能的客户端提供自定义字体。当我们可以通过@ font-face加载自定义字体时,我们不应该加载Cufon的自定义脚本。当我们更喜欢在目标客户端中使用cufon时,我们也不应该在客户端加载自定义字体。 (我们认为我们有支持@ font-face的系统/浏览器,但我们使用Cufon)。

我要求你编写一个基于@ font-face支持和其他因素的脚本,比如开发人员喜欢的,所以加载Cufon或自定义字体。 here是一个很棒的@ font-face探测器:

/*!
 * 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/BDgAAAD0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EkAACAAACAAAFAASQAAUEkAAFIAZQBnAHUAbABhAHIAAFJlZ3VsYXIAAAAAAAADAAAAAwAAABwAAQAAAAAARAADAAEAAAAcAAQAKAAAAAYABAABAAIAIAAt//8AAAAgAC3////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;
})();

我想要一个这样的函数:

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

我的问题是这部分:

add @font-face to the CSS 

抱歉英语不好。

2 个答案:

答案 0 :(得分:3)

jQuery有一个函数字体可用于检查是否加载了字体字体:

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

答案 1 :(得分:2)

使用http://yepnopejs.com/ + http://www.modernizr.com/(我建议在http://modernizr.github.com/Modernizr/2.0-beta/使用自定义构建器),您可以测试@ font-face兼容性,并有条件地加载带有@font的CSS文件-face规则,或加载Cufon。这将完全符合您的要求。

不幸的是,当异步加载时,Cufon在IE中不起作用。但由于它支持@ font-face,你不需要Cufon。有关详细信息,请参阅https://github.com/sorccu/cufon/issues/171