update.
[chise/ids.git] / ids.el
1 ;;; ids.el --- Parser and utility for Ideographic Description Sequence.
2
3 ;; Copyright (C) 2001, 2002, 2003, 2005, 2020 MORIOKA Tomohiko
4
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: IDS, IDC, Ideographs, UCS, Unicode
7
8 ;; This file is a part of CHISE-IDS.
9
10 ;; This program 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.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; 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.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; 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.
24
25 ;;; Commentary:
26
27 ;; Ideographic Description Sequence (IDS) is defined in ISO/IEC
28 ;; 10646-1:2000 Annex F.
29
30 ;;; Code:
31
32 (require 'ideograph-util)
33 (require 'ids-find)
34
35 (defun ideographic-structure-find-char (structure)
36   (car (ideographic-structure-find-chars structure))
37   ;; (dolist (product (char-feature (nth 1 structure) 'ideographic-products))
38   ;;   (if (equal structure
39   ;;              (char-feature product 'ideographic-structure))
40   ;;       (return product)))
41   )
42
43 (defun ids-parse-terminal (string)
44   (if (>= (length string) 1)
45       (let* ((chr (aref string 0))
46              (ucs (encode-char chr '=ucs 'defined-only))
47              big5)
48         (unless (or (and ucs (<= #x2FF0 ucs)(<= ucs #x2FFF))
49                     (memq (encode-char chr '=ucs-var-001)
50                           '(#x2FF0))
51                     (memq (encode-char chr '=ucs-itaiji-001)
52                           '(#x2FF9 #x2FF6)))
53           (if (and ucs (<= #xE000 ucs)(<= ucs #xF8FF)
54                    (setq big5 (encode-char chr 'chinese-big5)))
55               (setq chr (decode-char '=big5-cdp big5)))
56           (cons chr
57                 (substring string 1))))))
58
59 (defun ids-parse-op-2 (string)
60   (if (>= (length string) 1)
61       (let* ((chr (aref string 0))
62              (ucs (encode-char chr '=ucs 'defined-only)))
63         (if (or (and ucs
64                      (or (eq ucs #x2FF0)
65                          (eq ucs #x2FF1)
66                          (and (<= #x2FF4 ucs)(<= ucs #x2FFB))))
67                 (memq (encode-char chr '=ucs-var-001)
68                       '(#x2FF0))
69                 (memq (encode-char chr '=ucs-itaiji-001)
70                       '(#x2FF9 #x2FF6)))
71             (cons chr
72                   (substring string 1))))))
73
74 (defun ids-parse-op-3 (string)
75   (if (>= (length string) 1)
76       (let ((chr (aref string 0)))
77         (if (memq chr '(?\u2FF2 ?\u2FF3))
78             (cons chr
79                   (substring string 1))))))
80
81 (defun ids-parse-component (string simplify)
82   (let ((ret (ids-parse-element string simplify))
83         rret)
84     (when ret
85       (if (and simplify
86                (listp (car ret))
87                (setq rret (ideographic-structure-find-char
88                            (cdr (assq 'ideographic-structure (car ret))))))
89           (cons rret (cdr ret))
90         ret))))
91
92 (defun ids-parse-element (string simplify)
93   (let (ret op arg1 arg2 arg3)
94     (cond ((ids-parse-terminal string))
95           ((setq ret (ids-parse-op-2 string))
96            (setq op (car ret))
97            (when (setq ret (ids-parse-component (cdr ret) simplify))
98              (setq arg1 (car ret))
99              (when (setq ret (ids-parse-component (cdr ret) simplify))
100                (setq arg2 (car ret))
101                (cons (list (list 'ideographic-structure op arg1 arg2))
102                      (cdr ret)))))
103           ((setq ret (ids-parse-op-3 string))
104            (setq op (car ret))
105            (when (setq ret (ids-parse-component (cdr ret) simplify))
106              (setq arg1 (car ret))
107              (when (setq ret (ids-parse-component (cdr ret) simplify))
108                (setq arg2 (car ret))
109                (when (setq ret (ids-parse-component (cdr ret) simplify))
110                  (setq arg3 (car ret))
111                  (cons (list (list 'ideographic-structure op arg1 arg2 arg3))
112                        (cdr ret)))))))))
113
114 ;;;###autoload
115 (defun ids-parse-string (ids-string &optional simplify)
116   "Parse IDS-STRING and return the result."
117   (let ((ret (ids-parse-element ids-string simplify)))
118     (if (= (length (cdr ret)) 0)
119         (car ret))))
120
121 ;; (defun ids-format-unit (ids-char)
122 ;;   (let (ret)
123 ;;     (cond ((characterp ids-char)
124 ;;            (char-to-string ids-char))
125 ;;           ((integerp ids-char)
126 ;;            (char-to-string (decode-char 'ucs ids-char)))
127 ;;           ((setq ret (find-char ids-char))
128 ;;            (char-to-string ret))
129 ;;           ((setq ret (assq 'ideographic-structure ids-char))
130 ;;            (ids-format-list (cdr ret))))))
131
132 ;; ;;;###autoload
133 ;; (defun ids-format-list (ids-list)
134 ;;   "Format ideographic-structure IDS-LIST as an IDS-string."
135 ;;   (mapconcat (lambda (cell)
136 ;;                (ids-format-unit
137 ;;                 (if (char-ref-p cell)
138 ;;                     (plist-get cell :char)
139 ;;                   cell)))
140 ;;              ids-list ""))
141                      
142 (define-obsolete-function-alias
143   'ids-format-list 'ideographic-structure-to-ids)
144
145 ;;; @ End.
146 ;;;
147
148 (provide 'ids)
149
150 ;;; ids.el ends here