/// <summary>
        /// Returns the font in use at a particular index.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <returns>The font that's currently being applied at that
        /// index or null if no font Is being applied or the
        /// index Is out of range.</returns>
        public short GetFontAtIndex(int index)
        {
            int       size       = str.FormatRunCount;
            FormatRun currentRun = null;

            for (int i = 0; i < size; i++)
            {
                FormatRun r = str.GetFormatRun(i);
                if (r.CharacterPos > index)
                {
                    break;
                }
                else
                {
                    currentRun = r;
                }
            }
            if (currentRun == null)
            {
                return(NO_FONT);
            }
            else
            {
                return(currentRun.FontIndex);
            }
        }
Exemple #2
0
        /**
         * return a character representation of the fields of this record
         *
         *
         * @return String of output for biffviewer etc.
         *
         */
        public String GetDebugInfo()
        {
            StringBuilder buffer = new StringBuilder();

            buffer.Append("[UNICODESTRING]\n");
            buffer.Append("    .charcount       = ")
            .Append(StringUtil.ToHexString(CharCount)).Append("\n");
            buffer.Append("    .optionflags     = ")
            .Append(StringUtil.ToHexString(OptionFlags)).Append("\n");
            buffer.Append("    .string          = ").Append(String).Append("\n");
            if (field_4_format_Runs != null)
            {
                for (int i = 0; i < field_4_format_Runs.Count; i++)
                {
                    FormatRun r = field_4_format_Runs[(i)];
                    buffer.Append("      .format_Run" + i + "          = ").Append(r.ToString()).Append("\n");
                }
            }
            if (field_5_ext_rst != null)
            {
                buffer.Append("    .field_5_ext_rst          = ").Append("\n");
                buffer.Append(field_5_ext_rst.ToString()).Append("\n");
            }
            buffer.Append("[/UNICODESTRING]\n");
            return(buffer.ToString());
        }
Exemple #3
0
 public void RemoveFormatRun(FormatRun r)
 {
     field_4_format_Runs.Remove(r);
     if (field_4_format_Runs.Count == 0)
     {
         field_4_format_Runs = null;
         field_2_optionflags = richText.ClearByte(field_2_optionflags);
     }
 }
        /// <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(startIndex);
            }

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

            ArrayList deletedFR = new ArrayList();

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

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

            AddToSSTIfRequired();
        }
Exemple #5
0
        private int FindFormatRunAt(int characterPos)
        {
            int size = field_4_format_Runs.Count;

            for (int i = 0; i < size; i++)
            {
                FormatRun r = field_4_format_Runs[(i)];
                if (r._character == characterPos)
                {
                    return(i);
                }
                else if (r._character > characterPos)
                {
                    return(-1);
                }
            }
            return(-1);
        }
Exemple #6
0
        public AlRuns(IStreamReader reader, RecordType id, UInt16 length)
            : base(reader, id, length)
        {
            // assert that the correct record type is instantiated
            Debug.Assert(this.Id == ID);

            // initialize class members from stream
            cRuns = reader.ReadUInt16();

            if (cRuns > 0)
            {
                rgRuns = new FormatRun[cRuns];

                for (int i = 0; i < cRuns; i++)
                {
                    rgRuns[i] = new FormatRun(reader);
                }
            }

            // assert that the correct number of bytes has been read from the stream
            Debug.Assert(this.Offset + this.Length == this.Reader.BaseStream.Position);
        }
        public void TestSmallStringSize()
        {
            //Test a basic string
            UnicodeString s = MakeUnicodeString("Test");

            ConfirmSize(7, s);

            //Test a small string that is uncompressed
            s             = MakeUnicodeString(STR_16_BIT);
            s.OptionFlags = ((byte)0x01);
            ConfirmSize(11, s);

            //Test a compressed small string that has rich text formatting
            s.String      = "Test";
            s.OptionFlags = ((byte)0x8);
            FormatRun r = new FormatRun((short)0, (short)1);

            s.AddFormatRun(r);
            FormatRun r2 = new FormatRun((short)2, (short)2);

            s.AddFormatRun(r2);
            ConfirmSize(17, s);

            //Test a uncompressed small string that has rich text formatting
            s.String      = STR_16_BIT;
            s.OptionFlags = ((byte)0x9);
            ConfirmSize(21, s);

            //Test a compressed small string that has rich text and extended text
            s.String      = "Test";
            s.OptionFlags = ((byte)0xC);
            s.SetExtendedRst(new byte[] { (byte)0x1, (byte)0x2, (byte)0x3, (byte)0x4, (byte)0x5 });
            ConfirmSize(26, s);

            //Test a uncompressed small string that has rich text and extended text
            s.String      = STR_16_BIT;
            s.OptionFlags = ((byte)0xD);
            ConfirmSize(30, s);
        }
