a69ce6b8e4f465f81b175961107431c0f6a274d3
[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                     (eq (encode-char chr '=ucs-itaiji-001) #x2FF9))
50           (if (and ucs (<= #xE000 ucs)(<= ucs #xF8FF)
51                    (setq big5 (encode-char chr 'chinese-big5)))
52               (setq chr (decode-char '=big5-cdp big5)))
53           (cons chr
54                 (substring string 1))))))
55
56 (defun ids-parse-op-2 (string)
57   (if (>= (length string) 1)
58       (let* ((chr (aref string 0))
59              (ucs (encode-char chr '=ucs 'defined-only)))
60         (if (or (and ucs
61                      (or (eq ucs #x2FF0)
62                          (eq ucs #x2FF1)
63                          (and (<= #x2FF4 ucs)(<= ucs #x2FFB))))
64                 (eq (encode-char chr '=ucs-itaiji-001) #x2FF9))
65             (cons chr
66                   (substring string 1))))))
67
68 (defun ids-parse-op-3 (string)
69   (if (>= (length string) 1)
70       (let ((chr (aref string 0)))
71         (if (memq chr '(?\u2FF2 ?\u2FF3))
72             (cons chr
73                   (substring string 1))))))
74
75 (defun ids-parse-component (string simplify)
76   (let ((ret (ids-parse-element string simplify))
77         rret)
78     (when ret
79       (if (and simplify
80                (listp (car ret))
81                (setq rret (ideographic-structure-find-char
82                            (cdr (assq 'ideographic-structure (car ret))))))
83           (cons rret (cdr ret))
84         ret))))
85
86 (defun ids-parse-element (string simplify)
87   (let (ret op arg1 arg2 arg3)
88     (cond ((ids-parse-terminal string))
89           ((setq ret (ids-parse-op-2 string))
90            (setq op (car ret))
91            (when (setq ret (ids-parse-component (cdr ret) simplify))
92              (setq arg1 (car ret))
93              (when (setq ret (ids-parse-component (cdr ret) simplify))
94                (setq arg2 (car ret))
95                (cons (list (list 'ideographic-structure op arg1 arg2))
96                      (cdr ret)))))
97           ((setq ret (ids-parse-op-3 string))
98            (setq op (car ret))
99            (when (setq ret (ids-parse-component (cdr ret) simplify))
100              (setq arg1 (car ret))
101              (when (setq ret (ids-parse-component (cdr ret) simplify))
102                (setq arg2 (car ret))
103                (when (setq ret (ids-parse-component (cdr ret) simplify))
104                  (setq arg3 (car ret))
105                  (cons (list (list 'ideographic-structure op arg1 arg2 arg3))
106                        (cdr ret)))))))))
107
108 ;;;###autoload
109 (defun ids-parse-string (ids-string &optional simplify)
110   "Parse IDS-STRING and return the result."
111   (let ((ret (ids-parse-element ids-string simplify)))
112     (if (= (length (cdr ret)) 0)
113         (car ret))))
114
115 ;; (defun ids-format-unit (ids-char)
116 ;;   (let (ret)
117 ;;     (cond ((characterp ids-char)
118 ;;            (char-to-string ids-char))
119 ;;           ((integerp ids-char)
120 ;;            (char-to-string (decode-char 'ucs ids-char)))
121 ;;           ((setq ret (find-char ids-char))
122 ;;            (char-to-string ret))
123 ;;           ((setq ret (assq 'ideographic-structure ids-char))
124 ;;            (ids-format-list (cdr ret))))))
125
126 ;; ;;;###autoload
127 ;; (defun ids-format-list (ids-list)
128 ;;   "Format ideographic-structure IDS-LIST as an IDS-string."
129 ;;   (mapconcat (lambda (cell)
130 ;;                (ids-format-unit
131 ;;                 (if (char-ref-p cell)
132 ;;                     (plist-get cell :char)
133 ;;                   cell)))
134 ;;              ids-list ""))
135                      
136 (define-obsolete-function-alias
137   'ids-format-list 'ideographic-structure-to-ids)
138
139 ;;; @ End.
140 ;;;
141
142 (provide 'ids)
143
144 ;;; ids.el ends here