public void ArrangeLines(bool textWrap, SizeF proposedSize) { if (!textWrap || (double)proposedSize.Width <= 0.0 && (double)proposedSize.Height <= 0.0) { return; } for (int index1 = 0; index1 < this.lines.Count; ++index1) { while ((double)FormattedTextBlock.GetTextLineSize(this.lines[index1]).Width > (double)proposedSize.Width && this.lines[index1].List.Count > 1) { int index2 = this.lines[index1].List.Count - 1; FormattedText formattedText = this.lines[index1].List[index2]; this.lines[index1].List.RemoveAt(index2); if (index1 == this.lines.Count - 1) { this.lines.Add(new TextLine()); } TextLine line = this.lines[index1 + 1]; if (line.List.Count > 0 && !line.List[0].StartNewLine) { line.List.Insert(0, formattedText); } else { this.lines.Insert(index1 + 1, new TextLine() { List = { formattedText } }); } } } }
public SizeF GetTextSize( SizeF proposedSize, bool useCompatibleTextRendering, StringFormat sf, TextFormatFlags textFormatFlags, bool textWrap) { if (textWrap) { this.RecalculateBlockLines(textWrap); this.ArrangeLines(textWrap, proposedSize); } SizeF empty1 = (SizeF)Size.Empty; SizeF empty2 = (SizeF)Size.Empty; foreach (TextLine line in this.lines) { SizeF textLineSize = FormattedTextBlock.GetTextLineSize(line); float height = (double)textLineSize.Height <= 2.0 ? 0.0f : textLineSize.Height; line.LineSize = new SizeF(textLineSize.Width, height); empty1.Height += height; empty1.Width = Math.Max(textLineSize.Width, empty1.Width); } return(empty1); }
/// <summary> /// Main function for parsing process /// </summary> /// <param name="text">text to parse</param> /// <param name="baseForeColor">base Font color</param> /// <param name="baseFont">base font</param> /// <param name="fontSize">base font size</param> /// <param name="aligment">base textaligment</param> /// <param name="fontStyle"> base font style etc. Regular, Bold</param> /// <returns>Formatted text block that contains the whole structure</returns> public static FormattedTextBlock Parse(string text, Color baseForeColor, string baseFont, float fontSize, FontStyle fontStyle, ContentAlignment aligment) { if (string.IsNullOrEmpty(text)) { return(new FormattedTextBlock()); } Stack <FormattedText.HTMLLikeListType> lastListType = new Stack <FormattedText.HTMLLikeListType>(); lastListType.Push(FormattedText.HTMLLikeListType.None); text = text.Replace("\\<", "<").Replace("\\>", ">").Replace("\r\n", "<br>").Replace("\n", "<br>"); //prepare initially block FormattedTextBlock textBlock = new FormattedTextBlock(); FormattedText previousFormattedText = new FormattedText();//this is a base item for creating HTML previousFormattedText.FontColor = baseForeColor; previousFormattedText.FontName = baseFont; previousFormattedText.FontSize = fontSize; previousFormattedText.ContentAlignment = aligment; previousFormattedText.FontStyle = fontStyle; //create tokens StringTokenizer tokenizer = new StringTokenizer(text, "<"); int count = tokenizer.Count(); bool hasOpenTag = text.IndexOf("<") > -1; TextLine currentLine = new TextLine(); textBlock.Lines.Add(currentLine); bool shouldProduceNewLine = false; TinyHTMLParsersData parsersData = new TinyHTMLParsersData(); //enumerate tokens for (int i = 0; i < count; ++i) { FormattedText currentItem = ProcessToken(ref previousFormattedText, tokenizer, hasOpenTag, parsersData, ref shouldProduceNewLine); if (!string.IsNullOrEmpty(currentItem.HtmlTag) && currentItem.HtmlTag.Length >= 1 && shouldProduceNewLine) { currentLine = new TextLine(); textBlock.Lines.Add(currentLine); if (currentItem.HtmlTag.Length >= 2) { currentItem.HtmlTag = currentItem.HtmlTag.TrimEnd(' ').Trim('/'); } shouldProduceNewLine = (!string.IsNullOrEmpty(currentItem.text) && currentItem.text.Trim().Length == 0); } currentLine.List.Add(currentItem); previousFormattedText = new FormattedText(currentItem); } return(textBlock); }
public void MouseUp(object sender, MouseEventArgs e) { FormattedText formattedText = this.IsMouseOverBlock(sender, e); if (formattedText == null) { return; } formattedText.FontColor = TinyHTMLParsers.LinkClickedColor; FormattedTextBlock.RunLink(formattedText.Link); }
public static FormattedTextBlock Parse( string text, Color baseForeColor, string baseFont, float fontSize, FontStyle fontStyle, ContentAlignment aligment) { if (string.IsNullOrEmpty(text)) { return(new FormattedTextBlock()); } new Stack <FormattedText.HTMLLikeListType>().Push(FormattedText.HTMLLikeListType.None); text = text.Replace("\\", "[CDATA[\\]]"); text = text.Replace("\\<", "<"); text = text.Replace("\\>", ">"); text = text.Replace("\r\n", "<br>"); text = text.Replace("\n", "<br>"); FormattedTextBlock formattedTextBlock = new FormattedTextBlock(); FormattedText prevItem = new FormattedText(); prevItem.FontColor = baseForeColor; prevItem.FontName = baseFont; prevItem.FontSize = fontSize; prevItem.ContentAlignment = aligment; prevItem.FontStyle = fontStyle; StringTokenizer tokenizer = new StringTokenizer(text, "<"); int num = tokenizer.Count(); bool hasOpenTag = text.IndexOf("<") > -1; TextLine textLine = new TextLine(); formattedTextBlock.Lines.Add(textLine); bool shouldProduceNewLine = false; TinyHTMLParsers.TinyHTMLParsersData parserData = new TinyHTMLParsers.TinyHTMLParsersData(); for (int index = 0; index < num; ++index) { FormattedText prototypeFormattedText = TinyHTMLParsers.ProcessToken(ref prevItem, tokenizer, hasOpenTag, parserData, ref shouldProduceNewLine); if (!string.IsNullOrEmpty(prototypeFormattedText.HtmlTag) && prototypeFormattedText.HtmlTag.Length >= 1 && shouldProduceNewLine) { textLine = new TextLine(); formattedTextBlock.Lines.Add(textLine); if (prototypeFormattedText.HtmlTag.Length >= 2) { prototypeFormattedText.HtmlTag = prototypeFormattedText.HtmlTag.TrimEnd(' ').Trim('/'); } shouldProduceNewLine = !string.IsNullOrEmpty(prototypeFormattedText.text) && prototypeFormattedText.text.Trim().Length == 0; } textLine.List.Add(prototypeFormattedText); prevItem = new FormattedText(prototypeFormattedText); } return(formattedTextBlock); }