annotate lisp/international/utf-8.el @ 37032:620f6951fd8a

*** empty log message ***
author Gerd Moellmann <gerd@gnu.org>
date Wed, 28 Mar 2001 12:06:39 +0000
parents 898d0f4abcad
children b095952a8678
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
1 ;;; utf-8.el --- Limited UTF-8 decoding/encoding support
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
2
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
3 ;; Copyright (C) 2001 Electrotechnical Laboratory, JAPAN.
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
4 ;; Licensed to the Free Software Foundation.
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
5
36243
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
6 ;; Keywords: multilingual, Unicode, UTF-8, i18n
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
7
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
8 ;; This file is part of GNU Emacs.
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
9
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
11 ;; it under the terms of the GNU General Public License as published by
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
12 ;; the Free Software Foundation; either version 2, or (at your option)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
13 ;; any later version.
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
14
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
15 ;; GNU Emacs is distributed in the hope that it will be useful,
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
18 ;; GNU General Public License for more details.
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
19
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
23 ;; Boston, MA 02111-1307, USA.
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
24
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
25 ;;; Commentary:
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
26
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
27 ;; The coding-system `mule-utf-8' supports encoding/decoding of the
36243
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
28 ;; following character sets to and from UTF-8:
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
29 ;;
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
30 ;; ascii
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
31 ;; eight-bit-control
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
32 ;; latin-iso8859-1
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
33 ;; mule-unicode-0100-24ff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
34 ;; mule-unicode-2500-33ff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
35 ;; mule-unicode-e000-ffff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
36 ;;
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
37 ;; Characters of other character sets cannot be encoded with
36243
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
38 ;; mule-utf-8. Note that the mule-unicode charsets currently lack
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
39 ;; case and syntax information, so things like `downcase' will only
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
40 ;; work for characters from ASCII and Latin-1.
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
41 ;;
36243
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
42 ;; On decoding, Unicode characters that do not fit into the above
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
43 ;; character sets are handled as `eight-bit-control' or
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
44 ;; `eight-bit-graphic' characters to retain the information about the
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
45 ;; original byte sequence.
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
46
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
47 ;; UTF-8 is defined in RFC 2279. A sketch of the encoding is:
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
48
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
49 ;; scalar | utf-8
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
50 ;; value | 1st byte | 2nd byte | 3rd byte
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
51 ;; --------------------+-----------+-----------+----------
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
52 ;; 0000 0000 0xxx xxxx | 0xxx xxxx | |
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
53 ;; 0000 0yyy yyxx xxxx | 110y yyyy | 10xx xxxx |
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
54 ;; zzzz yyyy yyxx xxxx | 1110 zzzz | 10yy yyyy | 10xx xxxx
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
55
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
56 ;;; Code:
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
57
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
58 (define-ccl-program ccl-decode-mule-utf-8
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
59 ;;
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
60 ;; charset | bytes in utf-8 | bytes in emacs
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
61 ;; -----------------------+----------------+---------------
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
62 ;; ascii | 1 | 1
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
63 ;; -----------------------+----------------+---------------
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
64 ;; eight-bit-control | 2 | 2
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
65 ;; latin-iso8859-1 | 2 | 2
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
66 ;; -----------------------+----------------+---------------
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
67 ;; mule-unicode-0100-24ff | 2 | 4
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
68 ;; (< 0800) | |
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
69 ;; -----------------------+----------------+---------------
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
70 ;; mule-unicode-0100-24ff | 3 | 4
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
71 ;; (>= 8000) | |
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
72 ;; mule-unicode-2500-33ff | 3 | 4
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
73 ;; mule-unicode-e000-ffff | 3 | 4
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
74 ;;
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
75 ;; Thus magnification factor is two.
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
76 ;;
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
77 `(2
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
78 ((loop
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
79 (read r0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
80
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
81 ;; 1byte encoding, i.e., ascii
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
82 (if (r0 < #x80)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
83 (write r0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
84
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
85 ;; 2byte encoding
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
86 (if (r0 < #xe0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
87 ((read r1)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
88 (r0 &= #x1f)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
89 (r0 <<= 6)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
90 (r1 &= #x3f)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
91 (r1 += r0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
92 ;; now r1 holds scalar value
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
93
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
94 ;; eight-bit-control
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
95 (if (r1 < 160)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
96 ((r0 = ,(charset-id 'eight-bit-control))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
97 (write-multibyte-character r0 r1))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
98
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
99 ;; latin-iso8859-1
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
100 (if (r1 < 256)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
101 ((r0 = ,(charset-id 'latin-iso8859-1))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
102 (r1 -= 128)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
103 (write-multibyte-character r0 r1))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
104
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
105 ;; mule-unicode-0100-24ff (< 0800)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
106 ((r0 = ,(charset-id 'mule-unicode-0100-24ff))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
107 (r1 -= #x0100)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
108 (r2 = (((r1 / 96) + 32) << 7))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
109 (r1 %= 96)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
110 (r1 += (r2 + 32))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
111 (write-multibyte-character r0 r1)))))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
112
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
113 ;; 3byte encoding
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
114 (if (r0 < #xf0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
115 ((read r1 r2)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
116 (r3 = ((r0 & #x0f) << 12))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
117 (r3 += ((r1 & #x3f) << 6))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
118 (r3 += (r2 & #x3f))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
119 ;; now r3 holds scalar value
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
120
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
121 ;; mule-unicode-0100-24ff (>= 0800)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
122 (if (r3 < #x2500)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
123 ((r0 = ,(charset-id 'mule-unicode-0100-24ff))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
124 (r3 -= #x0100)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
125 (r3 //= 96)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
126 (r1 = (r7 + 32))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
127 (r1 += ((r3 + 32) << 7))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
128 (write-multibyte-character r0 r1))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
129
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
130 ;; mule-unicode-2500-33ff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
131 (if (r3 < #x3400)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
132 ((r0 = ,(charset-id 'mule-unicode-2500-33ff))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
133 (r3 -= #x2500)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
134 (r3 //= 96)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
135 (r1 = (r7 + 32))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
136 (r1 += ((r3 + 32) << 7))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
137 (write-multibyte-character r0 r1))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
138
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
139 ;; U+3400 .. U+DFFF
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
140 ;; keep those bytes as eight-bit-{control|graphic}
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
141 (if (r3 < #xe000)
36522
898d0f4abcad *** empty log message ***
Kenichi Handa <handa@m17n.org>
parents: 36465
diff changeset
142 (;; #xe0 <= r0 < #xf0, so r0 is eight-bit-graphic
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
143 (r3 = ,(charset-id 'eight-bit-graphic))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
144 (write-multibyte-character r3 r0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
145 (if (r1 < #xa0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
146 (r3 = ,(charset-id 'eight-bit-control)))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
147 (write-multibyte-character r3 r1)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
148 (if (r2 < #xa0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
149 (r3 = ,(charset-id 'eight-bit-control))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
150 (r3 = ,(charset-id 'eight-bit-graphic)))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
151 (write-multibyte-character r3 r2))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
152
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
153 ;; mule-unicode-e000-ffff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
154 ((r0 = ,(charset-id 'mule-unicode-e000-ffff))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
155 (r3 -= #xe000)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
156 (r3 //= 96)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
157 (r1 = (r7 + 32))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
158 (r1 += ((r3 + 32) << 7))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
159 (write-multibyte-character r0 r1))))))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
160
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
161 ;; 4byte encoding
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
162 ;; keep those bytes as eight-bit-{control|graphic}
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
163 ((read r1 r2 r3)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
164 ;; r0 > #xf0, thus eight-bit-graphic
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
165 (r4 = ,(charset-id 'eight-bit-graphic))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
166 (write-multibyte-character r4 r0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
167 (if (r1 < #xa0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
168 (r4 = ,(charset-id 'eight-bit-control)))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
169 (write-multibyte-character r4 r1)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
170 (if (r2 < #xa0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
171 (r4 = ,(charset-id 'eight-bit-control))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
172 (r4 = ,(charset-id 'eight-bit-graphic)))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
173 (write-multibyte-character r4 r2)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
174 (if (r3 < #xa0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
175 (r4 = ,(charset-id 'eight-bit-control))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
176 (r4 = ,(charset-id 'eight-bit-graphic)))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
177 (write-multibyte-character r4 r3)))))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
178
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
179 (repeat))))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
180
36243
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
181 "CCL program to decode UTF-8.
36465
f968e313e8ad Doc fixes.
Dave Love <fx@gnu.org>
parents: 36423
diff changeset
182 Basic decoding is done into the charsets ascii, latin-iso8859-1 and
f968e313e8ad Doc fixes.
Dave Love <fx@gnu.org>
parents: 36423
diff changeset
183 mule-unicode-*. Encodings of un-representable Unicode characters are
f968e313e8ad Doc fixes.
Dave Love <fx@gnu.org>
parents: 36423
diff changeset
184 decoded asis into eight-bit-control and eight-bit-graphic
f968e313e8ad Doc fixes.
Dave Love <fx@gnu.org>
parents: 36423
diff changeset
185 characters.")
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
186
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
187 (define-ccl-program ccl-encode-mule-utf-8
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
188 `(1
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
189 (loop
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
190 (read-multibyte-character r0 r1)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
191
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
192 (if (r0 == ,(charset-id 'ascii))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
193 (write r1)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
194
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
195 (if (r0 == ,(charset-id 'latin-iso8859-1))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
196 ;; r1 scalar utf-8
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
197 ;; 0000 0yyy yyxx xxxx 110y yyyy 10xx xxxx
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
198 ;; 20 0000 0000 1010 0000 1100 0010 1010 0000
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
199 ;; 7f 0000 0000 1111 1111 1100 0011 1011 1111
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
200 ((r0 = (((r1 & #x40) >> 6) | #xc2))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
201 (r1 &= #x3f)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
202 (r1 |= #x80)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
203 (write r0 r1))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
204
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
205 (if (r0 == ,(charset-id 'mule-unicode-0100-24ff))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
206 ((r0 = ((((r1 & #x3f80) >> 7) - 32) * 96))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
207 ;; #x3f80 == (0011 1111 1000 0000)b
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
208 (r1 &= #x7f)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
209 (r1 += (r0 + 224)) ; 240 == -32 + #x0100
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
210 ;; now r1 holds scalar value
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
211 (if (r1 < #x0800)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
212 ;; 2byte encoding
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
213 ((r0 = (((r1 & #x07c0) >> 6) | #xc0))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
214 ;; #x07c0 == (0000 0111 1100 0000)b
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
215 (r1 &= #x3f)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
216 (r1 |= #x80)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
217 (write r0 r1))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
218 ;; 3byte encoding
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
219 ((r0 = (((r1 & #xf000) >> 12) | #xe0))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
220 (r2 = ((r1 & #x3f) | #x80))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
221 (r1 &= #x0fc0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
222 (r1 >>= 6)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
223 (r1 |= #x80)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
224 (write r0 r1 r2))))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
225
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
226 (if (r0 == ,(charset-id 'mule-unicode-2500-33ff))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
227 ((r0 = ((((r1 & #x3f80) >> 7) - 32) * 96))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
228 (r1 &= #x7f)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
229 (r1 += (r0 + 9440)) ; 9440 == -32 + #x2500
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
230 (r0 = (((r1 & #xf000) >> 12) | #xe0))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
231 (r2 = ((r1 & #x3f) | #x80))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
232 (r1 &= #x0fc0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
233 (r1 >>= 6)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
234 (r1 |= #x80)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
235 (write r0 r1 r2))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
236
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
237 (if (r0 == ,(charset-id 'mule-unicode-e000-ffff))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
238 ((r0 = ((((r1 & #x3f80) >> 7) - 32) * 96))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
239 (r1 &= #x7f)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
240 (r1 += (r0 + 57312)) ; 57312 == -160 + #xe000
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
241 (r0 = (((r1 & #xf000) >> 12) | #xe0))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
242 (r2 = ((r1 & #x3f) | #x80))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
243 (r1 &= #x0fc0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
244 (r1 >>= 6)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
245 (r1 |= #x80)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
246 (write r0 r1 r2))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
247
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
248 (if (r0 == ,(charset-id 'eight-bit-control))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
249 ;; r1 scalar utf-8
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
250 ;; 0000 0yyy yyxx xxxx 110y yyyy 10xx xxxx
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
251 ;; 80 0000 0000 1000 0000 1100 0010 1000 0000
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
252 ;; 9f 0000 0000 1001 1111 1100 0010 1001 1111
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
253 (write r1)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
254
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
255 (if (r0 == ,(charset-id 'eight-bit-graphic))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
256 ;; r1 scalar utf-8
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
257 ;; 0000 0yyy yyxx xxxx 110y yyyy 10xx xxxx
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
258 ;; a0 0000 0000 1010 0000 1100 0010 1010 0000
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
259 ;; ff 0000 0000 1111 1111 1101 1111 1011 1111
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
260 (write r1)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
261
36243
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
262 ;; Unsupported character.
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
263 ;; Output U+FFFD, which is `ef bf bd' in UTF-8.
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
264 ((write #xef)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
265 (write #xbf)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
266 (write #xbd)))))))))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
267 (repeat)))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
268
36243
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
269 "CCL program to encode into UTF-8.
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
270 Only characters from the charsets ascii, eight-bit-control,
36465
f968e313e8ad Doc fixes.
Dave Love <fx@gnu.org>
parents: 36423
diff changeset
271 eight-bit-graphic, latin-iso8859-1 and mule-unicode-* are recognized.
f968e313e8ad Doc fixes.
Dave Love <fx@gnu.org>
parents: 36423
diff changeset
272 Others are encoded as U+FFFD.")
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
273
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
274 (make-coding-system
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
275 'mule-utf-8 4 ?u
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
276 "UTF-8 encoding for Emacs-supported Unicode characters.
36243
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
277 The supported Emacs character sets are:
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
278 ascii
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
279 eight-bit-control
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
280 eight-bit-graphic
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
281 latin-iso8859-1
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
282 mule-unicode-0100-24ff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
283 mule-unicode-2500-33ff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
284 mule-unicode-e000-ffff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
285
36243
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
286 Unicode characters out of the ranges U+0000-U+33FF and U+E200-U+FFFF
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
287 are decoded into sequences of eight-bit-control and eight-bit-graphic
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
288 characters to preserve their byte sequences. Emacs characters out of
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
289 these ranges are encoded into U+FFFD.
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
290
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
291 Note that, currently, characters in the mule-unicode charsets have no
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
292 syntax and case information. Thus, for instance, upper- and
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
293 lower-casing commands won't work with them."
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
294
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
295 '(ccl-decode-mule-utf-8 . ccl-encode-mule-utf-8)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
296 '((safe-charsets
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
297 ascii
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
298 eight-bit-control
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
299 eight-bit-graphic
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
300 latin-iso8859-1
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
301 mule-unicode-0100-24ff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
302 mule-unicode-2500-33ff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
303 mule-unicode-e000-ffff)
36371
f6bb3ed752b4 (mule-utf-8): Set correct value for valid-codes property.
Kenichi Handa <handa@m17n.org>
parents: 36243
diff changeset
304 (mime-charset . utf-8)
36423
aa776838b660 (mule-utf-8): Set coding-category property to coding-category-utf-8.
Kenichi Handa <handa@m17n.org>
parents: 36371
diff changeset
305 (coding-category . coding-category-utf-8)
36371
f6bb3ed752b4 (mule-utf-8): Set correct value for valid-codes property.
Kenichi Handa <handa@m17n.org>
parents: 36243
diff changeset
306 (valid-codes (0 . 255))))
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
307
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
308 (define-coding-system-alias 'utf-8 'mule-utf-8)