;; MLYM-OTF.flt -- Font Layout Table for Malayalam OpenType fonts
;; Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2010 AIST (H15PRO112)
;; See the end for copying conditions.

(font layouter mlym-otf nil
      (version "1.6.0")
      (font (nil nil unicode-bmp :otf=mlym)))

;;; <li> MLYM-OTF.flt
;;;
;;; For Malayalam OpenType fonts to draw the reformed Malayalam script.

;; AnjaliOldLipi.ttf, Dyuthi3.ttf, Meera_04.ttf, Rachana_04.ttf, suruma2.ttf
;; blwf: 0d32
;; pstf: 0d2f 0d30 0d35

;; RaghuMalayalamSans2.ttf
;; blwf: 0d32
;; pstf: 0d2f 0d30 0d35 0d41

;; lohit_ml.ttf
;; blwf: 0d32
;; pstf: 0d2f 0d30 0d31 0d35 0d41

;; Kalyani121.ttf
;; blwf: N/A
;; pstf: 0d2f 0d30 0d32 0d35 0d41

(category
 (0x0D00 0x0D7F	?X)			; generic
 (0x0D02 0x0D03	?A)			; SIGN ANUSVARA .. VISARGA
 (0x0D05 0x0D14	?V)			; LETTER A .. AU
 (0x0D15 0x0D39 ?C)			; LETTER KA .. HA
 (0x0D2F	?P)			; LETTER YA
 (0x0D30	?R)			; LETTER RA
 (0x0D32	?B)			; LETTER LA
 (0x0D35	?P)			; LETTER VA
 (0x0D3E 0x0D44	?p)			; VOWEL SIGN AA .. VOCALIC RR
 (0x0D46 0x0D48	?m)			; VOWEL SIGN E .. AI
 (0x0D4D	?H)			; SIGN VIRAMA
 (0x0D57	?p)			; AU LENGTH MARK
 (0x0D60 0x0D61	?V)			; LETTER VOCALIC RR .. VOCALIC LL
 (0x0D62 0x0D63 ?b)			; VOWEL SIGN VOCALIC L .. LL

 (0x200C	?N)			; ZWNJ
 (0x200D	?J)			; ZWJ
 (0x25CC	?X)			; DOTTED CIRCLE

 (pstf		?r)
 )

;; Stage 0
;; Preprocessing
(generator
 (0
  (cond
   ;; Decompose two-part vowel signs
   ((0x0D4A) 0x0D46 0x0D3E)
   ((0x0D4B) 0x0D47 0x0D3E)
   ((0x0D4C) 0x0D46 0x0D57)

   ("." =))
  *))

;; Stage 1
;; Syllable identification
(generator
 (0
  (cond
   ;; Consonant-based syllables
   ;;1      23                 4
   ("([CBPR]((J?H|HJ?)[CBPR])*)(H[NJ]?|m?p?A?)"
    < |
    (cond
     (".+HJ$" :otf=mlym=nukt,akhn+)
     ("(.+)(H)N?$" (1 :otf=mlym=nukt,akhn+) (2 =) 0x200C)
     (".+" :otf=mlym=nukt,akhn+))
    | >)

   ;; Syllables with an independent vowel
   ("V(J?H[BPR])?m?p?A?"
    < | = * | >)

   ;; Two-part vowel signs
   ((0x0D46 0x0D3E)
    (cond
     ((font-facility 0x25CC) < = 0x25CC = >)
     (".+" [ 0x0D4A ])))
   ((0x0D47 0x0D3E)
    (cond
     ((font-facility 0x25CC) < = 0x25CC = >)
     (".+" [ 0x0D4B ])))
   ((0x0D46 0x0D57)
    (cond
     ((font-facility 0x25CC) < = 0x25CC = >)
     (".+" [ 0x0D4C ])))

   ;; Combining marks are displayed with a DOTTED CIRCLE.
   ("m"
    (cond
     ((font-facility 0x25CC) < = 0x25CC >)
     ("." [ = ])))
   ("[HbpA]"
    (cond
     ((font-facility 0x25CC) < 0x25CC = >)
     ("." [ = ])))

   ;; Isolated blwf and pstf are displayed with a DOTTED CIRCLE in the
   ;; next stage.
   ("(J)(H)([BPR])"
    < | (1 =) (3 =) (2 =) | >)

   ("." =))
  *))

