1;;; tv-util.el --- support for Tai Viet			-*- coding: utf-8 -*-
2
3;; Copyright (C) 2007, 2008, 2009, 2010, 2011
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
11;; GNU Emacs is free software: you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
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
22;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
23
24;;; Code
25
26;; Regexp matching with a sequence of Tai Viet characters.
27(defconst tai-viet-re "[\xaa80-\xaac2\xaadb-\xaadf]+")
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 . "ꪴꪰꪲꪳꪷꪸꪾ")
35		    (combining-tone . "꪿꫁")
36		    (misc . "-"))))
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)
57		(>= #xaa80 (setq ch (aref string from)))
58		(<= #xaaDF ch))
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
131(defun tai-viet-composition-function (from to font-object string _direction)
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)))))
138
139;;
140(provide 'tai-viet-util)
141