Exemplo n.º 1
0
        public static void ConfigureStyle(this TextElement textElement, FigmaTypeStyle style)
        {
            string family = style.fontFamily;

            if (family == "SF UI Text")
            {
                family = ".SF NS Text";
            }
            else if (family == "SF Mono")
            {
                family = ".SF NS Display";
            }
            else
            {
                Console.WriteLine("FONT: {0} - {1}", family, style.fontPostScriptName);
            }
            textElement.FontFamily = new FontFamily(family);

            if (style.fontSize > 0)
            {
                textElement.FontSize = style.fontSize;// -3 ;
            }
            textElement.FontWeight = FontWeight.FromOpenTypeWeight(style.fontWeight);
            if (style.letterSpacing != default)
            {
                textElement.FontStretch = FontStretch.FromOpenTypeStretch(style.letterSpacing > 9 ? 9 : (int)style.letterSpacing);
            }

            var fill = style.fills.FirstOrDefault();

            if (fill != null)
            {
                textElement.Foreground = fill.color.ToColor();
            }
        }
Exemplo n.º 2
0
        internal static Table FromTableInfo(TableInfo info)
        {
            var control = new Table();

            Canvas.SetLeft(control, info.Location().X);
            Canvas.SetTop(control, info.Location().Y);
            control.TableFontFamily  = new FontFamily(info.FontFamily);
            control.TableFontSize    = Helper.ToEmSize(info.FontSize, MainWindow.DpiY);;
            control.TableFontStyle   = info.FontStyle == FontStyles.Italic.ToString() ? FontStyles.Italic : FontStyles.Normal;
            control.TableFontWeight  = FontWeight.FromOpenTypeWeight(info.FontOpenTypeWeight);
            control.TableFontStretch = FontStretch.FromOpenTypeStretch(info.FontOpenTypeStretch);
            control.TextFontFamily   = new FontFamily(info.TextFontFamily);
            control.TextFontSize     = Helper.ToEmSize(info.TextFontSize, MainWindow.DpiY);;
            control.TextFontStyle    = info.TextFontStyle == FontStyles.Italic.ToString() ? FontStyles.Italic : FontStyles.Normal;
            control.TextFontWeight   = FontWeight.FromOpenTypeWeight(info.TextFontOpenTypeWeight);
            control.TextFontStretch  = FontStretch.FromOpenTypeStretch(info.TextFontOpenTypeStretch);
            control.XlsColumn        = info.XlsColumn;

            control.Initialize(
                info.RowCount,
                info.RowHeight,
                new GridLength(info.Col0Width, GridUnitType.Pixel),
                new GridLength(info.Col1Width, GridUnitType.Pixel),
                new GridLength(info.Col2Width, GridUnitType.Pixel));

            return(control);
        }
