Skip to content

Commit

Permalink
Merge branch 'master' into fixes_issue_230_p1
Browse files Browse the repository at this point in the history
  • Loading branch information
dalthviz authored Feb 19, 2025
2 parents a4fb3ec + 4298545 commit 0bbae75
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 18 deletions.
16 changes: 16 additions & 0 deletions qtawesome/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
.. autosummary::
:toctree: _generate
get_fonts_info
install_bundled_fonts_system_wide
icon
load_font
Expand Down Expand Up @@ -80,6 +81,21 @@
}


def get_fonts_info():
"""
Return tuple with information about the bundled fonts being used.
Notes
-----
The tuple contains the fonts directory path and a list of font filenames.
"""
fonts_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)), "fonts")
return (
fonts_directory,
[entry for entry in os.listdir(fonts_directory) if entry.endswith(".ttf")],
)


def install_bundled_fonts_system_wide():
_instance().install_fonts_system_wide()

Expand Down
13 changes: 12 additions & 1 deletion qtawesome/iconic_font.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,20 @@ def hook(obj):
if QApplication.instance() is not None:
with open(os.path.join(directory, ttf_filename), "rb") as font_data:
data = font_data.read()
id_ = QFontDatabase.addApplicationFontFromData(data)
id_ = (
-1
if os.environ.get("QTA_FORCE_SYSTEM_FONTS_LOAD") and os.name == "nt"
else QFontDatabase.addApplicationFontFromData(data)
)
font_data.close()

if id_ == -1 and os.name == "nt":
# Try to load font from system Fonts directory
windows_dir = os.environ.get("WINDIR", r"C:\Windows")
if os.path.isdir(windows_dir):
from_system_ttf = os.path.join(windows_dir, "Fonts", ttf_filename)
id_ = QFontDatabase.addApplicationFont(from_system_ttf)

loadedFontFamilies = QFontDatabase.applicationFontFamilies(id_)

if loadedFontFamilies:
Expand Down
71 changes: 54 additions & 17 deletions qtawesome/tests/test_qtawesome.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,44 +64,81 @@ def test_bundled_font_user_installation():
"powershell.exe",
r'Get-ItemProperty "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"',
]
fonts_result = subprocess.run(
fonts_command, capture_output=True, check=True, text=True
).stdout
fonts_result = (
subprocess.run(fonts_command, capture_output=True, check=True, text=True)
.stdout.replace("\n", "")
.replace(" ", "")
)
for font_filename in fonts_expected:
assert font_filename in fonts_result


def test_get_fonts_info():
"""
Test that you can get the info of all the bundled fonts.
"""
fonts_expected = [
font_filename
for _prefix, font_filename, _charmap_filename in qta._BUNDLED_FONTS
]
fonts_root_dir, fonts_list = qta.get_fonts_info()
assert os.path.normcase(fonts_root_dir) == os.path.normcase(
os.path.join(
os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "fonts"
)
)
assert set(fonts_list) == set(fonts_expected)


@pytest.mark.skipif(os.name != "nt", reason="Only meant for Windows")
def test_bundled_font_system_installation():
"""
Test that the bundled fonts can be installed on Windows for all users.
See spyder-ide/qtawesome#167 and spyder-ide/spyder#18642
See spyder-ide/qtawesome#244
Notes
-----
* When running this test, it's possible that a prompt for privileges
may appear (UAC prompt).
"""
qta.install_bundled_fonts_system_wide()
fonts_expected = [
"FontAwesome",
"codicon",
"elusiveicons",
"Font Awesome 5 Brands Regular",
"Font Awesome 5 Free Regular",
"Font Awesome 5 Free Solid",
"Material Design Icons 5.9.55 Regular",
"Material Design Icons",
"Phosphor",
"remixicon",
font_filename
for _prefix, font_filename, _charmap_filename in qta._BUNDLED_FONTS
]
assert len(fonts_expected) == len(qta._BUNDLED_FONTS)
fonts_command = [
"powershell.exe",
r'Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"',
]
fonts_result = subprocess.run(
fonts_command, capture_output=True, check=True, text=True
).stdout
fonts_result = (
subprocess.run(fonts_command, capture_output=True, check=True, text=True)
.stdout.replace("\n", "")
.replace(" ", "")
)
for font_filename in fonts_expected:
assert font_filename in fonts_result


def test_font_load_from_system_fonts(monkeypatch):
"""
Test that the bundled fonts can be accessed from the system fonts folder on
Windows.
Notes
-----
* This test ensures that the logic to load fonts from the system fonts
only affects Windows even when it is being forced.
* When running this test, it's possible that a prompt for privileges may
appear (UAC prompt).
"""
qta.install_bundled_fonts_system_wide()
with monkeypatch.context() as m:
qta._resource["iconic"] = None
m.setenv("QTA_FORCE_SYSTEM_FONTS_LOAD", "true")
qta._instance()


if __name__ == "__main__":
pytest.main()

0 comments on commit 0bbae75

Please sign in to comment.