Exemple #8
0
        /** Adds a font run to the formatted string.
         *
         *  If a font run exists at the current charcter location, then it is
         *  Replaced with the font run to be Added.
         */
        public void AddFormatRun(FormatRun r)
        {
            if (field_4_format_Runs == null)
            {
                field_4_format_Runs = new List <FormatRun>();
            }

            int index = FindFormatRunAt(r._character);

            if (index != -1)
            {
                field_4_format_Runs.RemoveAt(index);
            }

            field_4_format_Runs.Add(r);
            //Need to sort the font Runs to ensure that the font Runs appear in
            //character order
            //collections.Sort(field_4_format_Runs);
            field_4_format_Runs.Sort();

            //Make sure that we now say that we are a rich string
            field_2_optionflags = richText.SetByte(field_2_optionflags);
        }
Exemple #9
0
        /**
         * Serialises out the String. There are special rules
         *  about where we can and can't split onto
         *  Continue records.
         */
        public void Serialize(ContinuableRecordOutput out1)
        {
            int numberOfRichTextRuns = 0;
            int extendedDataSize     = 0;

            if (IsRichText && field_4_format_Runs != null)
            {
                numberOfRichTextRuns = field_4_format_Runs.Count;
            }
            if (IsExtendedText && field_5_ext_rst != null)
            {
                extendedDataSize = 4 + field_5_ext_rst.DataSize;
            }

            // Serialise the bulk of the String
            // The WriteString handles tricky continue stuff for us
            out1.WriteString(field_3_string, numberOfRichTextRuns, extendedDataSize);

            if (numberOfRichTextRuns > 0)
            {
                //This will ensure that a run does not split a continue
                for (int i = 0; i < numberOfRichTextRuns; i++)
                {
                    if (out1.AvailableSpace < 4)
                    {
                        out1.WriteContinue();
                    }
                    FormatRun r = field_4_format_Runs[(i)];
                    r.Serialize(out1);
                }
            }

            if (extendedDataSize > 0)
            {
                field_5_ext_rst.Serialize(out1);
            }
        }
