我需要在一个内部应用中使用一些谷歌字体。客户端可能有或没有网络连接。阅读许可条款,似乎合法允许。
我需要在一个内部应用中使用一些谷歌字体。客户端可能有或没有网络连接。阅读许可条款,似乎合法允许。
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
一个很棒的解决方案是google-webfonts-helper。
它允许你选择多个字体变体,节省了很多时间。
latin-ext
字体。 - piotrekkr@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');
}
google-webfonts-helper
红玫瑰
:复制 CSS
标签页中
现代浏览器
最佳支持
../fonts/
路径下,则可以编辑它以表示您的实际路径(对我而言是 ../assets/fonts/
)red-rose-v1-latin-ext_latin
的 zip 文件;解压缩它并将所有字体直接放入您的 assets/fonts
目录中(根据您之前给出的内容)/* red-rose-regular - latin-ext_latin */
@font-face {
font-family: 'Red Rose';
font-style: normal;
font-weight: 400;
src: url('../assets/fonts/red-rose-v1-latin-ext_latin-regular.eot'); /* IE9 Compat Modes */
src: local('Red Rose Regular'), local('RedRose-Regular'),
url('../assets/fonts/red-rose-v1-latin-ext_latin-regular.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('../assets/fonts/red-rose-v1-latin-ext_latin-regular.woff2') format('woff2'), /* Super Modern Browsers */
url('../assets/fonts/red-rose-v1-latin-ext_latin-regular.woff') format('woff'), /* Modern Browsers */
url('../assets/fonts/red-rose-v1-latin-ext_latin-regular.ttf') format('truetype'), /* Safari, Android, iOS */
url('../assets/fonts/red-rose-v1-latin-ext_latin-regular.svg#RedRose') format('svg'); /* Legacy iOS */
}
/* Red Rose will now be available for use in your stylesheet, provide this font */
:root {
font-family: 'Red Rose', cursive, sans-serif;
}
如果您想要将所有字体(或其中一些)托管在自己的服务器上,您可以从此存储库下载字体并按照您想要的方式使用:https://github.com/praisedpk/Local-Google-Fonts
如果您只是想解决与Google Fonts相关的浏览器缓存问题,您可以使用替代的字体CDN,并将字体包含为:
<link href="https://pagecdn.io/lib/easyfonts/fonts.css" rel="stylesheet" />
或者一个特定的字体,例如:
<link href="https://pagecdn.io/lib/easyfonts/lato.css" rel="stylesheet" />
编辑: 如luckyrumo所指出,typefaces已被弃用,推荐使用: https://github.com/fontsource/fontsource
如果您正在使用Webpack,您可能会对此项目感兴趣: https://github.com/KyleAMathews/typefaces
例如,假设您想要使用Roboto字体:
npm install typeface-roboto --save
然后在您的应用程序入口点(main js文件)中导入它:
import 'typeface-roboto'