Slithy home


working with fonts

Slithy can use TrueType (*.ttf) and Type 1 (*.pfb) fonts to render text. When a font is loaded (with the load_font() or search_font() functions), Slithy creates a texture map with one instance of each character rasterized at the given size (which usually looks like this) and along with a table giving the coordinates of each character in the texture map. Slithy can then render text on the screen by drawing rectangles with the appropriate character textures.

Distributing presentations along with fonts is problematic, since you can't legally send out copies of your TTF and PFB files. I've been kind of ignoring the issue with the sample presentations, using commonly available font files that I think most people already have. Some people have asked about this, though, so now there's a better mechanism.

Slithy can now save the texture map and coordinate table it creates when loading a font, and load them back in just like loading the original font file. These files don't contain character outlines or hinting data, so I believe it should be perfectly legal to redistribute them. It's just like distributing a piece of paper with the characters printed out on it.

load_font() and search_font() now come in two flavors:

load_font( 'foo.pfb', 50 )     # with pixel size, load a TTF or PFB file
load_font( 'foo.slf' )         # without, load a "Slithy font"
The new function create_slithy_fonts() is used to make a Slithy font from a TTF or PFB file (at a particular pixel size).

managing fonts and images

I strongly recommend that if you have a Slithy project which uses multiple fonts and/or images, you organize it in the following way. Create a single Python file (I call mine resources.py) which loads all the fonts and images, and puts them into dictionaries. Here's a sample (the numbers refer to the comments below):
from slithy.library import *

fontpath.append( '.' )                                         # 1
add_extra_characters( u'\u201c\u201d' )                        # 2

create_slithy_fonts( ('times.ttf', 50, 'times.slf'),           # 3
                     ('ehrhardt.pfb', 50, 'ehrhardt.slf'),
                     ('mob.pfb', 50, 'minion.slf'),
                     ('mobi.pfb', 50, 'minion_i.slf'),
                     )
                     
fonts = { 'times' : search_font( 'times.slf' ),                # 4
          'ehrhardt' : search_font( 'ehrhardt.slf' ),
          'body' : search_font( 'minion.slf' ),
          'bodyi' : search_font( 'minion_i.slf' ),
          }

imagepath.append( '.' )                                        # 5

images = { 'tanguy' : search_image( 'tanguy.jpg' ),            # 6
           }

  1. The library object fontpath is a list of directories that search_font() will look in.
  2. To conserve texture memory, normally Slithy only loads characters in the Unicode range U+0000 to U+00ff. Here I'm requesting that it also look for the characters U+201c and U+201d as well. If they are present in the font, they'll be loaded.
  3. The arguments to create_slithy_fonts() are zero or more 3-tuples, where each one is "(original font file, pixelsize, output Slithy font file)".
  4. Here I'm putting all my font objects into a dictionary, so I can refer to them by a convenient name. When I want to show some body text, for instance, I'll use the font "fonts['body']". This makes it easy to keep a consistent style throughout the presentation and make global style changes.
  5. imagepath : search_image :: fontpath : search_font
  6. I'm doing the same thing for images as for fonts, putting them all into one dictionary so I can refer to them by name.

When I run this file directly (with "python resources.py"), the create_slithy_fonts() call will create the requested font files and it will not return. It will look like this:

% python resources.py
creating 'times.slf' from 'times.ttf' (at 50 pixels)
creating 'ehrhardt.slf' from 'ehrhardt.pfb' (at 50 pixels)
creating 'minion.slf' from 'mob.pfb' (at 50 pixels)
creating 'minion_i.slf' from 'mobi.pfb' (at 50 pixels)
4 slithy fonts created.
% 

Of course, you will need to have the TTF and PFB files available to do this. (This won't work on the samples in the archive because these files aren't included.)

To make use of the things I've loaded in my animations and diagrams, I need to do something like this:

from slithy.library import *
from resources import fonts, images

. . .

def some_animation():
    tx = Text( ..., font = fonts['body'], ... )
    . . .

When resources.py is loaded this way, from within another file, create_slithy_fonts() will do nothing. Only the SLF files are needed to view the presentation.