OSDN Git Service

Calculating the character widths is done by PangoLayout. FukuiNoNamariExt is not...
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Tue, 5 May 2009 10:02:37 +0000 (19:02 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Tue, 5 May 2009 10:02:37 +0000 (19:02 +0900)
src/FukuiNoNamari/ThreadViewBase/element.py

index dc5da2f..aaed13f 100644 (file)
@@ -20,7 +20,6 @@ pygtk.require('2.0')
 import gtk
 import pango
 import itertools
-from FukuiNoNamariExt import thread_view_extend
 
 def get_approximate_char_height(pango_context):
     desc = pango_context.get_font_description()
@@ -109,22 +108,18 @@ class ElementText:
         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
+                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 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
 
@@ -261,6 +256,8 @@ class ElementText:
 
 class ElementBoldText(ElementText):
 
+    ch_width_dict = {}   # key: char, value: width
+
     def _get_attrs(self):
         attrlist = pango.AttrList()
         attr = pango.AttrWeight(pango.WEIGHT_BOLD,
@@ -268,10 +265,8 @@ 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)
+    def _get_ch_width_dict(self):
+        return ElementBoldText.ch_width_dict
 
 
 class ElementLink(ElementText):