sl@0: /*
sl@0:  *
sl@0:  * (C) Copyright IBM Corp. 1998-2005 - All Rights Reserved
sl@0:  *
sl@0:  */
sl@0: 
sl@0: #include "LETypes.h"
sl@0: #include "OpenTypeTables.h"
sl@0: #include "OpenTypeUtilities.h"
sl@0: #include "IndicReordering.h"
sl@0: #include "LEGlyphStorage.h"
sl@0: #include "MPreFixups.h"
sl@0: 
sl@0: U_NAMESPACE_BEGIN
sl@0: 
sl@0: const LEUnicode KBearURA		= 0x0A73;
sl@0: const LEUnicode KDepVowelU		= 0x0A41;
sl@0: const LEUnicode KDepVowelUU		= 0x0A42;
sl@0: const LEUnicode KDepVowelOO		= 0x0A4B;
sl@0: const LEUnicode KVowelU	    	= 0x0A09;
sl@0: const LEUnicode KVowelUU		= 0x0A0A;
sl@0: const LEUnicode KVowelOO		= 0x0A13;
sl@0: 
sl@0: class ReorderingOutput : public UMemory {
sl@0: private:
sl@0:     le_int32 fOutIndex;
sl@0:     LEUnicode *fOutChars;
sl@0: 
sl@0:     LEGlyphStorage &fGlyphStorage;
sl@0: 
sl@0:     LEUnicode fMpre;
sl@0:     le_int32  fMpreIndex;
sl@0: 
sl@0:     LEUnicode fMbelow;
sl@0:     le_int32  fMbelowIndex;
sl@0: 
sl@0:     LEUnicode fMabove;
sl@0:     le_int32  fMaboveIndex;
sl@0: 
sl@0:     LEUnicode fMpost;
sl@0:     le_int32  fMpostIndex;
sl@0: 
sl@0:     LEUnicode fLengthMark;
sl@0:     le_int32  fLengthMarkIndex;
sl@0: 
sl@0:     const LETag *fMatraTags;
sl@0:     
sl@0:     le_int32 fMPreOutIndex;
sl@0:     MPreFixups *fMPreFixups;
sl@0:     
sl@0:     LEUnicode fVMabove;
sl@0:     LEUnicode fVMpost;
sl@0:     le_int32  fVMIndex;
sl@0:     const LETag *fVMTags;
sl@0:     
sl@0:     LEUnicode fSMabove;
sl@0:     LEUnicode fSMbelow;
sl@0:     le_int32  fSMIndex;
sl@0:     const LETag *fSMTags;
sl@0: 
sl@0:     void saveMatra(LEUnicode matra, le_int32 matraIndex, IndicClassTable::CharClass matraClass)
sl@0:     {
sl@0:         // FIXME: check if already set, or if not a matra...
sl@0:         if (IndicClassTable::isLengthMark(matraClass)) {
sl@0:             fLengthMark = matra;
sl@0:             fLengthMarkIndex = matraIndex;
sl@0:         } else {
sl@0:             switch (matraClass & CF_POS_MASK) {
sl@0:             case CF_POS_BEFORE:
sl@0:                 fMpre = matra;
sl@0:                 fMpreIndex = matraIndex;
sl@0:                 break;
sl@0:                
sl@0:             case CF_POS_BELOW:
sl@0:                 fMbelow = matra;
sl@0:                 fMbelowIndex = matraIndex;
sl@0:                 break;
sl@0:                
sl@0:             case CF_POS_ABOVE:
sl@0:                 fMabove = matra;
sl@0:                 fMaboveIndex = matraIndex;
sl@0:                 break;
sl@0:                
sl@0:             case CF_POS_AFTER:
sl@0:                 fMpost = matra;
sl@0:                 fMpostIndex = matraIndex;
sl@0:                 break;
sl@0:                
sl@0:             default:
sl@0:                 // can't get here...
sl@0:                 break;
sl@0:            }
sl@0:         }
sl@0:         
sl@0:         // 1922 mlyl : only the right part of matra AU should be displayed -->
sl@0:         if (fMpre == CC_DEPENDENT_VOWEL_SIGN_MLYL_E && fLengthMark != 0) 
sl@0:         {
sl@0:         	fMpre = 0;
sl@0:         	fMpreIndex = -1;
sl@0:         } 
sl@0:         // <-- 1922 mlyl
sl@0:     }
sl@0: 
sl@0: public:
sl@0:     ReorderingOutput(LEUnicode *outChars, LEGlyphStorage &glyphStorage, MPreFixups *mpreFixups)
sl@0:         : fOutIndex(0), fOutChars(outChars), fGlyphStorage(glyphStorage),
sl@0:           fMpre(0), fMpreIndex(0), fMbelow(0), fMbelowIndex(0), fMabove(0), fMaboveIndex(0),
sl@0:           fMpost(0), fMpostIndex(0), fLengthMark(0), fLengthMarkIndex(0), fMatraTags(NULL),
sl@0:           fMPreOutIndex(-1), fMPreFixups(mpreFixups),
sl@0:           fVMabove(0), fVMpost(0), fVMIndex(0), fVMTags(NULL),
sl@0:           fSMabove(0), fSMbelow(0), fSMIndex(0), fSMTags(NULL)
sl@0:     {
sl@0:         // nothing else to do...
sl@0:     }
sl@0: 
sl@0:     ~ReorderingOutput()
sl@0:     {
sl@0:         // nothing to do here...
sl@0:     }
sl@0: 
sl@0:     void reset()
sl@0:     {
sl@0:         fMpre = fMbelow = fMabove = fMpost = fLengthMark = 0;
sl@0:         fMPreOutIndex = -1;
sl@0:         
sl@0:         fVMabove = fVMpost  = 0;
sl@0:         fSMabove = fSMbelow = 0;
sl@0:     }
sl@0: 
sl@0:     void writeChar(LEUnicode ch, le_uint32 charIndex, const LETag *charTags)
sl@0:     {
sl@0:         LEErrorCode success = LE_NO_ERROR;
sl@0: 
sl@0:         fOutChars[fOutIndex] = ch;
sl@0: 
sl@0:         fGlyphStorage.setCharIndex(fOutIndex, charIndex, success);
sl@0:         fGlyphStorage.setAuxData(fOutIndex, (void *) charTags, success);
sl@0: 
sl@0:         fOutIndex += 1;
sl@0:     }
sl@0: 
sl@0:     le_bool noteMatra(const IndicClassTable *classTable, LEUnicode matra, le_uint32 matraIndex, const LETag *matraTags)
sl@0:     {
sl@0:         IndicClassTable::CharClass matraClass = classTable->getCharClass(matra);
sl@0: 
sl@0:         fMatraTags  = matraTags;
sl@0: 
sl@0:         if (IndicClassTable::isMatra(matraClass)) {
sl@0:             if (IndicClassTable::isSplitMatra(matraClass)) {
sl@0:                 const SplitMatra *splitMatra = classTable->getSplitMatra(matraClass);
sl@0:                 int i;
sl@0: 
sl@0:                 for (i = 0; i < 3 && (*splitMatra)[i] != 0; i += 1) {
sl@0:                     LEUnicode piece = (*splitMatra)[i];
sl@0:                     IndicClassTable::CharClass pieceClass = classTable->getCharClass(piece);
sl@0: 
sl@0:                     saveMatra(piece, matraIndex, pieceClass);
sl@0:                 }
sl@0:             } else {
sl@0:                 saveMatra(matra, matraIndex, matraClass);
sl@0:             }
sl@0: 
sl@0:             return TRUE;
sl@0:         }
sl@0: 
sl@0:         return FALSE;
sl@0:     }
sl@0:     
sl@0:     void noteVowelModifier(const IndicClassTable *classTable, LEUnicode vowelModifier, le_uint32 vowelModifierIndex, const LETag *vowelModifierTags)
sl@0:     {
sl@0:         IndicClassTable::CharClass vmClass = classTable->getCharClass(vowelModifier);
sl@0:         
sl@0:         fVMIndex = vowelModifierIndex;
sl@0:         fVMTags  = vowelModifierTags;
sl@0:         
sl@0:         if (IndicClassTable::isVowelModifier(vmClass)) {
sl@0:            switch (vmClass & CF_POS_MASK) {
sl@0:            case CF_POS_ABOVE:
sl@0:                fVMabove = vowelModifier;
sl@0:                break;
sl@0:             
sl@0:            case CF_POS_AFTER:
sl@0:                fVMpost = vowelModifier;
sl@0:                break;
sl@0:            
sl@0:            default:
sl@0:                // FIXME: this is an error...
sl@0:                break;
sl@0:            }
sl@0:         }
sl@0:     }
sl@0:     
sl@0:     void noteStressMark(const IndicClassTable *classTable, LEUnicode stressMark, le_uint32 stressMarkIndex, const LETag *stressMarkTags)
sl@0:     {
sl@0:        IndicClassTable::CharClass smClass = classTable->getCharClass(stressMark);
sl@0:         
sl@0:         fSMIndex = stressMarkIndex;
sl@0:         fSMTags  = stressMarkTags;
sl@0:         
sl@0:         if (IndicClassTable::isStressMark(smClass)) {
sl@0:             switch (smClass & CF_POS_MASK) {
sl@0:             case CF_POS_ABOVE:
sl@0:                 fSMabove = stressMark;
sl@0:                 break;
sl@0:             
sl@0:             case CF_POS_BELOW:
sl@0:                 fSMbelow = stressMark;
sl@0:                 break;
sl@0:            
sl@0:             default:
sl@0:                 // FIXME: this is an error...
sl@0:                 break;
sl@0:            }
sl@0:         }
sl@0:     }
sl@0: 
sl@0:     void noteBaseConsonant()
sl@0:     {
sl@0:         if (fMPreFixups != NULL && fMPreOutIndex >= 0) {
sl@0:             fMPreFixups->add(fOutIndex, fMPreOutIndex);
sl@0:         }
sl@0:     }
sl@0: 
sl@0:     void writeMpre()
sl@0:     {
sl@0:         if (fMpre != 0) {
sl@0:             fMPreOutIndex = fOutIndex;
sl@0:             writeChar(fMpre, fMpreIndex, fMatraTags);
sl@0:         }
sl@0:     }
sl@0: 
sl@0:     void writeMbelow()
sl@0:     {
sl@0:         if (fMbelow != 0) {
sl@0:             writeChar(fMbelow, fMbelowIndex, fMatraTags);
sl@0:         }
sl@0:     }
sl@0: 
sl@0:     void writeMabove()
sl@0:     {
sl@0:         if (fMabove != 0) {
sl@0:             writeChar(fMabove, fMaboveIndex, fMatraTags);
sl@0:         }
sl@0:     }
sl@0: 
sl@0:     void writeMpost()
sl@0:     {
sl@0:         if (fMpost != 0) {
sl@0:             writeChar(fMpost, fMpostIndex, fMatraTags);
sl@0:         }
sl@0:     }
sl@0: 
sl@0:     void writeLengthMark()
sl@0:     {
sl@0:         if (fLengthMark != 0) {
sl@0:             writeChar(fLengthMark, fLengthMarkIndex, fMatraTags);
sl@0:         }
sl@0:     }
sl@0:     
sl@0:     void writeVMabove()
sl@0:     {
sl@0:         if (fVMabove != 0) {
sl@0:             writeChar(fVMabove, fVMIndex, fVMTags);
sl@0:         }
sl@0:     }
sl@0:         
sl@0:     void writeVMpost()
sl@0:     {
sl@0:         if (fVMpost != 0) {
sl@0:             writeChar(fVMpost, fVMIndex, fVMTags);
sl@0:         }
sl@0:     }
sl@0:     
sl@0:     void writeSMabove()
sl@0:     {
sl@0:         if (fSMabove != 0) {
sl@0:             writeChar(fSMabove, fSMIndex, fSMTags);
sl@0:         }
sl@0:     }
sl@0:     
sl@0:     void writeSMbelow()
sl@0:     {
sl@0:         if (fSMbelow != 0) {
sl@0:             writeChar(fSMbelow, fSMIndex, fSMTags);
sl@0:         }
sl@0:     }
sl@0:     
sl@0:     le_int32 getOutputIndex()
sl@0:     {
sl@0:         return fOutIndex;
sl@0:     }
sl@0: };
sl@0: 
sl@0: enum
sl@0: {
sl@0:     C_DOTTED_CIRCLE = 0x25CC
sl@0: };
sl@0: 
sl@0: static const LETag emptyTag       = 0x00000000; // ''
sl@0: 
sl@0: static const LETag nuktFeatureTag = LE_NUKT_FEATURE_TAG;
sl@0: static const LETag akhnFeatureTag = LE_AKHN_FEATURE_TAG;
sl@0: static const LETag rphfFeatureTag = LE_RPHF_FEATURE_TAG;
sl@0: static const LETag blwfFeatureTag = LE_BLWF_FEATURE_TAG;
sl@0: static const LETag halfFeatureTag = LE_HALF_FEATURE_TAG;
sl@0: static const LETag pstfFeatureTag = LE_PSTF_FEATURE_TAG;
sl@0: static const LETag vatuFeatureTag = LE_VATU_FEATURE_TAG;
sl@0: static const LETag presFeatureTag = LE_PRES_FEATURE_TAG;
sl@0: static const LETag blwsFeatureTag = LE_BLWS_FEATURE_TAG;
sl@0: static const LETag abvsFeatureTag = LE_ABVS_FEATURE_TAG;
sl@0: static const LETag pstsFeatureTag = LE_PSTS_FEATURE_TAG;
sl@0: static const LETag halnFeatureTag = LE_HALN_FEATURE_TAG;
sl@0: 
sl@0: static const LETag blwmFeatureTag = LE_BLWM_FEATURE_TAG;
sl@0: static const LETag abvmFeatureTag = LE_ABVM_FEATURE_TAG;
sl@0: static const LETag distFeatureTag = LE_DIST_FEATURE_TAG;
sl@0: static const LETag loclFeatureTag = LE_LOCL_FEATURE_TAG;
sl@0: 
sl@0: // These are in the order in which the features need to be applied
sl@0: // for correct processing
sl@0: static const LETag featureOrder[] =
sl@0: {
sl@0:     loclFeatureTag,
sl@0:     nuktFeatureTag, akhnFeatureTag, rphfFeatureTag, blwfFeatureTag, halfFeatureTag, pstfFeatureTag,
sl@0:     vatuFeatureTag, presFeatureTag, blwsFeatureTag, abvsFeatureTag, pstsFeatureTag, halnFeatureTag,
sl@0:     blwmFeatureTag, abvmFeatureTag, distFeatureTag, emptyTag
sl@0: };
sl@0: 
sl@0: // The order of these is determined so that the tag array of each glyph can start
sl@0: // at an offset into this array 
sl@0: // FIXME: do we want a seperate tag array for each kind of character??
sl@0: // FIXME: are there cases where this ordering causes glyphs to get tags
sl@0: // that they shouldn't?
sl@0: static const LETag tagArray[] =
sl@0: {
sl@0:     rphfFeatureTag, blwfFeatureTag, halfFeatureTag, pstfFeatureTag, nuktFeatureTag, akhnFeatureTag,
sl@0:     vatuFeatureTag, presFeatureTag, blwsFeatureTag, abvsFeatureTag, pstsFeatureTag, halnFeatureTag,
sl@0:     blwmFeatureTag, abvmFeatureTag, distFeatureTag, loclFeatureTag, emptyTag
sl@0: };
sl@0: 
sl@0: 
sl@0: // Nokia: New state responses ia and de, and new states for Devanagari Independent Vowel A 
sl@0: // added by Nokia to allow the special Devanagari CANDRA A formation using A + CANDRA E, and
sl@0: // a new zero width joiner state specially for ZWJ's that come after the Independent Vowel A
sl@0: // Also, changed the consonant response to ZWJ by allowing it to have a ZWJ straight after it.
sl@0: // This allows Kannada RA to take a ZWJ after it and not make it a REPHA
sl@0: // Nokia: New states for Gurmukhi bearers to support decompositions of the independent vowels 
sl@0: // into a sequence of a vowel bearer and a depending vowel sign.
sl@0: static const le_int8 stateTable[][CC_COUNT] =
sl@0: {
sl@0: //   xx  vm  sm  iv  i2  ct  cn  nu  dv  s1  s2  s3  vr  zw  ia  de  cr  to  ba  bi  bu  ga  gi  gu 
sl@0:     { 1,  1,  1,  5,  8,  3,  2,  1,  1,  9,  5,  1,  1,  1, 20,  1, 25, 26, 27, 28, 29,  1,  1,  1}, //  0 - ground state
sl@0:     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //  1 - exit state
sl@0:     {-1,  6,  1, -1, -1, -1, -1, -1,  5,  9,  5,  5,  4, -1, -1,  5, -1, 26, -1, -1, -1,  5,  5,  5}, //  2 - consonant with nukta
sl@0:     {-1,  6,  1, -1, -1, -1, -1,  2,  5,  9,  5,  5,  4, -1, -1,  5, -1, 26, -1, -1, -1,  5,  5,  5}, //  3 - consonant
sl@0:     {-1, -1, -1, -1, -1, 12, 11, -1, -1, -1, -1, -1, -1, 22, -1, -1, 12, -1, -1, -1, -1, -1, -1, -1}, //  4 - ct vr
sl@0:     {-1,  6,  1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //  5 - dependent vowels
sl@0:     {-1, -1,  1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //  6 - vowel mark
sl@0:     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //  7 - ZWJ, ZWNJ seen after a consonant: Modified by Nokia
sl@0:     {-1,  6,  1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //  8 - independent vowels that can take a virama
sl@0:     {-1,  6,  1, -1, -1, -1, -1, -1, -1, -1, 10,  5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //  9 - first part of split vowel
sl@0:     {-1,  6,  1, -1, -1, -1, -1, -1, -1, -1, -1,  5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 10 - second part of split vowel
sl@0:     {-1,  6,  1, -1, -1, -1, -1, -1,  5,  9,  5,  5, 13, -1, -1,  5, -1, 26, -1, -1, -1,  5,  5,  5}, // 11 - ct vr ct nu
sl@0:     {-1,  6,  1, -1, -1, -1, -1, 11,  5,  9,  5,  5, 13, -1, -1,  5, -1, 26, -1, -1, -1,  5,  5,  5}, // 12 - ct vr ct
sl@0:     {-1, -1, -1, -1, -1, 15, 14, -1, -1, -1, -1, -1, -1, 23, -1, -1, 15, -1, -1, -1, -1, -1, -1, -1}, // 13 - ct vr ct vr
sl@0:     {-1,  6,  1, -1, -1, -1, -1, -1,  5,  9,  5,  5, 16, -1, -1,  5, -1, 26, -1, -1, -1,  5,  5,  5}, // 14 - ct vr ct vr ct nu
sl@0:     {-1,  6,  1, -1, -1, -1, -1, 14,  5,  9,  5,  5, 16, -1, -1,  5, -1, 26, -1, -1, -1,  5,  5,  5}, // 15 - ct vr ct vr ct
sl@0:     {-1, -1, -1, -1, -1, 18, 17, -1, -1, -1, -1, -1, -1, 24, -1, -1, 18, -1, -1, -1, -1, -1, -1, -1}, // 16 - ct vr ct vr ct vr
sl@0:     {-1,  6,  1, -1, -1, -1, -1, -1,  5,  9,  5,  5, 19, -1, -1,  5, -1, 26, -1, -1, -1,  5,  5,  5}, // 17 - ct vr ct vr ct vr ct nu
sl@0:     {-1,  6,  1, -1, -1, -1, -1, 17,  5,  9,  5,  5, 19, -1, -1,  5, -1, 26, -1, -1, -1,  5,  5,  5}, // 18 - ct vr ct vr ct vr ct
sl@0:     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 19 - ct vr ct vr ct vr ct vr
sl@0:     {-1,  6,  1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 21, -1,  5, -1, -1, -1, -1, -1, -1, -1, -1}, // 20 - devanagari independent vowel A that can take a modifier - CANDRA E: Added by Nokia
sl@0:     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  5, -1, -1, -1, -1, -1, -1, -1, -1}, // 21 - ZWJ after independent vowel A: Added by Nokia  
sl@0:     {-1,  6, -1, -1, -1, 12, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, 12, -1, -1, -1, -1, -1, -1, -1}, // 22 - ZWJ after a virama: Added by Nokia  
sl@0:     {-1,  6, -1, -1, -1, 15, 14, -1, -1, -1, -1, -1, -1, -1, -1, -1, 15, -1, -1, -1, -1, -1, -1, -1}, // 23 - ZWJ after a ct vr ct vr: Added by Nokia  
sl@0:     {-1,  6, -1, -1, -1, 18, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1, 18, -1, -1, -1, -1, -1, -1, -1}, // 24 - ZWJ after a ct vr ct vr ct vr: Added by Nokia  
sl@0:     {-1,  6,  1, -1, -1, -1, -1,  2,  5,  9,  5,  5,  4,  7, -1,  5, -1, 26, -1, -1, -1,  5,  5,  5}, // 25 - Kannada consonant Ra: Added by Nokia
sl@0:     {-1,  6,  1, -1, -1, -1, -1, -1, -1, -1, 10,  5,  4, 21, -1,  5, -1, -1, -1, -1, -1, -1, -1, -1}, // 26 - devanagari independent vowel A that can take a modifier - CANDRA E: Added by Nokia
sl@0:     {-1,  6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  5, -1, -1}, // 27 - Gurmukhi bearer ARA and independent A
sl@0:     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  5, -1}, // 28 - Gurmukhi bearer IRI
sl@0:     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  5}, // 29 - Gurmukhi bearer URA
sl@0: };
sl@0: 
sl@0: 
sl@0: const LETag *IndicReordering::getFeatureOrder()
sl@0: {
sl@0:     return featureOrder;
sl@0: }
sl@0: 
sl@0: le_int32 IndicReordering::findSyllable(const IndicClassTable *classTable, const LEUnicode *chars, le_int32 prev, le_int32 charCount)
sl@0: {
sl@0:     le_int32 cursor = prev;
sl@0:     le_int8 state = 0;
sl@0: 
sl@0:     while (cursor < charCount) {
sl@0:         IndicClassTable::CharClass charClass = classTable->getCharClass(chars[cursor]);
sl@0: 
sl@0:         state = stateTable[state][charClass & CF_CLASS_MASK];
sl@0: 
sl@0:         if (state < 0) {
sl@0:             break;
sl@0:         }
sl@0: 
sl@0:         cursor += 1;
sl@0:     }
sl@0: 
sl@0:     return cursor;
sl@0: }
sl@0: 
sl@0: le_int32 IndicReordering::reorder(const LEUnicode *chars, le_int32 charCount, le_int32 scriptCode,
sl@0:                                   LEUnicode *outChars, LEGlyphStorage &glyphStorage,
sl@0:                                   MPreFixups **outMPreFixups, LEErrorCode& success)
sl@0: {
sl@0:     MPreFixups *mpreFixups = NULL;
sl@0:     const IndicClassTable *classTable = IndicClassTable::getScriptClassTable(scriptCode);
sl@0: 
sl@0:     if (classTable->scriptFlags & SF_MPRE_FIXUP) {
sl@0:         mpreFixups = new MPreFixups(charCount);
sl@0:         if (!mpreFixups) {
sl@0:             success = LE_MEMORY_ALLOCATION_ERROR;
sl@0:             return 0;
sl@0:         }
sl@0:     }
sl@0: 
sl@0:     ReorderingOutput output(outChars, glyphStorage, mpreFixups);
sl@0:     le_int32 i, prev = 0;
sl@0: 
sl@0:     while (prev < charCount) {
sl@0:         le_int32 syllable = findSyllable(classTable, chars, prev, charCount);
sl@0:         le_int32 matra, markStart = syllable;
sl@0: 
sl@0:         output.reset();
sl@0:         
sl@0:         if (classTable->isStressMark(chars[markStart - 1])) {
sl@0:             markStart -= 1;
sl@0:             output.noteStressMark(classTable, chars[markStart], markStart, &tagArray[1]);
sl@0:         }
sl@0:         
sl@0:         if (prev != markStart && classTable->isVowelModifier(chars[markStart - 1])) {
sl@0:             markStart -= 1;
sl@0:             output.noteVowelModifier(classTable, chars[markStart], markStart, &tagArray[1]);
sl@0:         }
sl@0:         
sl@0:         matra = markStart - 1;
sl@0:         
sl@0:         // Added by Nokia. Workaround ligature substitution: Bearer URA + Dependent Vowel -> Independent Vowel
sl@0:         // For these sequences, no need to write matra since it will be replaced.
sl@0:         if (!(chars[prev] == KBearURA && (prev + 1 < syllable) && 
sl@0:         		(chars[prev+1] == KDepVowelU || chars[prev+1] == KDepVowelUU || chars[prev+1] == KDepVowelOO ))) {
sl@0:        		
sl@0:         	while (prev <= matra && output.noteMatra(classTable, chars[matra], matra, &tagArray[1])) {
sl@0:             	matra -= 1;
sl@0:         	}
sl@0:         }
sl@0: 
sl@0:         
sl@0:         switch (classTable->getCharClass(chars[prev]) & CF_CLASS_MASK) {
sl@0:         case CC_RESERVED:
sl@0:         case CC_INDEPENDENT_VOWEL:
sl@0:         case CC_ZERO_WIDTH_MARK:
sl@0:         case CC_INDEPENDENT_VOWEL_A:	// Added by Nokia
sl@0:         case CC_INDEPENDENT_VOWEL_TAMIL_O:	// Added by Nokia
sl@0:         case CC_GUR_BEARER_U:	//Added by Nokia
sl@0:         	
sl@0:         	// Added by Nokia. Workaround ligature substitution: Bearer URA + Dependent Vowel -> Independent Vowel
sl@0:         	if (chars[prev] == KBearURA && (prev + 1 < markStart)){
sl@0:         		if (chars[prev+1] == KDepVowelU){
sl@0:         	   		output.writeChar(KVowelU, prev, &tagArray[1]);
sl@0:         	   		prev = prev+2;
sl@0:         		}
sl@0:         		else if (chars[prev+1] == KDepVowelUU){
sl@0:         			output.writeChar(KVowelUU, prev, &tagArray[1]);
sl@0:         			prev = prev+2;
sl@0:         		}
sl@0:         		else if (chars[prev+1] == KDepVowelOO){
sl@0:         			output.writeChar(KVowelOO, prev, &tagArray[1]);
sl@0:         			prev = prev+2;
sl@0:         		}
sl@0:         	}
sl@0:        	
sl@0:             for (i = prev; i < syllable; i += 1) {
sl@0:                 output.writeChar(chars[i], i, &tagArray[1]);
sl@0:             }
sl@0: 
sl@0:             break;
sl@0: 
sl@0:         case CC_NUKTA:
sl@0:         case CC_VIRAMA:
sl@0:             output.writeChar(C_DOTTED_CIRCLE, prev, &tagArray[1]);
sl@0:             output.writeChar(chars[prev], prev, &tagArray[1]);
sl@0:             break;
sl@0: 
sl@0:         case CC_DEPENDENT_VOWEL:
sl@0:         case CC_DEPENDENT_VOWEL_CANDRA_E:	// Added by Nokia
sl@0:         case CC_SPLIT_VOWEL_PIECE_1:
sl@0:         case CC_SPLIT_VOWEL_PIECE_2:
sl@0:         case CC_SPLIT_VOWEL_PIECE_3:
sl@0:         case CC_VOWEL_MODIFIER:
sl@0:         case CC_STRESS_MARK:
sl@0:         case CC_GUR_DEPENDENT_VOWEL_A:
sl@0:         case CC_GUR_DEPENDENT_VOWEL_I:
sl@0:         case CC_GUR_DEPENDENT_VOWEL_U:
sl@0:             output.writeMpre();
sl@0: 
sl@0:             output.writeChar(C_DOTTED_CIRCLE, prev, &tagArray[1]);
sl@0: 
sl@0:             output.writeMbelow();
sl@0:             output.writeSMbelow();
sl@0:             output.writeMabove();
sl@0: 
sl@0:             if ((classTable->scriptFlags & SF_MATRAS_AFTER_BASE) != 0) {
sl@0:                 output.writeMpost();
sl@0:             }
sl@0: 
sl@0:             if ((classTable->scriptFlags & SF_REPH_AFTER_BELOW) != 0) {
sl@0:                 output.writeVMabove();
sl@0:                 output.writeSMabove(); // FIXME: there are no SM's in these scripts...
sl@0:             }
sl@0: 
sl@0:             if ((classTable->scriptFlags & SF_MATRAS_AFTER_BASE) == 0) {
sl@0:                 output.writeMpost();
sl@0:             }
sl@0: 
sl@0:             output.writeLengthMark();
sl@0: 
sl@0:             if ((classTable->scriptFlags & SF_REPH_AFTER_BELOW) == 0) {
sl@0:                 output.writeVMabove();
sl@0:                 output.writeSMabove();
sl@0:             }
sl@0: 
sl@0:             output.writeVMpost();
sl@0:             break;
sl@0: 
sl@0:         case CC_INDEPENDENT_VOWEL_2:
sl@0:         case CC_CONSONANT:
sl@0:         case CC_CONSONANT_WITH_NUKTA:
sl@0:         case CC_CONSONANT_KANNADA_BENGALI_RA:
sl@0:         case CC_GUR_BEARER_A:				// Added by Nokia
sl@0:         case CC_GUR_BEARER_I:				// Added by Nokia
sl@0:         {
sl@0:             le_uint32 length = markStart - prev;
sl@0:             le_int32  lastConsonant = markStart - 1;
sl@0:             le_int32  baseLimit = prev;
sl@0:             
sl@0:             // Check for REPH at front of syllable
sl@0:             if (length > 2 && classTable->isReph(chars[prev]) && classTable->isVirama(chars[prev + 1])) {
sl@0:                 baseLimit += 2;
sl@0: 
sl@0:                 // Check for eyelash RA, if the script supports it
sl@0:                 if ((classTable->scriptFlags & SF_EYELASH_RA) != 0 &&
sl@0:                     chars[baseLimit] == C_SIGN_ZWJ) {
sl@0:                     if (length > 3) {
sl@0:                         baseLimit += 1;
sl@0:                     } else {
sl@0:                         baseLimit -= 2;
sl@0:                     }
sl@0:                 }
sl@0:             }
sl@0: 
sl@0:             while (lastConsonant > baseLimit && !classTable->isConsonant(chars[lastConsonant])) {
sl@0:                 lastConsonant -= 1;
sl@0:             }
sl@0: 
sl@0:             le_int32 baseConsonant = lastConsonant;
sl@0:             le_int32 postBase = lastConsonant + 1;
sl@0:             le_int32 postBaseLimit = classTable->scriptFlags & SF_POST_BASE_LIMIT_MASK;
sl@0:             le_bool  seenVattu = FALSE;
sl@0:             le_bool  seenBelowBaseForm = FALSE;
sl@0: 
sl@0:             if (postBase < markStart && classTable->isNukta(chars[postBase])) {
sl@0:                 postBase += 1;
sl@0:             }
sl@0: 
sl@0:             while (baseConsonant > baseLimit) {
sl@0:                 IndicClassTable::CharClass charClass = classTable->getCharClass(chars[baseConsonant]);
sl@0: 
sl@0:                 if (IndicClassTable::isConsonant(charClass)) {
sl@0:                     IndicClassTable::CharClass nextCharClass
sl@0:                     	= classTable->getCharClass(chars[baseConsonant + 1]);
sl@0:                     le_bool hasNukta = IndicClassTable::isNukta(nextCharClass);
sl@0:                     // Consonants with nuktas never have post-base or below-base forms
sl@0:                     le_bool hasPostBaseForm = !hasNukta
sl@0:                     	&& IndicClassTable::hasPostBaseForm(charClass);
sl@0:                     le_bool hasBelowBaseForm = !hasNukta
sl@0:                     	&& IndicClassTable::hasBelowBaseForm(charClass);
sl@0: 
sl@0:                     // Added by Nokia -- A Bengali Ya should only have a post base form if it is the last component in a consonant cluster.
sl@0:                     if (IndicClassTable::isBengaliYa(charClass) && (baseConsonant != lastConsonant))
sl@0: 						hasPostBaseForm = FALSE;
sl@0: 
sl@0:                     // Added by Nokia 1922 mlyl: YAKAR/VAKAR/RAKAR are not formed after LLA/LLLA/RRA, but when LLA/LLLA/RRA is one of combination ligature?-->
sl@0:                     if ((chars[baseConsonant] == CC_CONSONANT_MLYL_YA || chars[baseConsonant] == CC_CONSONANT_MLYL_VA || chars[baseConsonant] == CC_CONSONANT_MLYL_RA)
sl@0:                     		&& baseConsonant >=2
sl@0:                     		&& (chars[baseConsonant - 2] == CC_CONSONANT_MLYL_LLA || chars[baseConsonant - 2] == CC_CONSONANT_MLYL_LLLA || chars[baseConsonant - 2] == CC_CONSONANT_MLYL_RRA))
sl@0:                     	hasPostBaseForm = FALSE;
sl@0: 
sl@0:                     // D15 + D4D + D2F/D35 + D4D + D30: YAKAR/VAKAR not formed
sl@0:                     if ((chars[baseConsonant] == CC_CONSONANT_MLYL_YA || chars[baseConsonant] == CC_CONSONANT_MLYL_VA)
sl@0:                     		&& lastConsonant >= 4
sl@0:                     		&& chars[lastConsonant] == CC_CONSONANT_MLYL_RA)
sl@0:                     	hasPostBaseForm = FALSE;
sl@0:                     // <-- 1922 mlyl
sl@0:                     
sl@0:                     if (postBaseLimit == 0 || seenVattu ||
sl@0:                         (baseConsonant > baseLimit && !classTable->isVirama(chars[baseConsonant - 1])) ||
sl@0:                         (!hasPostBaseForm && !hasBelowBaseForm)) {
sl@0:                         break;
sl@0:                     }
sl@0: 
sl@0:                     // Consonants with nuktas are never vattus
sl@0:                     seenVattu = !hasNukta && IndicClassTable::isVattu(charClass);
sl@0: 
sl@0:                     if (hasPostBaseForm) {
sl@0:                         if (seenBelowBaseForm) {
sl@0:                             break;
sl@0:                         }
sl@0: 
sl@0:                         postBase = baseConsonant;
sl@0:                     } else if (hasBelowBaseForm) {
sl@0:                         seenBelowBaseForm = TRUE;
sl@0:                     }
sl@0: 
sl@0:                     postBaseLimit -= 1;
sl@0:                 }
sl@0: 
sl@0:                 baseConsonant -= 1;
sl@0:             }
sl@0: 
sl@0:             // Write Mpre
sl@0:             output.writeMpre();
sl@0: 
sl@0:             // Write eyelash RA
sl@0:             // NOTE: baseLimit == prev + 3 iff eyelash RA present...
sl@0:             if (baseLimit == prev + 3) {
sl@0:                 output.writeChar(chars[prev], prev, &tagArray[2]);
sl@0:                 output.writeChar(chars[prev + 1], prev + 1, &tagArray[2]);
sl@0:                 output.writeChar(chars[prev + 2], prev + 2, &tagArray[2]);
sl@0:             }
sl@0: 
sl@0:             // write any pre-base consonants
sl@0:             le_bool supressVattu = TRUE;
sl@0: 
sl@0:             for (i = baseLimit; i < baseConsonant; i += 1) {
sl@0:                 LEUnicode ch = chars[i];
sl@0:                 // Don't put 'blwf' on first consonant.
sl@0:                 const LETag *tag = (i == baseLimit? &tagArray[2] : &tagArray[1]);
sl@0:                 IndicClassTable::CharClass charClass = classTable->getCharClass(ch);
sl@0:                 IndicClassTable::CharClass nextCharClass =
sl@0:                 	classTable->getCharClass(chars[i + 1]);
sl@0:                 	
sl@0:                 // Nokia: Added a check to see if the character that is two characters after
sl@0:                 // the RA is a ZWJ, which then doesn't allow the RA to become a VATTU and allows
sl@0:                 // the formation of an EYELASH RA
sl@0:                 le_bool isGenuineVattu = IndicClassTable::isVattu(charClass)
sl@0:                 	&& !IndicClassTable::isNukta(nextCharClass)
sl@0:                 	&& chars[i + 2] != C_SIGN_ZWJ;
sl@0: 
sl@0:                 if (IndicClassTable::isConsonant(charClass)) {
sl@0:                     if (isGenuineVattu && supressVattu) {
sl@0:                         tag = &tagArray[4];
sl@0:                     }
sl@0: 
sl@0:                     supressVattu = isGenuineVattu;
sl@0:                 } else if (IndicClassTable::isVirama(charClass) && chars[i + 1] == C_SIGN_ZWNJ)
sl@0:                 {
sl@0:                     tag = &tagArray[4];
sl@0:                 }
sl@0: 
sl@0:                 output.writeChar(ch, i, tag);
sl@0:             }
sl@0: 
sl@0:             le_int32 bcSpan = baseConsonant + 1;
sl@0: 
sl@0:             if (bcSpan < markStart && classTable->isNukta(chars[bcSpan])) {
sl@0:                 bcSpan += 1;
sl@0:             }
sl@0: 
sl@0:             if (baseConsonant == lastConsonant && bcSpan < markStart && classTable->isVirama(chars[bcSpan])) {
sl@0:                 bcSpan += 1;
sl@0: 
sl@0:                 if (bcSpan < markStart && chars[bcSpan] == C_SIGN_ZWNJ) {
sl@0:                     bcSpan += 1;
sl@0:                 }
sl@0:             }
sl@0: 
sl@0:             // note the base consonant for post-GSUB fixups
sl@0:             output.noteBaseConsonant();
sl@0: 
sl@0:             // write base consonant
sl@0:             for (i = baseConsonant; i < bcSpan; i += 1) {
sl@0: 	        	// 1922 mlyl -->
sl@0: 	        	if (i >= 2 && chars[i] == C_SIGN_ZWJ && classTable->isMlylChillu(chars[i-2]))
sl@0: 	        		output.writeChar(chars[i], i, &tagArray[2]);
sl@0: 	        	else
sl@0: 	        	// <-- 1922 mlyl
sl@0: 	                output.writeChar(chars[i], i, &tagArray[4]);
sl@0:             }
sl@0: 
sl@0:             //write Mbelow, SMbelow, Mabove, Mpost after base consonant with flag SF_MATRAS_AFTER_BASE setting
sl@0:             if ((classTable->scriptFlags & SF_MATRAS_AFTER_BASE) != 0) {
sl@0:                 output.writeMbelow();
sl@0:                 output.writeSMbelow(); // FIXME: there are no SMs in these scripts...
sl@0:                 output.writeMabove();
sl@0:                 output.writeMpost();
sl@0:             }
sl@0: 
sl@0:             // write below-base consonants
sl@0:             if (baseConsonant != lastConsonant) {
sl@0:                 for (i = bcSpan + 1; i < postBase; i += 1) {
sl@0:                     output.writeChar(chars[i], i, &tagArray[1]);
sl@0:                 }
sl@0: 
sl@0:                 if (postBase > lastConsonant) {
sl@0:                     // write halant that was after base consonant
sl@0:                     output.writeChar(chars[bcSpan], bcSpan, &tagArray[1]);
sl@0:                 }
sl@0:             }
sl@0: 
sl@0:             // Added by Nokia: write Mbelow, SMbelow, Mabove after below-base consonants without flag SF_MATRAS_AFTER_BASE or SF_MATRAS_AFTER_POSTBASE
sl@0:             if (((classTable->scriptFlags & SF_MATRAS_AFTER_BASE) == 0) &&
sl@0:             	((classTable->scriptFlags & SF_MATRAS_AFTER_POSTBASE) == 0)) {
sl@0:             	output.writeMbelow();
sl@0:                 output.writeSMbelow();
sl@0:                 output.writeMabove();
sl@0:             }
sl@0: 
sl@0:             if ((classTable->scriptFlags & SF_REPH_AFTER_BELOW) != 0) {
sl@0:                 if (baseLimit == prev + 2) {
sl@0:                     output.writeChar(chars[prev], prev, &tagArray[0]);
sl@0:                     output.writeChar(chars[prev + 1], prev + 1, &tagArray[0]);
sl@0:                 }
sl@0: 
sl@0:                 output.writeVMabove();
sl@0:                 output.writeSMabove(); // FIXME: there are no SM's in these scripts...
sl@0:             }
sl@0: 
sl@0:             // write post-base consonants
sl@0:             // FIXME: does this put the right tags on post-base consonants?
sl@0:             if (baseConsonant != lastConsonant) {
sl@0:                 if (postBase <= lastConsonant) {
sl@0:                     for (i = postBase; i <= lastConsonant; i += 1) {
sl@0:                         output.writeChar(chars[i], i, &tagArray[3]);
sl@0:                     }
sl@0: 
sl@0:                     // write halant that was after base consonant
sl@0:                     output.writeChar(chars[bcSpan], bcSpan, &tagArray[1]);
sl@0:                 }
sl@0: 
sl@0:                 // write the training halant, if there is one
sl@0:                 if (lastConsonant < matra && classTable->isVirama(chars[matra])) {
sl@0:                     output.writeChar(chars[matra], matra, &tagArray[4]);
sl@0:                 }
sl@0:             }
sl@0: 	        
sl@0:             // Added by Nokia: write Mbelow, SMbelow, Mabove and Mpost after post-base consonants with flag SF_MATRAS_AFTER_POSTBASE
sl@0: 	        if ((classTable->scriptFlags & SF_MATRAS_AFTER_POSTBASE) != 0) {
sl@0: 	        		output.writeMbelow();
sl@0:                     output.writeSMbelow();
sl@0:                     output.writeMabove();
sl@0:                     output.writeMpost();
sl@0: 	        }
sl@0: 
sl@0: 	        //Added by Nokia: write Mpost finally
sl@0:             if (((classTable->scriptFlags & SF_MATRAS_AFTER_BASE) == 0) &&
sl@0:             	((classTable->scriptFlags & SF_MATRAS_AFTER_POSTBASE) == 0)) {
sl@0:                 output.writeMpost();
sl@0: 	        }
sl@0:             
sl@0: 	        output.writeLengthMark();
sl@0: 
sl@0:             // write reph
sl@0:             if ((classTable->scriptFlags & SF_REPH_AFTER_BELOW) == 0) {
sl@0:                 if (baseLimit == prev + 2) {
sl@0:                     output.writeChar(chars[prev], prev, &tagArray[0]);
sl@0:                     output.writeChar(chars[prev + 1], prev + 1, &tagArray[0]);
sl@0:                 }
sl@0: 
sl@0:                 output.writeVMabove();
sl@0:                 output.writeSMabove();
sl@0:             }
sl@0: 
sl@0:             output.writeVMpost();
sl@0: 
sl@0:             break;
sl@0:         }
sl@0: 
sl@0:         default:
sl@0:             break;
sl@0:         }
sl@0: 
sl@0:         prev = syllable;
sl@0:     }
sl@0: 
sl@0:     *outMPreFixups = mpreFixups;
sl@0: 
sl@0:     return output.getOutputIndex();
sl@0: }
sl@0: 
sl@0: void IndicReordering::adjustMPres(MPreFixups *mpreFixups,
sl@0:     LEGlyphStorage &glyphStorage, LEErrorCode& success)
sl@0: {
sl@0:     if (LE_SUCCESS(success) && mpreFixups != NULL) {
sl@0:         mpreFixups->apply(glyphStorage, success);
sl@0:         
sl@0:         delete mpreFixups;
sl@0:     }
sl@0: }
sl@0: 
sl@0: U_NAMESPACE_END