Debian Codegraph @

lib/text/console/wide-char-setting.scm

raw
1 (define-module text.console.wide-char-setting
2 (use gauche.unicode)
3 (export <wide-char-setting> get-char-width))
4 (select-module text.console.wide-char-setting)
5
6 ;; <wide-char-setting>
7 ;; Initializable slots:
8 ;; mode - specify the mode for determining widths of wide characters.
9 ;; If 'Unicode is specified, character widths are determined
10 ;; by East Asian Width of Unicode.
11 ;; If 'Surrogate is specified, character widths are determined
12 ;; by checking surrogate pairs of Unicode.
13 ;; If 'Wide is specified, character widths are determined
14 ;; by only character codes.
15 ;; Otherwise, wide character support is disabled.
16 ;; wide-char-width - a width of wide characters.
17 ;; surrogate-char-width - a width of surrogate pair characters of Unicode.
18 ;; ambiguous-char-width - a width of ambiguous width characters of Unicode.
19 ;; To the above 3 slots, specify a multiple of the width of
20 ;; half-width characters.
21 ;; emoji-char-workaround - if this is not #f, emoji characters are
22 ;; treated as wide characters.
23 ;;
24 (define-class <wide-char-setting> ()
25 ((mode :init-keyword :mode :init-value 'Unicode)
26 (wide-char-width :init-keyword :wide-char-width :init-value 2)
27 (surrogate-char-width :init-keyword :surrogate-char-width :init-value 2)
28 (ambiguous-char-width :init-keyword :ambiguous-char-width :init-value 1)
29 (emoji-char-workaround :init-keyword :emoji-char-workaround :init-value #t)
30 ))
31
32 ;; Get a character width
33 (define (get-char-width wide-char-setting ch)
34 (define wide-char-mode (~ wide-char-setting'mode))
35 (define wide-char-width (~ wide-char-setting'wide-char-width))
36 (define surrogate-char-width (~ wide-char-setting'surrogate-char-width))
37 (define ambiguous-char-width (~ wide-char-setting'ambiguous-char-width))
38 (define emoji-char-workaround (~ wide-char-setting'emoji-char-workaround))
39 (define chcode (char->integer ch))
40 (cond
41 [(<= 0 chcode #x7f)
42 1]
43 [else
44 (case wide-char-mode
45 [(Unicode)
46 (if (and emoji-char-workaround
47 ;(<= #x1f000 chcode #x1ffff))
48 (unicode-emoji? chcode))
49 wide-char-width
50 (case (char-east-asian-width ch)
51 [(A) ambiguous-char-width]
52 [(F W) wide-char-width]
53 [(H N Na) 1]
54 [else ambiguous-char-width]))]
55 [(Surrogate)
56 (if (>= chcode #x10000)
57 surrogate-char-width
58 wide-char-width)]
59 [(Wide)
60 wide-char-width]
61 [else
62 1])]))
63
64 ;; unicode emoji table
65 ;; generated from
66 ;; https://unicode.org/Public/UNIDATA/emoji/emoji-data.txt
67 ;; (excluding less than U+1000)
68 ;;
69 ;; TODO: integrate into gauche.unicode
70 ;;
71 (define *unicode-emoji-table*
72 (vector
73 '(#x0203c . #x0203c) '(#x02049 . #x02049) '(#x02122 . #x02122)
74 '(#x02139 . #x02139) '(#x02194 . #x02199) '(#x021a9 . #x021aa)
75 '(#x0231a . #x0231b) '(#x02328 . #x02328) '(#x023cf . #x023cf)
76 '(#x023e9 . #x023f3) '(#x023f8 . #x023fa) '(#x024c2 . #x024c2)
77 '(#x025aa . #x025ab) '(#x025b6 . #x025b6) '(#x025c0 . #x025c0)
78 '(#x025fb . #x025fe) '(#x02600 . #x02604) '(#x0260e . #x0260e)
79 '(#x02611 . #x02611) '(#x02614 . #x02615) '(#x02618 . #x02618)
80 '(#x0261d . #x0261d) '(#x02620 . #x02620) '(#x02622 . #x02623)
81 '(#x02626 . #x02626) '(#x0262a . #x0262a) '(#x0262e . #x0262f)
82 '(#x02638 . #x0263a) '(#x02640 . #x02640) '(#x02642 . #x02642)
83 '(#x02648 . #x02653) '(#x0265f . #x02660) '(#x02663 . #x02663)
84 '(#x02665 . #x02666) '(#x02668 . #x02668) '(#x0267b . #x0267b)
85 '(#x0267e . #x0267f) '(#x02692 . #x02697) '(#x02699 . #x02699)
86 '(#x0269b . #x0269c) '(#x026a0 . #x026a1) '(#x026a7 . #x026a7)
87 '(#x026aa . #x026ab) '(#x026b0 . #x026b1) '(#x026bd . #x026be)
88 '(#x026c4 . #x026c5) '(#x026c8 . #x026c8) '(#x026ce . #x026cf)
89 '(#x026d1 . #x026d1) '(#x026d3 . #x026d4) '(#x026e9 . #x026ea)
90 '(#x026f0 . #x026f5) '(#x026f7 . #x026fa) '(#x026fd . #x026fd)
91 '(#x02702 . #x02702) '(#x02705 . #x02705) '(#x02708 . #x0270d)
92 '(#x0270f . #x0270f) '(#x02712 . #x02712) '(#x02714 . #x02714)
93 '(#x02716 . #x02716) '(#x0271d . #x0271d) '(#x02721 . #x02721)
94 '(#x02728 . #x02728) '(#x02733 . #x02734) '(#x02744 . #x02744)
95 '(#x02747 . #x02747) '(#x0274c . #x0274c) '(#x0274e . #x0274e)
96 '(#x02753 . #x02755) '(#x02757 . #x02757) '(#x02763 . #x02764)
97 '(#x02795 . #x02797) '(#x027a1 . #x027a1) '(#x027b0 . #x027b0)
98 '(#x027bf . #x027bf) '(#x02934 . #x02935) '(#x02b05 . #x02b07)
99 '(#x02b1b . #x02b1c) '(#x02b50 . #x02b50) '(#x02b55 . #x02b55)
100 '(#x03030 . #x03030) '(#x0303d . #x0303d) '(#x03297 . #x03297)
101 '(#x03299 . #x03299) '(#x1f004 . #x1f004) '(#x1f0cf . #x1f0cf)
102 '(#x1f170 . #x1f171) '(#x1f17e . #x1f17f) '(#x1f18e . #x1f18e)
103 '(#x1f191 . #x1f19a) '(#x1f1e6 . #x1f1ff) '(#x1f201 . #x1f202)
104 '(#x1f21a . #x1f21a) '(#x1f22f . #x1f22f) '(#x1f232 . #x1f23a)
105 '(#x1f250 . #x1f251) '(#x1f300 . #x1f321) '(#x1f324 . #x1f393)
106 '(#x1f396 . #x1f397) '(#x1f399 . #x1f39b) '(#x1f39e . #x1f3f0)
107 '(#x1f3f3 . #x1f3f5) '(#x1f3f7 . #x1f4fd) '(#x1f4ff . #x1f53d)
108 '(#x1f549 . #x1f54e) '(#x1f550 . #x1f567) '(#x1f56f . #x1f570)
109 '(#x1f573 . #x1f57a) '(#x1f587 . #x1f587) '(#x1f58a . #x1f58d)
110 '(#x1f590 . #x1f590) '(#x1f595 . #x1f596) '(#x1f5a4 . #x1f5a5)
111 '(#x1f5a8 . #x1f5a8) '(#x1f5b1 . #x1f5b2) '(#x1f5bc . #x1f5bc)
112 '(#x1f5c2 . #x1f5c4) '(#x1f5d1 . #x1f5d3) '(#x1f5dc . #x1f5de)
113 '(#x1f5e1 . #x1f5e1) '(#x1f5e3 . #x1f5e3) '(#x1f5e8 . #x1f5e8)
114 '(#x1f5ef . #x1f5ef) '(#x1f5f3 . #x1f5f3) '(#x1f5fa . #x1f64f)
115 '(#x1f680 . #x1f6c5) '(#x1f6cb . #x1f6d2) '(#x1f6d5 . #x1f6d7)
116 '(#x1f6e0 . #x1f6e5) '(#x1f6e9 . #x1f6e9) '(#x1f6eb . #x1f6ec)
117 '(#x1f6f0 . #x1f6f0) '(#x1f6f3 . #x1f6fc) '(#x1f7e0 . #x1f7eb)
118 '(#x1f90c . #x1f93a) '(#x1f93c . #x1f945) '(#x1f947 . #x1f978)
119 '(#x1f97a . #x1f9cb) '(#x1f9cd . #x1f9ff) '(#x1fa70 . #x1fa74)
120 '(#x1fa78 . #x1fa7a) '(#x1fa80 . #x1fa86) '(#x1fa90 . #x1faa8)
121 '(#x1fab0 . #x1fab6) '(#x1fac0 . #x1fac2) '(#x1fad0 . #x1fad6)))
122
123 ;; check if unicode emoji character
124 (define (unicode-emoji? chcode)
125 (define min-index 0)
126 (define max-index (- (vector-length *unicode-emoji-table*) 1))
127 (if (or (< chcode (car (vector-ref *unicode-emoji-table* 0)))
128 (> chcode (cdr (vector-ref *unicode-emoji-table* max-index))))
129 #f
130 (let loop ()
131 (cond
132 [(<= min-index max-index)
133 (let1 middle-index (quotient (+ min-index max-index) 2)
134 (cond
135 [(< chcode (car (vector-ref *unicode-emoji-table* middle-index)))
136 (set! max-index (- middle-index 1))
137 (loop)]
138 [(> chcode (cdr (vector-ref *unicode-emoji-table* middle-index)))
139 (set! min-index (+ middle-index 1))
140 (loop)]
141 [else
142 #t]))]
143 [else
144 #f]))))