1 ;;; tibet-util.el --- utilities for Tibetan -*- coding: iso-2022-7bit; -*-
3 ;; Copyright (C) 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 ;; Free Software Foundation, Inc.
5 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
6 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011
7 ;; National Institute of Advanced Industrial Science and Technology (AIST)
8 ;; Registration Number H14PRO021
10 ;; Author: Toru TOMABECHI <Toru.Tomabechi@orient.unil.ch>
11 ;; Keywords: multilingual, Tibetan
12 ;; Created: Feb. 17. 1997
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software: you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 ;; 1997.03.13 Modification in treatment of text properties;
31 ;; Support for some special signs and punctuations.
32 ;; 1999.10.25 Modification for a new composition way by K.Handa.
38 (defconst tibetan-obsolete-glyphs
39 `(("\e$(7!=\e(B" .
"\e$(7!=\e(B") ; 2 col <-> 1 col
40 ("\e$(7!?\e(B" .
"\e$(7!?\e(B")
41 ("\e$(7!@\e(B" .
"\e$(7!@\e(B")
42 ("\e$(7!A\e(B" .
"\e$(7!A\e(B")
43 ("\e$(7"`\e(B" . "\e$
(7"`\e(B")
44 ("\e$(7!;\e(B" .
"\e$(7!;\e(B")
45 ("\e$(7!D\e(B" .
"\e$(7!D\e(B")
46 ;; Yes these are dirty. But ...
47 ("\e$(7!>\e(B \e$(7!>\e(B" .
,(compose-string "\e$(7!>\e(B \e$(7!>\e(B" 0 3 [?
\e$
(7!>\e(B (Br . Bl
) ?
(Br . Bl
) ?
\e$
(7!>\e(B]))
48 ("\e$(7!4!5!5\e(B" .
,(compose-string
49 "\e$(7#R#S#S#S\e(B" 0 4
50 [?
\e$
(7#R
\e(B (Br . Bl
) ?
\e$
(7#S
\e(B (Br . Bl
) ?
\e$
(7#S
\e(B (Br . Bl
) ?
\e$
(7#S
\e(B]))
51 ("\e$(7!4!5\e(B" .
,(compose-string "\e$(7#R#S#S\e(B" 0 3 [?
\e$
(7#R
\e(B (Br . Bl
) ?
\e$
(7#S
\e(B (Br . Bl
) ?
\e$
(7#S
\e(B]))
52 ("\e$(7!6\e(B" .
,(compose-string "\e$(7#R#S!I\e(B" 0 3 [?
\e$
(7#R
\e(B (Br . Bl
) ?
\e$
(7#S
\e(B (br . tr
) ?
\e$
(7!I
\e(B]))
53 ("\e$(7!4\e(B" .
,(compose-string "\e$(7#R#S\e(B" 0 2 [?
\e$
(7#R
\e(B (Br . Bl
) ?
\e$
(7#S
\e(B]))))
56 (defun tibetan-char-p (ch)
57 "Check if char CH is Tibetan character.
58 Returns non-nil if CH is Tibetan. Otherwise, returns nil."
59 (memq (char-charset ch
) '(tibetan tibetan-1-column
)))
61 ;;; Functions for Tibetan <-> Tibetan-transcription.
64 (defun tibetan-tibetan-to-transcription (str)
65 "Transcribe Tibetan string STR and return the corresponding Roman string."
66 (let (;; Accumulate transcriptions here in reverse order.
72 (let ((idx (string-match tibetan-precomposition-rule-regexp str i
)))
74 ;; Ith character and the followings matches precomposable
79 (cdr (assoc (match-string 0 str
)
80 tibetan-precomposition-rule-alist
))
81 tibetan-precomposed-transcription-alist
)))
82 (setq ch
(substring str i
(1+ i
))
85 (car (or (rassoc ch tibetan-consonant-transcription-alist
)
86 (rassoc ch tibetan-vowel-transcription-alist
)
87 (rassoc ch tibetan-subjoined-transcription-alist
)))))
88 (setq trans
(cons this-trans trans
))))
89 (apply 'concat
(nreverse trans
))))
92 (defun tibetan-transcription-to-tibetan (str)
93 "Convert Tibetan Roman string STR to Tibetan character string.
94 The returned string has no composition information."
95 (let (;; Case is significant.
96 (case-fold-search nil
)
98 ;; Accumulate Tibetan strings here in reverse order.
101 (while (setq i
(string-match tibetan-regexp str idx
))
103 ;; STR contains a pattern that doesn't match Tibetan
104 ;; transcription. Include the pattern as is.
105 (setq t-str-list
(cons (substring str idx i
) t-str-list
)))
106 (setq subtrans
(match-string 0 str
)
108 (let ((t-char (cdr (assoc subtrans
109 tibetan-precomposed-transcription-alist
))))
111 ;; SUBTRANS corresponds to a transcription for
112 ;; precomposable Tibetan sequence.
113 (setq t-char
(car (rassoc t-char
114 tibetan-precomposition-rule-alist
)))
117 (or (assoc subtrans tibetan-consonant-transcription-alist
)
118 (assoc subtrans tibetan-vowel-transcription-alist
)
119 (assoc subtrans tibetan-modifier-transcription-alist
)
120 (assoc subtrans tibetan-subjoined-transcription-alist
)))))
121 (setq t-str-list
(cons t-char t-str-list
))))
122 (if (< idx
(length str
))
123 (setq t-str-list
(cons (substring str idx
) t-str-list
)))
124 (apply 'concat
(nreverse t-str-list
))))
127 ;;; Functions for composing/decomposing Tibetan sequence.
129 ;;; A Tibetan syllable is typically structured as follows:
131 ;;; [Prefix] C [C+] V [M] [Suffix [Post suffix]]
133 ;;; where C's are all vertically stacked, V appears below or above
134 ;;; consonant cluster and M is always put above the C[C+]V combination.
135 ;;; (Sanskrit visarga, though it is a vowel modifier, is considered
136 ;;; to be a punctuation.)
138 ;;; Here are examples of the words "bsgrubs" and "hfauM"
140 ;;; \e$(7"7"G###C"U"7"G\e(B \e$(7"H"R"U"_\e(B
148 ;;; Consonants `'' (\e$(7"A\e(B), `w' (\e$(7">\e(B), `y' (\e$(7"B\e(B), `r' (\e$(7"C\e(B) take special
149 ;;; forms when they are used as subjoined consonant. Consonant `r'
150 ;;; takes another special form when used as superjoined in such a case
151 ;;; as "rka", while it does not change its form when conjoined with
152 ;;; subjoined `'', `w' or `y' as in "rwa", "rya".
154 ;; Append a proper composition rule and glyph to COMPONENTS to compose
155 ;; CHAR with a composition that has COMPONENTS.
157 (defun tibetan-add-components (components char
)
158 (let ((last (last components
))
159 (stack-upper '(tc . bc
))
160 (stack-under '(bc . tc
))
162 ;; Special treatment for 'a chung.
163 ;; If 'a follows a consonant, turn it into the subjoined form.
164 ;; * Disabled by Tomabechi 2000/06/09 *
165 ;; Because in Unicode, \e$(7"A\e(B may follow directly a consonant without
166 ;; any intervening vowel, as in \e$(7"9"""Q"A!;\e(B=\e$(7"9\e(B \e$(7""\e(B \e$(7"A\e(B not \e$(7"9\e(B \e$(7""\e(B \e$(7"Q\e(B \e$(7"A\e(B
167 ;;(if (and (= char ?\e$(7"A\e(B)
168 ;; (aref (char-category-set (car last)) ?0))
169 ;; (setq char ?\e$(7"R\e(B)) ;; modified for new font by Tomabechi 1999/12/10
171 ;; Composite vowel signs are decomposed before being added
172 ;; Added by Tomabechi 2000/06/08
173 (if (memq char
'(?
\e$
(7"T\e(B ?\e$(7"V
\e(B ?
\e$
(7"W\e(B ?\e$(7"X
\e(B ?
\e$
(7"Y\e(B ?\e$(7"Z
\e(B ?
\e$
(7"b\e(B))
176 (cddr (assoc (char-to-string char)
177 tibetan-composite-vowel-alist)))
179 (cadr (assoc (char-to-string char)
180 tibetan-composite-vowel-alist))))
182 ;; Compose upper vowel sign vertically over.
183 ((aref (char-category-set char) ?2)
184 (setq rule stack-upper))
186 ;; Compose lower vowel sign vertically under.
187 ((aref (char-category-set char) ?3)
188 (if (or (eq char ?\e$(7"Q
\e(B) ;; `\e$(7"Q\e(B' and `\e$,1FP\e(B' should not visible when composed.
191 (setq rule stack-under
)))
192 ;; Transform ra-mgo (superscribed r) if followed by a subjoined
193 ;; consonant other than w, ', y, r.
194 ((and (= (car last
) ?
\e$
(7"C\e(B)
195 (not (memq char '(?\e$(7#>\e(B ?\e$(7"R
\e(B ?
\e$
(7#B
\e(B ?
\e$
(7#C
\e(B))))
196 (setcar last ?
\e$
(7!"\e(B) ;; modified for newfont by Tomabechi 1999/12/10
197 (setq rule stack-under))
198 ;; Transform initial base consonant if followed by a subjoined
201 (let ((laststr (char-to-string (car last))))
202 (if (and (/= char ?\e$(7"R
\e(B) ;; modified for new font by Tomabechi
203 (string-match "[\e$(7"!\e(B-\e$
(7"="?
"@"D
\e(B-\e$
(7"J"K
\e(B]" laststr))
204 (setcar last (string-to-char
205 (cdr (assoc (char-to-string (car last))
206 tibetan-base-to-subjoined-alist)))))
207 (setq rule stack-under))))
210 (setcdr last (list rule char)))
211 ;; Added by Tomabechi 2000/06/08
213 (nconc last comp-vowel))
217 (defun tibetan-compose-string (str)
218 "Compose Tibetan string STR.
"
220 ;; `\e$(7"A
\e(B' is included in the pattern for subjoined consonants
221 ;; because we treat it specially in tibetan-add-components.
222 ;; (This feature is removed by Tomabechi 2000/06/08)
223 (while (setq idx
(string-match tibetan-composable-pattern str idx
))
227 (if (eq (string-match tibetan-precomposition-rule-regexp str idx
) idx
)
228 (setq idx
(match-end 0)
230 (list (string-to-char
232 (assoc (match-string 0 str
)
233 tibetan-precomposition-rule-alist
)))))
234 (setq components
(list (aref str idx
))
237 (tibetan-add-components components
(aref str idx
))
239 (compose-string str from to components
))))
243 (defun tibetan-compose-region (beg end
)
244 "Compose Tibetan text the region BEG and END."
246 (let (str result chars
)
249 (narrow-to-region beg end
)
250 (goto-char (point-min))
251 ;; `\e$(7"A\e(B' is included in the pattern for subjoined consonants
252 ;; because we treat it specially in tibetan-add-components.
253 ;; (This feature is removed by Tomabechi 2000/06/08)
254 (while (re-search-forward tibetan-composable-pattern nil t
)
255 (let ((from (match-beginning 0))
259 (if (looking-at tibetan-precomposition-rule-regexp
)
262 (list (string-to-char
264 (assoc (match-string 0)
265 tibetan-precomposition-rule-alist
)))))
266 (goto-char (match-end 0)))
267 (setq components
(list (char-after from
)))
269 (while (< (point) to
)
270 (tibetan-add-components components
(following-char))
272 (compose-region from to components
)))))))
274 (defvar tibetan-decompose-precomposition-alist
275 (mapcar (function (lambda (x) (cons (string-to-char (cdr x
)) (car x
))))
276 tibetan-precomposition-rule-alist
))
279 (defun tibetan-decompose-region (from to
)
280 "Decompose Tibetan text in the region FROM and TO.
281 This is different from decompose-region because precomposed Tibetan characters
282 are decomposed into normal Tibetan character sequences."
285 (narrow-to-region from to
)
286 (decompose-region from to
)
289 (let* ((char (following-char))
290 (slot (assq char tibetan-decompose-precomposition-alist
)))
295 (forward-char 1))))))
299 (defun tibetan-decompose-string (str)
300 "Decompose Tibetan string STR.
301 This is different from decompose-string because precomposed Tibetan characters
302 are decomposed into normal Tibetan character sequences."
308 (setq char
(aref str idx
)
309 slot
(assq (aref str idx
) tibetan-decompose-precomposition-alist
)
310 new
(concat new
(if slot
(cdr slot
) (char-to-string char
)))
315 ;;; This variable is used to avoid repeated decomposition.
317 (setq-default tibetan-decomposed nil
)
320 (defun tibetan-decompose-buffer ()
321 "Decomposes Tibetan characters in the buffer into their components.
322 See also the documentation of the function `tibetan-decompose-region'."
324 (make-local-variable 'tibetan-decomposed
)
325 (cond ((not tibetan-decomposed
)
326 (tibetan-decompose-region (point-min) (point-max))
327 (setq tibetan-decomposed t
))))
330 (defun tibetan-compose-buffer ()
331 "Composes Tibetan character components in the buffer.
332 See also docstring of the function tibetan-compose-region."
334 (make-local-variable 'tibetan-decomposed
)
335 (tibetan-compose-region (point-min) (point-max))
336 (setq tibetan-decomposed nil
))
339 (defun tibetan-post-read-conversion (len)
342 (let ((buffer-modified-p (buffer-modified-p)))
343 (narrow-to-region (point) (+ (point) len
))
344 (tibetan-compose-region (point-min) (point-max))
345 (set-buffer-modified-p buffer-modified-p
)
346 (make-local-variable 'tibetan-decomposed
)
347 (setq tibetan-decomposed nil
)
348 (- (point-max) (point-min))))))
352 (defun tibetan-pre-write-conversion (from to
)
353 (setq tibetan-decomposed-temp tibetan-decomposed
)
354 (let ((old-buf (current-buffer)))
355 (set-buffer (generate-new-buffer " *temp*"))
358 (insert-buffer-substring old-buf from to
))
359 (if (not tibetan-decomposed-temp
)
360 (tibetan-decompose-region (point-min) (point-max)))
361 ;; Should return nil as annotations.
366 ;;; Unicode-related definitions.
369 (defvar tibetan-canonicalize-for-unicode-alist
370 '(("\e$(7"Q
\e(B" . "") ;; remove vowel a
371 ("\e$
(7"T\e(B" .
"\e$(7"R
"S\e(B") ;; decompose vowels whose use is ``discouraged'' in Unicode 3.0
372 ("\e$(7"V
\e(B" . "\e$
(7"R"U
\e(B")
373 ("\e$
(7"W\e(B" .
"\e$(7#C"a
\e(B")
374 ("\e$
(7"X\e(B" .
"\e$(7#C"R
"a\e(B")
375 ("\e$(7"Y
\e(B" . "\e$
(7#D
"a\e(B")
376 ("\e$(7"Z
\e(B" . "\e$
(7#D
"R"a
\e(B")
377 ("\e$
(7"b\e(B" .
"\e$(7"R
"a\e(B"))
378 "Rules for canonicalizing Tibetan vowels for Unicode.")
380 (defvar tibetan-canonicalize-for-unicode-regexp
381 "[\e$(7"Q
"T"V
"W"X
"Y"Z
"b\e(B]"
382 "Regexp for Tibetan vowels to be canonicalized in Unicode.")
384 (defun tibetan-canonicalize-for-unicode-region (from to
)
386 (narrow-to-region from to
)
388 (while (re-search-forward tibetan-canonicalize-for-unicode-regexp nil t
)
390 ;;(from (match-beginning 0))
393 (cdr (assoc (match-string 0)
394 tibetan-canonicalize-for-unicode-alist
))))
396 ;;(delete-region from to)
397 ;;(insert canonical-form)
398 (replace-match canonical-form
)
401 (defvar tibetan-strict-unicode t
402 "*Flag to control Tibetan canonicalizing for Unicode.
404 If non-nil, the vowel a is removed and composite vowels are decomposed
405 before writing buffer in Unicode. See also
406 `tibetan-canonicalize-for-unicode-regexp' and
407 `tibetan-canonicalize-for-unicode-alist'.")
410 (defun tibetan-pre-write-canonicalize-for-unicode (from to
)
411 (let ((old-buf (current-buffer))
412 (strict-unicode tibetan-strict-unicode
))
413 (set-buffer (generate-new-buffer " *temp*"))
416 (insert-buffer-substring old-buf from to
))
418 (tibetan-canonicalize-for-unicode-region (point-min) (point-max)))
419 ;; Should return nil as annotations.
422 (provide 'tibet-util
)
424 ;; arch-tag: 7a7333e8-1584-446c-b39c-a02b9def265d
425 ;;; tibet-util.el ends here