]> code.delx.au - gnu-emacs/blob - lisp/language/mlm-util.el
Merge from emacs--devo--0
[gnu-emacs] / lisp / language / mlm-util.el
1 ;;; mlm-util.el --- support for composing malayalam characters -*-coding: iso-2022-7bit;-*-
2
3 ;; Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
4 ;; Free Software Foundation, Inc.
5
6 ;; Maintainer: KAWABATA, Taichi <kawabata@m17n.org>
7 ;; Keywords: multilingual, Malayalam
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, or (at your option)
14 ;; 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;; Created: Feb. 11. 2003
27
28 ;;; Commentary:
29
30 ;; This file provides character(Unicode) to glyph(CDAC) conversion and
31 ;; composition of Malayalam script characters.
32
33 ;;; Code:
34
35 ;; Malayalam Composable Pattern
36 ;; C .. Consonants
37 ;; V .. Vowel
38 ;; H .. Halant
39 ;; M .. Matra
40 ;; V .. Vowel
41 ;; A .. Anuswar
42 ;; D .. Chandrabindu
43 ;; (N .. Zerowidth Non Joiner)
44 ;; (J .. Zerowidth Joiner. )
45 ;; 1. vowel
46 ;; V(A|visargam)?
47 ;; 2. syllable : maximum of 5 consecutive consonants. (e.g. kartsnya)
48 ;; ((CH)?(CH)?(CH)?CH)?C(H|M?(A|D)?)?
49
50 (defconst malayalam-consonant
51 "[\e$,1@5\e(B-\e$,1@Y\e(B]")
52
53 (defconst malayalam-composable-pattern
54 (concat
55 "\\([\e$,1@%\e(B-\e$,1@4\e(B][\e$,1@"\e(B]?\\)\\|\e$,1@#\e(B"
56 "\\|\\("
57 "\\(?:\\(?:[\e$,1@5\e(B-\e$,1@Y\e(B]\e$,1@m\e(B\\)?\\(?:[\e$,1@5\e(B-\e$,1@Y\e(B]\e$,1@m\e(B\\)?\\(?:[\e$,1@5\e(B-\e$,1@Y\e(B]\e$,1@m\e(B\\)?[\e$,1@5\e(B-\e$,1@Y\e(B]\e$,1@m\e(B\\)?"
58 "[\e$,1@5\e(B-\e$,1@Y\e(B]\\(?:\e$,1@m\e(B\\|[\e$,1@^\e(B-\e$,1@c@f@g@h@j@j@k@l\e(B]?[\e$,1@"@m\e(B]?\\)?"
59 "\\)")
60 "Regexp matching a composable sequence of Malayalam characters.")
61
62 ;;;###autoload
63 (defun malayalam-compose-region (from to)
64 (interactive "r")
65 (save-excursion
66 (save-restriction
67 (narrow-to-region from to)
68 (goto-char (point-min))
69 (while (re-search-forward malayalam-composable-pattern nil t)
70 (malayalam-compose-syllable-region (match-beginning 0)
71 (match-end 0))))))
72 (defun malayalam-compose-string (string)
73 (with-temp-buffer
74 (insert (decompose-string string))
75 (malayalam-compose-region (point-min) (point-max))
76 (buffer-string)))
77
78 ;;;###autoload
79 (defun malayalam-post-read-conversion (len)
80 (save-excursion
81 (save-restriction
82 (let ((buffer-modified-p (buffer-modified-p)))
83 (narrow-to-region (point) (+ (point) len))
84 (malayalam-compose-region (point-min) (point-max))
85 (set-buffer-modified-p buffer-modified-p)
86 (- (point-max) (point-min))))))
87
88 (defun malayalam-range (from to)
89 "Make the list of the integers of range FROM to TO."
90 (let (result)
91 (while (<= from to) (setq result (cons to result) to (1- to))) result))
92
93 (defun malayalam-regexp-of-hashtbl-keys (hashtbl)
94 "Return a regular expression that matches all keys in hashtable HASHTBL."
95 (let ((max-specpdl-size 1000))
96 (regexp-opt
97 (sort
98 (let (dummy)
99 (maphash (function (lambda (key val) (setq dummy (cons key dummy)))) hashtbl)
100 dummy)
101 (function (lambda (x y) (> (length x) (length y))))))))
102
103
104 ;;;###autoload
105 (defun malayalam-composition-function (pos &optional string)
106 "Compose Malayalam characters after the position POS.
107 If STRING is not nil, it is a string, and POS is an index to the string.
108 In this case, compose characters after POS of the string."
109 (if string
110 (if auto-compose-current-font
111 (if (eq (string-match "[\e$,1@ \e(B-\e$,1A?\e(B]+" pos) pos)
112 (or (font-shape-text 0 (match-end 0) auto-compose-current-font
113 string)
114 pos)))
115 (goto-char pos)
116 (if auto-compose-current-font
117 (if (looking-at "[\e$,1@ \e(B-\e$,1A?\e(B]+")
118 (or (font-shape-text pos (match-end 0) auto-compose-current-font)
119 pos)
120 (if (looking-at malayalam-composable-pattern)
121 (prog1 (match-end 0)
122 (malayalam-compose-syllable-region pos (match-end 0))))))))
123
124 ;; Notes on conversion steps.
125
126 ;; 1. chars to glyphs
127 ;;
128 ;; Simple replacement of characters to glyphs is done.
129
130 ;; 2. glyphs reordering.
131 ;;
132 ;; Two special reordering rule takes place.
133 ;; a. following "\e$,46[\e(B" goes to the front.
134 ;; b. following "\e$,46S6S\e(B", "\e$,46S\e(B" or "\e$,46T\e(B" goes to the front.
135 ;; This reordering occurs only to the last cluster of consonants.
136 ;; Preceding consonants with halant characters are not affected.
137
138 ;; 3. Composition.
139 ;;
140 ;; left modifiers will be attached at the left.
141 ;; others will be attached right.
142
143 (defvar mlm-char-glyph
144 '(;; various signs
145 ("\e$,1@"\e(B" . "\e$,46W\e(B")
146 ("\e$,1@#\e(B" . "\e$,46X\e(B")
147 ;; Independent Vowels
148 ("\e$,1@%\e(B" . "\e$,46!\e(B")
149 ("\e$,1@&\e(B" . "\e$,46"\e(B")
150 ("\e$,1@'\e(B" . "\e$,46#\e(B")
151 ("\e$,1@(\e(B" . "\e$,46#6U\e(B")
152 ("\e$,1@)\e(B" . "\e$,46$\e(B")
153 ("\e$,1@*\e(B" . "\e$,46$6U\e(B")
154 ("\e$,1@+\e(B" . "\e$,46%\e(B")
155 ("\e$,1@,\e(B" . "nil") ;; not in present use, not supported.
156 ("\e$,1@.\e(B" . "\e$,46&\e(B")
157 ("\e$,1@/\e(B" . "\e$,46'\e(B")
158 ("\e$,1@0\e(B" . "\e$,46S6&\e(B")
159 ("\e$,1@2\e(B" . "\e$,46(\e(B")
160 ("\e$,1@3\e(B" . "\e$,46(6M\e(B")
161 ("\e$,1@4\e(B" . "\e$,46(6U\e(B")
162 ;; Consonants
163 ("\e$,1@5\e(B" . "\e$,46)\e(B")
164 ("\e$,1@5@m@5\e(B" . "\e$,47!\e(B")
165 ("\e$,1@5@m@S\e(B" . "\e$,47"\e(B")
166 ("\e$,1@5@m@W\e(B" . "\e$,47#\e(B")
167 ("\e$,1@5@m@?\e(B" . "\e$,47N\e(B")
168 ("\e$,1@5@m@D\e(B" . "\e$,47`\e(B")
169 ("\e$,1@5@a\e(B" . "\e$,47f\e(B")
170 ("\e$,1@5@m@5@a\e(B" . "\e$,47g\e(B")
171 ("\e$,1@5@a\e(B" . "\e$,47f\e(B")
172 ("\e$,1@5@m@5@a\e(B" . "\e$,47g\e(B")
173
174 ("\e$,1@6\e(B" . "\e$,46*\e(B")
175
176 ("\e$,1@7\e(B" . "\e$,46+\e(B")
177 ("\e$,1@7@m@7\e(B" . "\e$,47$\e(B")
178 ("\e$,1@7@m@R\e(B" . "\e$,47%\e(B")
179 ("\e$,1@7@m@N\e(B" . "\e$,47\\e(B")
180 ("\e$,1@7@m@H\e(B" . "\e$,47a\e(B")
181
182 ("\e$,1@8\e(B" . "\e$,46,\e(B")
183
184 ("\e$,1@9\e(B" . "\e$,46-\e(B")
185 ("\e$,1@9@m@5\e(B" . "\e$,47&\e(B")
186 ("\e$,1@9@m@9\e(B" . "\e$,47'\e(B")
187 ("\e$,1@9@m@5@a\e(B" . "\e$,47h\e(B")
188
189 ("\e$,1@:\e(B" . "\e$,46.\e(B")
190 ("\e$,1@:@m@:\e(B" . "\e$,47(\e(B") ;; duplicate
191 ("\e$,1@:@m@;\e(B" . "\e$,47Q\e(B")
192
193 ("\e$,1@;\e(B" . "\e$,46/\e(B")
194
195 ("\e$,1@<\e(B" . "\e$,460\e(B")
196 ("\e$,1@<@m@<\e(B" . "\e$,47V\e(B")
197 ("\e$,1@<@m@>\e(B" . "\e$,47Z\e(B")
198
199 ("\e$,1@=\e(B" . "\e$,461\e(B")
200
201 ("\e$,1@>\e(B" . "\e$,462\e(B")
202 ("\e$,1@>@m@:\e(B" . "\e$,47)\e(B")
203 ("\e$,1@>@m@>\e(B" . "\e$,47*\e(B")
204
205 ("\e$,1@?\e(B" . "\e$,463\e(B")
206 ("\e$,1@?@m@?\e(B" . "\e$,47+\e(B")
207
208 ("\e$,1@@\e(B" . "\e$,464\e(B")
209 ("\e$,1@A\e(B" . "\e$,465\e(B")
210 ("\e$,1@A@m@A\e(B" . "\e$,47M\e(B")
211 ("\e$,1@B\e(B" . "\e$,466\e(B")
212
213 ("\e$,1@C\e(B" . "\e$,467\e(B")
214 ("\e$,1@C@a@m\e(B" . "\e$,47,\e(B") ;; half consonant
215 ("\e$,1@C@m@?\e(B" . "\e$,47-\e(B")
216 ("\e$,1@C@m@C\e(B" . "\e$,47.\e(B")
217 ("\e$,1@C@m@N\e(B" . "\e$,47W\e(B")
218 ("\e$,1@C@m@A\e(B" . "\e$,47^\e(B")
219 ("\e$,1@C@a\e(B" . "\e$,47i\e(B")
220
221 ("\e$,1@D\e(B" . "\e$,468\e(B")
222 ("\e$,1@D@m@D\e(B" . "\e$,47/\e(B")
223 ("\e$,1@D@m@E\e(B" . "\e$,470\e(B")
224 ("\e$,1@D@m@X\e(B" . "\e$,47U\e(B")
225 ("\e$,1@D@m@M\e(B" . "\e$,47[\e(B")
226 ("\e$,1@D@m@N\e(B" . "\e$,47_\e(B")
227
228 ("\e$,1@E\e(B" . "\e$,469\e(B")
229
230 ("\e$,1@F\e(B" . "\e$,46:\e(B")
231 ("\e$,1@F@m@F\e(B" . "\e$,471\e(B")
232 ("\e$,1@F@m@G\e(B" . "\e$,472\e(B")
233
234 ("\e$,1@G\e(B" . "\e$,46;\e(B")
235
236 ("\e$,1@H\e(B" . "\e$,46<\e(B")
237 ("\e$,1@H@a@m\e(B" . "\e$,473\e(B") ;; half consonant
238 ("\e$,1@H@m@D\e(B" . "\e$,474\e(B")
239 ("\e$,1@H@m@F\e(B" . "\e$,475\e(B")
240 ("\e$,1@H@m@H\e(B" . "\e$,476\e(B")
241 ("\e$,1@H@m@N\e(B" . "\e$,477\e(B")
242 ("\e$,1@H@m@G\e(B" . "\e$,47T\e(B")
243 ("\e$,1@H@m@E\e(B" . "\e$,47Y\e(B")
244 ("\e$,1@H@m@Q\e(B" . "\e$,47b\e(B")
245 ("\e$,1@H@a\e(B" . "\e$,47k\e(B")
246 ("\e$,1@H@m@H@a\e(B" . "\e$,47l\e(B")
247
248 ("\e$,1@J\e(B" . "\e$,46=\e(B")
249 ("\e$,1@J@m@J\e(B" . "\e$,478\e(B") ;; duplicate
250 ("\e$,1@J@m@R\e(B" . "\e$,479\e(B") ;; lakar
251
252 ("\e$,1@K\e(B" . "\e$,46>\e(B")
253
254 ("\e$,1@L\e(B" . "\e$,46?\e(B")
255 ("\e$,1@L@m@L\e(B" . "\e$,47:\e(B") ;; duplicate
256 ("\e$,1@L@m@R\e(B" . "\e$,47;\e(B") ;; lakar
257 ("\e$,1@L@m@G\e(B" . "\e$,47O\e(B")
258 ("\e$,1@L@m@F\e(B" . "\e$,47P\e(B")
259
260 ("\e$,1@M\e(B" . "\e$,46@\e(B")
261
262 ("\e$,1@N\e(B" . "\e$,46A\e(B")
263 ("\e$,1@N@m@J\e(B" . "\e$,47<\e(B")
264 ("\e$,1@N@m@N\e(B" . "\e$,47=\e(B")
265 ("\e$,1@N@m@R\e(B" . "\e$,47>\e(B") ;; lakar
266
267 ("\e$,1@O\e(B" . "\e$,46B\e(B")
268 ("\e$,1@O@m@O\e(B" . "\e$,47?\e(B") ;; duplicate
269 ("\e$,1@O@m@5@m@5\e(B" . "\e$,47m\e(B")
270
271 ("\e$,1@P\e(B" . "\e$,46C\e(B")
272 ("\e$,1@P@a@m\e(B" . "\e$,47@\e(B")
273 ("\e$,1@P@a\e(B" . "\e$,47j\e(B")
274
275 ("\e$,1@Q\e(B" . "\e$,46D\e(B")
276 ("\e$,1@Q@m\e(B" . "\e$,47@\e(B") ;; same glyph as "\e$,1@P@m\e(B"
277 ("\e$,1@Q@a@m\e(B" . "\e$,47@\e(B") ;; same glyph as "\e$,1@P@m\e(B"
278 ;;("\e$,1@Q@m@Q\e(B" . "\e$,47A\e(B")
279 ("\e$,1@Q@m@Q\e(B" . "\e$,47d\e(B")
280
281 ("\e$,1@R\e(B" . "\e$,46E\e(B")
282 ("\e$,1@R@a@m\e(B" . "\e$,47B\e(B")
283 ("\e$,1@R@m@R\e(B" . "\e$,47C\e(B") ;; lakar
284 ("\e$,1@R@m@J\e(B" . "\e$,47e\e(B")
285
286 ("\e$,1@S\e(B" . "\e$,46F\e(B")
287 ("\e$,1@S@a@m\e(B" . "\e$,47D\e(B")
288 ("\e$,1@S@m@S\e(B" . "\e$,47E\e(B")
289
290 ("\e$,1@T\e(B" . "\e$,46G\e(B")
291
292 ("\e$,1@U\e(B" . "\e$,46H\e(B")
293 ("\e$,1@U@m@U\e(B" . "\e$,47F\e(B")
294
295 ("\e$,1@V\e(B" . "\e$,46I\e(B")
296 ("\e$,1@V@m@R\e(B" . "\e$,47G\e(B")
297 ("\e$,1@V@m@V\e(B" . "\e$,47H\e(B")
298 ("\e$,1@V@m@:\e(B" . "\e$,47]\e(B")
299
300 ("\e$,1@W\e(B" . "\e$,46J\e(B")
301 ("\e$,1@W@m@?\e(B" . "\e$,47c\e(B")
302
303 ("\e$,1@X\e(B" . "\e$,46K\e(B")
304 ("\e$,1@X@m@R\e(B" . "\e$,47I\e(B")
305 ("\e$,1@X@m@X\e(B" . "\e$,47J\e(B")
306 ("\e$,1@X@m@Q@m@Q\e(B" . "\e$,47L\e(B")
307 ("\e$,1@X@m@E\e(B" . "\e$,47X\e(B")
308
309 ("\e$,1@Y\e(B" . "\e$,46L\e(B")
310 ("\e$,1@Y@m@R\e(B" . "\e$,47K\e(B")
311 ("\e$,1@Y@m@N\e(B" . "\e$,47R\e(B")
312 ("\e$,1@Y@m@H\e(B" . "\e$,47S\e(B")
313
314 ;; Dependent vowel signs
315 ("\e$,1@^\e(B" . "\e$,46M\e(B")
316 ("\e$,1@_\e(B" . "\e$,46N\e(B")
317 ("\e$,1@`\e(B" . "\e$,46O\e(B")
318 ("\e$,1@a\e(B" . "\e$,46P\e(B")
319 ("\e$,1@b\e(B" . "\e$,46Q\e(B")
320 ("\e$,1@c\e(B" . "\e$,46R\e(B")
321 ("\e$,1@f\e(B" . "\e$,46S\e(B")
322 ("\e$,1@g\e(B" . "\e$,46T\e(B")
323 ("\e$,1@h\e(B" . "\e$,46S6S\e(B")
324 ("\e$,1@j\e(B" . "\e$,46S6M\e(B")
325 ("\e$,1@k\e(B" . "\e$,46T6M\e(B")
326 ("\e$,1@l\e(B" . "\e$,46U\e(B")
327 ;; Various signs
328 ("\e$,1@m\e(B" . "\e$,46V\e(B")
329 ("\e$,1@m@O\e(B" . "\e$,46Y\e(B") ;; yakar
330 ("\e$,1@m@O@a\e(B" . "\e$,46\\e(B") ;; yakar + u
331 ("\e$,1@m@O@b\e(B" . "\e$,46]\e(B") ;; yakar + uu
332 ("\e$,1@m@U\e(B" . "\e$,46Z\e(B") ;; vakar modifier
333 ("\e$,1@m@P\e(B" . "\e$,46[\e(B") ;; rakar modifier is the same to rra modifier.
334 ("\e$,1@m@P@m\e(B" . "\e$,46R\e(B") ;; halant + rakar + halant
335 ("\e$,1@m@Q\e(B" . "\e$,46[\e(B") ;; rrakar modifier
336 ("\e$,1@m@Q@m\e(B" . "\e$,46R\e(B") ;; halant + rrakar + halant
337 ("\e$,1@m@m\e(B" . "\e$,46V\e(B") ;; double omission sign to stop forming half consonant.
338 ("\e$,1@w\e(B" . "\e$,46U\e(B") ;; not in present use, already at 0D4C.
339 ))
340
341 (defvar mlm-char-glyph-hash
342 (let* ((hash (make-hash-table :test 'equal)))
343 (mapc (function (lambda (x) (puthash (car x) (cdr x) hash)))
344 mlm-char-glyph)
345 hash))
346
347 (defvar mlm-char-glyph-regexp
348 (malayalam-regexp-of-hashtbl-keys mlm-char-glyph-hash))
349
350 ;; Malayalam languages needed to be reordered in a complex mannar.
351
352 (defvar mlm-consonants
353 (concat
354 "\e$,46)6*6+6,6-6.6/606162636465666768696:6;6<6=6>6?6@6A6B6C6D6E6F6G6H6I6J6K6L\e(B"
355 "\e$,47!7"7#7$7%7&7'7(7)7*7+7,7-7.7/707172737475767778797:7;7<7=7>7?7@7A7B7C7D7E7F7G7H7I7J7K7L7M7N7O7P7Q7R7S7T7U7V7W7X7Y7Z7[7\7]7^7_7`7a7b7c7d7e\e(B"
356 ))
357
358 (defvar mlm-consonants-regexp
359 (concat "\\(\e$,46[\e(B?[" mlm-consonants "][\e$,46Y6Z\e(B]?\\)"))
360
361 (defvar mlm-glyph-reorder-key-glyphs "[\e$,46[6S6T\e(B]")
362
363 (defvar mlm-glyph-reordering-regexp-list
364 `((,(concat "\\([" mlm-consonants "][\e$,46Y6Z\e(B]?\\)\e$,46[\e(B") . "\e$,46[\e(B\\1")
365 (,(concat mlm-consonants-regexp "\e$,46S6S\e(B") . "\e$,46S6S\e(B\\1")
366 (,(concat mlm-consonants-regexp "\e$,46S\e(B") . "\e$,46S\e(B\\1")
367 (,(concat mlm-consonants-regexp "\e$,46T\e(B") . "\e$,46T\e(B\\1")))
368
369 (defun malayalam-compose-syllable-string (string)
370 (with-temp-buffer
371 (insert (decompose-string string))
372 (malayalam-compose-syllable-region (point-min) (point-max))
373 (buffer-string)))
374
375 (defun malayalam-compose-syllable-region (from to)
376 "Compose malayalam syllable in region FROM to TO."
377 (let (glyph-str
378 match-str
379 glyph-reorder-regexps
380 glyph-reorder-replace
381 glyph-reorder-regexp)
382 (save-excursion
383 (save-restriction
384 (narrow-to-region from to)
385 (goto-char (point-min))
386 ;; char-glyph-conversion
387 (while (not (eobp))
388 (if (looking-at mlm-char-glyph-regexp)
389 (progn
390 (setq match-str (match-string 0)
391 glyph-str
392 (concat glyph-str
393 (gethash match-str mlm-char-glyph-hash)))
394 (goto-char (match-end 0)))
395 (setq glyph-str (concat glyph-str (string (following-char))))
396 (forward-char 1)))
397 (when (string-match mlm-glyph-reorder-key-glyphs glyph-str)
398 ;; glyph reordering
399 (setq glyph-reorder-regexps mlm-glyph-reordering-regexp-list)
400 (while glyph-reorder-regexps
401 (setq glyph-reorder-regexp (caar glyph-reorder-regexps))
402 (setq glyph-reorder-replace (cdar glyph-reorder-regexps))
403 (setq glyph-reorder-regexps (cdr glyph-reorder-regexps))
404 (if (string-match glyph-reorder-regexp glyph-str)
405 (setq glyph-str
406 (replace-match glyph-reorder-replace nil nil
407 glyph-str)))))
408 ;; concatenate and attach reference-points.
409 (setq glyph-str
410 (cdr
411 (apply
412 'nconc
413 (mapcar
414 (function
415 (lambda (x) (list '(5 . 3) x))) ;; default ref. point.
416 glyph-str))))
417 (compose-region from to glyph-str)))))
418
419 (provide 'mlm-util)
420
421 ;;; arch-tag: 7f25ee67-8f9d-49f2-837b-35c412c00eba
422 ;;; devan-util.el ends here