Exemple #1
0
        /// <summary>
        /// Performs simple parsing of the text.
        /// </summary>
        protected override void ProcessText()
        {
            string[] ss = Split(RawText);

            foreach (string s in ss)
            {
                InnerWords.Add(new Word(s));
            }
        }
Exemple #2
0
        /// <summary>
        /// Processes formatting text.
        /// </summary>
        protected override void ProcessText()
        {
            Stack formatting = new Stack();
            int   pos        = 0;

            // Locate formatting tags
            MatchCollection matches = _recognizer.Matches(RawText);

            foreach (Match match in matches)
            {
                if (match.Index > pos)
                {
                    string[] ss = Split(
                        RawText.Substring(pos, match.Index - pos));

                    // Collect formatting
                    Styles f     = Styles.Regular;
                    object color = null;
                    foreach (FormatToken token in formatting)
                    {
                        f |= token.Format;
                        if (token.Format == Styles.Color)
                        {
                            color = token.Param;
                        }
                    }

                    foreach (string s in ss)
                    {
                        Word word = null;

                        if (color != null)
                        {
                            word = new StyledWord(s, f, (Color)color);
                        }
                        else
                        {
                            word = new StyledWord(s, f, Color.Empty);
                        }

                        InnerWords.Add(word);
                    }

                    // Advance pos
                    pos = match.Index;
                }

                if (match.Index == pos)
                {
                    // Modify formatting
                    Styles f       = Styles.Regular;
                    object param   = null;
                    bool   closing = match.Value.IndexOf("/") != -1;
                    string mval    = match.Value.ToLower();

                    if (mval == "<b>" || mval == "</b>")
                    {
                        f = Styles.Bold;
                    }

                    else if (mval == "<i>" || mval == "</i>")
                    {
                        f = Styles.Italic;
                    }

                    else if (mval == "<u>" || mval == "</u>")
                    {
                        f = Styles.Underline;
                    }

                    else if (mval == "<sub>" || mval == "</sub>")
                    {
                        f = Styles.Sub;
                    }

                    else if (mval == "<sup>" || mval == "</sup>")
                    {
                        f = Styles.Sup;
                    }

                    else if (mval.IndexOf("color") != -1)
                    {
                        f = Styles.Color;
                        if (!closing)
                        {
                            int    spos  = mval.IndexOf("#");
                            string color = mval.Substring(spos + 1, 6);
                            param = Color.FromArgb(
                                int.Parse(color.Substring(0, 2), System.Globalization.NumberStyles.HexNumber),
                                int.Parse(color.Substring(2, 2), System.Globalization.NumberStyles.HexNumber),
                                int.Parse(color.Substring(4, 2), System.Globalization.NumberStyles.HexNumber));
                        }
                    }

                    // It it is opening tag, add it to the stack,
                    // otherwise - find the last corresponding opening
                    // tag and remove it from the stack.
                    if (!closing)
                    {
                        formatting.Push(new FormatToken(f, param));
                    }
                    else
                    {
                        Stack temp = new Stack();
                        while (formatting.Count > 0)
                        {
                            if ((formatting.Peek() as FormatToken).Format == f)
                            {
                                break;
                            }

                            temp.Push(formatting.Pop());
                        }

                        // Remove if found
                        if (formatting.Count > 0)
                        {
                            formatting.Pop();
                        }

                        // Return the remaining tokens
                        while (temp.Count > 0)
                        {
                            formatting.Push(temp.Pop());
                        }
                    }

                    // Advance pos
                    pos += match.Length;
                    continue;
                }

                throw new Exception("Styled text internal error.");
            }

            if (pos < RawText.Length)
            {
                string[] ss = Split(RawText.Substring(pos));

                // Collect formatting
                Styles f     = Styles.Regular;
                object color = null;
                foreach (FormatToken token in formatting)
                {
                    f |= token.Format;
                    if (token.Format == Styles.Color)
                    {
                        color = token.Param;
                    }
                }

                foreach (string s in ss)
                {
                    Word word = null;

                    if (color != null)
                    {
                        word = new StyledWord(s, f, (Color)color);
                    }
                    else
                    {
                        word = new StyledWord(s, f, Color.Empty);
                    }

                    InnerWords.Add(word);
                }
            }

            // Update the pure text
            _plainText = "";
            foreach (Word word in InnerWords)
            {
                _plainText += word.Value;
            }
        }