可下载的字体-无法下载某些Google字体 [英] downloadable fonts - can't download some google fonts
问题描述
我正在使用可下载的字体api .我下载了 Google示例应用程序,并且还将代码合并到了我的项目中.两者都可以成功运行,但是某些字体始终无法从我的应用程序和示例应用程序中下载.
I am playing with Downloadable fonts api. I downloaded the Google sample application and also incorporated the code into my project. Both run successfully but some fonts consistently fail to download both from my app and from the sample app.
我使用FontsContractCompat.requestFont
并由于原因1回调了onTypefaceRequestFailed(int reason)
.文档说它的意思是"FAIL_REASON_FONT_NOT_FOUND".
我认为这些字体应该存在,因为:1)它们出现在示例应用程序随附的xml文件中,2)它们出现在
I use FontsContractCompat.requestFont
and gets a callback to onTypefaceRequestFailed(int reason)
with reason 1. The documentation says it means "FAIL_REASON_FONT_NOT_FOUND".
I assume those fonts should exist because: 1) They appear in an xml file that comes with the sample app, 2) They appear in the online list of Google Fonts, and 3) They return from the developer 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
Here is the list of failed fonts:
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
推荐答案
这肯定很奇怪.我观察到,其中许多(但不是全部)字体没有"latin"或"latin-ext"子集,因此这似乎是一种自动过滤它们的方法.我整理了一个小的python2脚本,该脚本向API询问整个字体列表,然后过滤它们的拉丁"字样并输出剩下的内容作为新的字体系列资源文件,您可以将其重定向到family_names.xml
.
It's definitely weird. I observed that many (but not all) of those fonts don't have a "latin" or "latin-ext" subset, so that seemed a way to auto-filter them. I threw together a little python2 script that asks the API for the whole font list, then filters them for "latin" and outputs whats left as a new font-families resource file, which you can redirect to 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"字体.也许这些字体还有一些共同点,可以用来进一步过滤列表?
This script outputs a family_names.xml which is interesting. If you compare it to the one provided by google, it does black-out most of the fonts listed in the question. But it doesn't get all of them, including the "Zilla", "Ubuntu", "Barcode" and "Encode" fonts. Maybe there's something these fonts also have in common that can be used to filter the list further?
有趣的是,生成的列表还包括不在github列表中的新字体,包括:
Interestingly, the generated list also includes new fonts NOT in the github list, including:
- VolKorn SC
- 光谱
- 频谱SC
- Sedgewick Ave
- Sedgewick Ave Display
...."Barlow","Bellefair"等.而且其中某些字体似乎确实可以在Android上使用.
...."Barlow", "Bellefair", and a bunch more. And some of these fonts do seem to work with Android.
因此,我猜测该演示文件中的列表很旧.可能是由于许可问题或技术问题,因此有必要切换该列表.
So I'm guessing that the list in that demo file is just old. Maybe there were licensing issues or technical issues that made it necessary to switch up the list.
无论是否值得用更新和最新的列表提交拉取请求,以删除不再提供的字体,并添加经过API测试并已知可与之一起使用的字体.提供者.
Regardless it might be worth submitting a pull request with a newer and more up-to-date list that removes the no-longer provided fonts and adds in the ones that the API does offer that are tested and known to work with the provider.
这篇关于可下载的字体-无法下载某些Google字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!