forked from cvpe/Pythonista-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Japanese Braille Input.py
1181 lines (1109 loc) · 40.5 KB
/
Japanese Braille Input.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# https://forum.omz-software.com/topic/5584/braille-application
#
# Version 1.9
# - mod: title of "kanjis_other" button = next other explanation
# title of "kanjis_ok" button = actual explanation
# set to next when "kanjis_other" pressed
# Version 1.8
# - mod: some Python improvements adviced by @ccc
# - mod: 'other explanation' button will generate next sentence in English
# instead of Japanese
# Version 1.7
# - mod: title of kanjis_other button: "other explanation" in Japonese
# - new: dot-5 may be used as a prefix for dakuten
# or alone as interwords point '・'
# if character after dot-5 is not a character waited after the prefix,
# it will be processed as a dot-5 point
# - bug: dot-5 ok, dot-1 ok displays char 1 but this char is not generated
# - del: remove support of setting dots position by touching the screen with
# six fingers during more than 6 seconds.
# this process is not compatible with existence of other buttons
# remove usage of keychain for saving these positions
# Version 1.6
# - new: additional button to replace title of conversion selection button
# by another sentence to be spoken by VoiceOver at next tap.
# Sentences come from SentencesEngJpn.dat file which could contain
# several different sentences per Kanji.
# ================ functionality to be approved by user
# Version 1.5
# - new: set color of title of buttons which have a background image
# as transparent, so title is invisible while it will
# still be read by VoiceOver
# nb: conversion button does not have a background image,
# --- so its title will stay visible
# - mod: user confirmed that he/she does not need support of space/blank
# - new: suppport user additional "Hirgana -> Kanji" via a
# HiraganaToKanji.txt file containing one line by additional item
# a tab is needed between hirgana and kanji
# Version 1.4
# - mod: remove variable title of up and down scroll buttons in list
# to avoid confusion with ok button when using VoiceOver
# nb: user has confirmed he/she prefers this way to work,
# === I leave some lines as commented, if user changes his/her opinion
# - new: support some punctuations by pair: () 「」
# with only one dots-character as delimiter
# Version 1.3
# - new: buttons up,ok,down in conversion list will have as variable title
# a sentence as example of the kanji which would be generated if
# pressed
# needs SentencesEngJpn.dat file in same folder as script
# Version 1.2
# - bug: left delete during tableview does not hide scroll and ok 3 buttons
# - new: buttons title instead of image, so VoiceOver may speach it
# - title in Japanese
# - background_image instead of image so visible even if long title
# - new: buttons up,ok,down in conversion list will have as variable title
# the kanji which would be generated if pressed
# - mod: speech commented (temporary?)
# Version 1.1
# - bug: dot 5 prefix does not work since dot-5 point
# temporary remove dot 5 point
# Version 1.0
# - new: use ObjectiveC AVSpeechSynthesizer instead of Pythonista speech
# because speech does not work on iPad mini 4 and iPhone XS Max
# Version 0.9
# - new: support some punctuation with 2 characters: —
# - new: speech selected Kanji sent to TextField
# - mod: bigger font and rowheight of Kanji's conversion list
# - new: kanji's tableview scroll and select via buttons
# - current element is red, if you tap ✅, it will be sent to Textfield
# - bug: I can't actually put this current element at first visible
# row because content_offset does not work like I think
# wait and see
# Version 0.8
# - new: support Yō-on, displayed as Katakana
# - new: support arab digits with special process:
# prefix is valable until next char is not a digit
# or an hyphen which is not displayed.
# But the following process has to be done by user:
# "Words immediately follow numbers, unless they begin with a vowel or
# with r-.
# Because the syllables a i u e o and ra ri ru re ro are homographic
# with the digits 0–9, an hyphen is inserted to separate them.
# Thus 6人 "six people" (6 nin) is written w/o hyphen'' ⠼⠋⠇⠴ ⟨6nin)
# but 6円 "six yen" (6 en) is written with a hyphen, ⠼⠋⠤⠋⠴ ⟨6-en⟩,
# because ⠼⠋⠋⠴ would be read as ⟨66n⟩."
# - new: support Japanese punctuation of one single dots set: -。?!、・
# Version 0.7
# - bug corrected: sqlite3 operationalerror accessing HiraganaToKanji.db
# was not intercepted
# - new: cursor left and right move buttons
# - new: support Hirgana with one/dot prefix for (han)dakuten
# Version 0.6
# - bug corrected: right column of vertical dots had inverted n°s
# Version 0.5
# - new: if conversion doesn't give any Kanji, send Hirgana's to TextField
# without passing via the TextView
# Version 0.4
# - new: add Hirgana's as 1st element in Kanji's list, in green color
# - new: supports Braille dots buttons
# - vertically : default or argument v
# - horizontally: argument h
# - new: delete button deletes temporary dots character if in progress
# - bug corrected: delete button during Kanji selection did not hide
# the Kanji's list TableView
# Version 0.3
# - new: use keychain to save and restore Braille dots positions
# service=Braille account=Portrait or Landscape
# password = '{'dot n°':(x,y),...}'
# - bug corrected: if dots not tapped together, some ones can be lost
# due to reuse by system of same touch_id
# Version 0.2
# - bug corrected: back after close (x), gray hirgana was not cleared
# - bug corrected: TextField not visible on iPhone
# - bug corrected: automatic buttons dimensions and positions for
# ipad/iphone portrait/landscape Pythonista/custom keyboard
# Version 0.1
# - bug corrected: delete when cursor in TextField shows old Hirganas
# - bug corrected: tap ourside buttons was processed as invalid Braille dots
# - bug corrected: conversion button disappears if conversion gives no Kanji
# - bug corrected: hide conversion buttons if all hirganas deleted
# - bug corrected: back after close (x), closed conversion db error
# - bug corrected: back after close (x), hirganas were not cleared
# - bug corrected: ok button was even if dots combination was invalid
# - new: delete button deletes in textfield if no Hirgana in progress
# Version 0.0
# - initial draft version
#
# still todo
# ==========
"""
questions
- Q1: since some versions, I've commented some lines to avoid confusion
between sentences spoken by the script or by VoiceOver.
Could you describe in which cases the script has to speech text?
"""
# - add: get English translation of Kanji's?
# - mod: functionaly "other sentence" yes/no set by program initialization
# - new: speech of Kanji sent to TextField
# - bug: content_offset not ok for tableview
# - new: support ponctuation multiple chars
# - new: support ponctuation multiple chars used as start and end
# - bug: left delete should delete also the prefix if exists
import ast
import console
import Image, ImageDraw
import io
from objc_util import *
import os
import plistlib
import speech
import sqlite3
import sys
from typing import Tuple
import ui
AVSpeechUtterance=ObjCClass('AVSpeechUtterance')
AVSpeechSynthesizer=ObjCClass('AVSpeechSynthesizer')
AVSpeechSynthesisVoice=ObjCClass('AVSpeechSynthesisVoice')
voices=AVSpeechSynthesisVoice.speechVoices()
def get_synthesizer_and_voice(language: str = 'ja-JP') -> Tuple[ObjCInstance, ObjCInstance]:
synthesizer=AVSpeechSynthesizer.new()
for voice in AVSpeechSynthesisVoice.speechVoices():
# print(voice, voice.description())
if language in str(voice.description()):
return synthesizer, voice
raise ValueError(f"No voice found for {language}")
# @ccc code to get Pythonista Version
# https://github.com/cclauss/Ten-lines-or-less/blob/master/pythonista_version.py
def pythonista_version(): # 2.0.1 (201000)
plist = plistlib.readPlist(os.path.abspath(os.path.join(sys.executable, '..', 'Info.plist')))
return '{CFBundleShortVersionString} ({CFBundleVersion})'.format(**plist)
w = pythonista_version() # ex: 3.3 (330012)
PythonistaVersion = float(w.split(' ')[0])
#print(PythonistaVersion)
if PythonistaVersion >= 3.3:
import keyboard
class BrailleKeyboardInputAccessoryViewForTextField(ui.View):
def __init__(self, frame=None,*args, **kwargs):
super().__init__(self, *args, **kwargs)
self.background_color = 'white'
self.multitouch_enabled = True
self.touch_actives = {}
self.touch_n = 0
if frame:
self.frame=frame
# sources
# -------
# https://fr.wikipedia.org/wiki/Braille_japonais
# http://web.archive.org/web/20090807085414/http:/www.hi.sfc.keio.ac.jp/access/arc/NetBraille/etc/brttrl.html#3.5
# https://en.wikipedia.org/wiki/Japanese_Braille
# basic syllabes
self.Japanese_Braille = {
'1':'あ', # ⠁
'12':'い', # ⠃
'14':'う', # ⠉
'124':'え', # ⠋
'24':'お', # ⠊
'16':'か', # ⠡
'126':'き', # ⠣
'146':'く', # ⠩
'1246':'け', # ⠫
'246':'こ', # ⠪
'156':'さ', # ⠱
'1256':'し', # ⠳
'1456':'す', # ⠹
'12456':'せ', # ⠻
'2456':'そ', # ⠺
'1256':'し', # ⠳
'1456':'す', # ⠹
'12456':'せ', # ⠻
'2456':'そ', # ⠺
'135':'た', # ⠕
'1235':'ち', # ⠗
'1345':'つ', # ⠝
'12345':'て', # ⠟
'2345':'と', # ⠞
'13':'な', # ⠅
'123':'に', # ⠇
'134':'ぬ', # ⠍
'1234':'ね', # ⠏
'234':'の', # ⠎
'136':'は', # ⠥
'1236':'ひ', # ⠧
'1346':'ふ', # ⠭
'12346':'へ', # ⠯
'2346':'ほ', # ⠮
'1356':'ま', # ⠵
'12356':'み', # ⠷
'13456':'む', # ⠽
'123456':'め', # ⠿
'23456':'も', # ⠾
'356':'ん', # ⠴
'34':'や', # ⠌
'346':'ゆ', # ⠬
'345':'よ', # ⠜
'15':'ら', # ⠑
'125':'り', # ⠓
'145':'る', # ⠙
'1245':'れ', # ⠛
'245':'ろ' , # ⠚
'3':'わ', # ⠄
'23':'ゐ', # ⠆
'235':'ゑ', # ⠖
'35':'を', # ⠔
# dakuten list from http://www.yoihari.com/tenji/tdaku.htm
'5':'_', # needs another 2nd character
#'5':'・' , # or interwords point if next char not in list
'5|16':'', # ⠐⠡
'5|126':'', #
'5|146':'', #
'5|1246':'', #
'5|246':'', #
'5|156':'', #
'5|1256':'', #
'5|1456':'', #
'5|12456':'', #
'5|2456':'', #
'5|135':'', #
'5|1235':'', #
'5|1345':'', #
'5|12345':'', #
'5|2345':'', #
'5|136':'', #
'5|1236':'', #
'5|1346':'', #
'5|12346':'', #
'5|2346':'', #
'6':'_', # needs another 2nd character
'6|136':'', #
'6|1236':'', #
'6|1346':'', #
'6|12346':'', #
'6|2346':'', #
# Yō-on http://www.yoihari.com/tenji/tyou.htm
'4':'_', # needs another 2nd character
'4|16':'キャ', #
'4|146':'キュ',#
'4|246':'キョ',#
'4|156':'シャ',#
'4|1456':'シュ',#
'4|2456':'ショ',#
'4|135':'ヂャ',#
'4|1345':'チュ',#
'4|2345':'チョ',#
'4|13':'ニャ', #
'4|134':'ニュ',#
'4|234':'ニョ',#
'4|136':'ヒャ',#
'4|1346':'ヒュ',#
'4|2346':'ヒョ',#
'4|1356':'ミャ',#
'4|13456':'ミュ',#
'4|23456':'ミョ',#
'4|15':'リャ', #
'4|145':'リュ',#
'4|245':'リョ',#
'46':'_', # needs another 2nd character
'46|136':'ピャ', #
'46|1346':'ピュ',#
'46|2346':'ピョ',#
'45':'_', # needs another 2nd character
'45|16':'ギャ', #
'45|146':'ギュ', #
'45|246':'ギョ', #
'45|156':'ジャ', #
'45|1456':'ジュ',#
'45|2456':'ジョ',#
'45|135':'ヂャ', #
'45|1345':'ヂュ',#
'45|2345':'ヂョ',#
'45|136':'ビャ', #
'45|1346':'ビュ',#
'45|2346':'ビョ',#
# arab digits http://www.yoihari.com/tenji/tsuji.htm
'3456':'_', # needs another 2nd character
'3456|1':'1', #
'3456|12':'2', #
'3456|14':'3', #
'3456|145':'4', #
'3456|15':'5', #
'3456|124':'6', #
'3456|1245':'7',#
'3456|125':'8', #
'3456|24':'9', #
'3456|245':'0', #
# punctuation http://www.yoihari.com/tenji/tkigo.htm
# punctuation with 1 character
'36':'-', # hyphen: if end of arab digits, not displayed
'256':'。' , # end point
'26':'?', #
'235':'!', #
'56':'、', # comma
#'5':'・' , # interwords point, see above '5' as prefix
# punctuation with 2 characters
'25':'_', # needs another 2nd character
'25|25':'—', #
#'36|36':'~', # how to distinguish with hyphen?
# punctuation with 3 characters
#'25|25|134':'→',#
#'246|25|25':'←',#
#'2|2|2':'...', #
# punctuation with 4 characters
#'246|25|25|134':'⟷',#
# punctuation with start and end characters
'2356':'()', #
'36':'「」' #
}
# Generate dakuten characters from their prefix/dot and dots
for ele in self.Japanese_Braille:
prefix, bar, k = ele.partition('|') # ex 5, |, 1345
if bar:
if prefix == '5':
d = 1 # ex: 5 -> 1
elif prefix == '6':
d = 2
else:
continue # ex: 4
ch = self.Japanese_Braille[k] # ex: つ
b = ch.encode('utf-8') # ex: b'\xe3\x81\x8b'
n = b[:-1] + bytes([int(b[-1])+d])# ex: b'\xe3\x81\x8c'
c = str(n,'utf-8') # ex: か -> が へ -> ぺ
self.Japanese_Braille[ele] = c
self.prefix = ''
self.parentheses = False
self.singular_quotation_marks = False
# other symbols exist: sokuon, chōon, yōon, handakuten, gōyōon
# see https://en.wikipedia.org/wiki/Japanese_Braille
# for their dots combinations
# https://github.com/Doublevil/JmdictFurigana
self.conn = sqlite3.connect("HiraganaToKanji.db",check_same_thread=False)
self.cursor = self.conn.cursor()
# read and store eventual supplementar Kanji's
suppl_kanjis = 'HiraganaToKanji.txt'
if os.path.exists(suppl_kanjis):
with open(suppl_kanjis,encoding='utf-8') as fil:
self.local_kanjis = fil.read().split('\n')
else:
self.local_kanjis = []
# get sentences as examples for Kanjis
# https://www.manythings.org/anki/
with open('SentencesEngJpn.dat',encoding='utf-8') as fil:
self.sentences = fil.read().split('\n')
self.voice_def = None
# build dots buttons but bounds not yet known in init, let some delay
ui.delay(self.dimensions,0.1)
def dimensions(self):
wk, hk = self.bounds.size
#wk,hk = 756,237 # ipad mini 4
#wk,hk = 320,237 # iphone 5S
d = 48 # size of other buttons (close, delete, ...)
db = 4
z = 'v'
if not self.custom_keyboard:
if len(sys.argv) > 1:
z = sys.argv[1]
if z == 'h':
diam = int((wk-d-7*db)/6)
dy = int((hk - d - diam - db)/2)
dx = int((wk - d - 6*diam)/7)
x0 = dx + d
dx = dx + diam
y0 = d
x1 = x0 + 3*dx
else:
diam = (hk - 4*db)/3
dx = 0
dy = diam + db
x0 = db + d + db
y0 = db
x1 = wk - x0 - diam
#print(wk,hk,r,d)
x = x0
y = y0
for i in range(1,7):
b = ui.Button()
b.name = str(i)
b.background_color = (1,0,0,0.5)
b.tint_color = (1,1,1,0.8)
b.font = ('Academy Engraved LET',diam/2)
b.corner_radius = diam/2
b.title = b.name
b.frame = (x,y,diam,diam)
#b.TextField = tf # store tf as key attribute needed when pressed
b.touch_enabled = False
self.add_subview(b)
x = x + dx
if i < 3:
y = y + dy
if i == 3:
x = x1
if z == 'v':
y = y0
dy = -dy
elif i > 3:
y = y - dy
self.buttons_titles = {
'b_close':'キーボードを閉じる',
'b_delete':'左削除',
'b_left':'左に移動',
'b_right':'右に動く',
'b_decision':'点字OK',
'b_conversion':'漢字',
'kanjis_up':'漢字アップ',
'kanjis_down':'漢字',
'kanjis_other':'その他の説明', # add 1.6 # mod 1.7
'kanjis_ok':'漢字は大丈夫'
}
self.select_text = '選択する '
b_close = ui.Button(name='b_close')
b_close.frame = (2,2,d,d)
b_close.corner_radius = d/2
b_close.background_color = (0.8,0,0,0.5)
b_close.background_image = ui.Image.named('iob:ios7_close_outline_32')
b_close.action = self.close_button_action
self.add_subview(b_close)
b_delete = ui.Button(name='b_delete')
b_delete.frame = (2,hk-d-10,d,d)
b_delete.corner_radius = 24
b_delete.background_color = (0.8,0,0,0.5)
b_delete.background_image = ui.Image.named('typb:Delete')
b_delete.action = self.delete_button_action
self.add_subview(b_delete)
b_left = ui.Button(name='b_left')
b_left.frame = (wk/2-d-10,hk-d-10,d,d)
b_left.corner_radius = 24
b_left.background_color = (0.8,0,0,0.5)
b_left.background_image = ui.Image.named('typb:Left')
b_left.action = self.left_button_action
self.add_subview(b_left)
b_right = ui.Button(name='b_right')
b_right.frame = (wk/2+10,hk-d-10,d,d)
b_right.corner_radius = 24
b_right.background_color = (0.8,0,0,0.5)
b_right.background_image = ui.Image.named('typb:Right')
b_right.action = self.right_button_action
self.add_subview(b_right)
b_decision = ui.Button(name='b_decision')
b_decision.frame = (wk-d-2,hk-d-10,d,d)
b_decision.corner_radius = 24
b_decision.background_color = (0.8,0,0,0.5)
b_decision.background_image = ui.Image.named('iob:ios7_checkmark_outline_32')
b_decision.action = self.decision_button_action
b_decision.hidden = True
self.add_subview(b_decision)
dots = ui.ImageView(name='dots')
self.dots_e = e = 3
self.dots_d = d = 9
self.dots_h = h = 4*e + 3*d
self.dots_w = wd = 14 + d + e
dots.frame = (100,0,wd,h)
dots.hidden = True
self.add_subview(dots)
self.dots_xy = [(e,e),(e,e+(d+e)),(e,e+2*(d+e)),(14,e),(14,e+(d+e)),(14,e+2*(d+e))]
prefix_dots = ui.ImageView(name='prefix_dots')
prefix_dots.frame = (100,0,wd,h)
prefix_dots.hidden = True
self.add_subview(prefix_dots)
self.dots_xy = [(e,e),(e,e+(d+e)),(e,e+2*(d+e)),(14,e),(14,e+(d+e)),(14,e+2*(d+e))]
hirganas = ui.Label(name='hirganas')
hirganas.frame = (wk/2,2,0,32)
hirganas.text = ''
hirganas.font = ('Menlo',32)
hirganas.text_color = (0,0,1,1)
hirganas.border_color = 'lightgray'
hirganas.border_width = 1
self.add_subview(hirganas)
self.hirganas = []
hirgana = ui.Label(name='hirgana')
hirgana.frame = (0,0,32,32)
hirgana.text = ''
hirgana.font = ('Menlo',32)
hirgana.text_color = 'gray'
hirganas.add_subview(hirgana)
b_conversion = ui.Button(name='b_conversion')
b_conversion.frame = (0,2,32,32)
b_conversion.corner_radius = 32/2
b_conversion.background_color = (0.8,0,0,0.5)
b_conversion.title = '漢字'
b_conversion.hidden = True
b_conversion.background_image = None
b_conversion.action = self.conversion_button_action
self.add_subview(b_conversion)
kanjis = ui.TableView(name='kanjis')
kanjis.frame = (0,2+32,32,hk-(2+32+2))
kanjis.allows_multiple_selection = False
kanjis.border_color = 'lightgray'
kanjis.border_width = 1
kanjis.corner_radius = 5
kanjis.data_source = ui.ListDataSource(items=[])
kanjis.data_source.font = ('Menlo',64)
kanjis.row_height = 64
kanjis.delegate = self
kanjis.data_source.tableview_cell_for_row = self.tableview_cell_for_row
kanjis.hidden = True
self.add_subview(kanjis)
# up, down play on content_offset of TableView (subclass of ScrollView)
# y positions so buttons are equidistants
# x position set later when tableview width is set
h = kanjis.height
d_b = 64
e_b = (h-3*d_b)/4
b1 = ui.Button(name='kanjis_up')
b1.background_image = ui.Image.named('iob:arrow_up_c_32')
b1.background_color = (0,1,0,0.5)
y = kanjis.y + e_b
b1.frame =(0,y,d_b,d_b)
b1.corner_radius = b1.width/2
b1.hidden = True
b1.action = self.tableview_up
self.add_subview(b1)
b2 = ui.Button(name='kanjis_ok')
b2.background_image = ui.Image.named('iob:checkmark_round_32')
b2.background_color = (0,1,0,0.5)
y = y + d_b + e_b
b2.frame =(0,y,d_b,d_b)
b2.corner_radius = b2.width/2
b2.hidden = True
b2.action = self.tableview_ok
self.add_subview(b2)
b4 = ui.Button(name='kanjis_other') # add 1.6
b4.background_image = ui.Image.named('iob:refresh_32') # add 1.6
b4.background_color = (0,1,0,0.5) # add 1.6
b4.frame =(0,y,d_b,d_b) # add 1.6
b4.corner_radius = b2.width/2 # add 1.6
b4.hidden = True # add 1.6
b4.action = self.tableview_other # add 1.6
self.add_subview(b4) # add 1.6
b3 = ui.Button(name='kanjis_down')
b3.background_image = ui.Image.named('iob:arrow_down_c_32')
b3.background_color = (0,1,0,0.5)
y = y + d_b + e_b
b3.frame =(0,y,d_b,d_b)
b3.corner_radius = b3.width/2
b3.hidden = True
b3.action = self.tableview_down
self.add_subview(b3)
for sv in self.subviews:
if isinstance(sv, ui.Button):
sv_title = self.buttons_titles.get(sv.name, '')
if sv_title:
sv.title = sv_title
if sv.background_image:
sv.tint_color = (0,0,0,0) # title color transparent so invisible
# but title still said by VoiceOver
sv.image = None
#sv.background_image = None
def left_button_action(self,sender):
# move cursor left in textfield
if self.custom_keyboard:
keyboard.move_cursor(-1)
else:
cursor = self.tfo.offsetFromPosition_toPosition_(self.tfo.beginningOfDocument(), self.tfo.selectedTextRange().start())
if cursor <= 0:
return
cursor = cursor - 1
# set cursor
cursor_position = self.tfo.positionFromPosition_offset_(self.tfo.beginningOfDocument(), cursor)
self.tfo.selectedTextRange = self.tfo.textRangeFromPosition_toPosition_(cursor_position, cursor_position)
def right_button_action(self,sender):
# move cursor right in textfield
if self.custom_keyboard:
keyboard.move_cursor(+1)
else:
cursor = self.tfo.offsetFromPosition_toPosition_(self.tfo.beginningOfDocument(), self.tfo.selectedTextRange().start())
cursor = cursor + 1
# set cursor
cursor_position = self.tfo.positionFromPosition_offset_(self.tfo.beginningOfDocument(), cursor)
self.tfo.selectedTextRange = self.tfo.textRangeFromPosition_toPosition_(cursor_position, cursor_position)
def close_button_action(self,sender):
#self.conn.close()
if self.touch_actives != {}:
self.touch_n = 0
self.touch_actives = {}
self.prefix = ''
self['dots'].hidden = True
self['prefix_dots'].hidden = True
self['kanjis'].hidden = True
self['hirganas'].text = ''
self['hirganas']['hirgana'].text = ''
self.hirganas =[]
self['b_conversion'].hidden = True
self['hirganas'].width = 0
if not self.custom_keyboard:
self.tf.end_editing()
return
# we simulate 'dismiss keybord key' pressed
o = ObjCInstance(sender) # objectivec button
while True:
o = o.superview()
if 'KeyboardInputView' in str(o._get_objc_classname()):
KeyboardInputView = o
break
self.b_lowest_right = None
self.xo = 0
self.yo = 0
def analyze(v):
for sv in v.subviews():
if 'uibuttonlabel' in str(sv._get_objc_classname()).lower():
x = sv.superview().frame().origin.x
y = sv.superview().frame().origin.y
if y > self.yo:
self.b_lowest_right = sv
self.xo = x
self.yo = y
elif y == self.yo:
if x > self.xo:
self.b_lowest_right = sv
self.xo = x
self.yo = y
ret = analyze(sv)
analyze(KeyboardInputView)
b = self.b_lowest_right.superview()
class_name = str(b._get_objc_classname()).lower()
if 'uibutton' in class_name or 'ckbkeybutton' in class_name:
# simulate press the button
UIControlEventTouchUpInside = 255
b.sendActionsForControlEvents_(UIControlEventTouchUpInside)
def delete_button_action(self,sender):
if self['hirganas']['hirgana'].width > 0:
# temporary hirgana in progress
self['hirganas']['hirgana'].width = 0
self.touch_n = 0
self.touch_actives = {}
self['dots'].hidden = True
self['prefix_dots'].hidden = True
self['b_decision'].hidden = True
self.draw_hirganas()
elif len(self.hirganas) > 0:
# Hirhanas in progress
# process to delete last hirgana
# one hirganas uses a variable number of characters, thus not easy to remove it at right of a text
del self.hirganas[-1]
t = ''
for ch in self.hirganas:
t += ch
self['hirganas'].text = t
self.draw_hirganas()
# if Kanji selection was in progress, cancel it
if not self['kanjis'].hidden:
self.hide_buttons(True)
#self['kanjis'].hidden = True
#self['kanjis_up'].hidden = True
#self['kanjis_ok'].hidden = True
#self['kanjis_down'].hidden = True
#self['kanjis_other'].hidden = True # add 1.6
else:
# process to delete in textfield
if self.custom_keyboard:
keyboard.backspace(times=1)
else:
cursor = self.tfo.offsetFromPosition_toPosition_(self.tfo.beginningOfDocument(), self.tfo.selectedTextRange().start())
if cursor > 0:
self.tf.text = self.tf.text[:cursor-1] + self.tf.text[cursor:]
cursor = cursor - 1
# set cursor
cursor_position = self.tfo.positionFromPosition_offset_(self.tfo.beginningOfDocument(), cursor)
self.tfo.selectedTextRange = self.tfo.textRangeFromPosition_toPosition_(cursor_position, cursor_position)
def decision_button_action(self,sender):
if self.touch_actives != {}:
self.touch_n = 0
self.touch_actives = {}
self.key_pressed(self.seq)
self['dots'].hidden = True
#self['prefix_dots'].hidden = True
self['b_decision'].hidden = True
def conversion_button_action(self,sender):
t = self['hirganas'].text
items = [t]
try:
self.cursor.execute(
'select hiragana, kanji from Hiragana_to_Kanji where hiragana = ?',
(t,))
except Exception as e:
console.hud_alert('be sure that HiraganaToKanji.db file is present', 'error', 3)
for li in self.local_kanjis:
s = li.split('\t')
try:
if t not in s[0]:
continue
items.append(s[1])
break
except Exception as e:
# lome could be erroneously typed
continue
w_max = 0
for row in self.cursor:
t = row[1]
items.append(t)
for t in items:
w,h = ui.measure_string(t, font=self['kanjis'].data_source.font)
w_max = max(w_max,w+50)
sender.hidden = True
self['kanjis'].data_source.items = items
if len(items) == 1:
self.tableview_did_select(self['kanjis'], 0, 0)
return
# Kanji's exist, display a TableView
self['kanjis'].x = (self.width - w_max)/2
self['kanjis'].width = w_max
self['kanjis'].height = min(self.bounds.size[1]-(2+32+2), len(items)*self['kanjis'].row_height)
#self['kanjis'].hidden = False
x = self['kanjis'].x + self['kanjis'].width + 10
#self['kanjis_up'].hidden = False
self['kanjis_up'].x = x
#self['kanjis_ok'].hidden = False
self['kanjis_ok'].x = x
#self['kanjis_other'].hidden = False # add 1.6
e_b = self['kanjis_ok'].y - self['kanjis_up'].y - self['kanjis_other'].height # add 1.6
self['kanjis_other'].x = self['kanjis'].x - self['kanjis_other'].width - e_b # add 1.6
#self['kanjis_down'].hidden = False
self['kanjis_down'].x = x
self.hide_buttons(False)
self['kanjis'].current = 0
ui.delay(self.tableview_say_current,0.01)
self['kanjis_ok'].title = self.select_text+self.get_kanji(0)
self['kanjis_other'].title = self.select_text + self.get_kanji(0,next_sentence=True) # add 1.9
#self['kanjis_up'].title = self.get_kanji(-1)
#self['kanjis_down'].title = self.get_kanji(+1)
def get_kanji(self,delta,next_sentence=False): # mod 1.6
i = self['kanjis'].current + delta
if i < 0 or i == len(self['kanjis'].data_source.items):
i = self['kanjis'].current
kanji = self['kanjis'].data_source.items[i]
sentence = ''
if not next_sentence: # add 1.6
self.i_sentence = 0 # add 1.6
else: # add 1.6
self.i_sentence = self.i_sentence + 1 # add 1.6
i_found = 0 # add 1.6
for li in self.sentences:
s = li.split('\t')
try:
if kanji not in s[1]:
continue
if not next_sentence: # add 1.8
sentence = s[1] # 1 = Japanese 0 = English (test)
else: # add 1.8
sentence = s[0] # 1 = Japanese 0 = English (test) # add 1.8
#print(sentence)
if i_found != self.i_sentence: # add 1.6
# not yet the right sentence number reached # add 1.6
i_found = i_found + 1 # add 1.6
continue # add 1.6
#print(kanji,s) # test without VoiceOver
break
except Exception as e:
# some lines are blank
continue
kanji = kanji + ' ' + sentence
return kanji
def tableview_cell_for_row(self,tableview, section, row):
cell = ui.TableViewCell()
data = tableview.data_source.items[row]
cell.text_label.font = ('Menlo',32)
#cell.text_label.alignment = ui.ALIGN_LEFT
if row == tableview.current:
cell.text_label.text_color = 'red'
cell.bg_color = 'lightgray'
elif row == 0:
cell.text_label.text_color = 'green'
else:
cell.text_label.text_color = 'black'
cell.text_label.text = data
return cell
def tableview_up(self,sender):
tableview = self['kanjis']
if tableview.current > 0:
tableview.current = tableview.current - 1
self.table_view_scroll(tableview)
ui.delay(self.tableview_say_current,0.01)
#self['kanjis_up'].title = self.get_kanji(-1) # future title to hear
self['kanjis_ok'].title = self.select_text+self.get_kanji(0)
self['kanjis_other'].title = self.select_text + self.get_kanji(0,next_sentence=True) # add 1.9
#self['kanjis_down'].title = self.get_kanji(+1)
def tableview_down(self,sender):
tableview = self['kanjis']
#print(dir(ObjCInstance(tableview)))
if tableview.current < (len(tableview.data_source.items)-1):
tableview.current = tableview.current + 1
self.table_view_scroll(tableview)
ui.delay(self.tableview_say_current,0.01)
#self['kanjis_up'].title = self.get_kanji(-1) # future title to hear
self['kanjis_ok'].title = self.select_text+self.get_kanji(0)
self['kanjis_other'].title = self.select_text + self.get_kanji(0,next_sentence=True) # add 1.9
#self['kanjis_down'].title = self.get_kanji(+1)
def tableview_other(self,sender): # add 1.6
# del 1.9 self['kanjis_ok'].title = self.select_text + self.get_kanji(0,next_sentence=True)
self['kanjis_ok'].title = self['kanjis_other'].title # add 1.9
self['kanjis_other'].title = self.select_text + self.get_kanji(0,next_sentence=True) # add 1.9
def table_view_scroll(self,tableview):
x,y = tableview.content_offset
y = float(tableview.current*tableview.row_height)
#print(y)
tableview.content_offset = (x,y)
tableview.reload()
#tableview.selected_row = tableview.current
def tableview_ok(self,sender):
tableview = self['kanjis']
row = tableview.current
self.tableview_did_select(tableview, 0, row)
def tableview_say_current(self):
return
t = self['kanjis'].data_source.items[self['kanjis'].current]
#speech.say(t,'jp-JP')
utterance = AVSpeechUtterance.speechUtteranceWithString_(t)
#the value that sounds good apparantly depends on ios version
utterance.rate = 0.5
if self.voice_def == None:
# not yet defined
try:
synthesizer, voice = get_synthesizer_and_voice()
except ValueError as e:
#print(e)
synthesizer, voice = get_synthesizer_and_voice("en-US")
console.hud_alert('voice not found, call your support','error')
self.voice_def = voice
self.synthesizer =synthesizer
utterance.voice = self.voice_def
utterance.useCompactVoice = False
self.synthesizer.speakUtterance_(utterance)
def hide_buttons(self, hide: bool = True, field_names: str = "") -> None:
button_names = (field_names or "kanjis kanjis_up kanjis_ok kanjis_down kanjis_other").split()
for button_name in button_names:
self[button_name].hidden = hide
def tableview_did_select(self, tableview, section, row):
tableview.current = row
self.tableview_say_current()
t = tableview.data_source.items[row]
# insert kanji
if self.custom_keyboard:
keyboard.insert_text(t)
else:
cursor = self.tfo.offsetFromPosition_toPosition_(self.tfo.beginningOfDocument(), self.tfo.selectedTextRange().start())
self.tf.text = self.tf.text[:cursor] + t + self.tf.text[cursor:]
cursor = cursor + len(t)
# set cursor
cursor_position = self.tfo.positionFromPosition_offset_(self.tfo.beginningOfDocument(), cursor)
self.tfo.selectedTextRange = self.tfo.textRangeFromPosition_toPosition_(cursor_position, cursor_position)
self.hide_buttons(True)
#self['kanjis'].hidden = True
#self['kanjis_up'].hidden = True
#self['kanjis_ok'].hidden = True
#self['kanjis_down'].hidden = True
#self['kanjis_other'].hidden = True # add 1.6
self['hirganas'].text = ''
self.hirganas =[]
self['b_conversion'].hidden = True
self['hirganas'].width = 0
def touch_began(self,touch):
#print('touch_began',touch.location)
bn = self.dot_touched(touch)
if bn == '':
return
x0,y0 = touch.location
self.touch_actives[touch.touch_id] = ((x0,y0),bn,touch.location)
self.touch_n += 1
self.dots_touched()
def touch_moved(self,touch):
if touch.touch_id not in self.touch_actives:
return
bn = self.dot_touched(touch)
x0,y0 = self.touch_actives[touch.touch_id][0]
self.touch_actives[touch.touch_id] = ((x0,y0),bn,touch.location)
self.dots_touched()
def touch_ended(self,touch):
if touch.touch_id not in self.touch_actives:
return
x ,y = touch.location
self.touch_n -= 1 # but keep dict of touches
# change key in self.touch_actives because if a new touch
# begins, its touch_id could be reused, thus the same
new_key = 'T'+str(len(self.touch_actives))+str(touch.touch_id)
self.touch_actives[new_key] = self.touch_actives[touch.touch_id]
del self.touch_actives[touch.touch_id]
if self.touch_n > 0:
# still at least one finger on screen
return
# all fingers removed from screen
return #-------------------------- wait decision button pressed ------
def dot_touched(self,touch):
xt,yt = touch.location
for b in self.subviews:
if not isinstance(b, ui.Button):
continue
if b.name[0] == 'b': # not dots button
continue
r = b.width/2
x = b.x + r
y = b.y + r
if ((xt-x)**2+(yt-y)**2) <= r**2:
return b.name
return ''
def draw_dots(self,imageview,seq):
im = Image.new("RGB", (self.dots_w,self.dots_h), 'white')
draw = ImageDraw.Draw(im)
for c in range(1,7):
x,y = self.dots_xy[c-1]
draw.ellipse((x,y,x+10,y+10),'lightgray','lightgray')
for ch in seq:
c = int(ch)
x,y = self.dots_xy[c-1]
draw.ellipse((x,y,x+10,y+10),'red','red')
del draw
with io.BytesIO() as fp: