(char-cns11643-p): New function.
[chise/ids.git] / ids-util.el
1 ;;; ids-util.el --- Utilities about ideographic-structure -*- coding: utf-8 -*-
2
3 ;; Copyright (C) 2001,2002 MORIOKA Tomohiko
4
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: ideographic-structure, UTF-2000, database
7
8 ;; This file is a part of Tomoyo Utilities.
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 ;;;###autoload
30 (defun ideographic-structure-convert-to-ucs (structure)
31   (let (dest cell ucs ret)
32     (while structure
33       (setq cell (car structure))
34       (setq dest
35             (cons
36              (cond ((characterp cell)
37                     (if (or (get-char-attribute cell 'ucs)
38                             (null
39                              (setq ucs
40                                    (or (get-char-attribute cell '=>ucs)
41                                        (get-char-attribute cell '->ucs)))))
42                         cell
43                       (decode-char 'ucs ucs)))
44                    ((and (consp cell)
45                          (symbolp (car cell)))
46                     cell)
47                    ((setq ret (find-char cell))
48                     (if (or (get-char-attribute ret 'ucs)
49                             (null
50                              (setq ucs
51                                    (or (get-char-attribute ret '=>ucs)
52                                        (get-char-attribute ret '->ucs)))))
53                         cell
54                       (decode-char 'ucs ucs)))
55                    ((setq ret (assq 'ideographic-structure cell))
56                     (put-alist 'ideographic-structure
57                                (ideographic-structure-convert-to-ucs
58                                 (cdr ret))
59                                (copy-alist cell)))
60                    (t cell))
61              dest))
62       (setq structure (cdr structure)))
63     (nreverse dest)))
64
65 (defun char-cns11643-p (char &optional defined-only)
66   (some (lambda (n)
67           (encode-char char
68                        (intern (format "=cns11643-%d" n))
69                        defined-only))
70         '(1 2 3 4 5 6 7)))
71
72 (defun char-representative-of-cns11643 (char)
73   (if (char-cns11643-p char)
74       char
75     (let ((ucs (char-ucs char))
76           variants)
77       (if (and ucs
78                (setq variants
79                      (char-variants (decode-char 'ucs ucs))))
80           (while (and variants
81                       (setq char (car variants))
82                       (not (char-cns11643-p char)))
83             (setq variants (cdr variants))))
84       char)))
85
86 (defun ideographic-structure-convert-to-cns11643 (structure)
87   (let (dest cell ucs ret)
88     (while structure
89       (setq cell (car structure))
90       (setq dest
91             (cons
92              (cond ((characterp cell)
93                     (char-representative-of-cns11643 cell))
94                    ((and (consp cell)
95                          (symbolp (car cell)))
96                     cell)
97                    ((setq ret (find-char cell))
98                     (char-representative-of-cns11643 ret))
99                    ((setq ret (assq 'ideographic-structure cell))
100                     (put-alist 'ideographic-structure
101                                (ideographic-structure-convert-to-cns11643
102                                 (cdr ret))
103                                (copy-alist cell)))
104                    (t cell))
105              dest))
106       (setq structure (cdr structure)))
107     (nreverse dest)))
108
109 (defvar morohashi-char-replace-alist
110   (list
111    (cons (decode-char 'chinese-big5-cdp #x8B42)
112          (decode-char 'chinese-big5-cdp #x8B42))
113    (cons (decode-char 'chinese-big5-cdp #x8AFC)
114          (decode-char 'chinese-big5-cdp #x8AFC))
115    (cons (decode-char 'ucs #x2EBE)
116          (decode-char 'ucs #x2EBF))
117    (cons (decode-char 'ucs #x4EA0)
118          (decode-char 'chinese-big5-cdp #x8B42))
119    (cons (decode-char 'ucs #x4EBD)
120          (decode-char 'chinese-big5-cdp #x8AFC))
121    (cons (decode-char 'ucs #x517C)
122          (decode-char 'ideograph-gt 01936))
123    (cons ?亼 (decode-char 'chinese-big5-cdp #x8AFC))
124    (cons (decode-char 'chinese-big5-cdp #x8AFC)
125          (decode-char 'chinese-big5-cdp #x8AFC))
126    (cons (decode-char 'chinese-big5-cdp #x8B69)
127          (decode-char 'chinese-big5-cdp #x8A60))
128    (cons (decode-char 'ucs #x4FDE)
129          (decode-char 'ideograph-daikanwa 01437))
130    (cons (decode-char 'ucs #x5151)
131          (decode-char 'ideograph-daikanwa 01356))
132    (cons (decode-char 'ucs #x5154)
133          (decode-char 'ideograph-daikanwa 01368))
134    (cons (decode-char 'ucs #x5179)
135          (decode-char 'ideograph-daikanwa 30911))
136    (cons (decode-char 'ucs #x518D)
137          (decode-char 'ideograph-daikanwa 01524))
138    (cons (decode-char 'ucs #x5193)
139          (decode-char 'ideograph-gt 02025))
140    (cons (decode-char 'ucs #x53CA)
141          (decode-char 'ideograph-daikanwa 03118))
142    (cons (decode-char 'ucs #x544A)
143          (decode-char 'ideograph-daikanwa 03381))
144    (cons (decode-char 'ucs #x5468)
145          (decode-char 'ideograph-daikanwa 03441))
146    '(?夂 . ?夂)
147    (cons (decode-char 'ucs #x5922)
148          (decode-char 'ideograph-daikanwa 05802))
149    (cons (decode-char 'ucs #x5C1A)
150          (decode-char 'ucs #x5C19))
151    (cons (decode-char 'ucs #x5D29)
152          (decode-char 'ideograph-daikanwa 08212))
153    (cons (decode-char 'ucs #x5F66)
154          (decode-char 'ideograph-daikanwa 09980))
155    (cons (decode-char 'ucs #x6247)
156          (decode-char 'ideograph-daikanwa 11743))
157    (cons (decode-char 'ucs #x656C)
158          (decode-char 'ideograph-daikanwa 13303))
159    (cons (decode-char 'ucs #x65E2)
160          (decode-char 'ideograph-daikanwa 13724))
161    (cons (decode-char 'ucs #x6B21)
162          (decode-char 'ideograph-daikanwa 15992))
163    (cons (decode-char 'ucs #x7235)
164          (decode-char 'ideograph-daikanwa 19711))
165    (cons (decode-char 'ucs #x7523)
166          (decode-char 'ideograph-daikanwa 21684))
167    (cons (decode-char 'ucs #x76CA)
168          (decode-char 'ideograph-daikanwa 22972))
169    (cons (decode-char 'ucs #x771F)
170          (decode-char 'ideograph-daikanwa 23235))
171    (cons (decode-char 'ucs #x7FBD)
172          (decode-char 'ideograph-daikanwa 28614))
173    (cons (decode-char 'ucs #x7FC1)
174          (decode-char 'ideograph-daikanwa 28635))
175    (cons (decode-char 'ucs #x2EA4)
176          (decode-char 'ucs #x722B))
177    (cons (decode-char 'ucs #x8005)
178          (decode-char 'ideograph-daikanwa 28853))
179    (cons (decode-char 'ucs #x8096)
180          (decode-char 'ideograph-daikanwa 29263))
181    (cons (decode-char 'ucs #x82E5)
182          (decode-char 'ideograph-daikanwa 30796))
183    (cons (decode-char 'ucs #x82D7)
184          (decode-char 'ideograph-daikanwa 30781))
185    (cons (decode-char 'ucs #x82F1)
186          (decode-char 'ideograph-daikanwa 30808))
187    (cons (decode-char 'ucs #x8336)
188          (decode-char 'ideograph-daikanwa 30915))
189    (cons (decode-char 'ucs #x8449)
190          (decode-char 'ideograph-daikanwa 31387))
191    (cons (decode-char 'ucs #x9023)
192          (decode-char 'ideograph-daikanwa 38902))
193    (cons (decode-char 'ucs #x9053)
194          (decode-char 'ideograph-daikanwa 39010))
195    (cons (decode-char 'ucs #x9054)
196          (decode-char 'ideograph-daikanwa 39011))
197    (cons (decode-char 'ucs #x9063)
198          (decode-char 'ideograph-daikanwa 39052))
199    (cons (decode-char 'ucs #x9752)
200          (decode-char 'ucs #x9751))
201    (cons (decode-char 'ucs #x670B)
202          (decode-char 'ideograph-daikanwa 14340))
203    (cons (decode-char 'ucs #x8981)
204          (decode-char 'ideograph-daikanwa 34768))
205    (cons (decode-char 'ucs #x8AF8)
206          (decode-char 'ideograph-daikanwa 35743))
207    (cons (decode-char 'japanese-jisx0213-2 #x2327)
208          (decode-char 'japanese-jisx0213-2 #x2327))
209    (cons (decode-char 'chinese-big5-cdp #x8BFA)
210          (decode-char 'japanese-jisx0213-2 #x2327))
211    ))
212
213 ;;;###autoload
214 (defun ideographic-structure-convert-to-daikanwa (structure)
215   (let (dest cell morohashi ret ret2 ret3)
216     (while structure
217       (setq cell (car structure))
218       (setq dest
219             (cons
220              (cond ((characterp cell)
221                     (cond ((setq ret
222                                  (assq cell morohashi-char-replace-alist))
223                            (cdr ret))
224                           ((get-char-attribute cell 'ideograph-daikanwa)
225                            cell)
226                           ((setq morohashi
227                                  (get-char-attribute
228                                   cell 'morohashi-daikanwa))
229                            (cond ((null (cdr (cdr morohashi)))
230                                   cell)
231                                  ((= (nth 1 morohashi) 0)
232                                   (decode-char 'ideograph-daikanwa
233                                                (car morohashi)))
234                                  (t
235                                   (setq morohashi (list (car morohashi)
236                                                         (nth 1 morohashi)))
237                                   (or (map-char-attribute
238                                        (lambda (char val)
239                                          (if (equal morohashi val)
240                                              char))
241                                        'morohashi-daikanwa)
242                                       cell))))
243                           (t
244                            cell)))
245                    ((and (consp cell)
246                          (symbolp (car cell)))
247                     cell)
248                    ((setq ret (find-char cell))
249                     (if (or (get-char-attribute ret 'ideograph-daikanwa)
250                             (null
251                              (setq morohashi
252                                    (get-char-attribute
253                                     ret 'morohashi-daikanwa)))
254                             (null (cdr (cdr morohashi))))
255                         cell
256                       (if (= (nth 1 morohashi) 0)
257                           (decode-char 'ideograph-daikanwa (car morohashi))
258                         cell)))
259                    ((setq ret (assq 'ideographic-structure cell))
260                     (setq ret2
261                           (ideographic-structure-convert-to-daikanwa
262                            (cdr ret)))
263                     (if (setq ret3 (ideographic-structure-find-char ret2))
264                         ret3
265                       (put-alist 'ideographic-structure
266                                  ret2
267                                  (copy-alist cell))))
268                    (t cell))
269              dest))
270       (setq structure (cdr structure)))
271     (nreverse dest)))
272
273
274 ;;; @ End.
275 ;;;
276
277 (provide 'ids-util)
278
279 ;;; ids-util.el ends here