(update-ideograph-radical-table): Add characters specified in
[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 (catch 'tag
113           (dolist (cell (get-char-attribute char 'ideographic-))
114             (if (and (setq ret (plist-get cell :radical))
115                      (or (eq ret radical)
116                          (null radical)))
117                 (throw 'tag (plist-get cell :strokes)))))
118         (char-ideographic-strokes-from-domains
119          char preferred-domains radical)
120         (get-char-attribute char 'ideographic-strokes)
121         (char-ideographic-strokes-from-domains
122          char char-db-feature-domains radical)
123         (char-feature char 'ideographic-strokes)
124         (get-char-attribute char 'daikanwa-strokes)
125         (let ((strokes
126                (or (get-char-attribute char 'kangxi-strokes)
127                    (get-char-attribute char 'japanese-strokes)
128                    (get-char-attribute char 'korean-strokes)
129                    (let ((r (char-ideographic-radical char))
130                          (ts (get-char-attribute char 'total-strokes)))
131                      (if (and r ts)
132                          (- ts (aref ideograph-radical-strokes-vector r))))
133                    )))
134           (when strokes
135             (put-char-attribute char 'ideographic-strokes strokes)
136             strokes)))))
137
138 ;;;###autoload
139 (defun char-total-strokes-from-domains (char domains)
140   (let (ret)
141     (catch 'tag
142       (dolist (domain domains)
143         (if (setq ret (get-char-attribute
144                        char
145                        (intern
146                         (format "%s@%s"
147                                 'total-strokes domain))))
148             (throw 'tag ret))))))
149
150 ;;;###autoload
151 (defun char-total-strokes (char &optional preferred-domains)
152   (or (char-total-strokes-from-domains char preferred-domains)
153       (get-char-attribute char 'total-strokes)
154       (char-total-strokes-from-domains char char-db-feature-domains)))
155
156 ;;;###autoload
157 (defun update-ideograph-radical-table ()
158   (interactive)
159   (let (ret radical script)
160     (dolist (feature
161              (cons 'ideographic-radical
162                    (mapcar
163                     (lambda (domain)
164                       (intern (format "%s@%s" 'ideographic-radical domain)))
165                     char-db-feature-domains)))
166       (map-char-attribute
167        (lambda (chr radical)
168          (dolist (char (cons chr
169                              (append
170                               (get-char-attribute chr '<-identical)
171                               (get-char-attribute chr '->denotational))))
172            (when (and radical
173                       (eq radical
174                           (char-ideographic-radical char radical))
175                       (or (null (setq script
176                                       (get-char-attribute char 'script)))
177                           (memq 'Ideograph script)))
178              (unless (memq char
179                            (setq ret
180                                  (aref ideograph-radical-chars-vector
181                                        radical)))
182                (char-ideographic-strokes char)
183                (aset ideograph-radical-chars-vector radical
184                      (cons char ret)))))
185          nil)
186        feature))
187     (map-char-attribute
188      (lambda (char data)
189        (dolist (cell data)
190          (setq radical (plist-get cell :radical))
191          (when (and radical
192                     (or (null (setq script (get-char-attribute char 'script)))
193                         (memq 'Ideograph script)))
194            (unless (memq char
195                          (setq ret
196                                (aref ideograph-radical-chars-vector radical)))
197              (char-ideographic-strokes char)
198              (aset ideograph-radical-chars-vector radical
199                    (cons char ret))))))
200      'ideographic-)))
201
202 (defun int-list< (a b)
203   (if (numberp (car a))
204       (if (numberp (car b))
205           (if (= (car a) (car b))
206               (int-list< (cdr a)(cdr b))
207             (< (car a) (car b)))
208         nil)
209     (numberp (car b))))
210
211 (defun morohashi-daikanwa< (a b)
212   (if (integerp a)
213       (setq a (list a)))
214   (if (integerp b)
215       (setq b (list b)))
216   (cond ((eq (car a) 'ho)
217          (if (eq (car b) 'ho)
218              (int-list< (cdr a)(cdr b))
219            nil))
220         ((numberp (car a))
221          (if (eq (car b) 'ho)
222              t
223            (int-list< a b)))
224         (t
225          (if (eq (car b) 'ho)
226              t
227            (int-list< a b)))))
228
229 ;; (defun nil=-int< (a b)
230 ;;   (cond ((null a) nil)
231 ;;         ((null b) nil)
232 ;;         (t (< a b))))
233
234 ;; (defun nil>-int< (a b)
235 ;;   (cond ((null a) nil)
236 ;;         ((null b) t)
237 ;;         (t (< a b))))
238
239 ;;;###autoload
240 (defun char-representative-of-daikanwa (char)
241   (if (or (encode-char char 'ideograph-daikanwa 'defined-only)
242           (encode-char char '=daikanwa-rev2 'defined-only))
243       char
244     (let ((m (char-feature char '=>daikanwa))
245           m-m m-s pat)
246       (or (and (integerp m)
247                (or (decode-char '=daikanwa-rev2 m 'defined-only)
248                    (decode-char 'ideograph-daikanwa m)))
249           (when (or m
250                     (setq m (get-char-attribute char 'morohashi-daikanwa)))
251             (setq m-m (pop m))
252             (setq m-s (pop m))
253             (if (= m-s 0)
254                 (or (decode-char '=daikanwa-rev2 m-m 'defined-only)
255                     (decode-char 'ideograph-daikanwa m-m))
256               (when m
257                 (setq pat (list m-m m-s))
258                 (map-char-attribute (lambda (c v)
259                                       (if (equal pat v)
260                                           c))
261                                     'morohashi-daikanwa))))
262           char))))
263
264 (defun char-attributes-poly< (c1 c2 accessors testers defaulters)
265   (catch 'tag
266     (let (a1 a2 accessor tester dm)
267       (while (and accessors testers)
268         (setq accessor (car accessors)
269               tester (car testers)
270               dm (car defaulters))
271         (when (and accessor tester)
272           (setq a1 (funcall accessor c1)
273                 a2 (funcall accessor c2))
274           (cond ((null a1)
275                  (if a2
276                      (cond ((eq dm '<)
277                             (throw 'tag t))
278                            ((eq dm '>)
279                             (throw 'tag nil)))))
280                 ((null a2)
281                  (cond ((eq dm '<)
282                         (throw 'tag nil))
283                        ((eq dm '>)
284                         (throw 'tag t))))
285                 (t
286                  (cond ((funcall tester a1 a2)
287                         (throw 'tag t))
288                        ((funcall tester a2 a1)
289                         (throw 'tag nil))))))
290         (setq accessors (cdr accessors)
291               testers (cdr testers)
292               defaulters (cdr defaulters))))))
293
294 (defvar ideographic-radical nil)
295
296 (defun char-daikanwa-strokes (char &optional radical)
297   (unless radical
298     (setq radical ideographic-radical))
299   (let ((drc (char-representative-of-daikanwa char)))
300     (if (= (char-ideographic-radical drc radical)
301            (char-ideographic-radical char radical))
302         (setq char drc)))
303   (char-ideographic-strokes char radical '(daikanwa)))
304
305 ;;;###autoload
306 (defun char-daikanwa (char)
307   (or (encode-char char 'ideograph-daikanwa 'defined-only)
308       (encode-char char '=daikanwa-rev2 'defined-only)
309       (get-char-attribute char 'morohashi-daikanwa)
310       (let ((ret (char-feature char '=>daikanwa)))
311         (and ret
312              (if (or (get-char-attribute char '<-subsumptive)
313                      (get-char-attribute char '<-denotational))
314                  (list ret 0)
315                ret)))))
316
317 ;;;###autoload
318 (defun char-ucs (char)
319   (or (encode-char char '=ucs 'defined-only)
320       (char-feature char '=>ucs)))
321
322 (defun char-id (char)
323   (logand (char-int char) #x3FFFFFFF))
324
325 (defun ideograph-char< (a b &optional radical)
326   (let ((ideographic-radical (or radical
327                                  ideographic-radical)))
328     (char-attributes-poly<
329      a b
330      '(char-daikanwa-strokes char-daikanwa char-ucs char-id)
331      '(< morohashi-daikanwa< < <)
332      '(> > > >))))
333
334 (defun insert-ideograph-radical-char-data (radical)
335   (let ((chars
336          (sort (copy-list (aref ideograph-radical-chars-vector radical))
337                (lambda (a b)
338                  (ideograph-char< a b radical))))
339         attributes ; ccss
340         )
341     (dolist (name (char-attribute-list))
342       (unless (memq name char-db-ignored-attributes)
343         ;; (if (find-charset name)
344         ;;     (push name ccss)
345         (push name attributes)
346         ;; )
347         ))
348     (setq attributes (sort attributes #'char-attribute-name<)
349           ;; ccss (sort ccss #'char-attribute-name<)
350           )
351     (aset ideograph-radical-chars-vector radical chars)
352     (dolist (char chars)
353       (when ;;(or
354           (not (some (lambda (atr)
355                        (get-char-attribute char atr))
356                      char-db-ignored-attributes))
357         ;; (some (lambda (ccs)
358         ;;         (encode-char char ccs 'defined-only))
359         ;;       ccss)
360         ;;)
361         (insert-char-data char nil attributes ;ccss
362                           )))))
363
364 (defun write-ideograph-radical-char-data (radical file)
365   (if (file-directory-p file)
366       (let ((name (get-char-attribute (int-char (+ #x2EFF radical)) 'name)))
367         (if (string-match "KANGXI RADICAL " name)
368             (setq name (capitalize (substring name (match-end 0)))))
369         (setq name (mapconcat (lambda (char)
370                                 (if (eq char ? )
371                                     "-"
372                                   (char-to-string char))) name ""))
373         (setq file
374               (expand-file-name
375                (format "Ideograph-R%03d-%s.el" radical name)
376                file))))
377   (with-temp-buffer
378     (insert ";; -*- coding: utf-8-mcs -*-\n")
379     (insert-ideograph-radical-char-data radical)
380     (let ((coding-system-for-write 'utf-8-mcs))
381       (write-region (point-min)(point-max) file)
382       )))
383
384 (defun ideographic-structure= (char1 char2)
385   (if (char-ref-p char1)
386       (setq char1 (plist-get char1 :char)))
387   (if (char-ref-p char2)
388       (setq char2 (plist-get char2 :char)))
389   (let ((s1 (if (characterp char1)
390                 (get-char-attribute char1 'ideographic-structure)
391               (cdr (assq 'ideographic-structure char1))))
392         (s2 (if (characterp char2)
393                 (get-char-attribute char2 'ideographic-structure)
394               (cdr (assq 'ideographic-structure char2))))
395         e1 e2)
396     (if (or (null s1)(null s2))
397         (char-spec= char1 char2)
398       (catch 'tag
399         (while (and s1 s2)
400           (setq e1 (car s1)
401                 e2 (car s2))
402           (unless (ideographic-structure= e1 e2)
403             (throw 'tag nil))
404           (setq s1 (cdr s1)
405                 s2 (cdr s2)))
406         (and (null s1)(null s2))))))
407
408 ;;;###autoload
409 (defun ideographic-structure-find-char (structure)
410   (let (rest)
411     (map-char-attribute (lambda (char value)
412                           (setq rest structure)
413                           (catch 'tag
414                             (while (and rest value)
415                               (unless (ideographic-structure=
416                                        (car rest)(car value))
417                                 (throw 'tag nil))
418                               (setq rest (cdr rest)
419                                     value (cdr value)))
420                             (unless (or rest value)
421                               char)))
422                         'ideographic-structure)))
423
424 ;;;###autoload
425 (defun chise-string< (string1 string2 accessors)
426   (let ((len1 (length string1))
427         (len2 (length string2))
428         len
429         (i 0)
430         c1 c2
431         rest func
432         v1 v2)
433     (setq len (min len1 len2))
434     (catch 'tag
435       (while (< i len)
436         (setq c1 (aref string1 i)
437               c2 (aref string2 i))
438         (setq rest accessors)
439         (while (and rest
440                     (setq func (car rest))
441                     (setq v1 (funcall func c1)
442                           v2 (funcall func c2))
443                     (eq v1 v2))
444           (setq rest (cdr rest)))
445         (if v1
446             (if v2
447                 (cond ((< v1 v2)
448                        (throw 'tag t))
449                       ((> v1 v2)
450                        (throw 'tag nil)))
451               (throw 'tag nil))
452           (if v2
453               (throw 'tag t)))
454         (setq i (1+ i)))
455       (< len1 len2))))
456
457
458 (provide 'ideograph-util)
459
460 ;;; ideograph-util.el ends here