OSDN Git Service

update a chartool (pygtk version) to be able to search chars by a character code
authormshio <mshio@54a90f34-5e62-402c-8eae-46c47f0b2e07>
Sun, 27 Oct 2013 12:29:19 +0000 (12:29 +0000)
committermshio <mshio@54a90f34-5e62-402c-8eae-46c47f0b2e07>
Sun, 27 Oct 2013 12:29:19 +0000 (12:29 +0000)
git-svn-id: svn+ssh://svn.osdn.net/svnroot/sawarabi-fonts/trunk@59 54a90f34-5e62-402c-8eae-46c47f0b2e07

chartool/pygtk/main.py

index 67af930..3b09d98 100644 (file)
@@ -4,6 +4,7 @@
 import gtk
 import os
 import pygtk
 import gtk
 import os
 import pygtk
+import re
 
 
 class Controller:
 
 
 class Controller:
@@ -66,18 +67,25 @@ class Controller:
 
 
 class SearchAction:
 
 
 class SearchAction:
+    re_hex = re.compile(r"[0-9a-fA-F]")
+
     def __init__(self, entry_search):
         self.entry_search = entry_search
     def __init__(self, entry_search):
         self.entry_search = entry_search
+        self.char_util = CharUtil()
         self.code = None
         self.char = ''
 
     def set_next(self):
         en = self.entry_search
         buf = en.get_text().decode('utf-8')
         self.code = None
         self.char = ''
 
     def set_next(self):
         en = self.entry_search
         buf = en.get_text().decode('utf-8')
+        if len(buf) == 0: return
+
         pos = en.get_position()
         if pos == len(buf): pos = 0
         pos = en.get_position()
         if pos == len(buf): pos = 0
-        en.select_region(pos, pos + 1)
-        ch = buf[pos:pos + 1]
+        region = self.get_target_region(buf, pos)
+        target = buf[region[0]:region[1]]
+        en.select_region(region[0], region[1])
+        ch = self.char_util.get_unichar(target)
 
         self.code = hex(ord(ch))
         self.char = ch
 
         self.code = hex(ord(ch))
         self.char = ch
@@ -94,6 +102,34 @@ class SearchAction:
         mu = '<span font="%s">%s</span>' % (font_name, self.char)
         label.set_markup(mu)
 
         mu = '<span font="%s">%s</span>' % (font_name, self.char)
         label.set_markup(mu)
 
+    def get_target_region(self, text, pos):
+        ch = text[pos:pos + 1]
+        rlen = 0
+        llen = 0
+        while rlen <= 4 and self.char_util.is_hex_char(ch):
+            rlen += 1
+            if pos + rlen >= len(text): break
+            ch = text[(pos + rlen):(pos + rlen + 1)]
+        if rlen >= 1 and rlen < 5:
+            while rlen + llen <= 4 and pos - llen > 0:
+                ch = text[(pos - llen - 1):(pos - llen)]
+                if not self.char_util.is_hex_char(ch): break
+                llen += 1
+        ret = (pos - llen, pos + rlen)
+        if ret == (pos, pos): return (pos, pos + 1)
+        return ret
+
+class CharUtil:
+    def is_hex_char(self, char):
+        return (char >= '0' and char <= '9') or \
+            (char >= 'a' and char <= 'f') or \
+            (char >= 'A' and char <= 'F')
+
+    def get_unichar(self, buf):
+        if len(buf) <= 1: return buf
+
+        return unichr(int(buf, 16))
+
 
 if __name__ == '__main__':
     path = os.path.abspath(os.path.dirname(__file__))
 
 if __name__ == '__main__':
     path = os.path.abspath(os.path.dirname(__file__))