(chise-tex-coded-charset-expression-alist): Add settings for
[chise/omega.git] / chise2otf / elisp / chise-tex.el
1 ;;; chise-tex.el --- Coding-system based chise2otf like tool
2
3 ;; Copyright (C) 2004 MORIOKA Tomohiko
4
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: OTF package, pTeX, CHISE, Multiscript, Multilingual
7
8 ;; This file is a part of Omega/CHISE.
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 ;;; Code:
26
27 (defvar chise-tex-coded-charset-expression-alist
28   '((=ucs-hangul "UCShang"      4 X)
29     (=ucs@gb    "UCSgb"         4 X)
30     (=ucs@jis   "UCSjis"        4 X)
31     (=ucs@jis/2000 "UCSjis"     4 X)
32     (=ucs@ks    "UCSks"         4 X)
33     (=gt-pj-1   "GTpj1"         4 X)
34     (=gt-pj-2   "GTpj2"         4 X)
35     (=gt-pj-3   "GTpj3"         4 X)
36     (=gt-pj-4   "GTpj4"         4 X)
37     (=gt-pj-5   "GTpj5"         4 X)
38     (=gt-pj-6   "GTpj6"         4 X)
39     (=gt-pj-7   "GTpj7"         4 X)
40     (=gt-pj-8   "GTpj8"         4 X)
41     (=gt-pj-9   "GTpj9"         4 X)
42     (=gt-pj-10  "GTpj10"        4 X)
43     (=gt-pj-11  "GTpj11"        4 X)
44     (=ucs@cns   "UCScns"        4 X)))
45
46 (defun chise-tex-encode-region-for-gb (start end)
47   (interactive "r")
48   (save-excursion
49     (save-restriction
50       (narrow-to-region start end)
51       (goto-char start)
52       (let (chr ret rest spec)
53         (while (and (skip-chars-forward "\x00-\xFF")
54                     (not (eobp)))
55           (setq chr (char-after))
56           (cond ((memq chr '(?\e$(O#@\e(B))
57                  (delete-char)
58                  (insert (format "\\UCSjis{%04X}"
59                                  (encode-char chr '=ucs@jis)))
60                  )
61                 ((and (setq ret (encode-char chr '=jis-x0208-1983))
62                       (< ret #x3021))
63                  (forward-char))
64                 ;; ((setq ret (encode-char chr '=jis-x0208-1990))
65                 ;;  (delete-char)
66                 ;;  (insert (decode-char '=jis-x0208-1983 ret)))
67                 ((catch 'tag
68                    (setq rest chise-tex-coded-charset-expression-alist)
69                    (while (setq spec (car rest))
70                      (if (setq ret (encode-char chr (car spec)))
71                          (throw 'tag ret))
72                      (setq rest (cdr rest))))
73                  (delete-char)
74                  ;; (if (eq (char-before) ?\e$B!T\e(B)
75                  ;;     (insert " "))
76                  (insert (format (format "\\%s{%%0%d%s}"
77                                          (nth 1 spec)
78                                          (nth 2 spec)
79                                          (nth 3 spec))
80                                  ret)))
81                 (t
82                  (forward-char))))))))
83
84 (defun chise-tex-encode-region-for-jis (start end)
85   (interactive "r")
86   (save-excursion
87     (save-restriction
88       (narrow-to-region start end)
89       (goto-char start)
90       (let (chr ret rest spec)
91         (while (and (skip-chars-forward "\x00-\xFF")
92                     (not (eobp)))
93           (setq chr (char-after))
94           (cond ((encode-char chr '=jis-x0208-1983)
95                  (forward-char))
96                 ;; ((setq ret (encode-char chr '=jis-x0208-1990))
97                 ;;  (delete-char)
98                 ;;  (insert (decode-char '=jis-x0208-1983 ret)))
99                 ((and (or (encode-char chr '=jis-x0213-1-2000)
100                           (encode-char chr '=jis-x0213-2-2000))
101                       (setq ret (or (encode-char chr '=ucs@jis/2000)
102                                     (encode-char chr '=ucs@jis/fw))))
103                  (delete-char)
104                  ;; (if (eq (char-before) ?\e$B!T\e(B)
105                  ;;     (insert " "))
106                  (insert (format "\\UCSjis{%04X}" ret)))
107                 ((setq ret (encode-char chr '=ucs-hangul))
108                  (delete-char)
109                  ;; (if (eq (char-before) ?\e$B!T\e(B)
110                  ;;     (insert " "))
111                  (insert (format "\\UCSks{%04X}" ret)))
112                 ((eq chr ?\u2022)
113                  (delete-char)
114                  (insert "\\textbullet{}"))
115                 ((eq chr ?\u0294)
116                  (delete-char)
117                  (insert "\\UCSjis{0294}"))
118                 ((and (encode-char chr '=ucs@jp)
119                       (setq ret (char-representative-of-domain chr 'gb))
120                       (setq ret (encode-char ret '=ucs@gb)))
121                  (delete-char)
122                  ;; (if (eq (char-before) ?\e$B!T\e(B)
123                  ;;     (insert " "))
124                  (insert (format "\\UCSgb{%04X}" ret)))
125                 ((catch 'tag
126                    (setq rest chise-tex-coded-charset-expression-alist)
127                    (while (setq spec (car rest))
128                      (if (setq ret (encode-char chr (car spec)))
129                          (throw 'tag ret))
130                      (setq rest (cdr rest))))
131                  (delete-char)
132                  ;; (if (eq (char-before) ?\e$B!T\e(B)
133                  ;;     (insert " "))
134                  (insert (format (format "\\%s{%%0%d%s}"
135                                          (nth 1 spec)
136                                          (nth 2 spec)
137                                          (nth 3 spec))
138                                  ret)))
139                 (t
140                  (forward-char))))))))
141
142 (defun chise-tex-decode-region (start end)
143   (interactive "r")
144   (save-excursion
145     (save-restriction
146       (narrow-to-region start end)
147       (goto-char start)
148       (let (macro code ret me rest spec)
149         (while (re-search-forward "\\\\\\([a-zA-Z0-9]+\\){\\([0-9A-Fa-f]+\\)}"
150                                   nil t)
151           (setq macro (match-string 1)
152                 code (match-string 2)
153                 me (match-end 0))
154           (if (and (catch 'tag
155                      (setq rest chise-tex-coded-charset-expression-alist)
156                      (while (setq spec (car rest))
157                        (if (string= (nth 1 spec) macro)
158                            (throw 'tag spec))
159                        (setq rest (cdr rest))))
160                    (setq ret (decode-char (car spec)
161                                           (string-to-int
162                                            code
163                                            (if (eq (nth 3 spec) 'X)
164                                                16)))))
165               (progn
166                 (delete-region (match-beginning 0)(match-end 0))
167                 (insert ret))
168             (goto-char me)))))))
169
170 (make-coding-system
171  'iso-2022-jp-tex-gb 'iso2022
172  "ISO-2022-JP with TeX representation for GB fonts."
173  '(charset-g0 ascii
174    short t
175    seven t
176    input-charset-conversion ((latin-jisx0201 ascii)
177                              (japanese-jisx0208-1978 japanese-jisx0208))
178    pre-write-conversion chise-tex-encode-region-for-gb
179    post-read-conversion chise-tex-decode-region
180    mnemonic "pTeX(GB)/7bit"
181    ))
182
183 (make-coding-system
184  'iso-2022-jp-tex-jis 'iso2022
185  "ISO-2022-JP with TeX representation for JIS fonts."
186  '(charset-g0 ascii
187    short t
188    seven t
189    input-charset-conversion ((latin-jisx0201 ascii)
190                              (japanese-jisx0208-1978 japanese-jisx0208))
191    pre-write-conversion chise-tex-encode-region-for-jis
192    post-read-conversion chise-tex-decode-region
193    mnemonic "pTeX(JIS)/7bit"
194    ))
195
196
197 ;;; @ End.
198 ;;;
199
200 (provide 'chise-tex)
201
202 ;;; chise-tex.el ends here