//
// subtending signs
//

@EndOfAyah = [uni06DD];
@NumSign   = [uni0600];
@YearSign  = [uni0601];
@FootNote  = [uni0602];
@SafhaSign = [uni0603];

@NumSign3  = [uni0600.3];
@NumSign4  = [uni0600.4];

@SafhaSign3  = [uni0603.3];

#ifdef ITALIC
@Digits.all = [@Digits @Digits.rtl @Digits.ltr];
#else
@Digits.all = [@Digits];
#endif

lookup digit2small {
  sub @Digits by @Digits.small;
#ifdef ITALIC
  sub @Digits.rtl by @Digits.small2;
  sub @Digits.ltr by @Digits.small2;
#endif
} digit2small;

lookup digit2medi {
  sub @Digits by @Digits.medium;
#ifdef ITALIC
  sub @Digits.rtl by @Digits.medium2;
  sub @Digits.ltr by @Digits.medium2;
#endif
} digit2medi;

lookup Sign3 {
  sub @NumSign by @NumSign3;
  sub @SafhaSign by @SafhaSign3;
} Sign3;

lookup Sign4 {
  sub @NumSign by @NumSign4;
} Sign4;

// first we replace any sequence of digits (up to two, three or four places)
// following a subtending sign by smaller alternates
feature calt {
  // Uniscribe or HarfBuzz ‘latn’
  sub [@EndOfAyah          ] @Digits.all' lookup digit2small @Digits.all' lookup digit2small @Digits.all' lookup digit2small;
  sub [@EndOfAyah @FootNote] @Digits.all' lookup digit2small @Digits.all' lookup digit2small;
  sub [@EndOfAyah @FootNote] @Digits.all' lookup digit2small;

  sub [@NumSign @YearSign] @Digits.all' lookup digit2medi @Digits.all' lookup digit2medi @Digits.all' lookup digit2medi @Digits.all' lookup digit2medi;
  sub [@NumSign @YearSign @SafhaSign] @Digits.all' lookup digit2medi @Digits.all' lookup digit2medi @Digits.all' lookup digit2medi;
  sub [@NumSign @YearSign @SafhaSign] @Digits.all' lookup digit2medi @Digits.all' lookup digit2medi;
  sub [@NumSign @YearSign @SafhaSign] @Digits.all' lookup digit2medi;

  // HarfBuzz ‘arab’
  sub @Digits.all' lookup digit2small @Digits.all' lookup digit2small @Digits.all' lookup digit2small [@EndOfAyah];
  sub @Digits.all' lookup digit2small @Digits.all' lookup digit2small [@EndOfAyah @FootNote];
  sub @Digits.all' lookup digit2small [@EndOfAyah @FootNote];

  sub @Digits.all' lookup digit2medi @Digits.all' lookup digit2medi @Digits.all' lookup digit2medi @Digits.all' lookup digit2medi [@NumSign @YearSign]' lookup Sign4;
  sub @Digits.all' lookup digit2medi @Digits.all' lookup digit2medi @Digits.all' lookup digit2medi [@NumSign @YearSign @SafhaSign]' lookup Sign3;
  sub @Digits.all' lookup digit2medi @Digits.all' lookup digit2medi [@NumSign @YearSign @SafhaSign];
  sub @Digits.all' lookup digit2medi [@NumSign @YearSign @SafhaSign];
} calt;

// Should have been merged with the Uniscribe lookup above as we do for
// HarfBuzz, but then Uniscribe would crash
feature calt {
  sub @NumSign'   lookup Sign4 @Digits.medium @Digits.medium @Digits.medium @Digits.medium;
  sub @NumSign'   lookup Sign3 @Digits.medium @Digits.medium @Digits.medium;
  sub @SafhaSign' lookup Sign3 @Digits.medium @Digits.medium @Digits.medium;
} calt;

// then we contextually kern the sign with the digits so that they get enclosed by it
feature kern {
  // Uniscribe or HarfBuzz ‘latn’
  pos @EndOfAyah @Digits.small'  <-2210 0 -600 0> @Digits.small'  <-1610 0 -600 0> @Digits.small'  <-1010 0 -600 0>;
  pos @EndOfAyah @Digits.small'  <-1910 0 -600 0> @Digits.small'  <-1310 0 -600 0>;
  pos @EndOfAyah @Digits.small'  <-1610 0 -600 0>;

  pos @FootNote @Digits.small'  <-1390 0 -600 0> @Digits.small'  <-790  0 -600 0>;
  pos @FootNote @Digits.small'  <-1090 0 -600 0>;

  pos @NumSign4 @Digits.medium' <-3850 0 -900 0> @Digits.medium' <-2950 0 -900 0> @Digits.medium' <-2050 0 -900 0> @Digits.medium' <-1150 0 -900 0>;
  pos @NumSign3 @Digits.medium' <-2950 0 -900 0> @Digits.medium' <-2050 0 -900 0> @Digits.medium' <-1150 0 -900 0>;
  pos @NumSign  @Digits.medium' <-2150 0 -900 0> @Digits.medium' <-1250 0 -900 0>;
  pos @NumSign  @Digits.medium' <-1700 0 -900 0>;

  pos @YearSign @Digits.medium' <-3625 0 -900 0> @Digits.medium' <-2725 0 -900 0> @Digits.medium' <-1825 0 -900 0> @Digits.medium' <-925  0 -900 0>;
  pos @YearSign @Digits.medium' <-3175 0 -900 0> @Digits.medium' <-2275 0 -900 0> @Digits.medium' <-1375 0 -900 0>;
  pos @YearSign @Digits.medium' <-2725 0 -900 0> @Digits.medium' <-1825 0 -900 0>;
  pos @YearSign @Digits.medium' <-2275 0 -900 0>;

  pos @SafhaSign3 @Digits.medium' <-3275 0 -900 0> @Digits.medium' <-2375 0 -900 0> @Digits.medium' <-1475 0 -900 0>;
  pos @SafhaSign  @Digits.medium' <-2225 0 -900 0> @Digits.medium' <-1325 0 -900 0>;
  pos @SafhaSign  @Digits.medium' <-1775 0 -900 0>;

  // HarfBuzz ‘arab’
  pos @Digits.small'  <-1010 0 -600 0> @Digits.small'  <-1610 0 -600 0> @Digits.small'  <-2210 0 -600 0> @EndOfAyah;
  pos @Digits.small'  <-1310 0 -600 0> @Digits.small'  <-1910 0 -600 0> @EndOfAyah;
  pos @Digits.small'  <-1610 0 -600 0> @EndOfAyah;

  pos @Digits.small'  <-790  0 -600 0> @Digits.small'  <-1390 0 -600 0> @FootNote;
  pos @Digits.small'  <-1090 0 -600 0> @FootNote;

  pos @Digits.medium' <-1150 0 -900 0> @Digits.medium' <-2050 0 -900 0> @Digits.medium' <-2950 0 -900 0> @Digits.medium' <-3850 0 -900 0> @NumSign4;
  pos @Digits.medium' <-1150 0 -900 0> @Digits.medium' <-2050 0 -900 0> @Digits.medium' <-2950 0 -900 0> @NumSign3;
  pos @Digits.medium' <-1250 0 -900 0> @Digits.medium' <-2150 0 -900 0> @NumSign;
  pos @Digits.medium' <-1700 0 -900 0> @NumSign;

  pos @Digits.medium' <-925  0 -900 0> @Digits.medium' <-1825 0 -900 0> @Digits.medium' <-2725 0 -900 0> @Digits.medium' <-3625 0 -900 0> @YearSign;
  pos @Digits.medium' <-1375 0 -900 0> @Digits.medium' <-2275 0 -900 0> @Digits.medium' <-3175 0 -900 0> @YearSign;
  pos @Digits.medium' <-1825 0 -900 0> @Digits.medium' <-2725 0 -900 0> @YearSign;
  pos @Digits.medium' <-2275 0 -900 0> @YearSign;

  pos @Digits.medium' <-1475 0 -900 0> @Digits.medium' <-2375 0 -900 0> @Digits.medium' <-3275 0 -900 0> @SafhaSign3;
  pos @Digits.medium' <-1325 0 -900 0> @Digits.medium' <-2225 0 -900 0> @SafhaSign;
  pos @Digits.medium' <-1775 0 -900 0> @SafhaSign;
} kern;
