Beispiel #1
0
        internal static FontStyle GetFontStyle(this SyntaxHighlight.StyleBase activeStyle)
        {
            var f = FontStyle.Regular;

            if (activeStyle.Bold == true)
            {
                f |= FontStyle.Bold;
            }
            if (activeStyle.Italic == true)
            {
                f |= FontStyle.Italic;
            }
            if (activeStyle.Underline == true)
            {
                f |= FontStyle.Underline;
            }
            if (activeStyle.Strikethrough == true)
            {
                f |= FontStyle.Strikeout;
            }
            return(f);
        }
Beispiel #2
0
        internal static void MixStyle(SyntaxHighlight.StyleBase style, out FontStyle fontStyle, out GdiColor forecolor, out GdiColor backcolor)
        {
            forecolor = style.ForegroundOpacity > 0 ? ThemeHelper.DocumentTextColor.Alpha(style.ForegroundOpacity) : ThemeHelper.DocumentTextColor;
            backcolor = style.BackgroundOpacity > 0 ? ThemeHelper.DocumentPageColor.Alpha(style.BackgroundOpacity) : ThemeHelper.DocumentPageColor;
            fontStyle = style.GetFontStyle();
            if (style.ClassificationType == null)
            {
                return;
            }
            var p = TextEditorHelper.DefaultClassificationFormatMap.GetRunProperties(style.ClassificationType);

            if (p == null)
            {
                return;
            }
            SolidColorBrush colorBrush;

            if (style.ForeColor.A == 0)
            {
                colorBrush = p.ForegroundBrushEmpty ? null : p.ForegroundBrush as SolidColorBrush;
                if (colorBrush != null)
                {
                    forecolor = (style.ForegroundOpacity > 0 ? colorBrush.Color.Alpha(style.ForegroundOpacity) : colorBrush.Color).ToGdiColor();
                }
            }
            else
            {
                forecolor = style.AlphaForeColor.ToGdiColor();
            }
            if (style.BackColor.A == 0)
            {
                colorBrush = p.BackgroundBrushEmpty ? null : p.BackgroundBrush as SolidColorBrush;
                if (colorBrush != null)
                {
                    backcolor = (style.BackgroundOpacity > 0 ? colorBrush.Color.Alpha(style.BackgroundOpacity) : colorBrush.Color).ToGdiColor();
                }
            }
            else
            {
                backcolor = style.AlphaBackColor.ToGdiColor();
            }
            if (p.BoldEmpty == false && p.Bold && style.Bold != false)
            {
                fontStyle |= FontStyle.Bold;
            }
            if (p.ItalicEmpty == false && p.Italic && style.Italic != false)
            {
                fontStyle |= FontStyle.Italic;
            }
            if (p.TextDecorationsEmpty == false)
            {
                foreach (var decoration in p.TextDecorations)
                {
                    if (decoration.Location == System.Windows.TextDecorationLocation.Underline && style.Underline != false)
                    {
                        fontStyle |= FontStyle.Underline;
                    }
                    else if (decoration.Location == System.Windows.TextDecorationLocation.Strikethrough && style.Strikethrough != false)
                    {
                        fontStyle |= FontStyle.Strikeout;
                    }
                }
            }
        }
Beispiel #3
0
        TextFormattingRunProperties UpdateFormattingMap(StyleBase style, TextFormattingRunProperties textFormatting, double defaultSize)
        {
            var p = SetProperties(textFormatting, style, defaultSize);

            return(textFormatting != p ? p : null);
        }
Beispiel #4
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);
        }
Beispiel #5
0
        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);
        }