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

Font constructor now raises an error when it fails to load a valid font file #3064

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

oddbookworm
Copy link
Member

Sample piece of code:

import pygame

pygame.init()

win = pygame.Window()
screen = win.get_surface()

font = pygame.font.Font("MODERN.fnt", 20)
text = font.render("Hello World!", True, "white")

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill("black")
    screen.blit(text, (0, 0))
    win.flip()

Sample font file (same file is added in the examples dir in this pull):
MODERN.zip
Source for the sample file

@oddbookworm oddbookworm added the font pygame.font label Aug 19, 2024
@oddbookworm oddbookworm requested a review from a team as a code owner August 19, 2024 03:11
Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

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

Before this PR I get a pygame.error: Passed a NULL pointer error, after this PR I get pygame.error: Couldn't load font file which is much better OFC

Thanks! 💯

@ankith26 ankith26 added this to the 2.5.2 milestone Aug 20, 2024
# for that version of SDL_ttf)
font_path = os.path.join("examples", "data", "MODERN.fnt")
self.assertRaises(
(pygame.error, FileNotFoundError), lambda: pygame_font.Font(font_path)
Copy link
Member

Choose a reason for hiding this comment

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

How could this raise a FileNotFoundError?

pygame_font here could be ftfont based on freetype?
freetype.font segfaults on my system when I try to load this path, but maybe on some other systems it raises filenotfound.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I tracked down to where the segfault happens. Didn’t figure out exactly what went wrong, but the function call is here:
https://github.com/pygame-community/pygame-ce/blob/main/src_c/freetype/ft_wrap.c#L343

Copy link
Member Author

Choose a reason for hiding this comment

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

If it doesn’t segfault, freetype raises filenotfound

Copy link
Member

Choose a reason for hiding this comment

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

So pygame_font is either Font or ftfont, and this PR introduces a difference between the Font and ftfont APIs (which are supposed to match).

This is why I bring it up. The test shouldn't need to test for either behavior, it should test for the one correct behavior which should be shared.

@Starbuck5 Starbuck5 marked this pull request as draft October 13, 2024 07:04
@Starbuck5 Starbuck5 removed this from the 2.5.2 milestone Oct 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
font pygame.font
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants