1 ;;; ideograph-util.el --- Ideographic Character Database utility
3 ;; Copyright (C) 1999,2000,2001,2002 MORIOKA Tomohiko.
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: UTF-2000, ISO/IEC 10646, Unicode, UCS-4, MULE.
8 ;; This file is part of XEmacs UTF-2000.
10 ;; XEmacs UTF-2000 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 UTF-2000 is distributed in the hope that it will be useful,
16 ;; but 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 UTF-2000; 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 (cell (get-char-attribute char 'ideographic-))
36 (if (and (setq ret (plist-get cell :radical))
40 (get-char-attribute char 'ideographic-radical)
43 (or (get-char-attribute char 'daikanwa-radical)
44 (get-char-attribute char 'kangxi-radical)
45 (get-char-attribute char 'japanese-radical)
46 (get-char-attribute char 'korean-radical)))
48 (put-char-attribute char 'ideographic-radical ret)
51 (defvar ideograph-radical-strokes-vector
53 [nil 1 1 1 1 1 1 2 2 2
72 9 9 9 9 8 9 9 10 10 10
73 10 10 10 10 10 11 11 11 11 11
75 11 12 12 12 12 13 13 13 13 14
78 (defun char-ideographic-strokes (char &optional radical)
81 (dolist (cell (get-char-attribute char 'ideographic-))
82 (if (and (setq ret (plist-get cell :radical))
85 (throw 'tag (plist-get cell :strokes)))))
86 (get-char-attribute char 'daikanwa-strokes)
87 (get-char-attribute char 'ideographic-strokes)
89 (or (get-char-attribute char 'kangxi-strokes)
90 (get-char-attribute char 'japanese-strokes)
91 (get-char-attribute char 'korean-strokes)
92 (let ((r (char-ideographic-radical char))
93 (ts (get-char-attribute char 'total-strokes)))
95 (- ts (aref ideograph-radical-strokes-vector r))))
98 (put-char-attribute char 'ideographic-strokes strokes)
102 (defun update-ideograph-radical-table ()
104 (let (ret radical script)
106 (lambda (char radical)
108 (or (null (setq script (get-char-attribute char 'script)))
109 (memq 'Ideograph script)))
112 (aref ideograph-radical-chars-vector radical)))
113 (char-ideographic-strokes char)
114 (aset ideograph-radical-chars-vector radical
117 'ideographic-radical)
121 (setq radical (plist-get cell :radical))
123 (or (null (setq script (get-char-attribute char 'script)))
124 (memq 'Ideograph script)))
127 (aref ideograph-radical-chars-vector radical)))
128 (char-ideographic-strokes char)
129 (aset ideograph-radical-chars-vector radical
133 (defun int-list< (a b)
134 (if (numberp (car a))
135 (if (numberp (car b))
136 (if (= (car a) (car b))
137 (int-list< (cdr a)(cdr b))
142 (defun morohashi-daikanwa< (a b)
147 (cond ((eq (car a) 'ho)
149 (int-list< (cdr a)(cdr b))
160 ;; (defun nil=-int< (a b)
161 ;; (cond ((null a) nil)
165 ;; (defun nil>-int< (a b)
166 ;; (cond ((null a) nil)
170 (defun char-representative-of-daikanwa (char)
171 (if (get-char-attribute char 'ideograph-daikanwa)
173 (let ((m (get-char-attribute char 'morohashi-daikanwa))
179 (decode-char 'ideograph-daikanwa m-m)
181 (setq pat (list m-m m-s))
182 (map-char-attribute (lambda (c v)
185 'morohashi-daikanwa))))
188 (defun char-attributes-poly< (c1 c2 accessors testers defaulters)
190 (let (a1 a2 accessor tester dm)
191 (while (and accessors testers)
192 (setq accessor (car accessors)
195 (when (and accessor tester)
196 (setq a1 (funcall accessor c1)
197 a2 (funcall accessor c2))
210 (cond ((funcall tester a1 a2)
212 ((funcall tester a2 a1)
213 (throw 'tag nil))))))
214 (setq accessors (cdr accessors)
215 testers (cdr testers)
216 defaulters (cdr defaulters))))))
218 (defvar ideographic-radical nil)
220 (defun char-daikanwa-strokes (char &optional radical)
222 (setq radical ideographic-radical))
223 (let ((drc (char-representative-of-daikanwa char)))
224 (char-ideographic-strokes
225 (if (= (char-ideographic-radical drc radical)
226 (char-ideographic-radical char radical))
232 (defun char-daikanwa (char)
233 (or (get-char-attribute char 'ideograph-daikanwa)
234 (get-char-attribute char 'morohashi-daikanwa)))
237 (defun char-ucs (char)
238 (or (get-char-attribute char 'ucs)
239 (get-char-attribute char '=>ucs)
240 (get-char-attribute char '->ucs)))
242 (defun char-id (char)
243 (logand (char-int char) #x3FFFFFFF))
245 (defun ideograph-char< (a b &optional radical)
246 (let ((ideographic-radical (or radical
247 ideographic-radical)))
248 (char-attributes-poly<
250 '(char-daikanwa-strokes char-daikanwa char-ucs char-id)
251 '(< morohashi-daikanwa< < <)
254 (defun insert-ideograph-radical-char-data (radical)
256 (sort (copy-list (aref ideograph-radical-chars-vector radical))
258 (ideograph-char< a b radical))))
260 (dolist (name (char-attribute-list))
261 (unless (memq name char-db-ignored-attributes)
262 (if (find-charset name)
264 (push name attributes))))
265 (setq attributes (sort attributes #'char-attribute-name<)
266 ccss (sort ccss #'char-attribute-name<))
267 (aset ideograph-radical-chars-vector radical chars)
269 (when (some (lambda (ccs)
270 (encode-char char ccs))
272 (insert-char-data char nil attributes ccss)))))
274 (defun write-ideograph-radical-char-data (radical file)
275 (if (file-directory-p file)
276 (let ((name (get-char-attribute (int-char (+ #x2EFF radical)) 'name)))
277 (if (string-match "KANGXI RADICAL " name)
278 (setq name (capitalize (substring name (match-end 0)))))
279 (setq name (mapconcat (lambda (char)
282 (char-to-string char))) name ""))
285 (format "Ideograph-R%03d-%s.el" radical name)
288 (insert-ideograph-radical-char-data radical)
289 (char-db-update-comment)
290 (let ((coding-system-for-write 'utf-8))
291 (write-region (point-min)(point-max) file)
294 (defun ideographic-structure= (char1 char2)
295 (if (char-ref-p char1)
296 (setq char1 (plist-get char1 :char)))
297 (if (char-ref-p char2)
298 (setq char2 (plist-get char2 :char)))
299 (let ((s1 (if (characterp char1)
300 (get-char-attribute char1 'ideographic-structure)
301 (cdr (assq 'ideographic-structure char1))))
302 (s2 (if (characterp char2)
303 (get-char-attribute char2 'ideographic-structure)
304 (cdr (assq 'ideographic-structure char2))))
306 (if (or (null s1)(null s2))
307 (char-spec= char1 char2)
312 (unless (ideographic-structure= e1 e2)
316 (and (null s1)(null s2))))))
319 (defun ideographic-structure-find-char (structure)
321 (map-char-attribute (lambda (char value)
322 (setq rest structure)
324 (while (and rest value)
325 (unless (ideographic-structure=
326 (car rest)(car value))
328 (setq rest (cdr rest)
330 (unless (or rest value)
332 'ideographic-structure)))
334 (provide 'ideograph-util)
336 ;;; ideograph-util.el ends here