1;;; devan-util.el --- Support for composing Devanagari characters
2
3;; Copyright (C) 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4;;   Free Software Foundation, Inc.
5;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
6;;   National Institute of Advanced Industrial Science and Technology (AIST)
7;;   Registration Number H14PRO021
8
9;; Maintainer:  KAWABATA, Taichi <kawabata@m17n.org>
10;; Keywords: multilingual, Devanagari
11
12;; This file is part of GNU Emacs.
13
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 2, or (at your option)
17;; any later version.
18
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.
23
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.
28
29;; Created: Feb. 17. 2001
30
31;;; Commentary:
32
33;; This file provides character(Unicode) to glyph(CDAC) conversion and
34;; composition of Devanagari script characters.
35
36;;; Code:
37
38;;;###autoload
39
40;; Devanagari Composable Pattern
41;;    C .. Consonants
42;;    V .. Vowel
43;;    H .. Halant
44;;    M .. Matra
45;;    V .. Vowel
46;;    A .. Anuswar
47;;    D .. Chandrabindu
48;;    (N .. Zerowidth Non Joiner)
49;;    (J .. Zerowidth Joiner.  )
50;; 1. vowel
51;;  V(A/D)?
52;; 2. syllable : maximum of 5 consecutive consonants.  (e.g. kartsnya)
53;;  ((CH)?(CH)?(CH)?CH)?C(H|M?(A|D)?)?
54
55(defconst devanagari-consonant
56  "[$,15U(B-$,15y68(B-$,16?(B]")
57
58(defconst devanagari-composable-pattern
59  (concat
60   "\\([$,15E(B-$,15T6@6A(B][$,15A5B(B]?\\)\\|[$,15C6D(B]"
61   "\\|\\("
62   "\\(?:\\(?:[$,15U(B-$,15y68(B-$,16?(B]$,16-(B\\)?\\(?:[$,15U(B-$,15y68(B-$,16?(B]$,16-(B\\)?\\(?:[$,15U(B-$,15y68(B-$,16?(B]$,16-(B\\)?[$,15U(B-$,15y68(B-$,16?(B]$,16-(B\\)?"
63   "[$,15U(B-$,15y68(B-$,16?(B]\\(?:$,16-(B\\|[$,15~(B-$,16-6B6C(B]?[$,15B5A(B]?\\)?"
64   "\\)")
65  "Regexp matching a composable sequence of Devanagari characters.")
66
67;;;###autoload
68(defun devanagari-compose-region (from to)
69  (interactive "r")
70  (save-excursion
71    (save-restriction
72      (narrow-to-region from to)
73      (goto-char (point-min))
74      (while (re-search-forward devanagari-composable-pattern nil t)
75        (devanagari-compose-syllable-region (match-beginning 0)
76                                            (match-end 0))))))
77(defun devanagari-compose-string (string)
78  (with-temp-buffer
79    (insert (decompose-string string))
80    (devanagari-compose-region (point-min) (point-max))
81    (buffer-string)))
82
83;;;###autoload
84(defun devanagari-post-read-conversion (len)
85  (save-excursion
86    (save-restriction
87      (let ((buffer-modified-p (buffer-modified-p)))
88	(narrow-to-region (point) (+ (point) len))
89	(devanagari-compose-region (point-min) (point-max))
90	(set-buffer-modified-p buffer-modified-p)
91	(- (point-max) (point-min))))))
92
93(defun devanagari-range (from to)
94  "Make the list of the integers of range FROM to TO."
95  (let (result)
96    (while (<= from to) (setq result (cons to result) to (1- to))) result))
97
98(defun devanagari-regexp-of-hashtbl-keys (hashtbl)
99  "Return a regular expression that matches all keys in hashtable HASHTBL."
100  (let ((max-specpdl-size 1000))
101    (regexp-opt
102     (sort
103      (let (dummy)
104	(maphash (function (lambda (key val) (setq dummy (cons key dummy)))) hashtbl)
105	dummy)
106      (function (lambda (x y) (> (length x) (length y))))))))
107
108(defun devanagari-composition-function (from to pattern &optional string)
109  "Compose Devanagari characters in REGION, or STRING if specified.
110Assume that the REGION or STRING must fully match the composable
111PATTERN regexp."
112  (if string (devanagari-compose-syllable-string string)
113    (devanagari-compose-syllable-region from to))
114  (- to from))
115
116;; Register a function to compose Devanagari characters.
117(mapc
118 (function (lambda (ucs)
119   (aset composition-function-table (decode-char 'ucs ucs)
120	 (list (cons devanagari-composable-pattern
121                     'devanagari-composition-function)))))
122 (nconc '(#x0903) (devanagari-range #x0905 #x0939) (devanagari-range #x0958 #x0961)))
123
124;; Notes on conversion steps.
125
126;; 1. chars to glyphs
127;;
128;; Rules will not be applied to the halant appeared at the end of the
129;; text.  Also, the preceding/following "r" will be treated as special case.
130
131;; 2. glyphs reordering.
132;;
133;; The glyphs are split by halant, and each glyph groups are
134;; re-ordered in the following order.
135;;
136;; Note that `consonant-glyph' mentioned here does not contain the
137;; vertical bar (right modifier) attached at the right of the
138;; consonant.
139;;
140;; If the glyph-group contains right modifier,
141;;  (1) consonant-glyphs/vowels, with nukta sign
142;;  (2) spacing
143;;  (3) right modifier (may be matra)
144;;  (4) top matra
145;;  (5) preceding "r"
146;;  (6) anuswar
147;;  (7) following "r"
148;;  (8) bottom matra or halant.
149;;
150;; Otherwise,
151;;  (1) consonant-glyph/vowels, with nukta sign
152;;  (3) left matra
153;;  (4) top matra
154;;  (5) preceding "r"
155;;  (6) anuswar
156;;  (7) following "r"
157;;  (8) bottom matra or halant.
158;;  (2) spacing
159
160;; 3. glyph to glyph
161;;
162;; For better display, some glyph display would be tuned.
163
164;; 4. Composition.
165;;
166;; left modifiers will be attached at the left.
167;; others will be attached right.
168
169;; Problem::
170;;  Can we generalize this methods to other Indian scripts?
171
172(defvar dev-char-glyph
173  '(("$,15E(B" . "$,4 K(B")
174    ("$,15F(B" . "$,4 K")(B")
175    ("$,15~(B" . "$,4")(B")
176    ("$,15G(B" . "$,4 \(B")
177    ("$,15(B" . "$,4"*(B")
178    ("$,155A(B" . "$,4"*(B\$,4"&(B")
179    ("$,15H(B" . "$,4 \"'(B")
180    ("$,15H5A(B" . "$,4 \"'"&(B")
181    ("$,16 (B" . "$,4"2(B")
182    ("$,16 5A(B" . "$,4"2"&(B")
183    ("$,15I(B" . "$,4 ](B")
184    ("$,16!(B" . "$,4"6(B")
185    ("$,15J(B" . "$,4 ^"P(B")
186    ("$,16"(B" . "$,4":(B")
187    ("$,15K(B" . "$,4 `"Q(B")
188    ("$,16#(B" . "$,4">(B")
189    ;;("$,15L(B" . nil) ; not implemented.
190    ("$,16$(B" . "$,4"?(B")
191    ("$,15M(B" . "$,4 b"L(B")
192    ("$,15M5A(B" . "$,4 b"$(B")
193    ("$,15M5B(B" . "$,4 b"$(B")
194    ("$,16%(B" . "\$,4"L(B")
195    ("$,15N(B" . "$,4 b"@(B")
196    ("$,15N5A(B" . "$,4 b"@"&(B")
197    ("$,16&(B" . "\$,4"@(B")
198    ("$,16&5A(B" . "\$,4"@(B\$,4"&(B")
199    ("$,15O(B" . "$,4 b(B")
200    ("$,16'(B" . "\$,4"D(B")
201    ("$,16'5A(B" . "\$,4"D(B\$,4"&(B")
202    ("$,15P(B" . "$,4 b"D(B")
203    ("$,15P5A(B" . "$,4 b"D"&(B")
204    ("$,16((B" . "\$,4"H(B")
205    ("$,16(5A(B" . "\$,4"H(B\$,4"&(B")
206    ("$,15Q(B" . "$,4 K")"L(B") ;; special rule for reodering.
207    ("$,15Q5A(B" . "$,4 K")"$(B")
208    ("$,15Q5B(B" . "$,4 K")"$(B")
209    ("$,16)(B" . "\$,4")"L(B")
210    ("$,16)5A(B" . "\$,4")"$(B")
211    ("$,16)5B(B" . "\$,4")"$(B")
212    ("$,15R(B" . "$,4 K")"@(B")
213    ("$,15R5A(B" . "$,4 K")"@"&(B")
214    ("$,16*(B" . "\$,4")"@(B")
215    ("$,16*5A(B" . "\$,4")"@"&(B")
216    ("$,15S(B" . "$,4 K")"D(B")
217    ("$,15S5A(B" . "$,4 K")"D"&(B")
218    ("$,16+(B" . "\$,4")"D(B")
219    ("$,16+5A(B" . "\$,4")"D"&(B")
220    ("$,15T(B" . "$,4 K")"H(B")
221    ("$,15T5A(B" . "$,4 K")"H"&(B")
222    ("$,16,(B" . "\$,4")"H(B")
223    ("$,16,5A(B" . "\$,4")"H"&(B")
224    ("$,16@(B" . "$,4 a"Q(B")
225    ;;("$,16B(B" . nil)
226    ;;("$,16A(B" . nil)
227    ;;("$,16C(B" . nil)
228
229    ;; GRUTTALS
230    ("$,15U(B" . "$,4 e"R(B")
231    ("$,15U6-(B" . "$,4 c(B")
232    ("$,15U6-5p(B" . "$,4 g"R(B")
233    ("$,15U6-5d(B" . "$,4 h"R(B")
234    ("$,15U6-5w(B" . "$,4 i")(B")
235    ("$,15U6-5w6-(B" . "$,4 i(B")
236
237    ("$,15V(B" . "$,4 j")(B")
238    ("$,15V6-(B" . "$,4 j(B")
239    ("$,15V6-5p(B" . "$,4 l")(B")
240    ("$,15V6-5p6-(B" . "$,4 l(B")
241
242    ("$,15W(B" . "$,4 m")(B")
243    ("$,15W6-(B" . "$,4 m(B")
244    ("$,15W6-5p(B" . "$,4 o")(B")
245    ("$,15W6-5p6-(B" . "$,4 o(B")
246
247    ("$,15X(B" . "$,4 p")(B")
248    ("$,15X6-(B" . "$,4 p(B")
249    ("$,15X6-5p(B" . "$,4 q")(B")
250    ("$,15X6-5p6-(B" . "$,4 q(B")
251
252    ("$,15Y(B" . "$,4 r"S(B")
253    ;; PALATALS
254    ("$,15Z(B" . "$,4 s")(B")
255    ("$,15Z6-(B" . "$,4 s(B")
256    ("$,15Z6-5p(B" . "$,4 t")(B")
257    ("$,15Z6-5p6-(B" . "$,4 t(B")
258
259    ("$,15[(B" . "$,4 u"T(B")
260
261    ("$,15\(B" . "$,4 v")(B")
262    ("$,15\6-(B" . "$,4 v(B")
263    ("$,15\6-5p(B" . "$,4 x")(B")
264    ("$,15\6-5p6-(B" . "$,4 x(B")
265    ("$,15\6-5^(B" . "$,4 y")(B")
266    ("$,15\6-5^6-(B" . "$,4 y(B")
267
268    ("$,15](B" . "$,4 z")(B")
269    ("$,15]6-(B" . "$,4 z(B")
270    ("$,15]6-5p(B" . "$,4 {")(B")
271    ("$,15]6-5p6-(B" . "$,4 {(B")
272
273    ("$,15^(B" . "$,4 |")(B")
274    ("$,15^6-(B" . "$,4 |(B")
275    ;; CEREBRALS
276    ("$,15_(B" . "$,4 }"U(B")
277    ("$,15_6-5_(B" . "$,4 ~"U(B")
278    ("$,15_6-5`(B" . "$,4 "U(B")
279
280    ("$,15`(B" . "$,4! "V(B")
281    ("$,15`6-5`(B" . "$,4!!"V(B")
282
283    ("$,15a(B" . "$,4!""W(B")
284    ("$,15a6-5a(B" . "$,4!$"W(B")
285    ("$,15a6-5b(B" . "$,4!%"W(B")
286
287    ("$,15b(B" . "$,4!&"X(B")
288
289    ("$,15c(B" . "$,4!(")(B")
290    ("$,15c6-(B" . "$,4!((B")
291    ;; DENTALS
292    ("$,15d(B" . "$,4!)")(B")
293    ("$,15d6-(B" . "$,4!)(B")
294    ("$,15d6-5p(B" . "$,4!*")(B")
295    ("$,15d6-5p6-(B" . "$,4!*(B")
296    ("$,15d6-5d(B" . "$,4!+")(B")
297    ("$,15d6-5d6-(B" . "$,4!+(B")
298
299    ("$,15e(B" . "$,4!,")(B")
300    ("$,15e6-(B" . "$,4!,(B")
301    ("$,15e6-5p(B" . "$,4!-")(B")
302    ("$,15e6-5p6-(B" . "$,4!-(B")
303
304    ("$,15f(B" . "$,4!."Y(B")
305    ("$,15f6#(B" . "$,4!/"Y(B")
306    ("$,15f6-5p(B" . "$,4!0"Y(B")
307    ("$,15f6-5f(B" . "$,4!1"Y(B")
308    ("$,15f6-5g(B" . "$,4!2"Y(B")
309    ("$,15f6-5n(B" . "$,4!3(B")
310    ("$,15f6-5o(B" . "$,4!4(B")
311    ("$,15f6-5u(B" . "$,4!5"Y(B")
312
313    ("$,15g(B" . "$,4!6")(B")
314    ("$,15g6-(B" . "$,4!6(B")
315    ("$,15g6-5p(B" . "$,4!7")(B")
316    ("$,15g6-5p6-(B" . "$,4!7(B")
317
318    ("$,15h(B" . "$,4!8")(B")
319    ("$,15h6-(B" . "$,4!8(B")
320    ("$,15h6-5p(B" . "$,4!9")(B")
321    ("$,15h6-5p6-(B" . "$,4!9")(B")
322    ("$,15h6-5h(B" . "$,4!:")(B")
323    ("$,15h6-5h6-(B" . "$,4!:(B")
324
325    ("$,15i(B" . "$,4!8"#")(B")
326    ;; LABIALS
327    ("$,15j(B" . "$,4!;")(B")
328    ("$,15j6-(B" . "$,4!;(B")
329    ("$,15j6-5p(B" . "$,4!<")(B")
330    ("$,15j6-5p6-(B" . "$,4!<(B")
331
332    ("$,15k(B" . "$,4!a"[(B")
333    ("$,15k6-(B" . "$,4!=(B")
334    ("$,15k6-5p(B" . "$,4!c"[(B")
335
336    ("$,15l(B" . "$,4!d")(B")
337    ("$,15l6-(B" . "$,4!d(B")
338    ("$,15l6-5p(B" . "$,4!e")(B")
339    ("$,15l6-5p6-(B" . "$,4!e(B")
340
341    ("$,15m(B" . "$,4!f")(B")
342    ("$,15m6-(B" . "$,4!f(B")
343    ("$,15m6-5p(B" . "$,4!g")(B")
344    ("$,15m6-5p6-(B" . "$,4!g(B")
345
346    ("$,15n(B" . "$,4!h")(B")
347    ("$,15n6-(B" . "$,4!h(B")
348    ("$,15n6-5p(B" . "$,4!i")(B")
349    ("$,15n6-5p6-(B" . "$,4!i(B")
350    ;; SEMIVOWELS
351    ("$,15o(B" . "$,4!j")(B")
352    ("$,15o6-(B" . "$,4!j(B")
353    ("$,15o6-5p(B" . "$,4!k")(B")
354    ("$,15o6-5p6-(B" . "$,4!k(B")
355    ("$,16-5o(B" . "$,4!l(B") ;; when every ohter lig. fails.
356
357    ("$,15p(B" . "$,4!n"W(B")
358    ;; ("$,15p6-(B" . "\$,4"'(B") ;; special case.  only the topmost pos.
359    ("$,15q(B" . "$,4!n"#"W(B")
360    ("$,15q6-(B" . "$,4!m(B") ;; IS 13194 speical rule.
361    ("$,15p6!(B" . "$,4!o"[(B")
362    ("$,15p6"(B" . "$,4!p"\(B")
363
364    ("$,15r(B" . "$,4!q")(B")
365    ("$,15r6-(B" . "$,4!q(B")
366    ("$,15s(B" . "$,4!s(B")
367    ("$,15s6-(B" . "$,4!r(B")
368    ("$,15t(B" . "$,4!s"#(B")
369    ("$,15t6-(B" . "$,4!r"#(B")
370
371    ("$,15u(B" . "$,4!t")(B")
372    ("$,15u6-(B" . "$,4!t(B")
373    ("$,15u6-5p(B" . "$,4!u")(B")
374    ("$,15u6-5p6-(B" . "$,4!u(B")
375    ;; SIBILANTS
376    ("$,15v(B" . "$,4!v")(B")
377    ("$,15v6-(B" . "$,4!v(B")
378    ("$,15v6-5u(B" . "$,4!w")(B")
379    ("$,15v6-5u6-(B" . "$,4!w(B")
380    ("$,15v6-5p(B" . "$,4!x")(B")
381    ("$,15v6-5p6-(B" . "$,4!x(B")
382
383    ("$,15w(B" . "$,4!y")(B")
384    ("$,15w6-(B" . "$,4!y(B")
385    ("$,15x(B" . "$,4!z")(B")
386    ("$,15x6-(B" . "$,4!z(B")
387    ("$,15x6-5p(B" . "$,4!{")(B")
388    ("$,15x6-5p6-(B" . "$,4!{(B")
389
390    ("$,15y(B" . "$,4!}(B")
391    ("$,15y6-(B" . "$,4!|(B")
392    ("$,15y6#(B" . "$,4!~(B")
393    ("$,15y6-5p(B" . "$,4!(B")
394    ("$,15y6-5n(B" . "$,4" (B")
395    ("$,15y6-5o(B" . "$,4"!(B")
396    ;; NUKTAS
397    ("$,168(B" . "$,4 f"R"S(B")
398    ("$,1686-(B" . "$,4 d(B")
399    ("$,169(B" . "$,4 k")(B")
400    ("$,1696-(B" . "$,4 k(B")
401    ("$,16:(B" . "$,4 n")(B")
402    ("$,16:6-(B" . "$,4 n(B")
403    ("$,16;(B" . "$,4 w")(B")
404    ("$,16;6-(B" . "$,4 w(B")
405    ("$,16<(B" . "$,4!#"W(B")
406    ("$,16=(B" . "$,4!'"X(B")
407    ("$,16>(B" . "$,4!b"[(B")
408    ("$,16>6-(B" . "$,4!>(B")
409    ("$,16?(B" . "$,4!j"#")(B")
410    ;; misc modifiers.
411    ("$,15A(B" . "\$,4"$(B")
412    ("$,15B(B" . "\$,4"&(B")
413    ("$,15C(B" . "$,4 F(B")
414    ("$,15|(B" . "$,4"#(B")
415    ("$,15}(B" . "$,4 E(B")
416    ("$,16-(B" . "$,4""(B")
417    ("$,16-5p(B" . "$,4"%(B") ;; following "r"
418    ;; ("$,160(B" . "$,4 D(B")
419    ("$,16D(B" . "$,4 J(B")
420    ;; ("$,16F(B" . "")
421    ;; ("$,16G(B" . "")
422    ;; ("$,16H(B" . "")
423    ;; ("$,16I(B" . "")
424    ;; ("$,16J(B" . "")
425    ;; ("$,16K(B" . "")
426    ;; ("$,16L(B" . "")
427    ;; ("$,16M(B" . "")
428    ;; ("$,16N(B" . "")
429    ;; ("$,16O(B" . "")
430    )
431  "Devanagari characters to glyphs conversion table.
432Default value contains only the basic rules.  You may add your own
433preferred rule from the sanskrit fonts."  )
434
435(defvar dev-char-glyph-hash
436  (let* ((hash (make-hash-table :test 'equal)))
437    (mapc (function (lambda (x) (puthash (car x) (cdr x) hash)))
438	  dev-char-glyph)
439    hash))
440
441(defvar dev-char-glyph-regexp
442  (devanagari-regexp-of-hashtbl-keys dev-char-glyph-hash))
443
444;; glyph-to-glyph conversion table.
445;; it is supposed that glyphs are ordered in
446;;   [consonant/nukta] - [matra/halant] - [preceding-r] - [anuswar].
447
448(defvar dev-glyph-glyph
449  '(("\$,4"'(B\$,4"&(B" . "\$,4"((B")
450    ("\$,4"'(B\$,4"$(B" . "\$,4"((B")
451    ("$,4"*(B\$,4"&(B" . "$,4"+(B")
452    ("$,4"*(B\$,4"'(B" . "$,4",(B")
453    ("$,4"*(B\$,4"'(B\$,4"&(B" . "$,4"-(B")
454    ("$,4"2(B\$,4"&(B" . "$,4"3(B")
455    ("$,4"2(B\$,4"'(B" . "$,4"4(B")
456    ("$,4"2(B\$,4"'(B\$,4"&(B" . "$,4"5(B")
457    ("$,4"#(B\$,4"6(B" . "$,4"7(B")
458    ("$,4"%(B\$,4"6(B" . "$,4"8(B")
459    ;;("$,4"6(B" . "$,4"9(B")
460    ("$,4"#(B\$,4":(B" . "$,4";(B")
461    ("$,4"%(B\$,4":(B" . "$,4"<(B")
462    ;;("$,4":(B" . "$,4"=(B")
463    ("\$,4"@(B\$,4"&(B" . "\$,4"A(B")
464    ("\$,4"@(B\$,4"'(B" . "\$,4"B(B")
465    ("\$,4"@(B\$,4"'(B\$,4"&(B" . "\$,4"C(B")
466    ("\$,4"D(B\$,4"&(B" . "\$,4"E(B")
467    ("\$,4"D(B\$,4"'(B" . "\$,4"F(B")
468    ("\$,4"D(B\$,4"'(B\$,4"&(B" . "\$,4"G(B")
469    ("\$,4"H(B\$,4"&(B" . "\$,4"I(B")
470    ("\$,4"H(B\$,4"'(B" . "\$,4"J(B")
471    ("\$,4"H(B\$,4"'(B\$,4"&(B" . "\$,4"K(B")
472    ("\$,4"L(B\$,4"&(B" . "\$,4"M(B")
473    ("\$,4"L(B\$,4"'(B" . "\$,4"N(B")
474    ("\$,4"L(B\$,4"'(B\$,4"&(B" . "\$,4"O(B")
475    ))
476(defvar dev-glyph-glyph-hash
477  (let* ((hash (make-hash-table :test 'equal)))
478    (mapc (function (lambda (x) (puthash (car x) (cdr x) hash)))
479	  dev-glyph-glyph)
480    hash))
481(defvar dev-glyph-glyph-regexp
482  (devanagari-regexp-of-hashtbl-keys dev-glyph-glyph-hash))
483
484
485;; yet another glyph-to-glyph conversions.
486(defvar dev-glyph-glyph-2
487  '(("$,4"*(B" . "$,4".(B")
488    ("$,4"+(B" . "$,4"/(B")
489    ("$,4",(B" . "$,4"0(B")
490    ("$,4"-(B" . "$,4"1(B")))
491(defvar dev-glyph-glyph-2-hash
492  (let* ((hash (make-hash-table :test 'equal)))
493    (mapc (function (lambda (x) (puthash (car x) (cdr x) hash)))
494	  dev-glyph-glyph-2)
495    hash))
496(defvar dev-glyph-glyph-2-regexp
497  (devanagari-regexp-of-hashtbl-keys dev-glyph-glyph-2-hash))
498
499
500(defun dev-charseq (from &optional to)
501  (if (null to) (setq to from))
502  (mapcar (function (lambda (x) (indian-glyph-char x 'devanagari)))
503          (devanagari-range from to)))
504
505(defvar dev-glyph-cvn
506  (append
507   (dev-charseq #x2b)
508   (dev-charseq #x3c #xc1)
509   (dev-charseq #xc3))
510  "Devanagari Consonants/Vowels/Nukta Glyphs")
511
512(defvar dev-glyph-space
513  (dev-charseq #xf0 #xfe)
514  "Devanagari Spacing Glyphs")
515
516(defvar dev-glyph-right-modifier
517  (append
518   (dev-charseq #xc9)
519   (dev-charseq #xd2 #xd5))
520  "Devanagari Modifiers attached at the right side.")
521
522(defvar dev-glyph-right-modifier-regexp
523  (concat "[" dev-glyph-right-modifier "]"))
524
525(defvar dev-glyph-left-matra
526  (dev-charseq #xca #xd1)
527  "Devanagari Matras attached at the left side.")
528
529(defvar dev-glyph-top-matra
530  (dev-charseq #xe0 #xef)
531  "Devanagari Matras attached at the top side.")
532
533(defvar dev-glyph-bottom-modifier
534  (append
535   (dev-charseq #xd6 #xdf)
536   (dev-charseq #xc2))
537  "Devanagari Modifiers attached at the bottom.")
538
539(defvar dev-glyph-order
540  `((,dev-glyph-cvn . 1)
541    (,dev-glyph-space . 2)
542    (,dev-glyph-right-modifier . 3)
543    (,dev-glyph-left-matra . 3) ;; processed by reference point.
544    (,dev-glyph-top-matra . 4)
545    (,(dev-charseq #xc7 #xc8) . 5)
546    (,(dev-charseq #xc4) . 6)
547    (,(dev-charseq #xc6) . 6)
548    (,(dev-charseq #xc5) . 7)
549    (,dev-glyph-bottom-modifier . 8)))
550
551(mapc
552 (function (lambda (x)
553   (mapc
554     (function (lambda (y)
555       (put-char-code-property y 'composition-order (cdr x))))
556     (car x))))
557  dev-glyph-order)
558
559(mapc
560  (function (lambda (x)
561    (put-char-code-property x 'reference-point '(3 . 5))))
562 dev-glyph-left-matra)
563
564(defun devanagari-compose-syllable-string (string)
565  (with-temp-buffer
566    (insert (decompose-string string))
567    (devanagari-compose-syllable-region (point-min) (point-max))
568    (buffer-string)))
569
570(defun devanagari-compose-syllable-region (from to)
571  "Compose devanagari syllable in region FROM to TO."
572  (let ((glyph-str nil) (cons-num 0) glyph-str-list
573	(last-halant nil) (preceding-r nil) (last-modifier nil)
574	(last-char (char-before to)) match-str
575	glyph-block split-pos)
576    (save-excursion
577      (save-restriction
578	  ;;; *** char-to-glyph conversion ***
579	;; Special rule 1. -- Last halant must be preserved.
580	(if (eq last-char ?$,16-(B)
581	    (progn
582	      (setq last-halant t)
583	      (narrow-to-region from (1- to)))
584	  (narrow-to-region from to)
585	  ;; note if the last char is modifier.
586	  (if (or (eq last-char ?$,15A(B) (eq last-char ?$,15B(B))
587	      (setq last-modifier t)))
588	(goto-char (point-min))
589	;; Special rule 2. -- preceding "r halant" must be modifier.
590	(when (looking-at "$,15p6-(B.")
591	  (setq preceding-r t)
592	  (goto-char (+ 2 (point))))
593	;; translate the rest characters into glyphs
594	(while (re-search-forward dev-char-glyph-regexp nil t)
595	  (setq match-str (match-string 0))
596	  (setq glyph-str
597		(concat glyph-str
598			(gethash match-str dev-char-glyph-hash)))
599	  ;; count the number of consonant-glyhs.
600	  (if (string-match devanagari-consonant match-str)
601	      (setq cons-num (1+ cons-num))))
602	;; preceding-r must be attached before the anuswar if exists.
603	(if preceding-r
604	    (if last-modifier
605		(setq glyph-str (concat (substring glyph-str 0 -1)
606					"$,4"'(B" (substring glyph-str -1)))
607	      (setq glyph-str (concat glyph-str "$,4"'(B"))))
608	(if last-halant (setq glyph-str (concat glyph-str "$,4""(B")))
609	  ;;; *** glyph-to-glyph conversion ***
610	(when (string-match dev-glyph-glyph-regexp glyph-str)
611	  (setq glyph-str
612		(replace-match (gethash (match-string 0 glyph-str)
613					dev-glyph-glyph-hash)
614			       nil t glyph-str))
615	  (if (and (> cons-num 1)
616		   (string-match dev-glyph-glyph-2-regexp glyph-str))
617	      (setq glyph-str
618		    (replace-match (gethash (match-string 0 glyph-str)
619					    dev-glyph-glyph-2-hash)
620				   nil t glyph-str))))
621	  ;;; *** glyph reordering ***
622	(while (setq split-pos (string-match "$,4""(B\\|.$" glyph-str))
623	  (setq glyph-block (substring glyph-str 0 (1+ split-pos)))
624	  (setq glyph-str (substring glyph-str (1+ split-pos)))
625	  (setq
626	   glyph-block
627	   (if (string-match dev-glyph-right-modifier-regexp glyph-block)
628	       (sort (string-to-list glyph-block)
629		     (function (lambda (x y)
630			(< (get-char-code-property x 'composition-order)
631			   (get-char-code-property y 'composition-order)))))
632	     (sort (string-to-list glyph-block)
633		   (function (lambda (x y)
634		      (let ((xo (get-char-code-property x 'composition-order))
635			    (yo (get-char-code-property y 'composition-order)))
636			(if (= xo 2) nil (if (= yo 2) t (< xo yo)))))))))
637	  (setq glyph-str-list (nconc glyph-str-list glyph-block)))
638	  ;; concatenate and attach reference-points.
639	(setq glyph-str
640	      (cdr
641	       (apply
642		'nconc
643		(mapcar
644		 (function (lambda (x)
645		   (list
646		    (or (get-char-code-property x 'reference-point)
647		    '(5 . 3) ;; default reference point.
648		     )
649		    x)))
650		 glyph-str-list))))))
651      (compose-region from to glyph-str)))
652
653(provide 'devan-util)
654
655;;; arch-tag: 9bc4d6e3-f2b9-4110-886e-ff9b66b7eebc
656;;; devan-util.el ends here
657