also if you want something a little more robust
def render(
self,
font: str,
text_or_loc: str | TextFormatter,
color: Color,
background_color: Color = None,
justify: Anchor = LEFT,
can_be_loc: bool = True,
wrap: bool = False,
max_width: int = None
) -> Surface:
thefont = self._get_font(font)
if can_be_loc:
thetext = self._texts.get(text_or_loc)
else:
thetext = str(text_or_loc)
if wrap:
thetext = self.__wrap_text(thetext, font, max_width)
if "\n" in thetext:
lines = thetext.split('\n')
line_size = self.get_linesize(font)
bg_width = max(self.size(font, line)[0] for line in lines)
bg_height = len(lines)*line_size
background = Surface((bg_width, bg_height), SRCALPHA)
background.fill((0, 0, 0, 0) if background_color is None else background_color)
line_y = 0
for line in lines:
render = thefont.render(line, self._antialias, color, background_color)
background.blit(render, ((bg_width - render.get_width())*justify[0], line_y))
line_y += line_size
return background
return thefont.render(thetext, self._antialias, color, background_color)