-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
84 lines (64 loc) · 2.33 KB
/
convert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import csv
import json
import os
import re
class Convert():
charmap = {}
pre = ["േ", "െ", "ൈ", "ോ", "ൊ", "ൌ", "ൗ", "്ര"]
post = ["്യ", "്വ"]
def __init__(self, dirpath="maps"):
self.dirpath = dirpath
with open(os.path.join(dirpath, "index.json"), "r") as f:
self.map_index = json.loads(f.read())
def index(self):
return self.map_index
def load(self, fontname):
if fontname not in self.map_index:
raise Exception("'{}' not found in font maps".format(fontname))
with open(os.path.join(self.dirpath, self.map_index[fontname]), "r") as f:
self.charmap[fontname] = json.loads(f.read())
def convert(self, txt, fontname):
m = self.charmap[fontname]
out = []
txt = list(txt)
n = 0
while n < len(txt):
c = txt[n]
if c == " " or c == "\n" or c not in m:
out.append(c)
n += 1
continue
v = m[c]
if v in self.pre:
if n+2 < len(txt):
if m.get(txt[n+1]) == "്ര":
out.extend([m.get(txt[n+2], txt[n+2]),
m.get(txt[n+1], txt[n+1]),
v])
n += 3
continue
if m.get(txt[n+2]) in self.post:
out.extend([m.get(txt[n+1], txt[n+1]),
m.get(txt[n+2], txt[n+2]),
v])
n += 3
continue
if n+1 < len(txt):
# Handle two consecutive "െ"s, eg: കൈ
if m.get(txt[n+1]) == "െ":
out.extend([m.get(txt[n+2], txt[n+2]),
m.get(txt[n+1], txt[n+1]), v])
n += 3
continue
out.extend([m.get(txt[n+1], txt[n+1]), v])
n += 2
continue
out.append(v)
n += 1
return "".join(out)
if __name__ == "__main__":
c = Convert("./maps")
# Load one or more fonts
font = "ML-TTRevathi"
c.load(font)
print(c.convert("DÕmlw", font))