9

How can I display Chinese characters in PyGame? And what's a good free/libre font to use for this purpose?

Red
  • 26,798
  • 7
  • 36
  • 58

3 Answers3

7

pygame uses SDL_ttf for rendering, so you should be in fine shape as rendering goes.

unifont.org appears to have some extensive resources on Open-Source fonts for a range of scripts.

I grabbed the Cyberbit pan-unicode font and extracted the encluded ttf. The folowing 'worked on my machine' which is a Windows Vista Home Basic and Python 2.6:

# -*- coding: utf-8 -*-

import pygame, sys


unistr = u"黒澤 明"
pygame.font.init()
srf = pygame.display.set_mode((640,480))
f = pygame.font.Font("Cyberbit.ttf",20)
srf.blit(f.render(unistr,True,(0,0,0)),(0,0))
pygame.display.flip()

while True:
    srf.blit(f.render(unistr,True,(255,255,255)),(0,0))
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

As long as you're just displaying unicode text, you should be in fantastic shape. If, however, you want to actually read unicode input from the user, the situation is much more bleak. Pygame has no input methods of any sort.

nbro
  • 15,395
  • 32
  • 113
  • 196
SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
  • Pygame _does_ have rudimentary support for events, which - if I am not mistaken - includes key stroke events. It's not as straightforward as calling `input()`, but it should be possible to build UI input elements on top of that. There also seems to be support for cutting and pasting via `pygame.scrap`. – SwiftsNamesake Jan 06 '15 at 12:51
  • @SwiftsNamesake, I'm not saying pygame cannot read input; rather, I'm saying it has no standard way of using the OS or any other [input methods](http://en.wikipedia.org/wiki/Input_method), the mappings from sequences of key codes to (say, unicode) script for a particular language. This is a concern principally for input languages other than English, like Chinese or Arabic. – SingleNegationElimination Jan 06 '15 at 13:41
  • I understand that. I just wanted to clarify that there are ways of circumventing this issue by implementing said features yourself. – SwiftsNamesake Jan 06 '15 at 13:52
0

Here is a list of built-in fonts in pygame that can render Chinese:

arialms
fzshuti
fzyaoti
lisu
malgungothic
microsoftjhengheimicrosoftjhengheiui
microsoftjhengheimicrosoftjhengheiuibold
microsoftjhengheimicrosoftjhengheiuilight
microsoftyaheimicrosoftyaheiui
microsoftyaheimicrosoftyaheiuibold
microsoftyaheimicrosoftyaheiuilight
msgothicmsuigothicmspgothic
simsunnsimsun
stcaiyun
stfangsong
sthupo
stkaiti
stliti
stsong
stxihei
stxingkai
stxinwei
stzhongsong
youyuan
yugothicmediumyugothicuiregular
yugothicregularyugothicuisemilight
yugothicyugothicuilight
yugothicyugothicuisemiboldyugothicuibold

Every one of the above fonts works with Chinese characters. Here is an example with "stsong":

import pygame

text = "中文"
font = "stsong"

pygame.font.init()
wn = pygame.display.set_mode((500, 500))
font = pygame.font.SysFont(font, 190)
wn.blit(font.render(text, True, (255, 255, 255)), (50, 50))
pygame.display.update()

Output:

enter image description here

Red
  • 26,798
  • 7
  • 36
  • 58
0

As far as I'm aware — and I haven't tried it myself — PyGame should Just Work when you pass it a Unicode string containing Chinese characters, eg. u'\u4e2d\u56fd'.

See ‘East Asia’ under http://www.unifont.org/fontguide/ for quite a few suitable open-licence fonts.

bobince
  • 528,062
  • 107
  • 651
  • 834