-
Notifications
You must be signed in to change notification settings - Fork 88
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
base: master
Are you sure you want to change the base?
Be able to load a Face from a non-ASCII filename on Windows #177
Conversation
Looking at the code now, I think the problem is really that |
Honestly, I have no idea why this part of code is there. It seems to be for python 2 only and from what I can see, freetype-py need an python 3.7 or more
That's an alternative. I will implement it. |
Hmm, I think I see how that might come - freetype takes It is separate and different from the windows failure. |
ceeaa3b
to
83bf5d3
Compare
If a conversion fail, encode will raise an UnicodeEncodeError, so I don't see why we have an special case handling for python 2. I updated my branch with your recommandation. |
I'm not too fond of the constant you defined in ft_errors.py. I'd prefer to leave it as it was and use a comment when using erros 0x01. |
4c12be7
to
83bf5d3
Compare
I applied the requested corrections. |
You mean
|
# 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): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you need to add more values from upstream:
https://gitlab.freedesktop.org/freetype/freetype/-/blame/master/include/freetype/fterrdef.h#L61
Add more.
Most of the time you only care whether it is |
Could you let me know if there's anything I need to do for this PR to be accepted? |
I've just tried another strange solution for this: from ctypes import CDLL, c_ssize_t, c_char_p, c_wchar_p, create_string_buffer
# `c_ssize_t` is not in actual declaration, I use it to help express (size_t)(-1) in C.
crt = CDLL("msvcrt")
# size_t wcstombs( char *dst, const wchar_t *src, size_t len );
wcstombs = crt.wcstombs
wcstombs.restype = c_ssize_t
wcstombs.argtypes = (c_char_p, c_wchar_p, c_ssize_t)
# char* setlocale( int category, const char* locale );
setlocale = crt.setlocale
setlocale.restype = c_char_p
# replace `_init_from_file` with this function
def _init_from_file(self, library, face, index, path):
orig_lc = bytes(setlocale(0, None))
setlocale(0, b"")
u_filename = create_string_buffer(1024) # change the size if needed, `len(path) * 4` for example.
if wcstombs(u_filename, path, 1024) == -1:
raise Exception() # replace it with a better exception with message
error = FT_New_Face(library, u_filename, index, byref(face))
setlocale(0, orig_lc) # restore original locale
return error Currently I tested it with WINE 9.20 in Chinese locale and it works. I'm not sure if this works in other cases. |
Locale-related matter and file-system processing on wine are significantly different from genuine windows . While doing so is useful, testing against wine especially in those two areas can give essentially unrelated results compared to genuine windows, so does not indicate anything... |
I've just tried on my dual-boot Windows 11 after your reply. At least on my machine my patch made it instead of an error |
@HinTak Is there something I can do for this PR? |
Since we touched on testing, one useful (but quite substantial) task to do is to drive this new code though github's ci windows hosts, if it is possible to configure github's ci hosts to run in every (non-english) locale. I think that might be possible - or one can persuade github's ci system that having host configurable in every locale is a good thing. I am not too keen on adding large block of rarely(?) used code which is also difficult to test and verify. |
Is your goal to test this PR on every codepage? |
Not necessaily needed on every code page - I don't think every code page has a different windows locale? But if the test is automated, N code pages is really the same work as 2N code pages. |
A codepage can have many windows locale. See this stackoverflow post or even what I found out. From what I know, it isn't possible to change directly the ANSI Code page with a command or with a API. I also tried to use Set-WinSystemLocale with the command |
Should ping / re-activate this, or there abouts: https://github.com/orgs/community/discussions/68929 |
I should quantify my comment further: I am not too keen on adding large block of rarely(?) used code which affects one of the most frequently used code path (opening a file), which is also difficult to test and verify. I.e. it makes one of the most frequently used part more complicated, and penalises the English users (which is the majority of the users). You can argue that the slow-down is negligible; but nonetheless, the suggested change - even when it is completely bug-free and bullet-proof - is about slowing the majority of users down for a minority. |
I don't see how it penalises the English users. In term of performance, for ASCII filename, it is the same has before. |
This PR close #157
In brief, on Windows, FreeType doesn't support to create a Face with an non-ASCII filename.
To work around this problem, we load the font from memory.
Pillow use a similar approch.