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);
        }
Exemple #2
0
 /// <summary>
 /// Try to add or remove <paramref name="decoration"/> to the <paramref name="formatting"/> using <paramref name="needToAddDecoration"/>
 /// </summary>
 /// <param name="formatting"></param>
 /// <param name="needToAddDecoration">Determines when the input <paramref name="decoration"/> should be added or removed</param>
 private static TextFormattingRunProperties ApplyDecoration(
     TextFormattingRunProperties formatting, bool needToAddDecoration, TextDecoration decoration)
 {
     if (formatting.TextDecorations.Contains(decoration) ^ needToAddDecoration)
     {
         // NOTE: directly creates a new instance from existing collection to correctly determines
         // in the future that items are contained or not
         var clone = new TextDecorationCollection(formatting.TextDecorations);
         if (needToAddDecoration)
         {
             clone.Add(decoration);
         }
         else
         {
             clone.Remove(decoration);
         }
         return(formatting.SetTextDecorations(clone));
     }
     return(formatting);
 }
Exemple #3
0
        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);
        }