// This code is kind of a big hack. We want to support a special form of name of
// Allah, but the sequence <Alef><Lam><Lam><Heh> can occur in many unrelated
// words, showing the special form for those wards is very confusing and can
// even be offending to some people.  Thus we define explicit contexts,
// including Tashkil marks that can't but be the name of Allah and ignore the
// special form for any other occurrences, but this is far from being simple.
//
// The first calt substitutes any [<Alef>]<Lam><Lam><Heh> preceded by a letter
// that is known not to be part of any of the allowed words by a regular non
// special form.  The next block will then substitute the allowed context, given
// it meets a certain criteria.
//
// The idea originated from this forum thread:
// http://www.graphics4arab.com/showthread.php?t=3975
//  and I just adapted it for Amiri, credit where credit is due.

lookup Lellah {
  sub uni0644.init by uni0644.init_Lellah;
  sub uni0644.medi by uni0644.medi_Lellah;
  sub uni0647.fina by uni0647.fina_Lellah;
  sub uni06C1.fina by uni0647.fina_Lellah;
} Lellah;

lookup Lellah2 {
  sub uni0644.medi by uni0644.medi_Lellah uni0651 uni0670;
} Lellah2;

lookup Lellah3 {
  sub uni0644.medi by uni0644.medi_FaLellah;
} Lellah3;

// Lellah4 & Lellah5 are redundant and can be substituted by Lellah lookup, but
// this is a hack for a Uniscribe bug
lookup Lellah4 {
  sub uni0644.init by uni0644.init_Lellah;
} Lellah4;

lookup Lellah5 {
  sub uni0644.medi by uni0644.medi_Lellah;
} Lellah5;

// shortcuts
@Li = [uni0644.init];
@Lm = [uni0644.medi];
@Ai = [uni0627 uni0622 uni0671];
@Af = [uni0627.fina];
@Hf = [uni0647.fina uni06C1.fina];
@xF = [uni064E uni0670];

#ifndef QURAN
lookup LamLamInitX {
  sub uni0644.init by uni0644.init_LamLamInit;
  sub uni0644.medi by uni0644.medi_LamLamInit;
} LamLamInitX;

// define to not include [اآٱ]
@aAlf_isol = [uni0625 uni0774 uni0773 uni0623 uni0675 uni0672 uni0673];
// define to not include [اٱ]
@aAlf_fina = [uni0625.fina uni0774.fina uni0773.fina uni0623.fina uni0622.fina
              uni0675.fina uni0672.fina uni0673.fina];
// define to not include [و]
@aWaw_isol = [uni06CB uni0624 uni06CA uni06CF uni0778 uni06C6 uni06C7 uni06C4
              uni06C5 uni0676 uni0677 uni06C8 uni06C9 uni0779];
// define to not include [بت]
@aBaa_init = [uni0777.init uni0680.init uni0776.init uni06BC.init uni0750.init
              uni0756.init uni0768.init uni06CE.init uni0775.init uni06BD.init
              uni0626.init uni066E.init uni0620.init uni064A.init uni06BB.init
              uni067F.init uni0755.init uni067D.init uni067E.init uni067B.init
              uni067A.init uni0751.init uni0646.init uni0753.init uni0752.init
              uni0678.init uni063D.init uni062B.init uni0679.init uni06B9.init
              uni0769.init uni0649.init uni067C.init uni0754.init uni06D1.init
              uni06D0.init uni06BA.init uni06CC.init uni0767.init];
// define to not include [ف]
@aFaa_init = [uni066F.init uni0761.init uni0760.init uni0642.init uni06A8.init
              uni06A1.init uni06A2.init uni06A3.init uni06A4.init uni06A5.init
              uni06A6.init uni06A7.init];
// define to not include [ه]
@aHeh_init = [uni06C1.init];
// define to not include [ك] and include [ڪ]
@aKaf_init = [uni063B.init uni063C.init uni077F.init uni0764.init uni06AA.init
              uni06B0.init uni06B3.init uni06B2.init uni06AB.init uni06AC.init
              uni06AD.init uni06AE.init uni06AF.init uni06A9.init uni06B4.init
              uni0763.init uni0762.init uni06B1.init];

@Ignore    = [@aAyn.fina @aAyn.isol @aAlf_isol @aAlf_fina @aBaa.fina @aBaa.isol
              @aDal.fina @aDal.isol @aFaa.fina @aFaa.isol @aHaa.fina @aHaa.isol
              @aHeh.fina @aHeh.isol @aKaf.fina @aKaf.isol @aLam.fina @aLam.isol
              @aMem.fina @aMem.isol @aNon.fina @aNon.isol @aQaf.fina @aQaf.isol
              @aRaa.fina @aRaa.isol @aSad.fina @aSad.isol @aSen.fina @aSen.isol
              @aTaa.fina @aTaa.isol @aWaw.fina @aWaw_isol @aYaa.fina @aYaa.isol];

