-
Notifications
You must be signed in to change notification settings - Fork 0
/
unicoder.py
executable file
·1711 lines (1637 loc) · 54.6 KB
/
unicoder.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
#! /usr/bin/python3
from __future__ import print_function
__copyright__ = "(C) 2021-2024 Guido U. Draheim, licensed under the APLv2"
__version__ = "1.3.3501"
from typing import List, Dict, Generator, Tuple, Optional
from io import StringIO
import sys
import os
import logging
logg = logging.getLogger("UNICODER")
if True:
norm_base_A = ord('A')
norm_base_Z = ord('Z')
norm_base_a = ord('a')
norm_base_z = ord('z')
norm_base_0 = ord('0')
norm_base_9 = ord('9')
norm_base_sz = 0xDF
ital_base_A = 0x1D434
ital_base_Z = 0x1D44D
ital_base_a = 0x1D44E
ital_base_z = 0x1D467
ital_sans_A = 0x1D608
ital_sans_Z = 0x1D621
ital_sans_a = 0x1D622
ital_sans_z = 0x1D63B
bold_base_A = 0x1D400
bold_base_Z = 0x1D419
bold_base_a = 0x1D41A
bold_base_z = 0x1D433
bold_base_0 = 0x1D7CE
bold_base_9 = 0x1D7D7
bold_ital_base_A = 0x1D468
bold_ital_base_Z = 0x1D481
bold_ital_base_a = 0x1D482
bold_ital_base_z = 0x1D49B
#
norm_sans_A = 0x1D5A0
norm_sans_Z = 0x1D5B9
norm_sans_a = 0x1D5BA
norm_sans_z = 0x1D5D3
norm_sans_0 = 0x1D7E2
norm_sans_9 = 0x1D7EB
bold_sans_A = 0x1D5D4
bold_sans_Z = 0x1D5ED
bold_sans_a = 0x1D5EE
bold_sans_z = 0x1D607
bold_sans_0 = 0x1D7EC
bold_sans_9 = 0x1D8F5
ital_sans_A = 0x1D608
ital_sans_Z = 0x1D621
ital_sans_a = 0x1D622
ital_sans_z = 0x1D63B
bold_ital_sans_A = 0x1D63C
bold_ital_sans_Z = 0x1D655
bold_ital_sans_a = 0x1D656
bold_ital_sans_z = 0x1D66F
bold_sans_A = 0x1D5D4
bold_sans_Z = 0x1D5ED
bold_sans_a = 0x1D5EE
bold_sans_z = 0x1D607
#
norm_fraktur_A = 0x1D504
norm_fraktur_Y = 0x1D51C
norm_fraktur_a = 0x1D51E
norm_fraktur_z = 0x1D537
bold_fraktur_A = 0x1D56C
bold_fraktur_Z = 0x1D585
bold_fraktur_a = 0x1D586
bold_fraktur_z = 0x1D59F
# ital_fraktur_A = 0x1D56C
# ital_fraktur_Z = 0x1D585
# ital_fraktur_a = 0x1D586
# ital_fraktur_z = 0x1D59F
# bold_ital_fraktur_A = n/a
# bold_ital_fraktur_Z = n/a
# bold_ital_fraktur_a = n/a
# bold_ital_fraktur_z = n/a
#
norm_script_A = 0x1D49C
norm_script_Z = 0x1D4B5
norm_script_a = 0x1D4B6
norm_script_z = 0x1D4CF
bold_script_A = 0x1D4D0
bold_script_Z = 0x1D4E9
bold_script_a = 0x1D4EA
bold_script_z = 0x1D503
#
norm_double_A = 0x1D538
norm_double_Y = 0x1D550
norm_double_a = 0x1D552
norm_double_z = 0x1D56B
norm_double_0 = 0x1D7D8
norm_double_9 = 0x1D7E1
norm_mono_A = 0x1D670
norm_mono_Z = 0x1D689
norm_mono_a = 0x1D68A
norm_mono_z = 0x1D6A3
norm_mono_0 = 0x1D7F6
norm_mono_9 = 0x1D7FF
norm_parens_A = 0x1F110 # PARENTHESIZED CAPITAL
norm_parens_O = 0x1F11E # PARENTHESIZED CAPITAL
norm_parens_Z = 0x1F129 # PARENTHESIZED CAPITAL
norm_parens_a = 0x249C # PARENTHESIZED SMALL
norm_parens_o = 0x24AA # PARENTHESIZED SMALL
norm_parens_z = 0x24B5 # PARENTHESIZED SMALL
norm_parens_1 = 0x2472 # PARENTHESIZED DIGIT
norm_parens_9 = 0x247A # PARENTHESIZED DIGIT
norm_parens_20 = 0x2485 # PARENTHESIZED DIGIT
#
norm_circled_A = 0x24B6 # CIRCLED DIGIT
norm_circled_O = 0x24C4 # CIRCLED DIGIT
norm_circled_Z = 0x24CF # CIRCLED DIGIT
norm_circled_a = 0x24D0 # CIRCLED DIGIT
norm_circled_z = 0x24E9 # CIRCLED DIGIT
norm_circled_0 = 0x24EA # CIRCLED DIGIT
norm_circled_1 = 0x245E # CIRCLED DIGIT
norm_circled_9 = 0x2466 # CIRCLED DIGIT
norm_circled_20 = 0x2471 # CIRCLED DIGIT
norm_circled_sans_1 = 0x2780 # NEGATIVE CIRCLED SANS-SERIF DIGIT (dingbats)
norm_circled_sans_9 = 0x2788 # NEGATIVE CIRCLED SANS-SERIF DIGIT (dingbats)
norm_circled_sans_10 = 0x2789 # NEGATIVE CIRCLED SANS-SERIF DIGIT (dingbats)
#
norm_button_A = 0x1F150 # NEGATIVE CIRCLED CAPITAL
norm_button_O = 0x1F15E # NEGATIVE CIRCLED CAPITAL
norm_button_Z = 0x1F169 # NEGATIVE CIRCLED CAPITAL
norm_button_1 = 0x2774 # NEGATIVE CIRCLED DIGIT (dingbats)
norm_button_9 = 0x277E # NEGATIVE CIRCLED DIGIT (dingbats)
norm_button_10 = 0x277F # NEGATIVE CIRCLED DIGIT (dingbats)
norm_button_11 = 0x24EB # NEGATIVE CIRCLED DIGIT
norm_button_20 = 0x24F4 # NEGATIVE CIRCLED DIGIT
norm_button_0 = 0x24FF # NEGATIVE CIRCLED DIGIT
norm_button_sans_1 = 0x278A # NEGATIVE CIRCLED SANS-SERIF DIGIT (dingbats)
norm_button_sans_9 = 0x2792 # NEGATIVE CIRCLED SANS-SERIF DIGIT (dingbats)
norm_button_sans_10 = 0x2793 # NEGATIVE CIRCLED SANS-SERIF DIGIT (dingbats)
norm_signum_A = 0x1F170 # NEGATIVE SQUARED CAPITAL
norm_signum_O = 0x1F17E # NEGATIVE SQUARED CAPITAL
norm_signum_Z = 0x1F189 # NEGATIVE SQUARED CAPITAL
#
norm_dbutton_1 = 0x24F5 # DOUBLE CIRCLED DIGIT (dingbats)
norm_dbutton_9 = 0x24FE # DOUBLE CIRCLED DIGIT (dingbats)
norm_squared_A = 0x1F130 # SQUARED CAPITAL
norm_squared_O = 0x1F13E # SQUARED CAPITAL
norm_squared_Z = 0x1F149 # SQUARED CAPITAL
norm_dice_1 = 0x2680
norm_dice_6 = 0x2685
norm_regional_A = 0x1F1E6 # REGIONAL INDICATOR
norm_regional_Z = 0x1F1FF # REGIONAL INDICATOR
#
norm_greek_A = 0x391
norm_greek_O = 0x3A9
norm_greek_a = 0x3B1
norm_greek_o = 0x3C9
norm_greek_nabla = 0x2207
norm_greek_diffs = 0x2202
bold_greek_A = 0x1D6A8
bold_greek_O = 0x1D6C0
bold_greek_nabla = 0x1D6C1
bold_greek_a = 0x1D6C2
bold_greek_o = 0x1D6DA
bold_greek_diffs = 0x1D6DB
ital_greek_A = 0x1D6E2
ital_greek_O = 0x1D6FA
ital_greek_nabla = 0x1D6FB
ital_greek_a = 0x1D6FC
ital_greek_o = 0x1D714
ital_greek_diffs = 0x1D715
bold_ital_greek_A = 0x1D71C
bold_ital_greek_O = 0x1D734
bold_ital_greek_nabla = 0x1D735
bold_ital_greek_a = 0x1D736
bold_ital_greek_o = 0x1D74E
bold_ital_greek_diffs = 0x1D74F
#
norm_frac_1_4 = 0x00BC
norm_frac_1_2 = 0x00BD
norm_frac_3_4 = 0x00BE
norm_frac_1_7 = 0x2150
norm_frac_1_9 = 0x2151
norm_frac_1_10 = 0x2152
norm_frac_1_3 = 0x2153
norm_frac_2_3 = 0x2154
norm_frac_1_5 = 0x2155
norm_frac_2_5 = 0x2156
norm_frac_3_5 = 0x2157
norm_frac_4_5 = 0x2158
norm_frac_1_6 = 0x2159
norm_frac_5_6 = 0x215A
norm_frac_1_8 = 0x215B
norm_frac_3_8 = 0x215C
norm_frac_5_8 = 0x215D
norm_frac_7_8 = 0x215E
norm_frac_1_x = 0x215F
norm_frac_0_3 = 0x2189
norm_base_space = 0x20
norm_nobr_space = 0x00A0
norm_thin_space = 0x202F
norm_numm_space = 0x2007
#
norm_super_0 = 0x2070
norm_super_1 = 0x00B9
norm_super_2 = 0x00B2
norm_super_3 = 0x00B3
norm_super_4 = 0x2074
norm_super_5 = 0x2075
norm_super_6 = 0x2076
norm_super_7 = 0x2077
norm_super_8 = 0x2078
norm_super_9 = 0x2079
norm_super_plus = 0x207A
norm_super_minus = 0x207B
norm_super_leftparen = 0x207D
norm_super_rightparen = 0x0207E
norm_super_equals = 0x207C
norm_super_n = 0x207F
norm_sub_0 = 0x2080
norm_sub_1 = 0x2081
norm_sub_2 = 0x2082
norm_sub_3 = 0x2083
norm_sub_4 = 0x2084
norm_sub_5 = 0x2085
norm_sub_6 = 0x2086
norm_sub_7 = 0x2087
norm_sub_8 = 0x2088
norm_sub_9 = 0x2089
norm_sub_plus = 0x208A
norm_sub_minus = 0x208B
norm_sub_equals = 0x208C
norm_sub_leftparen = 0x208D
norm_sub_rightparen = 0x0208E
norm_sub_a = 0x2090
norm_sub_e = 0x2091
norm_sub_o = 0x2092
norm_sub_x = 0x2093
norm_sub_i = 0x1D62
norm_sub_r = 0x1D63
norm_sub_u = 0x1D64
norm_sub_v = 0x1D65
#
norm_turned_a = 0x0250
norm_turned_b = ord('q')
norm_turned_c = 0x0254
norm_turned_d = ord('p')
norm_turned_e = 0x01DD
norm_turned_f = 0x025F
norm_turned_g = 0x1D77 # 0x0183
norm_turned_h = 0x0265
norm_turned_i = 0x1D09 # 0x0131
norm_turned_j = 0x027E
norm_turned_k = 0x029E
norm_turned_l = ord('l')
norm_turned_m = 0x026F
norm_turned_n = ord('u')
norm_turned_o = ord('o')
norm_turned_p = ord('d')
norm_turned_q = ord('b')
norm_turned_r = 0x0279
norm_turned_s = ord('s')
norm_turned_t = 0x0287
norm_turned_u = ord('n')
norm_turned_v = 0x028C
norm_turned_w = 0x028D
norm_turned_x = ord('x')
norm_turned_y = 0x028E
norm_turned_z = ord('z')
norm_turned_amp = 0x214B
norm_turned_dot = 0x02D9
norm_turned_comma = 0x02BB
norm_turned_bang = 0x00A1
norm_turned_question = 0x00BF
norm_turned_A = 0x2200 # 0x2C6F
norm_turned_C = 0x0186
norm_turned_E = 0x018E
norm_turned_F = 0x2132
norm_turned_G = 0x2141 # 0x05E4
norm_turned_J = 0x017F
norm_turned_M = 0x019C
norm_turned_L = 0x2142
norm_turned_R = 0x1D1A
norm_turned_T = 0xA7B1 # 0x2534
norm_turned_U = 0x2229
norm_turned_V = 0x039B
norm_turned_W = ord('M')
norm_turned_Y = 0x2144
norm_turned_1 = 0x21C2
norm_turned_3 = 0x0190
norm_turned_6 = ord('9')
norm_turned_9 = ord('6')
norm_turned_ue = 0x1E49 # n with under line
norm_turned_UE = 0x1E4B # n with under hacek
norm_turned_AE = 0x1E7E # V with ring under
norm_turned_OE = 0x1ECC # O with dot under
norm_turned_ae = 0x1D02 # ae turned
norm_turned_oe = 0x1ECD # o with dot under # 0x1D14 # oe turned
norm_turned_sz = 0x00FE # thorn # 0x025B # open e # 0x0D93 # open e with retroflex
def nobrspace(text: str) -> str:
"""replace base space by thin nobreak space """
out = StringIO()
last_ch = 0
for c in text:
ch = ord(c)
if norm_base_space == ch:
if norm_base_0 <= last_ch and last_ch <= norm_base_9:
out.write(chr(norm_numm_space))
else:
out.write(chr(norm_nobr_space))
else:
out.write(c)
last_ch = ch
return out.getvalue()
def thinspace(text: str) -> str:
"""replace base space by thin nobreak space """
out = StringIO()
for c in text:
ch = ord(c)
if norm_base_space == ch or norm_numm_space == ch:
out.write(chr(norm_thin_space))
else:
out.write(c)
return out.getvalue()
def ansispace(text: str) -> str:
out = StringIO()
for c in text:
ch = ord(c)
if ch in (norm_thin_space, norm_numm_space, norm_nobr_space):
out.write(" ")
else:
out.write(c)
return out.getvalue()
def fractions(text: str) -> str:
"""replace base space by thin nobreak space """
def splitfrac(text: str) -> Generator[str, str, None]:
frac = "0123456789/"
if text[0] in frac:
isfrac = True
else:
isfrac = False
value = StringIO()
for c in text:
if c in frac:
if isfrac:
value.write(c)
else:
yield value.getvalue()
value = StringIO()
value.write(c)
isfrac = True
else:
if isfrac:
yield value.getvalue()
value = StringIO()
value.write(c)
isfrac = False
else:
value.write(c)
if value:
yield value.getvalue()
out = StringIO()
space = ""
for item in splitfrac(text):
if item == "1/8":
space = ""
out.write(chr(norm_frac_1_8))
elif item == "2/8" or item == "1/4":
space = ""
out.write(chr(norm_frac_1_4))
elif item == "3/8":
space = ""
out.write(chr(norm_frac_3_8))
elif item == "4/8" or item == "2/4" or item == "1/2":
space = ""
out.write(chr(norm_frac_1_2))
elif item == "5/8":
space = ""
out.write(chr(norm_frac_5_8))
elif item == "6/8" or item == "3/4":
space = ""
out.write(chr(norm_frac_3_4))
elif item == "7/8":
space = ""
out.write(chr(norm_frac_7_8))
elif item == "1/5":
space = ""
out.write(chr(norm_frac_1_5))
elif item == "2/5":
space = ""
out.write(chr(norm_frac_2_5))
elif item == "3/5":
space = ""
out.write(chr(norm_frac_3_5))
elif item == "4/5":
space = ""
out.write(chr(norm_frac_4_5))
elif item == "0/6" or item == "0/3":
space = ""
out.write(chr(norm_frac_0_3))
elif item == "1/6":
space = ""
out.write(chr(norm_frac_1_6))
elif item == "2/6" or item == "1/3":
space = ""
out.write(chr(norm_frac_1_3))
elif item == "3/6":
space = ""
out.write(chr(norm_frac_1_2))
elif item == "4/6" or item == "2/3":
space = ""
out.write(chr(norm_frac_2_3))
elif item == "5/6":
space = ""
out.write(chr(norm_frac_5_6))
else:
if ord(item[-1]) in [norm_base_space, norm_thin_space, norm_nobr_space, norm_numm_space]:
out.write(space + item[:-1])
space = item[-1]
else:
out.write(space + item)
space = ""
return out.getvalue()
norm_super_numbers: Dict[str, int] = {
"0": norm_super_0,
"1": norm_super_1,
"2": norm_super_2,
"3": norm_super_3,
"4": norm_super_4,
"5": norm_super_5,
"6": norm_super_6,
"7": norm_super_7,
"8": norm_super_8,
"9": norm_super_9,
"+": norm_super_plus,
"-": norm_super_minus,
}
norm_super_before: Dict[str, int] = {
"=": norm_super_equals,
"(": norm_super_leftparen,
}
norm_super_after: Dict[str, int] = {
")": norm_super_rightparen,
"n": norm_super_n,
}
def superscript(text: str) -> str:
out = StringIO()
for x, c in enumerate(text):
if c in norm_super_numbers:
out.write(chr(norm_super_numbers[c]))
elif c in norm_super_after and x > 0 and text[x - 1] in norm_super_numbers:
out.write(chr(norm_super_after[c]))
elif c in norm_super_before and x + 1 < len(text) and text[x + 1] in norm_super_numbers:
out.write(chr(norm_super_before[c]))
else:
out.write(c)
return out.getvalue()
norm_power_signs = "^"
def power(text: str) -> str:
out = StringIO()
power = False
graec: Optional[str] = None
for x, c in enumerate(text):
ch = ord(c)
if graec is not None:
if norm_base_a <= ch and ch <= norm_base_z:
graec += c
continue
if norm_base_A <= ch and ch <= norm_base_Z:
graec += c
continue
if graec:
out.write(greek(graec))
graec = ""
if c in norm_power_signs:
if x + 1 < len(text) and (text[x + 1] in norm_super_numbers or text[x + 1] in norm_super_before):
power = True
continue # drop the power sign
if x + 1 < len(text) and (norm_base_a <= ord(text[x + 1]) and ord(text[x + 1]) <= norm_base_z):
graec = "" # graec += text[x+1] # in next loop
continue
if x + 1 < len(text) and (norm_base_A <= ord(text[x + 1]) and ord(text[x + 1]) <= norm_base_Z):
graec = "" # graec += text[x+1] # in next loop
continue
if power:
if c in norm_super_numbers:
out.write(chr(norm_super_numbers[c]))
elif c in norm_super_before:
out.write(chr(norm_super_before[c]))
elif c in norm_super_after:
out.write(chr(norm_super_after[c]))
else:
power = False
out.write(c)
else:
out.write(c)
if graec:
out.write(greek(graec))
graec = ""
return out.getvalue()
norm_sub_numbers: Dict[str, int] = {
"0": norm_sub_0,
"1": norm_sub_1,
"2": norm_sub_2,
"3": norm_sub_3,
"4": norm_sub_4,
"5": norm_sub_5,
"6": norm_sub_6,
"7": norm_sub_7,
"8": norm_sub_8,
"9": norm_sub_9,
"+": norm_sub_plus,
"-": norm_sub_minus,
}
norm_sub_before: Dict[str, int] = {
"=": norm_sub_equals,
"(": norm_sub_leftparen,
}
norm_sub_after: Dict[str, int] = {
")": norm_sub_rightparen,
"a": norm_sub_a,
"e": norm_sub_e,
"i": norm_sub_i,
"o": norm_sub_o,
"u": norm_sub_u,
"v": norm_sub_v,
"x": norm_sub_x,
"r": norm_sub_r,
}
def subscript(text: str) -> str:
out = StringIO()
for x, c in enumerate(text):
if c in norm_sub_numbers:
out.write(chr(norm_sub_numbers[c]))
elif c in norm_sub_after and x > 0 and text[x - 1] in norm_sub_numbers:
out.write(chr(norm_sub_after[c]))
elif c in norm_sub_before and x + 1 < len(text) and text[x + 1] in norm_sub_numbers:
out.write(chr(norm_sub_before[c]))
else:
out.write(c)
return out.getvalue()
norm_indexed_signs = "_"
def indexed(text: str) -> str:
out = StringIO()
indexed = False
for x, c in enumerate(text):
if c in norm_indexed_signs and x + 1 < len(text) and (text[x + 1] in norm_sub_numbers or text[x + 1] in norm_sub_before):
indexed = True
continue # drop the indexed sign
if indexed:
if c in norm_sub_numbers:
out.write(chr(norm_sub_numbers[c]))
elif c in norm_sub_before:
out.write(chr(norm_sub_before[c]))
elif c in norm_sub_after:
out.write(chr(norm_sub_after[c]))
else:
indexed = False
out.write(c)
else:
out.write(c)
return out.getvalue()
ansi_sub_numbers: Dict[int, str] = {
norm_sub_0: "0",
norm_sub_1: "1",
norm_sub_2: "2",
norm_sub_3: "3",
norm_sub_4: "4",
norm_sub_5: "5",
norm_sub_6: "6",
norm_sub_7: "7",
norm_sub_8: "8",
norm_sub_9: "9",
norm_sub_plus: "+",
norm_sub_minus: "-",
norm_sub_equals: "=",
norm_sub_leftparen: "(",
norm_sub_rightparen: ")",
norm_sub_a: "a",
norm_sub_e: "e",
norm_sub_i: "i",
norm_sub_o: "o",
norm_sub_u: "u",
norm_sub_v: "v",
norm_sub_x: "x",
norm_sub_r: "r",
}
ansi_super_numbers: Dict[int, str] = {
norm_super_0: "0",
norm_super_1: "1",
norm_super_2: "2",
norm_super_3: "3",
norm_super_4: "4",
norm_super_5: "5",
norm_super_6: "6",
norm_super_7: "7",
norm_super_8: "8",
norm_super_9: "9",
norm_super_plus: "+",
norm_super_minus: "-",
}
def ansinumbers(text: str) -> str:
out = StringIO()
for c in text:
ch = ord(c)
if ch in ansi_sub_numbers:
out.write(ansi_sub_numbers[ch])
elif ch in ansi_super_numbers:
out.write(ansi_sub_numbers[ch])
else:
out.write(c)
return out.getvalue()
norm_greek_upper: Dict[str, Tuple[int, ...]] = {
"A": (0x391,), # Alpha
"B": (0x392,), # Beta
"G": (0x393,), # Gamma
"D": (0x394,), # Delta
"E": (0x395,), # Epsilon
"Z": (0x396,), # Zeta
"H": (0x397,), # Eta
"TH": (0x398,), # Theta
"I": (0x399,), # Iota
"K": (0x39A,), # Kappa
"L": (0x39B,), # Lambda
"M": (0x39C,), # My
"N": (0x39D,), # Ny
"X": (0x39E,), # Xi
"O": (0x39F,), # Omikron
"P": (0x3A0,), # Pi
"R": (0x3A1,), # Rho
# Schluss-Sigma
"S": (0x3A3,), # Sigma
"T": (0x3A4,), # Tau
"Y": (0x3A5,), # Ypsilon
"F": (0x3A6,), # Phi
"PH": (0x3A6,), # Phi
"C": (0x3A7,), # Chi
"CH": (0x3A7,), # Chi
"CK": (0x39A,), # Kappa
"W": (0x3A8,), # Psi
"U": (0x3A9,), # Omega
"OO": (0x3A9,), # Omega
#
"J": (0x399,), # Iota
"Q": (0x39A,), # Kappa
"QU": (0x39A,), # Kappa
"V": (norm_greek_nabla,), # Nabla Operator
}
norm_greek_lower: Dict[str, Tuple[int, ...]] = {
"a": (0x3B1,), # Alpha
"b": (0x3B2,), # Beta
"g": (0x3B3,), # Gamma
"d": (0x3B4,), # Delta
"e": (0x3B5,), # Epsilon
"z": (0x3B6,), # Zeta
"h": (0x3B7,), # Eta
"th": (0x3B8,), # Theta
"i": (0x3B9,), # Iota
"k": (0x3BA,), # Kappa
"l": (0x3BB,), # Lambda
"m": (0x3BC,), # My
"n": (0x3BD,), # Ny
"x": (0x3BE,), # Xi
"o": (0x3BF,), # Omikron
"p": (0x3C0,), # Pi
"r": (0x3C1,), # Rho
"s": (0x3C3,), # Sigma
"t": (0x3C4,), # Tau
"y": (0x3C5,), # Ypsilon
"f": (0x3C6,), # Phi
"ph": (0x3C6,), # Phi
"c": (0x3C7,), # Chi
"ch": (0x3C7,), # Chi
"ck": (0x3BA,), # Kappa
"w": (0x3C8,), # Psi
"u": (0x3C9,), # Omega
"oo": (0x3C9,), # Omega
#
"j": (0x3B9,), # Iota
"q": (0x3BA,), # Kappa
"qu": (0x3BA,), # Kappa
"v": (norm_greek_diffs,), # Differential
}
def greek(text: str) -> str:
def as_norm(text: str) -> str:
return text
def as_ital(text: str) -> str:
return ital(text)
def as_bold(text: str) -> str:
return bold(text)
def as_bold_ital(text: str) -> str:
return bold(ital(text))
out = StringIO()
skip = False
for i, c in enumerate(text):
if skip:
skip = False
continue
# orig_c = c
ch = ord(c)
as_style = as_norm
if ital_base_A <= ch and ch <= ital_base_Z:
ch = norm_base_A + (ch - ital_base_A)
c = chr(ch)
as_style = as_ital
if ch in ital_base_lower:
ch = ital_base_lower[ch]
c = chr(ch)
as_style = as_ital
if ital_base_a <= ch and ch <= ital_base_z:
ch = norm_base_a + (ch - ital_base_a)
c = chr(ch)
as_style = as_ital
if bold_base_A <= ch and ch <= bold_base_Z:
ch = norm_base_A + (ch - bold_base_A)
c = chr(ch)
as_style = as_bold
if bold_base_a <= ch and ch <= bold_base_z:
ch = norm_base_a + (ch - bold_base_a)
c = chr(ch)
as_style = as_bold
if bold_ital_base_A <= ch and ch <= bold_ital_base_Z:
ch = norm_base_A + (ch - bold_ital_base_A)
c = chr(ch)
as_style = as_bold_ital
if bold_ital_base_a <= ch and ch <= bold_ital_base_z:
ch = norm_base_a + (ch - bold_ital_base_a)
c = chr(ch)
as_style = as_bold_ital
if i + 1 < len(text):
d = text[i + 1]
else:
d = " "
# orig_d = d
dh = ord(d)
if ital_base_A <= dh and dh <= ital_base_Z:
d = chr(norm_base_A + (dh - ital_base_A))
if dh in ital_base_lower:
d = chr(ital_base_lower[dh])
if ital_base_a <= dh and dh <= ital_base_z:
d = chr(norm_base_a + (dh - ital_base_a))
if bold_base_A <= dh and dh <= bold_base_Z:
d = chr(norm_base_A + (dh - bold_base_A))
if bold_base_a <= dh and dh <= bold_base_z:
d = chr(norm_base_a + (dh - bold_base_a))
if bold_ital_base_A <= dh and dh <= bold_ital_base_Z:
d = chr(norm_base_A + (dh - bold_ital_base_A))
if bold_ital_base_a <= dh and dh <= bold_ital_base_z:
d = chr(norm_base_a + (dh - bold_ital_base_a))
#
# logg.info("'%s' => '%s' (%x) & '%s' => '%s'", orig_c, c, ch, orig_d, d)
#
if norm_base_A <= ch and ch <= norm_base_Z:
if c + d in norm_greek_upper:
for n in norm_greek_upper[c + d]:
out.write(as_style(chr(n)))
skip = True
elif c in norm_greek_upper:
for n in norm_greek_upper[c]:
out.write(as_style(chr(n)))
else:
logg.error("did not find greek for '%s'", c)
out.write(c)
elif norm_base_a <= ch and ch <= norm_base_z:
if c + d in norm_greek_lower:
for n in norm_greek_lower[c + d]:
out.write(as_style(chr(n)))
skip = True
elif c in norm_greek_lower:
for n in norm_greek_lower[c]:
out.write(as_style(chr(n)))
else:
logg.error("did not find greek for '%s'", c)
out.write(c)
else:
out.write(c)
return out.getvalue()
ansi_greek_upper: Dict[int, str] = {
0x391: "A", # Alpha
0x392: "B", # Beta
0x393: "G", # Gamma
0x394: "D", # Delta
0x395: "E", # Epsilon
0x396: "Z", # Zeta
0x397: "H", # Eta
0x398: "TH", # Theta
0x399: "I", # Iota
0x39A: "K", # Kappa
0x39B: "L", # Lambda
0x39C: "M", # My
0x39D: "N", # Ny
0x39E: "X", # Xi
0x39F: "O", # Omikron
0x3A0: "P", # Pi
0x3A1: "R", # Rho
# Schluss-Sigma
0x3A3: "S", # Sigma
0x3A4: "T", # Tau
0x3A5: "Y", # Ypsilon
0x3A6: "F", # Phi
0x3A7: "CH", # Chi
0x3A8: "W", # Psi
0x3A9: "U", # Omega
norm_greek_nabla: "V", # Nabla Operator
}
ansi_greek_lower: Dict[int, str] = {
0x3B1: "a", # Alpha
0x3B2: "b", # Beta
0x3B3: "g", # Gamma
0x3B4: "d", # Delta
0x3B5: "e", # Epsilon
0x3B6: "z", # Zeta
0x3B7: "h", # Eta
0x3B8: "th", # Theta
0x3B9: "i", # Iota
0x3BA: "k", # Kappa
0x3BB: "l", # Lambda
0x3BC: "m", # My
0x3BD: "n", # Ny
0x3BE: "x", # Xi
0x3BF: "o", # Omikron
0x3C0: "p", # Pi
0x3C1: "r", # Rho
0x3C3: "s", # Sigma
0x3C4: "t", # Tau
0x3C5: "y", # Ypsilon
0x3C6: "f", # Phi
0x3C7: "ch", # Chi
0x3C8: "w", # Psi
0x3C9: "u", # Omega
norm_greek_diffs: "v", # Differential
}
def ansigreek(text: str) -> str:
out = StringIO()
for c in text:
ch = ord(c)
if ch in ansi_greek_lower:
out.write(ansi_greek_lower[ch])
elif ch in ansi_greek_upper:
out.write(ansi_greek_upper[ch])
else:
out.write(c)
return out.getvalue()
# elder futhark
norm_rune_lower: Dict[str, Tuple[int, ...]] = {
"f": (0x16A0,), # Fehu
"u": (0x16A2,), # Uruz
"th": (0x16A6,), # Thurs
"a": (0x16A8,), # ansuz
"r": (0x16B1,), # raido
"k": (0x16B3,), # kaunan
"g": (0x16B7,), # gebo
"w": (0x16D5,), # wunja # 0x1F7 0x1BF
"h": (0x16BA,), # hagalaz
"n": (0x16BE,), # naudiz
"i": (0x16C1,), # isaz
"j": (0x16E1,), # jera
"y": (0x16C7,), # ihwaz
"p": (0x16C8,), # perth
"z": (0x16C9,), # algiz
"s": (0x16CB,), # sowilo
"t": (0x16CF,), # tiwaz
"b": (0x16D2,), # berkan
"e": (0x16D6,), # ehwaz
"m": (0x16D7,), # mannaz
"l": (0x16DA,), # laguz
"ng": (0x16DC,), # ingwaz
"o": (0x16DF,), # othila
"d": (0x16DE,), # dagaz
#
"c": (0x16B3,), # kaunan
"q": (0x16B3,), # kaunan
"qu": (0x16B3,), # kaunan
"u": (0x16B9,), # wunja
"v": (0x16B9,), # wunja
"x": (0x16B3, 0x16CB), # kaunan, sowilo
}
def rune(text: str) -> str: # gothic, blackletter
out = StringIO()
skip = False
for i, c in enumerate(text):
if skip:
skip = False
continue
ch = ord(c)
if norm_base_A <= ch and ch <= norm_base_Z:
ch = norm_base_a + (ch - norm_base_A)
c = chr(ch)
if i + 1 < len(text):
d = text[i + 1]
else:
d = " "
dh = ord(d)
if norm_base_A <= dh and dh <= norm_base_Z:
dh = norm_base_a + (dh - norm_base_A)
d = chr(dh)
if norm_base_a <= ch and ch <= norm_base_z:
if c + d in norm_rune_lower:
for n in norm_rune_lower[c + d]:
out.write(chr(n))
skip = True
elif c in norm_rune_lower:
for n in norm_rune_lower[c]:
out.write(chr(n))
else:
logg.error("did not find futhark rune for '%s'", c)
out.write(c)
else:
out.write(c)
return out.getvalue()
# younger futhark - 3 rows - max 16 letters
norm_viking_lower: Dict[str, Tuple[int, ...]] = {
"f": (0x16A0,), # Fe
"u": (0x16A2,), # Ur
"th": (0x16A6,), # Thurs
"a": (0x16A8,), # As/Oss
"r": (0x16B1,), # reidh
"k": (0x16B3,), # kaun
"h": (0x16BA,), # hagall
"n": (0x16BE,), # nauthr
"i": (0x16C1,), # isa/iss
"y": (0x16C7,), # yr
"r": (0x16C9,), # awr
"s": (0x16CB,), # sol
"t": (0x16CF,), # tyr
"b": (0x16D2,), # bjork
"m": (0x16D7,), # mathr
"l": (0x16DA,), # logr
# transliterate:
"x": (0x16B3, 0x16CB), # kaun, sol
"o": (0x16A8,), # As/Oss
"e": (0x16C1,), # isa/iss
"d": (0x16CF,), # tyr
"ng": (0x16BE,), # nauthr
"p": (0x16D2,), # bjork
"z": (0x16C9,), # awr
"j": (0x16C7,), # yr
"ae": (0x16C7,), # yr
"oe": (0x16A2,), # Ur
"ue": (0x16A2,), # Ur
"w": (0x16A2,), # Ur
"v": (0x16A2,), # Ur
"g": (0x16B3,), # kaun
"c": (0x16B3,), # kaunan
"q": (0x16B3,), # kaunan
"qu": (0x16B3,), # kaunan
}
def viking(text: str) -> str: # gothic, blackletter
out = StringIO()
skip = False
for i, c in enumerate(text):
if skip:
skip = False
continue
ch = ord(c)
if norm_base_A <= ch and ch <= norm_base_Z:
ch = norm_base_a + (ch - norm_base_A)
c = chr(ch)
if i + 1 < len(text):
d = text[i + 1]
else:
d = " "
dh = ord(d)
if norm_base_A <= dh and dh <= norm_base_Z:
dh = norm_base_a + (dh - norm_base_A)
d = chr(dh)
if norm_base_a <= ch and ch <= norm_base_z:
if c + d in norm_viking_lower:
for n in norm_viking_lower[c + d]: