pygame.error: Text has zero width
Closed this issue · 11 comments
When I use small font_size
in pygame.font.SysFont()
, error happeds as following. Now the version of pygame is 2.0.3
.
Python 3.6.9 |Anaconda, Inc.| (default, Jul 30 2019, 13:42:17)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
pygame 2.0.3 (SDL 2.0.16, Python 3.6.9)
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> pygame.init()
(5, 0)
>>> font = pygame.font.SysFont('arial', 6, True)
>>> txt = font.render('A', True, (255,255,255))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pygame.error: Text has zero width
>>> font = pygame.font.SysFont('arial', 8, True)
>>> txt = font.render('A', True, (255,255,255))
>>>
But when I use pygame==2.0.0
, the above error will not appear. My environment is:
- system version: Mac 10.15.6
- pygame version: 2.0.0 (work) & 2.0.3 (not work)
- python version: 3.6.9
I have tested and reproduced on my Intel MacBook Pro (running 12.0.1) with the following results:
Pygame 2.0.1 and below - issue does not occur
Pygame 2.0.2 and above - issue does occur (tested on both Python 3.9 and 3.10)
Also, this occurs with any font size lower than 8.
Tested on Windows 10 and I cannot reproduce using different versions of python/pygame so would indicate this is specific to Mac.
Since this is a mac sysfont regression, naturally, it's most natural to suspect my PR changing how mac sysfont works. (To fix it taking 20 seconds to initialize on most systems).
But the error message makes me think it might be a different version of freetype interacting differently with SDL_ttf.
All my Sysfont fix should've done, at most, is change the font file fetched for "arial." Does pygame.font.match_font("arial")
return something different on the different pygame versions? Does this happen with other fonts, from the system or not?
pygame.font.match_font("arial")
return something different on the different pygame versions?
Yes, there are different output on the different pygame versions.
>>> pygame.__version__
'2.0.3'
>>> pygame.font.match_font("arial")
'/System/Library/Fonts/Helvetica.ttc'
>>> pygame.__version__
'2.0.0'
>>> pygame.font.match_font("arial")
'/System/Library/Fonts/Supplemental/Arial.ttf'
Also, I try on other fonts and the issue only happens with arial
.
>>> font = pygame.font.SysFont('arial', 6, True)
>>> txt = font.render('A', True, (255,255,255))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pygame.error: Text has zero width
>>> font = pygame.font.SysFont('sfnstextcondensed', 6, True)
>>> txt = font.render('A', True, (255,255,255))
>>> font = pygame.font.SysFont('menlo', 6, True)
>>> txt = font.render('A', True, (255,255,255))
>>> font = pygame.font.SysFont('applesdgoth')
>>> txt = font.render('A', True, (255,255,255))
>>>
I also try to get the match_font
with other font, and menlo
as an example:
>>> pygame.__version__
'2.0.3'
>>> pygame.font.match_font("menlo")
'/System/Library/Fonts/Menlo.ttc'
>>> pygame.__version__
'2.0.0'
>>> pygame.font.match_font("menlo")
'/System/Library/Fonts/Menlo.ttc'
It looks like that the font file causes this issue?
In 2.0.3 it looks like it can't find Arial, but it knows Helvetica is close.
If you try to render with Helvetica, that would crash on both versions?
Here's my current theory:
- The way it finds font files was changed (by me, #2594)
- It can't find Arial, so it uses a Helvetica file. Because SysFont has an alias system, and the two are grouped together.
https://github.com/pygame/pygame/blob/main/src_py/sysfont.py#L300-L303 - The Helvetica file is always "broken" like this
If that's right, it seems like the font directories I programmed it to look in need to be expanded. This code describes the locations it looks currently: https://github.com/pygame/pygame/blob/main/src_py/sysfont.py#L184-L196. In theory, this would be fixed by adding /System/Library/Fonts/Supplemental
to the list of locations. This is actually something you could put in as a PR.
It's been a while, but I believe I used the "Font book" app on MacOS to see what locations had fonts in them, but it must be different between systems and between OS versions. So you could go through your "Font book" app, or use system_profiler -NSFontDataType
or something (I don't remember which I used) to see all fonts in your system, and add any font directory paths that I didn't.
Also thanks for going to the trouble to report this issue.
Looks like there is another font path there to search on Mac. Though I
guess with the problem last time l, of it taking forever to look through
font directories, not searching 'fonts/supplemental' is probably
intentional.
The previous implementation used a system call which could be really slow, system_profiler
. The new approach has a catalog of directories to search, and it looks like this one needs to be added. It's not exempt from the search intentionally.
I add /System/Library/Fonts/Supplemental
in https://github.com/pygame/pygame/blob/main/src_py/sysfont.py#L184-L196 and it works. arial
finds the correct font file in the given path.
>>> import pygame
pygame 2.0.3 (SDL 2.0.16, Python 3.6.9)
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> pygame.init()
(5, 0)
>>> font = pygame.font.SysFont('arial', 6, True)
>>> txt = font.render('A', True, (255,255,255))
>>> pygame.font.match_font("arial")
'/System/Library/Fonts/Supplemental/Arial.ttf'
>>>
But when it comes to helvetica
, though it finds a related font file, the error still happpens when I set font_size=6
.
>>> font = pygame.font.SysFont('helvetica', 6, True)
>>> txt = font.render('A', True, (255,255,255))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pygame.error: Text has zero width
>>> pygame.font.match_font("helvetica")
'/System/Library/Fonts/Helvetica.ttc'
>>> font = pygame.font.SysFont('helvetica', 8, True)
>>> txt = font.render('A', True, (255,255,255))
>>>
@mingzhang96 would you like to add this fix to pygame yourself by opening a pull request?
Also, in the Font Book app on MacOS, you can go through your fonts and check there aren't any other unsearched font directories on your OS and system.
The problem with Helvetica is something deep within SDL_TTF, and I think we shouldn't worry about it too much.
@Starbuck5 So Sorry to reply so late. Please check the pr. I add a new path in sysfont.py
to avoid this issue.