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))
36 (defvar ideograph-radical-strokes-vector
38 [nil 1 1 1 1 1 1 2 2 2
57 9 9 9 9 8 9 9 10 10 10
58 10 10 10 10 10 11 11 11 11 11
60 11 12 12 12 12 13 13 13 13 14
64 (defun char-ideographic-strokes (char &optional radical preferred-domains)
67 (dolist (cell (get-char-attribute char 'ideographic-))
68 (if (and (setq ret (plist-get cell :radical))
71 (throw 'tag (plist-get cell :strokes)))))
72 (char-ideographic-strokes-from-domains
73 char (append preferred-domains
75 char-db-feature-domains))
77 (get-char-attribute char 'daikanwa-strokes)
79 (or (get-char-attribute char 'kangxi-strokes)
80 (get-char-attribute char 'japanese-strokes)
81 (get-char-attribute char 'korean-strokes)
82 (let ((r (char-ideographic-radical char))
83 (ts (get-char-attribute char 'total-strokes)))
85 (- ts (aref ideograph-radical-strokes-vector r))))
88 (put-char-attribute char 'ideographic-strokes strokes)
92 (defun char-total-strokes-from-domains (char domains)
95 (dolist (domain domains)
96 (if (setq ret (char-feature
100 'total-strokes domain))))
101 (throw 'tag ret))))))
104 (defun char-total-strokes (char &optional preferred-domains)
105 (or (char-total-strokes-from-domains char preferred-domains)
106 (char-feature char 'total-strokes)
107 (char-total-strokes-from-domains char char-db-feature-domains)))
110 (defun update-ideograph-radical-table ()
112 (let (ret rret radical script dest)
114 (cons 'ideographic-radical
116 (dolist (feature (char-attribute-list))
117 (if (string-match "^ideographic-radical@[^@*]+$"
118 (symbol-name feature))
119 (setq dest (cons feature dest))))
122 (lambda (chr radical)
123 (dolist (char (append
125 (get-char-attribute chr '<-subsumptive))
129 (unless (eq (get-char-attribute
130 pc 'ideographic-radical)
135 (setq ret (append ret rret))
136 (setq dest (cons pc dest)))))
140 (get-char-attribute chr '<-identical)
141 (get-char-attribute chr '->denotational)))
147 (setq rest (cdr rest))
148 (setq dest (cons pc dest))
154 pc '->denotational)))))
158 (or (get-char-attribute
159 char 'ideographic-radical)
160 (char-ideographic-radical char radical)))
161 (null (char-ideographic-radical char)))
162 (or (null (setq script
163 (get-char-attribute char 'script)))
164 (memq 'Ideograph script)))
167 (aref ideograph-radical-chars-vector
169 (char-ideographic-strokes char)
170 (aset ideograph-radical-chars-vector radical
177 (setq radical (plist-get cell :radical))
179 (or (null (setq script (get-char-attribute char 'script)))
180 (memq 'Ideograph script)))
183 (aref ideograph-radical-chars-vector radical)))
184 (char-ideographic-strokes char)
185 (aset ideograph-radical-chars-vector radical
189 (defun int-list< (a b)
190 (if (numberp (car a))
191 (if (numberp (car b))
192 (if (= (car a) (car b))
193 (int-list< (cdr a)(cdr b))
198 (if (numberp (car b))
204 (defun morohashi-daikanwa< (a b)
209 (cond ((eq (car-safe a) 'ho)
210 (if (eq (car-safe b) 'ho)
211 (int-list< (cdr-safe a)(cdr-safe b))
219 (if (eq (car-safe b) 'ho)
223 ;; (defun nil=-int< (a b)
224 ;; (cond ((null a) nil)
228 ;; (defun nil>-int< (a b)
229 ;; (cond ((null a) nil)
233 (defvar ideographic-radical nil)
236 (defun char-representative-of-daikanwa (char &optional radical
237 ignore-default checked)
239 (setq radical ideographic-radical))
240 (if (or (null radical)
241 (eq (or (get-char-attribute char 'ideographic-radical)
242 (char-ideographic-radical char radical t))
244 (let ((ret (or (encode-char char 'ideograph-daikanwa 'defined-only)
245 (encode-char char '=daikanwa-rev2 'defined-only))))
247 (if (setq ret (get-char-attribute char 'morohashi-daikanwa))
248 (let ((m-m (car ret))
252 (or (decode-char '=daikanwa-rev2 m-m 'defined-only)
253 (decode-char 'ideograph-daikanwa m-m))
254 (setq pat (list m-m m-s))
255 (map-char-attribute (lambda (c v)
258 'morohashi-daikanwa))))
259 (and (setq ret (get-char-attribute char '=>daikanwa))
261 (or (decode-char '=daikanwa-rev2 ret 'defined-only)
262 (decode-char 'ideograph-daikanwa ret))
263 (map-char-attribute (lambda (c v)
266 'morohashi-daikanwa)))
267 (unless (memq char checked)
270 (append (get-char-attribute char '->subsumptive)
271 (get-char-attribute char '->denotational)))
274 (setq checked (cons char checked))
277 (if (setq ret (char-representative-of-daikanwa
278 sc radical t checked))
280 (setq checked (cons sc checked)
283 (setq rest (get-char-attribute char '->identical))
286 (when (setq ret (char-representative-of-daikanwa
287 sc radical t checked))
289 (setq checked (cons sc checked)
292 (append (get-char-attribute char '<-subsumptive)
293 (get-char-attribute char '<-denotational)))
296 (when (setq ret (char-representative-of-daikanwa
297 sc radical t checked))
299 (setq checked (cons sc checked)
301 (unless ignore-default
304 (defun char-attributes-poly< (c1 c2 accessors testers defaulters)
306 (let (a1 a2 accessor tester dm)
307 (while (and accessors testers)
308 (setq accessor (car accessors)
311 (when (and accessor tester)
312 (setq a1 (funcall accessor c1)
313 a2 (funcall accessor c2))
326 (cond ((funcall tester a1 a2)
328 ((funcall tester a2 a1)
329 (throw 'tag nil))))))
330 (setq accessors (cdr accessors)
331 testers (cdr testers)
332 defaulters (cdr defaulters))))))
334 (defun char-daikanwa-strokes (char &optional radical)
336 (setq radical ideographic-radical))
337 (let ((drc (char-representative-of-daikanwa char radical))
338 (r (char-ideographic-radical char radical)))
340 (= (char-ideographic-radical drc radical) r))
342 (char-ideographic-strokes char radical '(daikanwa)))
345 (defun char-daikanwa (char &optional radical checked depth)
347 (setq radical ideographic-radical))
348 (if (or (null radical)
349 (eq (or (get-char-attribute char 'ideographic-radical)
350 (char-ideographic-radical char radical t))
352 (let ((ret (or (encode-char char 'ideograph-daikanwa 'defined-only)
353 (encode-char char '=daikanwa-rev2 'defined-only)
354 (get-char-attribute char 'morohashi-daikanwa))))
359 (append ret (list depth)))
361 (and (setq ret (get-char-attribute char '=>daikanwa))
365 (unless (memq char checked)
370 (append (get-char-attribute char '->subsumptive)
371 (get-char-attribute char '->denotational)))
374 (setq checked (cons char checked))
377 (if (setq ret (char-daikanwa sc radical checked
380 (setq checked (cons sc checked)
383 (setq rest (get-char-attribute char '->identical))
386 (when (setq ret (char-daikanwa sc radical checked depth))
390 (append ret (list i)))))
391 (setq checked (cons sc checked)
394 (append (get-char-attribute char '<-subsumptive)
395 (get-char-attribute char '<-denotational)))
398 (when (setq ret (char-daikanwa sc radical checked depth))
402 (if (>= (setq lnum (car (last ret))) 0)
403 (append ret (list i))
405 (list 0 (- lnum) i))))))
406 (setq checked (cons sc checked)
407 rest (cdr rest))))))))))
409 (defun char-ideographic-strokes-diff (char &optional radical)
410 (if (or (get-char-attribute char '<-subsumptive)
411 (get-char-attribute char '<-denotational))
413 (when (and (setq s (char-ideographic-strokes char radical))
414 (setq ds (char-daikanwa-strokes char radical)))
419 (defun ideograph-char< (a b &optional radical)
420 (let ((ideographic-radical (or radical
421 ideographic-radical)))
422 (char-attributes-poly<
424 '(char-daikanwa-strokes char-daikanwa char-ucs
425 char-ideographic-strokes-diff char-id)
426 '(< morohashi-daikanwa< < < <)
429 (defun insert-ideograph-radical-char-data (radical)
431 (sort (copy-list (aref ideograph-radical-chars-vector radical))
433 (ideograph-char< a b radical))))
436 (dolist (name (char-attribute-list))
437 (unless (memq name char-db-ignored-attributes)
438 ;; (if (find-charset name)
440 (push name attributes)
443 (setq attributes (sort attributes #'char-attribute-name<)
444 ;; ccss (sort ccss #'char-attribute-name<)
446 (aset ideograph-radical-chars-vector radical chars)
449 (not (some (lambda (atr)
450 (get-char-attribute char atr))
451 char-db-ignored-attributes))
452 ;; (some (lambda (ccs)
453 ;; (encode-char char ccs 'defined-only))
456 (insert-char-data char nil attributes ;ccss
459 (defun write-ideograph-radical-char-data (radical file)
460 (if (file-directory-p file)
461 (let ((name (char-feature (decode-char 'ucs (+ #x2EFF radical))
463 (if (string-match "KANGXI RADICAL " name)
464 (setq name (capitalize (substring name (match-end 0)))))
465 (setq name (mapconcat (lambda (char)
468 (char-to-string char))) name ""))
471 (format "Ideograph-R%03d-%s.el" radical name)
474 (insert (format ";; -*- coding: %s -*-\n"
475 char-db-file-coding-system))
476 (insert-ideograph-radical-char-data radical)
477 (let ((coding-system-for-write char-db-file-coding-system))
478 (write-region (point-min)(point-max) file))))
480 (defun ideographic-structure= (char1 char2)
481 (if (char-ref-p char1)
482 (setq char1 (plist-get char1 :char)))
483 (if (char-ref-p char2)
484 (setq char2 (plist-get char2 :char)))
485 (let ((s1 (if (characterp char1)
486 (get-char-attribute char1 'ideographic-structure)
487 (cdr (assq 'ideographic-structure char1))))
488 (s2 (if (characterp char2)
489 (get-char-attribute char2 'ideographic-structure)
490 (cdr (assq 'ideographic-structure char2))))
492 (if (or (null s1)(null s2))
493 (char-spec= char1 char2)
498 (unless (ideographic-structure= e1 e2)
502 (and (null s1)(null s2))))))
505 (defun ideographic-structure-find-char (structure)
507 (map-char-attribute (lambda (char value)
508 (setq rest structure)
510 (while (and rest value)
511 (unless (ideographic-structure=
512 (car rest)(car value))
514 (setq rest (cdr rest)
516 (unless (or rest value)
518 'ideographic-structure)))
521 (provide 'ideograph-util)
523 ;;; ideograph-util.el ends here