1 """A collection of text rendering functions"""
3 def write(s,font,pos,color,text,border=1):
4 """Write text to a surface with a black border"""
5 # Render the text in black, at various offsets to fake a border
6 tmp = font.render(text,1,(0,0,0))
7 dirs = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
9 s.blit(tmp,(pos[0]+dx*border,pos[1]+dy*border))
10 # Now render the text properly, in the proper color
11 tmp = font.render(text,1,color)
14 def writec(s,font,color,text,border=1):
15 """Write centered text to a surface with a black border"""
16 # Center the text within the destination surface
18 x = (s.get_width()-w)/2
19 y = (s.get_height()-h)/2
20 write(s,font,(x,y),color,text,border)
22 def writepre(s,font,rect,color,text):
23 """Write preformatted text on a pygame surface"""
24 r,c,txt = rect,color,text
25 txt = txt.replace("\t"," ")
26 tmp = font.render(" ",1,c)
27 sw,sh = tmp.get_size()
29 for sentence in txt.split("\n"):
31 tmp = font.render(sentence,1,c)
35 def writewrap(s, font, rect, color, text, maxlines=None, wrapchar=False):
36 """Write wrapped text on a pygame surface.
38 maxlines -- specifies the maximum number of lines to write
40 wrapchar -- whether to wrap at the character level, or
43 r,c,txt = rect,color,text
44 txt = txt.replace("\t", " "*8)
45 tmp = font.render(" ", 1, c)
46 sw,sh = tmp.get_size()
50 for sentence in txt.split("\n"):
55 words = sentence.split(" ")
60 tmp = font.render(word, 1, c)
61 (iw, ih) = tmp.get_size()
66 if (maxlines != None and row > maxlines):
76 if (maxlines != None and row > maxlines):