Esempio n. 1
0
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] CodeBlock obj)
        {
            var paragraph = new Paragraph()
            {
                Tag = obj
            };

            renderer.Styles.ApplyCodeBlockStyle(paragraph);
            renderer.Push(paragraph);

            if (obj.Inline != null)
            {
                // there was a post-processor which has already processed the lines in this code block
                renderer.WriteChildren(obj.Inline);
            }
            else // there was no post-processor - we have to do the writing of the code lines
            {
                // original code: renderer.WriteLeafRawLines(obj); // Expand this call directly here in order to be able to include tags
                var lines = obj.Lines;
                if (lines.Lines != null)
                {
                    var slices = lines.Lines;
                    for (var i = 0; i < lines.Count; i++)
                    {
                        renderer.WriteInline(new Run(slices[i].Slice.ToString()));
                        renderer.WriteInline(new LineBreak());
                    }
                }
            }

            renderer.Pop();
        }
 /// <inheritdoc/>
 protected override void Write([NotNull] WpfRenderer renderer, [NotNull] LineBreakInline obj)
 {
     if (obj.IsHard)
     {
         renderer.WriteInline(new LineBreak());
     }
 }
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] CodeInline obj)
        {
            var run = new Run(obj.Content);

            run.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.CodeStyleKey);
            renderer.WriteInline(run);
        }
        /// <inheritdoc/>
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] LinkInline link)
        {
            var url = link.GetDynamicUrl != null?link.GetDynamicUrl() ?? link.Url : link.Url;

            if (!Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
            {
                url = "#";
            }

            if (link.IsImage)
            {
                var image = new Image
                {
                    Source = new BitmapImage(new Uri(url))
                };

                image.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.ImageStyleKey);

                renderer.WriteInline(new InlineUIContainer(image));
            }
            else
            {
                var hyperlink = new Hyperlink
                {
                    Command          = Commands.Hyperlink,
                    CommandParameter = url,
                    NavigateUri      = new Uri(url, UriKind.RelativeOrAbsolute),
                    ToolTip          = link.Title != string.Empty ? link.Title : null,
                };

                renderer.Push(hyperlink);
                renderer.WriteChildren(link);
                renderer.Pop();
            }
        }
Esempio n. 5
0
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] CodeInline obj)
        {
            var span = new Span()
            {
                Tag = obj
            };

            renderer.Styles.ApplyCodeStyle(span);

            var run = new Run("\x202F"); // Narrow fixed space

            run.FontStretch = FontStretches.UltraCondensed;
            span.Inlines.Add(run);

            run = new Run(obj.Content.Replace(" ", "\xA0"))
            {
                Tag = obj
            };                                                             // Replace Space with fixed space
            span.Inlines.Add(run);

            run             = new Run("\x202F");            // Narrow fixed space
            run.FontStretch = FontStretches.UltraCondensed; // has an effect only for non fixed space fonts
            span.Inlines.Add(run);

            renderer.WriteInline(span);
        }
Esempio n. 6
0
 /// <inheritdoc/>
 protected override void Write([NotNull] WpfRenderer renderer, [NotNull] LineBreakInline obj)
 {
     if (obj.IsHard)
     {
         renderer.WriteInline(new LineBreak()
         {
             Tag = obj
         });
     }
     else
     {
         // neither hard nor backslash -> but we have to add a space at least
         renderer.WriteInline(new Run(" ")
         {
             Tag = obj
         });
     }
 }
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] TaskList taskList)
        {
            var checkBox = new CheckBox
            {
                IsEnabled = false,
                IsChecked = taskList.Checked,
            };

            checkBox.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.TaskListStyleKey);
            renderer.WriteInline(new InlineUIContainer(checkBox));
        }
Esempio n. 8
0
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] MathInline obj)
        {
            var text = obj.Content.Text.Substring(obj.Content.Start, obj.Content.Length);

            TexFormula formula = null;

            try
            {
                formula = formulaParser.Parse(text);
            }
            catch (Exception)
            {
                renderer.WriteInline(new Run("[!!FORMULA PARSE ERROR!!]")
                {
                    Tag = obj
                });
                return;
            }

            var fontSize        = renderer.CurrentFontSize();
            var formulaRenderer = formula.GetRenderer(TexStyle.Display, fontSize, "Arial");
            var geo             = formulaRenderer.RenderToGeometry(0, 0);
            var geoD            = new System.Windows.Media.GeometryDrawing(Brushes.Black, null, geo);
            var di      = new DrawingImage(geoD);
            var uiImage = new System.Windows.Controls.Image()
            {
                Source = di
            };

            uiImage.Height = formulaRenderer.RenderSize.Height;                                                                         // size image to match rendersize -> get a zoom of 100%
            uiImage.Margin = new System.Windows.Thickness(0, 0, 0, -formulaRenderer.RenderSize.Height * formulaRenderer.RelativeDepth); // Move image so that baseline matches that of text
            var uiInline = new System.Windows.Documents.InlineUIContainer()
            {
                Child             = uiImage,
                Background        = Brushes.Yellow,
                BaselineAlignment = System.Windows.BaselineAlignment.Baseline,
                Tag = obj,
            };

            renderer.WriteInline(uiInline);
        }
Esempio n. 9
0
 /// <inheritdoc/>
 protected override void Write([NotNull] WpfRenderer renderer, [NotNull] LineBreakInline obj)
 {
     if (obj.IsHard)
     {
         renderer.WriteInline(new LineBreak());
     }
     else
     {
         // Soft line break.
         renderer.WriteText(" ");
     }
 }
Esempio n. 10
0
        /// <inheritdoc/>
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] LiteralInline obj)
        {
            if (obj.Content.IsEmpty)
            {
                return;
            }

            renderer.WriteInline(new Run(obj.Content.ToString())
            {
                Tag = obj
            });
        }
Esempio n. 11
0
        /// <inheritdoc/>
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] HtmlEntityInline obj)
        {
            if (obj.Transcoded.IsEmpty)
            {
                return;
            }

            renderer.WriteInline(new Run(obj.Transcoded.ToString())
            {
                Tag = obj
            });
        }
Esempio n. 12
0
        /// <inheritdoc/>
        protected override void Write(WpfRenderer renderer, LinkInline link)
        {
            if (renderer == null)
            {
                throw new ArgumentNullException(nameof(renderer));
            }
            if (link == null)
            {
                throw new ArgumentNullException(nameof(link));
            }

            var url = link.GetDynamicUrl != null?link.GetDynamicUrl() ?? link.Url : link.Url;

            if (!Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
            {
                url = "#";
            }

            if (link.IsImage)
            {
                var template = new ControlTemplate();
                var image    = new FrameworkElementFactory(typeof(Image));
                image.SetValue(Image.SourceProperty, new BitmapImage(new Uri(url, UriKind.RelativeOrAbsolute)));
                image.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.ImageStyleKey);
                template.VisualTree = image;

                var btn = new Button()
                {
                    Template         = template,
                    Command          = Commands.Image,
                    CommandParameter = url
                };

                renderer.WriteInline(new InlineUIContainer(btn));
            }
            else
            {
                var hyperlink = new Hyperlink
                {
                    Command          = Commands.Hyperlink,
                    CommandParameter = url,
                    NavigateUri      = new Uri(url, UriKind.RelativeOrAbsolute),
                    ToolTip          = !string.IsNullOrEmpty(link.Title) ? link.Title : null,
                };

                hyperlink.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.HyperlinkStyleKey);

                renderer.Push(hyperlink);
                renderer.WriteChildren(link);
                renderer.Pop();
            }
        }
Esempio n. 13
0
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] TaskList taskList)
        {
            var checkBox = new CheckBox
            {
                IsEnabled = false,
                IsChecked = taskList.Checked
            };

            renderer.Styles.ApplyTaskListStyle(checkBox);
            renderer.WriteInline(new InlineUIContainer(checkBox)
            {
                Tag = taskList
            });
        }
Esempio n. 14
0
        protected override void Write(WpfRenderer renderer, CodeInline obj)
        {
            if (renderer == null)
            {
                throw new ArgumentNullException(nameof(renderer));
            }
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            var run = new Run(obj.Content);

            run.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.CodeStyleKey);
            renderer.WriteInline(run);
        }
        /// <inheritdoc/>
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] AutolinkInline link)
        {
            var url = link.Url;

            if (!Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
            {
                url = "#";
            }

            var hyperlink = new Hyperlink
            {
                Command          = Commands.Hyperlink,
                CommandParameter = url,
                NavigateUri      = new Uri(url, UriKind.RelativeOrAbsolute),
                ToolTip          = url,
            };

            renderer.WriteInline(hyperlink);
        }
Esempio n. 16
0
        protected override void Write(WpfRenderer renderer, TaskList taskList)
        {
            if (renderer == null)
            {
                throw new ArgumentNullException(nameof(renderer));
            }
            if (taskList == null)
            {
                throw new ArgumentNullException(nameof(taskList));
            }

            var checkBox = new CheckBox
            {
                IsEnabled = false,
                IsChecked = taskList.Checked,
            };

            checkBox.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.TaskListStyleKey);
            renderer.WriteInline(new InlineUIContainer(checkBox));
        }
Esempio n. 17
0
        /// <inheritdoc/>
        protected override void Write(WpfRenderer renderer, LineBreakInline obj)
        {
            if (renderer == null)
            {
                throw new ArgumentNullException(nameof(renderer));
            }
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (obj.IsHard)
            {
                renderer.WriteInline(new LineBreak());
            }
            else
            {
                // Soft line break.
                renderer.WriteText(" ");
            }
        }
Esempio n. 18
0
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] MathBlock obj)
        {
            string text = string.Empty; // obj.Content.Text.Substring(obj.Content.Start, obj.Content.Length);

            for (int i = 0; i < obj.Lines.Count; ++i)
            {
                var l = obj.Lines.Lines[i];
                text += l.Slice.Text.Substring(l.Slice.Start, l.Slice.Length);
            }

            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            TexFormula formula = null;

            try
            {
                formula = formulaParser.Parse(text);
            }
            catch (Exception)
            {
                var paragraph = new Paragraph()
                {
                    Tag = obj
                };
                renderer.Push(paragraph);
                renderer.WriteInline(new Run("[!!FORMULA PARSE ERROR!!]")
                {
                    Tag = obj
                });
                renderer.Pop();
                return;
            }

            var fontSize = renderer.CurrentFontSize();

            if (fontSize <= 0)
            {
                throw new InvalidProgramException();
            }

            var formulaRenderer = formula.GetRenderer(TexStyle.Display, fontSize, "Arial");
            var geo             = formulaRenderer.RenderToGeometry(0, 0);
            var geoD            = new System.Windows.Media.GeometryDrawing(Brushes.Black, null, geo);
            var di      = new DrawingImage(geoD);
            var uiImage = new System.Windows.Controls.Image()
            {
                Source = di
            };

            uiImage.Height = formulaRenderer.RenderSize.Height; // size image to match rendersize -> get a zoom of 100%
            // uiImage.Margin = new System.Windows.Thickness(0, 0, 0, -formulaRenderer.RenderSize.Height * formulaRenderer.RelativeDepth); // Move image so that baseline matches that of text
            var uiBlock = new System.Windows.Documents.BlockUIContainer()
            {
                Child = uiImage,
                Tag   = obj,
            };

            renderer.WriteBlock(uiBlock);
        }
Esempio n. 19
0
        /// <inheritdoc/>
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] LinkInline link)
        {
            var url = link.GetDynamicUrl != null?link.GetDynamicUrl() ?? link.Url : link.Url;

            if (link.IsImage)
            {
                var inline = renderer.ImageProvider.GetInlineItem(url, out var inlineItemIsErrorMessage);

                double?width = null, height = null;

                if (link.ContainsData(typeof(Markdig.Renderers.Html.HtmlAttributes)))
                {
                    var htmlAttributes = (Markdig.Renderers.Html.HtmlAttributes)link.GetData(typeof(Markdig.Renderers.Html.HtmlAttributes));
                    if (null != htmlAttributes.Properties)
                    {
                        foreach (var entry in htmlAttributes.Properties)
                        {
                            switch (entry.Key.ToLowerInvariant())
                            {
                            case "width":
                                width = GetLength(entry.Value);
                                break;

                            case "height":
                                height = GetLength(entry.Value);
                                break;
                            }
                        }
                    }
                }

                if (null != inline)
                {
                    inline.Tag = link;

                    if (inline is InlineUIContainer container && container.Child is Image image)
                    {
                        renderer.Styles.ApplyImageStyle(image);

                        var bitmapSource = image.Source as BitmapSource;

                        if (width.HasValue && height.HasValue)
                        {
                            // then we do a non-uniform stretching
                            if (null != bitmapSource && (bitmapSource.PixelWidth < width.Value || bitmapSource.PixelHeight < height.Value))
                            {
                                // we have to use scale to up-scale the image
                                image.LayoutTransform = new System.Windows.Media.ScaleTransform(width.Value / image.Source.Width, height.Value / image.Source.Height);
                                image.Width           = width.Value;
                                image.Height          = height.Value;
                                image.Stretch         = System.Windows.Media.Stretch.Fill;
                            }
                            else
                            {
                                image.Width   = width.Value;
                                image.Height  = height.Value;
                                image.Stretch = System.Windows.Media.Stretch.Fill;
                            }
                        }
                        else if (width.HasValue)
                        {
                            if (null != bitmapSource && bitmapSource.PixelWidth < width.Value)
                            {
                                // we have to use scale to up-scale the image
                                double scale = width.Value / bitmapSource.PixelWidth;
                                image.LayoutTransform = new System.Windows.Media.ScaleTransform(scale, scale);
                            }
                            else
                            {
                                image.Width   = width.Value;
                                image.Stretch = System.Windows.Media.Stretch.Uniform;
                            }
                        }
                        else if (height.HasValue)
                        {
                            if (null != bitmapSource && bitmapSource.PixelHeight < height.Value)
                            {
                                // we have to use scale to up-scale the image
                                double scale = height.Value / bitmapSource.PixelHeight;
                                image.LayoutTransform = new System.Windows.Media.ScaleTransform(scale, scale);
                            }
                            else
                            {
                                image.Height  = height.Value;
                                image.Stretch = System.Windows.Media.Stretch.Uniform;
                            }
                        }
                        else // neither width nor height provided
                        {
                            // it seems like a bug (or a feature?) in Wpf that to determine the size of the image,
                            // _not_ the width and height property of the image source is used.
                            // Instead it seems here that the PixelWidth and the PixelHeight is used and interpreted
                            // as 1/96th inch.
                            // We correct for that by assigning the image the width and height of the imageSource

                            // TODO: if the above theory is correct, then the next lines will work only if the Dpi of the bitmapSource is >=96
                            image.Width   = image.Source.Width;
                            image.Height  = image.Source.Height;
                            image.Stretch = System.Windows.Media.Stretch.Uniform;
                        }
                    }

                    renderer.WriteInline(inline);
                }
            }
            else
            {
                Hyperlink hyperlink = null;
                if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
                {
                    hyperlink = new Hyperlink
                    {
                        Command          = Commands.Hyperlink,
                        CommandParameter = url,
                        NavigateUri      = new Uri(url, UriKind.RelativeOrAbsolute),
                        ToolTip          = link.Title != string.Empty ? link.Title : null,
                        Tag = link
                    };
                }
                else // not a well formed Uri String - then it is probably a fragment reference
                {
                    hyperlink = new Hyperlink
                    {
                        Command          = Commands.FragmentLink,
                        CommandParameter = url,
                        NavigateUri      = new Uri(url, UriKind.RelativeOrAbsolute),
                        ToolTip          = link.Title != string.Empty ? link.Title : null,
                        Tag = link
                    };
                }

                renderer.Styles.ApplyHyperlinkStyle(hyperlink);
                renderer.Push(hyperlink);
                renderer.WriteChildren(link);
                renderer.Pop();
            }
        }