;; Stage 2
;; Reorder halant for blwf and pstf
(generator
 (0
  (cond
   ;; Isolated blwf and pstf
   (" J([BP]H) "
    (cond
     ((font-facility 0x25CC) < 0x25CC (1 :otf=mlym=blwf,pstf+) >)
     (" J([BP]H) " [ (1 :otf=mlym=blwf,pstf+) ])))

   ;; Isolated pref
   (" J(RH) "
    (cond
     ((font-facility 0x25CC) < (1 :otf=mlym=pstf+) 0x25CC >)
     (" J(RH) " [ (1 :otf=mlym=pstf+) ])))

   ;; Overt halnt forms
   (" ([^ ]+)(HN) "
    | (1 reorder) (2 = =) |)

   ;; Ordinary syllables
   (" ([^mbpA ]+)(m?p?A?) "
    | (1 reorder) (2 = *) |)

   ("." =))
  *)

 (reorder
  (cond
   ;; pref after pstf
   ("(.|.+([^PJ]|JP))((J?HP)*J?HR)$"
    (1 reorder-B *) (3 reorder-any *))
   ;; pref between blwf and pstf
   ("(.*[^J])(J?HR(J?HP)+)$"
    (1 reorder-B *) (2 reorder-any *))
   ;; pref before blwf
   ("(.*[^J])(J?HR(J?HB)+(J?HP)*)$"
    (1 reorder-B *) (2 reorder-any *))
   ;; no pref
   ("(.|.+([^PJ]|JP|HJ))((J?HP)*)$"
    (1 reorder-B *) (3 reorder-any *))))

 (reorder-B
  (cond
   ("J?(H)(B)"
    (2 =) (1 =))
   ("." =)))

 (reorder-any
  ("J?(H)(.)"
   (2 =) (1 =)))
 )

;; Stage 3
;; Language forms and matra reordering
(generator
 (0
  (cond
   ;; Overt halant forms
   (" ([^ ]+)(HN) "
    | (1 blwf-pstf) (2 = =) |)

   ;; Other syllables
   ;; 1          2   3
   (" ([^mpA ]+)(m)?(p?A?) "
    | (2 =) (1 blwf-pstf) (3 = *) |)

   ("." =))
  *)

 (blwf-pstf
  (cond
   ;;1              23     4
   ("(.|.+BH|.+[^H])((PH)*)(RH)$"
    (4 :otf=mlym=pstf) (1 :otf=mlym=blwf+) (2 :otf=mlym=pstf+))
   ;;1   2   3
   ("(.*)(RH)((PH)+)$"
    (2 :otf=mlym=pstf+) (1 :otf=mlym=blwf+) (3 :otf=mlym=pstf+))
   ;;1   2   34     5
   ("(.*)(RH)((BH)+)((PH)*)$"
    (2 :otf=mlym=pstf+) (1 :otf=mlym=blwf+) (3 :otf=mlym=blwf+)
    (5 :otf=mlym=pstf+))
   ;;1              2
   ("(.|.+BH|.+[^H])((PH)*)$"
    (1 :otf=mlym=blwf+) (2 :otf=mlym=pstf+))))
 )

;; Stage 4
;; Final reordering (Move pre-base matra and pref after the last halant)
(generator
 (0
  (cond
   ;; With a pref, one or more intermediate-  and a final overt halant forms.
   ;; 1  2         3
   (" (r)([^ ]+HJ?)([^H ]+HN) "
    | (2 = *) (1 =) (3 = *) |)

   ;; With a pref and a final overt halant form.  Don't move the pref.
   (" r[^H ]+HN "
    = *)

   ;; 1     2         3
   (" (m?r?)([^ ]+HJ?)([^H ]+) "
    | (2 = *) (1 = *) (3 = *) |)

   ("." =))
  *))

;; Stage 5
;; Remove ZWNJ/ZWJ
(generator
 (0
  (cond
   ("[NJ]")

   ("." =))
  *))

;; Stage 6
;; GPOS processing
(generator
 (0
  (cond
   (" ([^ ]+) "
    (1 :otf=mlym=+abvm,blwm,dist))

   ("." =))
  *))

;; Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2010
;;   National Institute of Advanced Industrial Science and Technology (AIST)
;;   Registration Number H15PRO112

;; This file is part of the m17n database; a sub-part of the m17n
;; library.

;; The m17n library is free software; you can redistribute it and/or
;; modify it under the terms of the GNU Lesser General Public License
;; as published by the Free Software Foundation; either version 2.1 of
;; the License, or (at your option) any later version.

;; The m17n library is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; Lesser General Public License for more details.

;; You should have received a copy of the GNU Lesser General Public
;; License along with the m17n library; if not, write to the Free
;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;; Local Variables:
;; mode: emacs-lisp
;; End:
