1 ;;; ideograph-util.el --- Ideographic Character Database utility
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008,
4 ;; 2009, 2010 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.
29 (require 'ideograph-subr)
30 (require 'char-db-util)
33 (defvar ideograph-radical-chars-vector
34 (make-vector 215 nil))
38 (defun update-ideograph-radical-table ()
40 (let (ret rret radical script dest)
42 (cons 'ideographic-radical
44 (dolist (feature (char-attribute-list))
45 (if (string-match "^ideographic-radical@[^@*]+$"
46 (symbol-name feature))
47 (setq dest (cons feature dest))))
53 (get-char-attribute chr '<-subsumptive))
57 (unless (eq (get-char-attribute
58 pc 'ideographic-radical)
63 (setq ret (append ret rret))
64 (setq dest (cons pc dest)))))
68 (get-char-attribute chr '<-identical)
69 (get-char-attribute chr '->denotational)))
75 (setq rest (cdr rest))
76 (setq dest (cons pc dest))
82 pc '->denotational)))))
86 (or (get-char-attribute
87 char 'ideographic-radical)
88 (char-ideographic-radical char radical)))
89 (null (char-ideographic-radical char)))
90 (or (null (setq script
91 (get-char-attribute char 'script)))
92 (memq 'Ideograph script)))
95 (aref ideograph-radical-chars-vector
97 (char-ideographic-strokes char)
98 (aset ideograph-radical-chars-vector radical
105 (setq radical (plist-get cell :radical))
107 (or (null (setq script (get-char-attribute char 'script)))
108 (memq 'Ideograph script)))
111 (aref ideograph-radical-chars-vector radical)))
112 (char-ideographic-strokes char)
113 (aset ideograph-radical-chars-vector radical
118 (defun int-list< (a b)
119 (if (numberp (car a))
120 (if (numberp (car b))
121 (if (= (car a) (car b))
122 (int-list< (cdr a)(cdr b))
127 (if (numberp (car b))
133 (defun morohashi-daikanwa< (a b)
138 (cond ((eq (car-safe a) 'ho)
139 (if (eq (car-safe b) 'ho)
140 (int-list< (cdr-safe a)(cdr-safe b))
148 (if (eq (car-safe b) 'ho)
152 ;; (defun nil=-int< (a b)
153 ;; (cond ((null a) nil)
157 ;; (defun nil>-int< (a b)
158 ;; (cond ((null a) nil)
162 (defvar ideographic-radical nil)
165 (defun char-representative-of-daikanwa (char &optional radical
166 ignore-default checked)
168 (setq radical ideographic-radical))
169 (if (or (null radical)
170 (eq (or (get-char-attribute char 'ideographic-radical)
171 (char-ideographic-radical char radical t))
173 (let ((ret (or (encode-char char 'ideograph-daikanwa 'defined-only)
174 (encode-char char '=daikanwa-rev2 'defined-only))))
176 (if (setq ret (get-char-attribute char 'morohashi-daikanwa))
177 (let ((m-m (car ret))
181 (or (decode-char '=daikanwa-rev2 m-m 'defined-only)
182 (decode-char 'ideograph-daikanwa m-m))
183 (setq pat (list m-m m-s))
184 (map-char-attribute (lambda (c v)
187 'morohashi-daikanwa))))
188 (and (setq ret (get-char-attribute char '=>daikanwa))
190 (or (decode-char '=daikanwa-rev2 ret 'defined-only)
191 (decode-char 'ideograph-daikanwa ret))
192 (map-char-attribute (lambda (c v)
195 'morohashi-daikanwa)))
196 (unless (memq char checked)
199 (append (get-char-attribute char '->subsumptive)
200 (get-char-attribute char '->denotational)))
203 (setq checked (cons char checked))
206 (if (setq ret (char-representative-of-daikanwa
207 sc radical t checked))
209 (setq checked (cons sc checked)
212 (setq rest (get-char-attribute char '->identical))
215 (when (setq ret (char-representative-of-daikanwa
216 sc radical t checked))
218 (setq checked (cons sc checked)
221 (append (get-char-attribute char '<-subsumptive)
222 (get-char-attribute char '<-denotational)))
225 (when (setq ret (char-representative-of-daikanwa
226 sc radical t checked))
228 (setq checked (cons sc checked)
230 (unless ignore-default
233 (defun char-attributes-poly< (c1 c2 accessors testers defaulters)
235 (let (a1 a2 accessor tester dm)
236 (while (and accessors testers)
237 (setq accessor (car accessors)
240 (when (and accessor tester)
241 (setq a1 (funcall accessor c1)
242 a2 (funcall accessor c2))
255 (cond ((funcall tester a1 a2)
257 ((funcall tester a2 a1)
258 (throw 'tag nil))))))
259 (setq accessors (cdr accessors)
260 testers (cdr testers)
261 defaulters (cdr defaulters))))))
263 (defun char-daikanwa-strokes (char &optional radical)
265 (setq radical ideographic-radical))
266 (let ((drc (char-representative-of-daikanwa char radical))
267 (r (char-ideographic-radical char radical)))
270 (= (char-ideographic-radical drc radical) r)))
272 (char-ideographic-strokes char radical '(daikanwa)))
275 (defun char-daikanwa (char &optional radical checked depth)
277 (setq radical ideographic-radical))
278 (if (or (null radical)
279 (eq (or (get-char-attribute char 'ideographic-radical)
280 (char-ideographic-radical char radical t))
282 (let ((ret (or (encode-char char 'ideograph-daikanwa 'defined-only)
283 (encode-char char '=daikanwa-rev2 'defined-only)
284 (get-char-attribute char 'morohashi-daikanwa))))
289 (append ret (list depth)))
291 (and (setq ret (get-char-attribute char '=>daikanwa))
294 (append ret '(-10))))
295 (unless (memq char checked)
300 (append (get-char-attribute char '->subsumptive)
301 (get-char-attribute char '->denotational)))
304 (setq checked (cons char checked))
307 (if (setq ret (char-daikanwa sc radical checked
310 (setq checked (cons sc checked)
313 (setq rest (get-char-attribute char '->identical))
316 (when (setq ret (char-daikanwa sc radical checked depth))
320 (append ret (list i)))))
321 (setq checked (cons sc checked)
324 (append (get-char-attribute char '<-subsumptive)
325 (get-char-attribute char '<-denotational)))
328 (when (setq ret (char-daikanwa sc radical checked depth))
332 (if (>= (setq lnum (car (last ret))) 0)
333 (append ret (list i))
335 (list 0 (- lnum) i))))))
336 (setq checked (cons sc checked)
337 rest (cdr rest))))))))))
339 (defun char-ideographic-strokes-diff (char &optional radical)
340 (if (or (get-char-attribute char '<-subsumptive)
341 (get-char-attribute char '<-denotational))
343 (when (and (setq s (char-ideographic-strokes char radical))
344 (setq ds (char-daikanwa-strokes char radical)))
349 (defun ideograph-char< (a b &optional radical)
350 (let ((ideographic-radical (or radical
351 ideographic-radical)))
352 (char-attributes-poly<
354 '(char-daikanwa-strokes char-daikanwa char-ucs
355 char-ideographic-strokes-diff char-id)
356 '(< morohashi-daikanwa< < < <)
359 (defun insert-ideograph-radical-char-data (radical)
361 (sort (copy-list (aref ideograph-radical-chars-vector radical))
363 (ideograph-char< a b radical))))
366 (dolist (name (char-attribute-list))
367 (unless (memq name char-db-ignored-attributes)
368 ;; (if (find-charset name)
370 (push name attributes)
373 (setq attributes (sort attributes #'char-attribute-name<)
374 ;; ccss (sort ccss #'char-attribute-name<)
376 (aset ideograph-radical-chars-vector radical chars)
379 (not (some (lambda (atr)
380 (get-char-attribute char atr))
381 char-db-ignored-attributes))
382 ;; (some (lambda (ccs)
383 ;; (encode-char char ccs 'defined-only))
386 (insert-char-data char nil attributes ;ccss
389 (defun write-ideograph-radical-char-data (radical file)
390 (if (file-directory-p file)
391 (let ((name (char-feature (decode-char 'ucs (+ #x2EFF radical))
393 (if (string-match "KANGXI RADICAL " name)
394 (setq name (capitalize (substring name (match-end 0)))))
395 (setq name (mapconcat (lambda (char)
398 (char-to-string char))) name ""))
401 (format "Ideograph-R%03d-%s.el" radical name)
404 (insert (format ";; -*- coding: %s -*-\n"
405 char-db-file-coding-system))
406 (insert-ideograph-radical-char-data radical)
407 (let ((coding-system-for-write char-db-file-coding-system))
408 (write-region (point-min)(point-max) file))))
410 (defun ideographic-structure= (char1 char2)
411 (if (char-ref-p char1)
412 (setq char1 (plist-get char1 :char)))
413 (if (char-ref-p char2)
414 (setq char2 (plist-get char2 :char)))
415 (let ((s1 (if (characterp char1)
416 (get-char-attribute char1 'ideographic-structure)
417 (cdr (assq 'ideographic-structure char1))))
418 (s2 (if (characterp char2)
419 (get-char-attribute char2 'ideographic-structure)
420 (cdr (assq 'ideographic-structure char2))))
422 (if (or (null s1)(null s2))
423 (char-spec= char1 char2)
428 (unless (ideographic-structure= e1 e2)
432 (and (null s1)(null s2))))))
435 (defun ideographic-structure-find-char (structure)
437 (map-char-attribute (lambda (char value)
438 (setq rest structure)
440 (while (and rest value)
441 (unless (ideographic-structure=
442 (car rest)(car value))
444 (setq rest (cdr rest)
446 (unless (or rest value)
448 'ideographic-structure)))
451 (provide 'ideograph-util)
453 ;;; ideograph-util.el ends here