OSDN Git Service

74872a7e217492209e62e7ba0005428c286d8af0
[kp123/kp123.git] / data / convert.py
1 #!/usr/bin/env python3
2
3 import sys
4 import pdb
5 import codecs
6 from math import acos, pi
7
8 if len(sys.argv) < 2:
9     print("Usage: %s" % sys.argv[0])
10     sys.exit(1)
11
12 class pt:
13         def __init__(self, s):
14             s = s.split()
15             self.x = int(int(s[0]))
16             self.y = int(int(s[1]))
17         def __eq__(self, other):
18             return self.x == other.x and self.y == other.y
19
20 def get_chr(pts, cap = False):
21         if len(pts) != 2:
22             print("err len(pts)")
23             sys.exit(1)
24         p0 = pts[0]
25         p1 = pts[1]
26         if p0 == p1: return ""
27         x = (p0.x - p1.x)
28         y = (p0.y - p1.y)
29         z = (x**2 + y**2)**.5
30         a = acos(x/z)
31         if a < pi/8:
32             ret = 'F'
33         elif a >= 7*pi/8:
34             ret = 'D'   
35         elif y > 0:
36             if 5*pi/8 < a and a < 7*pi/8:
37                     ret = 'A'
38             elif 3*pi/8 <= a and a <= 5*pi/8:
39                     ret = 'B'
40             elif pi/8 < a and a < 3*pi/8:
41                     ret = 'C'
42         else:
43             if 5*pi/8 < a and a < 7*pi/8:
44                     ret = 'G'
45             elif 3*pi/8 <= a and a <= 5*pi/8:
46                     ret = 'H'
47             elif pi/8 < a and a < 3*pi/8:
48                     ret = 'I'
49         if not cap:
50             ret = str.lower(ret)
51         return ret
52
53 f = open(sys.argv[1])
54 fo = open("tomoe", 'wb')
55 fo.write(codecs.BOM_UTF16_LE)
56 k = None
57 pts = []
58 strokes = ""
59 cap = True
60
61 def dump_kanji():
62         global k
63         if k == None:
64             return
65         global fo
66
67 for i in f.readlines():
68         if i.find("utf8") != -1:
69             strokes = ""
70             pts = []
71             cap = True
72             i = i.replace("utf8", "")
73             i = i.strip(" ><x&#;/\n\r")
74             if len(i) != 4: continue
75             j = i[2:4] + i[0:2]
76             #fo.write(bytes.fromhex(j))
77             #global k
78             k = bytes.fromhex(j)
79         if i.find("<point") != -1:
80             i = i.replace("point", "")
81             i = i.replace("=", "")
82             i = i.replace("x", "")
83             i = i.replace("y", "")
84             i = i.replace("\"", "")
85             i = i.lstrip(" ><x&#;/\n\r")
86             i = i.rstrip(" ><x&#;/\n\r")
87             pts.insert(0, pt(i))
88             if len(pts) > 2:
89                     pts.pop()
90             if len(pts) == 2:
91                     strokes += get_chr(pts, cap)
92                     cap = False
93             continue
94         if i.find("<stroke>") != -1:
95             cap = True
96             pts = []
97         if i.find("</stroke>") != -1:
98             cap = False
99             pts = []
100         if i.find("</strokes>") != -1:
101             l = 0
102             for j in range(0, len(strokes)):
103                     if str.isupper(strokes[j]): l += 1
104             l = ord('A') + l - 1
105             if l > ord('Z'): l = ord('Z')
106             fo.write(bytes.fromhex("%.2X00" % l))
107             fo.write(k)
108             for j in range(0, len(strokes)):
109                     fo.write(bytes.fromhex("%.2X00" % ord(strokes[j])))
110             fo.write(bytes.fromhex("0A00"))
111             strokes = ""
112                 
113 f.close()
114 fo.close()