diff options
Diffstat (limited to 'savewebfonts_lib.py')
-rwxr-xr-x | savewebfonts_lib.py | 47 |
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}") |