| 1 |
;; unicode.scm -- Unicode character width and ANSI escape support |
| 2 |
;; Copyright (c) 2006-2017 Alex Shinn. All rights reserved. |
| 3 |
;; BSD-style license: http://synthcode.com/license.txt |
| 4 |
|
| 5 |
;; a condensed non-spacing mark range from UnicodeData.txt (chars with |
| 6 |
;; the Mn property) - generated partially by hand, should automate |
| 7 |
;; this better |
| 8 |
|
| 9 |
(define low-non-spacing-chars |
| 10 |
(bytevector |
| 11 |
#xff #xff #xff #xff #xff #xff #xff #xff #xff #xff #xff #xff #xff #xff 0 0 |
| 12 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 13 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 14 |
#x78 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 15 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 16 |
0 0 #xfe #xff #xff #xff #xff #xff #x1f 0 0 0 0 0 0 0 |
| 17 |
0 0 #x3f 0 0 0 0 0 0 #xf8 #xff #x01 0 0 #x01 0 |
| 18 |
0 0 0 0 0 0 0 0 0 0 #xc0 #xff #xff #x3f 0 0 |
| 19 |
0 0 #x02 0 0 0 #xff #xff #xff #x07 0 0 0 0 0 0 |
| 20 |
0 0 0 0 #xc0 #xff #x01 0 0 0 0 0 0 0 0 0 |
| 21 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 22 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 23 |
#x06 0 0 0 0 0 0 #x10 #xfe #x21 #x1e 0 #x0c 0 0 0 |
| 24 |
#x02 0 0 0 0 0 0 #x10 #x1e #x20 0 0 #x0c 0 0 0 |
| 25 |
#x06 0 0 0 0 0 0 #x10 #xfe #x3f 0 0 0 0 #x03 0 |
| 26 |
#x06 0 0 0 0 0 0 #x30 #xfe #x21 0 0 #x0c 0 0 0 |
| 27 |
#x02 0 0 0 0 0 0 #x90 #x0e #x20 #x40 0 0 0 0 0 |
| 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0 0 0 #x0c 0 0 0 #x0c 0 |
| 52 |
0 0 0 0 0 0 #x80 #x3f #x40 #xfe #x0f #x20 0 0 0 0 |
| 53 |
0 #x38 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 54 |
0 0 0 0 0 #x02 0 0 0 0 0 0 0 0 0 0 |
| 55 |
0 0 0 0 #x87 #x01 #x04 #x0e 0 0 0 0 0 0 0 0 |
| 56 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 57 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 58 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 59 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 60 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 61 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 62 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 63 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 64 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 65 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 66 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 67 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 68 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 69 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 70 |
0 0 0 0 0 0 0 0 0 0 #xff #x1f #xe2 #x07 |
| 71 |
)) |
| 72 |
|
| 73 |
(define (unicode-char-width c) |
| 74 |
(let ((ci (char->integer c))) |
| 75 |
(cond |
| 76 |
;; hand-checked ranges from EastAsianWidth.txt |
| 77 |
((<= #x1100 ci #x115F) 2) ; Hangul |
| 78 |
((<= #x2E80 ci #x4DB5) 2) ; CJK |
| 79 |
((<= #x4E00 ci #xA4C6) 2) |
| 80 |
((<= #xAC00 ci #xD7A3) 2) ; Hangul |
| 81 |
((<= #xF900 ci #xFAD9) 2) ; CJK compat |
| 82 |
((<= #xFE10 ci #xFE6B) 2) |
| 83 |
((<= #xFF01 ci #xFF60) 2) |
| 84 |
((<= #xFFE0 ci #xFFE6) 2) |
| 85 |
((<= #x20000 ci #x30000) 2) |
| 86 |
;; non-spacing mark (Mn) ranges from UnicodeData.txt |
| 87 |
((<= #x0300 ci #x3029) |
| 88 |
;; inlined bit-vector-ref for portability |
| 89 |
(let* ((i (- ci #x0300)) |
| 90 |
(byte (quotient i 8)) |
| 91 |
(off (remainder i 8))) |
| 92 |
(if (zero? (bitwise-and (bytevector-u8-ref low-non-spacing-chars byte) |
| 93 |
(arithmetic-shift 1 off))) |
| 94 |
1 |
| 95 |
0))) |
| 96 |
((<= #x302A ci #x302F) 0) |
| 97 |
((<= #x3099 ci #x309A) 0) |
| 98 |
((= #xFB1E ci) 0) |
| 99 |
((<= #xFE00 ci #xFE23) 0) |
| 100 |
((<= #x1D167 ci #x1D169) 0) |
| 101 |
((<= #x1D17B ci #x1D182) 0) |
| 102 |
((<= #x1D185 ci #x1D18B) 0) |
| 103 |
((<= #x1D1AA ci #x1D1AD) 0) |
| 104 |
((<= #xE0100 ci #xE01EF) 0) |
| 105 |
(else 1)))) |
| 106 |
|
| 107 |
(define (unicode-terminal-width str . o) |
| 108 |
(let ((start (if (pair? o) (car o) 0)) |
| 109 |
(end (if (and (pair? o) (pair? (cdr o))) |
| 110 |
(cadr o) |
| 111 |
(string-length str)))) |
| 112 |
(let lp1 ((i start) (width 0)) |
| 113 |
(if (>= i end) |
| 114 |
width |
| 115 |
(let ((c (string-ref str i))) |
| 116 |
(cond |
| 117 |
;; ANSI escapes |
| 118 |
((and (= 27 (char->integer c)) ; esc |
| 119 |
(< (+ i 1) end) |
| 120 |
(eqv? #\[ (string-ref str (+ i 1)))) |
| 121 |
(let lp2 ((i (+ i 2))) |
| 122 |
(cond ((>= i end) width) |
| 123 |
((memv (string-ref str i) '(#\m #\newline)) |
| 124 |
(lp1 (+ i 1) width)) |
| 125 |
(else (lp2 (+ i 1)))))) |
| 126 |
;; unicode characters |
| 127 |
((>= (char->integer c) #x80) |
| 128 |
(lp1 (+ i 1) (+ width (unicode-char-width c)))) |
| 129 |
;; normal ASCII |
| 130 |
(else (lp1 (+ i 1) (+ width 1))))))))) |
| 131 |
|
| 132 |
(define (as-unicode . args) |
| 133 |
(with ((string-width unicode-terminal-width)) |
| 134 |
(each-in-list args))) |