Skip to content

[WIP] PR: Fix crashes at startup on WSL because QtAwesome fonts are not available #283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions qtawesome/iconic_font.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import ctypes
import json
import os
import platform
import shutil
import warnings

Expand Down Expand Up @@ -679,15 +680,30 @@ def _install_fonts(self, fonts_directory, system_wide=False):
Based on https://stackoverflow.com/a/41841088/15954282 and
https://superuser.com/a/1663482
"""
if os.name != "nt":
isWSL = (
platform.system() == "Linux" and "microsoft" in platform.release().lower()
)

if os.name != "nt" and not (isWSL):
return fonts_directory

# Try to get WINDIR and LOCALAPPDATA path
windows_dir = os.environ.get("WINDIR", None)
local_appdata_dir = os.environ.get("LOCALAPPDATA", None)

if isWSL:
if windows_dir:
windows_dir = windows_dir.replace("\\", "/")
drive, path = windows_dir.split(":", 1)
windows_dir = f"/mnt/{drive.lower()}{path}"
if local_appdata_dir:
local_appdata_dir = local_appdata_dir.replace("\\", "/")
drive, path = local_appdata_dir.split(":", 1)
local_appdata_dir = f"/mnt/{drive.lower()}{path}"

if not windows_dir and system_wide:
return fonts_directory

local_appdata_dir = os.environ.get("LOCALAPPDATA", None)
if not local_appdata_dir and not system_wide:
return fonts_directory

Expand All @@ -699,7 +715,10 @@ def _install_fonts(self, fonts_directory, system_wide=False):
)
os.makedirs(user_fonts_dir, exist_ok=True)

install_fonts_dir = system_fonts_dir if system_wide else user_fonts_dir
if system_wide:
install_fonts_dir = system_fonts_dir
else:
install_fonts_dir = user_fonts_dir

# Setup bundled fonts on the WINDIR or LOCALAPPDATA fonts directory
for root, __, files in os.walk(fonts_directory):
Expand All @@ -715,6 +734,9 @@ def _install_fonts(self, fonts_directory, system_wide=False):

shutil.copy(src_path, dst_path)

if isWSL:
continue

# Further process the font file (`.ttf`)
if os.path.splitext(filename)[-1] == ".ttf":
# Load the font in the current session
Expand Down
Loading