1 ;;; ideograph-util.el --- Ideographic Character Database utility
3 ;; Copyright (C) 1999,2000,2001,2002,2003,2004 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)
29 (defvar ideograph-radical-chars-vector
30 (make-vector 215 nil))
32 (defun char-ideographic-radical (char &optional radical)
35 (dolist (domain char-db-feature-domains)
36 (if (and (setq ret (char-feature
40 'ideographic-radical domain))))
45 (dolist (cell (get-char-attribute char 'ideographic-))
46 (if (and (setq ret (plist-get cell :radical))
50 (char-feature char 'ideographic-radical)
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)))
58 (put-char-attribute char 'ideographic-radical ret)
61 (defvar ideograph-radical-strokes-vector
63 [nil 1 1 1 1 1 1 2 2 2
82 9 9 9 9 8 9 9 10 10 10
83 10 10 10 10 10 11 11 11 11 11
85 11 12 12 12 12 13 13 13 13 14
89 (defun char-ideographic-strokes-from-domains (char domains &optional radical)
92 (dolist (domain domains)
93 (if (and (setq ret (or (char-feature
97 'ideographic-radical domain)))
99 char 'ideographic-radical)))
102 (setq ret (char-feature
106 'ideographic-strokes domain)))))
107 (throw 'tag ret))))))
110 (defun char-ideographic-strokes (char &optional radical preferred-domains)
113 (dolist (cell (get-char-attribute char 'ideographic-))
114 (if (and (setq ret (plist-get cell :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)
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)))
132 (- ts (aref ideograph-radical-strokes-vector r))))
135 (put-char-attribute char 'ideographic-strokes strokes)
139 (defun char-total-strokes-from-domains (char domains)
142 (dolist (domain domains)
143 (if (setq ret (get-char-attribute
147 'total-strokes domain))))
148 (throw 'tag ret))))))
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)))
157 (defun update-ideograph-radical-table ()
159 (let (ret radical script dest)
161 (cons 'ideographic-radical
164 (intern (format "%s@%s" 'ideographic-radical domain)))
165 char-db-feature-domains)))
167 (lambda (chr radical)
168 (dolist (char (append
170 (get-char-attribute chr '<-subsumptive))
174 (unless (get-char-attribute
175 pc 'ideographic-radical)
176 (setq dest (cons pc dest))))
179 (get-char-attribute chr '<-identical)
180 (get-char-attribute chr '->denotational)))
183 (char-ideographic-radical char radical))
184 (null (char-ideographic-radical char)))
185 (or (null (setq script
186 (get-char-attribute char 'script)))
187 (memq 'Ideograph script)))
190 (aref ideograph-radical-chars-vector
192 (char-ideographic-strokes char)
193 (aset ideograph-radical-chars-vector radical
200 (setq radical (plist-get cell :radical))
202 (or (null (setq script (get-char-attribute char 'script)))
203 (memq 'Ideograph script)))
206 (aref ideograph-radical-chars-vector radical)))
207 (char-ideographic-strokes char)
208 (aset ideograph-radical-chars-vector radical
212 (defun int-list< (a b)
213 (if (numberp (car a))
214 (if (numberp (car b))
215 (if (= (car a) (car b))
216 (int-list< (cdr a)(cdr b))
221 (defun morohashi-daikanwa< (a b)
226 (cond ((eq (car a) 'ho)
228 (int-list< (cdr a)(cdr b))
239 ;; (defun nil=-int< (a b)
240 ;; (cond ((null a) nil)
244 ;; (defun nil>-int< (a b)
245 ;; (cond ((null a) nil)
250 (defun char-representative-of-daikanwa (char &optional radical)
252 (setq radical ideographic-radical))
253 (if (or (encode-char char 'ideograph-daikanwa 'defined-only)
254 (encode-char char '=daikanwa-rev2 'defined-only))
256 (let ((m (char-feature char '=>daikanwa))
259 (or (and (integerp m)
260 (or (decode-char '=daikanwa-rev2 m 'defined-only)
261 (decode-char 'ideograph-daikanwa m)))
263 (setq m (get-char-attribute char 'morohashi-daikanwa)))
267 (or (decode-char '=daikanwa-rev2 m-m 'defined-only)
268 (decode-char 'ideograph-daikanwa m-m))
270 (setq pat (list m-m m-s))
271 (map-char-attribute (lambda (c v)
274 'morohashi-daikanwa))))
275 (when (setq scs (get-char-attribute char '->subsumptive))
281 (char-representative-of-daikanwa sc))
283 (eq (char-ideographic-radical ret radical)
286 (setq scs (cdr scs)))
290 (defun char-attributes-poly< (c1 c2 accessors testers defaulters)
292 (let (a1 a2 accessor tester dm)
293 (while (and accessors testers)
294 (setq accessor (car accessors)
297 (when (and accessor tester)
298 (setq a1 (funcall accessor c1)
299 a2 (funcall accessor c2))
312 (cond ((funcall tester a1 a2)
314 ((funcall tester a2 a1)
315 (throw 'tag nil))))))
316 (setq accessors (cdr accessors)
317 testers (cdr testers)
318 defaulters (cdr defaulters))))))
320 (defvar ideographic-radical nil)
322 (defun char-daikanwa-strokes (char &optional radical)
324 (setq radical ideographic-radical))
325 (let ((drc (char-representative-of-daikanwa char radical))
326 (r (char-ideographic-radical char radical)))
328 (= (char-ideographic-radical drc radical) r))
330 (char-ideographic-strokes char radical '(daikanwa)))
333 (defun char-daikanwa (char &optional radical)
334 (or (encode-char char 'ideograph-daikanwa 'defined-only)
335 (encode-char char '=daikanwa-rev2 'defined-only)
336 (get-char-attribute char 'morohashi-daikanwa)
337 (let ((ret (char-feature char '=>daikanwa)))
339 (if (or (get-char-attribute char '<-subsumptive)
340 (get-char-attribute char '<-denotational))
343 (let ((scs (get-char-attribute char '->subsumptive))
346 (setq radical ideographic-radical))
353 (char-representative-of-daikanwa sc))
355 (eq (char-ideographic-radical ret radical)
358 (setq scs (cdr scs))))
360 (char-daikanwa ret)))))
363 (defun char-ucs (char)
364 (or (encode-char char '=ucs 'defined-only)
365 (char-feature char '=>ucs)))
367 (defun char-id (char)
368 (logand (char-int char) #x3FFFFFFF))
370 (defun ideograph-char< (a b &optional radical)
371 (let ((ideographic-radical (or radical
372 ideographic-radical)))
373 (char-attributes-poly<
375 '(char-daikanwa-strokes char-daikanwa char-ucs char-id)
376 '(< morohashi-daikanwa< < <)
379 (defun insert-ideograph-radical-char-data (radical)
381 (sort (copy-list (aref ideograph-radical-chars-vector radical))
383 (ideograph-char< a b radical))))
386 (dolist (name (char-attribute-list))
387 (unless (memq name char-db-ignored-attributes)
388 ;; (if (find-charset name)
390 (push name attributes)
393 (setq attributes (sort attributes #'char-attribute-name<)
394 ;; ccss (sort ccss #'char-attribute-name<)
396 (aset ideograph-radical-chars-vector radical chars)
399 (not (some (lambda (atr)
400 (get-char-attribute char atr))
401 char-db-ignored-attributes))
402 ;; (some (lambda (ccs)
403 ;; (encode-char char ccs 'defined-only))
406 (insert-char-data char nil attributes ;ccss
409 (defun write-ideograph-radical-char-data (radical file)
410 (if (file-directory-p file)
411 (let ((name (get-char-attribute (int-char (+ #x2EFF radical)) 'name)))
412 (if (string-match "KANGXI RADICAL " name)
413 (setq name (capitalize (substring name (match-end 0)))))
414 (setq name (mapconcat (lambda (char)
417 (char-to-string char))) name ""))
420 (format "Ideograph-R%03d-%s.el" radical name)
423 (insert ";; -*- coding: utf-8-mcs -*-\n")
424 (insert-ideograph-radical-char-data radical)
425 (let ((coding-system-for-write 'utf-8-mcs))
426 (write-region (point-min)(point-max) file)
429 (defun ideographic-structure= (char1 char2)
430 (if (char-ref-p char1)
431 (setq char1 (plist-get char1 :char)))
432 (if (char-ref-p char2)
433 (setq char2 (plist-get char2 :char)))
434 (let ((s1 (if (characterp char1)
435 (get-char-attribute char1 'ideographic-structure)
436 (cdr (assq 'ideographic-structure char1))))
437 (s2 (if (characterp char2)
438 (get-char-attribute char2 'ideographic-structure)
439 (cdr (assq 'ideographic-structure char2))))
441 (if (or (null s1)(null s2))
442 (char-spec= char1 char2)
447 (unless (ideographic-structure= e1 e2)
451 (and (null s1)(null s2))))))
454 (defun ideographic-structure-find-char (structure)
456 (map-char-attribute (lambda (char value)
457 (setq rest structure)
459 (while (and rest value)
460 (unless (ideographic-structure=
461 (car rest)(car value))
463 (setq rest (cdr rest)
465 (unless (or rest value)
467 'ideographic-structure)))
470 (defun chise-string< (string1 string2 accessors)
471 (let ((len1 (length string1))
472 (len2 (length string2))
478 (setq len (min len1 len2))
481 (setq c1 (aref string1 i)
483 (setq rest accessors)
485 (setq func (car rest))
486 (setq v1 (funcall func c1)
487 v2 (funcall func c2))
489 (setq rest (cdr rest)))
503 (provide 'ideograph-util)
505 ;;; ideograph-util.el ends here