/// <summary> /// Creates WPF Run instances that can be used for TextBlock.Inlines. /// </summary> public TextRunProperties[] CreateRuns() { TextRunProperties[] runs = new TextRunProperties[stateChanges.Count]; for (int i = 0; i < runs.Length; i++) { int startOffset = stateChangeOffsets[i]; int endOffset = i + 1 < stateChangeOffsets.Count ? stateChangeOffsets[i + 1] : text.Length; string runString = text.Substring(startOffset, endOffset - startOffset); TextRunProperties r = new TextRunProperties(); HighlightingState state = stateChanges[i]; r.Foreground = state.Foreground; r.Background = state.Background; if (state.Weight != null) { r.FontWeight = state.Weight.Value; } if (state.Family != null) { r.FontFamily = state.Family.Value; } if (state.Style != null) { r.FontStyle = state.Style.Value; } runs[i] = r; } return(runs); }
/// <summary> /// Applies the properties from the HighlightingColor to the specified text segment. /// </summary> public void SetHighlighting(int offset, int length, HighlightingColor color) { if (color == null) { throw new ArgumentNullException("color"); } int startIndex = GetIndexForOffset(offset); int endIndex = GetIndexForOffset(offset + length); for (int i = startIndex; i < endIndex; i++) { HighlightingState state = stateChanges[i]; if (color.Foreground != null) { state.Foreground = color.Foreground.GetBrush(null); } if (color.FontStyle != null) { state.Style = color.FontStyle; } if (color.FontWeight != null) { state.Weight = color.FontWeight; } } }
/// <summary> /// Creates WPF Run instances that can be used for TextBlock.Inlines. /// </summary> public Run[] CreateRuns() { Run[] runs = new Run[stateChanges.Count]; for (int i = 0; i < runs.Length; i++) { int startOffset = stateChangeOffsets[i]; int endOffset = i + 1 < stateChangeOffsets.Count ? stateChangeOffsets[i + 1] : text.Length; Run r = new Run(text.Substring(startOffset, endOffset - startOffset)); HighlightingState state = stateChanges[i]; if (state.Foreground != null) { r.Foreground = state.Foreground; } if (state.Weight != null) { r.FontWeight = state.Weight.Value; } if (state.Family != null) { r.FontFamily = state.Family; } if (state.Style != null) { r.FontStyle = state.Style.Value; } runs[i] = r; } return(runs); }
/// <summary> /// Applies the properties from the HighlightingColor to the specified text segment. /// </summary> public void SetHighlighting(int offset, int length, HighlightingColor color) { if (color == null) { throw new ArgumentNullException("color"); } if (color.Foreground == null && color.FontStyle == null && color.FontWeight == null) { // Optimization: don't split the HighlightingState when we're not changing // any property. For example, the "Punctuation" color in C# is // empty by default. return; } int startIndex = GetIndexForOffset(offset); int endIndex = GetIndexForOffset(offset + length); for (int i = startIndex; i < endIndex; i++) { HighlightingState state = stateChanges[i]; if (color.Foreground != null) { state.Foreground = color.Foreground.GetBrush(null); } if (color.FontStyle != null) { state.Style = color.FontStyle; } if (color.FontWeight != null) { state.Weight = color.FontWeight; } } }
HighlightedInlineBuilder(string text, int[] offsets, HighlightingState[] states) { this.text = text; stateChangeOffsets.AddRange(offsets); stateChanges.AddRange(states); }