Skip to content

Commit

Permalink
fix remote_styles condition
Browse files Browse the repository at this point in the history
googlefonts/conditions.py
  • Loading branch information
felipesanches committed Apr 2, 2024
1 parent f9c635e commit c030e23
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions Lib/fontbakery/checks/googlefonts/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,37 +284,33 @@ def remote_styles(font):
"""Get a dictionary of TTFont objects of all font files of
a given family as currently hosted at Google Fonts.
"""
if not font.context.network:
return

def download_family_from_Google_Fonts(familyname):
"""Return a zipfile containing a font family hosted on fonts.google.com"""
from zipfile import ZipFile
from fontbakery.utils import download_file

url_prefix = "https://fonts.google.com/download?family="
url = "{}{}".format(url_prefix, familyname.replace(" ", "+"))
return ZipFile(download_file(url)) # pylint: disable=R1732

def fonts_from_zip(zipfile):
"""return a list of fontTools TTFonts"""
from fontTools.ttLib import TTFont
from io import BytesIO

fonts = []
for file_name in zipfile.namelist():
if file_name.lower().endswith(".ttf"):
file_obj = BytesIO(zipfile.open(file_name).read())
fonts.append([file_name, TTFont(file_obj)])
return fonts
from fontbakery.utils import download_file
from fontTools.ttLib import TTFont
import json
import requests

if not font.listed_on_gfonts_api:
if not font.context.network or not font.listed_on_gfonts_api:
return None

remote_fonts_zip = download_family_from_Google_Fonts(font.familyname_with_spaces)
rstyles = {}
# download_family_from_Google_Fonts
dl_url = "https://fonts.google.com/download/list?family={}"
family = font.familyname_with_spaces
url = dl_url.format(family.replace(" ", "%20"))
data = json.loads(requests.get(url, timeout=10).text[5:])
remote_fonts = []
for item in data["manifest"]["fileRefs"]:
filename = item["filename"]
dl_url = item["url"]
if "static" in filename:
continue
if not filename.endswith(("otf", "ttf")):
continue
file_obj = download_file(dl_url)
if file_obj:
remote_fonts.append([filename, TTFont(file_obj)])

for remote_filename, remote_font in fonts_from_zip(remote_fonts_zip):
rstyles = {}
for remote_filename, remote_font in remote_fonts:
remote_style = os.path.splitext(remote_filename)[0]
if "-" in remote_style:
remote_style = remote_style.split("-")[1]
Expand Down

0 comments on commit c030e23

Please sign in to comment.