我需要在内部网应用程序上使用一些谷歌字体。客户端可能有也可能没有互联网连接。阅读许可条款,这似乎是法律允许的。


当前回答

最简单的方法-使用google-webfonts-helper

假设我们想要托管字体Red Rose:

Open google-webfonts-helper and filter for the required font on top left (type Red Rose and enter..) Choose from the charsets (default is latin; select others like latin-ext if you want extended support) Select the styles (deafult is regular) From the Copy CSS tab Select Modern Browser if you wish to support only modern browsers (woff2, woff) Select Best Support if you wish to support all browsers (I chose this - woff2, woff,ttf,svg,eot) In case your font files do not reside in ../fonts/ path, you can edit it to represent your actual path (for me it was ../assets/fonts/) Copy the CSS and keep for future use Download the zip file named red-rose-v1-latin-ext_latin; unzip it and place all fonts directly into your assets/fonts directory (based on what you gave earlier) In your stylesheet where you wish to embed, paste the copied CSS. It will look like the below if you chose these options

/* 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;
}

这一切!

其他回答

实际上,您可以直接从谷歌下载所有字体格式变体,并将它们包含在您的css中,以从您的服务器提供服务。这样你就不必担心谷歌跟踪你网站的用户。然而,缺点可能会减慢你自己的发球速度。字体对资源的要求很高。我还没有在这个问题上做任何测试,不知道是否有人有类似的想法。

编辑:正如luckyrumo指出的那样,字体被放弃了,取而代之的是:https://github.com/fontsource/fontsource

如果您正在使用Webpack,您可能会对这个项目感兴趣:https://github.com/KyleAMathews/typefaces

例如,你想使用Roboto字体:

npm install typeface-roboto --save

然后把它导入你的应用的入口点(主js文件):

import 'typeface-roboto'

我写了一个bash脚本,在谷歌的服务器上使用不同的用户代理获取CSS文件,将不同的字体格式下载到本地目录,并编写包含它们的CSS文件。注意,该脚本需要Bash版本4.x。

请参阅https://neverpanic.de/blog/2014/03/19/downloading-google-web-fonts-for-local-hosting/获取脚本(我在这里不复制它,所以我只需要在需要时在一个地方更新它)。

编辑:移动到https://github.com/neverpanic/google-font-download

你可以从https://github.com/google/fonts下载原始字体

之后,使用字体游弋工具将您的大型Unicode字体分成多个子集(例如拉丁,西里尔)。您应该使用该工具执行以下操作:

为您支持的每种语言生成子集 使用unicode范围子集节省带宽 删除臃肿从你的字体和优化他们的网页 将字体转换为压缩的woff2格式 为旧浏览器提供.woff备份 自定义字体加载和渲染 使用@font-face规则生成CSS文件 自托管web字体或在本地使用它们


字体游侠:https://www.npmjs.com/package/font-ranger

附注:你也可以使用Node.js API自动化这个过程

很好的解决方案是google-webfonts-helper .

它允许你选择多个字体变体,这节省了很多时间。