public bool Contains(Word word) { return base.InnerList.Contains(word); }
public bool Contains(Word word) { return(base.InnerList.Contains(word)); }
/// <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; } }
/// <summary> /// Draws a previously laid-out text at a specified offset. /// </summary> public void Draw(float xOff, float yOff, RenderTextCallback renderCallback, DrawTextHint hint) { if (GetHLines(_totalLines) == null) { throw new Exception("Draw invoked on a non-laid-out text."); } if (GetHLines(_totalLines).Count == 0) { return; } bool styled = (_text is StyledText); bool is_LastWord = false; // Original brush and font System.Drawing.Brush brushOriginal = hint.Brush; Font fontOriginal = hint.Font; // We now have the starting line and the number // of total lines to layout the text in. Go for it int iword = 0; for (int i = _startLine; i < _startLine + _totalLines; i++) { PointList hLine = GetHLines(_totalLines)[i] as PointList; for (int j = 0; j < hLine.Count; j += 2) { PointF pt1 = hLine[j]; PointF pt2 = hLine[j + 1]; RectangleF rc = new RectangleF( _bounds.X + xOff + pt1.X, _bounds.Y + yOff + pt1.Y, pt2.X - pt1.X, pt2.Y - pt1.Y); bool newLine = false; int newword = FitTextInRect(iword, rc, ref newLine); if (newword > iword) { // Calculate the total width of the words // which are about to be rendered, but skip // the whitespaces at the front and the rear int ifront = iword; int irear = newword - 1; /*DON'T SKIP WHITESPACES ( next 2 lines ) * */ while (ifront < newword && _text.Words[ifront].IsWhitespace) { ifront++; } while (irear >= ifront && _text.Words[irear].IsWhitespace) { irear--; } int w; float total = 0; for (w = ifront; w <= irear; w++) { total += _text.Words[w].Width; } // Adjust the left side of the destination // rectangle according to the specified alignment switch (_options.Alignment) { case StringAlignment.Near: // Do nothing break; case StringAlignment.Center: rc.X = rc.X + (rc.Width - total) / 2; break; case StringAlignment.Far: rc.X = rc.Right - total; break; } // Render the words in the range [ifront, irear] for (w = ifront; w <= irear; w++) { Word word = _text.Words[w]; if (w == _text.Words.Count - 1) { is_LastWord = true; } else { is_LastWord = false; } if (!_fits && hint.AddEllipsis && styled) { if (i == _startLine + _totalLines - 1) { if (j == hLine.Count - 2) { if (w == irear) { // Append the last word with ellipsis StyledText.StyledWord sword = word as StyledText.StyledWord; StyledText.StyledWord newWord = null; int chars = sword.Value.Length; float width = sword.Width; do { newWord = new StyledText.StyledWord( sword.Value.Substring(0, chars) + "...", sword.Format, sword.Color); newWord.UpdateMeasures(hint.Graphics, hint.Font); chars--; }while (chars > 0 && newWord.Width > width); word = newWord; } } } } /*DON'T SKIP WHITESPACES ( next line ) * */ if (!word.IsLineBreak && !word.IsWhitespace) { if (styled) { // In case of styled text formatting, // apply fonts and colors StyledText.StyledWord sword = word as StyledText.StyledWord; hint.Font = sword.CreateFont(hint.Font); hint.Brush = sword.CreateBrush(hint.Brush); rc.Y += sword.YOffset; } // Add 10 to width and height becaus GDI+ stupid // and clips the text hint.IsLastLine = is_LastWord; hint.CurrentWord = word; renderCallback(word.Value, new RectangleF( rc.X, rc.Y, word.Width + 10, rc.Height + 10), hint); if (styled) { // Restore font and brush StyledText.StyledWord sword = word as StyledText.StyledWord; sword.DisposeFont(hint.Font); sword.DisposeBrush(hint.Brush); hint.Font = fontOriginal; hint.Brush = brushOriginal; rc.Y -= sword.YOffset; } } rc.X += _text.Words[w].Width; } iword = newword; } if (newLine) { break; } } } }