1 """Some handy font-like objects.
3 Please note that this file is alpha, and is subject to modification in
4 future versions of pgu!
7 print('pgu.fonts - This module is alpha, and is subject to change.')
10 from pygame.locals import *
13 """Creates an instance of the TileFont class. Interface compatible
16 TileFonts are fonts that are stored in a tiled image. Where the image
17 opaque, it assumed that the font is visible. Font color is changed
18 automatically, so it does not work with fonts with stylized coloring.
21 size -- the dimensions of the characters
22 hints -- a string of hints "abcdefg..."
23 scale -- size to scale font to
24 sensitive -- case sensitivity
28 def __init__(self,fname,size,hints,scale=None,sensitive=False):
30 self.image = pygame.image.load(fname)
32 w,h = self.image.get_width(),self.image.get_height()
34 if not scale: scale = size
40 self.sensitive = sensitive
41 if not self.sensitive: hints = hints.lower()
43 if c not in ('\r','\n','\t'):
44 img = self.image.subsurface(x,y,tw,th)
47 if x >= w: x,y = 0,y+th
53 return len(text)*tw,th
55 def render(self,text,antialias=0,color=(255,255,255),background=None):
56 size = self.size(text)
59 if background == None:
60 s = pygame.Surface(size).convert_alpha()
63 s = pygame.Surface(size).convert()
66 if not self.sensitive: text = text.lower()
68 if color not in self.colors: self.colors[color] = {}
69 colored = self.colors[color]
75 img = self.chars[c].convert_alpha()
76 for yy in xrange(0,th):
77 for xx in xrange(0,tw):
78 r,g,b,a = img.get_at((xx,yy))
80 img.set_at((xx,yy),color)
83 if scale != (tw,th): img = pygame.transform.scale(img,scale)
90 """A decorator for normal fonts, adds a border. Interface compatible with pygame.Font.
93 size -- width of border; defaults 0
94 color -- color of border; default (0,0,0)
97 def __init__(self,font,size=1,color=(0,0,0)):
104 w,h = self.font.size(text)
108 def render(self,text,antialias=0,color=(255,255,255),background=None):
109 size = self.size(text)
111 if background == None:
112 s = pygame.Surface(size).convert_alpha()
115 s = pygame.Surface(size).convert()
118 bg = self.font.render(text,antialias,self.color)
119 fg = self.font.render(text,antialias,color)
122 dirs = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
123 for dx,dy in dirs: s.blit(bg,(si+dx*si,si+dy*si))