Ejemplo n.º 1
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)
        {
            FixYah(input, farsi);

            output.SetValue(input);

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

                // For special Lam Letter connections.
                if (iChar == (char)GeneralLetters.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)GeneralLetters.ArabicTatweel ||
                    iChar == (int)GeneralLetters.ZeroWidthNoJoiner)
                {
                    continue;
                }

                if (TextUtils.IsRTLCharacter(iChar))
                {
                    char converted = GlyphTable.Convert(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)
            {
                FixNumbers(output, farsi);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Handles the special Lam-Alef connection in the text.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="output"></param>
        /// <param name="i">Index of Lam letter</param>
        /// <returns><see langword="true" /> if special connection has been made.</returns>
        private static bool HandleSpecialLam(FastStringBuilder input, FastStringBuilder output, int i)
        {
            bool isFixed;
            switch (input.Get(i + 1))
            {
                case (char)GeneralLetters.AlefMaksoor:
                    output.Set(i, (char)0xFEF7);
                    isFixed = true;
                    break;
                case (char)GeneralLetters.Alef:
                    output.Set(i, (char)0xFEF9);
                    isFixed = true;
                    break;
                case (char)GeneralLetters.AlefHamza:
                    output.Set(i, (char)0xFEF5);
                    isFixed = true;
                    break;
                case (char)GeneralLetters.AlefMad:
                    output.Set(i, (char)0xFEF3);
                    isFixed = true;
                    break;
                default:
                    isFixed = false;
                    break;
            }

            if (isFixed)
            {
                output.Set(i + 1, (char)0xFFFF);
            }

            return isFixed;
        }
Ejemplo n.º 3
0
 /// <summary>
 ///     Removes tashkeel. Converts general RTL letters to isolated form. Also fixes Farsi and Arabic ی letter.
 /// </summary>
 /// <param name="text">Input to prepare</param>
 /// <param name="farsi"></param>
 /// <returns>Prepared input in char array</returns>
 public static void FixYah(FastStringBuilder text, bool farsi)
 {
     for (int i = 0; i < text.Length; i++)
     {
         if (farsi && text.Get(i) == (int)GeneralLetters.Ya)
         {
             text.Set(i, (char)GeneralLetters.PersianYa);
         }
         else if (farsi == false && text.Get(i) == (int)GeneralLetters.PersianYa)
         {
             text.Set(i, (char)GeneralLetters.Ya);
         }
     }
 }
Ejemplo n.º 4
0
        public void SetValue_MakesIndependentCopies()
        {
            var builder1 = new FastStringBuilder("my holy moly text");
            var builder2 = new FastStringBuilder(10);

            builder2.SetValue(builder1);

            builder1.Set(5, 'f');

            Assert.AreNotEqual(builder1.ToString(), builder2.ToString());
        }