From ae7958fa8d5aa3cf852609dfa6e830c639417bc0 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 27 Nov 2023 23:03:10 +1100 Subject: [PATCH 1/6] Handle pathlib.Path in FreeTypeFont --- Tests/test_imagefont.py | 6 ++++-- src/PIL/ImageFont.py | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index db0df047f..47ba1756e 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -4,6 +4,7 @@ import re import shutil import sys from io import BytesIO +from pathlib import Path import pytest from packaging.version import parse as parse_version @@ -76,8 +77,9 @@ def _render(font, layout_engine): return img -def test_font_with_name(layout_engine): - _render(FONT_PATH, layout_engine) +@pytest.mark.parametrize("font", (FONT_PATH, Path(FONT_PATH))) +def test_font_with_name(layout_engine, font): + _render(font, layout_engine) def test_font_with_filelike(layout_engine): diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index c29562135..c3837545b 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -31,6 +31,7 @@ import sys import warnings from enum import IntEnum from io import BytesIO +from pathlib import Path from . import Image from ._util import is_directory, is_path @@ -213,6 +214,8 @@ class FreeTypeFont: ) if is_path(font): + if isinstance(font, Path): + font = str(font) if sys.platform == "win32": font_bytes_path = font if isinstance(font, bytes) else font.encode() try: From 9e8edb42b6d460f319d4fab207bbfcecb801a4d8 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 28 Nov 2023 21:18:58 +1100 Subject: [PATCH 2/6] Added type hint --- src/PIL/ImageFont.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index c3837545b..7edd33bcd 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -25,6 +25,8 @@ # See the README file for information on usage and redistribution. # +from __future__ import annotations + import base64 import os import sys @@ -186,7 +188,14 @@ class ImageFont: class FreeTypeFont: """FreeType font wrapper (requires _imagingft service)""" - def __init__(self, font=None, size=10, index=0, encoding="", layout_engine=None): + def __init__( + self, + font: bytes | str | Path | None = None, + size: float = 10, + index: int = 0, + encoding: str = "", + layout_engine: int = None, + ) -> None: # FIXME: use service provider instead self.path = font From 4bc365547ab46754a45823c63e153da5ad353fce Mon Sep 17 00:00:00 2001 From: Andrew Murray <3112309+radarhere@users.noreply.github.com> Date: Tue, 28 Nov 2023 21:43:58 +1100 Subject: [PATCH 3/6] Corrected type hint Co-authored-by: Hugo van Kemenade --- src/PIL/ImageFont.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index 7edd33bcd..f39a6760f 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -194,7 +194,7 @@ class FreeTypeFont: size: float = 10, index: int = 0, encoding: str = "", - layout_engine: int = None, + layout_engine: int | None = None, ) -> None: # FIXME: use service provider instead From 13c1d752c6d0933c31841a2adc5346839325f100 Mon Sep 17 00:00:00 2001 From: Andrew Murray <3112309+radarhere@users.noreply.github.com> Date: Fri, 1 Dec 2023 08:42:25 +1100 Subject: [PATCH 4/6] Use enum in type hint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ondrej Baranovič --- src/PIL/ImageFont.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index f39a6760f..6c9895d71 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -194,7 +194,7 @@ class FreeTypeFont: size: float = 10, index: int = 0, encoding: str = "", - layout_engine: int | None = None, + layout_engine: Layout | None = None, ) -> None: # FIXME: use service provider instead From 984700b1f7adf84c4b3f58e5b29fd5e075f068b8 Mon Sep 17 00:00:00 2001 From: Nulano Date: Thu, 30 Nov 2023 23:58:47 +0100 Subject: [PATCH 5/6] fix documentation link to PIL.ImageFont.Layout --- docs/reference/ImageFont.rst | 19 +++++++++---------- src/PIL/ImageFont.py | 2 +- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/docs/reference/ImageFont.rst b/docs/reference/ImageFont.rst index a944a13fa..6edf4b05c 100644 --- a/docs/reference/ImageFont.rst +++ b/docs/reference/ImageFont.rst @@ -70,21 +70,20 @@ Methods Constants --------- -.. data:: PIL.ImageFont.Layout.BASIC +.. class:: Layout - Use basic text layout for TrueType font. - Advanced features such as text direction are not supported. + .. py:attribute:: BASIC -.. data:: PIL.ImageFont.Layout.RAQM + Use basic text layout for TrueType font. + Advanced features such as text direction are not supported. - Use Raqm text layout for TrueType font. - Advanced features are supported. + .. py:attribute:: RAQM - Requires Raqm, you can check support using - :py:func:`PIL.features.check_feature` with ``feature="raqm"``. + Use Raqm text layout for TrueType font. + Advanced features are supported. -Constants ---------- + Requires Raqm, you can check support using + :py:func:`PIL.features.check_feature` with ``feature="raqm"``. .. data:: MAX_STRING_LENGTH diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index 6c9895d71..ec017d420 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -787,7 +787,7 @@ def truetype(font=None, size=10, index=0, encoding="", layout_engine=None): This specifies the character set to use. It does not alter the encoding of any text provided in subsequent operations. :param layout_engine: Which layout engine to use, if available: - :data:`.ImageFont.Layout.BASIC` or :data:`.ImageFont.Layout.RAQM`. + :attr:`.ImageFont.Layout.BASIC` or :attr:`.ImageFont.Layout.RAQM`. If it is available, Raqm layout will be used by default. Otherwise, basic layout will be used. From d042c4bc37234808858b15511d9cff67cb1be6a6 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 4 Dec 2023 22:26:14 +1100 Subject: [PATCH 6/6] Added typing.IO to type hint --- src/PIL/ImageFont.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index 217580594..f406bfc94 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -34,6 +34,7 @@ import warnings from enum import IntEnum from io import BytesIO from pathlib import Path +from typing import IO from . import Image from ._util import is_directory, is_path @@ -190,7 +191,7 @@ class FreeTypeFont: def __init__( self, - font: bytes | str | Path | None = None, + font: bytes | str | Path | IO | None = None, size: float = 10, index: int = 0, encoding: str = "",