Exemplo n.º 3
0
        /// <inheritdoc />
        public DispatcherContainer()
        {
            var thread = new Thread(() =>
            {
                _visualTarget = new VisualTarget(_hostVisual);
                DrawingVisual drawingVisual = new DrawingVisual();
                var drawing = drawingVisual.RenderOpen();
                using (drawing)
                {
                    var text = new FormattedText("欢迎访问我博客 http://lindexi.gitee.io 里面有大量 UWP WPF 博客",
                                                 CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
                                                 new Typeface(new FontFamily("微软雅黑"), new FontStyle(), FontWeight.FromOpenTypeWeight(1),
                                                              FontStretch.FromOpenTypeStretch(1)), 20, Brushes.DarkSlateBlue);

                    drawing.DrawText(text, new Point(100, 100));
                }

                var containerVisual = new ContainerVisual();

                containerVisual.Children.Add(drawingVisual);

                _visualTarget.RootVisual = containerVisual;

                System.Windows.Threading.Dispatcher.Run();
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }
Exemplo n.º 4
0
        private int FillTable(DrawingContext ctx, TableInfo ci, TableDataSet dataSet, int seek = 0, Point mediaOffset = new Point())
        {
            int i = 0;

            for (i = 0; i < ci.RowCount; i++)
            {
                if (!dataSet.IsInRange(i + seek))
                {
                    break;
                }

                var text = new FormattedText(dataSet[i + seek, 0], System.Globalization.CultureInfo.CurrentCulture,
                                             FlowDirection.LeftToRight,
                                             new Typeface(
                                                 new FontFamily(ci.FontFamily),
                                                 ci.FontStyle == FontStyles.Italic.ToString() ? FontStyles.Italic : FontStyles.Normal,
                                                 FontWeight.FromOpenTypeWeight(OverrideFontWeight == 0 ? ci.FontOpenTypeWeight : OverrideFontWeight),
                                                 FontStretch.FromOpenTypeStretch(ci.FontOpenTypeStretch)),
                                             Helper.ToEmSize(ci.FontSize, m_DpiY), Brushes.Black);

                var orig = ci.Location();
                var loc  = new Point(orig.X, orig.Y + ci.RowPixelHeight * i);
                loc.X += mediaOffset.X;
                loc.Y += mediaOffset.Y;

                ctx.DrawText(text, loc);
            }

            // Fill 2 column
            for (i = 0; i < ci.RowCount; i++)
            {
                if (!dataSet.IsInRange(i + seek))
                {
                    break;
                }

                var text = new FormattedText(dataSet[i + seek, 1], System.Globalization.CultureInfo.CurrentCulture,
                                             FlowDirection.LeftToRight,
                                             new Typeface(
                                                 new FontFamily(ci.TextFontFamily),
                                                 ci.TextFontStyle == FontStyles.Italic.ToString() ? FontStyles.Italic : FontStyles.Normal,
                                                 FontWeight.FromOpenTypeWeight(OverrideFontWeight == 0 ? ci.TextFontOpenTypeWeight : OverrideFontWeight),
                                                 FontStretch.FromOpenTypeStretch(ci.TextFontOpenTypeStretch)),
                                             Helper.ToEmSize(ci.TextFontSize, m_DpiY), Brushes.Black);

                var orig = ci.Location();
                var loc  = new Point(orig.X + ci.Col0Width + ci.Col1Width, orig.Y + ci.RowPixelHeight * i);
                loc.X += mediaOffset.X;
                loc.Y += mediaOffset.Y;

                ctx.DrawText(text, loc);
            }

            return(i + seek);
        }
Exemplo n.º 5
0
 private void ChangeLabel(Label CurLabel, double left, double top,
                          double size, string FontName, bool Bold, Color TextColor,
                          int stretch = 5)
 {
     CurLabel.Margin      = new Thickness(left, top, 0, 0);
     CurLabel.FontSize    = size;
     CurLabel.FontFamily  = new FontFamily(FontName);
     CurLabel.FontWeight  = Bold ? FontWeights.Bold : FontWeights.Regular;
     CurLabel.Foreground  = new SolidColorBrush(TextColor);
     CurLabel.FontStretch = FontStretch.FromOpenTypeStretch(stretch);
 }
Exemplo n.º 6
0
        public Search(IsbDev sourceDev)
        {
            IsbDev = sourceDev;
            BuildSearchNodes(true);

            SearchCriteriaTextEditor = new ICSharpCode.AvalonEdit.TextEditor
            {
                Options =
                {
                    ShowEndOfLine = true,
                    ShowSpaces    = true,
                    ShowTabs      = true
                }
            };

            SearchCriteriaTextEditor.TextChanged    += SearchCriteriaChanged;
            SearchCriteriaTextEditor.TextArea.KeyUp += new System.Windows.Input.KeyEventHandler(TextArea_KeyUp);
            SearchCriteriaTextEditor.TextArea.TextView.LineTransformers.Add(new HighlightIncorrectRegExp(this));
            SearchCriteriaTextEditor.FontFamily  = new FontFamily("Courier New, Courier, monospace");
            SearchCriteriaTextEditor.FontSize    = 13;
            SearchCriteriaTextEditor.FontStretch = FontStretch.FromOpenTypeStretch(5);

            TextEditor = new ICSharpCode.AvalonEdit.TextEditor
            {
                Options =
                {
                    ConvertTabsToSpaces        = true,
                    ShowEndOfLine              = true,
                    ShowSpaces                 = true,
                    ShowTabs                   = true,
                    AllowScrollBelowDocument   = false,
                    EnableRectangularSelection = true,
                },
                ShowLineNumbers = true,
                Text            = "",
                IsReadOnly      = false
            };
            TextEditor.TextArea.TextView.LineTransformers.Add(new HighlightSearchedStrings(this));
            TextEditor.FontFamily      = new FontFamily("Courier New, Courier, monospace");
            TextEditor.FontSize        = 13;
            TextEditor.FontStretch     = FontStretch.FromOpenTypeStretch(1);
            TextEditor.TextArea.KeyUp += new System.Windows.Input.KeyEventHandler(TextArea_KeyUp);

            var syntaxHighlightingFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ISBL.xshd");

            using (Stream s = File.OpenRead(syntaxHighlightingFilePath))
            {
                using (XmlTextReader reader = new XmlTextReader(s))
                {
                    TextEditor.SyntaxHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance);
                }
            }
        }
Exemplo n.º 7
0
        public static FontDescriptor Deserialize(XmlElement node)
        {
            if (node.FindBoolean("IsDefault") == true)
            {
                return(Default);
            }

            string fontFamily  = node.FindString("Family");
            int?   fontWight   = node.FindInt32("Weight");
            int?   fontStyle   = node.FindInt32("Style");
            int?   fontStretch = node.FindInt32("Stretch");
            double?fontSize    = node.FindDouble("Size");

            TextDecorationCollection textDecorations = new TextDecorationCollection();
            XmlElement textDecorationsNode           = node["TextDecorations"];

            if (textDecorationsNode != null)
            {
                foreach (int?location in textDecorationsNode.OfType <XmlElement>().Select(decorationNode => decorationNode.FindInt32("Location")).Where(location => location != null))
                {
                    switch ((TextDecorationLocation)location.Value)
                    {
                    case TextDecorationLocation.Baseline:
                        textDecorations.Add(System.Windows.TextDecorations.Baseline[0]);
                        break;

                    case TextDecorationLocation.OverLine:
                        textDecorations.Add(System.Windows.TextDecorations.OverLine[0]);
                        break;

                    case TextDecorationLocation.Strikethrough:
                        textDecorations.Add(System.Windows.TextDecorations.Strikethrough[0]);
                        break;

                    case TextDecorationLocation.Underline:
                        textDecorations.Add(System.Windows.TextDecorations.Underline[0]);
                        break;
                    }
                }
            }

            return(new FontDescriptor
            {
                FontFamily = string.IsNullOrEmpty(fontFamily) ? new FontFamily() : new FontFamily(fontFamily),
                FontWeight = fontWight == null ? new FontWeight() : FontWeight.FromOpenTypeWeight(fontWight.Value),
                FontStyle = fontStyle == null ? new FontStyle() : (FontStyle)(FontStyleConverter)fontStyle.Value,
                FontStretch = fontStretch == null ? new FontStretch() : FontStretch.FromOpenTypeStretch(fontStretch.Value),
                FontSize = fontSize ?? 12,
                TextDecorations = textDecorations
            });
        }
        public static TextSpan FromCaptionInfo(CaptionInfo info)
        {
            var control = new TextSpan();

            Canvas.SetTop(control, info.Location().Y);
            Canvas.SetLeft(control, info.Location().X);
            control.SpanContent.Text = info.CaptionText;
            control.FontFamily       = new FontFamily(info.FontFamily);
            control.FontSize         = Helper.ToEmSize(info.FontSize, MainWindow.DpiY);
            control.FontStyle        = info.FontStyle == FontStyles.Italic.ToString() ? FontStyles.Italic : FontStyles.Normal;
            control.FontWeight       = FontWeight.FromOpenTypeWeight(info.FontOpenTypeWeight);
            control.FontStretch      = FontStretch.FromOpenTypeStretch(info.FontOpenTypeStretch);
            control.SpanWidth        = info.PixelWidth;
            control.XlsColumn        = info.XlsColumn;

            return(control);
        }
Exemplo n.º 9
0
        public static object ToFontStretch(object value)
        {
            if (value is FontStretch)
            {
                return(value);
            }

#if !(SILVERLIGHT || NETFX_CORE)
            if (value is int)
            {
                return(FontStretch.FromOpenTypeStretch((int)value));
            }
#endif

            var s = value as string;
            if (s != null)
            {
                switch (s.ToLower())
                {
                case "normal": return(FStretches.Normal);

#if !(SILVERLIGHT || NETFX_CORE)
                case "medium": return(FStretches.Medium);
#endif
                case "expanded": return(FStretches.Expanded);

                case "condensed": return(FStretches.Condensed);

                case "semiexpanded": return(FStretches.SemiExpanded);

                case "semicondensed": return(FStretches.SemiCondensed);

                case "extraexpanded": return(FStretches.ExtraExpanded);

                case "ultraexpanded": return(FStretches.UltraExpanded);

                case "ultracondensed": return(FStretches.UltraCondensed);

                case "extracondensed": return(FStretches.ExtraCondensed);
                }
            }

            throw new InvalidCastException();
        }
Exemplo n.º 10
0
        private void DrawFormattedText(DrawingContext ctx, string caption, IDrawingFormatable info, Point mediaOffset)
        {
            var text = new FormattedText(caption, System.Globalization.CultureInfo.CurrentCulture,
                                         FlowDirection.LeftToRight,
                                         new Typeface(
                                             new FontFamily(info.FontFamily),
                                             info.FontStyle == FontStyles.Italic.ToString() ? FontStyles.Italic : FontStyles.Normal,
                                             FontWeight.FromOpenTypeWeight(OverrideFontWeight == 0 ? info.FontOpenTypeWeight : OverrideFontWeight),
                                             FontStretch.FromOpenTypeStretch(info.FontOpenTypeStretch)),
                                         Helper.ToEmSize(info.FontSize, m_DpiY), Brushes.Black);

            text.TextAlignment = TextAlignment.Center;
            text.MaxTextWidth  = info.PixelWidth;
            text.MaxTextHeight = 24;

            var loc = info.Location();

            //var centerloc = new Point(loc.X + info.Width / 2, loc.Y + text.Height / 2);
            //Point textLocation = new Point(centerloc.X - text.WidthIncludingTrailingWhitespace / 2, centerloc.Y);
            loc.X += mediaOffset.X;
            loc.Y += mediaOffset.Y;

            ctx.DrawText(text, loc);
        }
Exemplo n.º 11
0
        /// <summary>
        /// 构建文本形状
        /// </summary>
        /// <param name="text">文本内容</param>
        /// <param name="x">横坐标</param>
        /// <param name="y">纵坐标</param>
        /// <param name="totalwidth">总宽度</param>
        /// <returns>形状</returns>
        private Geometry BuildGlyphRun(string text, double x, double y, ref double totalwidth)
        {
            double baseline = y;

            SVGBaseLineShift  baseline_shift  = this.GetAttributeValue <SVGBaseLineShift>("baseline-shift", true, null);
            double            font_size       = this.GetAttributeValue <SVGDouble>("font-size", true, new SVGDouble(12)).GetValue(12);
            SVGFontFamily     font_family     = this.GetAttributeValue <SVGFontFamily>("font-family", true, SVGFontFamily.Default);
            SVGFontWeight     font_weight     = this.GetAttributeValue <SVGFontWeight>("font-weight", true, SVGFontWeight.Default);
            SVGFontStyle      font_style      = this.GetAttributeValue <SVGFontStyle>("font-style", true, SVGFontStyle.Default);
            SVGTextAnchor     text_anchor     = this.GetAttributeValue <SVGTextAnchor>("text-anchor", true, SVGTextAnchor.Default);
            SVGTextDecoration text_decoration = this.GetAttributeValue <SVGTextDecoration>("text-decoration", true, null);
            double            letter_spacing  = this.GetAttributeValue <SVGDouble>("letter-spacing", true, SVGDouble.Zero).GetValue(12);
            double            word_spacing    = this.GetAttributeValue <SVGDouble>("word-spacing", true, SVGDouble.Zero).GetValue(12);

            GlyphRun glyphs = null;

            Typeface      type_face = new Typeface(font_family.Value, font_style.Value, font_weight.Value, FontStretch.FromOpenTypeStretch(9), SVGFontFamily.Default.Value);
            GlyphTypeface glyph_type_face;

            if (!type_face.TryGetGlyphTypeface(out glyph_type_face))
            {
                return(null);
            }

            glyphs = new GlyphRun();

            ((System.ComponentModel.ISupportInitialize)glyphs).BeginInit();

            glyphs.GlyphTypeface       = glyph_type_face;
            glyphs.FontRenderingEmSize = font_size;
            List <char>   text_chars     = new List <char>();
            List <ushort> glyph_indices  = new List <ushort>();
            List <double> advance_widths = new List <double>();

            totalwidth = 0;
            for (int i = 0; i < text.Length; ++i)
            {
                char   c          = text[i];
                int    code_point = c;
                ushort glyph_index;
                if (!glyph_type_face.CharacterToGlyphMap.TryGetValue(code_point, out glyph_index))
                {
                    continue;
                }

                text_chars.Add(c);
                double glyph_width = glyph_type_face.AdvanceWidths[glyph_index];
                glyph_indices.Add(glyph_index);
                advance_widths.Add(glyph_width * font_size + letter_spacing);
                if (char.IsWhiteSpace(c))
                {
                    advance_widths[advance_widths.Count - 1] += word_spacing;
                }
                totalwidth += advance_widths[advance_widths.Count - 1];
            }
            glyphs.Characters    = text_chars.ToArray();
            glyphs.GlyphIndices  = glyph_indices.ToArray();
            glyphs.AdvanceWidths = advance_widths.ToArray();

            double alignmentoffset = 0;

            if (text_anchor.Value == TextAlignment.Center)
            {
                alignmentoffset = totalwidth / 2;
            }
            else if (text_anchor.Value == TextAlignment.Right)
            {
                alignmentoffset = totalwidth;
            }

            glyphs.BaselineOrigin = new Point(x - alignmentoffset, baseline);

            ((System.ComponentModel.ISupportInitialize)glyphs).EndInit();

            GeometryGroup result = new GeometryGroup();

            result.Children.Add(glyphs.BuildGeometry());

            if (text_decoration != null)
            {
                double decoration_pos      = 0;
                double decoration_thinkess = 0;

                if (text_decoration.Value == TextDecorationLocation.Strikethrough)
                {
                    decoration_pos      = baseline - (type_face.StrikethroughPosition * font_size);
                    decoration_thinkess = type_face.StrikethroughThickness * font_size;
                }
                else if (text_decoration.Value == TextDecorationLocation.Underline)
                {
                    decoration_pos      = baseline - (type_face.UnderlinePosition * font_size);
                    decoration_thinkess = type_face.UnderlineThickness * font_size;
                }
                else if (text_decoration.Value == TextDecorationLocation.OverLine)
                {
                    decoration_pos      = baseline - font_size;
                    decoration_thinkess = type_face.StrikethroughThickness * font_size;
                }

                System.Windows.Rect bounds = new System.Windows.Rect(result.Bounds.Left, decoration_pos, result.Bounds.Width, decoration_thinkess);

                result.Children.Add(new RectangleGeometry(bounds));
            }

            return(result);
        }
Exemplo n.º 12
0
        static Geometry BuildGlyphRun(TextStyle textStyle, string text, double x, double y, ref double totalwidth)
        {
            double   fontSize = textStyle.FontSize;
            GlyphRun glyphs   = null;
            Typeface font     = new Typeface(new FontFamily(textStyle.FontFamily),
                                             textStyle.Fontstyle,
                                             textStyle.Fontweight,
                                             FontStretch.FromOpenTypeStretch(9),
                                             new FontFamily("MetricHPE Unicode MS"));
            GlyphTypeface glyphFace;
            double        baseline = y;

            if (font.TryGetGlyphTypeface(out glyphFace))
            {
                glyphs = new GlyphRun();
                ((System.ComponentModel.ISupportInitialize)glyphs).BeginInit();
                glyphs.GlyphTypeface       = glyphFace;
                glyphs.FontRenderingEmSize = fontSize;
                List <char>   textChars     = new List <char>();
                List <ushort> glyphIndices  = new List <ushort>();
                List <double> advanceWidths = new List <double>();
                totalwidth = 0;
                char[] charsToSkip = new char[] { '\t', '\r', '\n' };
                for (int i = 0; i < text.Length; ++i)
                {
                    char textchar  = text[i];
                    int  codepoint = textchar;
                    //if (charsToSkip.Any<char>(item => item == codepoint))
                    //	continue;
                    ushort glyphIndex;
                    if (glyphFace.CharacterToGlyphMap.TryGetValue(codepoint, out glyphIndex) == false)
                    {
                        continue;
                    }
                    textChars.Add(textchar);
                    double glyphWidth = glyphFace.AdvanceWidths[glyphIndex];
                    glyphIndices.Add(glyphIndex);
                    advanceWidths.Add(glyphWidth * fontSize + textStyle.LetterSpacing);
                    if (char.IsWhiteSpace(textchar))
                    {
                        advanceWidths[advanceWidths.Count - 1] += textStyle.WordSpacing;
                    }
                    totalwidth += advanceWidths[advanceWidths.Count - 1];
                }
                glyphs.Characters    = textChars.ToArray();
                glyphs.GlyphIndices  = glyphIndices.ToArray();
                glyphs.AdvanceWidths = advanceWidths.ToArray();

                // calculate text alignment
                double alignmentoffset = 0;
                if (textStyle.TextAlignment == TextAlignment.Center)
                {
                    alignmentoffset = totalwidth / 2;
                }
                if (textStyle.TextAlignment == TextAlignment.Right)
                {
                    alignmentoffset = totalwidth;
                }

                baseline = y;
                glyphs.BaselineOrigin = new Point(x - alignmentoffset, baseline);
                ((System.ComponentModel.ISupportInitialize)glyphs).EndInit();
            }
            else
            {
                return(new GeometryGroup());
            }

            // add text decoration to geometry
            GeometryGroup gp = new GeometryGroup();

            gp.Children.Add(glyphs.BuildGeometry());
            if (textStyle.TextDecoration != null)
            {
                double decorationPos       = 0;
                double decorationThickness = 0;
                if (textStyle.TextDecoration[0].Location == TextDecorationLocation.Strikethrough)
                {
                    decorationPos       = baseline - (font.StrikethroughPosition * fontSize);
                    decorationThickness = font.StrikethroughThickness * fontSize;
                }
                if (textStyle.TextDecoration[0].Location == TextDecorationLocation.Underline)
                {
                    decorationPos       = baseline - (font.UnderlinePosition * fontSize);
                    decorationThickness = font.UnderlineThickness * fontSize;
                }
                if (textStyle.TextDecoration[0].Location == TextDecorationLocation.OverLine)
                {
                    decorationPos       = baseline - fontSize;
                    decorationThickness = font.StrikethroughThickness * fontSize;
                }
                Rect bounds = new Rect(gp.Bounds.Left, decorationPos, gp.Bounds.Width, decorationThickness);
                gp.Children.Add(new RectangleGeometry(bounds));
            }
            return(gp);
        }
Exemplo n.º 13
0
 public virtual FontStretch GetFontStretch()
 => FontStretch.FromOpenTypeStretch(5);
Exemplo n.º 14
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 || settings.Stretch.HasValue)
            {
                format = format.SetTypeface(new Typeface(string.IsNullOrWhiteSpace(settings.Font) == false ? new FontFamily(settings.Font) : _DefaultFontFamily, FontStyles.Normal, FontWeights.Normal, settings.Stretch.HasValue ? FontStretch.FromOpenTypeStretch(settings.Stretch.Value) : FontStretches.Normal));
            }
            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 b ? b.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);
        }
        private void AddSpanElement(string text, Point location, Point mediaOffset)
        {
            var tf      = new Typeface(new FontFamily("Times New Roman"), FontStyles.Normal, FontWeight.FromOpenTypeWeight(400), FontStretch.FromOpenTypeStretch(5));
            var info    = new CaptionInfo(location.X, location.Y, text, 12d, tf);
            var element = TextSpan.FromCaptionInfo(info);

            AddCanvasElement(element, mediaOffset);
        }