Pygame Python Font Size

Viewed 654

So today i posted a question regarding pygame and i got the answer i had to use a function in pygame called pygame.font.Font.size() i went to the documentation of pygame and found this function but i did not understand what it meant by the documentation i guess i understood i should give a parameter as a string and it gives the height and width of my string if i am going to render it as a text I know i am not able to explain it clearly but i will try my best.

Here is some code to explain it:

import pygame
pygame.init()
width = 800
height = 600
a = [""]
win = pygame.display.set_mode((width, height))
font = pygame.font.Font("freesansbold.ttf", 32)
run = True

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

    for i in "hello how are you?":
        a[0] = a[0] + i
        text_1 = font.render(a[0], True, (255, 255, 255))
        win.fill((0, 0, 0))
        win.blit(text_1, (0, 0))
        print(font.size(i))
        pygame.display.update()
    a[0] = ''
    run = False
pygame.quit()

I am sorry if you are not able to understand. My question is : If you run this it runs for a second and goes away but if you look at the console you see some tuple values. According to me the first tuple's first value is that is if I render the letter h on my screen the width of the letter is the first tuples first element and the height of h is the first tuples second element. To the second it would be the same thing but just for the second tuple Apply the process for the question "hello how are you?". if you notice the width of some charecter's is different. But my question is why for all elememts the height is the same? The height of the letter h and the height of the letter e is not the same they why the console is giving me aways the same height?

1 Answers

Pygame font objects are bitmap fonts. Each glyph is represented with a bitmap with the height font specified when the pygame.font.Font object is created. e.g. 32:

font = pygame.font.Font("freesansbold.ttf", 32)

The width of the glyphs is different. pygame.font.Font.size returns the sum of the width of all glyphs. The height is always the height of the font. However, the letter itself may only cover a part of the bitmap.

You can investigate this, by getting the bounding rectangle of a glyph:

glyph_rect = pygame.mask.from_surface(text_1).get_bounding_rects()[0]

e.g.:

import pygame
pygame.init()
win = pygame.display.set_mode((800, 600))
font = pygame.font.Font("freesansbold.ttf", 32)
i = 0
text = "hello how are you?"
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    letter = text[i]
    text_1 = font.render(letter, True, (255, 255, 255))
        
    bw, bh = font.size(letter)
    glyph_rect = pygame.mask.from_surface(text_1).get_bounding_rects()
    if glyph_rect:
        gh = glyph_rect[0].height
        print(f'letter {letter}  bitmap height: {bh}  glyph height: {gh}')

    win.fill((0, 0, 0))
    win.blit(text_1, (0, 0))
    pygame.display.update()

    i += 1
    run = i < len(text)

pygame.quit()

Output:

letter h  bitmap height: 32  glyph height: 23
letter e  bitmap height: 32  glyph height: 17
letter l  bitmap height: 32  glyph height: 23
letter l  bitmap height: 32  glyph height: 23
letter o  bitmap height: 32  glyph height: 17
letter h  bitmap height: 32  glyph height: 23
letter o  bitmap height: 32  glyph height: 17
letter w  bitmap height: 32  glyph height: 17
letter a  bitmap height: 32  glyph height: 17
letter r  bitmap height: 32  glyph height: 17
letter e  bitmap height: 32  glyph height: 17
letter y  bitmap height: 33  glyph height: 24
letter o  bitmap height: 32  glyph height: 17
letter u  bitmap height: 32  glyph height: 17
letter ?  bitmap height: 32  glyph height: 18
Related