1 ;;; ideograph-util.el --- Ideographic Character Database utility
3 ;; Copyright (C) 1999,2000,2001,2002,2003 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 (domain char-db-feature-domains)
36 (if (and (setq ret (get-char-attribute
40 'ideographic-radical domain))))
45 (dolist (cell (get-char-attribute char 'ideographic-))
46 (if (and (setq ret (plist-get cell :radical))
50 (get-char-attribute 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
88 (defun char-ideographic-strokes (char &optional radical)
91 (dolist (domain char-db-feature-domains)
92 (if (and (setq ret (or (get-char-attribute
96 'ideographic-radical domain)))
98 char 'ideographic-radical)))
101 (setq ret (get-char-attribute
105 'ideographic-strokes domain)))))
108 (dolist (cell (get-char-attribute char 'ideographic-))
109 (if (and (setq ret (plist-get cell :radical))
112 (throw 'tag (plist-get cell :strokes)))))
113 (get-char-attribute char 'daikanwa-strokes)
114 (get-char-attribute char 'ideographic-strokes)
116 (or (get-char-attribute char 'kangxi-strokes)
117 (get-char-attribute char 'japanese-strokes)
118 (get-char-attribute char 'korean-strokes)
119 (let ((r (char-ideographic-radical char))
120 (ts (get-char-attribute char 'total-strokes)))
122 (- ts (aref ideograph-radical-strokes-vector r))))
125 (put-char-attribute char 'ideographic-strokes strokes)
129 (defun update-ideograph-radical-table ()
131 (let (ret radical script)
132 (dolist (domain char-db-feature-domains)
134 (lambda (char radical)
136 (or (null (setq script (get-char-attribute char 'script)))
137 (memq 'Ideograph script)))
140 (aref ideograph-radical-chars-vector radical)))
141 (char-ideographic-strokes char)
142 (aset ideograph-radical-chars-vector radical
145 (intern (format "%s@%s" 'ideographic-radical domain))))
147 (lambda (char radical)
149 (or (null (setq script (get-char-attribute char 'script)))
150 (memq 'Ideograph script)))
153 (aref ideograph-radical-chars-vector radical)))
154 (char-ideographic-strokes char)
155 (aset ideograph-radical-chars-vector radical
158 'ideographic-radical)
162 (setq radical (plist-get cell :radical))
164 (or (null (setq script (get-char-attribute char 'script)))
165 (memq 'Ideograph script)))
168 (aref ideograph-radical-chars-vector radical)))
169 (char-ideographic-strokes char)
170 (aset ideograph-radical-chars-vector radical
174 (defun int-list< (a b)
175 (if (numberp (car a))
176 (if (numberp (car b))
177 (if (= (car a) (car b))
178 (int-list< (cdr a)(cdr b))
183 (defun morohashi-daikanwa< (a b)
188 (cond ((eq (car a) 'ho)
190 (int-list< (cdr a)(cdr b))
201 ;; (defun nil=-int< (a b)
202 ;; (cond ((null a) nil)
206 ;; (defun nil>-int< (a b)
207 ;; (cond ((null a) nil)
212 (defun char-representative-of-daikanwa (char)
213 (if (or (encode-char char 'ideograph-daikanwa 'defined-only)
214 (encode-char char '=daikanwa-rev2 'defined-only))
216 (let ((m (get-char-attribute char 'morohashi-daikanwa))
222 (or (decode-char '=daikanwa-rev2 m-m 'defined-only)
223 (decode-char 'ideograph-daikanwa m-m))
225 (setq pat (list m-m m-s))
226 (map-char-attribute (lambda (c v)
229 'morohashi-daikanwa))))
232 (defun char-attributes-poly< (c1 c2 accessors testers defaulters)
234 (let (a1 a2 accessor tester dm)
235 (while (and accessors testers)
236 (setq accessor (car accessors)
239 (when (and accessor tester)
240 (setq a1 (funcall accessor c1)
241 a2 (funcall accessor c2))
254 (cond ((funcall tester a1 a2)
256 ((funcall tester a2 a1)
257 (throw 'tag nil))))))
258 (setq accessors (cdr accessors)
259 testers (cdr testers)
260 defaulters (cdr defaulters))))))
262 (defvar ideographic-radical nil)
264 (defun char-daikanwa-strokes (char &optional radical)
266 (setq radical ideographic-radical))
267 (let ((drc (char-representative-of-daikanwa char)))
268 (char-ideographic-strokes
269 (if (= (char-ideographic-radical drc radical)
270 (char-ideographic-radical char radical))
276 (defun char-daikanwa (char)
277 (or (encode-char char 'ideograph-daikanwa 'defined-only)
278 (encode-char char '=daikanwa-rev2 'defined-only)
279 (get-char-attribute char 'morohashi-daikanwa)))
282 (defun char-ucs (char)
283 (or (encode-char char '=ucs 'defined-only)
284 (get-char-attribute char '=>ucs)))
286 (defun char-id (char)
287 (logand (char-int char) #x3FFFFFFF))
289 (defun ideograph-char< (a b &optional radical)
290 (let ((ideographic-radical (or radical
291 ideographic-radical)))
292 (char-attributes-poly<
294 '(char-daikanwa-strokes char-daikanwa char-ucs char-id)
295 '(< morohashi-daikanwa< < <)
298 (defun insert-ideograph-radical-char-data (radical)
300 (sort (copy-list (aref ideograph-radical-chars-vector radical))
302 (ideograph-char< a b radical))))
304 (dolist (name (char-attribute-list))
305 (unless (memq name char-db-ignored-attributes)
306 (if (find-charset name)
308 (push name attributes))))
309 (setq attributes (sort attributes #'char-attribute-name<)
310 ccss (sort ccss #'char-attribute-name<))
311 (aset ideograph-radical-chars-vector radical chars)
313 (when (or (not (some (lambda (atr)
314 (get-char-attribute char atr))
315 char-db-ignored-attributes))
317 (encode-char char ccs 'defined-only))
319 (insert-char-data char nil attributes ccss)))))
321 (defun write-ideograph-radical-char-data (radical file)
322 (if (file-directory-p file)
323 (let ((name (get-char-attribute (int-char (+ #x2EFF radical)) 'name)))
324 (if (string-match "KANGXI RADICAL " name)
325 (setq name (capitalize (substring name (match-end 0)))))
326 (setq name (mapconcat (lambda (char)
329 (char-to-string char))) name ""))
332 (format "Ideograph-R%03d-%s.el" radical name)
335 (insert ";; -*- coding: utf-8-mcs -*-\n")
336 (insert-ideograph-radical-char-data radical)
337 (let ((coding-system-for-write 'utf-8-mcs))
338 (write-region (point-min)(point-max) file)
341 (defun ideographic-structure= (char1 char2)
342 (if (char-ref-p char1)
343 (setq char1 (plist-get char1 :char)))
344 (if (char-ref-p char2)
345 (setq char2 (plist-get char2 :char)))
346 (let ((s1 (if (characterp char1)
347 (get-char-attribute char1 'ideographic-structure)
348 (cdr (assq 'ideographic-structure char1))))
349 (s2 (if (characterp char2)
350 (get-char-attribute char2 'ideographic-structure)
351 (cdr (assq 'ideographic-structure char2))))
353 (if (or (null s1)(null s2))
354 (char-spec= char1 char2)
359 (unless (ideographic-structure= e1 e2)
363 (and (null s1)(null s2))))))
366 (defun ideographic-structure-find-char (structure)
368 (map-char-attribute (lambda (char value)
369 (setq rest structure)
371 (while (and rest value)
372 (unless (ideographic-structure=
373 (car rest)(car value))
375 (setq rest (cdr rest)
377 (unless (or rest value)
379 'ideographic-structure)))
381 (provide 'ideograph-util)
383 ;;; ideograph-util.el ends here