1 ;;; ideograph-util.el --- Ideographic Character Database utility
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008,
4 ;; 2009 MORIOKA Tomohiko.
6 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
7 ;; Keywords: CHISE, Chaon model, ISO/IEC 10646, Unicode, UCS-4, MULE.
9 ;; This file is part of XEmacs CHISE.
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.
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.
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.
28 (require 'char-db-util)
31 (defun expand-char-feature-name (feature domain)
33 (intern (format "%s@%s" feature domain))
37 (defun map-char-family (function char &optional ignore-sisters)
38 (let ((rest (list char))
42 (unless (memq (car rest) checked)
43 (if (setq ret (funcall function (car rest)))
45 (setq checked (cons (car rest) checked)
47 (get-char-attribute (car rest) '->subsumptive)
48 (get-char-attribute (car rest) '->denotational)
49 (get-char-attribute (car rest) '->identical)))
50 (unless ignore-sisters
51 (setq rest (append rest
52 (get-char-attribute (car rest) '<-subsumptive)
53 (get-char-attribute (car rest) '<-denotational)))))
54 (setq rest (cdr rest))))))
56 (defun get-char-feature-from-domains (char feature domains
63 (dolist (domain domains)
64 (if (and (or (null tester)
65 (equal (or (char-feature
66 ch (expand-char-feature-name
68 (char-feature ch tester))
70 (setq ret (or (char-feature
71 ch (expand-char-feature-name
73 (char-feature ch feature))))
78 (defvar ideograph-radical-chars-vector
79 (make-vector 215 nil))
81 (defun char-ideographic-radical (char &optional radical ignore-sisters)
84 (get-char-feature-from-domains
85 char 'ideographic-radical (cons nil char-db-feature-domains)
86 'ideographic-radical radical ignore-sisters)
87 (get-char-feature-from-domains
88 char 'ideographic-radical (cons nil char-db-feature-domains)
91 ;; (dolist (domain char-db-feature-domains)
92 ;; (if (and (setq ret (char-feature
96 ;; 'ideographic-radical domain))))
97 ;; (or (eq ret radical)
99 ;; (throw 'tag ret))))
101 (dolist (cell (get-char-attribute char 'ideographic-))
102 (if (and (setq ret (plist-get cell :radical))
106 (get-char-feature-from-domains
107 char 'ideographic-radical (cons nil char-db-feature-domains))
108 ;; (char-feature char 'ideographic-radical)
111 (or (get-char-attribute char 'daikanwa-radical)
112 (get-char-attribute char 'kangxi-radical)
113 (get-char-attribute char 'japanese-radical)
114 (get-char-attribute char 'korean-radical)))
116 (put-char-attribute char 'ideographic-radical ret)
119 (defvar ideograph-radical-strokes-vector
120 ;;0 1 2 3 4 5 6 7 8 9
121 [nil 1 1 1 1 1 1 2 2 2
140 9 9 9 9 8 9 9 10 10 10
141 10 10 10 10 10 11 11 11 11 11
143 11 12 12 12 12 13 13 13 13 14
147 (defun char-ideographic-strokes-from-domains (char domains &optional radical)
149 (get-char-feature-from-domains char 'ideographic-strokes domains
150 'ideographic-radical radical)
151 (get-char-feature-from-domains char 'ideographic-strokes domains)))
154 (defun char-ideographic-strokes (char &optional radical preferred-domains)
157 (dolist (cell (get-char-attribute char 'ideographic-))
158 (if (and (setq ret (plist-get cell :radical))
161 (throw 'tag (plist-get cell :strokes)))))
162 (char-ideographic-strokes-from-domains
163 char (append preferred-domains
165 char-db-feature-domains))
167 (get-char-attribute char 'daikanwa-strokes)
169 (or (get-char-attribute char 'kangxi-strokes)
170 (get-char-attribute char 'japanese-strokes)
171 (get-char-attribute char 'korean-strokes)
172 (let ((r (char-ideographic-radical char))
173 (ts (get-char-attribute char 'total-strokes)))
175 (- ts (aref ideograph-radical-strokes-vector r))))
178 (put-char-attribute char 'ideographic-strokes strokes)
182 (defun char-total-strokes-from-domains (char domains)
185 (dolist (domain domains)
186 (if (setq ret (char-feature
190 'total-strokes domain))))
191 (throw 'tag ret))))))
194 (defun char-total-strokes (char &optional preferred-domains)
195 (or (char-total-strokes-from-domains char preferred-domains)
196 (char-feature char 'total-strokes)
197 (char-total-strokes-from-domains char char-db-feature-domains)))
200 (defun update-ideograph-radical-table ()
202 (let (ret rret radical script dest)
204 (cons 'ideographic-radical
206 (dolist (feature (char-attribute-list))
207 (if (string-match "^ideographic-radical@[^@*]+$"
208 (symbol-name feature))
209 (setq dest (cons feature dest))))
212 (lambda (chr radical)
213 (dolist (char (append
215 (get-char-attribute chr '<-subsumptive))
219 (unless (eq (get-char-attribute
220 pc 'ideographic-radical)
225 (setq ret (append ret rret))
226 (setq dest (cons pc dest)))))
230 (get-char-attribute chr '<-identical)
231 (get-char-attribute chr '->denotational)))
237 (setq rest (cdr rest))
238 (setq dest (cons pc dest))
244 pc '->denotational)))))
248 (or (get-char-attribute
249 char 'ideographic-radical)
250 (char-ideographic-radical char radical)))
251 (null (char-ideographic-radical char)))
252 (or (null (setq script
253 (get-char-attribute char 'script)))
254 (memq 'Ideograph script)))
257 (aref ideograph-radical-chars-vector
259 (char-ideographic-strokes char)
260 (aset ideograph-radical-chars-vector radical
267 (setq radical (plist-get cell :radical))
269 (or (null (setq script (get-char-attribute char 'script)))
270 (memq 'Ideograph script)))
273 (aref ideograph-radical-chars-vector radical)))
274 (char-ideographic-strokes char)
275 (aset ideograph-radical-chars-vector radical
279 (defun int-list< (a b)
280 (if (numberp (car a))
281 (if (numberp (car b))
282 (if (= (car a) (car b))
283 (int-list< (cdr a)(cdr b))
288 (if (numberp (car b))
294 (defun morohashi-daikanwa< (a b)
299 (cond ((eq (car-safe a) 'ho)
300 (if (eq (car-safe b) 'ho)
301 (int-list< (cdr-safe a)(cdr-safe b))
309 (if (eq (car-safe b) 'ho)
313 ;; (defun nil=-int< (a b)
314 ;; (cond ((null a) nil)
318 ;; (defun nil>-int< (a b)
319 ;; (cond ((null a) nil)
323 (defvar ideographic-radical nil)
326 (defun char-representative-of-daikanwa (char &optional radical
327 ignore-default checked)
329 (setq radical ideographic-radical))
330 (if (or (null radical)
331 (eq (or (get-char-attribute char 'ideographic-radical)
332 (char-ideographic-radical char radical t))
334 (let ((ret (or (encode-char char 'ideograph-daikanwa 'defined-only)
335 (encode-char char '=daikanwa-rev2 'defined-only))))
337 (if (setq ret (get-char-attribute char 'morohashi-daikanwa))
338 (let ((m-m (car ret))
342 (or (decode-char '=daikanwa-rev2 m-m 'defined-only)
343 (decode-char 'ideograph-daikanwa m-m))
344 (setq pat (list m-m m-s))
345 (map-char-attribute (lambda (c v)
348 'morohashi-daikanwa))))
349 (and (setq ret (get-char-attribute char '=>daikanwa))
351 (or (decode-char '=daikanwa-rev2 ret 'defined-only)
352 (decode-char 'ideograph-daikanwa ret))
353 (map-char-attribute (lambda (c v)
356 'morohashi-daikanwa)))
357 (unless (memq char checked)
360 (append (get-char-attribute char '->subsumptive)
361 (get-char-attribute char '->denotational)))
364 (setq checked (cons char checked))
367 (if (setq ret (char-representative-of-daikanwa
368 sc radical t checked))
370 (setq checked (cons sc checked)
373 (setq rest (get-char-attribute char '->identical))
376 (when (setq ret (char-representative-of-daikanwa
377 sc radical t checked))
379 (setq checked (cons sc checked)
382 (append (get-char-attribute char '<-subsumptive)
383 (get-char-attribute char '<-denotational)))
386 (when (setq ret (char-representative-of-daikanwa
387 sc radical t checked))
389 (setq checked (cons sc checked)
391 (unless ignore-default
394 (defun char-attributes-poly< (c1 c2 accessors testers defaulters)
396 (let (a1 a2 accessor tester dm)
397 (while (and accessors testers)
398 (setq accessor (car accessors)
401 (when (and accessor tester)
402 (setq a1 (funcall accessor c1)
403 a2 (funcall accessor c2))
416 (cond ((funcall tester a1 a2)
418 ((funcall tester a2 a1)
419 (throw 'tag nil))))))
420 (setq accessors (cdr accessors)
421 testers (cdr testers)
422 defaulters (cdr defaulters))))))
424 (defun char-daikanwa-strokes (char &optional radical)
426 (setq radical ideographic-radical))
427 (let ((drc (char-representative-of-daikanwa char radical))
428 (r (char-ideographic-radical char radical)))
430 (= (char-ideographic-radical drc radical) r))
432 (char-ideographic-strokes char radical '(daikanwa)))
435 (defun char-daikanwa (char &optional radical checked depth)
437 (setq radical ideographic-radical))
438 (if (or (null radical)
439 (eq (or (get-char-attribute char 'ideographic-radical)
440 (char-ideographic-radical char radical t))
442 (let ((ret (or (encode-char char 'ideograph-daikanwa 'defined-only)
443 (encode-char char '=daikanwa-rev2 'defined-only)
444 (get-char-attribute char 'morohashi-daikanwa))))
449 (append ret (list depth)))
451 (and (setq ret (get-char-attribute char '=>daikanwa))
455 (unless (memq char checked)
460 (append (get-char-attribute char '->subsumptive)
461 (get-char-attribute char '->denotational)))
464 (setq checked (cons char checked))
467 (if (setq ret (char-daikanwa sc radical checked
470 (setq checked (cons sc checked)
473 (setq rest (get-char-attribute char '->identical))
476 (when (setq ret (char-daikanwa sc radical checked depth))
480 (append ret (list i)))))
481 (setq checked (cons sc checked)
484 (append (get-char-attribute char '<-subsumptive)
485 (get-char-attribute char '<-denotational)))
488 (when (setq ret (char-daikanwa sc radical checked depth))
492 (if (>= (setq lnum (car (last ret))) 0)
493 (append ret (list i))
495 (list 0 (- lnum) i))))))
496 (setq checked (cons sc checked)
497 rest (cdr rest))))))))))
500 (defun char-ucs (char)
501 (or (encode-char char '=ucs 'defined-only)
502 (char-feature char '=>ucs)))
505 (defun char-id (char)
506 (logand (char-int char) #x3FFFFFFF))
508 (defun char-ideographic-strokes-diff (char &optional radical)
509 (if (or (get-char-attribute char '<-subsumptive)
510 (get-char-attribute char '<-denotational))
512 (when (and (setq s (char-ideographic-strokes char radical))
513 (setq ds (char-daikanwa-strokes char radical)))
518 (defun ideograph-char< (a b &optional radical)
519 (let ((ideographic-radical (or radical
520 ideographic-radical)))
521 (char-attributes-poly<
523 '(char-daikanwa-strokes char-daikanwa char-ucs
524 char-ideographic-strokes-diff char-id)
525 '(< morohashi-daikanwa< < < <)
528 (defun insert-ideograph-radical-char-data (radical)
530 (sort (copy-list (aref ideograph-radical-chars-vector radical))
532 (ideograph-char< a b radical))))
535 (dolist (name (char-attribute-list))
536 (unless (memq name char-db-ignored-attributes)
537 ;; (if (find-charset name)
539 (push name attributes)
542 (setq attributes (sort attributes #'char-attribute-name<)
543 ;; ccss (sort ccss #'char-attribute-name<)
545 (aset ideograph-radical-chars-vector radical chars)
548 (not (some (lambda (atr)
549 (get-char-attribute char atr))
550 char-db-ignored-attributes))
551 ;; (some (lambda (ccs)
552 ;; (encode-char char ccs 'defined-only))
555 (insert-char-data char nil attributes ;ccss
558 (defun write-ideograph-radical-char-data (radical file)
559 (if (file-directory-p file)
560 (let ((name (char-feature (decode-char 'ucs (+ #x2EFF radical))
562 (if (string-match "KANGXI RADICAL " name)
563 (setq name (capitalize (substring name (match-end 0)))))
564 (setq name (mapconcat (lambda (char)
567 (char-to-string char))) name ""))
570 (format "Ideograph-R%03d-%s.el" radical name)
573 (insert (format ";; -*- coding: %s -*-\n"
574 char-db-file-coding-system))
575 (insert-ideograph-radical-char-data radical)
576 (let ((coding-system-for-write char-db-file-coding-system))
577 (write-region (point-min)(point-max) file))))
579 (defun ideographic-structure= (char1 char2)
580 (if (char-ref-p char1)
581 (setq char1 (plist-get char1 :char)))
582 (if (char-ref-p char2)
583 (setq char2 (plist-get char2 :char)))
584 (let ((s1 (if (characterp char1)
585 (get-char-attribute char1 'ideographic-structure)
586 (cdr (assq 'ideographic-structure char1))))
587 (s2 (if (characterp char2)
588 (get-char-attribute char2 'ideographic-structure)
589 (cdr (assq 'ideographic-structure char2))))
591 (if (or (null s1)(null s2))
592 (char-spec= char1 char2)
597 (unless (ideographic-structure= e1 e2)
601 (and (null s1)(null s2))))))
604 (defun ideographic-structure-find-char (structure)
606 (map-char-attribute (lambda (char value)
607 (setq rest structure)
609 (while (and rest value)
610 (unless (ideographic-structure=
611 (car rest)(car value))
613 (setq rest (cdr rest)
615 (unless (or rest value)
617 'ideographic-structure)))
620 (defun chise-string< (string1 string2 accessors)
621 (let ((len1 (length string1))
622 (len2 (length string2))
628 (setq len (min len1 len2))
631 (setq c1 (aref string1 i)
633 (setq rest accessors)
635 (setq func (car rest))
636 (setq v1 (funcall func c1)
637 v2 (funcall func c2))
639 (setq rest (cdr rest)))
653 (provide 'ideograph-util)
655 ;;; ideograph-util.el ends here