Skip to content

Commit

Permalink
🔨 Improve the reliability of web font matching (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmdvs authored May 17, 2024
1 parent 6781109 commit 6449c86
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/converter/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def record_font(self, fig_font_name):
font_file, font_name = font.get_webfont(*font_descriptor)
except Exception as e:
if isinstance(e, font.FontNotFoundError) or (
isinstance(e, HTTPError) and e.code == 404
isinstance(e, HTTPError) and (e.code == 404 or e.code == 400)
):
logging.warning(f"Could not find font {font_descriptor} via Google Fonts")
else:
Expand Down
37 changes: 26 additions & 11 deletions src/converter/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import urllib.request
import urllib.parse
import json
import logging
from converter import utils
from fontTools.ttLib import TTFont
from sketchformat.document import FontReference, JsonFileReference
Expand All @@ -25,21 +24,37 @@ def retrieve_webfont_family(family):
return json.loads(bytearray(list_response.read())[5:])


def get_webfont(family, subfamily):
family_list = retrieve_webfont_family(family)
def download_webfonts(font_file_urls):
font_files = []

for fi in family_list["manifest"]["fileRefs"]:
filename = fi["filename"].replace("static/", "")
for fi in font_file_urls:
filename = fi["filename"].split("/")[-1]
if not filename.endswith((".ttf", ".otf")):
continue
if family.lower() in filename.lower() and subfamily.lower() in filename.lower():
font_file_path = f"{fonts_cache_dir}/{filename}"
if not os.path.exists(font_file_path):
urllib.request.urlretrieve(fi["url"], font_file_path)

font_file = open(font_file_path, "rb")
font_names = extract_names(font_file)
font_file_path = f"{fonts_cache_dir}/{filename}"
if not os.path.exists(font_file_path):
urllib.request.urlretrieve(fi["url"], font_file_path)

font_files.append(font_file_path)

return font_files


def get_webfont(family, subfamily):
family_list = retrieve_webfont_family(family)
font_files = download_webfonts(family_list["manifest"]["fileRefs"])

for font_file_path in font_files:
font_file = open(font_file_path, "rb")
font_names = extract_names(font_file)

if font_names["family"].lower() == family.lower() and (
font_names["subfamily"].lower() == subfamily.lower()
or font_names["subfamily"].replace(" ", "").lower()
== subfamily.replace(" ", "").lower()
):
font_file.seek(0)
return font_file, font_names["postscript"]

raise FontNotFoundError(f"Could not find font {family} {subfamily}")
Expand Down

0 comments on commit 6449c86

Please sign in to comment.