/// <summary> /// Renders a subscript element. /// </summary> /// <param name="element"> The parsed inline element to render. </param> /// <param name="context"> Persistent state. </param> protected override void RenderSubscriptRun(SubscriptTextInline element, IRenderContext context) { var localContext = context as InlineRenderContext; var parent = localContext?.Parent as TextElement; if (localContext == null && parent == null) { throw new RenderContextIncorrectException(); } var span = new Span { FontSize = parent.FontSize * 0.7, FontFamily = parent.FontFamily, FontStyle = parent.FontStyle, FontWeight = parent.FontWeight }; span.BaselineAlignment = BaselineAlignment.Subscript; var childContext = new InlineRenderContext(span.Inlines, context) { Parent = span }; RenderInlineChildren(element.Inlines, childContext); localContext.InlineCollection.Add(span); }
/// <summary> /// Renders a paragraph element. /// </summary> protected override void RenderParagraph(ParagraphBlock element, IRenderContext context) { if (!(context is BlockCollectionRenderContext localContext)) { throw new RenderContextIncorrectException(); } var blockCollection = localContext.BlockCollection; var paragraph = new Paragraph { Margin = ParagraphMargin, LineHeight = ParagraphLineHeight, Foreground = localContext.Foreground }; var childContext = new InlineRenderContext(paragraph.Inlines, context) { Parent = paragraph }; RenderInlineChildren(element.Inlines, childContext); blockCollection.Add(paragraph); }
/// <summary> /// Renders a link element /// </summary> /// <param name="element"> The parsed inline element to render. </param> /// <param name="context"> Persistent state. </param> protected override void RenderMarkdownLink(MarkdownLinkInline element, IRenderContext context) { if (!(context is InlineRenderContext localContext)) { throw new RenderContextIncorrectException(); } // HACK: Superscript is not allowed within a hyperlink. But if we switch it around, so // that the superscript is outside the hyperlink, then it will render correctly. // This assumes that the entire hyperlink is to be rendered as superscript. if (AllTextIsSuperscript(element) == false) { var link = new Hyperlink(); LinkRegister.RegisterNewHyperLink(link, element.Url); RemoveSuperscriptRuns(element, insertCaret: true); var childContext = new InlineRenderContext(link.Inlines, context) { Parent = link, WithinHyperlink = true }; if (localContext.OverrideForeground) { link.Foreground = localContext.Foreground; } else if (LinkForeground != null) { link.Foreground = LinkForeground; } RenderInlineChildren(element.Inlines, childContext); context.TrimLeadingWhitespace = childContext.TrimLeadingWhitespace; ToolTipService.SetToolTip(link, element.Tooltip ?? element.Url); localContext.InlineCollection.Add(link); } else { // THE HACK IS ON! // Create a fake superscript element. var fakeSuperscript = new SuperscriptTextInline { Inlines = new List <MarkdownInline> { element } }; RemoveSuperscriptRuns(element, insertCaret: false); RenderSuperscriptRun(fakeSuperscript, context); } }
/// <summary> /// Renders a superscript element. /// </summary> /// <param name="element"> The parsed inline element to render. </param> /// <param name="context"> Persistent state. </param> protected override void RenderSuperscriptRun(SuperscriptTextInline element, IRenderContext context) { var localContext = context as InlineRenderContext; var parent = localContext?.Parent as TextElement; if (localContext == null && parent == null) { throw new RenderContextIncorrectException(); } // Le <sigh>, InlineUIContainers are not allowed within hyperlinks. if (localContext.WithinHyperlink) { RenderInlineChildren(element.Inlines, context); return; } var span = new Span() { FontSize = parent.FontSize * 0.8, FontFamily = parent.FontFamily, FontStyle = parent.FontStyle, FontWeight = parent.FontWeight, BaselineAlignment = BaselineAlignment.TextTop }; if (parent is Span parentspan) { span.BaselineAlignment = parentspan.BaselineAlignment switch { BaselineAlignment.Bottom => BaselineAlignment.Subscript, BaselineAlignment.Baseline => BaselineAlignment.Center, BaselineAlignment.Center => BaselineAlignment.TextTop, BaselineAlignment.TextTop => BaselineAlignment.Top, BaselineAlignment.Subscript => BaselineAlignment.Baseline, _ => BaselineAlignment.TextTop }; } var childContext = new InlineRenderContext(span.Inlines, context) { Parent = span }; RenderInlineChildren(element.Inlines, childContext); localContext.InlineCollection.Add(span); }
/// <summary> /// Renders a strikethrough element. /// </summary> /// <param name="element"> The parsed inline element to render. </param> /// <param name="context"> Persistent state. </param> protected override void RenderStrikethroughRun(StrikethroughTextInline element, IRenderContext context) { if (!(context is InlineRenderContext localContext)) { throw new RenderContextIncorrectException(); } Span span = new Span { TextDecorations = TextDecorations.Strikethrough }; var childContext = new InlineRenderContext(span.Inlines, context) { Parent = span }; RenderInlineChildren(element.Inlines, childContext); localContext.InlineCollection.Add(span); }
/// <summary> /// Renders a bold run element. /// </summary> /// <param name="element"> The parsed inline element to render. </param> /// <param name="context"> Persistent state. </param> protected override void RenderBoldRun(BoldTextInline element, IRenderContext context) { if (!(context is InlineRenderContext localContext)) { throw new RenderContextIncorrectException(); } Span boldSpan = new Span { FontWeight = FontWeights.Bold }; var childContext = new InlineRenderContext(boldSpan.Inlines, context) { Parent = boldSpan, WithinBold = true }; RenderInlineChildren(element.Inlines, childContext); localContext.InlineCollection.Add(boldSpan); }
/// <summary> /// Renders a text run element. /// </summary> /// <param name="element"> The parsed inline element to render. </param> /// <param name="context"> Persistent state. </param> protected override void RenderItalicRun(ItalicTextInline element, IRenderContext context) { if (!(context is InlineRenderContext localContext)) { throw new RenderContextIncorrectException(); } Span italicSpan = new Span { FontStyle = FontStyles.Italic }; var childContext = new InlineRenderContext(italicSpan.Inlines, context) { Parent = italicSpan, WithinItalics = true }; RenderInlineChildren(element.Inlines, childContext); localContext.InlineCollection.Add(italicSpan); }
/// <summary> /// Renders a table element. /// </summary> protected override void RenderTable(TableBlock element, IRenderContext context) { if (!(context is BlockCollectionRenderContext localContext)) { throw new RenderContextIncorrectException(); } var blockCollection = localContext.BlockCollection; var table = new Table() { CellSpacing = 0.0 }; while (table.Columns.Count < element.ColumnDefinitions.Count) { table.Columns.Add(new TableColumn()); } var tableRG = new TableRowGroup(); while (tableRG.Rows.Count < element.Rows.Count) { tableRG.Rows.Add(new TableRow()); } table.RowGroups.Add(tableRG); var borderthickness = new Thickness(TableBorderThickness / 2); for (int rowIndex = 0; rowIndex < element.Rows.Count; rowIndex++) { var row = element.Rows[rowIndex]; for (int cellIndex = 0; cellIndex < Math.Min(element.ColumnDefinitions.Count, row.Cells.Count); cellIndex++) { var cell = row.Cells[cellIndex]; var paragraph = new Paragraph { Foreground = localContext.Foreground, Margin = TableCellPadding }; paragraph.TextAlignment = element.ColumnDefinitions[cellIndex].Alignment switch { ColumnAlignment.Right => TextAlignment.Right, ColumnAlignment.Center => TextAlignment.Center, _ => TextAlignment.Left }; if (rowIndex == 0) { paragraph.FontWeight = FontWeights.Bold; } var childContext = new InlineRenderContext(paragraph.Inlines, context) { Parent = paragraph, TrimLeadingWhitespace = true }; RenderInlineChildren(cell.Inlines, childContext); var tablecell = new TableCell(paragraph) { BorderThickness = borderthickness, BorderBrush = TableBorderBrush }; tableRG.Rows[rowIndex].Cells.Add(tablecell); } } table.Margin = TableMargin; table.BorderBrush = TableBorderBrush; table.BorderThickness = borderthickness; blockCollection.Add(table); } }
/// <summary> /// Renders a header element. /// </summary> protected override void RenderHeader(HeaderBlock element, IRenderContext context) { if (!(context is BlockCollectionRenderContext localContext)) { throw new RenderContextIncorrectException(); } var blockCollection = localContext.BlockCollection; var paragraph = new Paragraph(); var childInlines = paragraph.Inlines; switch (element.HeaderLevel) { case 1: paragraph.Margin = Header1Margin; paragraph.FontSize = Header1FontSize; paragraph.FontWeight = Header1FontWeight; paragraph.Foreground = Header1Foreground; break; case 2: paragraph.Margin = Header2Margin; paragraph.FontSize = Header2FontSize; paragraph.FontWeight = Header2FontWeight; paragraph.Foreground = Header2Foreground; break; case 3: paragraph.Margin = Header3Margin; paragraph.FontSize = Header3FontSize; paragraph.FontWeight = Header3FontWeight; paragraph.Foreground = Header3Foreground; break; case 4: paragraph.Margin = Header4Margin; paragraph.FontSize = Header4FontSize; paragraph.FontWeight = Header4FontWeight; paragraph.Foreground = Header4Foreground; break; case 5: paragraph.Margin = Header5Margin; paragraph.FontSize = Header5FontSize; paragraph.FontWeight = Header5FontWeight; paragraph.Foreground = Header5Foreground; break; case 6: paragraph.Margin = Header6Margin; paragraph.FontSize = Header6FontSize; paragraph.FontWeight = Header6FontWeight; paragraph.Foreground = Header6Foreground; var underline = new Underline(); childInlines = underline.Inlines; paragraph.Inlines.Add(underline); break; } var childContext = new InlineRenderContext(childInlines, context) { Parent = paragraph, TrimLeadingWhitespace = true }; RenderInlineChildren(element.Inlines, childContext); blockCollection.Add(paragraph); }