Python not finding fonts

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Prelates, Moderators General

Python not finding fonts

Postby Kerberos » Thu Aug 07, 2008 1:50 am UTC

(Maybe this belongs in Coding, but since it involves Python's interaction with the rest of my computer, I figured I'd try it here first, since it's not all about code? I'm bad when it comes to this sort of thing.)

I'm attempting to write a program in Pygame to make dropdown menus. This requires making text on the screen. So I'd like to be able to put text on the screen. Unfortunately, Python doesn't seem to be able to find its fonts. If I say "print pygame.font.get_fonts()", it replies "[None]". If I say "print pygame.font.get_default_font()", it tells me "freeansbold.ttf", but if I search my computer for the string "freeans", it doesn't find anything.

I know where my computer keeps some fonts (or at least a lot of .ttf files with names like "Arial" and "TimesRoman"), but I don't know how to tell Pygame to look there to find them. I don't even know where it's looking now. Does anyone know how to add a directory to the font-finding path or something?
User avatar
Kerberos
 
Posts: 189
Joined: Sun Oct 21, 2007 1:41 am UTC
Location: Male

Re: Python not finding fonts

Postby mat-tina » Thu Aug 07, 2008 6:44 am UTC

Are you sure it didn't say "freesansbold.ttf"? Mine is located along with pygame in /usr/lib/python2.5/site-packages/pygame/freesansbold.ttf

Regarding your question, it might be best to include the font when distributing your software, for platform independence:
Code: Select all
font_path = os.path.join(font_directory, 'freesansbold.ttf')
font = pygame.font.Font(default.font, f_size)


I have no clue why you can't find your fonts. What system are you using? What does pygame.font.match_font say for a font you know exists?
Felltir wrote:has no sig, and therefore something to hide
GENERATION n: The first time you see this, copy it into your sig on any forum. If n is an even number, divide it by 2. If it's odd, multiply it by 3 and add 1. Prove that this sequence converges to 1 for all n.
User avatar
mat-tina
 
Posts: 331
Joined: Mon Jun 02, 2008 3:33 pm UTC

Re: Python not finding fonts

Postby Kerberos » Thu Aug 07, 2008 4:34 pm UTC

Heh, yeah, it was 'freesansbold.ttf'. I do have that font, in 'lib' in 'pygame-1.8.0rc3' which is in a folder on my desktop. I had to build my Python and Pygame myself, so maybe things aren't where they're supposed to be.

I'm using OS X 10.4.9, and if I say "print pygame.font.match_font(pygame.font.get_default_font())", it returns 'None'. It does the same if I say "print pygame.font.match_font('/Application/OpenOffice.org1.0.3/share/fonts/truetype/ArialHB.ttf')".

If I could just find out where it's looking, I could copy all the fonts there. Is there a way to do that?
User avatar
Kerberos
 
Posts: 189
Joined: Sun Oct 21, 2007 1:41 am UTC
Location: Male

Re: Python not finding fonts

Postby mat-tina » Thu Aug 07, 2008 5:02 pm UTC

I might be wrong, but I don't think pygame would search for fonts in OpenOffice's directories. To load one of those fonts (not recommended), do:
Code: Select all
font = pygame.font.Font('/Application/OpenOffice.org1.0.3/share/fonts/truetype/ArialHB.ttf')


pygame.font.match_font works on the same syntax as returned by get_fonts, i.e. lowercase, unpunctuated and suffixless. Try pygame.font.match_font('freesansbold').

freesansbold.ttf should be in pygame's lib directory! That's the font it falls back to if no other fonts can be found on the system.

On my computer, it searches for fonts in /usr/share/fonts/corefonts/. I don't know if it's such a good idea to put stuff in there by hand.

Furthermore, why compile without installing (make install)? Do that and see if it works any better.
Felltir wrote:has no sig, and therefore something to hide
GENERATION n: The first time you see this, copy it into your sig on any forum. If n is an even number, divide it by 2. If it's odd, multiply it by 3 and add 1. Prove that this sequence converges to 1 for all n.
User avatar
mat-tina
 
Posts: 331
Joined: Mon Jun 02, 2008 3:33 pm UTC

Re: Python not finding fonts

Postby Kerberos » Thu Aug 07, 2008 10:44 pm UTC

pygame.font.match_font('freesansbold') returns None.

Eh, I don't know why I said to match a font in the OpenOffice thing. I guess I meant to leave out the "match" part. But if I do that code you gave me, it... doesn't complain about the font, at least. However, when I say to draw it on the screen, all of the letters are replaced by blank boxes, like "can't display this character."

And there weren't any versions of Pygame compatible with my version of Python running on my computer, so I couldn't just click a "download Pygame" link.
User avatar
Kerberos
 
Posts: 189
Joined: Sun Oct 21, 2007 1:41 am UTC
Location: Male

Re: Python not finding fonts

Postby mat-tina » Fri Aug 08, 2008 4:15 am UTC

What Python version? When I last was at a Mac, the preinstalled interpreter was 2.3 or something... I'm pretty sure there is a Pygame package for Python 2.5.

The only thing I know of that can cause boxes is escape sequences (and possibly non-ASCII, I'm not sure). I'm almost out of ideas. Sorry. Though, my far fetched solutions are: Install correctly and/or upgrade to the newest version.

One more thing: pygame.font depends on the SDL_ttf library. What is the version of that on your computer?
(
Code: Select all
$ locate libSDL_ttf
)

While we're at it, post the following:
Code: Select all
$ python --version                                       
Python 2.5.2
$ sdl-config --version                                 
1.2.11
$ python
>>> import pygame
>>> pygame.version.ver
'1.7.1release'
Felltir wrote:has no sig, and therefore something to hide
GENERATION n: The first time you see this, copy it into your sig on any forum. If n is an even number, divide it by 2. If it's odd, multiply it by 3 and add 1. Prove that this sequence converges to 1 for all n.
User avatar
mat-tina
 
Posts: 331
Joined: Mon Jun 02, 2008 3:33 pm UTC


Return to Coding

Who is online

Users browsing this forum: Yahoo [Bot] and 7 guests