protected override void Write([NotNull] WpfRenderer renderer, [NotNull] ListBlock listBlock) { var list = new List(); if (listBlock.IsOrdered) { list.MarkerStyle = TextMarkerStyle.Decimal; if (listBlock.OrderedStart != null && (listBlock.DefaultOrderedStart != listBlock.OrderedStart)) { list.StartIndex = int.Parse(listBlock.OrderedStart); } } else { list.MarkerStyle = TextMarkerStyle.Disc; } renderer.Push(list); foreach (var item in listBlock) { var listItemBlock = (ListItemBlock)item; var listItem = new ListItem(); renderer.Push(listItem); renderer.WriteChildren(listItemBlock); renderer.Pop(); } renderer.Pop(); }
protected override void Write([NotNull] WpfRenderer renderer, [NotNull] HeadingBlock obj) { var paragraph = new Paragraph(); ComponentResourceKey styleKey = null; switch (obj.Level) { case 1: styleKey = Styles.Heading1StyleKey; break; case 2: styleKey = Styles.Heading2StyleKey; break; case 3: styleKey = Styles.Heading3StyleKey; break; case 4: styleKey = Styles.Heading4StyleKey; break; case 5: styleKey = Styles.Heading5StyleKey; break; case 6: styleKey = Styles.Heading6StyleKey; break; } if (styleKey != null) { paragraph.SetResourceReference(FrameworkContentElement.StyleProperty, styleKey); } renderer.Push(paragraph); renderer.WriteLeafInline(obj); renderer.Pop(); }
protected override void Write([NotNull] WpfRenderer renderer, [NotNull] HeadingBlock obj) { var paragraph = new Paragraph() { Tag = obj }; switch (obj.Level) { case 1: renderer.Styles.ApplyHeading1Style(paragraph); break; case 2: renderer.Styles.ApplyHeading2Style(paragraph); break; case 3: renderer.Styles.ApplyHeading3Style(paragraph); break; case 4: renderer.Styles.ApplyHeading4Style(paragraph); break; case 5: renderer.Styles.ApplyHeading5Style(paragraph); break; case 6: renderer.Styles.ApplyHeading6Style(paragraph); break; } renderer.Push(paragraph); renderer.WriteLeafInline(obj); renderer.Pop(); }
/// <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(); } }
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] AutolinkInline link) { var url = link.Url; if (link.IsEmail) { url = "mailto:" + url; } if (!Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute)) { url = "#"; } var hyperlink = new Hyperlink { Command = Commands.Hyperlink, CommandParameter = url, NavigateUri = new Uri(url, UriKind.RelativeOrAbsolute), ToolTip = link.Url, }; hyperlink.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.HyperlinkStyleKey); renderer.Push(hyperlink); renderer.WriteText(link.Url); renderer.Pop(); }
protected override void Write([NotNull] WpfRenderer renderer, [NotNull] EmphasisInline obj) { Span span = null; switch (obj.DelimiterChar) { case '*': case '_': span = obj.IsDouble ? (Span) new Bold() : new Italic(); break; case '~': span = new Span(); if (obj.IsDouble) { renderer.Styles.ApplyStrikeThroughStyle(span); } else { renderer.Styles.ApplySubscriptStyle(span); if (span.FontSize < 1) { span.FontSize = renderer.CurrentFontSize() * span.FontSize; } } break; case '^': span = new Span(); renderer.Styles.ApplySuperscriptStyle(span); if (span.FontSize < 1) { span.FontSize = renderer.CurrentFontSize() * span.FontSize; } break; case '+': span = new Span(); renderer.Styles.ApplyInsertedStyle(span); break; case '=': span = new Span(); renderer.Styles.ApplyMarkedStyle(span); break; } if (span != null) { span.Tag = obj; renderer.Push(span); renderer.WriteChildren(obj); renderer.Pop(); } else { renderer.WriteChildren(obj); } }
/// <inheritdoc/> protected override void Write([NotNull] WpfRenderer renderer, [NotNull] ParagraphBlock obj) { var paragraph = new Paragraph(); renderer.Push(paragraph); renderer.WriteLeafInline(obj); renderer.Pop(); }
protected override void Write([NotNull] WpfRenderer renderer, [NotNull] CodeBlock obj) { var paragraph = new Paragraph(); paragraph.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.CodeBlockStyleKey); renderer.Push(paragraph); renderer.WriteLeafRawLines(obj); renderer.Pop(); }
/// <inheritdoc/> protected override void Write([NotNull] WpfRenderer renderer, [NotNull] QuoteBlock obj) { var section = new Section(); renderer.Push(section); renderer.WriteChildren(obj); section.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.QuoteBlockStyleKey); renderer.Pop(); }
protected override void Write([NotNull] WpfRenderer renderer, [NotNull] Markdig.Extensions.Figures.FigureCaption obj) { Paragraph paragraph = new Paragraph() { Tag = obj }; renderer.Push(paragraph); renderer.WriteLeafInline(obj); renderer.Pop(); }
protected override void Write(WpfRenderer renderer, ListBlock listBlock) { if (renderer == null) { throw new ArgumentNullException(nameof(renderer)); } if (listBlock == null) { throw new ArgumentNullException(nameof(listBlock)); } var list = new List(); if (listBlock.IsOrdered) { list.MarkerStyle = TextMarkerStyle.Decimal; if (listBlock.OrderedStart != null && (listBlock.DefaultOrderedStart != listBlock.OrderedStart)) { list.StartIndex = int.Parse(listBlock.OrderedStart, NumberFormatInfo.InvariantInfo); } } else { list.MarkerStyle = TextMarkerStyle.Disc; } renderer.Push(list); foreach (var item in listBlock) { var listItemBlock = (ListItemBlock)item; var listItem = new ListItem(); renderer.Push(listItem); renderer.WriteChildren(listItemBlock); renderer.Pop(); } renderer.Pop(); }
/// <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(); } }
protected override void Write(WpfRenderer renderer, EmphasisInline obj) { if (renderer == null) { throw new ArgumentNullException(nameof(renderer)); } if (obj == null) { throw new ArgumentNullException(nameof(obj)); } Span?span = null; switch (obj.DelimiterChar) { case '*': case '_': span = obj.DelimiterCount == 2 ? (Span) new Bold() : new Italic(); break; case '~': span = new Span(); span.SetResourceReference(FrameworkContentElement.StyleProperty, obj.DelimiterCount == 2 ? Styles.StrikeThroughStyleKey : Styles.SubscriptStyleKey); break; case '^': span = new Span(); span.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.SuperscriptStyleKey); break; case '+': span = new Span(); span.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.InsertedStyleKey); break; case '=': span = new Span(); span.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.MarkedStyleKey); break; } if (span != null) { renderer.Push(span); renderer.WriteChildren(obj); renderer.Pop(); } else { renderer.WriteChildren(obj); } }
/// <inheritdoc/> protected override void Write([NotNull] WpfRenderer renderer, [NotNull] QuoteBlock obj) { var section = new Section() { Tag = obj }; renderer.Styles.ApplyQuoteBlockStyle(section); renderer.Push(section); renderer.WriteChildren(obj); renderer.Pop(); }
protected override void Write(WpfRenderer renderer, CodeBlock obj) { if (renderer == null) { throw new ArgumentNullException(nameof(renderer)); } var paragraph = new Paragraph(); paragraph.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.CodeBlockStyleKey); renderer.Push(paragraph); renderer.WriteLeafRawLines(obj); renderer.Pop(); }
/// <inheritdoc/> protected override void Write(WpfRenderer renderer, QuoteBlock obj) { if (renderer == null) { throw new ArgumentNullException(nameof(renderer)); } if (obj == null) { throw new ArgumentNullException(nameof(obj)); } var section = new Section(); renderer.Push(section); renderer.WriteChildren(obj); section.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.QuoteBlockStyleKey); renderer.Pop(); }
protected override void Write([NotNull] WpfRenderer renderer, [NotNull] EmphasisInline obj) { Span span = null; switch (obj.DelimiterChar) { case '*': case '_': span = obj.IsDouble ? (Span) new Bold() : new Italic(); break; case '~': span = new Span(); span.SetResourceReference(FrameworkContentElement.StyleProperty, obj.IsDouble ? Styles.StrikeThroughStyleKey: Styles.SubscriptStyleKey); break; case '^': span = new Span(); span.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.SuperscriptStyleKey); break; case '+': span = new Span(); span.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.InsertedStyleKey); break; case '=': span = new Span(); span.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.MarkedStyleKey); break; } if (span != null) { renderer.Push(span); renderer.WriteChildren(obj); renderer.Pop(); } else { renderer.WriteChildren(obj); } }
protected override void Write([NotNull] WpfRenderer renderer, [NotNull] EmphasisInline obj) { Span span = null; if (obj.DelimiterChar == '*' || obj.DelimiterChar == '_') { span = obj.IsDouble ? (Span) new Bold() : new Italic(); } if (span != null) { renderer.Push(span); renderer.WriteChildren(obj); renderer.Pop(); } else { renderer.WriteChildren(obj); } }
protected override void Write(WpfRenderer renderer, HeadingBlock obj) { if (renderer == null) { throw new ArgumentNullException(nameof(renderer)); } if (obj == null) { throw new ArgumentNullException(nameof(obj)); } var paragraph = new Paragraph(); ComponentResourceKey?styleKey = null; switch (obj.Level) { case 1: styleKey = Styles.Heading1StyleKey; break; case 2: styleKey = Styles.Heading2StyleKey; break; case 3: styleKey = Styles.Heading3StyleKey; break; case 4: styleKey = Styles.Heading4StyleKey; break; case 5: styleKey = Styles.Heading5StyleKey; break; case 6: styleKey = Styles.Heading6StyleKey; break; } if (styleKey != null) { paragraph.SetResourceReference(FrameworkContentElement.StyleProperty, styleKey); } renderer.Push(paragraph); renderer.WriteLeafInline(obj); renderer.Pop(); }
/// <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(); } }
protected override void Write([NotNull] WpfRenderer renderer, [NotNull] Table table) { var wpfTable = new WpfTable(); wpfTable.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.TableStyleKey); foreach (var tableColumnDefinition in table.ColumnDefinitions) { wpfTable.Columns.Add(new WpfTableColumn { Width = (tableColumnDefinition?.Width ?? 0) != 0 ? new GridLength(tableColumnDefinition.Width, GridUnitType.Star) : GridLength.Auto, }); } var wpfRowGroup = new WpfTableRowGroup(); renderer.Push(wpfTable); renderer.Push(wpfRowGroup); foreach (var rowObj in table) { var row = (TableRow)rowObj; var wpfRow = new WpfTableRow(); renderer.Push(wpfRow); if (row.IsHeader) { wpfRow.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.TableHeaderStyleKey); } for (var i = 0; i < row.Count; i++) { var cellObj = row[i]; var cell = (TableCell)cellObj; var wpfCell = new WpfTableCell { ColumnSpan = cell.ColumnSpan, RowSpan = cell.RowSpan, }; wpfCell.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.TableCellStyleKey); renderer.Push(wpfCell); renderer.Write(cell); renderer.Pop(); if (table.ColumnDefinitions.Count > 0) { var columnIndex = cell.ColumnIndex < 0 || cell.ColumnIndex >= table.ColumnDefinitions.Count ? i : cell.ColumnIndex; columnIndex = columnIndex >= table.ColumnDefinitions.Count ? table.ColumnDefinitions.Count - 1 : columnIndex; var alignment = table.ColumnDefinitions[columnIndex].Alignment; if (alignment.HasValue) { switch (alignment) { case TableColumnAlign.Center: wpfCell.TextAlignment = TextAlignment.Center; break; case TableColumnAlign.Right: wpfCell.TextAlignment = TextAlignment.Right; break; case TableColumnAlign.Left: wpfCell.TextAlignment = TextAlignment.Left; break; } } } } renderer.Pop(); } renderer.Pop(); renderer.Pop(); }
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); }