Font file name containing unicode error

Issue #196 resolved
matham created an issue


I have an issue where when a filename contains unicode chars on windows 7, pygmae fails to read it. Following is code demonstrating the issue:

>>> import pygame
>>> pygame.font.init()
>>> filenameu = u'कीवी.ttf'
>>> filenameb = u'कीवी.ttf'.encode('utf-8')
>>> print os.path.exists(filenameu)
>>> print os.path.exists(filenameb)


pygame.font.Font(filenameu, 15)

results in:

Traceback (most recent call last):
  File "C:\", line 8, in <module>
    pygame.font.Font(filenameu, 15)
IOError: unable to read font file '????.ttf'


pygame.font.Font(filenameb, 15)

results in:

Traceback (most recent call last):
  File "C:\", line 9, in <module>
    pygame.font.Font(filenameb, 15)
IOError: unable to read font file 'कीवी.ttf'

Conclusion is that pygame doesn't support unicode filenames on windows. I also tried various encodings instead of utf8, but none worked.

Comments (21)

  1. Jason Marshall

    Which version of Python are you using? What is returned by sys.getfilesystemencoding()? Really old versions of Python 2.x on Windows required unicode_filename.encode('mbcs').

  2. matham reporter

    Thanks for the very quick response. Here's some more info and tests:

    >>> sys.version
    '2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]'
    >>> sys.getfilesystemencoding()
    >>> pygame.ver
    >>> filename_mbcs = u'कीवी.ttf'.encode('mbcs')
    >>> print os.path.exists(filename_mbcs)
    >>> pygame.font.Font(filename_mbcs, 15)
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
    IOError: unable to read font file '????.ttf'


  3. Jason Marshall

    My phone gets an e-mail whenever a new issue is submitted, so that's why you got a quick response. I can't replicate this bug from my phone though.

  4. Jason Marshall
    • marked as minor
    • changed component to font
    • changed milestone to 1.9.2

    Where can I get the font file you're using? I can rename any font with a Devanagari filename, but if there's also a problem with your file, it would be easier to troubleshoot with your file.

  5. Jason Marshall

    Actually, I am able to replicate the issue on Android. I'm using QPython, which includes pygame 1.9.1release. File system is utf-8.

    >>> fu=pygame.font.Font(fn2,12)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    UnicodeEncodeError: 'ascii' codec can't encode characters in position 29-32: ordinal not in range(128)

    I don't know if it's significant that the exception raised is different though.

    When I renamed the font file, I saw no error when I created the Font object.

  6. Jason Marshall

    Here is a work-around that didn't raise an exception in QPython. Pass Font a file object instead of a filename.

    # coding: utf-8
    import pygame
    fn1 = u'/storage/emulated/0/Download/kiwi.ttf'
    fn2 = u'/storage/emulated/0/Download/कीवी.ttf'
    f2o = open(fn2, 'rb')
    fu = pygame.font.Font(f2o, 15)
    surf = fu.render('test', 0, pygame.Color('blue'))
    print 0
  7. matham reporter

    Ok, that seems to make it work. Should I close the issue or should I wait to see if there's another solution?

    Also, I suppose I'd need to keep the handle to file open as long as that font is in use?

  8. Lenard Lindstrom

    When I added Unicode file name support to Pygame I clearly did no understand file names on Windows. I see now that Windows stores paths as UTF-16. So fopen does not work on Windows, of course. The _wfopen functions is needed instead. I will see what I can do. Luckily I can use the Python source code as a guide.

  9. Log in to comment