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

5

我正在使用可下载字体API。 我下载了Google示例应用程序,并将代码合并到我的项目中。 两者都运行成功,但是一些字体始终无法从我的应用程序和示例应用程序中下载。

我使用FontsContractCompat.requestFont并获得回调到onTypefaceRequestFailed(int reason),其中的原因是1。文档说这意味着“FAIL_REASON_FONT_NOT_FOUND”。 我认为这些字体应该存在,因为:1)它们出现在随示例应用程序一起提供的xml文件中,2)它们出现在Google字体的在线列表中,并且3)它们从开发人员web api返回(https://www.googleapis.com/webfonts/v1/webfonts?key=)

以下是失败字体的列表: Angkor Archivo Asap Condensed Baloo Bhaijaan Baloo Tammudu Battambang Bayon Bellefair BioRhyme Expanded Bokor Cabin Condensed Chau Philomene One Chenla Content Dangrek Encode Sans Encode Sans Condensed Encode Sans Expanded Encode Sans Semi Condensed Encode Sans Semi Expanded Fasthand Faustina Freehand Hanuman Khmer Koulen Libre Barcode 128 Libre Barcode 128 Text Libre Barcode 39 Libre Barcode 39 Extended Libre Barcode 39 Extended Text Libre Barcode 39 Text Mada Manuale Metal Moul Moulpali Mukta Mukta Mahee Mukta Malar Nokora Open Sans Condensed Preahvihear Roboto Condensed Saira Saira Condensed Saira Extra Condensed Saira Semi Condensed Sedgwick Ave Sedgwick Ave Display Siemreap Suwannaphum Taprom Ubuntu Condensed Zilla Slab Zilla Slab Highlight


我能够通过这个链接下载字体:https://fonts.google.com/download?family=Angkor,但我不确定为什么它不能与API一起使用。 - Tushar Gogna
谢谢。我也尝试过那个。。 - Gili Garibi
遇到了同样的问题。真的很困惑为什么有些字体无法加载,因为文档上说:“我可以使用哪些字体?整个 Google Fonts 开源集合!请访问 https://fonts.google.com 浏览。” - Steven Schoen
1个回答

4

这确实很奇怪。我发现许多字体(但不是全部)都没有"latin"或"latin-ext"子集,因此这似乎是自动过滤它们的一种方法。我编写了一个小的Python2脚本,向API请求整个字体列表,然后对它们进行"latin"过滤,并将剩下的内容输出为新的字体家族资源文件,您可以将其重定向到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。如果你将它与Google提供的那个进行比较,它会黑掉问题中列出的大多数字体,但并不包括“Zilla”、“Ubuntu”、“Barcode”和“Encode”等字体。也许这些字体还有其他共同点,可以用来进一步筛选列表?
有趣的是,生成的列表还包括在github列表中没有的新字体,包括:
- VolKorn SC - Spectral - Spectral SC - Sedgewick Ave - Sedgewick Ave Display
...以及更多。其中一些字体似乎可以在Android上使用。
因此,我猜想该演示文件中的列表可能已经过时了。也许存在许可问题或技术问题,使得需要更改列表。
无论如何,值得提交一个拉取请求,使用更新和更全面的列表,删除不再提供的字体,并添加已知可与提供者一起使用的API提供的字体。

1
我不再参与这个项目,但你的回答似乎足够有帮助,值得获得认可。如果其他人遇到相同的问题,我相信他们会喜欢它(而且我很惊讶,几乎没有人似乎已经遇到过)。 - Gili Garibi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接