1 ;;; utf-8.el --- Limited UTF-8 decoding/encoding support
3 ;; Copyright (C) 2001 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
6 ;; Keywords: multilingual, Unicode, UTF-8
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; The coding-system `mule-utf-8' supports encoding/decoding of the
28 ;; following character sets:
33 ;; mule-unicode-0100-24ff
34 ;; mule-unicode-2500-33ff
35 ;; mule-unicode-e000-ffff
37 ;; Characters of other character sets cannot be encoded with
40 ;; On decoding, Unicode characters that do not fit in above character
41 ;; sets are handled as `eight-bit-control' or `eight-bit-graphic'
42 ;; characters to retain original information (i.e. original byte
46 ;; value | 1st byte | 2nd byte | 3rd byte
47 ;; --------------------+-----------+-----------+----------
48 ;; 0000 0000 0xxx xxxx | 0xxx xxxx | |
49 ;; 0000 0yyy yyxx xxxx | 110y yyyy | 10xx xxxx |
50 ;; zzzz yyyy yyxx xxxx | 1110 zzzz | 10yy yyyy | 10xx xxxx
54 (eval-when-compile (require 'ucs-tables
))
56 (define-ccl-program ccl-decode-mule-utf-8
58 ;; charset | bytes in utf-8 | bytes in emacs
59 ;; -----------------------+----------------+---------------
61 ;; -----------------------+----------------+---------------
62 ;; eight-bit-control | 2 | 2
63 ;; latin-iso8859-1 | 2 | 2
64 ;; -----------------------+----------------+---------------
65 ;; mule-unicode-0100-24ff | 2 | 4
67 ;; -----------------------+----------------+---------------
68 ;; mule-unicode-0100-24ff | 3 | 4
70 ;; mule-unicode-2500-33ff | 3 | 4
71 ;; mule-unicode-e000-ffff | 3 | 4
73 ;; Thus magnification factor is two.
79 ;; 1byte encoding, i.e., ascii
90 ;; now r1 holds scalar value
94 ((r0 = ,(charset-id 'eight-bit-control
))
95 (write-multibyte-character r0 r1
))
99 ((r0 = ,(charset-id 'latin-iso8859-1
))
101 (write-multibyte-character r0 r1
))
103 ;; mule-unicode-0100-24ff (< 0800)
104 ((r0 = ,(charset-id 'mule-unicode-0100-24ff
))
106 (r2 = (((r1 / 96) + 32) << 7))
109 (write-multibyte-character r0 r1
)))))
114 (r3 = ((r0 & #x0f
) << 12))
115 (r3 += ((r1 & #x3f
) << 6))
117 ;; now r3 holds scalar value
119 ;; mule-unicode-0100-24ff (>= 0800)
121 ((r0 = ,(charset-id 'mule-unicode-0100-24ff
))
125 (r1 += ((r3 + 32) << 7))
126 (write-multibyte-character r0 r1
))
128 ;; mule-unicode-2500-33ff
130 ((r0 = ,(charset-id 'mule-unicode-2500-33ff
))
134 (r1 += ((r3 + 32) << 7))
135 (write-multibyte-character r0 r1
))
138 ;; keep those bytes as eight-bit-{control|graphic}
140 (;; #xe0 < r0 < #xf0, so r0 is eight-bit-graphic
141 (r3 = ,(charset-id 'eight-bit-graphic
))
142 (write-multibyte-character r3 r0
)
144 (r3 = ,(charset-id 'eight-bit-control
)))
145 (write-multibyte-character r3 r1
)
147 (r3 = ,(charset-id 'eight-bit-control
))
148 (r3 = ,(charset-id 'eight-bit-graphic
)))
149 (write-multibyte-character r3 r2
))
151 ;; mule-unicode-e000-ffff
152 ((r0 = ,(charset-id 'mule-unicode-e000-ffff
))
156 (r1 += ((r3 + 32) << 7))
157 (write-multibyte-character r0 r1
))))))
160 ;; keep those bytes as eight-bit-{control|graphic}
162 ;; r0 > #xf0, thus eight-bit-graphic
163 (r4 = ,(charset-id 'eight-bit-graphic
))
164 (write-multibyte-character r4 r0
)
166 (r4 = ,(charset-id 'eight-bit-control
)))
167 (write-multibyte-character r4 r1
)
169 (r4 = ,(charset-id 'eight-bit-control
))
170 (r4 = ,(charset-id 'eight-bit-graphic
)))
171 (write-multibyte-character r4 r2
)
173 (r4 = ,(charset-id 'eight-bit-control
))
174 (r4 = ,(charset-id 'eight-bit-graphic
)))
175 (write-multibyte-character r4 r3
)))))
179 "CCL program to decode UTF-8 into ascii, eight-bit-control, latin-iso8859-1 and mule-unicode-*.")
181 (define-ccl-program ccl-encode-mule-utf-8
184 (read-multibyte-character r0 r1
)
186 (translate-character ucs-mule-8859-to-mule-unicode r0 r1
)
188 (if (r0 == ,(charset-id 'ascii
))
191 (if (r0 == ,(charset-id 'latin-iso8859-1
))
193 ;; 0000 0yyy yyxx xxxx 110y yyyy 10xx xxxx
194 ;; 20 0000 0000 1010 0000 1100 0010 1010 0000
195 ;; 7f 0000 0000 1111 1111 1100 0011 1011 1111
196 ((r0 = (((r1 & #x40
) >> 6) |
#xc2
))
201 (if (r0 == ,(charset-id 'mule-unicode-0100-24ff
))
202 ((r0 = ((((r1 & #x3f80
) >> 7) -
32) * 96))
203 ;; #x3f80 == (0011 1111 1000 0000)b
205 (r1 += (r0 + 224)) ; 240 == -32 + #x0100
206 ;; now r1 holds scalar value
209 ((r0 = (((r1 & #x07c0
) >> 6) |
#xc0
))
210 ;; #x07c0 == (0000 0111 1100 0000)b
215 ((r0 = (((r1 & #xf000
) >> 12) |
#xe0
))
216 (r2 = ((r1 & #x3f
) |
#x80
))
222 (if (r0 == ,(charset-id 'mule-unicode-2500-33ff
))
223 ((r0 = ((((r1 & #x3f80
) >> 7) -
32) * 96))
225 (r1 += (r0 + 9440)) ; 9440 == -32 + #x2500
226 (r0 = (((r1 & #xf000
) >> 12) |
#xe0
))
227 (r2 = ((r1 & #x3f
) |
#x80
))
233 (if (r0 == ,(charset-id 'mule-unicode-e000-ffff
))
234 ((r0 = ((((r1 & #x3f80
) >> 7) -
32) * 96))
236 (r1 += (r0 + 57312)) ; 57312 == -160 + #xe000
237 (r0 = (((r1 & #xf000
) >> 12) |
#xe0
))
238 (r2 = ((r1 & #x3f
) |
#x80
))
244 (if (r0 == ,(charset-id 'eight-bit-control
))
246 ;; 0000 0yyy yyxx xxxx 110y yyyy 10xx xxxx
247 ;; 80 0000 0000 1000 0000 1100 0010 1000 0000
248 ;; 9f 0000 0000 1001 1111 1100 0010 1001 1111
251 (if (r0 == ,(charset-id 'eight-bit-graphic
))
253 ;; 0000 0yyy yyxx xxxx 110y yyyy 10xx xxxx
254 ;; a0 0000 0000 1010 0000 1100 0010 1010 0000
255 ;; ff 0000 0000 1111 1111 1101 1111 1011 1111
258 ;; unsupported character.
259 ;; output U+FFFD, which is `ef bf bd' in UTF-8
260 ;; actually it never reach here
266 "CCL program to encode ascii, eight-bit-control, latin-iso8859-1 and mule-unicode-*. into UTF-8.")
270 "UTF-8 encoding for Emacs-supported Unicode characters.
271 Supported character sets are:
276 mule-unicode-0100-24ff
277 mule-unicode-2500-33ff
278 mule-unicode-e000-ffff
280 Unicode characters out of these ranges are decoded
281 into eight-bit-control or eight-bit-graphic."
283 '(ccl-decode-mule-utf-8 . ccl-encode-mule-utf-8
)
298 mule-unicode-0100-24ff
299 mule-unicode-2500-33ff
300 mule-unicode-e000-ffff
)
301 (mime-charset . utf-8
)
302 ;; Kluge to ensure the translation table is loaded.
303 `(pre-write-conversion .
,(lambda (junk) (require 'ucs-tables
)))))
305 (define-coding-system-alias 'utf-8
'mule-utf-8
)