Issue #196 new

Font file name containing unicode error

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 (13)

  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
    • changed milestone to 1.9.2
    • changed component to font
    • marked as minor

    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.

  4. 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.

  5. 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
  6. 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?

  7. Log in to comment