| 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 |
(defvar iso2022-gb-designation "\e$A") |
|---|
| 54 |
|
|---|
| 55 |
(defvar hz-gb-designnation "~{") |
|---|
| 56 |
|
|---|
| 57 |
(defvar iso2022-ascii-designation "\e(B") |
|---|
| 58 |
|
|---|
| 59 |
(defvar hz-ascii-designnation "~}") |
|---|
| 60 |
|
|---|
| 61 |
(defvar zw-start-gb "^zW") |
|---|
| 62 |
|
|---|
| 63 |
(defvar hz/zw-start-gb |
|---|
| 64 |
(concat hz-gb-designnation "\\|" zw-start-gb "\\|[^\0-\177]")) |
|---|
| 65 |
|
|---|
| 66 |
(defvar decode-hz-line-continuation nil |
|---|
| 67 |
"Flag to tell if we should care line continuation convention of Hz.") |
|---|
| 68 |
|
|---|
| 69 |
(defconst hz-set-msb-table |
|---|
| 70 |
(eval-when-compile |
|---|
| 71 |
(let ((chars nil) |
|---|
| 72 |
(i 0)) |
|---|
| 73 |
(while (< i 33) |
|---|
| 74 |
(push i chars) |
|---|
| 75 |
(setq i (1+ i))) |
|---|
| 76 |
(while (< i 127) |
|---|
| 77 |
(push (+ i 128) chars) |
|---|
| 78 |
(setq i (1+ i))) |
|---|
| 79 |
(apply 'string (nreverse chars))))) |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
(defun decode-hz-region (beg end) |
|---|
| 83 |
"Decode HZ/ZW encoded text in the current region. |
|---|
| 84 |
Return the length of resulting text." |
|---|
| 85 |
(interactive "r") |
|---|
| 86 |
(save-excursion |
|---|
| 87 |
(save-restriction |
|---|
| 88 |
(let (pos ch) |
|---|
| 89 |
(narrow-to-region beg end) |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
(goto-char (point-min)) |
|---|
| 96 |
(while (search-forward "~" nil t) |
|---|
| 97 |
(setq ch (following-char)) |
|---|
| 98 |
(if (or (= ch ?\n) (= ch ?~)) (delete-char -1))) |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
(goto-char (point-min)) |
|---|
| 103 |
(setq beg nil) |
|---|
| 104 |
(while (re-search-forward hz/zw-start-gb nil t) |
|---|
| 105 |
(setq pos (match-beginning 0) |
|---|
| 106 |
ch (char-after pos)) |
|---|
| 107 |
|
|---|
| 108 |
(or beg (setq beg pos)) |
|---|
| 109 |
(end-of-line) |
|---|
| 110 |
(setq end (point)) |
|---|
| 111 |
(if (>= ch 128) |
|---|
| 112 |
nil |
|---|
| 113 |
(goto-char pos) |
|---|
| 114 |
(delete-char 2) |
|---|
| 115 |
(setq end (- end 2)) |
|---|
| 116 |
(if (= ch ?z) |
|---|
| 117 |
(progn |
|---|
| 118 |
(translate-region (point) end hz-set-msb-table) |
|---|
| 119 |
(goto-char end)) |
|---|
| 120 |
(if (search-forward hz-ascii-designnation |
|---|
| 121 |
(if decode-hz-line-continuation nil end) |
|---|
| 122 |
t) |
|---|
| 123 |
(delete-char -2)) |
|---|
| 124 |
(setq end (point)) |
|---|
| 125 |
(translate-region pos (point) hz-set-msb-table)))) |
|---|
| 126 |
(if beg |
|---|
| 127 |
(decode-coding-region beg end 'euc-china))) |
|---|
| 128 |
(- (point-max) (point-min))))) |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
(defun decode-hz-buffer () |
|---|
| 132 |
"Decode HZ/ZW encoded text in the current buffer." |
|---|
| 133 |
(interactive) |
|---|
| 134 |
(decode-hz-region (point-min) (point-max))) |
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
(defun encode-hz-region (beg end) |
|---|
| 138 |
"Encode the text in the current region to HZ. |
|---|
| 139 |
Return the length of resulting text." |
|---|
| 140 |
(interactive "r") |
|---|
| 141 |
(save-excursion |
|---|
| 142 |
(save-restriction |
|---|
| 143 |
(narrow-to-region beg end) |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
(goto-char (point-min)) |
|---|
| 147 |
(while (search-forward "~" nil t) (insert ?~)) |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
(goto-char (point-min)) |
|---|
| 151 |
(if (re-search-forward "\\cc" nil t) |
|---|
| 152 |
(let (pos) |
|---|
| 153 |
(goto-char (setq pos (match-beginning 0))) |
|---|
| 154 |
(encode-coding-region pos (point-max) 'iso-2022-7bit) |
|---|
| 155 |
(goto-char pos) |
|---|
| 156 |
(while (search-forward iso2022-gb-designation nil t) |
|---|
| 157 |
(delete-char -3) |
|---|
| 158 |
(insert hz-gb-designnation)) |
|---|
| 159 |
(goto-char pos) |
|---|
| 160 |
(while (search-forward iso2022-ascii-designation nil t) |
|---|
| 161 |
(delete-char -3) |
|---|
| 162 |
(insert hz-ascii-designnation)))) |
|---|
| 163 |
(- (point-max) (point-min))))) |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
(defun encode-hz-buffer () |
|---|
| 167 |
"Encode the text in the current buffer to HZ." |
|---|
| 168 |
(interactive) |
|---|
| 169 |
(encode-hz-region (point-min) (point-max))) |
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
(eval-when-compile |
|---|
| 179 |
(defun big5-to-flat-code (num) |
|---|
| 180 |
"Convert NUM in Big 5 encoding to a `flat code'. |
|---|
| 181 |
0xA140 will be mapped to position 0, 0xA141 to position 1, etc. |
|---|
| 182 |
There are no gaps in the flat code." |
|---|
| 183 |
|
|---|
| 184 |
(let ((hi (/ num 256)) |
|---|
| 185 |
(lo (% num 256))) |
|---|
| 186 |
(+ (* 157 (- hi #xa1)) |
|---|
| 187 |
(- lo (if (>= lo #xa1) 98 64))))) |
|---|
| 188 |
|
|---|
| 189 |
(defun flat-code-to-big5 (num) |
|---|
| 190 |
"Convert NUM from a `flat code' to Big 5 encoding. |
|---|
| 191 |
This is the inverse function of `big5-to-flat-code'." |
|---|
| 192 |
|
|---|
| 193 |
(let ((hi (/ num 157)) |
|---|
| 194 |
(lo (% num 157))) |
|---|
| 195 |
(+ (* 256 (+ hi #xa1)) |
|---|
| 196 |
(+ lo (if (< lo 63) 64 98))))) |
|---|
| 197 |
|
|---|
| 198 |
(defun euc-to-flat-code (num) |
|---|
| 199 |
"Convert NUM in EUC encoding (in GL representation) to a `flat code'. |
|---|
| 200 |
0x2121 will be mapped to position 0, 0x2122 to position 1, etc. |
|---|
| 201 |
There are no gaps in the flat code." |
|---|
| 202 |
|
|---|
| 203 |
(let ((hi (/ num 256)) |
|---|
| 204 |
(lo (% num 256))) |
|---|
| 205 |
(+ (* 94 (- hi #x21)) |
|---|
| 206 |
(- lo #x21)))) |
|---|
| 207 |
|
|---|
| 208 |
(defun flat-code-to-euc (num) |
|---|
| 209 |
"Convert NUM from a `flat code' to EUC encoding (in GL representation). |
|---|
| 210 |
The inverse function of `euc-to-flat-code'. The high and low bytes are |
|---|
| 211 |
returned in a list." |
|---|
| 212 |
|
|---|
| 213 |
(let ((hi (/ num 94)) |
|---|
| 214 |
(lo (% num 94))) |
|---|
| 215 |
(list (+ hi #x21) (+ lo #x21)))) |
|---|
| 216 |
|
|---|
| 217 |
(defun expand-euc-big5-alist (alist) |
|---|
| 218 |
"Create a translation table and fills it with data given in ALIST. |
|---|
| 219 |
Elements of ALIST can be either given as |
|---|
| 220 |
|
|---|
| 221 |
((euc-charset . startchar) . (big5-range-begin . big5-range-end)) |
|---|
| 222 |
|
|---|
| 223 |
or as |
|---|
| 224 |
|
|---|
| 225 |
(euc-character . big5-charcode) |
|---|
| 226 |
|
|---|
| 227 |
The former maps a range of glyphs in an EUC charset (where STARTCHAR |
|---|
| 228 |
is in GL representation) to a certain range of Big 5 encoded |
|---|
| 229 |
characters, the latter maps a single glyph. Glyphs which can't be |
|---|
| 230 |
mapped will be represented with the byte 0xFF. |
|---|
| 231 |
|
|---|
| 232 |
The return value is the filled translation table." |
|---|
| 233 |
|
|---|
| 234 |
(let ((chartable (make-char-table 'translation-table #xFF)) |
|---|
| 235 |
char |
|---|
| 236 |
big5 |
|---|
| 237 |
i |
|---|
| 238 |
end |
|---|
| 239 |
codepoint |
|---|
| 240 |
charset) |
|---|
| 241 |
(dolist (elem alist) |
|---|
| 242 |
(setq char (car elem) |
|---|
| 243 |
big5 (cdr elem)) |
|---|
| 244 |
(cond ((and (consp char) |
|---|
| 245 |
(consp big5)) |
|---|
| 246 |
(setq i (big5-to-flat-code (car big5)) |
|---|
| 247 |
end (big5-to-flat-code (cdr big5)) |
|---|
| 248 |
codepoint (euc-to-flat-code (cdr char)) |
|---|
| 249 |
charset (car char)) |
|---|
| 250 |
(while (>= end i) |
|---|
| 251 |
(aset chartable |
|---|
| 252 |
(decode-big5-char (flat-code-to-big5 i)) |
|---|
| 253 |
(apply (function make-char) |
|---|
| 254 |
charset |
|---|
| 255 |
(flat-code-to-euc codepoint))) |
|---|
| 256 |
(setq i (1+ i) |
|---|
| 257 |
codepoint (1+ codepoint)))) |
|---|
| 258 |
((and (char-valid-p char) |
|---|
| 259 |
(numberp big5)) |
|---|
| 260 |
(setq i (decode-big5-char big5)) |
|---|
| 261 |
(aset chartable i char)) |
|---|
| 262 |
(t |
|---|
| 263 |
(error "Unknown slot type: %S" elem)))) |
|---|
| 264 |
|
|---|
| 265 |
chartable))) |
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
(define-translation-table 'big5-to-cns |
|---|
| 270 |
(eval-when-compile |
|---|
| 271 |
(expand-euc-big5-alist |
|---|
| 272 |
'( |
|---|
| 273 |
|
|---|
| 274 |
((chinese-cns11643-1 . #x2121) . (#xA140 . #xA1F5)) |
|---|
| 275 |
(?$(G"X(B . #xA1F6) |
|---|
| 276 |
(?$(G"W(B . #xA1F7) |
|---|
| 277 |
((chinese-cns11643-1 . #x2259) . (#xA1F8 . #xA2AE)) |
|---|
| 278 |
((chinese-cns11643-1 . #x2421) . (#xA2AF . #xA3BF)) |
|---|
| 279 |
|
|---|
| 280 |
((chinese-cns11643-1 . #x4221) . (#xA3C0 . #xA3E0)) |
|---|
| 281 |
|
|---|
| 282 |
((chinese-cns11643-1 . #x4421) . (#xA440 . #xACFD)) |
|---|
| 283 |
(?$(GWS(B . #xACFE) |
|---|
| 284 |
((chinese-cns11643-1 . #x5323) . (#xAD40 . #xAFCF)) |
|---|
| 285 |
((chinese-cns11643-1 . #x5754) . (#xAFD0 . #xBBC7)) |
|---|
| 286 |
((chinese-cns11643-1 . #x6B51) . (#xBBC8 . #xBE51)) |
|---|
| 287 |
(?$(GkP(B . #xBE52) |
|---|
| 288 |
((chinese-cns11643-1 . #x6F5C) . (#xBE53 . #xC1AA)) |
|---|
| 289 |
((chinese-cns11643-1 . #x7536) . (#xC1AB . #xC2CA)) |
|---|
| 290 |
(?$(Gu5(B . #xC2CB) |
|---|
| 291 |
((chinese-cns11643-1 . #x7737) . (#xC2CC . #xC360)) |
|---|
| 292 |
((chinese-cns11643-1 . #x782E) . (#xC361 . #xC3B8)) |
|---|
| 293 |
(?$(Gxe(B . #xC3B9) |
|---|
| 294 |
(?$(Gxd(B . #xC3BA) |
|---|
| 295 |
((chinese-cns11643-1 . #x7866) . (#xC3BB . #xC455)) |
|---|
| 296 |
(?$(Gx-(B . #xC456) |
|---|
| 297 |
((chinese-cns11643-1 . #x7962) . (#xC457 . #xC67E)) |
|---|
| 298 |
|
|---|
| 299 |
((chinese-cns11643-1 . #x2621) . (#xC6A1 . #xC6BE)) |
|---|
| 300 |
|
|---|
| 301 |
(?$(G'#(B . #xC6BF) |
|---|
| 302 |
(?$(G'$(B . #xC6C0) |
|---|
| 303 |
(?$(G'&(B . #xC6C1) |
|---|
| 304 |
(?$(G'((B . #xC6C2) |
|---|
| 305 |
(?$(G'-(B . #xC6C3) |
|---|
| 306 |
(?$(G'.(B . #xC6C4) |
|---|
| 307 |
(?$(G'/(B . #xC6C5) |
|---|
| 308 |
(?$(G'4(B . #xC6C6) |
|---|
| 309 |
(?$(G'7(B . #xC6C7) |
|---|
| 310 |
(?$(G':(B . #xC6C8) |
|---|
| 311 |
(?$(G'<(B . #xC6C9) |
|---|
| 312 |
(?$(G'B(B . #xC6CA) |
|---|
| 313 |
(?$(G'G(B . #xC6CB) |
|---|
| 314 |
(?$(G'N(B . #xC6CC) |
|---|
| 315 |
(?$(G'S(B . #xC6CD) |
|---|
| 316 |
(?$(G'T(B . #xC6CE) |
|---|
| 317 |
(?$(G'U(B . #xC6CF) |
|---|
| 318 |
(?$(G'Y(B . #xC6D0) |
|---|
| 319 |
(?$(G'Z(B . #xC6D1) |
|---|
| 320 |
(?$(G'a(B . #xC6D2) |
|---|
| 321 |
(?$(G'f(B . #xC6D3) |
|---|
| 322 |
(?$(G()(B . #xC6D4) |
|---|
| 323 |
(?$(G(*(B . #xC6D5) |
|---|
| 324 |
(?$(G(c(B . #xC6D6) |
|---|
| 325 |
(?$(G(l(B . #xC6D7) |
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
|
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
(?$(J!#(B . #xC879) |
|---|
| 339 |
(?$(J!$(B . #xC87B) |
|---|
| 340 |
(?$(J!*(B . #xC87D) |
|---|
| 341 |
(?$(J!R(B . #xC8A2) |
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
|
|---|
| 345 |
|
|---|
| 346 |
|
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 |
|
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 |
((chinese-cns11643-2 . #x2121) . (#xC940 . #xC949)) |
|---|
| 357 |
(?$(GDB(B . #xC94A) |
|---|
| 358 |
((chinese-cns11643-2 . #x212B) . (#xC94B . #xC96B)) |
|---|
| 359 |
((chinese-cns11643-2 . #x214D) . (#xC96C . #xC9BD)) |
|---|
| 360 |
(?$(H!L(B . #xC9BE) |
|---|
| 361 |
((chinese-cns11643-2 . #x217D) . (#xC9BF . #xC9EC)) |
|---|
| 362 |
((chinese-cns11643-2 . #x224E) . (#xC9ED . #xCAF6)) |
|---|
| 363 |
(?$(H"M(B . #xCAF7) |
|---|
| 364 |
((chinese-cns11643-2 . #x2439) . (#xCAF8 . #xD6CB)) |
|---|
| 365 |
(?$(H>c(B . #xD6CC) |
|---|
| 366 |
((chinese-cns11643-2 . #x3770) . (#xD6CD . #xD779)) |
|---|
| 367 |
(?$(H?j(B . #xD77A) |
|---|
| 368 |
((chinese-cns11643-2 . #x387E) . (#xD77B . #xDADE)) |
|---|
| 369 |
(?$(H7o(B . #xDADF) |
|---|
| 370 |
((chinese-cns11643-2 . #x3E64) . (#xDAE0 . #xDBA6)) |
|---|
| 371 |
((chinese-cns11643-2 . #x3F6B) . (#xDBA7 . #xDDFB)) |
|---|
| 372 |
(?$(HAv(B . #xDDFC);; a duplicate of #xDCD1 |
|---|
| 373 |
((chinese-cns11643-2 . #x4424) . (#xDDFD . #xE8A2)) |
|---|
| 374 |
((chinese-cns11643-2 . #x554C) . (#xE8A3 . #xE975)) |
|---|
| 375 |
((chinese-cns11643-2 . #x5723) . (#xE976 . #xEB5A)) |
|---|
| 376 |
((chinese-cns11643-2 . #x5A29) . (#xEB5B . #xEBF0)) |
|---|
| 377 |
(?$(HUK(B . #xEBF1) |
|---|
| 378 |
((chinese-cns11643-2 . #x5B3F) . (#xEBF2 . #xECDD)) |
|---|
| 379 |
(?$(HW"(B . #xECDE) |
|---|
| 380 |
((chinese-cns11643-2 . #x5C6A) . (#xECDF . #xEDA9)) |
|---|
| 381 |
((chinese-cns11643-2 . #x5D75) . (#xEDAA . #xEEEA)) |
|---|
| 382 |
(?$(Hd/(B . #xEEEB) |
|---|
| 383 |
((chinese-cns11643-2 . #x6039) . (#xEEEC . #xF055)) |
|---|
| 384 |
(?$(H]t(B . #xF056) |
|---|
| 385 |
((chinese-cns11643-2 . #x6243) . (#xF057 . #xF0CA)) |
|---|
| 386 |
(?$(HZ((B . #xF0CB) |
|---|
| 387 |
((chinese-cns11643-2 . #x6337) . (#xF0CC . #xF162)) |
|---|
| 388 |
((chinese-cns11643-2 . #x6430) . (#xF163 . #xF16A)) |
|---|
| 389 |
(?$(Hga(B . #xF16B) |
|---|
| 390 |
((chinese-cns11643-2 . #x6438) . (#xF16C . #xF267)) |
|---|
| 391 |
(?$(Hi4(B . #xF268) |
|---|
| 392 |
((chinese-cns11643-2 . #x6573) . (#xF269 . #xF2C2)) |
|---|
| 393 |
((chinese-cns11643-2 . #x664E) . (#xF2C3 . #xF374)) |
|---|
| 394 |
((chinese-cns11643-2 . #x6762) . (#xF375 . #xF465)) |
|---|
| 395 |
((chinese-cns11643-2 . #x6935) . (#xF466 . #xF4B4)) |
|---|
| 396 |
(?$(HfM(B . #xF4B5) |
|---|
| 397 |
((chinese-cns11643-2 . #x6962) . (#xF4B6 . #xF4FC)) |
|---|
| 398 |
((chinese-cns11643-2 . #x6A4C) . (#xF4FD . #xF662)) |
|---|
| 399 |
(?$(HjK(B . #xF663) |
|---|
| 400 |
((chinese-cns11643-2 . #x6C52) . (#xF664 . #xF976)) |
|---|
| 401 |
((chinese-cns11643-2 . #x7167) . (#xF977 . #xF9C3)) |
|---|
| 402 |
(?$(Hqf(B . #xF9C4) |
|---|
| 403 |
(?$(Hr4(B . #xF9C5) |
|---|
| 404 |
(?$(Hr@(B . #xF9C6) |
|---|
| 405 |
((chinese-cns11643-2 . #x7235) . (#xF9C7 . #xF9D1)) |
|---|
| 406 |
((chinese-cns11643-2 . #x7241) . (#xF9D2 . #xF9D5)) |
|---|
| 407 |
|
|---|
| 408 |
|
|---|
| 409 |
(?$(IC7(B . #xF9D6) |
|---|
| 410 |
(?$(IOP(B . #xF9D7) |
|---|
| 411 |
(?$(IDN(B . #xF9D8) |
|---|
| 412 |
(?$(IPJ(B . #xF9D9) |
|---|
| 413 |
(?$(I,](B . #xF9DA) |
|---|
| 414 |
(?$(I=~(B . #xF9DB) |
|---|
| 415 |
(?$(IK\(B . #xF9DC) |
|---|
| 416 |
) |
|---|
| 417 |
)) |
|---|
| 418 |
) |
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 |
(provide 'china-util) |
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 |
|
|---|
| 425 |
|
|---|