35

我了解到,Google 会根据访问它的浏览器/设备自动提供TTFEOTWOFFSVG字体文件。

现在我计划从我的服务器本身托管和提供字体文件,为此我首先必须下载网络字体的所有文件格式。

我如何或在哪里可以下载我想使用的网络字体的 4 种文件格式?

PS:通过使用不同的浏览器——Chrome、IE9 和 Safari(开发 - iPhone UA),我能够获得 WOFF、EOT 和 TTF 格式。不过 SVG 格式不走运。如果有更简单的方法,那就太棒了。

编辑:哦,顺便说一句,我知道我可以从fontsquirrel下载各种格式,但我说的是从官方 repo 下载。

4

6 回答 6

52

======= 2016-05-31 更新 =======

我制作了一个小的 PHP 脚本来从 Google 字体 CSS 导入 URL 获取下载链接,例如:https ://fonts.googleapis.com/css?family=Roboto:400,700|Slabo+27px|Lato:400,300italic,900italic

你可以在这里使用这个工具:http: //nikoskip.me/gfonts.php

例如,如果你使用上面的导入 URL,你会得到:

在此处输入图像描述

我厌倦了在每个新版本的 Chrome 上更新这个答案,因为它们总是改变你可以欺骗用户代理字符串的方式,所以请改用这个脚本。

======= 旧解决方案 =======

使用 Chrome 中的 DevTools,您可以覆盖用户代理。

如何:

  1. 在 Google 字体页面获取您需要的字体。
  2. 您将在 CSS 中导入一个 URL,例如: http://fonts.googleapis.com/css?family=Cabin:500,700,500italic, 700italic
  3. 在浏览器中打开该 URL,您将看到可以实际下载字体的完整 URL。
  4. 转到开发人员工具 (F12) 并按 ESC
  5. 选择“仿真”选项卡,然后单击“网络”子选项卡
  6. 最后Spoof user agent选择 IE9 用于 EOT 格式,Android 4 用于 TTF,此 UA 字符串用于 SVG:(Mozilla/4.0 (iPad; CPU OS 4_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/4.1 Mobile/9A405 Safari/7534.48.3感谢匿名)
于 2013-10-23T20:27:57.827 回答
8

您可以在http://code.google.com/p/googlefontdirectory/克隆 Google webfonts 目录

您还可以在http://code.google.com/p/googlefontdirectory/source/browse/#font_name获取单一字体文件

于 2012-04-24T14:39:17.720 回答
5

如何获取字体下载 url,包括 SVG 和 woff2。

下载每种字体所需的用户代理如下。来源

module.exports = {
  USER_AGENTS: {
    eot: 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)',
    woff: 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0',
    woff2: 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML like Gecko) Chrome/38.0.2125.104 Safari/537.36', // complete woff2 file for one variant
    svg: 'Mozilla/4.0 (iPad; CPU OS 4_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/4.1 Mobile/9A405 Safari/7534.48.3',
    ttf: 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.54.16 (KHTML, like Gecko) Version/5.1.4 Safari/534.54.16'
  },
  GOOGLE_FONTS_API_KEY: 'AIzaSyDY-C-Lt9uyPP5fSTjMCR4bB944SlI4spw',
  CACHE_DIR: __dirname + "/cachedFonts/",
}

使用 devtool 添加这些用户代理。

如何将新设备添加到 chrome

来源

您现在可以访问https://fonts.googleapis.com/css?family=Open+Sans和欺骗您的用户代理,通过访问@font-face.

或者google-webfonts-helper为您完成这一切。 google-webfonts-helper 这里有一篇很棒的博客文章来自我获取图像的开发人员。

为什么要自托管?

如果可能,请始终使用通用 CDN,您的字体甚至不需要下载(浏览器缓存)的可能性更大。

如果您担心谷歌向用户发送错误的字体,很可能是因为他们欺骗了他们的用户代理,那么还有另一种选择,仍然可以获得使用谷歌托管的好处。

插入@font-face自己,只需使用上述步骤找到谷歌字体网址并将其插入<head>;

<style>

