tripl3dogdare
tripl3dogdare

Reputation: 65

Rendering unicode font with pygame

I'm currently attempting to render text read from a file onto the screen using pygame. However, when I attempt to render a non-standard Unicode character (i.e. Japanese hirigana), it responds only with an assortment of random characters and boxes. I've searched through everything I could find and nothing works. Any ideas? The file is properly encoded and read in UTF-8, so the problem is during the rendering stage.

My text rendering code:

def text(screen, pos, text='', font=None, color=BLACK, halign='LEFT', valign='TOP'):
    if font == None: font = pygame.font.Font(None,16)
    draw = font.render(text.encode('utf8'), True, color)
    pos = list(pos)

    if halign.upper() == 'CENTER': pos[0] -= font.size(text)[0]/2
    if halign.upper() == 'RIGHT': pos[0] -= font.size(text)[0]

    if valign.upper() == 'CENTER': pos[1] -= font.size(text)[1]/2
    if valign.upper() == 'BOTTOM': pos[1] -= font.size(text)[1]

    screen.blit(draw, pos)

The text I'm trying to render:

おはようございます、みんな!

The result: https://i.sstatic.net/88hBp.jpg

EDIT: After some testing, it turns out that it was the font I was using... Along those lines, does anyone know of an alternative to Segoe UI Light that has kana support?

Upvotes: 2

Views: 2176

Answers (1)

Ilario Pierbattista
Ilario Pierbattista

Reputation: 3265

On Debian, it's available setofont (fonts-seto)


    font_file = pygame.font.match_font("setofont")  # Select and 
    font = pygame.font.Font(font_file, 30)          # open the font
    writing = font.render(u"おはようございます、みんな!", True, (0, 0, 0))

screenshot

Upvotes: 1

Related Questions