aboutsummaryrefslogtreecommitdiff
path: root/savewebfonts_lib.py
diff options
context:
space:
mode:
authorB. Stack <bgstack15@gmail.com>2021-04-06 11:58:04 -0400
committerB. Stack <bgstack15@gmail.com>2021-04-06 11:58:04 -0400
commitc87a6f1c31cda6b9692298feb9d6e956acacc696 (patch)
tree916e4ca18e4569f54ee3c87796103d729fa03411 /savewebfonts_lib.py
parentupdate readme (diff)
downloadsave-webfonts-c87a6f1c31cda6b9692298feb9d6e956acacc696.tar.gz
save-webfonts-c87a6f1c31cda6b9692298feb9d6e956acacc696.tar.bz2
save-webfonts-c87a6f1c31cda6b9692298feb9d6e956acacc696.zip
abstract some inline-font helper logic
Also handle some "non-woff" errors better. The file is a woff file, but the base64 conversion sometimes fails? It makes no sense.
Diffstat (limited to 'savewebfonts_lib.py')
-rwxr-xr-xsavewebfonts_lib.py47
1 files changed, 38 insertions, 9 deletions
diff --git a/savewebfonts_lib.py b/savewebfonts_lib.py
index 857dfc7..1046bf1 100755
--- a/savewebfonts_lib.py
+++ b/savewebfonts_lib.py
@@ -127,6 +127,32 @@ def get_webfonts_for_one_css(url, config):
# c is a flat list of all font files, many of which are duplicates
return c
+def save_inline_font_helper(intro,url):
+ """
+ Call this from save_font if url.startswith("data:")
+ Return need_convert, ext, tf, and filename
+ """
+ need_convert = False
+ ext = ""
+ tf = tempfile.NamedTemporaryFile()
+ if "/x-font-woff2;" in intro or "/font-woff2;" in intro:
+ need_convert = True
+ ext = ".woff2"
+ elif "/x-font-woff;" in intro or "/font-woff;" in intro:
+ need_convert = True
+ ext = ".woff"
+ elif "/x-font-ttf;" in intro or "/font-ttf;" in intro:
+ ext = ".ttf"
+
+ contents = url[len(intro):]
+ filename = ttfify_filename(contents[:20])
+ #print(f"TEMP, sifh: {contents}")
+ if ";base64," in intro:
+ contents = base64.b64decode(contents)
+ #print(f"TEMP, did the base64 conversion")
+ tf.write(contents)
+ return need_convert, ext, tf, filename
+
def save_font(url,destdir,config):
"""
Given a url, and destination dir, and optionally an existing http session, download the url and save to a file. If convert, save any woff/woff2 to ttf.
@@ -146,20 +172,19 @@ def save_font(url,destdir,config):
filename = ttfify_filename(filename)
if url.startswith("data:"):
- if url.startswith("data:application/x-font-woff;charset=utf-8;base64,"):
- need_convert = True
- ext = ".woff"
- tf = tempfile.NamedTemporaryFile()
- contents = url[len("data:application/x-font-woff;charset=utf-8;base64,"):] # no worries about dryrun; we have already downloaded the font contents which are inline in the css file itself.
- tf.write(base64.b64decode(contents))
- filename = ttfify_filename(contents[:20])
+ for fheader in ["data:application/x-font-woff;charset=utf-8;base64,","data:application/x-font-ttf;charset=utf-8;base64,","data:application/font-woff;charset=utf-8;base64,"]:
+ if url.startswith(fheader):
+ need_convert, ext, tf, filename = save_inline_font_helper(fheader, url)
+ #print(f"TEMP: FOR {url}, {need_convert} {ext} {tf} {filename}")
+ break
filepath = os.path.join(destdir, filename)
if not os.path.exists(filepath):
if url.startswith("data:"):
# Yes, some repetition here.
- if url.startswith("data:application/x-font-woff;charset=utf-8;base64,"):
+ #if url.startswith("data:application/x-font-woff;charset=utf-8;base64,"):
+ if tf is not None:
pass
else:
# not supported yet!
@@ -342,7 +367,11 @@ def convert_woffwoff2_ttf(url, filename, config):
with tempfile.TemporaryFile() as tf:
tf.write(file_contents)
- font = ttLib.TTFont(tf)
+ try:
+ font = ttLib.TTFont(tf)
+ except ttLib.TTLibError as e:
+ eprint(f"Warning: not a woff/woff2: {url[:config.MAX_STRING_PRINT_LENGTH]} for file {filename}")
+ return -1
if config.debuglevel >= 3:
eprint(f"Converting {url[:config.MAX_STRING_PRINT_LENGTH]} from {font.flavor} to ttf as file {filename}")
bgstack15