OSDN Git Service

modified: README
[kp123/kp123.git] / data / convert.py
diff --git a/data/convert.py b/data/convert.py
new file mode 100755 (executable)
index 0000000..74872a7
--- /dev/null
@@ -0,0 +1,114 @@
+#!/usr/bin/env python3
+
+import sys
+import pdb
+import codecs
+from math import acos, pi
+
+if len(sys.argv) < 2:
+    print("Usage: %s" % sys.argv[0])
+    sys.exit(1)
+
+class pt:
+       def __init__(self, s):
+           s = s.split()
+           self.x = int(int(s[0]))
+           self.y = int(int(s[1]))
+       def __eq__(self, other):
+           return self.x == other.x and self.y == other.y
+
+def get_chr(pts, cap = False):
+       if len(pts) != 2:
+           print("err len(pts)")
+           sys.exit(1)
+       p0 = pts[0]
+       p1 = pts[1]
+       if p0 == p1: return ""
+       x = (p0.x - p1.x)
+       y = (p0.y - p1.y)
+       z = (x**2 + y**2)**.5
+       a = acos(x/z)
+       if a < pi/8:
+           ret = 'F'
+       elif a >= 7*pi/8:
+           ret = 'D'   
+       elif y > 0:
+           if 5*pi/8 < a and a < 7*pi/8:
+                   ret = 'A'
+           elif 3*pi/8 <= a and a <= 5*pi/8:
+                   ret = 'B'
+           elif pi/8 < a and a < 3*pi/8:
+                   ret = 'C'
+       else:
+           if 5*pi/8 < a and a < 7*pi/8:
+                   ret = 'G'
+           elif 3*pi/8 <= a and a <= 5*pi/8:
+                   ret = 'H'
+           elif pi/8 < a and a < 3*pi/8:
+                   ret = 'I'
+       if not cap:
+           ret = str.lower(ret)
+       return ret
+
+f = open(sys.argv[1])
+fo = open("tomoe", 'wb')
+fo.write(codecs.BOM_UTF16_LE)
+k = None
+pts = []
+strokes = ""
+cap = True
+
+def dump_kanji():
+       global k
+       if k == None:
+           return
+       global fo
+
+for i in f.readlines():
+       if i.find("utf8") != -1:
+           strokes = ""
+           pts = []
+           cap = True
+           i = i.replace("utf8", "")
+           i = i.strip(" ><x&#;/\n\r")
+           if len(i) != 4: continue
+           j = i[2:4] + i[0:2]
+           #fo.write(bytes.fromhex(j))
+           #global k
+           k = bytes.fromhex(j)
+       if i.find("<point") != -1:
+           i = i.replace("point", "")
+           i = i.replace("=", "")
+           i = i.replace("x", "")
+           i = i.replace("y", "")
+           i = i.replace("\"", "")
+           i = i.lstrip(" ><x&#;/\n\r")
+           i = i.rstrip(" ><x&#;/\n\r")
+           pts.insert(0, pt(i))
+           if len(pts) > 2:
+                   pts.pop()
+           if len(pts) == 2:
+                   strokes += get_chr(pts, cap)
+                   cap = False
+           continue
+       if i.find("<stroke>") != -1:
+           cap = True
+           pts = []
+       if i.find("</stroke>") != -1:
+           cap = False
+           pts = []
+       if i.find("</strokes>") != -1:
+           l = 0
+           for j in range(0, len(strokes)):
+                   if str.isupper(strokes[j]): l += 1
+           l = ord('A') + l - 1
+           if l > ord('Z'): l = ord('Z')
+           fo.write(bytes.fromhex("%.2X00" % l))
+           fo.write(k)
+           for j in range(0, len(strokes)):
+                   fo.write(bytes.fromhex("%.2X00" % ord(strokes[j])))
+           fo.write(bytes.fromhex("0A00"))
+           strokes = ""
+               
+f.close()
+fo.close()