@Ignore2   = [@aAyn.init @aAyn.medi @aBaa_init @aBaa.medi @aFaa_init @aFaa.medi
              @aHaa.init @aHaa.medi @aHeh_init @aHeh.medi @aKaf_init @aKaf.medi
              @aLam.init @aLam.medi @aMem.init @aMem.medi @aSad.init @aSad.medi
              @aSen.init @aSen.medi @aTaa.init @aTaa.medi];

feature calt {
  lookupflag IgnoreMarks;
  sub @Ignore       @Li' lookup LamLamInitX @Lm' lookup LamLamInitX @Hf;
  sub @Ignore  @Ai' @Li' lookup LamLamInitX @Lm' lookup LamLamInitX @Hf;
  sub @Ignore2 @Af' @Li' lookup LamLamInitX @Lm' lookup LamLamInitX @Hf;
} calt;

feature calt {
  // لله
  sub @Li' lookup Lellah4 @Lm' lookup Lellah2              @Hf' lookup Lellah;
  sub @Li' lookup Lellah  @Lm' lookup Lellah uni0651'      @Hf' lookup Lellah;
  sub @Li' lookup Lellah  @Lm' lookup Lellah uni0651' @xF' @Hf' lookup Lellah;

  // لِله
  sub @Li' lookup Lellah4 uni0650' @Lm' lookup Lellah2              @Hf' lookup Lellah;
  sub @Li' lookup Lellah  uni0650' @Lm' lookup Lellah uni0651'      @Hf' lookup Lellah;
  sub @Li' lookup Lellah  uni0650' @Lm' lookup Lellah uni0651' @xF' @Hf' lookup Lellah;

  // لِّله
  sub @Li' lookup Lellah4 uni0651'uni0650' @Lm' lookup Lellah2              @Hf' lookup Lellah;
  sub @Li' lookup Lellah  uni0651'uni0650' @Lm' lookup Lellah uni0651'      @Hf' lookup Lellah;
  sub @Li' lookup Lellah  uni0651'uni0650' @Lm' lookup Lellah uni0651' @xF' @Hf' lookup Lellah;

  // فلله
  sub uni0641.init'          @Lm' lookup Lellah3          @Lm' lookup Lellah2               @Hf' lookup Lellah;
  sub uni0641.init'          @Lm' lookup Lellah3          @Lm' lookup Lellah5 uni0651'      @Hf' lookup Lellah;
  sub uni0641.init'          @Lm' lookup Lellah3          @Lm' lookup Lellah5 uni0651' @xF' @Hf' lookup Lellah;
  sub uni0641.init' uni064E' @Lm' lookup Lellah3          @Lm' lookup Lellah2               @Hf' lookup Lellah;
  sub uni0641.init' uni064E' @Lm' lookup Lellah3          @Lm' lookup Lellah5 uni0651'      @Hf' lookup Lellah;
  sub uni0641.init' uni064E' @Lm' lookup Lellah3          @Lm' lookup Lellah5 uni0651' @xF' @Hf' lookup Lellah;
  sub uni0641.init'          @Lm' lookup Lellah3 uni0650' @Lm' lookup Lellah2               @Hf' lookup Lellah;
  sub uni0641.init'          @Lm' lookup Lellah3 uni0650' @Lm' lookup Lellah5 uni0651'      @Hf' lookup Lellah;
  sub uni0641.init'          @Lm' lookup Lellah3 uni0650' @Lm' lookup Lellah5 uni0651' @xF' @Hf' lookup Lellah;
  sub uni0641.init' uni064E' @Lm' lookup Lellah3 uni0650' @Lm' lookup Lellah2               @Hf' lookup Lellah;
  sub uni0641.init' uni064E' @Lm' lookup Lellah3 uni0650' @Lm' lookup Lellah5 uni0651'      @Hf' lookup Lellah;
  sub uni0641.init' uni064E' @Lm' lookup Lellah3 uni0650' @Lm' lookup Lellah5 uni0651' @xF' @Hf' lookup Lellah;
} calt;

// InDesign does not apply the heh substituion, so we try to apply it again.
feature calt {
  lookupflag IgnoreMarks;
  sub uni0644.medi_Lellah' @Hf' lookup Lellah;
} calt;
#else // QURAN
// This a stripped down version of the above for use in Amiri Quran,
// because here we can safely assume the [<Faa>]<Lam><Lam><Heh> sequence always
// mean the name of Allah.

feature calt {
  lookupflag IgnoreMarks;
  // لله
  sub @Li' lookup Lellah4
      @Lm' lookup Lellah5
      @Hf' lookup Lellah;

  // فلله
  sub [uni0641.init]'
      @Lm' lookup Lellah3
      @Lm' lookup Lellah5
      @Hf' lookup Lellah;
} calt;
#endif
