Skip to content
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

Be able to load a Face from a non-ASCII filename on Windows #177

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions freetype/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,9 +1182,20 @@ def __init__( self, path_or_stream, index = 0 ):
if hasattr(path_or_stream, "read"):
error = self._init_from_memory(library, face, index, path_or_stream.read())
else:
open_from_memory = False
try:
error = self._init_from_file(library, face, index, path_or_stream)

# FreeType cannot load fonts with non-ASCII characters on Windows.
# So if the file exist and freetype cannot open the file, we try to open it with memory.
# See: https://gitlab.freedesktop.org/freetype/freetype/-/issues/1098
# The error code 0x01 correspond to FT_Err_Cannot_Open_Resource
if error == 0x01 and os.path.isfile(path_or_stream):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

open_from_memory = True
except UnicodeError:
open_from_memory = True

if open_from_memory:
with open(path_or_stream, mode="rb") as f:
filebody = f.read()
error = self._init_from_memory(library, face, index, filebody)
Expand Down