1 ;;; ideograph-util.el --- Ideographic Character Database utility
3 ;; Copyright (C) 1999,2000,2001,2002,2003,2004,2005 MORIOKA Tomohiko.
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: CHISE, Chaon model, ISO/IEC 10646, Unicode, UCS-4, MULE.
8 ;; This file is part of XEmacs CHISE.
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.
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.
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.
27 (require 'char-db-util)
30 (defun expand-char-feature-name (feature domain)
32 (intern (format "%s@%s" feature domain))
36 (defun map-char-family (function char &optional ignore-sisters)
37 (let ((rest (list char))
41 (unless (memq (car rest) checked)
42 (if (setq ret (funcall function (car rest)))
44 (setq checked (cons (car rest) checked)
46 (get-char-attribute (car rest) '->subsumptive)
47 (get-char-attribute (car rest) '->denotational)
48 (get-char-attribute (car rest) '->identical)))
49 (unless ignore-sisters
50 (setq rest (append rest
51 (get-char-attribute (car rest) '<-subsumptive)
52 (get-char-attribute (car rest) '<-denotational)))))
53 (setq rest (cdr rest))))))
55 (defun get-char-feature-from-domains (char feature domains
62 (dolist (domain domains)
63 (if (and (or (null tester)
64 (equal (or (char-feature
65 ch (expand-char-feature-name
67 (char-feature ch tester))
69 (setq ret (or (char-feature
70 ch (expand-char-feature-name
72 (char-feature ch feature))))
77 (defvar ideograph-radical-chars-vector
78 (make-vector 215 nil))
80 (defun char-ideographic-radical (char &optional radical ignore-sisters)
83 (get-char-feature-from-domains
84 char 'ideographic-radical (cons nil char-db-feature-domains)
85 'ideographic-radical radical ignore-sisters)
86 (get-char-feature-from-domains
87 char 'ideographic-radical (cons nil char-db-feature-domains)
90 ;; (dolist (domain char-db-feature-domains)
91 ;; (if (and (setq ret (char-feature
95 ;; 'ideographic-radical domain))))
96 ;; (or (eq ret radical)
98 ;; (throw 'tag ret))))
100 (dolist (cell (get-char-attribute char 'ideographic-))
101 (if (and (setq ret (plist-get cell :radical))
105 (get-char-feature-from-domains
106 char 'ideographic-radical (cons nil char-db-feature-domains))
107 ;; (char-feature char 'ideographic-radical)
110 (or (get-char-attribute char 'daikanwa-radical)
111 (get-char-attribute char 'kangxi-radical)
112 (get-char-attribute char 'japanese-radical)
113 (get-char-attribute char 'korean-radical)))
115 (put-char-attribute char 'ideographic-radical ret)
118 (defvar ideograph-radical-strokes-vector
119 ;;0 1 2 3 4 5 6 7 8 9
120 [nil 1 1 1 1 1 1 2 2 2
139 9 9 9 9 8 9 9 10 10 10
140 10 10 10 10 10 11 11 11 11 11
142 11 12 12 12 12 13 13 13 13 14
146 (defun char-ideographic-strokes-from-domains (char domains &optional radical)
148 (get-char-feature-from-domains char 'ideographic-strokes domains
149 'ideographic-radical radical)
150 (get-char-feature-from-domains char 'ideographic-strokes domains)))
153 (defun char-ideographic-strokes (char &optional radical preferred-domains)
156 (dolist (cell (get-char-attribute char 'ideographic-))
157 (if (and (setq ret (plist-get cell :radical))
160 (throw 'tag (plist-get cell :strokes)))))
161 (char-ideographic-strokes-from-domains
162 char (append preferred-domains
164 char-db-feature-domains))
166 (get-char-attribute char 'daikanwa-strokes)
168 (or (get-char-attribute char 'kangxi-strokes)
169 (get-char-attribute char 'japanese-strokes)
170 (get-char-attribute char 'korean-strokes)
171 (let ((r (char-ideographic-radical char))
172 (ts (get-char-attribute char 'total-strokes)))
174 (- ts (aref ideograph-radical-strokes-vector r))))
177 (put-char-attribute char 'ideographic-strokes strokes)
181 (defun char-total-strokes-from-domains (char domains)
184 (dolist (domain domains)
185 (if (setq ret (char-feature
189 'total-strokes domain))))
190 (throw 'tag ret))))))
193 (defun char-total-strokes (char &optional preferred-domains)
194 (or (char-total-strokes-from-domains char preferred-domains)
195 (char-feature char 'total-strokes)
196 (char-total-strokes-from-domains char char-db-feature-domains)))
199 (defun update-ideograph-radical-table ()
201 (let (ret rret radical script dest)
203 (cons 'ideographic-radical
205 (dolist (feature (char-attribute-list))
206 (if (string-match "^ideographic-radical@[^@*]+$"
207 (symbol-name feature))
208 (setq dest (cons feature dest))))
211 (lambda (chr radical)
212 (dolist (char (append
214 (get-char-attribute chr '<-subsumptive))
218 (unless (eq (get-char-attribute
219 pc 'ideographic-radical)
224 (setq ret (append ret rret))
225 (setq dest (cons pc dest)))))
229 (get-char-attribute chr '<-identical)
230 (get-char-attribute chr '->denotational)))
236 (setq rest (cdr rest))
237 (setq dest (cons pc dest))
243 pc '->denotational)))))
247 (or (get-char-attribute
248 char 'ideographic-radical)
249 (char-ideographic-radical char radical)))
250 (null (char-ideographic-radical char)))
251 (or (null (setq script
252 (get-char-attribute char 'script)))
253 (memq 'Ideograph script)))
256 (aref ideograph-radical-chars-vector
258 (char-ideographic-strokes char)
259 (aset ideograph-radical-chars-vector radical
266 (setq radical (plist-get cell :radical))
268 (or (null (setq script (get-char-attribute char 'script)))
269 (memq 'Ideograph script)))
272 (aref ideograph-radical-chars-vector radical)))
273 (char-ideographic-strokes char)
274 (aset ideograph-radical-chars-vector radical
278 (defun int-list< (a b)
279 (if (numberp (car a))
280 (if (numberp (car b))
281 (if (= (car a) (car b))
282 (int-list< (cdr a)(cdr b))
287 (defun morohashi-daikanwa< (a b)
292 (cond ((eq (car a) 'ho)
294 (int-list< (cdr a)(cdr b))
305 ;; (defun nil=-int< (a b)
306 ;; (cond ((null a) nil)
310 ;; (defun nil>-int< (a b)
311 ;; (cond ((null a) nil)
315 (defvar ideographic-radical nil)
318 (defun char-representative-of-daikanwa (char &optional radical
319 ignore-default checked)
321 (setq radical ideographic-radical))
322 (if (or (null radical)
323 (eq (or (get-char-attribute char 'ideographic-radical)
324 (char-ideographic-radical char radical t))
326 (let ((ret (or (encode-char char 'ideograph-daikanwa 'defined-only)
327 (encode-char char '=daikanwa-rev2 'defined-only))))
329 (if (setq ret (get-char-attribute char 'morohashi-daikanwa))
330 (let ((m-m (car ret))
334 (or (decode-char '=daikanwa-rev2 m-m 'defined-only)
335 (decode-char 'ideograph-daikanwa m-m))
336 (setq pat (list m-m m-s))
337 (map-char-attribute (lambda (c v)
340 'morohashi-daikanwa))))
341 (and (setq ret (get-char-attribute char '=>daikanwa))
343 (or (decode-char '=daikanwa-rev2 ret 'defined-only)
344 (decode-char 'ideograph-daikanwa ret))
345 (map-char-attribute (lambda (c v)
348 'morohashi-daikanwa)))
349 (unless (memq char checked)
352 (append (get-char-attribute char '->subsumptive)
353 (get-char-attribute char '->denotational)))
356 (setq checked (cons char checked))
359 (if (setq ret (char-representative-of-daikanwa
360 sc radical t checked))
362 (setq checked (cons sc checked)
365 (setq rest (get-char-attribute char '->identical))
368 (when (setq ret (char-representative-of-daikanwa
369 sc radical t checked))
371 (setq checked (cons sc checked)
374 (append (get-char-attribute char '<-subsumptive)
375 (get-char-attribute char '<-denotational)))
378 (when (setq ret (char-representative-of-daikanwa
379 sc radical t checked))
381 (setq checked (cons sc checked)
383 (unless ignore-default
386 (defun char-attributes-poly< (c1 c2 accessors testers defaulters)
388 (let (a1 a2 accessor tester dm)
389 (while (and accessors testers)
390 (setq accessor (car accessors)
393 (when (and accessor tester)
394 (setq a1 (funcall accessor c1)
395 a2 (funcall accessor c2))
408 (cond ((funcall tester a1 a2)
410 ((funcall tester a2 a1)
411 (throw 'tag nil))))))
412 (setq accessors (cdr accessors)
413 testers (cdr testers)
414 defaulters (cdr defaulters))))))
416 (defun char-daikanwa-strokes (char &optional radical)
418 (setq radical ideographic-radical))
419 (let ((drc (char-representative-of-daikanwa char radical))
420 (r (char-ideographic-radical char radical)))
422 (= (char-ideographic-radical drc radical) r))
424 (char-ideographic-strokes char radical '(daikanwa)))
427 (defun char-daikanwa (char &optional radical checked)
429 (setq radical ideographic-radical))
430 (if (or (null radical)
431 (eq (or (get-char-attribute char 'ideographic-radical)
432 (char-ideographic-radical char radical t))
434 (let ((ret (or (encode-char char 'ideograph-daikanwa 'defined-only)
435 (encode-char char '=daikanwa-rev2 'defined-only)
436 (get-char-attribute char 'morohashi-daikanwa))))
438 (and (setq ret (get-char-attribute char '=>daikanwa))
442 (unless (memq char checked)
445 (append (get-char-attribute char '->subsumptive)
446 (get-char-attribute char '->denotational)))
449 (setq checked (cons char checked))
452 (if (setq ret (char-daikanwa sc radical checked))
454 (setq checked (cons sc checked)
457 (setq rest (get-char-attribute char '->identical))
460 (when (setq ret (char-daikanwa sc radical checked))
464 (append ret (list i)))))
465 (setq checked (cons sc checked)
468 (append (get-char-attribute char '<-subsumptive)
469 (get-char-attribute char '<-denotational)))
472 (when (setq ret (char-daikanwa sc radical checked))
476 (append ret (list i)))))
477 (setq checked (cons sc checked)
478 rest (cdr rest))))))))))
481 (defun char-ucs (char)
482 (or (encode-char char '=ucs 'defined-only)
483 (char-feature char '=>ucs)))
485 (defun char-id (char)
486 (logand (char-int char) #x3FFFFFFF))
488 (defun ideograph-char< (a b &optional radical)
489 (let ((ideographic-radical (or radical
490 ideographic-radical)))
491 (char-attributes-poly<
493 '(char-daikanwa-strokes char-daikanwa char-ucs char-id)
494 '(< morohashi-daikanwa< < <)
497 (defun insert-ideograph-radical-char-data (radical)
499 (sort (copy-list (aref ideograph-radical-chars-vector radical))
501 (ideograph-char< a b radical))))
504 (dolist (name (char-attribute-list))
505 (unless (memq name char-db-ignored-attributes)
506 ;; (if (find-charset name)
508 (push name attributes)
511 (setq attributes (sort attributes #'char-attribute-name<)
512 ;; ccss (sort ccss #'char-attribute-name<)
514 (aset ideograph-radical-chars-vector radical chars)
517 (not (some (lambda (atr)
518 (get-char-attribute char atr))
519 char-db-ignored-attributes))
520 ;; (some (lambda (ccs)
521 ;; (encode-char char ccs 'defined-only))
524 (insert-char-data char nil attributes ;ccss
527 (defun write-ideograph-radical-char-data (radical file)
528 (if (file-directory-p file)
529 (let ((name (char-feature (decode-char 'ucs (+ #x2EFF radical))
531 (if (string-match "KANGXI RADICAL " name)
532 (setq name (capitalize (substring name (match-end 0)))))
533 (setq name (mapconcat (lambda (char)
536 (char-to-string char))) name ""))
539 (format "Ideograph-R%03d-%s.el" radical name)
542 (insert (format ";; -*- coding: %s -*-\n"
543 char-db-file-coding-system))
544 (insert-ideograph-radical-char-data radical)
545 (let ((coding-system-for-write char-db-file-coding-system))
546 (write-region (point-min)(point-max) file))))
548 (defun ideographic-structure= (char1 char2)
549 (if (char-ref-p char1)
550 (setq char1 (plist-get char1 :char)))
551 (if (char-ref-p char2)
552 (setq char2 (plist-get char2 :char)))
553 (let ((s1 (if (characterp char1)
554 (get-char-attribute char1 'ideographic-structure)
555 (cdr (assq 'ideographic-structure char1))))
556 (s2 (if (characterp char2)
557 (get-char-attribute char2 'ideographic-structure)
558 (cdr (assq 'ideographic-structure char2))))
560 (if (or (null s1)(null s2))
561 (char-spec= char1 char2)
566 (unless (ideographic-structure= e1 e2)
570 (and (null s1)(null s2))))))
573 (defun ideographic-structure-find-char (structure)
575 (map-char-attribute (lambda (char value)
576 (setq rest structure)
578 (while (and rest value)
579 (unless (ideographic-structure=
580 (car rest)(car value))
582 (setq rest (cdr rest)
584 (unless (or rest value)
586 'ideographic-structure)))
589 (defun chise-string< (string1 string2 accessors)
590 (let ((len1 (length string1))
591 (len2 (length string2))
597 (setq len (min len1 len2))
600 (setq c1 (aref string1 i)
602 (setq rest accessors)
604 (setq func (car rest))
605 (setq v1 (funcall func c1)
606 v2 (funcall func c2))
608 (setq rest (cdr rest)))
622 (provide 'ideograph-util)
624 ;;; ideograph-util.el ends here