diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index e21bf8cbf5c..0f1c52b664f 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -4,6 +4,7 @@ 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/docs/reference/ImageFont.rst b/docs/reference/ImageFont.rst index a944a13fa37..6edf4b05c55 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 18de103758c..f406bfc948f 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -25,12 +25,16 @@ # See the README file for information on usage and redistribution. # +from __future__ import annotations + import base64 import os import sys 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 @@ -185,7 +189,14 @@ def getlength(self, text, *args, **kwargs): 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 | IO | None = None, + size: float = 10, + index: int = 0, + encoding: str = "", + layout_engine: Layout | None = None, + ) -> None: # FIXME: use service provider instead if size <= 0: @@ -217,6 +228,8 @@ def load_from_bytes(f): ) 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: @@ -779,7 +792,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.