Merge from emacs-24; up to 2013-01-03T02:37:57Z!rgm@gnu.org
[bpt/emacs.git] / lisp / language / tv-util.el
CommitLineData
ee4f8284
KH
1;;; tv-util.el --- support for Tai Viet -*- coding: utf-8 -*-
2
5df4f04c 3;; Copyright (C) 2007, 2008, 2009, 2010, 2011
ee4f8284
KH
4;; National Institute of Advanced Industrial Science and Technology (AIST)
5;; Registration Number H13PRO009
6
7;; Keywords: multilingual, Tai Viet, i18n
8
9;; This file is part of GNU Emacs.
10
4936186e 11;; GNU Emacs is free software: you can redistribute it and/or modify
ee4f8284 12;; it under the terms of the GNU General Public License as published by
4936186e
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
ee4f8284
KH
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
4936186e 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
ee4f8284
KH
23
24;;; Code
25
26;; Regexp matching with a sequence of Tai Viet characters.
beb000f9 27(defconst tai-viet-re "[\xaa80-\xaac2\xaadb-\xaadf]+")
ee4f8284
KH
28
29;; Char-table of information about glyph type of Tai Viet characters.
30(defconst tai-viet-glyph-info
31 (let ((table (make-char-table nil))
32 (specials '((right-overhang . "ꪊꪋꪌꪍꪏꪓꪖꪜꪞꪡꪤꪨ")
33 (left-overhang . "ꫂ")
34 (combining-vowel . "ꪴꪰꪲꪳꪷꪸꪾ")
482a9df0
KH
35 (combining-tone . "꪿꫁")
36 (misc . "-"))))
ee4f8284
KH
37 ;; Set all TaiViet characters to `t'.
38 (set-char-table-range table (cons #xaa80 #xaac2) t)
39 (set-char-table-range table (cons #xaadb #xaadf) t)
40 ;; Overwrite it for special characters.
41 (dolist (elt specials)
42 (let ((category (car elt))
43 (chars (cdr elt)))
44 (dotimes (i (length chars))
45 (aset table (aref chars i) category))))
46 table))
47
48(defun tai-viet-compose-string (from to string)
49 "Compose Tai Viet characters in STRING between indices FROM and TO."
50 (let* ((ch (aref string from))
51 (info (aref tai-viet-glyph-info ch))
52 prev-info)
53 (if (eq info 'non-spacing)
54 (compose-string string from (1+ from) (string ch ?\t)))
55 (setq from (1+ from) prev-info info)
56 (while (and (< from to)
c8e50a73
KH
57 (>= #xaa80 (setq ch (aref string from)))
58 (<= #xaaDF ch))
ee4f8284
KH
59 (setq info (aref tai-viet-glyph-info ch))
60 (if (and (eq info 'non-spacing)
61 (eq prev-info 'non-spacing))
62 (compose-string from (1+ from) (string ?\t ch)))
63 (setq from (1+ from) prev-info info))
64 (if (eq info 'right-overhang)
65 (compose-string string (1- from) from (string ch ?\t)))
66 from))
67
68(defun tai-viet-compose-region (from to)
69 "Compose Tai Viet characters in the region between FROM and TO."
70 (decompose-region from to)
71 (let ((normal-rule '(Br . Bl))
72 (tone-rule '(tr . bl))
73 (prev-viet nil)
74 ch info pos components overhang)
75 (while (< from to)
76 (or ch
77 (setq ch (char-after from)
78 info (aref tai-viet-glyph-info ch)))
79 (setq from (1+ from))
80 (if (not info)
81 (setq prev-viet nil
82 ch nil)
83 (if (memq info '(combining-vowel combining-tone))
84 (progn
85 ;; Display this as a spacing glyph.
86 (compose-region (1- from) from (string ?\t ch))
87 (setq prev-viet t
88 ch nil))
89 (setq pos (1- from)
90 components ch
91 overhang (if (eq info 'right-overhang)
92 'right-overhang
93 (if (and (not prev-viet) (eq info 'left-overhang))
94 'left-overhang))
95 prev-viet t
96 ch nil)
97 (if (and (< from to)
98 (setq ch (char-after from)
99 info (aref tai-viet-glyph-info ch)))
100 (if (memq info '(combining-vowel combining-tone))
101 (progn
102 (setq components
103 (list components normal-rule ch)
104 from (1+ from)
105 ch nil)
106 (if (and (< from to)
107 (setq ch (char-after from)
108 info (aref tai-viet-glyph-info ch))
109 (eq info 'combining-tone))
110 (setq components (nconc components
111 (list tone-rule ch))
112 from (1+ from)))
113 (if (eq overhang 'left-overhang)
114 (setq components (cons ?\t
115 (cons normal-rule components)))
116 (if (and (eq overhang 'right-overhang)
117 (>= from to))
118 (setq components (nconc components
119 (list normal-rule ?\t)))))
120 (compose-region pos from components))
121 (if (eq overhang 'left-overhang)
122 (compose-region pos from (string ?\t components))))
123 (if (eq overhang 'left-overhang)
124 (compose-region pos from (string ?\t components))
125 (if (and (eq overhang 'right-overhang) (>= from to))
126 (compose-region pos from (string components ?\t))))))))
127 from))
128
129
130;;;###autoload
268dff94
KH
131(defun tai-viet-composition-function (from to font-object string)
132 (if string
133 (if (string-match tai-viet-re string from)
134 (tai-viet-compose-string from (match-end 0) string))
135 (goto-char from)
136 (if (looking-at tai-viet-re)
137 (tai-viet-compose-region from (match-end 0)))))
ee4f8284
KH
138
139;;
140(provide 'tai-viet-util)
650e2d28 141