1 ;;; ideograph-util.el --- Ideographic Character Database utility
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008,
4 ;; 2009, 2010, 2012 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 '=daikanwa@rev2 'defined-only)
174 (encode-char char '=daikanwa/+p 'defined-only)
175 (encode-char char '=daikanwa/+2p 'defined-only)
176 (encode-char char '=daikanwa/ho 'defined-only)
179 (if (setq ret (get-char-attribute char 'morohashi-daikanwa))
180 (let ((m-m (car ret))
184 (or (decode-char '=daikanwa@rev2 m-m 'defined-only)
185 (decode-char '=daikanwa m-m))
186 (or (cond ((eq m-m 'ho)
187 (decode-char '=daikanwa/ho m-s))
189 (decode-char '=daikanwa/+p m-m))
191 (decode-char '=daikanwa/+2p m-m)))
193 (setq pat (list m-m m-s))
194 (map-char-attribute (lambda (c v)
197 'morohashi-daikanwa))))))
198 (and (setq ret (get-char-attribute char '=>daikanwa))
200 (or (decode-char '=daikanwa@rev2 ret 'defined-only)
201 (decode-char '=daikanwa ret))
202 (map-char-attribute (lambda (c v)
205 'morohashi-daikanwa)))
206 (unless (memq char checked)
209 (append (get-char-attribute char '->subsumptive)
210 (get-char-attribute char '->denotational)))
213 (setq checked (cons char checked))
216 (if (setq ret (char-representative-of-daikanwa
217 sc radical t checked))
219 (setq checked (cons sc checked)
222 (setq rest (get-char-attribute char '->identical))
225 (when (setq ret (char-representative-of-daikanwa
226 sc radical t checked))
228 (setq checked (cons sc checked)
231 (append (get-char-attribute char '<-subsumptive)
232 (get-char-attribute char '<-denotational)))
235 (when (setq ret (char-representative-of-daikanwa
236 sc radical t checked))
238 (setq checked (cons sc checked)
240 (unless ignore-default
243 (defun char-attributes-poly< (c1 c2 accessors testers defaulters)
245 (let (a1 a2 accessor tester dm)
246 (while (and accessors testers)
247 (setq accessor (car accessors)
250 (when (and accessor tester)
251 (setq a1 (funcall accessor c1)
252 a2 (funcall accessor c2))
265 (cond ((funcall tester a1 a2)
267 ((funcall tester a2 a1)
268 (throw 'tag nil))))))
269 (setq accessors (cdr accessors)
270 testers (cdr testers)
271 defaulters (cdr defaulters))))))
273 (defun char-daikanwa-strokes (char &optional radical)
275 (setq radical ideographic-radical))
276 (let ((drc (char-representative-of-daikanwa char radical))
277 (r (char-ideographic-radical char radical)))
280 (= (char-ideographic-radical drc radical) r)))
282 (char-ideographic-strokes char radical '(daikanwa)))
285 (defun char-daikanwa (char &optional radical checked depth)
287 (setq radical ideographic-radical))
288 (if (or (null radical)
289 (eq (or (get-char-attribute char 'ideographic-radical)
290 (char-ideographic-radical char radical t))
292 (let ((ret (or (encode-char char '=daikanwa@rev2 'defined-only)
293 ;; (encode-char char '=daikanwa 'defined-only)
294 (get-char-attribute char 'morohashi-daikanwa))))
297 ((setq ret (encode-char char '=daikanwa/+p 'defined-only))
298 (setq ret (list ret 1)))
299 ((setq ret (encode-char char '=daikanwa/+2p 'defined-only))
300 (setq ret (list ret 2)))
301 ((setq ret (encode-char char '=daikanwa/ho 'defined-only))
302 (setq ret (list 'ho ret)))))
307 (append ret (list depth)))
309 (and (setq ret (get-char-attribute char '=>daikanwa))
312 (append ret '(-10))))
313 (unless (memq char checked)
318 (append (get-char-attribute char '->subsumptive)
319 (get-char-attribute char '->denotational)))
322 (setq checked (cons char checked))
325 (if (setq ret (char-daikanwa sc radical checked
328 (setq checked (cons sc checked)
331 (setq rest (get-char-attribute char '->identical))
334 (when (setq ret (char-daikanwa sc radical checked depth))
338 (append ret (list i)))))
339 (setq checked (cons sc checked)
342 (append (get-char-attribute char '<-subsumptive)
343 (get-char-attribute char '<-denotational)))
346 (when (setq ret (char-daikanwa sc radical checked depth))
350 (if (>= (setq lnum (car (last ret))) 0)
351 (append ret (list i))
353 (list 0 (- lnum) i))))))
354 (setq checked (cons sc checked)
355 rest (cdr rest))))))))))
357 (defun char-ideographic-strokes-diff (char &optional radical)
358 (if (or (get-char-attribute char '<-subsumptive)
359 (get-char-attribute char '<-denotational))
361 (when (and (setq s (char-ideographic-strokes char radical))
362 (setq ds (char-daikanwa-strokes char radical)))
367 (defun ideograph-char< (a b &optional radical)
368 (let ((ideographic-radical (or radical
369 ideographic-radical)))
370 (char-attributes-poly<
372 '(char-daikanwa-strokes char-daikanwa char-ucs
373 char-ideographic-strokes-diff char-id)
374 '(< morohashi-daikanwa< < < <)
377 (defun insert-ideograph-radical-char-data (radical)
379 (sort (copy-list (aref ideograph-radical-chars-vector radical))
381 (ideograph-char< a b radical))))
384 (dolist (name (char-attribute-list))
385 (unless (memq name char-db-ignored-attributes)
386 ;; (if (find-charset name)
388 (push name attributes)
391 (setq attributes (sort attributes #'char-attribute-name<)
392 ;; ccss (sort ccss #'char-attribute-name<)
394 (aset ideograph-radical-chars-vector radical chars)
397 (not (some (lambda (atr)
398 (get-char-attribute char atr))
399 char-db-ignored-attributes))
400 ;; (some (lambda (ccs)
401 ;; (encode-char char ccs 'defined-only))
404 (insert-char-data char nil attributes ;ccss
407 (defun write-ideograph-radical-char-data (radical file)
408 (if (file-directory-p file)
409 (let ((name (char-feature (decode-char 'ucs (+ #x2EFF radical))
411 (if (string-match "KANGXI RADICAL " name)
412 (setq name (capitalize (substring name (match-end 0)))))
413 (setq name (mapconcat (lambda (char)
416 (char-to-string char))) name ""))
419 (format "Ideograph-R%03d-%s.el" radical name)
422 (insert (format ";; -*- coding: %s -*-\n"
423 char-db-file-coding-system))
424 (insert-ideograph-radical-char-data radical)
425 (let ((coding-system-for-write char-db-file-coding-system))
426 (write-region (point-min)(point-max) file))))
428 (defun ideographic-structure= (char1 char2)
429 (if (char-ref-p char1)
430 (setq char1 (plist-get char1 :char)))
431 (if (char-ref-p char2)
432 (setq char2 (plist-get char2 :char)))
433 (let ((s1 (if (characterp char1)
434 (get-char-attribute char1 'ideographic-structure)
435 (cdr (assq 'ideographic-structure char1))))
436 (s2 (if (characterp char2)
437 (get-char-attribute char2 'ideographic-structure)
438 (cdr (assq 'ideographic-structure char2))))
440 (if (or (null s1)(null s2))
441 (char-spec= char1 char2)
446 (unless (ideographic-structure= e1 e2)
450 (and (null s1)(null s2))))))
453 (defun ideographic-structure-find-char (structure)
455 (map-char-attribute (lambda (char value)
456 (setq rest structure)
458 (while (and rest value)
459 (unless (ideographic-structure=
460 (car rest)(car value))
462 (setq rest (cdr rest)
464 (unless (or rest value)
466 'ideographic-structure)))
469 (provide 'ideograph-util)
471 ;;; ideograph-util.el ends here