(update-ideograph-radical-table): Modify for `<-subsumptive'.
[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, 2003, 2004, 2005, 2007, 2008,
4 ;;   2009, 2010, 2012, 2014, 2015 MORIOKA Tomohiko.
5
6 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
7 ;; Keywords: CHISE, Chaon model, ISO/IEC 10646, Unicode, UCS-4, MULE.
8
9 ;; This file is part of XEmacs CHISE.
10
11 ;; XEmacs CHISE is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; XEmacs CHISE is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs CHISE; see the file COPYING.  If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (require 'chise-subr)
29 (require 'ideograph-subr)
30 (require 'char-db-util)
31
32
33 (defvar ideograph-radical-chars-vector
34   (make-vector 215 nil))
35
36
37 ;;;###autoload
38 (defun update-ideograph-radical-table ()
39   (interactive)
40   (let (ret rret radical script dest)
41     (dolist (feature
42              (cons 'ideographic-radical
43                    (progn
44                      (dolist (feature (char-attribute-list))
45                        (if (string-match "^ideographic-radical@[^@*]+$"
46                                          (symbol-name feature))
47                            (setq dest (cons feature dest))))
48                      dest)))
49       (map-char-attribute
50        (lambda (chr radical)
51          (dolist (char (append
52                         (if (setq ret
53                                   (get-char-attribute chr '<-subsumptive))
54                             (progn
55                               (setq dest nil)
56                               (setq ret (list ret))
57                               (dolist (pc ret)
58                                 (unless (eq (get-char-attribute
59                                              pc 'ideographic-radical)
60                                             radical)
61                                   (if (setq rret
62                                             (get-char-attribute
63                                              pc '<-subsumptive))
64                                       (setq ret (append ret (list rret)))
65                                     (setq dest (cons pc dest)))))
66                               dest)
67                           (list chr))
68                         (let ((rest (append
69                                      (get-char-attribute chr '<-identical)
70                                      (get-char-attribute chr '->denotational)))
71                               pc)
72                           (setq dest nil)
73                           (while rest
74                             (setq pc (car rest))
75                             (if (memq pc dest)
76                                 (setq rest (cdr rest))
77                               (setq dest (cons pc dest))
78                               (setq rest
79                                     (append (cdr rest)
80                                             (get-char-attribute
81                                              pc '<-identical)
82                                             (get-char-attribute
83                                              pc '->denotational)))))
84                           dest)))
85            (when (and radical
86                       (or (eq radical
87                               (or (get-char-attribute
88                                    char 'ideographic-radical)
89                                   (char-ideographic-radical char radical)))
90                           (null (char-ideographic-radical char)))
91                       (or (null (setq script
92                                       (get-char-attribute char 'script)))
93                           (memq 'Ideograph script)))
94              (unless (memq char
95                            (setq ret
96                                  (aref ideograph-radical-chars-vector
97                                        radical)))
98                (char-ideographic-strokes char)
99                (aset ideograph-radical-chars-vector radical
100                      (cons char ret)))))
101          nil)
102        feature))
103     (map-char-attribute
104      (lambda (char data)
105        (dolist (cell data)
106          (setq radical (plist-get cell :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      'ideographic-)))
117
118
119 (defun int-list< (a b)
120   (if (numberp (car a))
121       (if (numberp (car b))
122           (if (= (car a) (car b))
123               (int-list< (cdr a)(cdr b))
124             (< (car a) (car b)))
125         (if (= (car a) 0)
126             nil
127           (< (car a) 0)))
128     (if (numberp (car b))
129         (if (= (car b) 0)
130             t
131           (< 0 (car b)))
132       )))
133
134 (defun morohashi-daikanwa< (a b)
135   (if (integerp a)
136       (setq a (list a)))
137   (if (integerp b)
138       (setq b (list b)))
139   (cond ((eq (car-safe a) 'ho)
140          (if (eq (car-safe b) 'ho)
141              (int-list< (cdr-safe a)(cdr-safe b))
142            nil))
143         ((or (integerp a)
144              (integerp (car a)))
145          (if (eq (car b) 'ho)
146              t
147            (int-list< a b)))
148         (t
149          (if (eq (car-safe b) 'ho)
150              t
151            (int-list< a b)))))
152
153 ;; (defun nil=-int< (a b)
154 ;;   (cond ((null a) nil)
155 ;;         ((null b) nil)
156 ;;         (t (< a b))))
157
158 ;; (defun nil>-int< (a b)
159 ;;   (cond ((null a) nil)
160 ;;         ((null b) t)
161 ;;         (t (< a b))))
162
163 (defvar ideographic-radical nil)
164
165 ;;;###autoload
166 (defun char-representative-of-daikanwa (char &optional radical
167                                              ignore-default checked)
168   (unless radical
169     (setq radical ideographic-radical))
170   (if (or (null radical)
171           (eq (or (get-char-attribute char 'ideographic-radical)
172                   (char-ideographic-radical char radical t))
173               radical))
174       (let ((ret (or (encode-char char '=daikanwa@rev2 'defined-only)
175                      (encode-char char '=daikanwa/+p 'defined-only)
176                      (encode-char char '=daikanwa/+2p 'defined-only)
177                      (encode-char char '=daikanwa/ho 'defined-only)
178                      )))
179         (or (and ret char)
180             (if (setq ret (get-char-attribute char 'morohashi-daikanwa))
181                 (let ((m-m (car ret))
182                       (m-s (nth 1 ret))
183                       pat)
184                   (if (= m-s 0)
185                       (or (decode-char '=daikanwa@rev2 m-m 'defined-only)
186                           (decode-char '=daikanwa m-m))
187                     (or (cond ((eq m-m 'ho)
188                                (decode-char '=daikanwa/ho m-s))
189                               ((eq m-s 1)
190                                (decode-char '=daikanwa/+p m-m))
191                               ((eq m-s 2)
192                                (decode-char '=daikanwa/+2p m-m)))
193                         (progn
194                           (setq pat (list m-m m-s))
195                           (map-char-attribute (lambda (c v)
196                                                 (if (equal pat v)
197                                                     c))
198                                               'morohashi-daikanwa))))))
199             (and (setq ret (get-char-attribute char '=>daikanwa))
200                  (if (numberp ret)
201                      (or (decode-char '=daikanwa@rev2 ret 'defined-only)
202                          (decode-char '=daikanwa ret))
203                    (map-char-attribute (lambda (c v)
204                                          (if (equal ret v)
205                                              char))
206                                        'morohashi-daikanwa)))
207             (unless (memq char checked)
208               (catch 'tag
209                 (let ((rest
210                        (append (get-char-attribute char '->subsumptive)
211                                (get-char-attribute char '->denotational)))
212                       (i 0)
213                       sc)
214                   (setq checked (cons char checked))
215                   (while rest
216                     (setq sc (car rest))
217                     (if (setq ret (char-representative-of-daikanwa
218                                    sc radical t checked))
219                         (throw 'tag ret))
220                     (setq checked (cons sc checked)
221                           rest (cdr rest)
222                           i (1+ i)))
223                   (setq rest (get-char-attribute char '->identical))
224                   (while rest
225                     (setq sc (car rest))
226                     (when (setq ret (char-representative-of-daikanwa
227                                      sc radical t checked))
228                       (throw 'tag ret))
229                     (setq checked (cons sc checked)
230                           rest (cdr rest)))
231                   (setq rest
232                         (append (if (setq ret (get-char-attribute
233                                                char '<-subsumptive))
234                                     (list ret))
235                                 (if (setq ret (get-char-attribute
236                                                char '<-denotational))
237                                     (list ret))))
238                   (while rest
239                     (setq sc (car rest))
240                     (when (setq ret (char-representative-of-daikanwa
241                                      sc radical t checked))
242                       (throw 'tag ret))
243                     (setq checked (cons sc checked)
244                           rest (cdr rest))))))
245             (unless ignore-default
246               char)))))
247
248 (defun char-attributes-poly< (c1 c2 accessors testers defaulters)
249   (catch 'tag
250     (let (a1 a2 accessor tester dm)
251       (while (and accessors testers)
252         (setq accessor (car accessors)
253               tester (car testers)
254               dm (car defaulters))
255         (when (and accessor tester)
256           (setq a1 (funcall accessor c1)
257                 a2 (funcall accessor c2))
258           (cond ((null a1)
259                  (if a2
260                      (cond ((eq dm '<)
261                             (throw 'tag t))
262                            ((eq dm '>)
263                             (throw 'tag nil)))))
264                 ((null a2)
265                  (cond ((eq dm '<)
266                         (throw 'tag nil))
267                        ((eq dm '>)
268                         (throw 'tag t))))
269                 (t
270                  (cond ((funcall tester a1 a2)
271                         (throw 'tag t))
272                        ((funcall tester a2 a1)
273                         (throw 'tag nil))))))
274         (setq accessors (cdr accessors)
275               testers (cdr testers)
276               defaulters (cdr defaulters))))))
277
278 (defun char-daikanwa-radical (char &optional radical ignore-sisters)
279   (or (and (encode-char char '=daikanwa@rev2 'defined-only)
280            (or (get-char-attribute char 'ideographic-radical@daikanwa)
281                (get-char-attribute char 'ideographic-radical)))
282       (char-ideographic-radical char radical ignore-sisters)))
283
284 (defun char-daikanwa-strokes (char &optional radical)
285   (unless radical
286     (setq radical ideographic-radical))
287   (let ((drc (char-representative-of-daikanwa char radical))
288         (r (char-ideographic-radical char radical)))
289     (if (and drc
290              (or (null r)
291                  (= (char-ideographic-radical drc radical) r)))
292         (setq char drc)))
293   (char-ideographic-strokes char radical '(daikanwa)))
294
295 ;;;###autoload
296 (defun char-daikanwa (char &optional radical checked depth)
297   (unless radical
298     (setq radical ideographic-radical))
299   (if (or (null radical)
300           (eq (or (get-char-attribute char 'ideographic-radical)
301                   (char-daikanwa-radical char radical t))
302               radical))
303       (let ((ret (or (encode-char char '=daikanwa@rev2 'defined-only)
304                      ;; (encode-char char '=daikanwa 'defined-only)
305                      (get-char-attribute char 'morohashi-daikanwa))))
306         (unless ret
307           (cond
308            ((setq ret (encode-char char '=daikanwa/+p 'defined-only))
309             (setq ret (list ret 1)))
310            ((setq ret (encode-char char '=daikanwa/+2p 'defined-only))
311             (setq ret (list ret 2)))
312            ((setq ret (encode-char char '=daikanwa/ho 'defined-only))
313             (setq ret (list 'ho ret)))))
314         (or (if ret
315                 (if depth
316                     (if (integerp ret)
317                         (list ret depth)
318                       (append ret (list depth)))
319                   ret))
320             (and (setq ret (get-char-attribute char '=>daikanwa))
321                  (if (numberp ret)
322                      (list ret -10)
323                    (append ret '(-10))))
324             (unless (memq char checked)
325               (unless depth
326                 (setq depth 0))
327               (catch 'tag
328                 (let ((rest
329                        (append
330                         (get-char-attribute char '->subsumptive)
331                         (get-char-attribute char '->denotational)
332                         (get-char-attribute char '->denotational@component)
333                         ))
334                       (i 0)
335                       sc lnum)
336                   (setq checked (cons char checked))
337                   (while rest
338                     (setq sc (car rest))
339                     (if (setq ret (char-daikanwa sc radical checked
340                                                  (1- depth)))
341                         (throw 'tag ret))
342                     (setq checked (cons sc checked)
343                           rest (cdr rest)
344                           i (1+ i)))
345                   (setq rest (get-char-attribute char '->identical))
346                   (while rest
347                     (setq sc (car rest))
348                     (when (setq ret (char-daikanwa sc radical checked depth))
349                       (throw 'tag
350                              (if (numberp ret)
351                                  (list ret 0)
352                                (append ret (list i)))))
353                     (setq checked (cons sc checked)
354                           rest (cdr rest)))
355                   (setq rest
356                         (append
357                          (if (setq ret (get-char-attribute
358                                         char '<-subsumptive))
359                              (list ret))
360                          (if (setq ret (get-char-attribute
361                                         char '<-denotational))
362                              (list ret))
363                          (get-char-attribute char '<-denotational@component)
364                          ))
365                   (while rest
366                     (setq sc (car rest))
367                     (when (setq ret (char-daikanwa sc radical checked depth))
368                       (throw 'tag
369                              (if (numberp ret)
370                                  (list ret 0 i)
371                                (if (>= (setq lnum (car (last ret))) 0)
372                                    (append ret (list i))
373                                  (nconc (butlast ret)
374                                         (list 0 (- lnum) i))))))
375                     (setq checked (cons sc checked)
376                           rest (cdr rest))))))))))
377
378 (defun char-ideographic-strokes-diff (char &optional radical)
379   (if (or (get-char-attribute char '<-subsumptive)
380           (get-char-attribute char '<-denotational))
381       (let (s ds)
382         (when (and (setq s (char-ideographic-strokes char radical))
383                    (setq ds (char-daikanwa-strokes char radical)))
384           (abs (- s ds))))
385     0))
386
387 ;;;###autoload
388 (defun ideograph-char< (a b &optional radical)
389   (let ((ideographic-radical (or radical
390                                  ideographic-radical)))
391     (char-attributes-poly<
392      a b
393      '(char-daikanwa-strokes char-daikanwa char-ucs
394                              char-ideographic-strokes-diff char-id)
395      '(< morohashi-daikanwa< < < <)
396      '(> > > > >))))
397
398 (defun insert-ideograph-radical-char-data (radical)
399   (let ((chars
400          (sort (copy-list (aref ideograph-radical-chars-vector radical))
401                (lambda (a b)
402                  (ideograph-char< a b radical))))
403         attributes ; ccss
404         )
405     (dolist (name (char-attribute-list))
406       (unless (memq name char-db-ignored-attributes)
407         ;; (if (find-charset name)
408         ;;     (push name ccss)
409         (push name attributes)
410         ;; )
411         ))
412     (setq attributes (sort attributes #'char-attribute-name<)
413           ;; ccss (sort ccss #'char-attribute-name<)
414           )
415     (aset ideograph-radical-chars-vector radical chars)
416     (dolist (char chars)
417       (when ;;(or
418           (not (some (lambda (atr)
419                        (get-char-attribute char atr))
420                      char-db-ignored-attributes))
421         ;; (some (lambda (ccs)
422         ;;         (encode-char char ccs 'defined-only))
423         ;;       ccss)
424         ;;)
425         (insert-char-data char nil attributes ;ccss
426                           )))))
427
428 (defun write-ideograph-radical-char-data (radical file)
429   (if (file-directory-p file)
430       (let ((name (char-feature (decode-char 'ucs (+ #x2EFF radical))
431                                 'name)))
432         (if (string-match "KANGXI RADICAL " name)
433             (setq name (capitalize (substring name (match-end 0)))))
434         (setq name (mapconcat (lambda (char)
435                                 (if (eq char ? )
436                                     "-"
437                                   (char-to-string char))) name ""))
438         (setq file
439               (expand-file-name
440                (format "Ideograph-R%03d-%s.el" radical name)
441                file))))
442   (with-temp-buffer
443     (insert (format ";; -*- coding: %s -*-\n"
444                     char-db-file-coding-system))
445     (insert-ideograph-radical-char-data radical)
446     (let ((coding-system-for-write char-db-file-coding-system))
447       (write-region (point-min)(point-max) file))))
448
449 (defun ideographic-structure= (char1 char2)
450   (if (char-ref-p char1)
451       (setq char1 (plist-get char1 :char)))
452   (if (char-ref-p char2)
453       (setq char2 (plist-get char2 :char)))
454   (let ((s1 (if (characterp char1)
455                 (get-char-attribute char1 'ideographic-structure)
456               (cdr (assq 'ideographic-structure char1))))
457         (s2 (if (characterp char2)
458                 (get-char-attribute char2 'ideographic-structure)
459               (cdr (assq 'ideographic-structure char2))))
460         e1 e2)
461     (if (or (null s1)(null s2))
462         (char-spec= char1 char2)
463       (catch 'tag
464         (while (and s1 s2)
465           (setq e1 (car s1)
466                 e2 (car s2))
467           (unless (ideographic-structure= e1 e2)
468             (throw 'tag nil))
469           (setq s1 (cdr s1)
470                 s2 (cdr s2)))
471         (and (null s1)(null s2))))))
472
473 ;;;###autoload
474 (defun ideographic-structure-find-char (structure)
475   (let (rest)
476     (map-char-attribute (lambda (char value)
477                           (setq rest structure)
478                           (catch 'tag
479                             (while (and rest value)
480                               (unless (ideographic-structure=
481                                        (car rest)(car value))
482                                 (throw 'tag nil))
483                               (setq rest (cdr rest)
484                                     value (cdr value)))
485                             (unless (or rest value)
486                               char)))
487                         'ideographic-structure)))
488
489
490 (provide 'ideograph-util)
491
492 ;;; ideograph-util.el ends here