;; KHMR-OTF.flt -- Font Layout Table for Khmer OpenType fonts
;; Copyright (C) 2005, 2007  AIST (H15PRO112)
;; See the end for copying conditions.

(font layouter khmr-otf nil
      (font (nil nil unicode-bmp :otf=khmr=pres)))

;;; <li> KHMR-OTF.flt
;;;
;;; For Khmer OpenType fonts to draw Khmer.
;;; A Font is available from 
;;; <http://www.khmeros.info/drupal/?q=en/download/fonts>.

(category
 (0x1780 0x17FF ?E)			; E: else
 (0x19E0 0x19FF ?E)
 (0x1780 0x17B3 ?C)			; C: consonant & independent vowel
 (0x179A	?R)			; R: RO
 (0x17B6	?p)			; p: vowel sign (post)
 (0x17B7 0x17BA ?a)			; a: vowel sign (above)
 (0x17BB 0x17BD ?b)			; b: vowel sign (below)
 (0x17BE	?A)			; A: vowel sign (two-part, above)
 (0x17BF 0x17C0 ?P)			; P: vowel sign (two-part, post)
 (0x17C1 0x17C3 ?m)			; m: vowel sign (pre)
 (0x17C4 0x17C5 ?P)
 (0x17C6	?s)			; s: sign (above)
 (0x17C7 0x17C8 ?S)			; S: sign (post)
 (0x17C9 0x17CA ?c)			; c: consonant shifter
 (0x17CB 0x17D1 ?s)
 (0x17CC	?r)			; r: ROBAT
 (0x17D2	?H)			; H: COENG
 (0x17D3	?s)
 (0x17DD	?s)
 (0x200C	?N)			; N: ZERO WIDTH NON-JOINER
 (0x200D	?J)			; J: ZERO WIDTH JOINER
 )

;; Generic syllable pattern is as follows.
;; [CR](r|N?c)?(HCr?)*(HR)?(HCr?)*[NJ]?(m|b|a|A|p|P)?s*S?(H[CR])?

;; Step 0.
;; Move m to the beginning.
;; Split A and P.
;; Exchange the order of pP and s.
;; Move HR before the base.

(generator
 (0
  (cond

   ;; pre vowel sign
   ;;1    2       3       4  5     67            8  9    10
   ("([CR](r|N?c)?(HCr?)*)(HR(r)?)?((HCr?)*[NJ]?)(m)(s*S?(H[CR])?)"
    < | (8 =) (4 = =) (1 = *) (5 =) (6 = *) (9 = *) | > )

   ;; two-part above vowel sign
   ;;1    2       3       4  5     67                8
   ("([CR](r|N?c)?(HCr?)*)(HR(r)?)?((HCr?)*[NJ]?As*S?(H[CR])?)"
    < | 0x17C1 (4 = =) (1 = *) (5 =) (6 = *) | > )

   ;; post vowel sign
   ;;1    2       3       4  5     67            8  9   10 11
   ("([CR](r|N?c)?(HCr?)*)(HR(r)?)?((HCr?)*[NJ]?)(p)(s*)(S?(H[CR])?)"
    < | (4 = =) (1 = *) (5 =) (6 = *) (9 = *) (8 =) (10 = *) | > )

   ;; two-part post vowel sign
   ;;1    2       3       4  5     67            8  9   10 11
   ("([CR](r|N?c)?(HCr?)*)(HR(r)?)?((HCr?)*[NJ]?)(P)(s*)(S?(H[CR])?)"
    < | 0x17C1 (4 = =) (1 = *) (5 =) (6 = *) (9 = *) (8 =) (10 = *) | > )

   ;; other vowel signs or no vowel sign
   ;;1    2       3       4  5     67                     8
   ("([CR](r|N?c)?(HCr?)*)(HR(r)?)?((HCr?)*[NJ]?[b|a]?s*S?(H[CR])?)"
    < | (4 = =) (1 = *) (5 =) (6 = *) | > )

   ("." =))
  *))

;; Now a syllable looks like below.
;; m?(HR)?[CR](r|N?c)?(HCr?)*[NJ]?(b|a|A)?s*(p|P)?S?(H[CR])?

;; Step 1.
;; Set the form of consonant shifter.

(generator
 (0
  (cond
   ;; shifter + above vowel sign without ZWNJ
   ;; Shifter take blwf.  HR takes pref.  HC's take blwf or pstf.
   ;; 1   2    3     4      5     6     7     8
   (" (m)?(HR)?([CR]c(HCr?)*(N|J)?(a|A))(s*S?)(H[CR])? "
    |
    (1 =)
    (2 otf:khmr=pref+)
    (3 otf:khmr=blwf,abvf,pstf+)
    (7 = *)
    (8 otf:khmr=blwf,pstf+)
    | )

   ;; shifter + ZWNJ + above vowel sign, or, shifter without above vowel sign
   ;; Shifter stays above.  HR takes pref.  HC's take blwf or pstf.
   ;; 1   2    3        45       6           7   8     9   10  11
   (" (m)?(HR)?([CR]N?c)((HCr?)*)([NJ]?[ba]?)(A)?(s*p?)(P)?(S)?(H[CR])? "
    |
    (1 =)
    (2 otf:khmr=pref+)
    (3 = *)
    (4 otf:khmr=blwf,pstf+)
    (6 = *)
    (7 otf:khmr=abvf+)
    (8 = *)
    (9 otf:khmr=pstf+)
    (10 =)
    (11 otf:khmr=blwf,pstf+)
    | )

   ;; no shifter
   ;; 1   2    3       45       6           7   8     9   10  11
   (" (m)?(HR)?([CR]r?)((HCr?)*)([NJ]?[ba]?)(A)?(s*p?)(P)?(S)?(H[CR])? "
    |
    (1 =)
    (2 otf:khmr=pref+)
    (3 = *)
    (4 otf:khmr=blwf,pstf+)
    (6 = *)
    (7 otf:khmr=abvf+)
    (8 = *)
    (9 otf:khmr=pstf+)
    (10 =)
    (11 otf:khmr=blwf,pstf+)
    | )

   ("." =))
  *))

;; Now a syllable looks like below.  ~ characters are OTF feature applied.
;; m?(HR)?[CR](r|c)?(HCr?)*[NJ]?(b|a|A)?s*(p|P)?S?(H[CR])?
;;    ~~   ~~    ~   ~~~~            ~       ~     ~~~~~

;; Step 2.
;; Concatenate adjacent Khmer syllables.
;; Remove J.  Retain N to prevent ligature.

(generator
 (0
  (cond
   ("  ")
   ("J")
   ("." =))
  *))

;; Step 3.
;; Apply other OTF features.

(generator
 (0
  (cond
   (" ([^ ]*) "
    (1 otf:khmr=pres,blws,abvs,psts,clig))
   ("."
    [ = ]))
  *))

;; Step 4.
;; Remove N to clean up.
(generator
 (0
  (cond
   ("N")
   ("." =))
  *))

;; Copyright (C) 2005, 2007
;;   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:
