OSDN Git Service

extract a common function of new_glyphs.py and rest_of_kanji.py
authormshio <mshio@54a90f34-5e62-402c-8eae-46c47f0b2e07>
Sun, 20 Jan 2013 09:19:42 +0000 (09:19 +0000)
committermshio <mshio@54a90f34-5e62-402c-8eae-46c47f0b2e07>
Sun, 20 Jan 2013 09:19:42 +0000 (09:19 +0000)
git-svn-id: svn+ssh://svn.osdn.net/svnroot/sawarabi-fonts/trunk@27 54a90f34-5e62-402c-8eae-46c47f0b2e07

script/fontparser.py [new file with mode: 0644]
script/listprinter.py [new file with mode: 0644]
script/new_glyphs.py
script/rest_of_kanji.py

diff --git a/script/fontparser.py b/script/fontparser.py
new file mode 100644 (file)
index 0000000..7cc664b
--- /dev/null
@@ -0,0 +1,39 @@
+# -*- coding: utf-8 -*-
+
+# Author: mshio <mshio@users.sourceforge.jp>
+
+__version__ = '0.11'
+
+import fontforge
+from types import MethodType
+
+
+class FontParser:
+    """
+    An object in order to parse the characters in the specified font.
+    """
+    def __init__(self, font_path):
+        self.forge = fontforge.open(font_path)
+
+    def parse(self, method):
+        self.proc = MethodType(method, self, FontParser)
+        f = self.forge
+        for g in f:
+            if g[0] != '.' and f[g].unicode > 0:
+                self.proc(f[g])
+
+class FontDiffParser:
+    def __init__(self, old_font_path, new_font_path):
+        self.font_path = [old_font_path, new_font_path]
+
+    def get_diff(self):
+        buf = []
+        old = fontforge.open(self.font_path[0])
+
+        def collect(self, glyph_obj):
+            if glyph_obj.glyphname not in old:
+                buf.append(glyph_obj.unicode)
+
+        FontParser(self.font_path[1]).parse(collect)
+
+        return buf
diff --git a/script/listprinter.py b/script/listprinter.py
new file mode 100644 (file)
index 0000000..9f1aa5f
--- /dev/null
@@ -0,0 +1,32 @@
+# -*- coding: utf-8 -*-
+
+# Author: mshio <mshio@users.sourceforge.jp>
+
+__version__ = '0.11'
+
+import sys
+
+class SimpleListPrinter:
+    def __init__(self, col=10, out=sys.stdout, delimiter=' '):
+        self.col = col
+        self.out = out
+        self.delimiter = self._get_delimiter(delimiter)
+
+    def output(self, array):
+        n = 0
+        size = len(array)
+        for c in array:
+            n += 1
+            self.out.write(unichr(c).encode('utf-8'))
+            delimiter = self.delimiter if n % self.col != 0 and n < size else '\n'
+            self.out.write(delimiter)
+
+    def _get_delimiter(self, delimiter):
+        ch = None
+        if type(delimiter) == int:
+            ch = unichr(delimiter).encode('utf-8')
+        elif type(delimiter) == unicode:
+            ch = delimiter.encode('utf-8')
+        elif type(delimiter) == str:
+            ch = delimiter
+        return ch
index 6a9ce22..b3deb53 100644 (file)
@@ -1,53 +1,50 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
-import fontforge
+# $Id$
+# Author: mshio <mshio@users.sourceforge.jp>
+
+__version__ = '0.11'
+
 import os
 import sys
 import tarfile
+from fontparser import FontDiffParser
+from listprinter import SimpleListPrinter
+
+
+def print_new_glyph(archive, newfile, printer=None):
+  def fontfilepath_in_archive(archive_path, weight, ext='ttf'):
+    filenm = archive_path.split('/')[-1]
+    p = filenm.split('-')
+    family_name = p[0]
+    type_name = p[1]
 
-def get_fontfilename_in_archive(string):
-  name = string.split('/')[-1]
-  p = name.split('-')
-  family = p[0]
-  type = p[1]
+    return '%s-%s/%s-%s-%s.%s' % (family_name, type_name,
+                                  family_name, type_name, weight, ext)
 
-  return '%s-%s/%s-%s-medium.ttf' % (family, type, family, type)
+  def expand_archive(archive_path, fontfile_path):
+    f = tarfile.open(archive_path, 'r:gz')
+    f.extract(fontfile_path)
 
