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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# vim:set et sts=4 sw=4:
# -*- coding: utf-8 -*-
#
# ibus-anthy - The Anthy engine for IBus
#
# Copyright (c) 2007-2008 Peng Huang <shawn.p.huang@gmail.com>
# Copyright (c) 2007-2010 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
import romaji
import kana
import thumb
from segment import unichar_half_to_full
HalfSymbolTable = {}
for i in range(32, 127):
if not chr(i).isalnum():
HalfSymbolTable[unichar_half_to_full(chr(i))] = chr(i)
HalfNumberTable = {}
for i in range(10):
HalfNumberTable[unichar_half_to_full(str(i))] = str(i)
PeriodTable = {u'。': u'.', u'、': u',', u'。': u'.', u'、': u','}
SymbolTable = {}
SymbolTable[0] = {u'「': u'「', u'」': u'」', u'/': u'/'}
SymbolTable[1] = {u'「': u'「', u'」': u'」', u'/': u'・'}
SymbolTable[2] = {u'「': u'[', u'」': u']', u'/': u'/'}
SymbolTable[3] = {u'「': u'[', u'」': u']', u'/': u'・'}
TYPING_MODE_ROMAJI, \
TYPING_MODE_KANA, \
TYPING_MODE_THUMB_SHIFT = range(3)
class JaString:
def __init__(self, mode=TYPING_MODE_ROMAJI):
self.__mode = mode
self.reset()
def reset(self):
self.__cursor = 0
self.__segments = list()
def set_mode(self, mode):
self.__mode = mode
self.reset()
def insert(self, c):
segment_before = None
segment_after = None
new_segments = None
if self.__cursor >= 1:
segment_before = self.__segments[self.__cursor - 1]
if self.__cursor < len(self.__segments):
segment_after = self.__segments[self.__cursor]
if segment_before and not segment_before.is_finished():
new_segments = segment_before.append(c)
elif segment_after and not segment_after.is_finished():
new_segments = segment_after.prepend(c)
else:
if c != u"\0" and c != u"":
if self.__mode == TYPING_MODE_ROMAJI:
new_segments = [romaji.RomajiSegment(c)]
elif self.__mode == TYPING_MODE_KANA:
new_segments = [kana.KanaSegment(c)]
elif self.__mode == TYPING_MODE_THUMB_SHIFT:
new_segments = [thumb.ThumbShiftSegment(c)]
if new_segments:
self.__segments[self.__cursor:self.__cursor] = new_segments
self.__cursor += len(new_segments)
def remove_before(self):
index = self.__cursor - 1
if index >= 0:
segment = self.__segments[index]
segment.pop()
if segment.is_empty():
del self.__segments[index]
self.__cursor = index
return True
return False
def remove_after(self):
index = self.__cursor
if index < len(self.__segments):
segment = self.__segments[index]
segment.pop()
if segment.is_empty():
del self.__segments[index]
return True
return False
def get_string(self, type):
pass
def move_cursor(self, delta):
self.__cursor += delta
if self.__cursor < 0:
self.__cursor = 0
elif self.__cursor > len(self.__segments):
self.__cursor = len(self.__segments)
def _chk_text(self, s):
period = self._prefs.get_value('common', 'period_style')
symbol = self._prefs.get_value('common', 'symbol_style')
half_symbol = self._prefs.get_value('common', 'half_width_symbol')
half_number = self._prefs.get_value('common', 'half_width_number')
ret = ''
for c in s:
c = c if not period else PeriodTable.get(c, c)
c = c if not symbol else SymbolTable[symbol].get(c, c)
c = c if not half_symbol else HalfSymbolTable.get(c, c)
c = c if not half_number else HalfNumberTable.get(c, c)
ret += c
return ret
def get_hiragana(self, commit=False):
conv = lambda s: s.to_hiragana()
R = lambda s: s if not (commit and s[-1:] == u'n') else s[:-1] + u'ん'
text_before = R(u"".join(map(conv, self.__segments[:self.__cursor])))
text_after = R(u"".join(map(conv, self.__segments[self.__cursor:])))
return self._chk_text(text_before + text_after), len(text_before)
def get_katakana(self, commit=False):
conv = lambda s: s.to_katakana()
R = lambda s: s if not (commit and s[-1:] == u'n') else s[:-1] + u'ン'
text_before = R(u"".join(map(conv, self.__segments[:self.__cursor])))
text_after = R(u"".join(map(conv, self.__segments[self.__cursor:])))
return self._chk_text(text_before + text_after), len(text_before)
def get_half_width_katakana(self, commit=False):
conv = lambda s: s.to_half_width_katakana()
R = lambda s: s if not (commit and s[-1:] == u'n') else s[:-1] + u'ン'
text_before = R(u"".join(map(conv, self.__segments[:self.__cursor])))
text_after = R(u"".join(map(conv, self.__segments[self.__cursor:])))
return self._chk_text(text_before + text_after), len(text_before)
def get_latin(self):
conv = lambda s: s.to_latin()
text_before = u"".join(map(conv, self.__segments[:self.__cursor]))
text_after = u"".join(map(conv, self.__segments[self.__cursor:]))
return text_before + text_after, len(text_before)
def get_wide_latin(self):
conv = lambda s: s.to_wide_latin()
text_before = u"".join(map(conv, self.__segments[:self.__cursor]))
text_after = u"".join(map(conv, self.__segments[self.__cursor:]))
return text_before + text_after, len(text_before)
def is_empty(self):
return all(map(lambda s: s.is_empty(), self.__segments))
def get_raw(self, start, end):
i = 0
r = u''
for s in self.__segments:
if i >= end:
break
elif start <= i:
r += s.to_latin()
i += len(s.to_hiragana())
return r
|