[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] BUG: SysFont('Verdana') gives me Bold+Italic variant on Unix



Hi,

I have a laptop running Ubuntu Breezy and pygame version 1.6.  I want to
use pygame.font.SysFont instead of specifying a font path manually.
However watch this:

  >>> import pygame
  >>> pygame.font.init()
  >>> pygame.font.match_font('Verdana')
  /usr/share/fonts/truetype/msttcorefonts/verdanaz.ttf
  >>> pygame.font.match_font('Verdana', bold=True)
  /usr/share/fonts/truetype/msttcorefonts/verdanaz.ttf
  >>> pygame.font.match_font('Verdana', italic=True)
  /usr/share/fonts/truetype/msttcorefonts/verdanaz.ttf
  >>> pygame.font.match_font('Verdana', bold=True, italic=True)
  /usr/share/fonts/truetype/msttcorefonts/verdanaz.ttf

All four requests returned the same font file name name!

/usr/share/fonts/truetype/msttcorefonts/ contains all four versions of
Verdana:

  Verdana.ttf
  Verdana_Bold.ttf
  Verdana_Italic.ttf
  Verdana_Bold_Italic.ttf

plus these symlinks

  verdana.ttf   -> Verdana.ttf
  verdanab.ttf  -> Verdana_Bold.ttf
  verdanai.ttf  -> Verdana_Italic.ttf
  verdanaz.ttf  -> Verdana_Bold_Italic.ttf

There are no fonts.dir nor fonts.scale files in this directory.  There
is a file named fonts.cache-1.

pygame/sysfont.py reads the fonts.cache-1 like this:

  def read_unix_fontscache(dir, file, fonts):
      file = open(os.path.join(dir, file))
      for line in file.readlines():
          try:
              font, num, vals = line.split(' ', 2)
          except ValueError:
              continue
          font = font.replace('"', '')
          if font[-4:].lower() != '.ttf':
              continue
          font = os.path.join(dir, font)
          vals = vals.split(':')
          name = _simplename(vals[0][1:])
          bold = vals[1].find('Bold') >= 0
          italic = vals[1].find('Italic') >= 0
          _addfont(name, bold, italic, font, fonts)

And herein lies the bug.  The relevant line from my fonts.cache-1 says
this about verdanaz.ttf:

  "verdanaz.ttf" 0 "Verdana:familylang=en:style=Bold Italic,[...]"

The code of read_unix_fontscache splits the font description into a vals
list

  vals[0] == '"Verdana'
  vals[1] == 'familylang=en'
  vals[2] == 'style=Bold Italic,[...]'

and then blindly assumes the second element in the list defines the font
style.  Since neither 'Bold' nor 'Italic' appear in vals[1],
read_unix_fontscache assumes verdanaz.ttf is the regular font variant.


Fix: replace the last few lines of the function with something like

          vals = vals.split(':')
          name = _simplename(vals[0][1:])
          bold = italic = 0
          for prop in vals[1:]:
              if prop.startswith('style='):
                  bold = prop.find('Bold') >= 0
                  italic = prop.find('Italic') >= 0
          _addfont(name, bold, italic, font, fonts)


In the meantime I'll probably resort to using pygame.font.match_font to
locate the font directory, and then splice the file names manually with
os.path.dirname/os.path.join.

Marius Gedminas
-- 
Computo, ergo sum.
		-- Curt Suplee

Attachment: signature.asc
Description: Digital signature