1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 from exceptions import *
26
27 -class Font(object):
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().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
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
77 return "Font(source='%s')" % self.source
78
80 return "<Font(source='%s') at %x>" % (self.source,id(self))
81
90