[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Font.render() and multi-line text?
Stuart wrote:
Hello People,
I'm sure this is a simple question, but I was wondering if font.render can
handle multi-line tetx (ie: embedded \n characters) properly, or if I need to
split and manually re-position for each line.
At present it does not seem to be managing it (\n shows up as a 'box' - ie:
bad character), but maybe I'm just missing something?
PS: I just *LOVE* the fact that unicode is handled correctly, more font
libraries out there need to gain this level of competence!
hi stuart, while it's not worlds of trouble to do it yourself, awhile
back someone put some handy code to do 'wordwrapped text output' which
wraps text output to a rectangular area. it also handles the \n
characters. it even does justification.
http://www.pygame.org/pcr/text_rect/index.php
if you really just need linefeed characters, something like this should
work..
def rendertext(font, text, pos=(0,0), color=(255,255,255), bg=(0,0,0)):
lines = text.splitlines()
#first we need to find image size...
width = height = 0
for l in lines:
width = max(width, font.size(l)[0])
height += font.get_linesize()
#create 8bit image for non-aa text..
img = pygame.Surface((width, height), 0, 8)
img.set_palette([bg, color])
#render each line
height = 0
for l in lines:
t = font.render(l, 0, color, bg)
img.blit(t, (0, height))
height += font.get_linesize()
return img
you'll probably need to change this around a little for different needs.
for example if you want antialiased fonts change the Surface call to
Surface((width, height), SRCALPHA, 32), then no need to set a palette.