def render_multiline(self,font,text):
"""
renders multiline TEXT on the pygame surface SURF with the
font style FONT.
A new line in text is denoted by \n, no other characters are
escaped. Other forms of white-spaces should be converted to space.
returns the updated surface, words and the character bounding boxes.
"""
# get the number of lines
lines = text.split('\n')
lengths = [len(l) for l in lines]
# font parameters:
line_spacing = font.get_sized_height() + 1
# initialize the surface to proper size:
line_bounds = font.get_rect(lines[np.argmax(lengths)])
fsize = (round(2.0*line_bounds.width), round(1.25*line_spacing*len(lines)))
surf = pygame.Surface(fsize, pygame.locals.SRCALPHA, 32)
bbs = []
space = font.get_rect('O')
x, y = 0, 0
for l in lines:
x = 0 # carriage-return
y += line_spacing # line-feed
for ch in l: # render each character
if ch.isspace(): # just shift
x += space.width
else:
# render the character
ch_bounds = font.render_to(surf, (x,y), ch)
ch_bounds.x = x + ch_bounds.x
ch_bounds.y = y - ch_bounds.y
x += ch_bounds.width
bbs.append(np.array(ch_bounds))
# get the union of characters for cropping:
r0 = pygame.Rect(bbs[0])
rect_union = r0.unionall(bbs)
# get the words:
words = ' '.join(text.split())
# crop the surface to fit the text:
bbs = np.array(bbs)
surf_arr, bbs = crop_safe(pygame.surfarray.pixels_alpha(surf), rect_union, bbs, pad=5)
surf_arr = surf_arr.swapaxes(0,1)
#self.visualize_bb(surf_arr,bbs)
return surf_arr, words, bbs
评论列表
文章目录