1 """Some handy font-like objects.
3 <p>please note that this file is alpha, and is subject to modification in
4 future versions of pgu!</p>
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 with pygame.Font
15 <p>TileFonts are fonts that are stored in a tiled image. Where the image opaque, it assumed that the font is visible. Font color is changed automatically, so it does not work with
16 fonts with stylized coloring.</p>
18 <pre>TileFont(fname,size,hints,scale=None,sensitive=False)</pre>
21 <dt>size <dd>the dimensions of the characters
22 <dt>hints <dd>a string of hints "abcdefg..."
23 <dt>scale <dd>size to scale font to
24 <dt>sensitive <dd>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.
92 <pre>BorderFont(font,size=1,color=(0,0,0))</pre>
95 <dt>size <dd>width of border; defaults 0
96 <dt>color <dd>color of border; default (0,0,0)
99 def __init__(self
,font
,size
=1,color
=(0,0,0)):
106 w
,h
= self
.font
.size(text
)
110 def render(self
,text
,antialias
=0,color
=(255,255,255),background
=None):
111 size
= self
.size(text
)
113 if background
== None:
114 s
= pygame
.Surface(size
).convert_alpha()
117 s
= pygame
.Surface(size
).convert()
120 bg
= self
.font
.render(text
,antialias
,self
.color
)
121 fg
= self
.font
.render(text
,antialias
,color
)
124 dirs
= [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
125 for dx
,dy
in dirs
: s
.blit(bg
,(si
+dx
*si
,si
+dy
*si
))