From 2e15dc9f534be4411e4eddbf3d85b2034384b6a3 Mon Sep 17 00:00:00 2001 From: mamg22 <45301823+mamg22@users.noreply.github.com> Date: Tue, 11 Jun 2024 22:00:08 -0400 Subject: [PATCH 01/10] Improve xdg directory support in Linux --- src/PIL/ImageFont.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index fa5608e6cd8..163bb700678 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -837,12 +837,19 @@ def freetype(font: StrOrBytesPath | BinaryIO | None) -> FreeTypeFont: if windir: dirs.append(os.path.join(windir, "fonts")) elif sys.platform in ("linux", "linux2"): - lindirs = os.environ.get("XDG_DATA_DIRS") - if not lindirs: - # According to the freedesktop spec, XDG_DATA_DIRS should - # default to /usr/share - lindirs = "/usr/share" - dirs += [os.path.join(lindir, "fonts") for lindir in lindirs.split(":")] + data_home = os.environ.get("XDG_DATA_HOME") + if not data_home: + # The freedesktop spec defines the following default directory for + # when XDG_DATA_HOME is unset or empty. This user-level directory + # takes precedence over system-level directories. + data_home = os.path.expanduser("~/.local/share") + dirs.append(os.path.join(data_home, "fonts")) + + 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" + dirs += [os.path.join(ddir, "fonts") for ddir in data_dirs.split(":")] elif sys.platform == "darwin": dirs += [ "/Library/Fonts", From 1175e53d53ccd1fa86a94b2f8d2365dbc0a3336a Mon Sep 17 00:00:00 2001 From: mamg22 <45301823+mamg22@users.noreply.github.com> Date: Tue, 11 Jun 2024 22:08:53 -0400 Subject: [PATCH 02/10] Set XDG_DATA_HOME on font tests --- Tests/test_imagefont.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 4398f8a3055..3cba726d64e 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -564,6 +564,7 @@ def loadable_font( # catching syntax like errors monkeypatch.setattr(sys, "platform", platform) if platform == "linux": + monkeypatch.setenv("XDG_DATA_HOME", "/home/__pillow__/.local/share") monkeypatch.setenv("XDG_DATA_DIRS", "/usr/share/:/usr/local/share/") def fake_walker(path: str) -> list[tuple[str, list[str], list[str]]]: From 00161099c7df3aa76bcbd7106aa144072b45a592 Mon Sep 17 00:00:00 2001 From: mamg22 <45301823+mamg22@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:36:14 -0400 Subject: [PATCH 03/10] Update docs for ImageFont.truetype [ci skip] --- src/PIL/ImageFont.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index 163bb700678..235a76325c1 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -775,10 +775,15 @@ def truetype( :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 - search in other directories, such as the :file:`fonts/` - directory on Windows or :file:`/Library/Fonts/`, - :file:`/System/Library/Fonts/` and :file:`~/Library/Fonts/` on - macOS. + search in other directories, such as: + + * The :file:`fonts/` directory on Windows, + * :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 index: Which font face to load (default is first available face). From ed828d23df58f8e59f69933b74db12d7fba87c8c Mon Sep 17 00:00:00 2001 From: mamg22 <45301823+mamg22@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:08:48 -0400 Subject: [PATCH 04/10] Update changes in release notes [ci skip] --- docs/releasenotes/10.4.0.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/releasenotes/10.4.0.rst b/docs/releasenotes/10.4.0.rst index 44727efd41f..0a54cc7a39a 100644 --- a/docs/releasenotes/10.4.0.rst +++ b/docs/releasenotes/10.4.0.rst @@ -42,6 +42,14 @@ The ``hints`` parameter in :py:meth:`~PIL.ImageDraw.getdraw()` has been deprecat API Changes =========== +ImageFont.truetype +^^^^^^^^^^^^^^^^^^ + +:py:function:`~PIL.ImageFont.truetype` now searches for fonts in the directory +indicated by the ``XDG_DATA_HOME`` in addition to those specified in ``XDG_DATA_DIRS``. +Additionally, the default for ``XDG_DATA_DIRS`` has been updated to match the +freedesktop spec. + TODO ^^^^ From 6cf08afb1587bfb148334907f91ae7dbe4d8a41b Mon Sep 17 00:00:00 2001 From: mamg22 <45301823+mamg22@users.noreply.github.com> Date: Thu, 13 Jun 2024 00:03:03 -0400 Subject: [PATCH 05/10] Fix broken reference in release notes [ci skip] Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- docs/releasenotes/10.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/releasenotes/10.4.0.rst b/docs/releasenotes/10.4.0.rst index 0a54cc7a39a..36c2758f9bb 100644 --- a/docs/releasenotes/10.4.0.rst +++ b/docs/releasenotes/10.4.0.rst @@ -45,7 +45,7 @@ API Changes ImageFont.truetype ^^^^^^^^^^^^^^^^^^ -:py:function:`~PIL.ImageFont.truetype` now searches for fonts in the directory +:py:func:`~PIL.ImageFont.truetype` now searches for fonts in the directory indicated by the ``XDG_DATA_HOME`` in addition to those specified in ``XDG_DATA_DIRS``. Additionally, the default for ``XDG_DATA_DIRS`` has been updated to match the freedesktop spec. From 48606afeb6e69483c329535106383c3dd79a323c Mon Sep 17 00:00:00 2001 From: mamg22 <45301823+mamg22@users.noreply.github.com> Date: Thu, 13 Jun 2024 00:12:07 -0400 Subject: [PATCH 06/10] Add missing wording for envvar in release notes [ci skip] --- docs/releasenotes/10.4.0.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/releasenotes/10.4.0.rst b/docs/releasenotes/10.4.0.rst index 36c2758f9bb..00506a094ae 100644 --- a/docs/releasenotes/10.4.0.rst +++ b/docs/releasenotes/10.4.0.rst @@ -45,10 +45,10 @@ API Changes ImageFont.truetype ^^^^^^^^^^^^^^^^^^ -:py:func:`~PIL.ImageFont.truetype` now searches for fonts in the directory -indicated by the ``XDG_DATA_HOME`` in addition to those specified in ``XDG_DATA_DIRS``. -Additionally, the default for ``XDG_DATA_DIRS`` has been updated to match the -freedesktop spec. +:py:func:`~PIL.ImageFont.truetype` now searches for fonts in the directory indicated +by the ``XDG_DATA_HOME`` envirnorment variable in addition to those specified in +``XDG_DATA_DIRS``. Additionally, the default for ``XDG_DATA_DIRS`` has been updated +to match the freedesktop spec. TODO ^^^^ From d56ffebf2a18ffd94043300418bcb98819a6a776 Mon Sep 17 00:00:00 2001 From: mamg22 <45301823+mamg22@users.noreply.github.com> Date: Thu, 13 Jun 2024 09:43:40 -0400 Subject: [PATCH 07/10] Fix typo in release notes [ci skip] Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- docs/releasenotes/10.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/releasenotes/10.4.0.rst b/docs/releasenotes/10.4.0.rst index 00506a094ae..1678b460c37 100644 --- a/docs/releasenotes/10.4.0.rst +++ b/docs/releasenotes/10.4.0.rst @@ -46,7 +46,7 @@ ImageFont.truetype ^^^^^^^^^^^^^^^^^^ :py:func:`~PIL.ImageFont.truetype` now searches for fonts in the directory indicated -by the ``XDG_DATA_HOME`` envirnorment variable in addition to those specified in +by the ``XDG_DATA_HOME`` environment variable in addition to those specified in ``XDG_DATA_DIRS``. Additionally, the default for ``XDG_DATA_DIRS`` has been updated to match the freedesktop spec. From f62796dadc35ed21ab5b06be1e655f283046c7cb Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 15 Jun 2024 19:35:46 +1000 Subject: [PATCH 08/10] Rearranged code --- src/PIL/ImageFont.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index 235a76325c1..cc00cdfb6a1 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -848,13 +848,15 @@ def freetype(font: StrOrBytesPath | BinaryIO | None) -> FreeTypeFont: # when XDG_DATA_HOME is unset or empty. This user-level directory # takes precedence over system-level directories. data_home = os.path.expanduser("~/.local/share") - dirs.append(os.path.join(data_home, "fonts")) + 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" - dirs += [os.path.join(ddir, "fonts") for ddir in data_dirs.split(":")] + xdg_dirs += data_dirs.split(":") + + dirs += [os.path.join(xdg_dir, "fonts") for xdg_dir in xdg_dirs] elif sys.platform == "darwin": dirs += [ "/Library/Fonts", From 8d14a452df0a6ab1264294b0b28b35032db4f791 Mon Sep 17 00:00:00 2001 From: mamg22 <45301823+mamg22@users.noreply.github.com> Date: Sat, 15 Jun 2024 19:50:10 -0400 Subject: [PATCH 09/10] Update test environment variable for ImageFont Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- Tests/test_imagefont.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 3cba726d64e..4e6ec86d9d7 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -564,7 +564,7 @@ def loadable_font( # catching syntax like errors monkeypatch.setattr(sys, "platform", platform) if platform == "linux": - monkeypatch.setenv("XDG_DATA_HOME", "/home/__pillow__/.local/share") + monkeypatch.setenv("XDG_DATA_HOME", os.path.expanduser("~/.local/share")) monkeypatch.setenv("XDG_DATA_DIRS", "/usr/share/:/usr/local/share/") def fake_walker(path: str) -> list[tuple[str, list[str], list[str]]]: From b2c4539cd99da978d523dcaf2d8577b6ee5a7934 Mon Sep 17 00:00:00 2001 From: mamg22 <45301823+mamg22@users.noreply.github.com> Date: Sat, 15 Jun 2024 19:51:59 -0400 Subject: [PATCH 10/10] Remove releate note for truetype() changes The proposed changes don't match match with previous release notes' meaning of "API Changes". --- docs/releasenotes/10.4.0.rst | 8 -------- 1 file changed, 8 deletions(-) diff --git a/docs/releasenotes/10.4.0.rst b/docs/releasenotes/10.4.0.rst index 1678b460c37..44727efd41f 100644 --- a/docs/releasenotes/10.4.0.rst +++ b/docs/releasenotes/10.4.0.rst @@ -42,14 +42,6 @@ The ``hints`` parameter in :py:meth:`~PIL.ImageDraw.getdraw()` has been deprecat API Changes =========== -ImageFont.truetype -^^^^^^^^^^^^^^^^^^ - -:py:func:`~PIL.ImageFont.truetype` now searches for fonts in the directory indicated -by the ``XDG_DATA_HOME`` environment variable in addition to those specified in -``XDG_DATA_DIRS``. Additionally, the default for ``XDG_DATA_DIRS`` has been updated -to match the freedesktop spec. - TODO ^^^^