我需要在内部网应用程序上使用一些谷歌字体。客户端可能有也可能没有互联网连接。阅读许可条款,这似乎是法律允许的。
当前回答
CSS文件的内容(来自include URL)取决于我从什么浏览器查看它。例如,当使用Chrome浏览器浏览http://fonts.googleapis.com/css?family=Open+Sans时,该文件只包含WOFF链接。使用Internet Explorer(如下),它包括EOT和WOFF。我把所有链接都粘贴到浏览器里下载。
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 400;
src: url(http://themes.googleusercontent.com/static/fonts/opensans/v6/cJZKeOuBrn4kERxqtaUH3fY6323mHUZFJMgTvxaG2iE.eot);
src: local('Open Sans'), local('OpenSans'), url(http://themes.googleusercontent.com/static/fonts/opensans/v6/cJZKeOuBrn4kERxqtaUH3fY6323mHUZFJMgTvxaG2iE.eot) format('embedded-opentype'), url(http://themes.googleusercontent.com/static/fonts/opensans/v6/cJZKeOuBrn4kERxqtaUH3T8E0i7KZn-EPnyo3HZu7kw.woff) format('woff');
}
当你拥有自己的网页字体时,你需要正确地链接到每种字体类型,处理遗留的浏览器错误等。当您使用谷歌Web Fonts(由谷歌托管)时,谷歌自动链接到该浏览器的正确字体类型。
其他回答
这在法律上是允许的,只要你遵守字体许可的条款——通常是OFL。
你需要一套网页字体格式,而font Squirrel Webfont Generator可以生成这些格式。
但是OFL要求在修改字体时重命名它们,使用生成器意味着修改它们。
请记住,我的回答已经过时了很多。
下面还有一些技术上更复杂的答案,例如:
Neverpanic/google-font-download google-webfont-helper 本地字体
所以,不要因为这是目前公认的答案,就以为这是最好的答案。
你现在也可以下载谷歌的整个字体集通过github在他们的谷歌/字体资源库。它们还提供字体的约420MB压缩快照。
You first download your font selection as a zipped package, providing you with a bunch of true type fonts. Copy them somewhere public, somewhere you can link to from your css.
On the google webfont download page, you'll find a include link like so:
http://fonts.googleapis.com/css?family=Cantarell:400,700,400italic,700italic|Candal
It links to a CSS defining the fonts via a bunch of @font-face
defintions.
Open it in a browser to copy and paste them into your own CSS and modify the urls to include the right font file and format types.
So this:
@font-face {
font-family: 'Cantarell';
font-style: normal;
font-weight: 700;
src: local('Cantarell Bold'), local('Cantarell-Bold'), url(http://themes.googleusercontent.com/static/fonts/cantarell/v3/Yir4ZDsCn4g1kWopdg-ehHhCUOGz7vYGh680lGh-uXM.woff) format('woff');
}
becomes this:
/* Your local CSS File */
@font-face {
font-family: 'Cantarell';
font-style: normal;
font-weight: 700;
src: local('Cantarell Bold'), local('Cantarell-Bold'), url(../font/Cantarell-Bold.ttf) format('truetype');
}
As you can see, a downside of hosting the fonts on your own system this way is, that you restrict yourself to the true type format, whilst the google webfont service determines by the accessing device which formats will be transmitted.
Furthermore, I had to add a .htaccess
file to my the directory holding the fonts containing mime types to avoid errors from popping up in Chrome Dev Tools.
For this solution, only true type is needed, but defining more does not hurt when you want to include different fonts as well, like font-awesome
.
#.htaccess
AddType application/vnd.ms-fontobject .eot
AddType font/ttf .ttf
AddType font/otf .otf
AddType application/x-font-woff .woff
我在一个grunt任务中使用了grunt-local-googlefont。
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
"local-googlefont" : {
"opensans" : {
"options" : {
"family" : "Open Sans",
"sizes" : [
300,
400,
600
],
"userAgents" : [
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)", //download eot
"Mozilla/5.0 (Linux; U; Android 4.1.2; nl-nl; GT-I9300 Build/JZO54K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", //download ttf
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1944.0 Safari/537.36" //download woff and woff2
],
"cssDestination" : "build/fonts/css",
"fontDestination" : "build/fonts",
"styleSheetExtension" : "css",
"fontDestinationCssPrefix" : "fonts"
}
}
}
});
grunt.loadNpmTasks('grunt-local-googlefont');
};
然后,要检索它们:
grunt local-googlefont:opensans
注意,我使用的是原始版本的一个分支,它在检索名称中有空格的字体时工作得更好。
我有一个类似于@neverpanic的PHP脚本,自动从谷歌下载CSS和字体(暗示和未暗示)。然后,它根据用户代理从您自己的服务器提供正确的CSS和字体。它有自己的缓存,所以用户代理的字体和CSS只会被下载一次。
它还处于早期阶段,但可以在这里找到:DaAwesomeP / php-offline-fonts
编辑:正如luckyrumo指出的那样,字体被放弃了,取而代之的是:https://github.com/fontsource/fontsource
如果您正在使用Webpack,您可能会对这个项目感兴趣:https://github.com/KyleAMathews/typefaces
例如,你想使用Roboto字体:
npm install typeface-roboto --save
然后把它导入你的应用的入口点(主js文件):
import 'typeface-roboto'