1 ;;; tibet-util.el --- utilities for Tibetan -*- coding: utf-8-emacs; -*-
3 ;; Copyright (C) 1997, 2001-2015 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011
6 ;; National Institute of Advanced Industrial Science and Technology (AIST)
7 ;; Registration Number H14PRO021
9 ;; Author: Toru TOMABECHI <Toru.Tomabechi@orient.unil.ch>
10 ;; Keywords: multilingual, Tibetan
11 ;; Created: Feb. 17. 1997
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; 1997.03.13 Modification in treatment of text properties;
30 ;; Support for some special signs and punctuation.
31 ;; 1999.10.25 Modification for a new composition way by K.Handa.
37 (defconst tibetan-obsolete-glyphs
38 `(("།" .
"།") ; 2 col <-> 1 col
45 ;; Yes these are dirty. But ...
46 ("༎ ༎" .
,(compose-string "༎ ༎" 0 3 [?༎
(Br . Bl
) ?
(Br . Bl
) ?༎
]))
47 ("༄༅༅" .
,(compose-string
49 [?࿁
(Br . Bl
) ?࿂
(Br . Bl
) ?࿂
(Br . Bl
) ?࿂
]))
50 ("༄༅" .
,(compose-string "࿁࿂࿂" 0 3 [?࿁
(Br . Bl
) ?࿂
(Br . Bl
) ?࿂
]))
51 ("༆" .
,(compose-string "࿁࿂༙" 0 3 [?࿁
(Br . Bl
) ?࿂
(br . tr
) ?༙
]))
52 ("༄" .
,(compose-string "࿁࿂" 0 2 [?࿁
(Br . Bl
) ?࿂
]))))
55 (defun tibetan-char-p (ch)
56 "Check if char CH is Tibetan character.
57 Returns non-nil if CH is Tibetan. Otherwise, returns nil."
58 (memq (char-charset ch
) '(tibetan tibetan-1-column
)))
60 ;;; Functions for Tibetan <-> Tibetan-transcription.
63 (defun tibetan-tibetan-to-transcription (str)
64 "Transcribe Tibetan string STR and return the corresponding Roman string."
65 (let (;; Accumulate transcriptions here in reverse order.
71 (let ((idx (string-match tibetan-precomposition-rule-regexp str i
)))
73 ;; Ith character and the followings matches precomposable
78 (cdr (assoc (match-string 0 str
)
79 tibetan-precomposition-rule-alist
))
80 tibetan-precomposed-transcription-alist
)))
81 (setq ch
(substring str i
(1+ i
))
84 (car (or (rassoc ch tibetan-consonant-transcription-alist
)
85 (rassoc ch tibetan-vowel-transcription-alist
)
86 (rassoc ch tibetan-subjoined-transcription-alist
)))))
87 (setq trans
(cons this-trans trans
))))
88 (apply 'concat
(nreverse trans
))))
91 (defun tibetan-transcription-to-tibetan (str)
92 "Convert Tibetan Roman string STR to Tibetan character string.
93 The returned string has no composition information."
94 (let (;; Case is significant.
95 (case-fold-search nil
)
97 ;; Accumulate Tibetan strings here in reverse order.
100 (while (setq i
(string-match tibetan-regexp str idx
))
102 ;; STR contains a pattern that doesn't match Tibetan
103 ;; transcription. Include the pattern as is.
104 (setq t-str-list
(cons (substring str idx i
) t-str-list
)))
105 (setq subtrans
(match-string 0 str
)
107 (let ((t-char (cdr (assoc subtrans
108 tibetan-precomposed-transcription-alist
))))
110 ;; SUBTRANS corresponds to a transcription for
111 ;; precomposable Tibetan sequence.
112 (setq t-char
(car (rassoc t-char
113 tibetan-precomposition-rule-alist
)))
116 (or (assoc subtrans tibetan-consonant-transcription-alist
)
117 (assoc subtrans tibetan-vowel-transcription-alist
)
118 (assoc subtrans tibetan-modifier-transcription-alist
)
119 (assoc subtrans tibetan-subjoined-transcription-alist
)))))
120 (setq t-str-list
(cons t-char t-str-list
))))
121 (if (< idx
(length str
))
122 (setq t-str-list
(cons (substring str idx
) t-str-list
)))
123 (apply 'concat
(nreverse t-str-list
))))
126 ;;; Functions for composing/decomposing Tibetan sequence.
128 ;;; A Tibetan syllable is typically structured as follows:
130 ;;; [Prefix] C [C+] V [M] [Suffix [Post suffix]]
132 ;;; where C's are all vertically stacked, V appears below or above
133 ;;; consonant cluster and M is always put above the C[C+]V combination.
134 ;;; (Sanskrit visarga, though it is a vowel modifier, is considered
135 ;;; to be a punctuation.)
137 ;;; Here are examples of the words "bsgrubs" and "hfauM"
147 ;;; Consonants `'' (འ), `w' (ཝ), `y' (ཡ), `r' (ར) take special
148 ;;; forms when they are used as subjoined consonant. Consonant `r'
149 ;;; takes another special form when used as superjoined in such a case
150 ;;; as "rka", while it does not change its form when conjoined with
151 ;;; subjoined `'', `w' or `y' as in "rwa", "rya".
153 ;; Append a proper composition rule and glyph to COMPONENTS to compose
154 ;; CHAR with a composition that has COMPONENTS.
156 (defun tibetan-add-components (components char
)
157 (let ((last (last components
))
158 (stack-upper '(tc . bc
))
159 (stack-under '(bc . tc
))
161 ;; Special treatment for 'a chung.
162 ;; If 'a follows a consonant, turn it into the subjoined form.
163 ;; * Disabled by Tomabechi 2000/06/09 *
164 ;; Because in Unicode, འ may follow directly a consonant without
165 ;; any intervening vowel, as in མཁའ་=མ ཁ འ not མ ཁ འ
166 ;;(if (and (= char ?འ)
167 ;; (aref (char-category-set (car last)) ?0))
168 ;; (setq char ?ཱ)) ;; modified for new font by Tomabechi 1999/12/10
170 ;; Composite vowel signs are decomposed before being added
171 ;; Added by Tomabechi 2000/06/08
172 (if (memq char
'(?ཱི ?ཱུ ?ྲྀ ?ཷ ?ླྀ ?ཹ ?ཱྀ
))
175 (cddr (assoc (char-to-string char
)
176 tibetan-composite-vowel-alist
)))
178 (cadr (assoc (char-to-string char
)
179 tibetan-composite-vowel-alist
))))
181 ;; Compose upper vowel sign vertically over.
182 ((aref (char-category-set char
) ?
2)
183 (setq rule stack-upper
))
185 ;; Compose lower vowel sign vertically under.
186 ((aref (char-category-set char
) ?
3)
187 (if (or (eq char ?
) ;; `' and `' should not visible when composed.
190 (setq rule stack-under
)))
191 ;; Transform ra-mgo (superscribed r) if followed by a subjoined
192 ;; consonant other than w, ', y, r.
193 ((and (= (car last
) ?ར
)
194 (not (memq char
'(?ྭ ?ཱ ?ྱ ?ྲ
))))
195 (setcar last ?
) ;; modified for newfont by Tomabechi 1999/12/10
196 (setq rule stack-under
))
197 ;; Transform initial base consonant if followed by a subjoined
200 (let ((laststr (char-to-string (car last
))))
201 (if (and (/= char ?ཱ
) ;; modified for new font by Tomabechi
202 (string-match "[ཀ-ཛྷཞཟལ-ཀྵཪ]" laststr
))
203 (setcar last
(string-to-char
204 (cdr (assoc (char-to-string (car last
))
205 tibetan-base-to-subjoined-alist
)))))
206 (setq rule stack-under
))))
209 (setcdr last
(list rule char
)))
210 ;; Added by Tomabechi 2000/06/08
212 (nconc last comp-vowel
))
216 (defun tibetan-compose-string (str)
217 "Compose Tibetan string STR."
219 ;; `འ' is included in the pattern for subjoined consonants
220 ;; because we treat it specially in tibetan-add-components.
221 ;; (This feature is removed by Tomabechi 2000/06/08)
222 (while (setq idx
(string-match tibetan-composable-pattern str idx
))
226 (if (eq (string-match tibetan-precomposition-rule-regexp str idx
) idx
)
227 (setq idx
(match-end 0)
229 (list (string-to-char
231 (assoc (match-string 0 str
)
232 tibetan-precomposition-rule-alist
)))))
233 (setq components
(list (aref str idx
))
236 (tibetan-add-components components
(aref str idx
))
238 (compose-string str from to components
))))
242 (defun tibetan-compose-region (beg end
)
243 "Compose Tibetan text the region BEG and END."
245 (let (str result chars
)
248 (narrow-to-region beg end
)
249 (goto-char (point-min))
250 ;; `འ' is included in the pattern for subjoined consonants
251 ;; because we treat it specially in tibetan-add-components.
252 ;; (This feature is removed by Tomabechi 2000/06/08)
253 (while (re-search-forward tibetan-composable-pattern nil t
)
254 (let ((from (match-beginning 0))
258 (if (looking-at tibetan-precomposition-rule-regexp
)
261 (list (string-to-char
263 (assoc (match-string 0)
264 tibetan-precomposition-rule-alist
)))))
265 (goto-char (match-end 0)))
266 (setq components
(list (char-after from
)))
268 (while (< (point) to
)
269 (tibetan-add-components components
(following-char))
271 (compose-region from to components
)))))))
273 (defvar tibetan-decompose-precomposition-alist
274 (mapcar (function (lambda (x) (cons (string-to-char (cdr x
)) (car x
))))
275 tibetan-precomposition-rule-alist
))
278 (defun tibetan-decompose-region (from to
)
279 "Decompose Tibetan text in the region FROM and TO.
280 This is different from decompose-region because precomposed Tibetan characters
281 are decomposed into normal Tibetan character sequences."
284 (narrow-to-region from to
)
285 (decompose-region from to
)
288 (let* ((char (following-char))
289 (slot (assq char tibetan-decompose-precomposition-alist
)))
294 (forward-char 1))))))
298 (defun tibetan-decompose-string (str)
299 "Decompose Tibetan string STR.
300 This is different from decompose-string because precomposed Tibetan characters
301 are decomposed into normal Tibetan character sequences."
307 (setq char
(aref str idx
)
308 slot
(assq (aref str idx
) tibetan-decompose-precomposition-alist
)
309 new
(concat new
(if slot
(cdr slot
) (char-to-string char
)))
314 ;;; This variable is used to avoid repeated decomposition.
316 (setq-default tibetan-decomposed nil
)
319 (defun tibetan-decompose-buffer ()
320 "Decomposes Tibetan characters in the buffer into their components.
321 See also the documentation of the function `tibetan-decompose-region'."
323 (make-local-variable 'tibetan-decomposed
)
324 (cond ((not tibetan-decomposed
)
325 (tibetan-decompose-region (point-min) (point-max))
326 (setq tibetan-decomposed t
))))
329 (defun tibetan-compose-buffer ()
330 "Composes Tibetan character components in the buffer.
331 See also docstring of the function tibetan-compose-region."
333 (make-local-variable 'tibetan-decomposed
)
334 (tibetan-compose-region (point-min) (point-max))
335 (setq tibetan-decomposed nil
))
338 (defun tibetan-post-read-conversion (len)
341 (let ((buffer-modified-p (buffer-modified-p)))
342 (narrow-to-region (point) (+ (point) len
))
343 (tibetan-compose-region (point-min) (point-max))
344 (set-buffer-modified-p buffer-modified-p
)
345 (make-local-variable 'tibetan-decomposed
)
346 (setq tibetan-decomposed nil
)
347 (- (point-max) (point-min))))))
351 (defun tibetan-pre-write-conversion (from to
)
352 (setq tibetan-decomposed-temp tibetan-decomposed
)
353 (let ((old-buf (current-buffer)))
354 (set-buffer (generate-new-buffer " *temp*"))
357 (insert-buffer-substring old-buf from to
))
358 (if (not tibetan-decomposed-temp
)
359 (tibetan-decompose-region (point-min) (point-max)))
360 ;; Should return nil as annotations.
365 ;;; Unicode-related definitions.
368 (defvar tibetan-canonicalize-for-unicode-alist
369 '(("" .
"") ;; remove vowel a
370 ("ཱི" .
"ཱི") ;; decompose vowels whose use is ``discouraged'' in Unicode 3.0
377 "Rules for canonicalizing Tibetan vowels for Unicode.")
379 (defvar tibetan-canonicalize-for-unicode-regexp
381 "Regexp for Tibetan vowels to be canonicalized in Unicode.")
383 (defun tibetan-canonicalize-for-unicode-region (from to
)
385 (narrow-to-region from to
)
387 (while (re-search-forward tibetan-canonicalize-for-unicode-regexp nil t
)
389 ;;(from (match-beginning 0))
392 (cdr (assoc (match-string 0)
393 tibetan-canonicalize-for-unicode-alist
))))
395 ;;(delete-region from to)
396 ;;(insert canonical-form)
397 (replace-match canonical-form
)
400 (defvar tibetan-strict-unicode t
401 "Flag to control Tibetan canonicalizing for Unicode.
403 If non-nil, the vowel a is removed and composite vowels are decomposed
404 before writing buffer in Unicode. See also
405 `tibetan-canonicalize-for-unicode-regexp' and
406 `tibetan-canonicalize-for-unicode-alist'.")
409 (defun tibetan-pre-write-canonicalize-for-unicode (from to
)
410 (let ((old-buf (current-buffer))
411 (strict-unicode tibetan-strict-unicode
))
412 (set-buffer (generate-new-buffer " *temp*"))
415 (insert-buffer-substring old-buf from to
))
417 (tibetan-canonicalize-for-unicode-region (point-min) (point-max)))
418 ;; Should return nil as annotations.
421 (provide 'tibet-util
)
423 ;;; tibet-util.el ends here