Ejemplo n.º 1
0
        /// <summary>
        /// Apply <see cref="Typeface"/> that build from <paramref name="classification"/> using
        /// <paramref name="defaultFormatting"/> as fallback values
        /// </summary>
        private static TextFormattingRunProperties ApplyTypeFace(
            TextFormattingRunProperties formatting, Classification classification, TextFormattingRunProperties defaultFormatting)
        {
            TextFormattingRunProperties ApplyTypeFace(TypeFaces mask, Typeface fallbackFace)
            {
                if (mask.Is(TypeFaces.Style) && !formatting.ItalicEmpty)
                {
                    formatting = formatting.ClearItalic();
                }

                return(formatting.SetTypeface(new Typeface(
                                                  mask.Is(TypeFaces.Family) ? FontFamilyService.SupportedFamilies[classification.FontFamily] : fallbackFace.FontFamily,
                                                  mask.Is(TypeFaces.Style) ? FontStyleService.SupportedStyleByNames[classification.FontStyle] : fallbackFace.Style,
                                                  fallbackFace.Weight,
                                                  mask.Is(TypeFaces.Stretch) ? FontStretchService.SupportedStretches[classification.FontStretch] : fallbackFace.Stretch)));
            }

            if (formatting.TypefaceEmpty)
            {
                var faces = TypeFaces.Family | TypeFaces.Stretch;
                switch (classification.FontStyle)
                {
                case FontStyleService.Italic when !formatting.Italic:
                    formatting = formatting.SetItalic(true);
                    break;

                case FontStyleService.Normal when formatting.Italic:
                    formatting = formatting.SetItalic(false);
                    break;

                default:
                    faces |= TypeFaces.Style;
                    break;
                }
                formatting = ApplyTypeFace(faces, defaultFormatting.Typeface);
            }
            else
            {
                var typeFace = formatting.Typeface;
                if (!typeFace.Style.Equals(FontStyleService.SupportedStyleByNames[classification.FontStyle]) ||
                    !typeFace.FontFamily.Source.Equals(classification.FontFamily) ||
                    typeFace.Stretch.ToOpenTypeStretch() != classification.FontStretch)
                {
                    formatting = ApplyTypeFace(TypeFaces.All, typeFace);
                }
            }
            return(formatting);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }