(char-daikanwa): Use `<-subsumptive' instead of `<-unified'.
[chise/xemacs-chise.git.1] / lisp / utf-2000 / ideograph-util.el
1 ;;; ideograph-util.el --- Ideographic Character Database utility
2
3 ;; Copyright (C) 1999,2000,2001,2002,2003,2004 MORIOKA Tomohiko.
4
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: CHISE, Chaon model, ISO/IEC 10646, Unicode, UCS-4, MULE.
7
8 ;; This file is part of XEmacs CHISE.
9
10 ;; XEmacs CHISE 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 CHISE 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 XEmacs CHISE; 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 (domain char-db-feature-domains)
36             (if (and (setq ret (char-feature
37                                 char
38                                 (intern
39                                  (format "%s@%s"
40                                          'ideographic-radical domain))))
41                      (or (eq ret radical)
42                          (null radical)))
43                 (throw 'tag ret))))
44         (catch 'tag
45           (dolist (cell (get-char-attribute char 'ideographic-))
46             (if (and (setq ret (plist-get cell :radical))
47                      (or (eq ret radical)
48                          (null radical)))
49                 (throw 'tag ret))))
50         (char-feature char 'ideographic-radical)
51         (progn
52           (setq ret
53                 (or (get-char-attribute char 'daikanwa-radical)
54                     (get-char-attribute char 'kangxi-radical)
55                     (get-char-attribute char 'japanese-radical)
56                     (get-char-attribute char 'korean-radical)))
57           (when ret
58             (put-char-attribute char 'ideographic-radical ret)
59             ret)))))
60
61 (defvar ideograph-radical-strokes-vector
62   ;;0  1  2  3  4  5  6  7  8  9
63   [nil 1  1  1  1  1  1  2  2  2
64     2  2  2  2  2  2  2  2  2  2
65     2  2  2  2  2  2  2  2  2  2
66     3  3  3  3  3  3  3  3  3  3
67     3  3  3  3  3  3  3  3  3  3
68     3  3  3  3  3  3  3  3  3  3
69     3  4  4  4  3  4  4  4  4  4
70     4  4  4  4  4  4  4  4  4  4
71     4  4  4  4  4  3  4  4  4  4
72     4  4  4  4  3  5  4  5  5  5
73     ;; 100
74     5  5  5  5  5  5  5  5  5  5
75     5  5  5  5  5  5  5  5  6  6
76     6  6  6  6  6  6  6  6  6  6
77     4  6  6  6  6  6  6  6  6  6
78     4  6  6  6  6  6  6  7  7  7
79     7  7  7  7  7  7  7  7  7  7
80     7  7  4  3  7  7  7  8  7  8
81     3  8  8  8  8  8  9  9  9  9
82     9  9  9  9  8  9  9 10 10 10
83    10 10 10 10 10 11 11 11 11 11
84    ;; 200
85    11 12 12 12 12 13 13 13 13 14
86    14 15 16 16 17])
87
88 ;;;###autoload
89 (defun char-ideographic-strokes-from-domains (char domains &optional radical)
90   (let (ret)
91     (catch 'tag
92       (dolist (domain domains)
93         (if (and (setq ret (or (char-feature
94                                 char
95                                 (intern
96                                  (format "%s@%s"
97                                          'ideographic-radical domain)))
98                                (char-feature
99                                 char 'ideographic-radical)))
100                  (or (eq ret radical)
101                      (null radical))
102                  (setq ret (char-feature
103                             char
104                             (intern
105                              (format "%s@%s"
106                                      'ideographic-strokes domain)))))
107             (throw 'tag ret))))))
108
109 ;;;###autoload
110 (defun char-ideographic-strokes (char &optional radical preferred-domains)
111   (let (ret)
112     (or (char-ideographic-strokes-from-domains
113          char preferred-domains radical)
114         (char-feature char 'ideographic-strokes)
115         (char-ideographic-strokes-from-domains
116          char char-db-feature-domains radical)
117         (catch 'tag
118           (dolist (cell (get-char-attribute char 'ideographic-))
119             (if (and (setq ret (plist-get cell :radical))
120                      (or (eq ret radical)
121                          (null radical)))
122                 (throw 'tag (plist-get cell :strokes)))))
123         (get-char-attribute char 'daikanwa-strokes)
124         (let ((strokes
125                (or (get-char-attribute char 'kangxi-strokes)
126                    (get-char-attribute char 'japanese-strokes)
127                    (get-char-attribute char 'korean-strokes)
128                    (let ((r (char-ideographic-radical char))
129                          (ts (get-char-attribute char 'total-strokes)))
130                      (if (and r ts)
131                          (- ts (aref ideograph-radical-strokes-vector r))))
132                    )))
133           (when strokes
134             (put-char-attribute char 'ideographic-strokes strokes)
135             strokes)))))
136
137 ;;;###autoload
138 (defun char-total-strokes-from-domains (char domains)
139   (let (ret)
140     (catch 'tag
141       (dolist (domain domains)
142         (if (setq ret (get-char-attribute
143                        char
144                        (intern
145                         (format "%s@%s"
146                                 'total-strokes domain))))
147             (throw 'tag ret))))))
148
149 ;;;###autoload
150 (defun char-total-strokes (char &optional preferred-domains)
151   (or (char-total-strokes-from-domains char preferred-domains)
152       (get-char-attribute char 'total-strokes)
153       (char-total-strokes-from-domains char char-db-feature-domains)))
154
155 ;;;###autoload
156 (defun update-ideograph-radical-table ()
157   (interactive)
158   (let (ret radical script)
159     (dolist (feature
160              (cons 'ideographic-radical
161                    (mapcar
162                     (lambda (domain)
163                       (intern (format "%s@%s" 'ideographic-radical domain)))
164                     char-db-feature-domains)))
165       (map-char-attribute
166        (lambda (chr radical)
167          (dolist (char (cons chr
168                              (get-char-attribute chr '->denotational)))
169            (when (and radical
170                       (or (null (setq script
171                                       (get-char-attribute char 'script)))
172                           (memq 'Ideograph script)))
173              (unless (memq char
174                            (setq ret
175                                  (aref ideograph-radical-chars-vector
176                                        radical)))
177                (char-ideographic-strokes char)
178                (aset ideograph-radical-chars-vector radical
179                      (cons char ret)))))
180          nil)
181        feature))
182     (map-char-attribute
183      (lambda (char data)
184        (dolist (cell data)
185          (setq radical (plist-get cell :radical))
186          (when (and radical
187                     (or (null (setq script (get-char-attribute char 'script)))
188                         (memq 'Ideograph script)))
189            (unless (memq char
190                          (setq ret
191                                (aref ideograph-radical-chars-vector radical)))
192              (char-ideographic-strokes char)
193              (aset ideograph-radical-chars-vector radical
194                    (cons char ret))))))
195      'ideographic-)))
196
197 (defun int-list< (a b)
198   (if (numberp (car a))
199       (if (numberp (car b))
200           (if (= (car a) (car b))
201               (int-list< (cdr a)(cdr b))
202             (< (car a) (car b)))
203         nil)
204     (numberp (car b))))
205
206 (defun morohashi-daikanwa< (a b)
207   (if (integerp a)
208       (setq a (list a)))
209   (if (integerp b)
210       (setq b (list b)))
211   (cond ((eq (car a) 'ho)
212          (if (eq (car b) 'ho)
213              (int-list< (cdr a)(cdr b))
214            nil))
215         ((numberp (car a))
216          (if (eq (car b) 'ho)
217              t
218            (int-list< a b)))
219         (t
220          (if (eq (car b) 'ho)
221              t
222            (int-list< a b)))))
223
224 ;; (defun nil=-int< (a b)
225 ;;   (cond ((null a) nil)
226 ;;         ((null b) nil)
227 ;;         (t (< a b))))
228
229 ;; (defun nil>-int< (a b)
230 ;;   (cond ((null a) nil)
231 ;;         ((null b) t)
232 ;;         (t (< a b))))
233
234 ;;;###autoload
235 (defun char-representative-of-daikanwa (char)
236   (if (or (encode-char char 'ideograph-daikanwa 'defined-only)
237           (encode-char char '=daikanwa-rev2 'defined-only))
238       char
239     (let ((m (char-feature char '=>daikanwa))
240           m-m m-s pat)
241       (or (and (integerp m)
242                (or (decode-char '=daikanwa-rev2 m 'defined-only)
243                    (decode-char 'ideograph-daikanwa m)))
244           (when (or m
245                     (setq m (get-char-attribute char 'morohashi-daikanwa)))
246             (setq m-m (pop m))
247             (setq m-s (pop m))
248             (if (= m-s 0)
249                 (or (decode-char '=daikanwa-rev2 m-m 'defined-only)
250                     (decode-char 'ideograph-daikanwa m-m))
251               (when m
252                 (setq pat (list m-m m-s))
253                 (map-char-attribute (lambda (c v)
254                                       (if (equal pat v)
255                                           c))
256                                     'morohashi-daikanwa))))
257           char))))
258
259 (defun char-attributes-poly< (c1 c2 accessors testers defaulters)
260   (catch 'tag
261     (let (a1 a2 accessor tester dm)
262       (while (and accessors testers)
263         (setq accessor (car accessors)
264               tester (car testers)
265               dm (car defaulters))
266         (when (and accessor tester)
267           (setq a1 (funcall accessor c1)
268                 a2 (funcall accessor c2))
269           (cond ((null a1)
270                  (if a2
271                      (cond ((eq dm '<)
272                             (throw 'tag t))
273                            ((eq dm '>)
274                             (throw 'tag nil)))))
275                 ((null a2)
276                  (cond ((eq dm '<)
277                         (throw 'tag nil))
278                        ((eq dm '>)
279                         (throw 'tag t))))
280                 (t
281                  (cond ((funcall tester a1 a2)
282                         (throw 'tag t))
283                        ((funcall tester a2 a1)
284                         (throw 'tag nil))))))
285         (setq accessors (cdr accessors)
286               testers (cdr testers)
287               defaulters (cdr defaulters))))))
288
289 (defvar ideographic-radical nil)
290
291 (defun char-daikanwa-strokes (char &optional radical)
292   (unless radical
293     (setq radical ideographic-radical))
294   (let ((drc (char-representative-of-daikanwa char)))
295     (if (= (char-ideographic-radical drc radical)
296            (char-ideographic-radical char radical))
297         (setq char drc)))
298   (char-ideographic-strokes char radical '(daikanwa)))
299
300 ;;;###autoload
301 (defun char-daikanwa (char)
302   (or (encode-char char 'ideograph-daikanwa 'defined-only)
303       (encode-char char '=daikanwa-rev2 'defined-only)
304       (let ((ret (char-feature char '=>daikanwa)))
305         (and ret
306              (if (or (get-char-attribute char '<-subsumptive)
307                      (get-char-attribute char '<-denotational))
308                  (list ret 0)
309                ret)))
310       (get-char-attribute char 'morohashi-daikanwa)))
311
312 ;;;###autoload
313 (defun char-ucs (char)
314   (or (encode-char char '=ucs 'defined-only)
315       (get-char-attribute char '=>ucs)))
316
317 (defun char-id (char)
318   (logand (char-int char) #x3FFFFFFF))
319
320 (defun ideograph-char< (a b &optional radical)
321   (let ((ideographic-radical (or radical
322                                  ideographic-radical)))
323     (char-attributes-poly<
324      a b
325      '(char-daikanwa-strokes char-daikanwa char-ucs char-id)
326      '(< morohashi-daikanwa< < <)
327      '(> > > >))))
328
329 (defun insert-ideograph-radical-char-data (radical)
330   (let ((chars
331          (sort (copy-list (aref ideograph-radical-chars-vector radical))
332                (lambda (a b)
333                  (ideograph-char< a b radical))))
334         attributes ccss)
335     (dolist (name (char-attribute-list))
336       (unless (memq name char-db-ignored-attributes)
337         (if (find-charset name)
338             (push name ccss)
339           (push name attributes))))
340     (setq attributes (sort attributes #'char-attribute-name<)
341           ccss (sort ccss #'char-attribute-name<))
342     (aset ideograph-radical-chars-vector radical chars)
343     (dolist (char chars)
344       (when (or (not (some (lambda (atr)
345                              (get-char-attribute char atr))
346                            char-db-ignored-attributes))
347                 (some (lambda (ccs)
348                         (encode-char char ccs 'defined-only))
349                       ccss))
350         (insert-char-data char nil attributes ccss)))))
351
352 (defun write-ideograph-radical-char-data (radical file)
353   (if (file-directory-p file)
354       (let ((name (get-char-attribute (int-char (+ #x2EFF radical)) 'name)))
355         (if (string-match "KANGXI RADICAL " name)
356             (setq name (capitalize (substring name (match-end 0)))))
357         (setq name (mapconcat (lambda (char)
358                                 (if (eq char ? )
359                                     "-"
360                                   (char-to-string char))) name ""))
361         (setq file
362               (expand-file-name
363                (format "Ideograph-R%03d-%s.el" radical name)
364                file))))
365   (with-temp-buffer
366     (insert ";; -*- coding: utf-8-mcs -*-\n")
367     (insert-ideograph-radical-char-data radical)
368     (let ((coding-system-for-write 'utf-8-mcs))
369       (write-region (point-min)(point-max) file)
370       )))
371
372 (defun ideographic-structure= (char1 char2)
373   (if (char-ref-p char1)
374       (setq char1 (plist-get char1 :char)))
375   (if (char-ref-p char2)
376       (setq char2 (plist-get char2 :char)))
377   (let ((s1 (if (characterp char1)
378                 (get-char-attribute char1 'ideographic-structure)
379               (cdr (assq 'ideographic-structure char1))))
380         (s2 (if (characterp char2)
381                 (get-char-attribute char2 'ideographic-structure)
382               (cdr (assq 'ideographic-structure char2))))
383         e1 e2)
384     (if (or (null s1)(null s2))
385         (char-spec= char1 char2)
386       (catch 'tag
387         (while (and s1 s2)
388           (setq e1 (car s1)
389                 e2 (car s2))
390           (unless (ideographic-structure= e1 e2)
391             (throw 'tag nil))
392           (setq s1 (cdr s1)
393                 s2 (cdr s2)))
394         (and (null s1)(null s2))))))
395
396 ;;;###autoload
397 (defun ideographic-structure-find-char (structure)
398   (let (rest)
399     (map-char-attribute (lambda (char value)
400                           (setq rest structure)
401                           (catch 'tag
402                             (while (and rest value)
403                               (unless (ideographic-structure=
404                                        (car rest)(car value))
405                                 (throw 'tag nil))
406                               (setq rest (cdr rest)
407                                     value (cdr value)))
408                             (unless (or rest value)
409                               char)))
410                         'ideographic-structure)))
411
412 ;;;###autoload
413 (defun chise-string< (string1 string2 accessors)
414   (let ((len1 (length string1))
415         (len2 (length string2))
416         len
417         (i 0)
418         c1 c2
419         rest func
420         v1 v2)
421     (setq len (min len1 len2))
422     (catch 'tag
423       (while (< i len)
424         (setq c1 (aref string1 i)
425               c2 (aref string2 i))
426         (setq rest accessors)
427         (while (and rest
428                     (setq func (car rest))
429                     (setq v1 (funcall func c1)
430                           v2 (funcall func c2))
431                     (eq v1 v2))
432           (setq rest (cdr rest)))
433         (if v1
434             (if v2
435                 (cond ((< v1 v2)
436                        (throw 'tag t))
437                       ((> v1 v2)
438                        (throw 'tag nil)))
439               (throw 'tag nil))
440           (if v2
441               (throw 'tag t)))
442         (setq i (1+ i)))
443       (< len1 len2))))
444
445
446 (provide 'ideograph-util)
447
448 ;;; ideograph-util.el ends here