1 ;;; tibet-util.el --- utilities for Tibetan -*- coding: iso-2022-7bit; -*-
3 ;; Copyright (C) 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 ;; Free Software Foundation, Inc.
5 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
6 ;; 2005, 2006, 2007, 2008
7 ;; National Institute of Advanced Industrial Science and Technology (AIST)
8 ;; Registration Number H14PRO021
10 ;; Keywords: multilingual, Tibetan
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 3, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
29 ;; Author: Toru TOMABECHI, <Toru.Tomabechi@orient.unil.ch>
31 ;; Created: Feb. 17. 1997
34 ;; 1997.03.13 Modification in treatment of text properties;
35 ;; Support for some special signs and punctuations.
36 ;; 1999.10.25 Modification for a new composition way by K.Handa.
42 (defconst tibetan-obsolete-glyphs
43 `(("
\e$(7!=
\e(B" . "
\e$(7!=
\e(B") ; 2 col <-> 1 col
44 ("
\e$(7!?
\e(B" . "
\e$(7!?
\e(B")
45 ("
\e$(7!@
\e(B" . "
\e$(7!@
\e(B")
46 ("
\e$(7!A
\e(B" . "
\e$(7!A
\e(B")
47 ("
\e$(7"`
\e(B" . "
\e$(7"`
\e(B")
48 ("
\e$(7!;
\e(B" . "
\e$(7!;
\e(B")
49 ("
\e$(7!D
\e(B" . "
\e$(7!D
\e(B")
50 ;; Yes these are dirty. But ...
51 ("
\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]))
52 ("
\e$(7!4!5!5
\e(B" . ,(compose-string
53 "
\e$(7#R#S#S#S
\e(B" 0 4
54 [?
\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]))
55 ("
\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]))
56 ("
\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]))
57 ("
\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]))))
60 (defun tibetan-char-p (ch)
61 "Check if char CH is Tibetan character.
62 Returns non-nil if CH is Tibetan. Otherwise, returns nil."
63 (memq (char-charset ch) '(tibetan tibetan-1-column)))
65 ;;; Functions for Tibetan <-> Tibetan-transcription.
68 (defun tibetan-tibetan-to-transcription (str)
69 "Transcribe Tibetan string STR and return the corresponding Roman string."
70 (let (;; Accumulate transcriptions here in reverse order.
76 (let ((idx (string-match tibetan-precomposition-rule-regexp str i)))
78 ;; Ith character and the followings matches precomposable
83 (cdr (assoc (match-string 0 str)
84 tibetan-precomposition-rule-alist))
85 tibetan-precomposed-transcription-alist)))
86 (setq ch (substring str i (1+ i))
89 (car (or (rassoc ch tibetan-consonant-transcription-alist)
90 (rassoc ch tibetan-vowel-transcription-alist)
91 (rassoc ch tibetan-subjoined-transcription-alist)))))
92 (setq trans (cons this-trans trans))))
93 (apply 'concat (nreverse trans))))
96 (defun tibetan-transcription-to-tibetan (str)
97 "Convert Tibetan Roman string STR to Tibetan character string.
98 The returned string has no composition information."
99 (let (;; Case is significant.
100 (case-fold-search nil)
102 ;; Accumulate Tibetan strings here in reverse order.
105 (while (setq i (string-match tibetan-regexp str idx))
107 ;; STR contains a pattern that doesn't match Tibetan
108 ;; transcription. Include the pattern as is.
109 (setq t-str-list (cons (substring str idx i) t-str-list)))
110 (setq subtrans (match-string 0 str)
112 (let ((t-char (cdr (assoc subtrans
113 tibetan-precomposed-transcription-alist))))
115 ;; SUBTRANS corresponds to a transcription for
116 ;; precomposable Tibetan sequence.
117 (setq t-char (car (rassoc t-char
118 tibetan-precomposition-rule-alist)))
121 (or (assoc subtrans tibetan-consonant-transcription-alist)
122 (assoc subtrans tibetan-vowel-transcription-alist)
123 (assoc subtrans tibetan-modifier-transcription-alist)
124 (assoc subtrans tibetan-subjoined-transcription-alist)))))
125 (setq t-str-list (cons t-char t-str-list))))
126 (if (< idx (length str))
127 (setq t-str-list (cons (substring str idx) t-str-list)))
128 (apply 'concat (nreverse t-str-list))))
131 ;;; Functions for composing/decomposing Tibetan sequence.
133 ;;; A Tibetan syllable is typically structured as follows:
135 ;;; [Prefix] C [C+] V [M] [Suffix [Post suffix]]
137 ;;; where C's are all vertically stacked, V appears below or above
138 ;;; consonant cluster and M is always put above the C[C+]V combination.
139 ;;; (Sanskrit visarga, though it is a vowel modifier, is considered
140 ;;; to be a punctuation.)
142 ;;; Here are examples of the words "bsgrubs" and "hfauM"
144 ;;;
\e$(7"7"G###C"U"7"G
\e(B
\e$(7"H"R"U"_
\e(B
152 ;;; 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
153 ;;; forms when they are used as subjoined consonant. Consonant `r'
154 ;;; takes another special form when used as superjoined in such a case
155 ;;; as "rka", while it does not change its form when conjoined with
156 ;;; subjoined `'', `w' or `y' as in "rwa", "rya".
158 ;; Append a proper composition rule and glyph to COMPONENTS to compose
159 ;; CHAR with a composition that has COMPONENTS.
161 (defun tibetan-add-components (components char)
162 (let ((last (last components))
163 (stack-upper '(tc . bc))
164 (stack-under '(bc . tc))
166 ;; Special treatment for 'a chung.
167 ;; If 'a follows a consonant, turn it into the subjoined form.
168 ;; * Disabled by Tomabechi 2000/06/09 *
169 ;; Because in Unicode,
\e$(7"A
\e(B may follow directly a consonant without
170 ;; 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
171 ;;(if (and (= char ?
\e$(7"A
\e(B)
172 ;; (aref (char-category-set (car last)) ?0))
173 ;; (setq char ?
\e$(7"R
\e(B)) ;; modified for new font by Tomabechi 1999/12/10
175 ;; Composite vowel signs are decomposed before being added
176 ;; Added by Tomabechi 2000/06/08
177 (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))
180 (cddr (assoc (char-to-string char)
181 tibetan-composite-vowel-alist)))
183 (cadr (assoc (char-to-string char)
184 tibetan-composite-vowel-alist))))
186 ;; Compose upper vowel sign vertically over.
187 ((aref (char-category-set char) ?2)
188 (setq rule stack-upper))
190 ;; Compose lower vowel sign vertically under.
191 ((aref (char-category-set char) ?3)
192 (if (or (eq char ?
\e$(7"Q
\e(B) ;; `
\e$(7"Q
\e(B' and `
\e$,1FP
\e(B' should not visible when composed.
195 (setq rule stack-under)))
196 ;; Transform ra-mgo (superscribed r) if followed by a subjoined
197 ;; consonant other than w, ', y, r.
198 ((and (= (car last) ?
\e$(7"C
\e(B)
199 (not (memq char '(?
\e$(7#>
\e(B ?
\e$(7"R
\e(B ?
\e$(7#B
\e(B ?
\e$(7#C
\e(B))))
200 (setcar last ?
\e$(7!"
\e(B) ;; modified for newfont by Tomabechi 1999/12/10
201 (setq rule stack-under))
202 ;; Transform initial base consonant if followed by a subjoined
205 (let ((laststr (char-to-string (car last))))
206 (if (and (/= char ?
\e$(7"R
\e(B) ;; modified for new font by Tomabechi
207 (string-match "[
\e$(7"!
\e(B-
\e$(7"="?"@"D
\e(B-
\e$(7"J"K
\e(B]" laststr))
208 (setcar last (string-to-char
209 (cdr (assoc (char-to-string (car last))
210 tibetan-base-to-subjoined-alist)))))
211 (setq rule stack-under))))
214 (setcdr last (list rule char)))
215 ;; Added by Tomabechi 2000/06/08
217 (nconc last comp-vowel))
221 (defun tibetan-compose-string (str)
222 "Compose Tibetan string STR."
224 ;; `
\e$(7"A
\e(B' is included in the pattern for subjoined consonants
225 ;; because we treat it specially in tibetan-add-components.
226 ;; (This feature is removed by Tomabechi 2000/06/08)
227 (while (setq idx (string-match tibetan-composable-pattern str idx))
231 (if (eq (string-match tibetan-precomposition-rule-regexp str idx) idx)
232 (setq idx (match-end 0)
234 (list (string-to-char
236 (assoc (match-string 0 str)
237 tibetan-precomposition-rule-alist)))))
238 (setq components (list (aref str idx))
241 (tibetan-add-components components (aref str idx))
243 (compose-string str from to components))))
247 (defun tibetan-compose-region (beg end)
248 "Compose Tibetan text the region BEG and END."
250 (let (str result chars)
253 (narrow-to-region beg end)
254 (goto-char (point-min))
255 ;; `
\e$(7"A
\e(B' is included in the pattern for subjoined consonants
256 ;; because we treat it specially in tibetan-add-components.
257 ;; (This feature is removed by Tomabechi 2000/06/08)
258 (while (re-search-forward tibetan-composable-pattern nil t)
259 (let ((from (match-beginning 0))
263 (if (looking-at tibetan-precomposition-rule-regexp)
266 (list (string-to-char
268 (assoc (match-string 0)
269 tibetan-precomposition-rule-alist)))))
270 (goto-char (match-end 0)))
271 (setq components (list (char-after from)))
273 (while (< (point) to)
274 (tibetan-add-components components (following-char))
276 (compose-region from to components)))))))
278 (defvar tibetan-decompose-precomposition-alist
279 (mapcar (function (lambda (x) (cons (string-to-char (cdr x)) (car x))))
280 tibetan-precomposition-rule-alist))
283 (defun tibetan-decompose-region (from to)
284 "Decompose Tibetan text in the region FROM and TO.
285 This is different from decompose-region because precomposed Tibetan characters
286 are decomposed into normal Tibetan character sequences."
289 (narrow-to-region from to)
290 (decompose-region from to)
293 (let* ((char (following-char))
294 (slot (assq char tibetan-decompose-precomposition-alist)))
299 (forward-char 1))))))
303 (defun tibetan-decompose-string (str)
304 "Decompose Tibetan string STR.
305 This is different from decompose-string because precomposed Tibetan characters
306 are decomposed into normal Tibetan character sequences."
312 (setq char (aref str idx)
313 slot (assq (aref str idx) tibetan-decompose-precomposition-alist)
314 new (concat new (if slot (cdr slot) (char-to-string char)))
319 (defun tibetan-composition-function (pos &optional string)
321 (if auto-compose-current-font
322 (if (eq (string-match "[
\e$(7!0
\e(B-
\e$,1GQ
\e(B]+" pos) pos)
323 (or (font-shape-text 0 (match-end 0) auto-compose-current-font
327 (if auto-compose-current-font
328 (if (looking-at "[
\e$(7!0
\e(B-
\e$,1GQ
\e(B]+")
329 (or (font-shape-text pos (match-end 0) auto-compose-current-font)
331 (if (looking-at tibetan-composable-pattern)
333 (tibetan-compose-region pos (match-end 0))))))))
336 ;;; This variable is used to avoid repeated decomposition.
338 (setq-default tibetan-decomposed nil)
341 (defun tibetan-decompose-buffer ()
342 "Decomposes Tibetan characters in the buffer into their components.
343 See also the documentation of the function `tibetan-decompose-region'."
345 (make-local-variable 'tibetan-decomposed)
346 (cond ((not tibetan-decomposed)
347 (tibetan-decompose-region (point-min) (point-max))
348 (setq tibetan-decomposed t))))
351 (defun tibetan-compose-buffer ()
352 "Composes Tibetan character components in the buffer.
353 See also docstring of the function tibetan-compose-region."
355 (make-local-variable 'tibetan-decomposed)
356 (tibetan-compose-region (point-min) (point-max))
357 (setq tibetan-decomposed nil))
360 (defun tibetan-post-read-conversion (len)
363 (let ((buffer-modified-p (buffer-modified-p)))
364 (narrow-to-region (point) (+ (point) len))
365 (tibetan-compose-region (point-min) (point-max))
366 (set-buffer-modified-p buffer-modified-p)
367 (make-local-variable 'tibetan-decomposed)
368 (setq tibetan-decomposed nil)
369 (- (point-max) (point-min))))))
373 (defun tibetan-pre-write-conversion (from to)
374 (setq tibetan-decomposed-temp tibetan-decomposed)
375 (let ((old-buf (current-buffer)))
376 (set-buffer (generate-new-buffer " *temp*"))
379 (insert-buffer-substring old-buf from to))
380 (if (not tibetan-decomposed-temp)
381 (tibetan-decompose-region (point-min) (point-max)))
382 ;; Should return nil as annotations.
387 ;;; Unicode-related definitions.
390 (defvar tibetan-canonicalize-for-unicode-alist
391 '(("
\e$(7"Q
\e(B" . "") ;; remove vowel a
392 ("
\e$(7"T
\e(B" . "
\e$(7"R"S
\e(B") ;; decompose vowels whose use is ``discouraged'' in Unicode 3.0
393 ("
\e$(7"V
\e(B" . "
\e$(7"R"U
\e(B")
394 ("
\e$(7"W
\e(B" . "
\e$(7#C"a
\e(B")
395 ("
\e$(7"X
\e(B" . "
\e$(7#C"R"a
\e(B")
396 ("
\e$(7"Y
\e(B" . "
\e$(7#D"a
\e(B")
397 ("
\e$(7"Z
\e(B" . "
\e$(7#D"R"a
\e(B")
398 ("
\e$(7"b
\e(B" . "
\e$(7"R"a
\e(B"))
399 "Rules for canonicalizing Tibetan vowels for Unicode.")
401 (defvar tibetan-canonicalize-for-unicode-regexp
402 "[
\e$(7"Q"T"V"W"X"Y"Z"b
\e(B]"
403 "Regexp for Tibetan vowels to be canonicalized in Unicode.")
405 (defun tibetan-canonicalize-for-unicode-region (from to)
407 (narrow-to-region from to)
409 (while (re-search-forward tibetan-canonicalize-for-unicode-regexp nil t)
411 ;;(from (match-beginning 0))
414 (cdr (assoc (match-string 0)
415 tibetan-canonicalize-for-unicode-alist))))
417 ;;(delete-region from to)
418 ;;(insert canonical-form)
419 (replace-match canonical-form)
422 (defvar tibetan-strict-unicode t
423 "*Flag to control Tibetan canonicalizing for Unicode.
425 If non-nil, the vowel a is removed and composite vowels are decomposed
426 before writing buffer in Unicode. See also
427 `tibetan-canonicalize-for-unicode-regexp' and
428 `tibetan-canonicalize-for-unicode-alist'.")
431 (defun tibetan-pre-write-canonicalize-for-unicode (from to)
432 (let ((old-buf (current-buffer))
433 (strict-unicode tibetan-strict-unicode))
434 (set-buffer (generate-new-buffer " *temp*"))
437 (insert-buffer-substring old-buf from to))
439 (tibetan-canonicalize-for-unicode-region (point-min) (point-max)))
440 ;; Should return nil as annotations.
443 (provide 'tibet-util)
445 ;;; arch-tag: 7a7333e8-1584-446c-b39c-a02b9def265d
446 ;;; tibet-util.el ends here