def render_inline_text(surface: Surface, text: str, size: int, text_color: Tuple[int, int, int], start_x: int = 0, end_x: int = None, start_y: int = 0, end_y: int = None): width, height = surface.get_size() end_x = end_x or width end_y = end_y or height font = Font(None, size) font.origin = True line_spacing = font.get_sized_height() + 2 x, y = start_x, line_spacing + start_y space = font.get_rect(' ') words = text.split(' ') # render word by word for word in words: bounds = font.get_rect(word) if x + bounds.width + bounds.x >= end_x: x, y = start_x, y + line_spacing if x + bounds.width + bounds.x >= end_x: raise ValueError(f"word too wide for the surface: {text}") if y + bounds.height - bounds.y >= end_y: raise ValueError(f"text to long for the surface: {text}") font.render_to(surface, (x, y), None, text_color) x += bounds.width + space.width return x, y
def word_wrap(self, text, size, text_color, margin): font = Font(None, size) font.origin = True words = text.split(' ') width, height = self.image.get_size() line_spacing = font.get_sized_height() + 2 x, y = 0, line_spacing + margin space = font.get_rect(' ') for word in words: bounds = font.get_rect(word) if x + bounds.width + bounds.x >= width: x, y = 0, y + line_spacing if x + bounds.width + bounds.x >= width: raise ValueError("word too wide for the surface") if y + bounds.height - bounds.y >= height: raise ValueError("text to long for the surface") font.render_to(self.image, (x, y), None, text_color) x += bounds.width + space.width return x, y