qq refactoring avant prise en charge des répétitions.
[minwii.git] / src / pgu / text.py
1 """a collection of text rendering functions
2 """
3 def write(s,font,pos,color,text,border=1):
4 """write text to a surface with a black border
5
6 <pre>write(s,font,pos,color,text,border=1)</pre>
7 """
8 i = font.render(text,1,(0,0,0))
9 si = border
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)
13 s.blit(i,pos)
14
15 def writec(s,font,color,text,border=1):
16 """write centered text to a surface with a black border
17
18 <pre>writec(s,font,color,text,border=1)</pre>
19 """
20 w,h = font.size(text)
21 x = (s.get_width()-w)/2
22 y = (s.get_height()-h)/2
23 write(s,font,(x,y),color,text,border)
24
25 def writepre(s,font,rect,color,text):
26 """write preformatted text
27
28 <pre>writepre(s,font,rect,color,text)</pre>
29 """
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()
34 y = r.top
35 for sentence in txt.split("\n"):
36 x = r.left
37 i = font.render(sentence,1,c)
38 s.blit(i,(x,y))
39 y += sh
40
41 def writewrap(s,font,rect,color,text):
42 """write wrapped text
43
44 <pre>writewrap(s,font,rect,color,text)</pre>
45 """
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()
50 y = r.top
51 for sentence in txt.split("\n"):
52 x = r.left
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
57 s.blit(i,(x,y))
58 x += iw+sw
59 y += sh
60
61 # vim: set filetype=python sts=4 sw=4 noet si :