5

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

我使用原因 1FontsContractCompat.requestFont并获得回调onTypefaceRequestFailed(int reason)。文档说这意味着“FAIL_REASON_FONT_NOT_FOUND”。我认为这些字体应该存在,因为:1)它们出现在示例应用程序附带的 xml 文件中,2)它们出现在Google Fonts 的在线列表中,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

4

1 回答 1

4

这肯定很奇怪。我观察到许多(但不是全部)这些字体没有“latin”或“latin-ext”子集,所以这似乎是一种自动过滤它们的方法。我拼凑了一个小 python2 脚本,它向 API 询问整个字体列表,然后将它们过滤为“拉丁”并将剩下的内容作为新的字体系列资源文件输出,您可以将其重定向到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 列表中的新字体,包括:

  • 沃尔科恩 SC
  • 光谱
  • 光谱 SC
  • 塞奇威克大街
  • 塞奇威克大街展示

....“Barlow”、“Bellefair”等等。其中一些字体似乎确实适用于 Android。

所以我猜那个演示文件中的列表只是旧的。也许存在许可问题或技术问题,因此有必要切换列表。

无论如何,提交一个包含更新和更新列表的拉取请求可能是值得的,该列表删除不再提供的字体并添加 API 确实提供的经过测试并已知可与提供商一起使用的字体。

于 2018-01-14T10:13:59.650 回答