-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Handle pathlib.Path in FreeTypeFont #7578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
ae7958f
9e8edb4
4bc3655
13c1d75
984700b
ea680d9
9a6c47a
d042c4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,12 +25,15 @@ | |
| # 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 . import Image | ||
| from ._util import is_directory, is_path | ||
|
|
@@ -185,7 +188,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 | None = None, | ||
| size: float = 10, | ||
| index: int = 0, | ||
| encoding: str = "", | ||
| layout_engine: int = None, | ||
| ) -> None: | ||
| # FIXME: use service provider instead | ||
|
|
||
| self.path = font | ||
|
|
@@ -213,6 +223,8 @@ def load_from_bytes(f): | |
| ) | ||
|
|
||
| if is_path(font): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm looking at Do we really want consider bytes as paths? I believe this is obsolete in Python3
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #258 was the original request for bytes as paths.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's reachable. The logic hasn't changed since If it is a path, then pass the path to the C layer. If we're on Windows and the path is not ASCII, then open a file pointer to the path and read the contents of the file pointer with
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I see. I believe there is misnaming here, since f is not a bytes, it's file object.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh right, I remember figuring that out after being confused in the past.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, I've pushed a commit. Testing in PyCharm, I find it also includes
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @radarhere I see you've used Testing with both PyCharm and mypy, I find that a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I was concerned about strings being used, but looking now, that doesn't appear to be a problem. |
||
| if isinstance(font, Path): | ||
| font = str(font) | ||
hugovk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if sys.platform == "win32": | ||
| font_bytes_path = font if isinstance(font, bytes) else font.encode() | ||
| try: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.