public FrameworkElement Create(TextFormatterProvider provider, bool useEllipsis, bool filterOutNewLines) { var textBlockText = sb.ToString(); tokens.Finish(); if (!useEllipsis && filterOutNewLines) { return new FastTextBlock(provider, new TextSrc { text = textBlockText, tokens = tokens, }); } var textBlock = new TextBlock(); int offs = 0; foreach (var line in GetLines(textBlockText)) { if (offs != 0 && !filterOutNewLines) textBlock.Inlines.Add(new LineBreak()); int endOffs = offs + line.Item1.Length; Debug.Assert(offs <= textBlockText.Length); while (offs < endOffs) { int defaultTextLength, tokenLength; TextTokenKind tokenKind; if (!tokens.Find(offs, out defaultTextLength, out tokenKind, out tokenLength)) { Debug.Fail("Could not find token info"); break; } if (defaultTextLength != 0) { var text = textBlockText.Substring(offs, defaultTextLength); textBlock.Inlines.Add(text); } offs += defaultTextLength; if (tokenLength != 0) { var hlColor = GetColor(tokenKind); var text = textBlockText.Substring(offs, tokenLength); var elem = new Run(text); if (hlColor.FontStyle != null) elem.FontStyle = hlColor.FontStyle.Value; if (hlColor.FontWeight != null) elem.FontWeight = hlColor.FontWeight.Value; if (hlColor.Foreground != null) elem.Foreground = hlColor.Foreground; if (hlColor.Background != null) elem.Background = hlColor.Background; textBlock.Inlines.Add(elem); } offs += tokenLength; } Debug.Assert(offs == endOffs); offs += line.Item2; Debug.Assert(offs <= textBlockText.Length); } if (useEllipsis) textBlock.TextTrimming = TextTrimming.CharacterEllipsis; return textBlock; }
/// <summary> /// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object. /// </summary> /// <param name="owner"></param> /// <param name="provider"></param> /// <returns></returns> public static ITextFormatter Create(DependencyObject owner, TextFormatterProvider provider) { if (owner == null) { throw new ArgumentNullException("owner"); } #if DOTNET4 switch (provider) { case TextFormatterProvider.BuiltIn: return(new WpfTextFormatter(TextOptions.GetTextFormattingMode(owner))); case TextFormatterProvider.GlyphRunFormatter: return(new GlyphRunFormatter(TextOptions.GetTextFormattingMode(owner))); } return(null); #else object formattingMode = null; if (TextFormattingModeProperty != null) { formattingMode = owner.GetValue(TextFormattingModeProperty); } switch (provider) { case TextFormatterProvider.BuiltIn: return(new WpfTextFormatter(formattingMode)); case TextFormatterProvider.GlyphRunFormatter: return(new Rendering.GlyphRunFormatter(formattingMode)); } #endif }
public FastTextBlock(TextFormatterProvider provider, IFastTextSource src) { this.provider = provider; this.src = src; }
public FastTextBlock(TextFormatterProvider provider) : this(provider, new TextSrc()) { }
public FrameworkElement Create(TextFormatterProvider provider, bool useEllipsis, bool filterOutNewLines) { var textBlockText = sb.ToString(); tokens.Finish(); if (!useEllipsis && filterOutNewLines) { return(new FastTextBlock(provider, new TextSrc { text = textBlockText, tokens = tokens, })); } var textBlock = new TextBlock(); int offs = 0; foreach (var line in GetLines(textBlockText)) { if (offs != 0 && !filterOutNewLines) { textBlock.Inlines.Add(new LineBreak()); } int endOffs = offs + line.Item1.Length; Debug.Assert(offs <= textBlockText.Length); while (offs < endOffs) { int defaultTextLength, tokenLength; TextTokenKind tokenKind; if (!tokens.Find(offs, out defaultTextLength, out tokenKind, out tokenLength)) { Debug.Fail("Could not find token info"); break; } if (defaultTextLength != 0) { var text = textBlockText.Substring(offs, defaultTextLength); textBlock.Inlines.Add(text); } offs += defaultTextLength; if (tokenLength != 0) { var hlColor = GetColor(tokenKind); var text = textBlockText.Substring(offs, tokenLength); var elem = new Run(text); if (hlColor.FontStyle != null) { elem.FontStyle = hlColor.FontStyle.Value; } if (hlColor.FontWeight != null) { elem.FontWeight = hlColor.FontWeight.Value; } if (hlColor.Foreground != null) { elem.Foreground = hlColor.Foreground; } if (hlColor.Background != null) { elem.Background = hlColor.Background; } textBlock.Inlines.Add(elem); } offs += tokenLength; } Debug.Assert(offs == endOffs); offs += line.Item2; Debug.Assert(offs <= textBlockText.Length); } if (useEllipsis) { textBlock.TextTrimming = TextTrimming.CharacterEllipsis; } return(textBlock); }