@font-face {
    font-family: 'Open Sans';
    font-style: normal;
    font-weight: 400;
    src: url(google-font-url-here/opensans.eot);
    src: local('Open Sans'), local('OpenSans'),
    url(google-font-url-here/opensans.eot?#iefix) format('embedded-opentype'),
    url(google-font-url-here/opensans.woff2) format('woff2'), 
    url(google-font-url-here/opensans.woff) format('woff'), 
    url(google-font-url-here/opensans.ttf) format('truetype'), 
    url(google-font-url-here/opensans.svg#OpenSans) format('svg');
}

</style>

这会带来风险,因为 URL 可能会发生变化!

于 2016-03-09T08:23:19.543 回答
4

https://github.com/google/fonts

旧答案: 我在线克隆了https://code.google.com/archive/p/googlefontdirectory/ :)

https://bitbucket.org/Tymek/google-web-fonts/你去!

于 2013-04-23T12:46:09.630 回答
2

我编写了一个 PowerShell 脚本来自动下载提供给几个不同用户代理的字体。对于基本字体,它获得所有四种格式(woff、ttf、svg、eot)。Google 似乎没有为粗体和斜体提供 SVG 和 EOT 文件。


$agents = "Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0",`
    "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1",`
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)",`
    "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7",`
    "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US)"

foreach($arg in $args) {
    $arg;
    foreach($agent in $agents) {
        $agent;
        $webclient = New-Object System.Net.WebClient
        [void]$webclient.Headers.Add("user-agent", $agent)
        $url = "http://fonts.googleapis.com/css?family=$arg"

        $css = $webclient.DownloadString($url)
        $css
        $fonts = $css |
            Select-String -AllMatches "http://[A-Za-z0-9/._?&=%-]+" |
            Select-Object -ExpandProperty Matches |
            Select-Object -ExpandProperty Value

        foreach($font in $fonts) {
            $font
            $fontfile = [System.Io.Path]::GetFileName((new-object System.Uri $font).LocalPath)
            [void]$webclient.DownloadFile($font, "$pwd\$fontfile")
        }
    }
}

一旦它位于 .ps1 文件中,就可以使用要下载的字体作为参数调用它:

PS> .\DownloadFonts.ps1 "Open+Sans:400,700,400italic,700italic"

该脚本将输出从 Google 服务器中提取的 CSS,以帮助您确定哪个文件是哪个文件(例如,在我的情况下,SVG 字体被提取为一个名为“font”的文件)。

这是基于 RichardN 和 ldeck 在博客文章Locally Caching Google Web Fonts上发布的 bash 脚本。

作为参考,这里是 ldeck 的 bash 脚本:


#!/bin/sh

for family in $*; do
 for url in $( {
 for agent in \
 'Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0' \
 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1' \
 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)' ;
 do
 curl -A "$agent" -s "http://fonts.googleapis.com/css?family=$family" | \
 grep -oE 'http://[A-Za-z0-9/._-]+'; \
 done } | sort -u ) ;
 do
 extn=${url##*.} ;
 file=$(echo "$family"| tr +[:upper:] _[:lower:]);
 echo $url $file.$extn;
 curl -s "$url" -o "$file.$extn";
 done
done

进一步参考:使用 HTML5 AppCache

于 2015-12-29T00:15:50.997 回答
1

作为我在开源项目MDIDX上工作的一部分,我编写了一个python 脚本。它将从 Google Fonts CDN 获取给定字体系列的所有可能字体格式。这很天真 - 暴力破解通用平台的用户代理

可以这样简化:

import re
import os
import requests

def main():
    font_family = "Material Icons"
    output_directory = "fonts"
    download_fonts(font_family, output_directory)

def download_fonts(font_family, output_directory, output_file_name=None):
    if not os.path.isdir(output_directory):
        os.makedirs(output_directory)

    if not output_file_name:
        output_file_name = font_family.lower().replace(" ","-")

    user_agents = USER_AGENTS.splitlines()
    user_agents = map(lambda x: x, user_agents)

    font_urls = set()
    for user_agent in user_agents:
        url = 'https://fonts.googleapis.com/icon?family={}'.format("+".join(font_family.strip().split()))
        r = requests.get(url, headers={"user-agent": user_agent})
        r.raise_for_status()
        urls = re.findall('url\((.*?)\)', r.text)
        urls = map(str, urls)
        urls = filter(str, urls)
        font_urls.update(urls)

    fonts_map = {font_url.split('.')[-1].lower(): font_url for font_url in font_urls}

    for file_extension, url in fonts_map.items():
        file_path = os.path.join(output_directory, '{output_file_name}.{file_extension}'.format(output_file_name=output_file_name, file_extension=file_extension))
        _download_file(url, file_path)

USER_AGENTS = """
Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.3) Gecko/20090913 Firefox/3.5.3
Mozilla/5.0 (Windows; U; Windows NT 6.1; en; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)
Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)
Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.1) Gecko/20090718 Firefox/3.5.1
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.1 (KHTML, like Gecko) Chrome/4.0.219.6 Safari/532.1
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; InfoPath.2)
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.5.30729; .NET CLR 3.0.30729)
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Win64; x64; Trident/4.0)
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; .NET CLR 2.0.50727; InfoPath.2)Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)
Mozilla/4.0 (compatible; MSIE 6.1; Windows XP)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; da-dk) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1
Opera/9.80 (Windows NT 6.1; U; es-ES) Presto/2.9.181 Version/12.00
Mozilla/5.0 (Linux; U; Android 2.3.4; fr-fr; HTC Desire Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; Android 7.0; SAMSUNG SM-N920C Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/6.2 Chrome/56.0.2924.87 Mobile Safari/537.36
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 BIDUBrowser/2.x Safari/537.31
Mozilla/5.0 (Linux; U; Android 4.4.2; zh-cn; GT-I9500 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.0 QQ-URL-Manager Mobile Safari/537.36
Mozilla/5.0 (Android 8.0.0; Tablet; rv:57.0) Gecko/57.0 Firefox/57.0
Mozilla/5.0 (Android 8.1.0; Mobile; rv:61.0) Gecko/61.0 Firefox/61.0
"""

def _download_file(url, file_path):
    r = requests.get(url)
    r.raise_for_status()
    with open(file_path, 'wb') as f:
        f.write(r.content)

if __name__ == '__main__':
    main()

在此处输入图像描述

于 2019-01-25T10:22:04.873 回答