From: Simon Cross Date: Sun, 11 May 2014 21:29:40 +0000 (+0200) Subject: Refactor text boxes. X-Git-Tag: 0.1~378 X-Git-Url: https://git.ctpug.org.za/?p=naja.git;a=commitdiff_plain;h=d8daea1f8a67c1a547440b3934454da9792236ef Refactor text boxes. --- diff --git a/naja/widgets/text.py b/naja/widgets/text.py index 78e7f4c..6ca239d 100644 --- a/naja/widgets/text.py +++ b/naja/widgets/text.py @@ -40,60 +40,57 @@ class TextBoxWidget(TextWidget): super(TextBoxWidget, self).__init__(*args, **kwargs) + def lines(self): + if self.box_width != 0: + return self._wrapped_lines() + else: + return self.text.splitlines() + + def _wrapped_lines(self): + def words_fit(words): + words_line = ' '.join(words) + width = self.font.size(words_line)[0] + if width < self.box_width: + return True + elif len(words) == 1: + Exception("Word %r too long for box." % (words[0],)) + return False + + for line in self.text.splitlines(): + current_words = [] + for word in line.split(): + current_words.append(word) + if words_fit(current_words): + continue + else: + yield ' '.join(current_words[:-1]) + current_words = current_words[-1:] + if current_words and words_fit(current_words): + yield ' '.join(current_words) + def prepare(self): self.font = resources.get_font(self.fontname, self.fontsize) - self.lines = [] - height = 0 - width = 0 - for line in self.text.split('\n'): - if self.box_width is not 0: - current_line = '' - current_line_rendered = None - for word in line.split(' '): - if len(current_line) > 0: - current_line += ' ' + word - else: - current_line = word - line = self.font.render(current_line, True, self.colour) - if line.get_rect().width <= self.box_width: - current_line_rendered = line - width = max(width, line.get_rect().width) - height += line.get_rect().height - elif current_line_rendered is not None: - line_rect = current_line_rendered.get_rect() - self.lines.append(pygame.transform.scale( - current_line_rendered, (line_rect.width * 2, - line_rect.height * 2))) - current_line = word - current_line_rendered = self.font.render( - word, True, self.colour) - else: - raise Exception( - 'Line too long. Please resize or rephrase') - if current_line_rendered is not None: - line_rect = current_line_rendered.get_rect() - self.lines.append(pygame.transform.scale( - current_line_rendered, (line_rect.width * 2, - line_rect.height * 2))) - else: - line = self.font.render(line, True, self.colour) - line_rect = line.get_rect() - width = max(width, line.get_rect().width) - height += line.get_rect().height - self.lines.append(pygame.transform.scale( - line, (line_rect.width * 2, line_rect.height * 2))) - - width += 2 + self.padding - height += 2 + self.padding + rendered_lines = [] + width, height = self.padding, self.padding + for line in self.lines(): + line_surface = self.font.render(line, True, self.colour) + line_rect = line_surface.get_rect() + line_surface = pygame.transform.scale( + line_surface, (line_rect.width * 2, line_rect.height * 2)) + line_rect = line_surface.get_rect() + rendered_lines.append(line_surface) + width = max(width, line_rect.width) + height += line_rect.height + self.surface = pygame.surface.Surface((width, height), pygame.locals.SRCALPHA) + self.surface.fill(self.bg_colour) + self.size = self.surface.get_rect().size + + x, y = self.padding, self.padding + for line_surface in rendered_lines: + self.surface.blit(line_surface, (x, y)) + y += line_surface.get_rect().height def draw(self, surface): - self.surface.fill(self.bg_colour) - y = self.pos[1] - for line in self.lines: - surface.blit(line, (self.pos[0] + self.padding, y + self.padding)) - y += line.get_rect().height - self.text_rect = surface.get_rect() - if not self.size: - self.rect.size = self.text_rect.size + surface.blit(self.surface, self.rect)