OSDN Git Service

ThreadView, character widths are cached. This is expected to improve performance.
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Fri, 1 May 2009 17:49:45 +0000 (02:49 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Fri, 1 May 2009 17:49:45 +0000 (02:49 +0900)
src/FukuiNoNamari/thread_view.py
src/FukuiNoNamari/thread_window.py

index ebecefe..0537fb3 100644 (file)
@@ -97,20 +97,38 @@ class ElementEmpty:
     
 class ElementText:
 
+    ch_width_dict = {}   # key: char, value: width
+
     def __init__(self, text, pango_layout):
         self.text = text
         self.pango_layout = pango_layout
 
-        attrlist = self._get_attrs()
-        self.widths = thread_view_extend.get_char_width(
-            pango_layout.get_context(), text, attrlist)
+        self.recalc_char_widths()
 
         self.line_list = []
 
     def recalc_char_widths(self):
-        attrlist = self._get_attrs()
-        self.widths = thread_view_extend.get_char_width(
-            self.pango_layout.get_context(), self.text, attrlist)
+        self.widths = [i for i in itertools.repeat(0, len(self.text))]
+
+        dict = self._get_ch_width_dict()
+        need_to_get = False
+        for index, ch in enumerate(self.text):
+            if ch not in dict:
+                need_to_get = True
+                break
+            else:
+                width = dict[ch]
+                self.widths[index] = width
+
+        if need_to_get:
+            attrlist = self._get_attrs()
+            self.widths = thread_view_extend.get_char_width(
+                self.pango_layout.get_context(), self.text, attrlist)
+            for index, width in enumerate(self.widths):
+                dict[self.text[index]] = self.widths[index]
+
+    def _get_ch_width_dict(self):
+        return ElementText.ch_width_dict
 
     def _get_attrs(self):
         attrs = pango.AttrList()
@@ -246,6 +264,11 @@ class ElementBoldText(ElementText):
         attrlist.insert(attr)
         return attrlist
 
+    def recalc_char_widths(self):
+        attrlist = self._get_attrs()
+        self.widths = thread_view_extend.get_char_width(
+            self.pango_layout.get_context(), self.text, attrlist)
+
 
 class ElementLink(ElementText):
     
index 6ce6394..b2786db 100644 (file)
@@ -109,7 +109,9 @@ class HTMLParserToThreadView:
             self.layout = self.threadview.create_res_layout(
                 self.left_margin, self.resnum)
 
+        gtk.gdk.threads_enter()
         self.layout.add_text(data, bold, href)
+        gtk.gdk.threads_leave()
 
     def to_thread_view(self):
         if self.layout is not None: