]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/layout/IndicReordering.cpp
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / layout / IndicReordering.cpp
index ae29042860c93d0bd51feeb3ca3d701127dea288..7554d45311f9978ce5ca42f12a478f986231ee8a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *
- * (C) Copyright IBM Corp. 1998-2004 - All Rights Reserved
+ * (C) Copyright IBM Corp. 1998-2006 - All Rights Reserved
  *
  */
 
 
 U_NAMESPACE_BEGIN
 
-class ReorderingOutput : public UMemory {
+#define loclFeatureTag LE_LOCL_FEATURE_TAG
+#define initFeatureTag LE_INIT_FEATURE_TAG
+#define nuktFeatureTag LE_NUKT_FEATURE_TAG
+#define akhnFeatureTag LE_AKHN_FEATURE_TAG
+#define rphfFeatureTag LE_RPHF_FEATURE_TAG
+#define blwfFeatureTag LE_BLWF_FEATURE_TAG
+#define halfFeatureTag LE_HALF_FEATURE_TAG
+#define pstfFeatureTag LE_PSTF_FEATURE_TAG
+#define vatuFeatureTag LE_VATU_FEATURE_TAG
+#define presFeatureTag LE_PRES_FEATURE_TAG
+#define blwsFeatureTag LE_BLWS_FEATURE_TAG
+#define abvsFeatureTag LE_ABVS_FEATURE_TAG
+#define pstsFeatureTag LE_PSTS_FEATURE_TAG
+#define halnFeatureTag LE_HALN_FEATURE_TAG
+
+#define blwmFeatureTag LE_BLWM_FEATURE_TAG
+#define abvmFeatureTag LE_ABVM_FEATURE_TAG
+#define distFeatureTag LE_DIST_FEATURE_TAG
+
+#define loclFeatureMask 0x80000000UL
+#define rphfFeatureMask 0x40000000UL
+#define blwfFeatureMask 0x20000000UL
+#define halfFeatureMask 0x10000000UL
+#define pstfFeatureMask 0x08000000UL
+#define nuktFeatureMask 0x04000000UL
+#define akhnFeatureMask 0x02000000UL
+#define vatuFeatureMask 0x01000000UL
+#define presFeatureMask 0x00800000UL
+#define blwsFeatureMask 0x00400000UL
+#define abvsFeatureMask 0x00200000UL
+#define pstsFeatureMask 0x00100000UL
+#define halnFeatureMask 0x00080000UL
+#define blwmFeatureMask 0x00040000UL
+#define abvmFeatureMask 0x00020000UL
+#define distFeatureMask 0x00010000UL
+#define initFeatureMask 0x00008000UL
+
+class IndicReorderingOutput : public UMemory {
 private:
-    le_int32 fOutIndex;
+    le_int32   fOutIndex;
     LEUnicode *fOutChars;
 
     LEGlyphStorage &fGlyphStorage;
 
-    LEUnicode fMpre;
-    le_int32  fMpreIndex;
+    LEUnicode   fMpre;
+    le_int32    fMpreIndex;
 
-    LEUnicode fMbelow;
-    le_int32  fMbelowIndex;
+    LEUnicode   fMbelow;
+    le_int32    fMbelowIndex;
 
-    LEUnicode fMabove;
-    le_int32  fMaboveIndex;
+    LEUnicode   fMabove;
+    le_int32    fMaboveIndex;
 
-    LEUnicode fMpost;
-    le_int32  fMpostIndex;
+    LEUnicode   fMpost;
+    le_int32    fMpostIndex;
 
-    LEUnicode fLengthMark;
-    le_int32  fLengthMarkIndex;
+    LEUnicode   fLengthMark;
+    le_int32    fLengthMarkIndex;
 
-    const LETag *fMatraTags;
+    LEUnicode   fVirama;
+    le_int32    fViramaIndex;
+
+    FeatureMask fMatraFeatures;
     
-    le_int32 fMPreOutIndex;
+    le_int32    fMPreOutIndex;
     MPreFixups *fMPreFixups;
     
-    LEUnicode fVMabove;
-    LEUnicode fVMpost;
-    le_int32  fVMIndex;
-    const LETag *fVMTags;
+    LEUnicode   fVMabove;
+    LEUnicode   fVMpost;
+    le_int32    fVMIndex;
+    FeatureMask fVMFeatures;
     
-    LEUnicode fSMabove;
-    LEUnicode fSMbelow;
-    le_int32  fSMIndex;
-    const LETag *fSMTags;
+    LEUnicode   fSMabove;
+    LEUnicode   fSMbelow;
+    le_int32    fSMIndex;
+    FeatureMask fSMFeatures;
 
     void saveMatra(LEUnicode matra, le_int32 matraIndex, IndicClassTable::CharClass matraClass)
     {
@@ -56,24 +96,27 @@ private:
         if (IndicClassTable::isLengthMark(matraClass)) {
             fLengthMark = matra;
             fLengthMarkIndex = matraIndex;
+        } else if (IndicClassTable::isVirama(matraClass)) {
+            fVirama = matra;
+            fViramaIndex = matraIndex;
         } else {
-            switch (matraClass & IndicClassTable::CF_POS_MASK) {
-            case IndicClassTable::CF_POS_BEFORE:
+            switch (matraClass & CF_POS_MASK) {
+            case CF_POS_BEFORE:
                 fMpre = matra;
                 fMpreIndex = matraIndex;
                 break;
                
-            case IndicClassTable::CF_POS_BELOW:
+            case CF_POS_BELOW:
                 fMbelow = matra;
                 fMbelowIndex = matraIndex;
                 break;
                
-            case IndicClassTable::CF_POS_ABOVE:
+            case CF_POS_ABOVE:
                 fMabove = matra;
                 fMaboveIndex = matraIndex;
                 break;
                
-            case IndicClassTable::CF_POS_AFTER:
+            case CF_POS_AFTER:
                 fMpost = matra;
                 fMpostIndex = matraIndex;
                 break;
@@ -86,36 +129,52 @@ private:
     }
 
 public:
-    ReorderingOutput(LEUnicode *outChars, LEGlyphStorage &glyphStorage, MPreFixups *mpreFixups)
+    IndicReorderingOutput(LEUnicode *outChars, LEGlyphStorage &glyphStorage, MPreFixups *mpreFixups)
         : fOutIndex(0), fOutChars(outChars), fGlyphStorage(glyphStorage),
           fMpre(0), fMpreIndex(0), fMbelow(0), fMbelowIndex(0), fMabove(0), fMaboveIndex(0),
-          fMpost(0), fMpostIndex(0), fLengthMark(0), fLengthMarkIndex(0), fMatraTags(NULL),
-          fMPreOutIndex(-1), fMPreFixups(mpreFixups),
-          fVMabove(0), fVMpost(0), fVMIndex(0), fVMTags(NULL),
-          fSMabove(0), fSMbelow(0), fSMIndex(0), fSMTags(NULL)
+          fMpost(0), fMpostIndex(0), fLengthMark(0), fLengthMarkIndex(0), fVirama(0), fViramaIndex(0),
+          fMatraFeatures(0), fMPreOutIndex(-1), fMPreFixups(mpreFixups),
+          fVMabove(0), fVMpost(0), fVMIndex(0), fVMFeatures(0),
+          fSMabove(0), fSMbelow(0), fSMIndex(0), fSMFeatures(0)
     {
         // nothing else to do...
     }
 
-    ~ReorderingOutput()
+    ~IndicReorderingOutput()
     {
         // nothing to do here...
     }
 
     void reset()
     {
-        fMpre = fMbelow = fMabove = fMpost = fLengthMark = 0;
+        fMpre = fMbelow = fMabove = fMpost = fLengthMark = fVirama = 0;
         fMPreOutIndex = -1;
         
         fVMabove = fVMpost  = 0;
         fSMabove = fSMbelow = 0;
     }
 
-    le_bool noteMatra(const IndicClassTable *classTable, LEUnicode matra, le_uint32 matraIndex, const LETag *matraTags)
+    void writeChar(LEUnicode ch, le_uint32 charIndex, FeatureMask charFeatures)
+    {
+        LEErrorCode success = LE_NO_ERROR;
+
+        fOutChars[fOutIndex] = ch;
+
+        fGlyphStorage.setCharIndex(fOutIndex, charIndex, success);
+        fGlyphStorage.setAuxData(fOutIndex, charFeatures, success);
+
+        fOutIndex += 1;
+    }
+
+    le_bool noteMatra(const IndicClassTable *classTable, LEUnicode matra, le_uint32 matraIndex, FeatureMask matraFeatures, le_bool wordStart)
     {
         IndicClassTable::CharClass matraClass = classTable->getCharClass(matra);
 
-        fMatraTags  = matraTags;
+        fMatraFeatures  = matraFeatures;
+
+        if (wordStart) {
+            fMatraFeatures |= initFeatureMask;
+        }
 
         if (IndicClassTable::isMatra(matraClass)) {
             if (IndicClassTable::isSplitMatra(matraClass)) {
@@ -138,20 +197,20 @@ public:
         return FALSE;
     }
     
-    void noteVowelModifier(const IndicClassTable *classTable, LEUnicode vowelModifier, le_uint32 vowelModifierIndex, const LETag *vowelModifierTags)
+    void noteVowelModifier(const IndicClassTable *classTable, LEUnicode vowelModifier, le_uint32 vowelModifierIndex, FeatureMask vowelModifierFeatures)
     {
         IndicClassTable::CharClass vmClass = classTable->getCharClass(vowelModifier);
         
         fVMIndex = vowelModifierIndex;
-        fVMTags  = vowelModifierTags;
+        fVMFeatures  = vowelModifierFeatures;
         
         if (IndicClassTable::isVowelModifier(vmClass)) {
-           switch (vmClass & IndicClassTable::CF_POS_MASK) {
-           case IndicClassTable::CF_POS_ABOVE:
+           switch (vmClass & CF_POS_MASK) {
+           case CF_POS_ABOVE:
                fVMabove = vowelModifier;
                break;
             
-           case IndicClassTable::CF_POS_AFTER:
+           case CF_POS_AFTER:
                fVMpost = vowelModifier;
                break;
            
@@ -162,20 +221,20 @@ public:
         }
     }
     
-    void noteStressMark(const IndicClassTable *classTable, LEUnicode stressMark, le_uint32 stressMarkIndex, const LETag *stressMarkTags)
+    void noteStressMark(const IndicClassTable *classTable, LEUnicode stressMark, le_uint32 stressMarkIndex, FeatureMask stressMarkFeatures)
     {
        IndicClassTable::CharClass smClass = classTable->getCharClass(stressMark);
         
         fSMIndex = stressMarkIndex;
-        fSMTags  = stressMarkTags;
+        fSMFeatures  = stressMarkFeatures;
         
         if (IndicClassTable::isStressMark(smClass)) {
-            switch (smClass & IndicClassTable::CF_POS_MASK) {
-            case IndicClassTable::CF_POS_ABOVE:
+            switch (smClass & CF_POS_MASK) {
+            case CF_POS_ABOVE:
                 fSMabove = stressMark;
                 break;
             
-            case IndicClassTable::CF_POS_BELOW:
+            case CF_POS_BELOW:
                 fSMbelow = stressMark;
                 break;
            
@@ -193,82 +252,78 @@ public:
         }
     }
 
+    // Handles virama in Sinhala split vowels.
+    void writeVirama()
+    {
+        if (fVirama != 0) {
+            writeChar(fVirama, fViramaIndex, fMatraFeatures);
+        }
+    }
+
     void writeMpre()
     {
         if (fMpre != 0) {
             fMPreOutIndex = fOutIndex;
-            writeChar(fMpre, fMpreIndex, fMatraTags);
+            writeChar(fMpre, fMpreIndex, fMatraFeatures);
         }
     }
 
     void writeMbelow()
     {
         if (fMbelow != 0) {
-            writeChar(fMbelow, fMbelowIndex, fMatraTags);
+            writeChar(fMbelow, fMbelowIndex, fMatraFeatures);
         }
     }
 
     void writeMabove()
     {
         if (fMabove != 0) {
-            writeChar(fMabove, fMaboveIndex, fMatraTags);
+            writeChar(fMabove, fMaboveIndex, fMatraFeatures);
         }
     }
 
     void writeMpost()
     {
         if (fMpost != 0) {
-            writeChar(fMpost, fMpostIndex, fMatraTags);
+            writeChar(fMpost, fMpostIndex, fMatraFeatures);
         }
     }
 
     void writeLengthMark()
     {
         if (fLengthMark != 0) {
-            writeChar(fLengthMark, fLengthMarkIndex, fMatraTags);
+            writeChar(fLengthMark, fLengthMarkIndex, fMatraFeatures);
         }
     }
     
     void writeVMabove()
     {
         if (fVMabove != 0) {
-            writeChar(fVMabove, fVMIndex, fVMTags);
+            writeChar(fVMabove, fVMIndex, fVMFeatures);
         }
     }
         
     void writeVMpost()
     {
         if (fVMpost != 0) {
-            writeChar(fVMpost, fVMIndex, fVMTags);
+            writeChar(fVMpost, fVMIndex, fVMFeatures);
         }
     }
     
     void writeSMabove()
     {
         if (fSMabove != 0) {
-            writeChar(fSMabove, fSMIndex, fSMTags);
+            writeChar(fSMabove, fSMIndex, fSMFeatures);
         }
     }
     
     void writeSMbelow()
     {
         if (fSMbelow != 0) {
-            writeChar(fSMbelow, fSMIndex, fSMTags);
+            writeChar(fSMbelow, fSMIndex, fSMFeatures);
         }
     }
     
-    void writeChar(LEUnicode ch, le_uint32 charIndex, const LETag *charTags)
-    {
-        LEErrorCode success = LE_NO_ERROR;
-
-        fOutChars[fOutIndex] = ch;
-
-        fGlyphStorage.setCharIndex(fOutIndex, charIndex, success);
-        fGlyphStorage.setAuxData(fOutIndex, (void *) charTags, success);
-
-        fOutIndex += 1;
-    }
-
     le_int32 getOutputIndex()
     {
         return fOutIndex;
@@ -280,66 +335,60 @@ enum
     C_DOTTED_CIRCLE = 0x25CC
 };
 
-static const LETag emptyTag       = 0x00000000; // ''
-
-static const LETag nuktFeatureTag = LE_NUKT_FEATURE_TAG;
-static const LETag akhnFeatureTag = LE_AKHN_FEATURE_TAG;
-static const LETag rphfFeatureTag = LE_RPHF_FEATURE_TAG;
-static const LETag blwfFeatureTag = LE_BLWF_FEATURE_TAG;
-static const LETag halfFeatureTag = LE_HALF_FEATURE_TAG;
-static const LETag pstfFeatureTag = LE_PSTF_FEATURE_TAG;
-static const LETag vatuFeatureTag = LE_VATU_FEATURE_TAG;
-static const LETag presFeatureTag = LE_PRES_FEATURE_TAG;
-static const LETag blwsFeatureTag = LE_BLWS_FEATURE_TAG;
-static const LETag abvsFeatureTag = LE_ABVS_FEATURE_TAG;
-static const LETag pstsFeatureTag = LE_PSTS_FEATURE_TAG;
-static const LETag halnFeatureTag = LE_HALN_FEATURE_TAG;
-
-static const LETag blwmFeatureTag = LE_BLWM_FEATURE_TAG;
-static const LETag abvmFeatureTag = LE_ABVM_FEATURE_TAG;
-static const LETag distFeatureTag = LE_DIST_FEATURE_TAG;
-
-// These are in the order in which the features need to be applied
-// for correct processing
-static const LETag featureOrder[] =
-{
-    nuktFeatureTag, akhnFeatureTag, rphfFeatureTag, blwfFeatureTag, halfFeatureTag, pstfFeatureTag,
-    vatuFeatureTag, presFeatureTag, blwsFeatureTag, abvsFeatureTag, pstsFeatureTag, halnFeatureTag,
-    blwmFeatureTag, abvmFeatureTag, distFeatureTag, emptyTag
-};
+// TODO: Find better names for these!
+#define tagArray4 (loclFeatureMask | nuktFeatureMask | akhnFeatureMask | vatuFeatureMask | presFeatureMask | blwsFeatureMask | abvsFeatureMask | pstsFeatureMask | halnFeatureMask | blwmFeatureMask | abvmFeatureMask | distFeatureMask)
+#define tagArray3 (pstfFeatureMask | tagArray4)
+#define tagArray2 (halfFeatureMask | tagArray3)
+#define tagArray1 (blwfFeatureMask | tagArray2)
+#define tagArray0 (rphfFeatureMask | tagArray1)
 
-// The order of these is determined so that the tag array of each glyph can start
-// at an offset into this array 
-// FIXME: do we want a seperate tag array for each kind of character??
-// FIXME: are there cases where this ordering causes glyphs to get tags
-// that they shouldn't?
-static const LETag tagArray[] =
+static const FeatureMap featureMap[] =
 {
-    rphfFeatureTag, blwfFeatureTag, halfFeatureTag, pstfFeatureTag, nuktFeatureTag, akhnFeatureTag,
-    vatuFeatureTag, presFeatureTag, blwsFeatureTag, abvsFeatureTag, pstsFeatureTag, halnFeatureTag,
-    blwmFeatureTag, abvmFeatureTag, distFeatureTag, emptyTag
+    {loclFeatureTag, loclFeatureMask},
+    {initFeatureTag, initFeatureMask},
+    {nuktFeatureTag, nuktFeatureMask},
+    {akhnFeatureTag, akhnFeatureMask},
+    {rphfFeatureTag, rphfFeatureMask},
+    {blwfFeatureTag, blwfFeatureMask},
+    {halfFeatureTag, halfFeatureMask},
+    {pstfFeatureTag, pstfFeatureMask},
+    {vatuFeatureTag, vatuFeatureMask},
+    {presFeatureTag, presFeatureMask},
+    {blwsFeatureTag, blwsFeatureMask},
+    {abvsFeatureTag, abvsFeatureMask},
+    {pstsFeatureTag, pstsFeatureMask},
+    {halnFeatureTag, halnFeatureMask},
+    {blwmFeatureTag, blwmFeatureMask},
+    {abvmFeatureTag, abvmFeatureMask},
+    {distFeatureTag, distFeatureMask}
 };
 
-static const le_int8 stateTable[][IndicClassTable::CC_COUNT] =
-{
-//   xx  vm  sm  iv  i2  ct  cn  nu  dv  s1  s2  s3  vr  zw
-    { 1,  1,  1,  5,  8,  3,  2,  1,  5,  9,  5,  1,  1,  1}, //  0 - ground state
-    {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //  1 - exit state
-    {-1,  6,  1, -1, -1, -1, -1, -1,  5,  9,  5,  5,  4, -1}, //  2 - consonant
-    {-1,  6,  1, -1, -1, -1, -1,  2,  5,  9,  5,  5,  4, -1}, //  3 - consonant with nukta
-    {-1, -1, -1, -1, -1,  3,  2, -1, -1, -1, -1, -1, -1,  7}, //  4 - consonant virama
-    {-1,  6,  1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //  5 - dependent vowels
-    {-1, -1,  1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //  6 - vowel mark
-    {-1, -1, -1, -1, -1,  3,  2, -1, -1, -1, -1, -1, -1, -1}, //  7 - ZWJ, ZWNJ
-    {-1,  6,  1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  4, -1}, //  8 - independent vowels that can take a virama
-    {-1,  6,  1, -1, -1, -1, -1, -1, -1, -1, 10,  5, -1, -1}, //  9 - first part of split vowel
-    {-1,  6,  1, -1, -1, -1, -1, -1, -1, -1, -1,  5, -1, -1}  // 10 - second part of split vowel
+static const le_int32 featureCount = LE_ARRAY_SIZE(featureMap);
 
+static const le_int8 stateTable[][CC_COUNT] =
+{
+//   xx  vm  sm  iv  i2  i3  ct  cn  nu  dv  s1  s2  s3  vr  zw
+    { 1,  6,  1,  5,  8, 11,  3,  2,  1,  5,  9,  5,  5,  1,  1}, //  0 - ground state
+    {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //  1 - exit state
+    {-1,  6,  1, -1, -1, -1, -1, -1, -1,  5,  9,  5,  5,  4, 12}, //  2 - consonant with nukta
+    {-1,  6,  1, -1, -1, -1, -1, -1,  2,  5,  9,  5,  5,  4, 12}, //  3 - consonant
+    {-1, -1, -1, -1, -1, -1,  3,  2, -1, -1, -1, -1, -1, -1,  7}, //  4 - consonant virama
+    {-1,  6,  1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //  5 - dependent vowels
+    {-1, -1,  1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //  6 - vowel mark
+    {-1, -1, -1, -1, -1, -1,  3,  2, -1, -1, -1, -1, -1, -1, -1}, //  7 - consonant virama ZWJ, consonant ZWJ virama
+    {-1,  6,  1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  4, -1}, //  8 - independent vowels that can take a virama
+    {-1,  6,  1, -1, -1, -1, -1, -1, -1, -1, -1, 10,  5, -1, -1}, //  9 - first part of split vowel
+    {-1,  6,  1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  5, -1, -1}, // 10 - second part of split vowel
+    {-1,  6,  1, -1, -1, -1, -1, -1, -1,  5,  9,  5,  5,  4, -1}, // 11 - independent vowels that can take an iv
+    {-1, -1,  1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  7, -1}  // 12 - consonant ZWJ (TODO: Take everything else that can be after a consonant?)
 };
 
-const LETag *IndicReordering::getFeatureOrder()
+
+const FeatureMap *IndicReordering::getFeatureMap(le_int32 &count)
 {
-    return featureOrder;
+    count = featureCount;
+
+    return featureMap;
 }
 
 le_int32 IndicReordering::findSyllable(const IndicClassTable *classTable, const LEUnicode *chars, le_int32 prev, le_int32 charCount)
@@ -350,7 +399,7 @@ le_int32 IndicReordering::findSyllable(const IndicClassTable *classTable, const
     while (cursor < charCount) {
         IndicClassTable::CharClass charClass = classTable->getCharClass(chars[cursor]);
 
-        state = stateTable[state][charClass & IndicClassTable::CF_CLASS_MASK];
+        state = stateTable[state][charClass & CF_CLASS_MASK];
 
         if (state < 0) {
             break;
@@ -369,12 +418,13 @@ le_int32 IndicReordering::reorder(const LEUnicode *chars, le_int32 charCount, le
     MPreFixups *mpreFixups = NULL;
     const IndicClassTable *classTable = IndicClassTable::getScriptClassTable(scriptCode);
 
-    if (classTable->scriptFlags & IndicClassTable::SF_MPRE_FIXUP) {
+    if (classTable->scriptFlags & SF_MPRE_FIXUP) {
         mpreFixups = new MPreFixups(charCount);
     }
 
-    ReorderingOutput output(outChars, glyphStorage, mpreFixups);
+    IndicReorderingOutput output(outChars, glyphStorage, mpreFixups);
     le_int32 i, prev = 0;
+    le_bool lastInWord = FALSE;
 
     while (prev < charCount) {
         le_int32 syllable = findSyllable(classTable, chars, prev, charCount);
@@ -384,66 +434,72 @@ le_int32 IndicReordering::reorder(const LEUnicode *chars, le_int32 charCount, le
         
         if (classTable->isStressMark(chars[markStart - 1])) {
             markStart -= 1;
-            output.noteStressMark(classTable, chars[markStart], markStart, &tagArray[1]);
+            output.noteStressMark(classTable, chars[markStart], markStart, tagArray1);
         }
         
-        if (classTable->isVowelModifier(chars[markStart - 1])) {
+        if (markStart != prev && classTable->isVowelModifier(chars[markStart - 1])) {
             markStart -= 1;
-            output.noteVowelModifier(classTable, chars[markStart], markStart, &tagArray[1]);
+            output.noteVowelModifier(classTable, chars[markStart], markStart, tagArray1);
         }
 
         matra = markStart - 1;
 
-        while (output.noteMatra(classTable, chars[matra], matra, &tagArray[1]) && matra != prev) {
+        while (output.noteMatra(classTable, chars[matra], matra, tagArray1, !lastInWord) && matra != prev) {
             matra -= 1;
         }
 
-        switch (classTable->getCharClass(chars[prev]) & IndicClassTable::CF_CLASS_MASK) {
-        case IndicClassTable::CC_RESERVED:
-        case IndicClassTable::CC_INDEPENDENT_VOWEL:
-        case IndicClassTable::CC_ZERO_WIDTH_MARK:
+        lastInWord = TRUE;
+
+        switch (classTable->getCharClass(chars[prev]) & CF_CLASS_MASK) {
+        case CC_RESERVED:
+            lastInWord = FALSE;
+            /* fall through */
+
+        case CC_INDEPENDENT_VOWEL:
+        case CC_ZERO_WIDTH_MARK:
             for (i = prev; i < syllable; i += 1) {
-                output.writeChar(chars[i], i, &tagArray[1]);
+                output.writeChar(chars[i], i, tagArray1);
             }
 
             break;
 
-        case IndicClassTable::CC_NUKTA:
-        case IndicClassTable::CC_VIRAMA:
-            output.writeChar(C_DOTTED_CIRCLE, prev, &tagArray[1]);
-            output.writeChar(chars[prev], prev, &tagArray[1]);
+        case CC_NUKTA:
+        case CC_VIRAMA:
+            output.writeChar(C_DOTTED_CIRCLE, prev, tagArray1);
+            output.writeChar(chars[prev], prev, tagArray1);
             break;
 
-        case IndicClassTable::CC_DEPENDENT_VOWEL:
-        case IndicClassTable::CC_SPLIT_VOWEL_PIECE_1:
-        case IndicClassTable::CC_SPLIT_VOWEL_PIECE_2:
-        case IndicClassTable::CC_SPLIT_VOWEL_PIECE_3:
-        case IndicClassTable::CC_VOWEL_MODIFIER:
-        case IndicClassTable::CC_STRESS_MARK:
+        case CC_DEPENDENT_VOWEL:
+        case CC_SPLIT_VOWEL_PIECE_1:
+        case CC_SPLIT_VOWEL_PIECE_2:
+        case CC_SPLIT_VOWEL_PIECE_3:
+        case CC_VOWEL_MODIFIER:
+        case CC_STRESS_MARK:
             output.writeMpre();
 
-            output.writeChar(C_DOTTED_CIRCLE, prev, &tagArray[1]);
+            output.writeChar(C_DOTTED_CIRCLE, prev, tagArray1);
 
             output.writeMbelow();
             output.writeSMbelow();
             output.writeMabove();
 
-            if ((classTable->scriptFlags & IndicClassTable::SF_MATRAS_AFTER_BASE) != 0) {
+            if ((classTable->scriptFlags & SF_MATRAS_AFTER_BASE) != 0) {
                 output.writeMpost();
             }
 
-            if ((classTable->scriptFlags & IndicClassTable::SF_REPH_AFTER_BELOW) != 0) {
+            if ((classTable->scriptFlags & SF_REPH_AFTER_BELOW) != 0) {
                 output.writeVMabove();
                 output.writeSMabove(); // FIXME: there are no SM's in these scripts...
             }
 
-            if ((classTable->scriptFlags & IndicClassTable::SF_MATRAS_AFTER_BASE) == 0) {
+            if ((classTable->scriptFlags & SF_MATRAS_AFTER_BASE) == 0) {
                 output.writeMpost();
             }
 
             output.writeLengthMark();
+            output.writeVirama();
 
-            if ((classTable->scriptFlags & IndicClassTable::SF_REPH_AFTER_BELOW) == 0) {
+            if ((classTable->scriptFlags & SF_REPH_AFTER_BELOW) == 0) {
                 output.writeVMabove();
                 output.writeSMabove();
             }
@@ -451,9 +507,10 @@ le_int32 IndicReordering::reorder(const LEUnicode *chars, le_int32 charCount, le
             output.writeVMpost();
             break;
 
-        case IndicClassTable::CC_INDEPENDENT_VOWEL_2:
-        case IndicClassTable::CC_CONSONANT:
-        case IndicClassTable::CC_CONSONANT_WITH_NUKTA:
+        case CC_INDEPENDENT_VOWEL_2:
+        case CC_INDEPENDENT_VOWEL_3:
+        case CC_CONSONANT:
+        case CC_CONSONANT_WITH_NUKTA:
         {
             le_uint32 length = markStart - prev;
             le_int32  lastConsonant = markStart - 1;
@@ -464,7 +521,7 @@ le_int32 IndicReordering::reorder(const LEUnicode *chars, le_int32 charCount, le
                 baseLimit += 2;
 
                 // Check for eyelash RA, if the script supports it
-                if ((classTable->scriptFlags & IndicClassTable::SF_EYELASH_RA) != 0 &&
+                if ((classTable->scriptFlags & SF_EYELASH_RA) != 0 &&
                     chars[baseLimit] == C_SIGN_ZWJ) {
                     if (length > 3) {
                         baseLimit += 1;
@@ -480,11 +537,11 @@ le_int32 IndicReordering::reorder(const LEUnicode *chars, le_int32 charCount, le
 
             le_int32 baseConsonant = lastConsonant;
             le_int32 postBase = lastConsonant + 1;
-            le_int32 postBaseLimit = classTable->scriptFlags & IndicClassTable::SF_POST_BASE_LIMIT_MASK;
+            le_int32 postBaseLimit = classTable->scriptFlags & SF_POST_BASE_LIMIT_MASK;
             le_bool  seenVattu = FALSE;
             le_bool  seenBelowBaseForm = FALSE;
 
-            if (classTable->isNukta(chars[postBase])) {
+            if (postBase < markStart && classTable->isNukta(chars[postBase])) {
                 postBase += 1;
             }
 
@@ -522,9 +579,9 @@ le_int32 IndicReordering::reorder(const LEUnicode *chars, le_int32 charCount, le
             // Write eyelash RA
             // NOTE: baseLimit == prev + 3 iff eyelash RA present...
             if (baseLimit == prev + 3) {
-                output.writeChar(chars[prev], prev, &tagArray[2]);
-                output.writeChar(chars[prev + 1], prev + 1, &tagArray[2]);
-                output.writeChar(chars[prev + 2], prev + 2, &tagArray[2]);
+                output.writeChar(chars[prev], prev, tagArray2);
+                output.writeChar(chars[prev + 1], prev + 1, tagArray2);
+                output.writeChar(chars[prev + 2], prev + 2, tagArray2);
             }
 
             // write any pre-base consonants
@@ -533,21 +590,21 @@ le_int32 IndicReordering::reorder(const LEUnicode *chars, le_int32 charCount, le
             for (i = baseLimit; i < baseConsonant; i += 1) {
                 LEUnicode ch = chars[i];
                 // Don't put 'blwf' on first consonant.
-                const LETag *tag = (i == baseLimit? &tagArray[2] : &tagArray[1]);
+                FeatureMask features = (i == baseLimit? tagArray2 : tagArray1);
                 IndicClassTable::CharClass charClass = classTable->getCharClass(ch);
 
                 if (IndicClassTable::isConsonant(charClass)) {
                     if (IndicClassTable::isVattu(charClass) && supressVattu) {
-                        tag = &tagArray[4];
+                        features = tagArray4;
                     }
 
                     supressVattu = IndicClassTable::isVattu(charClass);
                 } else if (IndicClassTable::isVirama(charClass) && chars[i + 1] == C_SIGN_ZWNJ)
                 {
-                    tag = &tagArray[4];
+                    features = tagArray4;
                 }
 
-                output.writeChar(ch, i, tag);
+                output.writeChar(ch, i, features);
             }
 
             le_int32 bcSpan = baseConsonant + 1;
@@ -569,10 +626,10 @@ le_int32 IndicReordering::reorder(const LEUnicode *chars, le_int32 charCount, le
 
             // write base consonant
             for (i = baseConsonant; i < bcSpan; i += 1) {
-                output.writeChar(chars[i], i, &tagArray[4]);
+                output.writeChar(chars[i], i, tagArray4);
             }
 
-            if ((classTable->scriptFlags & IndicClassTable::SF_MATRAS_AFTER_BASE) != 0) {
+            if ((classTable->scriptFlags & SF_MATRAS_AFTER_BASE) != 0) {
                 output.writeMbelow();
                 output.writeSMbelow(); // FIXME: there are no SMs in these scripts...
                 output.writeMabove();
@@ -582,26 +639,26 @@ le_int32 IndicReordering::reorder(const LEUnicode *chars, le_int32 charCount, le
             // write below-base consonants
             if (baseConsonant != lastConsonant) {
                 for (i = bcSpan + 1; i < postBase; i += 1) {
-                    output.writeChar(chars[i], i, &tagArray[1]);
+                    output.writeChar(chars[i], i, tagArray1);
                 }
 
                 if (postBase > lastConsonant) {
                     // write halant that was after base consonant
-                    output.writeChar(chars[bcSpan], bcSpan, &tagArray[1]);
+                    output.writeChar(chars[bcSpan], bcSpan, tagArray1);
                 }
             }
 
             // write Mbelow, SMbelow, Mabove
-            if ((classTable->scriptFlags & IndicClassTable::SF_MATRAS_AFTER_BASE) == 0) {
+            if ((classTable->scriptFlags & SF_MATRAS_AFTER_BASE) == 0) {
                 output.writeMbelow();
                 output.writeSMbelow();
                 output.writeMabove();
             }
 
-            if ((classTable->scriptFlags & IndicClassTable::SF_REPH_AFTER_BELOW) != 0) {
+            if ((classTable->scriptFlags & SF_REPH_AFTER_BELOW) != 0) {
                 if (baseLimit == prev + 2) {
-                    output.writeChar(chars[prev], prev, &tagArray[0]);
-                    output.writeChar(chars[prev + 1], prev + 1, &tagArray[0]);
+                    output.writeChar(chars[prev], prev, tagArray0);
+                    output.writeChar(chars[prev + 1], prev + 1, tagArray0);
                 }
 
                 output.writeVMabove();
@@ -613,31 +670,32 @@ le_int32 IndicReordering::reorder(const LEUnicode *chars, le_int32 charCount, le
             if (baseConsonant != lastConsonant) {
                 if (postBase <= lastConsonant) {
                     for (i = postBase; i <= lastConsonant; i += 1) {
-                        output.writeChar(chars[i], i, &tagArray[3]);
+                        output.writeChar(chars[i], i, tagArray3);
                     }
 
                     // write halant that was after base consonant
-                    output.writeChar(chars[bcSpan], bcSpan, &tagArray[1]);
+                    output.writeChar(chars[bcSpan], bcSpan, tagArray1);
                 }
 
                 // write the training halant, if there is one
                 if (lastConsonant < matra && classTable->isVirama(chars[matra])) {
-                    output.writeChar(chars[matra], matra, &tagArray[4]);
+                    output.writeChar(chars[matra], matra, tagArray4);
                 }
             }
 
             // write Mpost
-            if ((classTable->scriptFlags & IndicClassTable::SF_MATRAS_AFTER_BASE) == 0) {
+            if ((classTable->scriptFlags & SF_MATRAS_AFTER_BASE) == 0) {
                 output.writeMpost();
             }
 
             output.writeLengthMark();
+            output.writeVirama();
 
             // write reph
-            if ((classTable->scriptFlags & IndicClassTable::SF_REPH_AFTER_BELOW) == 0) {
+            if ((classTable->scriptFlags & SF_REPH_AFTER_BELOW) == 0) {
                 if (baseLimit == prev + 2) {
-                    output.writeChar(chars[prev], prev, &tagArray[0]);
-                    output.writeChar(chars[prev + 1], prev + 1, &tagArray[0]);
+                    output.writeChar(chars[prev], prev, tagArray0);
+                    output.writeChar(chars[prev + 1], prev + 1, tagArray0);
                 }
 
                 output.writeVMabove();