Example #1
0
        /// <summary>
        ///     Removes tashkeel. Converts general RTL letters to isolated form. Also fixes Farsi and Arabic ی letter.
        /// </summary>
        /// <param name="input">Input to prepare</param>
        /// <returns>Prepared input in char array</returns>
        protected virtual char[] PrepareInput(string input)
        {
            input = RemoveTashkeel(input);
            char[] letters = input.ToCharArray();
            for (int i = 0; i < letters.Length; i++)
            {
                if (Farsi && letters[i] == (int)GeneralLetters.Ya)
                {
                    letters[i] = (char)GeneralLetters.PersianYa;
                }
                else if (Farsi == false && letters[i] == (int)GeneralLetters.PersianYa)
                {
                    letters[i] = (char)GeneralLetters.Ya;
                }

                letters[i] = (char)GlyphTable.Convert(letters[i]);
            }

            return(letters);
        }
Example #2
0
        /// <summary>
        ///     Fixes the shape of letters based on their position.
        /// </summary>
        /// <param name="letters"></param>
        /// <returns></returns>
        protected virtual char[] FixGlyphs(char[] letters)
        {
            // We copy the letters into final letters and fix the ones that actually need a fix
            char[] lettersFinal = new char[letters.Length];
            Array.Copy(letters, lettersFinal, letters.Length);

            for (int i = 0; i < letters.Length; i++)
            {
                bool skipNext = false;

                // For special Lam Letter connections.
                if (letters[i] == (char)GeneralLetters.Lam)
                {
                    if (i < letters.Length - 1)
                    {
                        skipNext = HandleSpecialLam(letters, lettersFinal, i);
                    }
                }

                // We don't want to fix tatweel or zwnj character
                if (letters[i] == (int)GeneralLetters.ArabicTatweel || letters[i] == (int)GeneralLetters.ZeroWidthNoJoiner)
                {
                    continue;
                }

                if (IsRTLCharacter(letters[i]))
                {
                    if (IsMiddleLetter(letters, i))
                    {
                        letters[i]      = (char)GlyphTable.Convert(letters[i]);
                        lettersFinal[i] = (char)(letters[i] + 3);
                    }
                    else if (IsFinishingLetter(letters, i))
                    {
                        letters[i]      = (char)GlyphTable.Convert(letters[i]);
                        lettersFinal[i] = (char)(letters[i] + 1);
                    }
                    else if (IsLeadingLetter(letters, i))
                    {
                        letters[i]      = (char)GlyphTable.Convert(letters[i]);
                        lettersFinal[i] = (char)(letters[i] + 2);
                    }
                }

                // If this letter as Lam and special Lam-Alef connection was made, We want to skip the Alef
                // (Lam-Alef occupies 1 space)
                if (skipNext)
                {
                    i++;
                    continue;
                }

                if (!PreserveNumbers && char.IsDigit(letters[i]))
                {
                    lettersFinal[i] = FixNumbers(letters[i]);
                }
            }

            //Restore tashkeel to their places.
            lettersFinal = RestoreTashkeel(lettersFinal);
            return(lettersFinal);
        }
Example #3
0
        /// <summary>
        ///     Fixes the shape of letters based on their position.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="output"></param>
        /// <param name="preserveNumbers"></param>
        /// <param name="farsi"></param>
        /// <returns></returns>
        public static void Fix(FastStringBuilder input, FastStringBuilder output, bool preserveNumbers, bool farsi, bool fixTextTags)
        {
            FixYah(input, farsi);

            output.SetValue(input);

            for (int i = 0; i < input.Length; i++)
            {
                bool skipNext = false;
                int  iChar    = input.Get(i);

                // For special Lam Letter connections.
                if (iChar == (int)ArabicGeneralLetters.Lam)
                {
                    if (i < input.Length - 1)
                    {
                        skipNext = HandleSpecialLam(input, output, i);
                        if (skipNext)
                        {
                            iChar = output.Get(i);
                        }
                    }
                }

                // We don't want to fix tatweel or zwnj character
                if (iChar == (int)ArabicGeneralLetters.ArabicTatweel ||
                    iChar == (int)ArabicGeneralLetters.ZeroWidthNoJoiner)
                {
                    continue;
                }

                if (iChar < 0xFFFF && TextUtils.IsGlyphFixedArabicCharacter((char)iChar))
                {
                    char converted = GlyphTable.Convert((char)iChar);

                    if (IsMiddleLetter(input, i))
                    {
                        output.Set(i, (char)(converted + 3));
                    }
                    else if (IsFinishingLetter(input, i))
                    {
                        output.Set(i, (char)(converted + 1));
                    }
                    else if (IsLeadingLetter(input, i))
                    {
                        output.Set(i, (char)(converted + 2));
                    }
                }

                // If this letter as Lam and special Lam-Alef connection was made, We want to skip the Alef
                // (Lam-Alef occupies 1 space)
                if (skipNext)
                {
                    i++;
                }
            }

            if (!preserveNumbers)
            {
                if (fixTextTags)
                {
                    FixNumbersOutsideOfTags(output, farsi);
                }
                else
                {
                    FixNumbers(output, farsi);
                }
            }
        }