OSDN Git Service

the characters widths array is not used on ElementText.
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Tue, 5 May 2009 11:06:22 +0000 (20:06 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Tue, 5 May 2009 11:06:22 +0000 (20:06 +0900)
src/FukuiNoNamari/ThreadViewBase/element.py

index aaed13f..61d60d2 100644 (file)
@@ -104,21 +104,18 @@ class ElementText:
 
         self.line_list = []
 
-    def recalc_char_widths(self):
-        self.widths = [i for i in itertools.repeat(0, len(self.text))]
-
+    def _get_ch_width(self, ch):
         dict = self._get_ch_width_dict()
-        for index, ch in enumerate(self.text):
-            if ch not in dict:
-                self.pango_layout.set_attributes(self._get_attrs())
-                self.pango_layout.set_text(ch)
-                a, logi = self.pango_layout.get_extents()
-                width = 1.0 * Rectangle(*logi).width / pango.SCALE
-                self.widths[index] = width
-                dict[ch] = width
-            else:
-                width = dict[ch]
-                self.widths[index] = width
+        if ch not in dict:
+            self.pango_layout.set_attributes(self._get_attrs())
+            self.pango_layout.set_text(ch)
+            a, logi = self.pango_layout.get_extents()
+            width = 1.0 * Rectangle(*logi).width / pango.SCALE
+            dict[ch] = width
+        return dict[ch]
+
+    def recalc_char_widths(self):
+        pass
 
     def _get_ch_width_dict(self):
         return ElementText.ch_width_dict
@@ -140,7 +137,8 @@ class ElementText:
             if y >= top and y < bottom:
                 sum_of_widths = line.rectangle.x
                 index = line.start_index
-                for width in self.widths[line.start_index:line.end_index]:
+                subtext = self.text[line.start_index:line.end_index]
+                for width in itertools.imap(self._get_ch_width, subtext):
                     if sum_of_widths + width/2 > x:
                         break
                     sum_of_widths += width
@@ -157,8 +155,8 @@ class ElementText:
 
         ch_h = get_approximate_char_height(self.pango_layout.get_context())
 
-        for index, ch in enumerate(self.text):
-            ch_w = self.widths[index]
+        iterable = itertools.imap(self._get_ch_width, self.text)
+        for index, ch_w in enumerate(iterable):
             if current_line_x + current_line_width + ch_w > width:
                 line = Line(
                     current_line_start_index, index,