Refactor text boxes.
authorSimon Cross <hodgestar@gmail.com>
Sun, 11 May 2014 21:29:40 +0000 (23:29 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sun, 11 May 2014 21:29:49 +0000 (23:29 +0200)
naja/widgets/text.py

index 78e7f4cda34da9965ff3d22d02480383104c55b5..6ca239d6fbdc08d9e467a718fcf86d955d4cf252 100644 (file)
@@ -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)