diff --git a/.gitignore b/.gitignore index 9491a2f..f4291b6 100644 --- a/.gitignore +++ b/.gitignore @@ -360,4 +360,6 @@ MigrationBackup/ .ionide/ # Fody - auto-generated XML schema -FodyWeavers.xsd \ No newline at end of file +FodyWeavers.xsd + +.idea/ diff --git a/HTMLToQPDF.Example/Assets/testHtml.html b/HTMLToQPDF.Example/Assets/testHtml.html index 850d36c..1e9a1d1 100644 --- a/HTMLToQPDF.Example/Assets/testHtml.html +++ b/HTMLToQPDF.Example/Assets/testHtml.html @@ -13,7 +13,7 @@ -

Lorem

+

Lorem

h1

h2

h3

diff --git a/HTMLToQPDF.Example/Utilities/PDFCreator.cs b/HTMLToQPDF.Example/Utilities/PDFCreator.cs index cb9aabc..ee8c236 100644 --- a/HTMLToQPDF.Example/Utilities/PDFCreator.cs +++ b/HTMLToQPDF.Example/Utilities/PDFCreator.cs @@ -18,6 +18,7 @@ public static void Create(string html, string path, bool customStyles) page.MarginVertical(1f, Unit.Centimetre); page.DefaultTextStyle(TextStyle.Default + .FontFamily("MS Reference Sans Serif") .Fallback(y => y.FontFamily("MS Reference Sans Serif") .Fallback(y => y.FontFamily("Segoe UI Emoji") .Fallback(y => y.FontFamily("Microsoft YaHei"))))); @@ -28,7 +29,8 @@ public static void Create(string html, string path, bool customStyles) { if (customStyles) { - handler.SetTextStyleForHtmlElement("h1", TextStyle.Default.FontColor(Colors.DeepOrange.Accent4).FontSize(32).Bold()); + handler.SetTextStyleForHtmlElement("h1", t => t.FontColor(Colors.DeepOrange.Accent4).FontSize(32).Bold()); + handler.SetCssStyleForHtmlElement("underline", t => t.Underline()); handler.SetContainerStyleForHtmlElement("div", c => c.Background(Colors.Teal.Lighten5)); handler.SetContainerStyleForHtmlElement("img", c => c.MaxHeight(7, Unit.Centimetre)); handler.SetContainerStyleForHtmlElement("table", c => c.Background(Colors.Pink.Lighten5)); diff --git a/HTMLToQPDF/Components/HTMLComponent.cs b/HTMLToQPDF/Components/HTMLComponent.cs index 744b756..1430dac 100644 --- a/HTMLToQPDF/Components/HTMLComponent.cs +++ b/HTMLToQPDF/Components/HTMLComponent.cs @@ -16,24 +16,26 @@ internal class HTMLComponent : IComponent { public GetImgBySrc GetImgBySrc { get; set; } = ImgUtils.GetImgBySrc; - public Dictionary TextStyles { get; } = new Dictionary() + public Dictionary> TextStyles { get; } = new() { - { "h1", TextStyle.Default.FontSize(32).Bold() }, - { "h2", TextStyle.Default.FontSize(28).Bold() }, - { "h3", TextStyle.Default.FontSize(24).Bold() }, - { "h4", TextStyle.Default.FontSize(20).Bold() }, - { "h5", TextStyle.Default.FontSize(16).Bold() }, - { "h6", TextStyle.Default.FontSize(12).Bold() }, - { "b", TextStyle.Default.Bold() }, - { "i", TextStyle.Default.Italic() }, - { "small", TextStyle.Default.Light() }, - { "strike", TextStyle.Default.Strikethrough() }, - { "s", TextStyle.Default.Strikethrough() }, - { "u", TextStyle.Default.Underline() }, - { "a", TextStyle.Default.Underline() }, + { "h1", t => t.FontSize(32).Bold() }, + { "h2", t => t.FontSize(28).Bold() }, + { "h3", t => t.FontSize(24).Bold() }, + { "h4", t => t.FontSize(20).Bold() }, + { "h5", t => t.FontSize(16).Bold() }, + { "h6", t => t.FontSize(12).Bold() }, + { "b", t => t.Bold() }, + { "i", t => t.Italic() }, + { "small", t => t.Light() }, + { "strike", t => t.Strikethrough() }, + { "s", t => t.Strikethrough() }, + { "u", t => t.Underline() }, + { "a", t => t.Underline() }, }; - public Dictionary> ContainerStyles { get; } = new Dictionary>() + public Dictionary> CssStyles { get; } = new(); + + public Dictionary> ContainerStyles { get; } = new() { { "p", c => c.PaddingVertical(6) }, { "ul", c => c.PaddingLeft(30) }, @@ -52,7 +54,8 @@ public void Compose(IContainer container) CreateSeparateBranchesForTextNodes(node); - container.Component(node.GetComponent(new HTMLComponentsArgs(TextStyles, ContainerStyles, ListVerticalPadding, GetImgBySrc))); + var args = new HTMLComponentsArgs(TextStyles, CssStyles, ContainerStyles, ListVerticalPadding, GetImgBySrc); + container.Component(node.GetComponent(args)); } /// diff --git a/HTMLToQPDF/Components/HTMLComponentsArgs.cs b/HTMLToQPDF/Components/HTMLComponentsArgs.cs index 613d60c..2b13ee6 100644 --- a/HTMLToQPDF/Components/HTMLComponentsArgs.cs +++ b/HTMLToQPDF/Components/HTMLComponentsArgs.cs @@ -4,19 +4,10 @@ namespace HTMLToQPDF.Components { public delegate byte[]? GetImgBySrc(string src); - internal class HTMLComponentsArgs - { - public Dictionary TextStyles { get; } - public Dictionary> ContainerStyles { get; } - public float ListVerticalPadding { get; } - public GetImgBySrc GetImgBySrc { get; } - - public HTMLComponentsArgs(Dictionary textStyles, Dictionary> containerStyles, float listVerticalPadding, GetImgBySrc getImgBySrc) - { - TextStyles = textStyles; - ContainerStyles = containerStyles; - ListVerticalPadding = listVerticalPadding; - GetImgBySrc = getImgBySrc; - } - } + internal record HTMLComponentsArgs( + Dictionary> TextStyles, + Dictionary> CssStyles, + Dictionary> ContainerStyles, + float ListVerticalPadding, + GetImgBySrc GetImgBySrc); } \ No newline at end of file diff --git a/HTMLToQPDF/Components/ParagraphComponent.cs b/HTMLToQPDF/Components/ParagraphComponent.cs index 9bc0258..ab482de 100644 --- a/HTMLToQPDF/Components/ParagraphComponent.cs +++ b/HTMLToQPDF/Components/ParagraphComponent.cs @@ -9,12 +9,14 @@ namespace HTMLQuestPDF.Components internal class ParagraphComponent : IComponent { private readonly List lineNodes; - private readonly Dictionary textStyles; + private readonly Dictionary> textStyles; + private readonly Dictionary> cssStyles; public ParagraphComponent(List lineNodes, HTMLComponentsArgs args) { this.lineNodes = lineNodes; this.textStyles = args.TextStyles; + this.cssStyles = args.CssStyles; } private HtmlNode? GetParrentBlock(HtmlNode node) @@ -109,7 +111,11 @@ public TextSpanAction GetTextStyles(HtmlNode element) public TextStyle GetTextStyle(HtmlNode element) { - return textStyles.TryGetValue(element.Name.ToLower(), out TextStyle? style) ? style : TextStyle.Default; + var style = TextStyle.Default; + foreach (var cssName in element.GetClasses()) + if (cssStyles.TryGetValue(cssName, out var cssStyle)) + style = cssStyle(style); + return textStyles.TryGetValue(element.Name.ToLower(), out var textStyle) ? textStyle(style) : style; } } } \ No newline at end of file diff --git a/HTMLToQPDF/HTMLDescriptor.cs b/HTMLToQPDF/HTMLDescriptor.cs index e822c15..fe95e6f 100644 --- a/HTMLToQPDF/HTMLDescriptor.cs +++ b/HTMLToQPDF/HTMLDescriptor.cs @@ -18,11 +18,16 @@ public void OverloadImgReceivingFunc(GetImgBySrc getImg) PDFPage.GetImgBySrc = getImg; } - public void SetTextStyleForHtmlElement(string tagName, TextStyle style) + public void SetTextStyleForHtmlElement(string tagName, Func style) { PDFPage.TextStyles[tagName.ToLower()] = style; } + public void SetCssStyleForHtmlElement(string cssName, Func style) + { + PDFPage.CssStyles[cssName.ToLower()] = style; + } + public void SetContainerStyleForHtmlElement(string tagName, Func style) { PDFPage.ContainerStyles[tagName.ToLower()] = style; diff --git a/HTMLToQPDF/StyleSettings.cs b/HTMLToQPDF/StyleSettings.cs index 3a30f73..c4eb200 100644 --- a/HTMLToQPDF/StyleSettings.cs +++ b/HTMLToQPDF/StyleSettings.cs @@ -8,7 +8,7 @@ namespace HTMLQuestPDF { internal static class HTMLMapSettings { - public static readonly string[] LineElements = new string[] { + public static readonly string[] LineElements = { "a", "b", "br", @@ -24,7 +24,8 @@ internal static class HTMLMapSettings "tr", "u", "img", - "text" + "text", + "span" }; public static readonly string[] BlockElements = new string[] {