Google网页字体的本地备选方案

12
HTML5 Boilerplate使用一个脚本来加载本地的jQuery副本,如果无论什么原因,谷歌CDN版本加载失败:

HTML5 Boilerplate使用一个脚本来加载本地的jQuery副本,如果因为某些原因,Google CDN版本无法加载:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.9.1.min.js"><\/script>')</script>

使用Google Web Fonts是否可以实现这样的功能?即:如果远程字体加载失败,则使用存储在服务器上的本地字体副本代替。

3个回答

22

我刚刚遇到了这个问题,在来到这个页面后想到了一个解决方案:

@import url(http://fonts.googleapis.com/css?family=Ubuntu:400,400italic,700,700italic);

@font-face {
  font-family: "UbuntuFallback";
  font-style: normal;
  font-weight: normal;
  src: url("/webfonts/ubuntu/ubuntu-webfont.eot?#iefix") format("embedded-opentype"), url("/webfonts/ubuntu/ubuntu-webfont.woff") format("woff"), url("/webfonts/ubuntu/ubuntu-webfont.ttf") format("truetype");
}
@font-face {
  font-family: "UbuntuFallback";
  font-style: normal;
  font-weight: bold;
  src: url("/webfonts/ubuntu/ubuntu-bold-webfont.eot?#iefix") format("embedded-opentype"), url("/webfonts/ubuntu/ubuntu-bold-webfont.woff") format("woff"), url("/webfonts/ubuntu/ubuntu-bold-webfont.ttf") format("truetype");
}
@font-face {
  font-family: "UbuntuFallback";
  font-style: italic;
  font-weight: normal;
  src: url("/webfonts/ubuntu/ubuntu-italic-webfont.eot?#iefix") format("embedded-opentype"), url("/webfonts/ubuntu/ubuntu-italic-webfont.woff") format("woff"), url("/webfonts/ubuntu/ubuntu-italic-webfont.ttf") format("truetype");
}
@font-face {
  font-family: "UbuntuFallback";
  font-style: italic;
  font-weight: bold;
  src: url("/webfonts/ubuntu/ubuntu-bolditalic-webfont.eot?#iefix") format("embedded-opentype"), url("/webfonts/ubuntu/ubuntu-bolditalic-webfont.woff") format("woff"), url("/webfonts/ubuntu/ubuntu-bolditalic-webfont.ttf") format("truetype");
}

body 
{
  font-family: Ubuntu, UbuntuFallback, Tahoma, Sans-Serif;
}

所以我想使用Ubuntu字体,但是我们的网站在本地主机上运行,不一定连接到互联网。我创建了一些@font-face声明来引用Ubuntu字体,将其命名为其他名称(“UbuntuFallback”),并将其放在font-family样式列表中。

完成!


1
非常感谢 - 奇怪的是,即使这么明显和优雅,我也没能看到它.. :) - Moritz Friedrich

1
如果您下载了所有必要的Web字体并将其存储在本地文件Webfonts.css中,您可以执行以下操作:
<script type="text/javascript">
if (window.jQuery) {
    document.write(unescape('%3Clink href="http://fonts.googleapis.com/css?family=OFL+Sorts+Mill+Goudy+TT|Yanone+Kaffeesatz|Tangerine" rel="stylesheet" type="text/css"%3E'));
}
else {
   document.write(unescape('%3Clink href="css/WebFonts.css"    rel="stylesheet" type="text/css"%3E'));
   document.write('<script src="js/vendor/jquery-1.9.1.min.js"><\/script>')
}
</script>

注意:假设cdn的fonts.googleapis.com和ajax.googleapis.com同时失效..

0
一个客户端回退调用包含不同字体调用(这里是Tangerine字体)的CSS文件:
(function test($){
    var $span = $('<span style="display:none;font-family:Tangerine"></span>').appendTo('body'); // span test creation
    if ($span.css('fontFamily') !== 'Tangerine'){
        $('head').append('<link href="./Styles/Public/Fonts.css" rel="stylesheet">'); // fallback link
    }
    $span.remove(); // span test remove
})(jQuery);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接