(insert-ideograph-radical-char-data): Ignore non-Unicode UCS
[chise/xemacs-chise.git-] / lisp / utf-2000 / ideograph-util.el
1 ;;; ideograph-util.el --- Ideographic Character Database utility
2
3 ;; Copyright (C) 1999,2000,2001,2002 MORIOKA Tomohiko.
4
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: UTF-2000, ISO/IEC 10646, Unicode, UCS-4, MULE.
7
8 ;; This file is part of XEmacs UTF-2000.
9
10 ;; XEmacs UTF-2000 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 ;; XEmacs UTF-2000 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 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 XEmacs UTF-2000; 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 ;;; Code:
26
27 (require 'char-db-util)
28
29 (defvar ideograph-radical-chars-vector
30   (make-vector 215 nil))
31
32 (defun char-ideographic-radical (char &optional radical)
33   (let (ret)
34     (or (catch 'tag
35           (dolist (cell (get-char-attribute char 'ideographic-))
36             (if (and (setq ret (plist-get cell :radical))
37                      (or (eq ret radical)
38                          (null radical)))
39                 (throw 'tag ret))))
40         (get-char-attribute char 'ideographic-radical)
41         (progn
42           (setq ret
43                 (or (get-char-attribute char 'daikanwa-radical)
44                     (get-char-attribute char 'kangxi-radical)
45                     (get-char-attribute char 'japanese-radical)
46                     (get-char-attribute char 'korean-radical)))
47           (when ret
48             (put-char-attribute char 'ideographic-radical ret)
49             ret)))))
50
51 (defvar ideograph-radical-strokes-vector
52   ;;0  1  2  3  4  5  6  7  8  9
53   [nil 1  1  1  1  1  1  2  2  2
54     2  2  2  2  2  2  2  2  2  2
55     2  2  2  2  2  2  2  2  2  2
56     3  3  3  3  3  3  3  3  3  3
57     3  3  3  3  3  3  3  3  3  3
58     3  3  3  3  3  3  3  3  3  3
59     3  4  4  4  3  4  4  4  4  4
60     4  4  4  4  4  4  4  4  4  4
61     4  4  4  4  4  3  4  4  4  4
62     4  4  4  4  3  5  4  5  5  5
63     ;; 100
64     5  5  5  5  5  5  5  5  5  5
65     5  5  5  5  5  5  5  5  6  6
66     6  6  6  6  6  6  6  6  6  6
67     4  6  6  6  6  6  6  6  6  6
68     4  6  6  6  6  6  6  7  7  7
69     7  7  7  7  7  7  7  7  7  7
70     7  7  4  3  7  7  7  8  7  8
71     3  8  8  8  8  8  9  9  9  9
72     9  9  9  9  8  9  9 10 10 10
73    10 10 10 10 10 11 11 11 11 11
74    ;; 200
75    11 12 12 12 12 13 13 13 13 14
76    14 15 16 16 17])
77
78 (defun char-ideographic-strokes (char &optional radical)
79   (let (ret)
80     (or (catch 'tag
81           (dolist (cell (get-char-attribute char 'ideographic-))
82             (if (and (setq ret (plist-get cell :radical))
83                      (or (eq ret radical)
84                          (null radical)))
85                 (throw 'tag (plist-get cell :strokes)))))
86         (get-char-attribute char 'daikanwa-strokes)
87         (get-char-attribute char 'ideographic-strokes)
88         (let ((strokes
89                (or (get-char-attribute char 'kangxi-strokes)
90                    (get-char-attribute char 'japanese-strokes)
91                    (get-char-attribute char 'korean-strokes)
92                    (let ((r (char-ideographic-radical char))
93                          (ts (get-char-attribute char 'total-strokes)))
94                      (if (and r ts)
95                          (- ts (aref ideograph-radical-strokes-vector r))))
96                    )))
97           (when strokes
98             (put-char-attribute char 'ideographic-strokes strokes)
99             strokes)))))
100
101 ;;;###autoload
102 (defun update-ideograph-radical-table ()
103   (interactive)
104   (let (ret radical script)
105     (map-char-attribute
106      (lambda (char radical)
107        (when (and radical
108                   (or (null (setq script (get-char-attribute char 'script)))
109                       (memq 'Ideograph script)))
110          (unless (memq char
111                        (setq ret
112                              (aref ideograph-radical-chars-vector radical)))
113            (char-ideographic-strokes char)
114            (aset ideograph-radical-chars-vector radical
115                  (cons char ret))))
116        nil)
117      'ideographic-radical)
118     (map-char-attribute
119      (lambda (char data)
120        (dolist (cell data)
121          (setq radical (plist-get cell :radical))
122          (when (and radical
123                     (or (null (setq script (get-char-attribute char 'script)))
124                         (memq 'Ideograph script)))
125            (unless (memq char
126                          (setq ret
127                                (aref ideograph-radical-chars-vector radical)))
128              (char-ideographic-strokes char)
129              (aset ideograph-radical-chars-vector radical
130                    (cons char ret))))))
131      'ideographic-)))
132
133 (defun int-list< (a b)
134   (if (numberp (car a))
135       (if (numberp (car b))
136           (if (= (car a) (car b))
137               (int-list< (cdr a)(cdr b))
138             (< (car a) (car b)))
139         nil)
140     (numberp (car b))))
141
142 (defun morohashi-daikanwa< (a b)
143   (if (integerp a)
144       (setq a (list a)))
145   (if (integerp b)
146       (setq b (list b)))
147   (cond ((eq (car a) 'ho)
148          (if (eq (car b) 'ho)
149              (int-list< (cdr a)(cdr b))
150            nil))
151         ((numberp (car a))
152          (if (eq (car b) 'ho)
153              t
154            (int-list< a b)))
155         (t
156          (if (eq (car b) 'ho)
157              t
158            (int-list< a b)))))
159
160 ;; (defun nil=-int< (a b)
161 ;;   (cond ((null a) nil)
162 ;;         ((null b) nil)
163 ;;         (t (< a b))))
164
165 ;; (defun nil>-int< (a b)
166 ;;   (cond ((null a) nil)
167 ;;         ((null b) t)
168 ;;         (t (< a b))))
169
170 (defun char-representative-of-daikanwa (char)
171   (if (get-char-attribute char 'ideograph-daikanwa)
172       char
173     (let ((m (get-char-attribute char 'morohashi-daikanwa))
174           m-m m-s pat)
175       (or (when m
176             (setq m-m (pop m))
177             (setq m-s (pop m))
178             (if (= m-s 0)
179                 (decode-char 'ideograph-daikanwa m-m)
180               (when m
181                 (setq pat (list m-m m-s))
182                 (map-char-attribute (lambda (c v)
183                                       (if (equal pat v)
184                                           c))
185                                     'morohashi-daikanwa))))
186           char))))
187
188 (defun char-attributes-poly< (c1 c2 accessors testers defaulters)
189   (catch 'tag
190     (let (a1 a2 accessor tester dm)
191       (while (and accessors testers)
192         (setq accessor (car accessors)
193               tester (car testers)
194               dm (car defaulters))
195         (when (and accessor tester)
196           (setq a1 (funcall accessor c1)
197                 a2 (funcall accessor c2))
198           (cond ((null a1)
199                  (if a2
200                      (cond ((eq dm '<)
201                             (throw 'tag t))
202                            ((eq dm '>)
203                             (throw 'tag nil)))))
204                 ((null a2)
205                  (cond ((eq dm '<)
206                         (throw 'tag nil))
207                        ((eq dm '>)
208                         (throw 'tag t))))
209                 (t
210                  (cond ((funcall tester a1 a2)
211                         (throw 'tag t))
212                        ((funcall tester a2 a1)
213                         (throw 'tag nil))))))
214         (setq accessors (cdr accessors)
215               testers (cdr testers)
216               defaulters (cdr defaulters))))))
217
218 (defvar ideographic-radical nil)
219
220 (defun char-daikanwa-strokes (char &optional radical)
221   (unless radical
222     (setq radical ideographic-radical))
223   (let ((drc (char-representative-of-daikanwa char)))
224     (char-ideographic-strokes
225      (if (= (char-ideographic-radical drc radical)
226             (char-ideographic-radical char radical))
227          drc
228        char)
229      radical)))
230
231 ;;;###autoload
232 (defun char-daikanwa (char)
233   (or (get-char-attribute char 'ideograph-daikanwa)
234       (get-char-attribute char 'morohashi-daikanwa)))
235
236 ;;;###autoload
237 (defun char-ucs (char)
238   (or (get-char-attribute char 'ucs)
239       (get-char-attribute char '=>ucs)
240       (get-char-attribute char '->ucs)))
241
242 (defun char-id (char)
243   (logand (char-int char) #x3FFFFFFF))
244
245 (defun ideograph-char< (a b &optional radical)
246   (let ((ideographic-radical (or radical
247                                  ideographic-radical)))
248     (char-attributes-poly<
249      a b
250      '(char-daikanwa-strokes char-daikanwa char-ucs char-id)
251      '(< morohashi-daikanwa< < <)
252      '(> > > >))))
253
254 (defun insert-ideograph-radical-char-data (radical)
255   (let ((chars
256          (sort (copy-list (aref ideograph-radical-chars-vector radical))
257                (lambda (a b)
258                  (ideograph-char< a b radical))))
259         attributes ccss)
260     (dolist (name (char-attribute-list))
261       (unless (memq name char-db-ignored-attributes)
262         (if (find-charset name)
263             (push name ccss)
264           (push name attributes))))
265     (setq attributes (sort attributes #'char-attribute-name<)
266           ccss (sort ccss #'char-attribute-name<))
267     (aset ideograph-radical-chars-vector radical chars)
268     (dolist (char chars)
269       (when (some (lambda (ccs)
270                     (let ((code (encode-char char ccs)))
271                       (and code
272                            ;;(not (memq ccs char-db-ignored-attributes))
273                            ;;(or (not (memq ccs '(ucs))
274                            (and (<= 0 code)(<= code #x10FFFF)))))
275                   ccss)
276         (insert-char-data char nil attributes ccss)))))
277
278 (defun write-ideograph-radical-char-data (radical file)
279   (if (file-directory-p file)
280       (let ((name (get-char-attribute (int-char (+ #x2EFF radical)) 'name)))
281         (if (string-match "KANGXI RADICAL " name)
282             (setq name (capitalize (substring name (match-end 0)))))
283         (setq name (mapconcat (lambda (char)
284                                 (if (eq char ? )
285                                     "-"
286                                   (char-to-string char))) name ""))
287         (setq file
288               (expand-file-name
289                (format "Ideograph-R%03d-%s.el" radical name)
290                file))))
291   (with-temp-buffer
292     (insert-ideograph-radical-char-data radical)
293     (char-db-update-comment)
294     (let ((coding-system-for-write 'utf-8))
295       (write-region (point-min)(point-max) file)
296       )))
297
298 (defun ideographic-structure= (char1 char2)
299   (if (char-ref-p char1)
300       (setq char1 (plist-get char1 :char)))
301   (if (char-ref-p char2)
302       (setq char2 (plist-get char2 :char)))
303   (let ((s1 (if (characterp char1)
304                 (get-char-attribute char1 'ideographic-structure)
305               (cdr (assq 'ideographic-structure char1))))
306         (s2 (if (characterp char2)
307                 (get-char-attribute char2 'ideographic-structure)
308               (cdr (assq 'ideographic-structure char2))))
309         e1 e2)
310     (if (or (null s1)(null s2))
311         (char-spec= char1 char2)
312       (catch 'tag
313         (while (and s1 s2)
314           (setq e1 (car s1)
315                 e2 (car s2))
316           (unless (ideographic-structure= e1 e2)
317             (throw 'tag nil))
318           (setq s1 (cdr s1)
319                 s2 (cdr s2)))
320         (and (null s1)(null s2))))))
321
322 ;;;###autoload
323 (defun ideographic-structure-find-char (structure)
324   (let (rest)
325     (map-char-attribute (lambda (char value)
326                           (setq rest structure)
327                           (catch 'tag
328                             (while (and rest value)
329                               (unless (ideographic-structure=
330                                        (car rest)(car value))
331                                 (throw 'tag nil))
332                               (setq rest (cdr rest)
333                                     value (cdr value)))
334                             (unless (or rest value)
335                               char)))
336                         'ideographic-structure)))
337
338 (provide 'ideograph-util)
339
340 ;;; ideograph-util.el ends here