我了解到 Google 会自动提供 TTF、EOT、WOFF 或 SVG 字体文件,具体取决于从中访问的 浏览器/设备。
现在我计划从我的服务器本身托管和提供字体文件,为此我首先必须下载网络字体的所有文件格式。
如何或在哪里可以下载我想要使用的网络字体的 4 种文件格式?
PS: 通过使用不同的浏览器 - Chrome、IE9 和 Safari(开发 - iPhone UA),我能够获得 WOFF、EOT 和 TTF 格式。但 SVG 格式的运气不佳。如果有更简单的方法那就太棒了。
编辑:哦,顺便说一句,我确实知道我可以从 fontsquirrel 下载各种格式,但我说的是从这里的官方存储库下载。
=======更新于2016-05-31 =======
我制作了一个小型 PHP 脚本,从 Google Fonts 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,您可以覆盖用户代理。
如何:
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
(感谢匿名)您可以在 http://code.google.com/p/googlefontdirectory/
克隆 Google webfonts 目录您还可以在 http://code.google.com/p/googlefontdirectory/source/browse/#font_name
获取单个字体文件下载每种字体所需的用户代理如下。 来源。
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 添加这些用户代理。
您现在可以访问
https://fonts.googleapis.com/css?family=Open+Sans
并欺骗您的用户代理,通过访问 @font-face
中找到的网址下载字体。
或者 google-webfonts-helper 可以为您完成这一切。 有一篇很棒的博客文章here,来自我获取该图像的开发人员。
如果可能,请始终使用通用 CDN,您的字体很可能甚至不需要下载(浏览器缓存)。
如果您担心谷歌向用户发送错误的字体,很可能是因为他们欺骗了他们的用户代理,那么还有另一个选择,仍然可以获得使用谷歌托管的好处。
自己插入
@font-face
,只需使用上述步骤找到 google 字体 url 并将其插入到 <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>
我编写了一个 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 在博客文章本地缓存 Google Web 字体上发布的 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。
我编写了一个 python 脚本,作为我在开源项目 MDIDX 上工作的一部分。它将从 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()