Example #1
0
        /// <summary>
        /// Applies a font to the specified Chars of a string.
        /// </summary>
        /// <param name="startIndex">The start index to apply the font to (inclusive).</param>
        /// <param name="endIndex">The end index to apply the font to (exclusive).</param>
        /// <param name="fontIndex">The font to use.</param>
        public void ApplyFont(int startIndex, int endIndex, short fontIndex)
        {
            if (startIndex > endIndex)
            {
                throw new ArgumentException("Start index must be less than end index.");
            }
            if (startIndex < 0 || endIndex > Length)
            {
                throw new ArgumentException("Start and end index not in range.");
            }
            if (startIndex == endIndex)
            {
                return;
            }

            //Need to Check what the font Is currently, so we can reapply it after
            //the range Is completed
            short currentFont = NO_FONT;

            if (endIndex != Length)
            {
                currentFont = this.GetFontAtIndex(endIndex);
            }

            //Need to clear the current formatting between the startIndex and endIndex
            _string = CloneStringIfRequired();
            System.Collections.Generic.List <UnicodeString.FormatRun> formatting = _string.FormatIterator();

            ArrayList deletedFR = new ArrayList();

            if (formatting != null)
            {
                IEnumerator formats = formatting.GetEnumerator();
                while (formats.MoveNext())
                {
                    UnicodeString.FormatRun r = (UnicodeString.FormatRun)formats.Current;
                    if ((r.CharacterPos >= startIndex) && (r.CharacterPos < endIndex))
                    {
                        deletedFR.Add(r);
                    }
                }
            }
            foreach (UnicodeString.FormatRun fr in deletedFR)
            {
                _string.RemoveFormatRun(fr);
            }

            _string.AddFormatRun(new UnicodeString.FormatRun((short)startIndex, fontIndex));
            if (endIndex != Length)
            {
                _string.AddFormatRun(new UnicodeString.FormatRun((short)endIndex, currentFont));
            }

            AddToSSTIfRequired();
        }