-
-
Notifications
You must be signed in to change notification settings - Fork 109
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
PR: Return original font directory when font user installation load fails #273
Conversation
qtawesome/iconic_font.py
Outdated
# Regardless of the font file being actually removed | ||
# since the load failed we need to return here where | ||
# the font file is originally located. With that a | ||
# `FontError` can be eventually raised with useful |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of returning fonts_directory
below, why don't we raise a FontError
there, saying that it was not possible to load a specific font? The error message could also use get_fonts_info
to tell users what fonts they need to install manually and where they are located.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After giving it some thought, if we raise FontError
here the fallback logic to use system wide fonts will never be reached (since then always an error here will be raised). Maybe we should show instead a warning here and only in case we are unable to detect the font that failed being loaded is available from the system fonts directory? What do you think @ccordoba12 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last commit implements the idea above but let me know what you think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, that looks better to me. But then I think we don't need to return fonts_directory
below, but a simple return
would work as well. And the comment you added here should be reworded, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking, we need to return fonts_directory
, otherwise you will end up with a TypeError
around the code that loads the fonts at
qtawesome/qtawesome/iconic_font.py
Lines 404 to 416 in 507aa4e
if directory is None: | |
directory = self._get_fonts_directory() | |
# Load font | |
if QApplication.instance() is not None: | |
with open(os.path.join(directory, ttf_filename), "rb") as font_data: | |
data = font_data.read() | |
id_ = ( | |
-1 | |
if os.environ.get("QTA_FORCE_SYSTEM_FONTS_LOAD") and os.name == "nt" | |
else QFontDatabase.addApplicationFontFromData(data) | |
) | |
font_data.close() |
This error would raise since _get_fonts_directory
would end up returning None
due to _get_fonts_directory
currently being in charge of calling the install logic and returning the value given
qtawesome/qtawesome/iconic_font.py
Lines 655 to 668 in 507aa4e
def _get_fonts_directory(self): | |
""" | |
Get bundled fonts directory. | |
On Windows an attempt to install the fonts per user is done | |
to prevent errors with fonts loading. | |
See spyder-ide/qtawesome#167 and spyder-ide/spyder#18642 for | |
context. | |
""" | |
fonts_directory = os.path.join( | |
os.path.dirname(os.path.realpath(__file__)), "fonts" | |
) | |
return self._install_fonts(fonts_directory) |
Thinking about this, if we would want to be able to return None
over the _install_fonts
method, maybe we should prevent _get_fonts_directory
from returning None
? So maybe _get_fonts_directory
should look something like:
fonts_directory = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "fonts"
)
install_fonts_directory = self._install_fonts(fonts_directory)
return install_fonts_directory if install_fonts_directory else fonts_directory
?
Let me know what you think @ccordoba12
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I think it's fine to leave things as they are. I just noticed that _install_fonts
returns fonts_directory
in several places. So, let's not change that logic in this PR because it's not needed to fix issue #264.
I'll leave a suggestion to improve this comment, then this should be ready.
Sorry for closing this PR (I don't know why it happened). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some improvements to the comment and warning text, then this should be ready.
Co-authored-by: Carlos Cordoba <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me now, thanks @dalthviz!
Fixes #264