Merge pull request #8135 from mamg22/improved-xdg-dirs-support

Improve `ImageFont.freetype` support for XDG directories on Linux
This commit is contained in:
Andrew Murray 2024-06-22 14:27:55 +10:00 committed by GitHub
commit 1c8d270746
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 10 deletions

View File

@ -564,6 +564,7 @@ def test_find_font(
# catching syntax like errors # catching syntax like errors
monkeypatch.setattr(sys, "platform", platform) monkeypatch.setattr(sys, "platform", platform)
if platform == "linux": if platform == "linux":
monkeypatch.setenv("XDG_DATA_HOME", os.path.expanduser("~/.local/share"))
monkeypatch.setenv("XDG_DATA_DIRS", "/usr/share/:/usr/local/share/") monkeypatch.setenv("XDG_DATA_DIRS", "/usr/share/:/usr/local/share/")
def fake_walker(path: str) -> list[tuple[str, list[str], list[str]]]: def fake_walker(path: str) -> list[tuple[str, list[str], list[str]]]:

View File

@ -775,10 +775,15 @@ def truetype(
:param font: A filename or file-like object containing a TrueType font. :param font: A filename or file-like object containing a TrueType font.
If the file is not found in this filename, the loader may also If the file is not found in this filename, the loader may also
search in other directories, such as the :file:`fonts/` search in other directories, such as:
directory on Windows or :file:`/Library/Fonts/`,
:file:`/System/Library/Fonts/` and :file:`~/Library/Fonts/` on * The :file:`fonts/` directory on Windows,
macOS. * :file:`/Library/Fonts/`, :file:`/System/Library/Fonts/`
and :file:`~/Library/Fonts/` on macOS.
* :file:`~/.local/share/fonts`, :file:`/usr/local/share/fonts`,
and :file:`/usr/share/fonts` on Linux; or those specified by
the ``XDG_DATA_HOME`` and ``XDG_DATA_DIRS`` environment variables
for user-installed and system-wide fonts, respectively.
:param size: The requested size, in pixels. :param size: The requested size, in pixels.
:param index: Which font face to load (default is first available face). :param index: Which font face to load (default is first available face).
@ -837,12 +842,21 @@ def truetype(
if windir: if windir:
dirs.append(os.path.join(windir, "fonts")) dirs.append(os.path.join(windir, "fonts"))
elif sys.platform in ("linux", "linux2"): elif sys.platform in ("linux", "linux2"):
lindirs = os.environ.get("XDG_DATA_DIRS") data_home = os.environ.get("XDG_DATA_HOME")
if not lindirs: if not data_home:
# According to the freedesktop spec, XDG_DATA_DIRS should # The freedesktop spec defines the following default directory for
# default to /usr/share # when XDG_DATA_HOME is unset or empty. This user-level directory
lindirs = "/usr/share" # takes precedence over system-level directories.
dirs += [os.path.join(lindir, "fonts") for lindir in lindirs.split(":")] data_home = os.path.expanduser("~/.local/share")
xdg_dirs = [data_home]
data_dirs = os.environ.get("XDG_DATA_DIRS")
if not data_dirs:
# Similarly, defaults are defined for the system-level directories
data_dirs = "/usr/local/share:/usr/share"
xdg_dirs += data_dirs.split(":")
dirs += [os.path.join(xdg_dir, "fonts") for xdg_dir in xdg_dirs]
elif sys.platform == "darwin": elif sys.platform == "darwin":
dirs += [ dirs += [
"/Library/Fonts", "/Library/Fonts",