comparison engine/python/fife/extensions/pychan/fonts.py @ 378:64738befdf3b

bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author vtchill@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 11 Jan 2010 23:34:52 +0000
parents
children
comparison
equal deleted inserted replaced
377:fe6fb0e0ed23 378:64738befdf3b
1 # -*- coding: utf-8 -*-
2
3 # ####################################################################
4 # Copyright (C) 2005-2009 by the FIFE team
5 # http://www.fifengine.de
6 # This file is part of FIFE.
7 #
8 # FIFE is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the
20 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # ####################################################################
23
24 # Font handling
25 from exceptions import *
26
27 class Font(object):
28 def __init__(self,name,get):
29 from internal import get_manager
30 self.font = None
31 self.name = name
32 self.typename = get("type")
33 self.source = get("source")
34 self.row_spacing = int(get("row_spacing",0))
35 self.glyph_spacing = int(get("glyph_spacing",0))
36
37 if self.typename == "truetype":
38 self.size = int(get("size"))
39 self.antialias = int(get("antialias",1))
40 self.color = map(int,get("color","255,255,255").split(','))
41 self.font = get_manager().hook.engine.getGuiManager().createFont(self.source,self.size,"")
42
43 if self.font is None:
44 raise InitializationError("Could not load font %s" % name)
45
46 self.font.setAntiAlias(self.antialias)
47 self.font.setColor(*self.color)
48 else:
49 raise InitializationError("Unsupported font type %s" % self.typename)
50
51 self.font.setRowSpacing( self.row_spacing )
52 self.font.setGlyphSpacing( self.glyph_spacing )
53
54 @staticmethod
55 def loadFromFile(filename):
56 """
57 Static method to load font definitions out of a PyChan config file.
58 """
59 import ConfigParser
60
61 fontdef = ConfigParser.ConfigParser()
62 fontdef.read(filename)
63
64 sections = [section for section in fontdef.sections() if section.startswith("Font/")]
65
66 fonts = []
67 for section in sections:
68 name = section[5:]
69 def _get(name,default=None):
70 if fontdef.has_option(section,name):
71 return fontdef.get(section,name)
72 return default
73 fonts.append( Font(name,_get) )
74 return fonts
75
76 def __str__(self):
77 return "Font(source='%s')" % self.source
78
79 def __repr__(self):
80 return "<Font(source='%s') at %x>" % (self.source,id(self))
81
82 def loadFonts(filename):
83 """
84 Load fonts from a config file. These are then available via their name.
85 """
86 from internal import get_manager
87
88 for font in Font.loadFromFile(filename):
89 get_manager().addFont(font)