source: trunk/game/font.py @ 828

Revision 828, 2.2 KB checked in by barra_parpg, 8 years ago (diff)

Patch by aspidites:

  • renamed all Directory options to Path for brevity and consistency
  • settings.py no longer sets absolute paths as this was crashing fife
  • settings.py no longer errors out on file read. instead, it errors out only if system.cfg isn't found and displays a useful help message
  • added a settings_path argument to settings.py to compensate for the fact that on linux, the settings file is not in the same location as the other data files
  • run.py no longer takes a -p option. instead, 1 to 3 filenames are taken as required arguments
  • subclassed pychan's Font object to create PARPGFont, which is arguably more flexible
  • instead of taking a fontdef file, it takes a settings object as well as a python dictionary called fontdef
  • created three fontdef modules in /fonts to assist in creation of the forementioned fontdef dictonaries.
  • one needs only copy the file, rename it, and change the name option to create a new fontdef
  • because of the above changes, the fontspath is not configurable
Line 
1import os
2
3from fife.extensions.pychan.fonts import Font
4from fife.extensions.pychan.internal import get_manager
5
6class PARPGFont(Font):
7    """ Font class for PARPG
8        This class behaves identical to PyChan's Font class except in
9        initialization. Ratherthan take a name and a get object, this class
10        takes a fontdef and settings object as explained below. This class is
11        necessary because the original Font class was too restrictive on how it
12        accepted objects
13
14        @param fontdef: defines the font's name, size, type, and optionally
15                        row spacing as well as glyph spacing.
16        @type fontdef: dictionary
17       
18        @param settings: settings object used to dynamically determine the
19                         font's source location
20        @type settings: parpg.settings.Settings object
21    """
22    def __init__(self, fontdef, settings):
23        self.font = None
24        self.name = fontdef['name']
25        self.typename = fontdef['typename']
26
27        if self.typename == 'truetype':
28            self.filename = '{0}.ttf'.format(self.name.lower().split('_')[0])
29
30        self.source = os.path.join(settings.system_path,
31                                   settings.fife.FontsPath,
32                                   self.filename)
33        self.row_spacing = fontdef.get('row_spacing', 0)
34        self.glyph_spacing = fontdef.get('glyph_spacing', 0)
35
36        if self.typename == 'truetype':
37            self.size = fontdef['size']
38            self.antialias = fontdef['antialias']
39            self.color = fontdef.get('color', [255, 255, 255])
40            manager = get_manager().hook.engine.getGuiManager()
41            self.font = manager.createFont(self.source, self.size, '')
42
43            if not self.font:
44                raise InitializationError('Could not load font '
45                                          '{0}'.format(self.name))
46       
47            self.font.setAntiAlias(self.antialias)
48            self.font.setColor(*self.color)
49        else:
50            raise InitializationError('Unsupported font type '
51                                      '{0}'.format(self.typename))
52
53        self.font.setRowSpacing(self.row_spacing)
54        self.font.setGlyphSpacing(self.glyph_spacing)
Note: See TracBrowser for help on using the repository browser.