private static TextFormattingRunProperties Apply( TextFormattingRunProperties formatting, Classification classification, TextFormattingRunProperties defaultFormatting) { // NOTE: avoid creating a new instance for fields that weren't changed formatting = ApplyTypeFace(formatting, classification, defaultFormatting); if (formatting.Bold != classification.IsBold) { formatting = formatting.SetBold(classification.IsBold); } if (classification.FontRenderingSizeWasReset) { /// NOTE: we should not try to set property of <param name="defaultFormatting" /> if it's marked as empty /// to avoid set which will mark property of <param name="formatting" /> as non empty formatting = defaultFormatting.FontRenderingEmSizeEmpty ? formatting.ClearFontRenderingEmSize() : formatting.SetFontHintingEmSize(defaultFormatting.FontRenderingEmSize); } else if (Math.Abs(formatting.FontRenderingEmSize - classification.FontRenderingSize) > 0.001) { formatting = formatting.SetFontRenderingEmSize(classification.FontRenderingSize); } if (classification.BackgroundWasReset) { /// NOTE: we should not try to set a some of value from <param name="defaultFormatting" /> if it's marked as empty /// to avoid set that will mark value from <param name="formatting" /> as non empty formatting = defaultFormatting.BackgroundBrushEmpty ? formatting.ClearBackgroundBrush() : formatting.SetBackground(defaultFormatting.BackgroundBrush.GetColor()); } else if (!(formatting.BackgroundBrush is SolidColorBrush backgroundBrush) || !backgroundBrush.Color.Equals(classification.Background)) { formatting = formatting.SetBackgroundBrush(new SolidColorBrush(classification.Background)); } if (classification.ForegroundWasReset) { // NOTE: Foreground always is set, just look at // https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.text.classification.iclassificationformatmap.defaulttextproperties?view=visualstudiosdk-2017#remarks formatting = formatting.SetForeground(defaultFormatting.ForegroundBrush.GetColor()); } else if (!(formatting.ForegroundBrush is SolidColorBrush foregroundBrush) || !foregroundBrush.Color.Equals(classification.Foreground)) { formatting = formatting.SetForegroundBrush(new SolidColorBrush(classification.Foreground)); } formatting = ApplyDecoration(formatting, classification.IsOverline, TextDecorations.OverLine[0]); formatting = ApplyDecoration(formatting, classification.IsUnderline, TextDecorations.Underline[0]); formatting = ApplyDecoration(formatting, classification.IsStrikethrough, TextDecorations.Strikethrough[0]); formatting = ApplyDecoration(formatting, classification.IsBaseline, TextDecorations.Baseline[0]); return(formatting); }
private TextFormattingRunProperties CreateTextProperties(CategoryItemDecorationSettings colorSetting) { TextFormattingRunProperties textFormatting = TextFormattingRunProperties.CreateTextFormattingRunProperties(); textFormatting = textFormatting.SetBackground(colorSetting.BackgroundColor); textFormatting = textFormatting.SetForeground(colorSetting.ForegroundColor); textFormatting = textFormatting.SetBold(colorSetting.IsBold); textFormatting = textFormatting.SetItalic(colorSetting.IsItalic); if (colorSetting.HasStrikethrough || colorSetting.IsUnderlined) { TextDecorationCollection decorationsCollection = new TextDecorationCollection(); TextDecorationCollection decorations = textFormatting.TextDecorations.Clone(); if (colorSetting.IsUnderlined) { decorations.Add(TextDecorations.Underline); } if (colorSetting.HasStrikethrough) { decorations.Add(TextDecorations.Strikethrough); } decorationsCollection.Add(decorations); textFormatting = textFormatting.SetTextDecorations(decorationsCollection); } return(textFormatting); }
TextFormattingRunProperties SetProperties(TextFormattingRunProperties format, StyleBase styleOption, double textSize) { var settings = styleOption; var fontSize = textSize + settings.FontSize; if (fontSize < 1) { fontSize = 1; } if (string.IsNullOrWhiteSpace(settings.Font) == false) { format = format.SetTypeface(new Typeface(settings.Font)); } if (settings.FontSize != 0) { if (format.FontRenderingEmSizeEmpty || fontSize != format.FontRenderingEmSize) { format = format.SetFontRenderingEmSize(fontSize); } } if (settings.Bold.HasValue) { if (format.BoldEmpty || settings.Bold != format.Bold) { format = format.SetBold(settings.Bold.Value); } } if (settings.Italic.HasValue) { if (format.ItalicEmpty || settings.Italic != format.Italic) { format = format.SetItalic(settings.Italic.Value); } } if (settings.ForegroundOpacity > 0) { format = format.SetForegroundOpacity(settings.ForegroundOpacity / 255.0); } if (settings.ForeColor.A > 0) { if (format.ForegroundBrushEmpty || (format.ForegroundBrush as SolidColorBrush)?.Color != settings.ForeColor) { format = format.SetForeground(settings.ForeColor); } } if (settings.BackColor.A > 0) { var bc = settings.BackColor.A > 0 ? settings.BackColor : format.BackgroundBrushEmpty == false && format.BackgroundBrush is SolidColorBrush ? (format.BackgroundBrush as SolidColorBrush).Color : Colors.Transparent; if (settings.BackgroundOpacity != 0) { format = format.SetBackgroundOpacity(settings.BackgroundOpacity / 255.0); } if (bc.A > 0) { var bb = format.BackgroundBrush as LinearGradientBrush; switch (settings.BackgroundEffect) { case BrushEffect.Solid: if (format.BackgroundBrushEmpty || (format.BackgroundBrush as SolidColorBrush)?.Color != bc) { format = format.SetBackground(bc); } break; case BrushEffect.ToBottom: if (bb == null || bb.StartPoint.Y > bb.EndPoint.Y || bb.GradientStops.Count != 2 || bb.GradientStops[0].Color != _BackColor || bb.GradientStops[1].Color != bc) { format = format.SetBackgroundBrush(new LinearGradientBrush(_BackColor, bc, 90)); } break; case BrushEffect.ToTop: if (bb == null || bb.StartPoint.Y < bb.EndPoint.Y || bb.GradientStops.Count != 2 || bb.GradientStops[0].Color != bc || bb.GradientStops[1].Color != _BackColor) { format = format.SetBackgroundBrush(new LinearGradientBrush(bc, _BackColor, 90)); } bb = new LinearGradientBrush(bc, _BackColor, 90); break; case BrushEffect.ToRight: if (bb == null || bb.StartPoint.X >= bb.EndPoint.X || bb.GradientStops.Count != 2 || bb.GradientStops[0].Color != _BackColor || bb.GradientStops[1].Color != bc) { format = format.SetBackgroundBrush(new LinearGradientBrush(_BackColor, bc, 0)); } break; case BrushEffect.ToLeft: if (bb == null || bb.StartPoint.X >= bb.EndPoint.X || bb.GradientStops.Count != 2 || bb.GradientStops[0].Color != bc || bb.GradientStops[1].Color != _BackColor) { format = format.SetBackgroundBrush(new LinearGradientBrush(bc, _BackColor, 0)); } break; default: throw new NotImplementedException("Background effect not supported: " + settings.BackgroundEffect.ToString()); } } } else if (settings.BackColor.A > 0) { if (format.BackgroundBrushEmpty || (format.BackgroundBrush as SolidColorBrush)?.Color != settings.BackColor) { format = format.SetBackground(settings.BackColor); } } if (settings.Underline.HasValue || settings.Strikethrough.HasValue || settings.OverLine.HasValue) { var tdc = new TextDecorationCollection(); if (settings.Underline.GetValueOrDefault()) { tdc.Add(TextDecorations.Underline); } if (settings.Strikethrough.GetValueOrDefault()) { tdc.Add(TextDecorations.Strikethrough); } if (settings.OverLine.GetValueOrDefault()) { tdc.Add(TextDecorations.OverLine); } format = format.SetTextDecorations(tdc); } return(format); }
TextFormattingRunProperties SetProperties(TextFormattingRunProperties properties, StyleBase styleOption, double textSize) { var settings = styleOption; var fontSize = textSize + settings.FontSize; if (fontSize < 1) { fontSize = 1; } if (string.IsNullOrWhiteSpace(settings.Font) == false) { properties = properties.SetTypeface(new Typeface(settings.Font)); } if (settings.FontSize != 0) { properties = properties.SetFontRenderingEmSize(fontSize); } if (settings.Bold.HasValue) { properties = properties.SetBold(settings.Bold.Value); } if (settings.Italic.HasValue) { properties = properties.SetItalic(settings.Italic.Value); } if (settings.ForeColor.A > 0) { if (settings.ForeColor.A == 255 && properties.ForegroundOpacityEmpty) { properties = properties.SetForeground(settings.ForeColor.Alpha(255)); } else { properties = properties.SetForegroundOpacity(settings.ForeColor.A / 255.0) .SetForeground(settings.ForeColor); } } var bc = settings.BackColor.A > 0 ? settings.BackColor : properties.BackgroundBrushEmpty == false && properties.BackgroundBrush is SolidColorBrush ? (properties.BackgroundBrush as SolidColorBrush).Color : Colors.Transparent; if (bc.A > 0) { if (settings.BackColor.A < 255 || properties.BackgroundOpacityEmpty == false) { properties = properties.SetBackgroundOpacity(bc.A / 255.0); } switch (settings.BackgroundEffect) { case BrushEffect.Solid: properties = properties.SetBackground(bc); break; case BrushEffect.ToBottom: properties = properties.SetBackgroundBrush(new LinearGradientBrush(_BackColor, bc, 90)); break; case BrushEffect.ToTop: properties = properties.SetBackgroundBrush(new LinearGradientBrush(bc, _BackColor, 90)); break; case BrushEffect.ToRight: properties = properties.SetBackgroundBrush(new LinearGradientBrush(_BackColor, bc, 0)); break; case BrushEffect.ToLeft: properties = properties.SetBackgroundBrush(new LinearGradientBrush(bc, _BackColor, 0)); break; default: break; } } if (settings.Underline.HasValue || settings.Strikethrough.HasValue || settings.OverLine.HasValue) { var tdc = new TextDecorationCollection(); if (settings.Underline.GetValueOrDefault()) { tdc.Add(TextDecorations.Underline); } if (settings.Strikethrough.GetValueOrDefault()) { tdc.Add(TextDecorations.Strikethrough); } if (settings.OverLine.GetValueOrDefault()) { tdc.Add(TextDecorations.OverLine); } properties = properties.SetTextDecorations(tdc); } return(properties); }