Commit before breaking everything
[matches/honours.git] / research / transmission_spectroscopy / simulator / pgu-0.18 / build / lib / 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     # 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)]
8     for dx,dy in dirs: 
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)
12     s.blit(tmp,pos)
13
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
17     w,h = font.size(text)
18     x = (s.get_width()-w)/2
19     y = (s.get_height()-h)/2
20     write(s,font,(x,y),color,text,border)
21     
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()
28     y = r.top
29     for sentence in txt.split("\n"):
30         x = r.left
31         tmp = font.render(sentence,1,c)
32         s.blit(tmp,(x,y))
33         y += sh
34
35 def writewrap(s, font, rect, color, text, maxlines=None, wrapchar=False):
36     """Write wrapped text on a pygame surface.
37
38     maxlines -- specifies the maximum number of lines to write 
39         before stopping
40     wrapchar -- whether to wrap at the character level, or 
41         word level
42     """
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()
47     y = r.top
48     row = 1
49     done = False
50     for sentence in txt.split("\n"):
51         x = r.left
52         if wrapchar:
53             words = sentence
54         else:
55             words = sentence.split(" ")
56             
57         for word in words:
58             if (not wrapchar):
59                 word += " "
60             tmp = font.render(word, 1, c)
61             (iw, ih) = tmp.get_size()
62             if (x+iw > r.right):
63                 x = r.left
64                 y += sh
65                 row += 1
66                 if (maxlines != None and row > maxlines):
67                     done = True
68                     break
69             s.blit(tmp, (x, y))
70             #x += iw+sw
71             x += iw
72         if done:
73             break
74         y += sh
75         row += 1
76         if (maxlines != None and row > maxlines):
77             break
78

UCC git Repository :: git.ucc.asn.au