Apply new glyph-image conventions for `ucs@*', `adobe-japan1-*',
[chise/xemacs-chise.git.1] / lisp / utf-2000 / chise-subr.el
1 ;;; chise-subr.el --- basic lisp subroutines for XEmacs CHISE
2
3 ;; Copyright (C) 1999, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009,
4 ;;   2010, 2011, 2012 MORIOKA Tomohiko.
5
6 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
7 ;; Keywords: CHISE, Character Database, ISO/IEC 10646, UCS, Unicode, MULE.
8
9 ;; This file is part of XEmacs CHISE.
10
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.
15
16 ;; XEmacs CHISE is distributed in the hope that it will be useful,
17 ;; but 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.
20
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.
25
26 ;;; Code:
27
28 (defvar char-db-feature-domains
29   ;; (let (dest str len ret domain)
30   ;;   (dolist (fn (char-attribute-list))
31   ;;     (setq str (symbol-name fn))
32   ;;     (when (string-match "^ideographic-radical@\\([^*]+\\)$" str)
33   ;;       (setq domain (substring str (match-beginning 1)))
34   ;;       (when (> (setq len (length domain)) 0)
35   ;;         (setq ret (read-from-string domain))
36   ;;         (when (= (cdr ret) len)
37   ;;           (setq domain (car ret))
38   ;;           (unless (memq domain dest)
39   ;;             (push domain dest))))))
40   ;;   (sort dest #'string<))
41   '(ucs ucs/compat daikanwa cns gt jis jis/alt jis/a jis/b
42         jis-x0212 jis-x0213 cdp shinjigen
43         r030 r140 misc unknown))
44
45 (defconst charset-id-=adobe-japan1-0 (charset-id '=adobe-japan1-0))
46 (defconst charset-id-=adobe-japan1-6 (charset-id '=adobe-japan1-6))
47 (defconst charset-id-=>>>adobe-japan1-0 (charset-id '=>>>adobe-japan1-0))
48 (defconst charset-id-=>>>adobe-japan1-6 (charset-id '=>>>adobe-japan1-6))
49 (defconst charset-id-=>>adobe-japan1-0 (charset-id '=>>adobe-japan1-0))
50 (defconst charset-id-=>>adobe-japan1-6 (charset-id '=>>adobe-japan1-6))
51
52
53 ;;; @ feature name
54 ;;;
55
56 ;;;###autoload
57 (defun expand-char-feature-name (feature domain)
58   (if domain
59       (intern (format "%s@%s" feature domain))
60     feature))
61
62 ;;;###autoload
63 (defun char-attribute-name< (ka kb)
64   "Return t if symbol KA is less than KB in feature-name sorting order."
65   (cond
66    ((and (symbolp ka)
67          (eq (aref (symbol-name ka) 0) ?*))
68     (cond ((and (symbolp kb)
69                 (eq (aref (symbol-name kb) 0) ?*))
70            (string< (symbol-name ka)
71                     (symbol-name kb))
72            ))
73     )
74    ((and (symbolp kb)
75          (eq (aref (symbol-name kb) 0) ?*))
76     t)
77    ((eq '->denotational kb)
78     t)
79    ((eq '->subsumptive kb)
80     (not (eq '->denotational ka)))
81    ((eq '->denotational ka)
82     nil)
83    ((eq '->subsumptive ka)
84     nil)
85    ((and (symbolp ka)
86          (string-match "^->" (symbol-name ka)))
87     (cond ((and (symbolp kb)
88                 (string-match "^->" (symbol-name kb)))
89            (string< (symbol-name ka)
90                     (symbol-name kb))
91            ))
92     )
93    ((and (symbolp kb)
94          (string-match "^->" (symbol-name kb)))
95     t)
96    ((and (symbolp ka)
97          (string-match "^<-" (symbol-name ka)))
98     (cond ((symbolp kb)
99            (cond ((string-match "^<-" (symbol-name kb))
100                   (string< (symbol-name ka)
101                            (symbol-name kb))
102                   )
103                  ;; ((string-match "^->" (symbol-name kb))
104                  ;;  t)
105                  )))
106     )
107    ((and (symbolp kb)
108          (string-match "^<-" (symbol-name kb)))
109     t
110     ;; (not (string-match "^->" (symbol-name ka)))
111     )
112    ((find-charset ka)
113     (if (find-charset kb)
114         (let (a-ir b-ir a-id b-id)
115           (if (setq a-ir (charset-property ka 'iso-ir))
116               (if (setq b-ir (charset-property kb 'iso-ir))
117                   (cond
118                    ((= a-ir b-ir)
119                     (< (charset-id ka)(charset-id kb))
120                     )
121                    ((= a-ir 177)
122                     t)
123                    ((= b-ir 177)
124                     nil)
125                    ((< a-ir
126                        b-ir)
127                     ))
128                 (cond
129                  ((= a-ir 177)
130                   t)
131                  ((and (setq b-id (charset-id kb))
132                        (or (and (<= charset-id-=adobe-japan1-0 b-id)
133                                 (<= b-id charset-id-=adobe-japan1-6))
134                            (and (<= charset-id-=>>>adobe-japan1-0 b-id)
135                                 (<= b-id charset-id-=>>>adobe-japan1-6))
136                            (and (<= charset-id-=>>adobe-japan1-0 b-id)
137                                 (<= b-id charset-id-=>>adobe-japan1-6))
138                            ))
139                   nil)
140                  (t)))
141             (if (setq b-ir (charset-property kb 'iso-ir))
142                 (cond
143                  ((= b-ir 177)
144                   nil)
145                  ((and (setq a-id (charset-id ka))
146                        (or (and (<= charset-id-=adobe-japan1-0 a-id)
147                                 (<= a-id charset-id-=adobe-japan1-6))
148                            (and (<= charset-id-=>>>adobe-japan1-0 a-id)
149                                 (<= a-id charset-id-=>>>adobe-japan1-6))
150                            (and (<= charset-id-=>>adobe-japan1-0 a-id)
151                                 (<= a-id charset-id-=>>adobe-japan1-6))
152                            ))
153                   t)
154                  (t nil))
155               (cond
156                ((and (setq a-id (charset-id ka))
157                      (or (and (<= charset-id-=adobe-japan1-0 a-id)
158                               (<= a-id charset-id-=adobe-japan1-6))
159                          (and (<= charset-id-=>>>adobe-japan1-0 a-id)
160                               (<= a-id charset-id-=>>>adobe-japan1-6))
161                          (and (<= charset-id-=>>adobe-japan1-0 a-id)
162                               (<= a-id charset-id-=>>adobe-japan1-6))
163                          ))
164                 (if (and (setq b-id (charset-id kb))
165                          (or (and (<= charset-id-=adobe-japan1-0 b-id)
166                                   (<= b-id charset-id-=adobe-japan1-6))
167                              (and (<= charset-id-=>>>adobe-japan1-0 b-id)
168                                   (<= b-id charset-id-=>>>adobe-japan1-6))
169                              (and (<= charset-id-=>>adobe-japan1-0 b-id)
170                                   (<= b-id charset-id-=>>adobe-japan1-6))
171                              ))
172                     (< a-id b-id)
173                   t))
174                ((and (setq b-id (charset-id kb))
175                      (or (and (<= charset-id-=adobe-japan1-0 b-id)
176                               (<= b-id charset-id-=adobe-japan1-6))
177                          (and (<= charset-id-=>>>adobe-japan1-0 b-id)
178                               (<= b-id charset-id-=>>>adobe-japan1-6))
179                          (and (<= charset-id-=>>adobe-japan1-0 b-id)
180                               (<= b-id charset-id-=>>adobe-japan1-6))
181                          ))
182                 nil)
183                (t
184                 (< (charset-id ka)(charset-id kb))
185                 )))))
186       nil)
187     )
188    ((find-charset kb))
189    ((symbolp ka)
190     (cond ((symbolp kb)
191            (string< (symbol-name ka)
192                     (symbol-name kb)))
193           (t)))
194    ((symbolp kb)
195     nil)))
196
197
198 ;;; @ char feature
199 ;;;
200
201 ;;;###autoload
202 (defun char-ucs (char)
203   "Return code-point of UCS."
204   (or (encode-char char '=ucs 'defined-only)
205       (char-feature char '=>ucs)))
206
207 ;;;###autoload
208 (defun char-id (char)
209   (logand (char-int char) #x3FFFFFFF))
210
211
212 ;;; @ char hierarchy
213 ;;;
214
215 ;;;###autoload
216 (defun map-char-family (function char &optional ignore-sisters)
217   (let ((rest (list char))
218         ret checked)
219     (catch 'tag
220       (while rest
221         (unless (memq (car rest) checked)
222           (if (setq ret (funcall function (car rest)))
223               (throw 'tag ret))
224           (setq checked (cons (car rest) checked)
225                 rest (append rest
226                              (get-char-attribute (car rest) '->subsumptive)
227                              (get-char-attribute (car rest) '->denotational)
228                              (get-char-attribute (car rest) '->identical)))
229           (unless ignore-sisters
230             (setq rest (append rest
231                                (get-char-attribute (car rest) '<-subsumptive)
232                                (get-char-attribute (car rest) '<-denotational)))))
233         (setq rest (cdr rest))))))
234
235
236 ;;;###autoload
237 (defun define-char-before (char-spec next-char)
238   "Define CHAR-SPEC and insert it before NEXT-CHAR." 
239   (let (mother sisters rest)
240     (when (and (or (characterp next-char)
241                    (setq next-char (find-char next-char)))
242                (setq mother (get-char-attribute next-char '<-subsumptive))
243                (setq mother (car mother))
244                (setq sisters (get-char-attribute mother '->subsumptive)))
245       (if (eq (car sisters) next-char)
246           (setq sisters (cons (define-char char-spec) sisters))
247         (setq rest sisters)
248         (while (and (cdr rest)
249                     (not (eq (nth 1 rest) next-char)))
250           (setq rest (cdr rest)))
251         (if (null rest)
252             (setq sisters (cons (define-char char-spec) sisters))
253           (setcdr rest (cons (define-char char-spec) (cdr rest)))))
254       (put-char-attribute mother '->subsumptive sisters))))
255
256 ;;;###autoload
257 (defun define-char-after (prev-char char-spec)
258   "Define CHAR-SPEC and insert it after PREV-CHAR." 
259   (let (mother sisters rest)
260     (when (and (or (characterp prev-char)
261                    (setq prev-char (find-char prev-char)))
262                (setq mother (get-char-attribute prev-char '<-subsumptive))
263                (setq mother (car mother))
264                (setq sisters (get-char-attribute mother '->subsumptive)))
265       (setq rest sisters)
266       (while (and rest
267                   (not (eq (car rest) prev-char)))
268         (setq rest (cdr rest)))
269       (if (null rest)
270           (setq sisters (cons (define-char char-spec) sisters))
271         (setcdr rest (cons (define-char char-spec) (cdr rest))))
272       (put-char-attribute mother '->subsumptive sisters))))
273
274
275 ;;; @ string
276 ;;;
277
278 ;;;###autoload
279 (defun chise-string< (string1 string2 accessors)
280   (let ((len1 (length string1))
281         (len2 (length string2))
282         len
283         (i 0)
284         c1 c2
285         rest func
286         v1 v2)
287     (setq len (min len1 len2))
288     (catch 'tag
289       (while (< i len)
290         (setq c1 (aref string1 i)
291               c2 (aref string2 i))
292         (setq rest accessors)
293         (while (and rest
294                     (setq func (car rest))
295                     (setq v1 (funcall func c1)
296                           v2 (funcall func c2))
297                     (eq v1 v2))
298           (setq rest (cdr rest)))
299         (if v1
300             (if v2
301                 (cond ((< v1 v2)
302                        (throw 'tag t))
303                       ((> v1 v2)
304                        (throw 'tag nil)))
305               (throw 'tag nil))
306           (if v2
307               (throw 'tag t)))
308         (setq i (1+ i)))
309       (< len1 len2))))
310
311
312 ;;; @ end
313 ;;;
314
315 (provide 'chise-subr)
316
317 ;;; chise-subr.el ends here