+ Character character = *(textBuffer + index);
+
+ // Get the script of the character.
+ Script script = TextAbstraction::GetCharacterScript(character);
+
+ // Some characters (like white spaces) are valid for many scripts. The rules to set a script
+ // for them are:
+ // - If they are at the begining of a paragraph they get the script of the first character with
+ // a defined script. If they are at the end, they get the script of the last one.
+ // - If they are between two scripts with the same direction, they get the script of the previous
+ // character with a defined script. If the two scripts have different directions, they get the
+ // script of the first character of the paragraph with a defined script.
+
+ // Skip those characters valid for many scripts like white spaces or '\n'.
+ bool endOfText = index == lastCharacter;
+ while(!endOfText &&
+ (TextAbstraction::COMMON == script))
+ {
+ // Check if whether is right to left markup and Keeps true if the previous value was true.
+ currentScriptRun.isRightToLeft = currentScriptRun.isRightToLeft || TextAbstraction::IsRightToLeftMark(character);
+
+ // ZWJ, ZWNJ between emojis should be treated as EMOJI.
+ if(TextAbstraction::EMOJI == currentScriptRun.script && !(TextAbstraction::IsZeroWidthJoiner(character) || TextAbstraction::IsZeroWidthNonJoiner(character)))
+ {
+ // Emojis doesn't mix well with characters common to all scripts. Insert the emoji run.
+ scripts.Insert(scripts.Begin() + scriptIndex, currentScriptRun);
+ ++scriptIndex;
+
+ // Initialize the new one.
+ currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
+ currentScriptRun.characterRun.numberOfCharacters = 0u;
+ currentScriptRun.script = TextAbstraction::UNKNOWN;
+ numberOfAllScriptCharacters = 0u;
+ }
+
+ // Count all these characters to be added into a script.
+ ++numberOfAllScriptCharacters;
+
+ if(TextAbstraction::IsNewParagraph(character))
+ {
+ // The character is a new paragraph.
+ // To know when there is a new paragraph is needed because if there is a white space
+ // between two scripts with different directions, it is added to the script with
+ // the same direction than the first script of the paragraph.
+ isFirstScriptToBeSet = true;
+
+ // Characters common to all scripts at the end of the paragraph are added to the last script.
+ currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
+
+ // Store the script run.
+ scripts.Insert(scripts.Begin() + scriptIndex, currentScriptRun);
+ ++scriptIndex;
+
+ // Initialize the new one.
+ currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
+ currentScriptRun.characterRun.numberOfCharacters = 0u;
+ currentScriptRun.script = TextAbstraction::UNKNOWN;
+ numberOfAllScriptCharacters = 0u;
+ // Initialize whether is right to left direction
+ currentScriptRun.isRightToLeft = false;
+ }