-def get_glyph_list(fontfile):
-  ret = []
-  font = fontforge.open(fontfile)
-  for g in font:
-    glyph = font[g]
-    ret.append(glyph.unicode)
-  font.close()
-  return ret
+  def clean_up(fontfile_path):
+    os.remove(fontfile_path)
+    os.rmdir(fontfile_path.split('/')[0])
 
-def remove_dep_item(list1, list2):
-  for l in list1:
-    list2.remove(l)
+  def main(archive, newfile, printer):
+    font_path = fontfilepath_in_archive(archive, 'medium')
+    expand_archive(archive, font_path)
 
-  return list2
+    parser = FontDiffParser(font_path, newfile)
+    diff = parser.get_diff()
 
-def print_result_list(list):
-  out = sys.stdout
-  col = 0
-  dec = 0
-  for n in list:
-    if n < 0 or n > sys.maxunicode:
-      continue
-    if col != 0:
-      out.write('、')
-    col = col + 1
-    out.write(unichr(n).encode('utf-8'))
-    if col == 10:
-      col = 0
-      dec = dec + 1
-      out.write('\n')
-  if col != 0:
-    out.write('\n')
-  out.write('%d chars\n' % (dec * 10 + col))
+    p = printer
+    if p == None: p = SimpleListPrinter(delimiter=0x3001)
+    p.output(diff)
+    print
+    print "%d char(s)" % len(diff)
 
+  main(archive, newfile, printer)
 
 if __name__ == '__main__':
   if len(sys.argv) != 3:
@@ -57,15 +54,4 @@ if __name__ == '__main__':
   archive = sys.argv[1]
   newfile = sys.argv[2]
 
-  fontfile = get_fontfilename_in_archive(archive)
-  f = tarfile.open(archive, 'r:gz')
-  f.extract(fontfile)
-
-  oldglyphs = get_glyph_list(fontfile)
-  newglyphs = get_glyph_list(newfile)
-
-  result = remove_dep_item(oldglyphs, newglyphs)
-  print_result_list(result)
-
-  os.remove(fontfile)
-  os.rmdir(fontfile.split('/')[0])
+  print_new_glyph(archive, newfile)
index 4843a81..8a44b89 100644 (file)
@@ -1,3 +1,4 @@
+#!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
 # $Id$
 このスクリプトを実行するには、FontForge が必要です。
 """
 
-__version__ = '0.10'
+__version__ = '0.11'
 
-from types import MethodType
-import fontforge
 import sys
+from types import MethodType
+from fontparser import FontParser
+from listprinter import SimpleListPrinter
 
 class KanjiList:
     def __init__(self, file_path):
@@ -56,38 +58,9 @@ class KanjiList:
         fh.close()
         return self.buffer
 
-class FontForgeCharList:
-    def __init__(self, font_path):
-        self.forge = fontforge.open(font_path)
-
-    def loop(self, method):
-        self.proc = MethodType(method, self, FontForgeCharList)
-        f = self.forge
-        for g in f:
-            if g[0] != '.' and f[g].unicode > 0:
-                self.proc(f[g])
-
-class ListPrinter:
-    def __init__(self, col, out):
-        self.col = col
-        self.out = out
-
-    def output(self, list):
-        col = 0
-        n = 0
-        for c in list:
-            col += 1
-            n += 1
-            self.out.write(unichr(c).encode('utf-8'))
-            if col == self.col:
-                col = 0
-                self.out.write('\n')
-            else:
-                self.out.write(' ')
-        return n
 
 if __name__ == '__main__':
-    forge = FontForgeCharList(sys.argv[1])
+    parser = FontParser(sys.argv[1])
     kanji_list = KanjiList(sys.argv[2]).read()
 
     def remove(self, char):
@@ -96,9 +69,9 @@ if __name__ == '__main__':
                 kanji_list.remove(c)
                 break
 
-    forge.loop(remove)
+    parser.parse(remove)
 
-    p = ListPrinter(10, sys.stdout)
-    count = p.output(kanji_list)
+    p = SimpleListPrinter(delimiter=0x3001)
+    p.output(kanji_list)
     print
-    print "%d char(s)" % count
+    print "%d char(s)" % len(kanji_list)