+ self.fontsize = (fontsize or FONT_SIZE) // EIGHT_BIT_SCALE
+ self.colour = convert_colour(colour or PALETTE.BLACK)
+
+ def render_line(self, text):
+ text_surf = self.font.render(text, True, self.colour)
+ text_rect = text_surf.get_rect()
+ return pygame.transform.scale(
+ text_surf, (text_rect.width * EIGHT_BIT_SCALE,
+ text_rect.height * EIGHT_BIT_SCALE))
+
+ def prepare(self):
+ self.font = resources.get_font(self.fontname, self.fontsize)
+ self.surface = self.render_line(self.text)
+ self.size = self.surface.get_rect().size
+
+ def draw(self, surface):
+ surface.blit(self.surface, self.pos)
+
+
+class TextBoxWidget(TextWidget):
+ def __init__(self, *args, **kwargs):
+ self.padding = kwargs.pop('padding', 4)
+ self.border = kwargs.pop('border', 2)
+ self.bg_colour = convert_colour(kwargs.pop('bg_colour',
+ PALETTE.LIGHT_VIOLET))
+ self.border_colour = convert_colour(kwargs.pop('border_colour',
+ PALETTE.BLACK))
+ self.box_width = kwargs.pop('box_width', 0)
+
+ super(TextBoxWidget, self).__init__(*args, **kwargs)
+
+ def lines(self, image_map):
+ if self.box_width != 0:
+ return self._wrapped_lines(image_map)
+ else:
+ return self.text.splitlines()
+
+ def _prepare_glyph(self, image_map, glyph, current_words, lines):
+ size = self.font.size(' '.join(current_words[:-1] + ['']))
+ x = size[0] * EIGHT_BIT_SCALE + self.padding
+ y = size[1] * lines * EIGHT_BIT_SCALE + self.padding
+ for glyph_key in glyph.glyph_keys:
+ image_name, colour = MARKUP_MAP[glyph_key]
+ if colour is None:
+ colour = self.colour
+ image = resources.get_image(
+ image_name, transforms=(EIGHT_BIT, blender(colour)))
+ image_map[(x, y)] = image
+ x += image.get_width()
+
+ def _check_markup(self, word):
+ suffix = ''
+ if word[-1] in '.,':
+ suffix = word[-1]
+ word = word[:-1]