downloadable fonts - can't download some googl

2019-05-11 04:11发布

问题:

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.

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=)

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

回答1:

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.

Usage: 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)

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?

Interestingly, the generated list also includes new fonts NOT in the github list, including:

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

...."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.

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.