Skip to content

Commit

Permalink
Merge pull request #7578 from radarhere/font
Browse files Browse the repository at this point in the history
Handle pathlib.Path in FreeTypeFont
  • Loading branch information
radarhere committed Dec 6, 2023
2 parents 7d892d3 + d042c4b commit e43dd66
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
6 changes: 4 additions & 2 deletions Tests/test_imagefont.py
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
19 changes: 9 additions & 10 deletions docs/reference/ImageFont.rst
Expand Up @@ -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

Expand Down
17 changes: 15 additions & 2 deletions src/PIL/ImageFont.py
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit e43dd66

Please sign in to comment.