Exemple #10
0
        public int CompareTo(UnicodeString str)
        {
            //int result = String.CompareTo(str.String);
            int result = string.Compare(String, str.String, StringComparison.CurrentCulture);

            //As per the Equals method lets do this in stages
            if (result != 0)
            {
                return(result);
            }

            //OK string appears to be equal but now lets compare formatting Runs
            if ((field_4_format_Runs == null) && (str.field_4_format_Runs == null))
            {
                //Strings are Equal, and there are no formatting Runs.
                return(0);
            }

            if ((field_4_format_Runs == null) && (str.field_4_format_Runs != null))
            {
                //Strings are Equal, but one or the other has formatting Runs
                return(1);
            }
            if ((field_4_format_Runs != null) && (str.field_4_format_Runs == null))
            {
                //Strings are Equal, but one or the other has formatting Runs
                return(-1);
            }

            //Strings are Equal, so now compare formatting Runs.
            int size = field_4_format_Runs.Count;

            if (size != str.field_4_format_Runs.Count)
            {
                return(size - str.field_4_format_Runs.Count);
            }

            for (int i = 0; i < size; i++)
            {
                FormatRun Run1 = field_4_format_Runs[(i)];
                FormatRun run2 = str.field_4_format_Runs[(i)];

                result = Run1.CompareTo(run2);
                if (result != 0)
                {
                    return(result);
                }
            }

            //Well the format Runs are equal as well!, better check the ExtRst data
            if ((field_5_ext_rst == null) && (str.field_5_ext_rst == null))
            {
                return(0);
            }
            if ((field_5_ext_rst == null) && (str.field_5_ext_rst != null))
            {
                return(1);
            }
            if ((field_5_ext_rst != null) && (str.field_5_ext_rst == null))
            {
                return(-1);
            }

            result = field_5_ext_rst.CompareTo(str.field_5_ext_rst);
            if (result != 0)
            {
                return(result);
            }

            //Phew!! After all of that we have finally worked out that the strings
            //are identical.
            return(0);
        }
Exemple #11
0
        /**
         * Our handling of Equals is inconsistent with CompareTo.  The trouble is because we don't truely understand
         * rich text fields yet it's difficult to make a sound comparison.
         *
         * @param o     The object to Compare.
         * @return      true if the object is actually Equal.
         */
        public override bool Equals(Object o)
        {
            if (!(o is UnicodeString))
            {
                return(false);
            }
            UnicodeString other = (UnicodeString)o;

            //OK lets do this in stages to return a quickly, first check the actual string
            bool eq = ((field_1_charCount == other.field_1_charCount) &&
                       (field_2_optionflags == other.field_2_optionflags) &&
                       field_3_string.Equals(other.field_3_string));

            if (!eq)
            {
                return(false);
            }

            //OK string appears to be equal but now lets compare formatting Runs
            if ((field_4_format_Runs == null) && (other.field_4_format_Runs == null))
            {
                //Strings are Equal, and there are not formatting Runs.
                return(true);
            }
            if (((field_4_format_Runs == null) && (other.field_4_format_Runs != null)) ||
                (field_4_format_Runs != null) && (other.field_4_format_Runs == null))
            {
                //Strings are Equal, but one or the other has formatting Runs
                return(false);
            }

            //Strings are Equal, so now compare formatting Runs.
            int size = field_4_format_Runs.Count;

            if (size != other.field_4_format_Runs.Count)
            {
                return(false);
            }

            for (int i = 0; i < size; i++)
            {
                FormatRun Run1 = field_4_format_Runs[(i)];
                FormatRun run2 = other.field_4_format_Runs[(i)];

                if (!Run1.Equals(run2))
                {
                    return(false);
                }
            }

            // Well the format Runs are equal as well!, better check the ExtRst data
            if (field_5_ext_rst == null && other.field_5_ext_rst == null)
            {
                // Good
            }
            else if (field_5_ext_rst != null && other.field_5_ext_rst != null)
            {
                int extCmp = field_5_ext_rst.CompareTo(other.field_5_ext_rst);
                if (extCmp == 0)
                {
                    // Good
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }

            //Phew!! After all of that we have finally worked out that the strings
            //are identical.
            return(true);
        }
        /// <summary>
        /// Gets the font used in a particular formatting run.
        /// </summary>
        /// <param name="index">the index of the formatting run.</param>
        /// <returns>the font number used.</returns>
        public short GetFontOfFormattingRun(int index)
        {
            FormatRun r = str.GetFormatRun(index);

            return(r.FontIndex);
        }
        /// <summary>
        /// The index within the string to which the specified formatting run applies.
        /// </summary>
        /// <param name="index">the index of the formatting run</param>
        /// <returns>the index within the string.</returns>
        public int GetIndexOfFormattingRun(int index)
        {
            FormatRun r = str.GetFormatRun(index);

            return(r.CharacterPos);
        }