(stage 2): Use iteration rather than recursive for reordering.
[m17n/m17n-db.git] / FLT / DEVA-OTF.flt
1 ;; DEVA-OTF.flt -- Font Layout Table for Devanagari OpenType fonts
2 ;; Copyright (C) 2003, 2004, 2007, 2008, 2010 AIST (H15PRO112)
3 ;; See the end for copying conditions.
4
5 (font layouter deva-otf nil
6       (version "1.6.0")
7       (font (nil nil unicode-bmp :otf=deva)))
8
9 ;;; <li> DEVA-OTF.flt
10 ;;;
11 ;;; For Devanagari OpenType fonts to draw the Devanagari script.
12
13 ;; Accepted modifier sequence:
14 ;; 1. matra (pre, below, above, post)
15 ;; 2. candrabindu (0901) or anusvara (0902)
16 ;; 3. udatta (0951) or anudatta (0952)
17 ;; 4. grave (0953) or acute (0954)
18 ;; 5. visarga (0903)
19
20 ;; Rendering glyph order:
21 ;; 1. below matra
22 ;; 2. anudatta (0952)
23 ;; 3. above- or post matra
24 ;; 4. reph
25 ;; 5. candrabindu (0901) or anusvara (0902)
26 ;; 6. udatta (0951)
27 ;; 7. visarga (0903)
28
29 (category
30  ;; X: generic
31  ;; V: independent vowel
32  ;; C: consonant (except for RA)
33  ;; R: consonant RA
34  ;; n: NUKTA
35  ;; H: HALANT
36  ;; m: vowel sign (pre)
37  ;; u: vowel sign (above)
38  ;; b: vowel sign (below)
39  ;; p: vowel sign (post)
40  ;; a: vowel modifier (above)
41  ;; A: vowel modifier (post)
42  ;; s: stress sign / accent (above)
43  ;; S: stress sign (below)
44  ;; Z: internal use
45  ;; N: ZWNJ
46  ;; J: ZWJ
47  (0x0900 0x097F ?X)                     ; generic
48  (0x0900 0x0902 ?a)                     ; SIGN INVERTED CANDRABINDU .. ANUSVARA
49  (0x0903        ?A)                     ; SIGN VISARGA
50  (0x0904 0x0914 ?V)                     ; LETTER SHORT A .. AU
51  (0x0915 0x0939 ?C)                     ; LETTER KA .. HA
52  (0x0930        ?R)                     ; LETTER RA
53  (0x093C        ?n)                     ; SIGN NUKTA
54  (0x093E        ?p)                     ; VOWEL SIGN AA
55  (0x093F        ?m)                     ; VOWEL SIGN I
56  (0x0940        ?p)                     ; VOWEL SIGN II
57  (0x0941 0x0944 ?b)                     ; VOWEL SIGN UU .. VOCALIC RR
58  (0x0945 0x0948 ?u)                     ; VOWEL SIGN CANDRA E .. AI
59  (0x0949 0x094C ?p)                     ; VOWEL SIGN CANDRA O .. AU
60  (0x094D        ?H)                     ; SIGN VIRAMA
61  (0x094E        ?m)                     ; VOWEL SIGN PRISHTHAMATRA E
62  (0x0951        ?s)                     ; STRESS SIGN UDATTA
63  (0x0952        ?S)                     ; STRESS SIGN ANUDATTA
64  (0x0953 0x0954 ?t)                     ; GRAVE ACCENT .. ACUTE ACCENT
65  (0x0955        ?u)                     ; VOWEL SIGN CHANDRA LONG E
66  (0x0958 0x095F ?C)                     ; LETTER QA .. YYA
67  (0x0960 0x0961 ?V)                     ; LETTER VOCALIC RR  .. VOCALIC LL
68  (0x0962 0x0963 ?b)                     ; VOWEL SIGN VOCALIC L .. VOCALIC LL
69  (0x0972        ?V)                     ; LETTER CANDRA A
70  (0x0979 0x097F ?C)                     ; LETTER ZHA .. BBA
71
72  (0x200C        ?N)                     ; ZWNJ
73  (0x200D        ?J)                     ; ZWJ
74  (0x25CC        ?X)                     ; DOTTED CIRCLE
75
76  (rphf          ?r)
77  )
78
79 ;; Stage 0
80 ;; Preprocessing
81 (generator
82  (0
83   (cond
84    ;; Compose a consonant and a nukta
85    ((0x0928 0x093C) 0x0929)
86    ((0x0930 0x093C) 0x0931)
87    ((0x0933 0x093C) 0x0934)
88    ((0x0915 0x093C) 0x0958)
89    ((0x0916 0x093C) 0x0959)
90    ((0x0917 0x093C) 0x095A)
91    ((0x091C 0x093C) 0x095B)
92    ((0x0921 0x093C) 0x095C)
93    ((0x0922 0x093C) 0x095D)
94    ((0x092B 0x093C) 0x095E)
95    ((0x092F 0x093C) 0x095F)
96
97    ("." =))
98   *))
99
100 ;; Stage 1
101 ;; Syllable identification
102 (generator
103  (0
104   (cond
105    ;; Syllables with an independent vowel
106    ;;
107    ("(RH)?(Vn?)((J?HR)?m?u?b?p?n?a?[sS]?t?A?)"
108     < | (1 = =) (2 :otf=deva=nukt+) (3 = *) | >)
109
110    ;; Consonant-based syllables
111    ;;1      23                 4
112    ("([CR]n?((J?H|HJ?)[CR]n?)*)(H[NJ]?|m?u?b?p?n?a?[sS]?t?A?)"
113     < | (1 :otf=deva=nukt,akhn+)
114     (4 (cond ("HJ" = =)
115              ("(H)N?" (1 =) 0x200C)
116              (".+" = *))) | >)
117
118    ;; Combining marks are displayed with a DOTTED CIRCLE.
119    ("m"
120     (cond
121      ((font-facility 0x25CC) < = 0x25CC >)
122      ("." [ = ])))
123    ("[nHubpaSsA]"
124     (cond
125      ((font-facility 0x25CC) < 0x25CC = >)
126      ("." [ = ])))
127
128    ;; Isolated blwf is displayed with a DOTTED CIRCLE in the next stage.
129    ("(J)(H)(R)"
130     < | (1 =) (3 =) (2 =) | >)
131
132    ("." =))
133   *))
134
135 ;; Stage 2
136 ;; Reorder halant for blwf
137 (generator
138  (0
139   (cond
140    ;; Isolated blwf
141    (" J(RH) "
142     (cond
143      ((font-facility 0x25CC) < 0x25CC :otf=deva=blwf+ >)
144      (" J(RH) " [ (1 :otf=deva=blwf+) ])))
145
146    ;; Syllables with an overt halant
147    (" (RHJ?)?([^ ]+)(HN) "
148     | (1 = *) (2 reorder *) (3 = =) |)
149
150    ;; Ordinary syllables
151    (" (RHJ?)?([^mubpasStA ]+)(m?u?b?p?n?a?[sS]?t?A?) "
152     | (1 = *) (2 reorder *) (3 = *) |)
153
154    ("." =))
155   *)
156
157  ;; In Devanagari and Gujarati, vattu has a half form.  Thus we handle
158  ;; the sequence CHRHC as (CH)half + (RH)blwf + C instead of
159  ;; reordering and interpreting it as C + (RH)blwf + H + C.
160  (reorder
161   (cond
162    ("J?(H)(R)$" (2 =) (1 =))
163    ("J?(HR)" (1 = =))
164    ("." =)))
165  )
166
167 ;; Stage 3
168 ;; Language forms and matra reordering
169 (generator
170  (0
171   (cond
172    ;; Overt halant forms
173    (" (RHJ?)?([^ ]+)(HN) "
174     |
175     (1 (cond ("RHJ" :otf=deva=half+)
176              ("RH" :otf=deva=rphf+)))
177     (2 (cond ("(.H)(.+)" (1 :otf=deva=half+) (2 :otf=deva=blwf,half+))
178              (".+" :otf=deva=blwf,half+)))
179     (3 = =) |)
180
181    ;; Other syllables
182    ;; 1      2               3     4     5     6       7   8
183    (" (RHJ?)?([^mubpasStA ]*)(mn?)?(un?)?(bn?)?(p?n?a?)(S)?(s?t?A?) "
184     | (3 = *)
185     (1 (cond ("RHJ" :otf=deva=half+)
186              ("RH" :otf=deva=rphf+)))
187     (2 (cond ("(.H)(.+)" (1 :otf=deva=half+) (2 :otf=deva=blwf,half+))
188              (".+" :otf=deva=blwf,half+)))
189     (5 = *) (7 =) (4 = *) (6 = *) (8 = *) |)
190
191    ("." =))
192   *))
193
194 ;; Stage 4
195 ;; Final reordering #1 (Move pre-base matra after the last halant)
196 (generator
197  (0
198   (cond
199    ;; 1    2         3
200    (" (mn?)([^ ]+HJ?)([^H ]+) "
201     | (2 = *) (1 = *) (3 = *) |)
202
203    ("." =))
204   *))
205
206 ;; Stage 5
207 ;; Final reordering #2 (Move reph after the first halant)
208 (generator
209  (0
210   (cond
211    ;; Syllables with a reph and an explicit halant
212    ;; 1     2  3             4
213    (" (mn?)?(r)([^H ]+HJ?)([^ ]*) "
214     | (1 = *) (3 = *) (2 =) (4 = *) |)
215
216    ;; A reph without explicit halant
217    ;; 1     2  3          4
218    (" (mn?)?(r)([^astA ]+)(a?s?t?A?) "
219     | (1 = *) (3 = *) (2 =) (4 = *) |)
220
221    ("." =))
222   *))
223
224 ;; Stage 6
225 ;; Nukta for matra, vattu and presentation forms
226 (generator
227  (0
228   (cond
229    (" ([^ ]+) "
230     | (1 :otf=deva=nukt,vatu,pres,abvs,blws,psts,haln+) |)
231
232    ("." =))
233   *))
234
235 ;; Stage 7
236 ;; Remove ZWNJ/ZWJ
237 (generator
238  (0
239   (cond
240    ("[NJ]")
241
242    ("." =))
243   *))
244
245 ;; Stage 8
246 ;; GPOS processing
247 (generator
248  (0
249   (cond
250    (" ([^ ]+) "
251     (1 :otf=deva=+abvm,blwm,dist))
252
253    ("." =))
254   *))
255
256 ;; Copyright (C) 2003, 2004, 2007, 2008, 2010
257 ;;   National Institute of Advanced Industrial Science and Technology (AIST)
258 ;;   Registration Number H15PRO112
259
260 ;; This file is part of the m17n database; a sub-part of the m17n
261 ;; library.
262
263 ;; The m17n library is free software; you can redistribute it and/or
264 ;; modify it under the terms of the GNU Lesser General Public License
265 ;; as published by the Free Software Foundation; either version 2.1 of
266 ;; the License, or (at your option) any later version.
267
268 ;; The m17n library is distributed in the hope that it will be useful,
269 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
270 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
271 ;; Lesser General Public License for more details.
272
273 ;; You should have received a copy of the GNU Lesser General Public
274 ;; License along with the m17n library; if not, write to the Free
275 ;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
276 ;; Boston, MA 02110-1301, USA.
277
278 ;; Local Variables:
279 ;; mode: emacs-lisp
280 ;; End: