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
6 <pre>write(s,font,pos,color,text,border=1)</pre>
8 i = font.render(text,1,(0,0,0))
10 dirs = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
11 for dx,dy in dirs: s.blit(i,(pos[0]+dx*si,pos[1]+dy*si))
12 i = font.render(text,1,color)
15 def writec(s,font,color,text,border=1):
16 """write centered text to a surface with a black border
18 <pre>writec(s,font,color,text,border=1)</pre>
21 x = (s.get_width()-w)/2
22 y = (s.get_height()-h)/2
23 write(s,font,(x,y),color,text,border)
25 def writepre(s,font,rect,color,text):
26 """write preformatted text
28 <pre>writepre(s,font,rect,color,text)</pre>
30 r,c,txt = rect,color,text
31 txt = txt.replace("\t"," ")
32 i = font.render(" ",1,c)
33 sw,sh = i.get_width(),i.get_height()
35 for sentence in txt.split("\n"):
37 i = font.render(sentence,1,c)
41 def writewrap(s,font,rect,color,text):
44 <pre>writewrap(s,font,rect,color,text)</pre>
46 r,c,txt = rect,color,text
47 txt = txt.replace("\t"," ")
48 i = font.render(" ",1,c)
49 sw,sh = i.get_width(),i.get_height()
51 for sentence in txt.split("\n"):
53 for word in sentence.split(" "):
54 i = font.render(word,1,c)
55 iw,ih = i.get_width(),i.get_height()
56 if x+iw > r.right: x,y = r.left,y+sh
61 # vim: set filetype=python sts=4 sw=4 noet si :