comparison engine/extensions/pychan/fonts.py @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 9a1529f9625e
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 # coding: utf-8
2
3 # Font handling
4 from exceptions import *
5
6 class Font(object):
7 def __init__(self,name,get):
8 from manager import Manager
9 self.font = None
10 self.name = name
11 self.typename = get("type")
12 self.source = get("source")
13 self.row_spacing = int(get("row_spacing",0))
14 self.glyph_spacing = int(get("glyph_spacing",0))
15
16 if self.typename == "truetype":
17 self.size = int(get("size"))
18 self.antialias = int(get("antialias",1))
19 self.color = map(int,get("color","255,255,255").split(','))
20 self.font = Manager.manager.guimanager.createFont(self.source,self.size,"")
21
22 if self.font is None:
23 raise InitializationError("Could not load font %s" % name)
24
25 self.font.setAntiAlias(self.antialias)
26 self.font.setColor(*self.color)
27 else:
28 raise InitializationError("Unsupported font type %s" % self.typename)
29
30 self.font.setRowSpacing( self.row_spacing )
31 self.font.setGlyphSpacing( self.glyph_spacing )
32
33 @staticmethod
34 def loadFromFile(filename):
35 """
36 Static method to load font definitions out of a PyChan config file.
37 """
38 import ConfigParser
39
40 fontdef = ConfigParser.ConfigParser()
41 fontdef.read(filename)
42
43 sections = [section for section in fontdef.sections() if section.startswith("Font/")]
44
45 fonts = []
46 for section in sections:
47 name = section[5:]
48 def _get(name,default=None):
49 if fontdef.has_option(section,name):
50 return fontdef.get(section,name)
51 return default
52 fonts.append( Font(name,_get) )
53 return fonts
54
55 def __str__(self):
56 return "Font(source='%s')" % self.source
57
58 def __repr__(self):
59 return "<Font(source='%s') at %x>" % (self.source,id(self))
60
61 def loadFonts(filename):
62 """
63 Load fonts from a config file. These are then available via their name.
64 """
65 from manager import Manager
66
67 for font in Font.loadFromFile(filename):
68 Manager.manager.addFont(font)
69