可下载的字体 - 无法下载一些谷歌字体

时间:2017-11-01 08:45:10

标签: android fonts google-font-api google-fonts

我正在玩Downloadable fonts api。我下载了Google sample application并将代码合并到我的项目中。两者都运行成功,但有些字体始终无法从我的应用程序和示例应用程序下载。

我使用FontsContractCompat.requestFont并获得onTypefaceRequestFailed(int reason)的回调,理由为1.文档说它意味着“FAIL_REASON_FONT_NOT_FOUND”。 我认为这些字体应该存在,因为:1)它们出现在示例应用程序附带的xml文件中,2)它们出现在online list of Google Fonts中,3)它们从开发人员web api返回({{1} })

以下是失败字体列表:https://www.googleapis.com/webfonts/v1/webfonts?key=

1 个答案:

答案 0 :(得分:3)

这绝对是奇怪的。我观察到很多(但不是全部)这些字体没有" latin"或" latin-ext"子集,所以这似乎是一种自动过滤它们的方法。我把一个小的python2脚本放在一起,要求API提供整个字体列表,然后过滤它们的#34; latin"并输出最新的font-families资源文件,您可以将其重定向到family_names.xml

用法:fontlist.py <API_KEY>

#!/usr/bin/python
# fontlist.py by fat-tire
#
# Collects Google provider latin & latin-ext font families and creates a replacement for
# https://github.com/googlesamples/android-DownloadableFonts/blob/master/app/src/main/res/values/family_names.xml
#
# See https://developers.google.com/fonts/docs/developer_api for more info on the Google Fonts API
#
# Usage:     fontlist.py <API_KEY> > family_names.xml

import sys, urllib2, json

if len(sys.argv) != 2:
    print "Usage:"
    print "  fontlist.py <API_KEY> > family_names.xml"
    print "No Google Fonts API key?  Get one at https://developers.google.com/fonts/docs/developer_api#APIKey"
    sys.exit(0)

APIKEY=sys.argv[1]
url="https://www.googleapis.com/webfonts/v1/webfonts?key="

opener = urllib2.build_opener()
try:
    request = urllib2.Request(url + APIKEY)
    conn = opener.open(request)
except Exception, e:
    print "Whoopsie.  Got a " + str(e.code) + " " + str(e.reason) + " error.  You sure that API is legit?"
    sys.exit(1)
data = json.loads(conn.read())

count = 0
items = data["items"]

print "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
print "<!-- Collected from " + url+APIKEY + " -->"
print """<resources>
    <string-array name="family_names">"""
for key in items:
    if "latin" in key["subsets"]:
        print " "*10 + "<item>" + key["family"] + "</item>"
        count = count + 1
print """    <!--Total:  """ + str(count) + """-->
    </array>
</resources>"""
sys.exit(0)

此脚本输出一个有趣的family_names.xml。如果将它与one provided by google进行比较,则会使问题中列出的大多数字体变黑。但它并没有得到所有这些,包括&#34; Zilla&#34;,&#34; Ubuntu&#34;,&#34;条码&#34;和&#34;编码&#34;字体。也许这些字体也有共同之处,可用于进一步过滤列表?

有趣的是,生成的列表还包括不在github列表中的新字体,包括:

  • VolKorn SC
  • Spectral SC
  • Sedgewick Ave
  • Sedgewick Ave Display

....&#34; Barlow&#34;,&#34; Bellefair&#34;等等。其中一些字体似乎适用于Android。

所以我猜测该演示文件中的列表只是旧的。也许有许可问题或技术问题使得有必要切换列表。

无论是否值得提交一个拉取请求,其中包含更新且更新的列表,该列表会删除不再提供的字体,并添加API提供的已测试且已知可与之一起使用的字体。提供商。