From 7db347e9f7ced99af1ca1a91d28f011bd4fc2a0a Mon Sep 17 00:00:00 2001 From: madelson <1269046+madelson@users.noreply.github.com> Date: Wed, 20 Nov 2024 02:42:37 -0500 Subject: [PATCH] Enable trimming and AOT (#939) * Enable trimming and AOT analysis for all none-test projects * Try annotating various XmlSerializer related functions with RequiresUnreferencedCode * fix remaining AOT warnings * fix attribute warning message --------- Co-authored-by: Richard Webb --- src/Directory.Build.targets | 6 ++++++ .../Export/AltoXmlTextExporter.cs | 16 ++++++++++++++++ .../Export/HOcrTextExporter.cs | 4 ++++ .../Export/ITextExporter.cs | 4 ++++ .../Export/PageXmlTextExporter.cs | 13 +++++++++++++ .../Export/SvgTextExporter.cs | 4 ++++ src/UglyToad.PdfPig.sln | 1 + 7 files changed, 48 insertions(+) create mode 100644 src/Directory.Build.targets diff --git a/src/Directory.Build.targets b/src/Directory.Build.targets new file mode 100644 index 00000000..af14558c --- /dev/null +++ b/src/Directory.Build.targets @@ -0,0 +1,6 @@ + + + true + true + + diff --git a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/AltoXmlTextExporter.cs b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/AltoXmlTextExporter.cs index bfe623cb..62a86d69 100644 --- a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/AltoXmlTextExporter.cs +++ b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/AltoXmlTextExporter.cs @@ -4,6 +4,7 @@ using Content; using DocumentLayoutAnalysis; using System; + using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq; using System.Xml; @@ -94,6 +95,9 @@ /// /// The document to extract page layouts from. /// Draw PdfPaths present in the page. +#if NET6_0_OR_GREATER + [RequiresUnreferencedCode("Members from AltoDocument may be trimmed if not referenced directly")] +#endif public string Get(PdfDocument document, bool includePaths = false) { var altoDocument = CreateAltoDocument("unknown"); @@ -105,6 +109,9 @@ /// Get the Alto (XML) string of the page layout. Excludes s. /// /// The page to export the XML layout for. +#if NET6_0_OR_GREATER + [RequiresUnreferencedCode("Members from AltoDocument may be trimmed if not referenced directly")] +#endif public string Get(Page page) => Get(page, false); /// @@ -112,6 +119,9 @@ /// /// The page to export the XML layout for. /// Whether the output should include the PdfPaths present in the page. +#if NET6_0_OR_GREATER + [RequiresUnreferencedCode("Members from AltoDocument may be trimmed if not referenced directly")] +#endif public string Get(Page page, bool includePaths) { var document = CreateAltoDocument("unknown"); @@ -355,6 +365,9 @@ }; } +#if NET6_0_OR_GREATER + [RequiresUnreferencedCode("Members from AltoDocument may be trimmed if not referenced directly")] +#endif private string Serialize(AltoDocument altoDocument) { var serializer = new XmlSerializer(typeof(AltoDocument)); @@ -377,6 +390,9 @@ /// /// Deserialize an from a given Alto format XML document. /// +#if NET6_0_OR_GREATER + [RequiresUnreferencedCode("Members from AltoDocument may be trimmed if not referenced directly")] +#endif public static AltoDocument Deserialize(string xmlPath) { var serializer = new XmlSerializer(typeof(AltoDocument)); diff --git a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/HOcrTextExporter.cs b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/HOcrTextExporter.cs index edb19ad8..3441ac8c 100644 --- a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/HOcrTextExporter.cs +++ b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/HOcrTextExporter.cs @@ -4,6 +4,7 @@ using Core; using DocumentLayoutAnalysis; using System; + using System.Diagnostics.CodeAnalysis; using System.Linq; using UglyToad.PdfPig.DocumentLayoutAnalysis.PageSegmenter; using UglyToad.PdfPig.Graphics; @@ -118,6 +119,9 @@ /// Get the hOCR (HTML) string of the page layout. Excludes PdfPaths. /// /// The page. +#if NET6_0_OR_GREATER + [RequiresUnreferencedCode("'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.")] +#endif public string Get(Page page) { return Get(page, false); diff --git a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/ITextExporter.cs b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/ITextExporter.cs index 497bf072..3c6c1da8 100644 --- a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/ITextExporter.cs +++ b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/ITextExporter.cs @@ -1,6 +1,7 @@ namespace UglyToad.PdfPig.DocumentLayoutAnalysis.Export { using Content; + using System.Diagnostics.CodeAnalysis; /// /// Exports the page's text into the desired format. @@ -12,6 +13,9 @@ /// /// The page to convert to the format. /// The containing the page contents represented in a compatible format. +#if NET6_0_OR_GREATER + [RequiresUnreferencedCode("'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.")] +#endif string Get(Page page); } } diff --git a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/PageXmlTextExporter.cs b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/PageXmlTextExporter.cs index 324bfa0c..7297143a 100644 --- a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/PageXmlTextExporter.cs +++ b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/PageXmlTextExporter.cs @@ -10,6 +10,7 @@ using ReadingOrderDetector; using System; using System.Collections.Generic; + using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Xml; using System.Xml.Serialization; @@ -105,6 +106,9 @@ /// Get the PAGE-XML (XML) string of the pages layout. Excludes PdfPaths. /// /// +#if NET6_0_OR_GREATER + [RequiresUnreferencedCode("Members from PageXmlDocument may be trimmed if not referenced directly")] +#endif public string Get(Page page) { return Get(page, false); @@ -115,6 +119,9 @@ /// /// /// Draw PdfPaths present in the page. +#if NET6_0_OR_GREATER + [RequiresUnreferencedCode("Members from PageXmlDocument may be trimmed if not referenced directly")] +#endif public string Get(Page page, bool includePaths) { PageXmlData data = new PageXmlData(); @@ -373,6 +380,9 @@ }; } +#if NET6_0_OR_GREATER + [RequiresUnreferencedCode("Members from PageXmlDocument may be trimmed if not referenced directly")] +#endif private string Serialize(PageXmlDocument pageXmlDocument) { XmlSerializer serializer = new XmlSerializer(typeof(PageXmlDocument)); @@ -395,6 +405,9 @@ /// /// Deserialize an from a given PAGE format XML document. /// +#if NET6_0_OR_GREATER + [RequiresUnreferencedCode("Members from PageXmlDocument may be trimmed if not referenced directly")] +#endif public static PageXmlDocument Deserialize(string xmlPath) { XmlSerializer serializer = new XmlSerializer(typeof(PageXmlDocument)); diff --git a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/SvgTextExporter.cs b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/SvgTextExporter.cs index 03811df1..1c5ac654 100644 --- a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/SvgTextExporter.cs +++ b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/SvgTextExporter.cs @@ -2,6 +2,7 @@ { using System; using System.Collections.Generic; + using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text; using System.Xml; @@ -65,6 +66,9 @@ /// /// Get the page contents as an SVG. /// +#if NET6_0_OR_GREATER + [RequiresUnreferencedCode("'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.")] +#endif public string Get(Page page) { var builder = new StringBuilder($"\n\n"); diff --git a/src/UglyToad.PdfPig.sln b/src/UglyToad.PdfPig.sln index 444459e4..1ad22d80 100644 --- a/src/UglyToad.PdfPig.sln +++ b/src/UglyToad.PdfPig.sln @@ -10,6 +10,7 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C55738D2-3165-4D03-9CE2-10A2E2EEC465}" ProjectSection(SolutionItems) = preProject .editorconfig = .editorconfig + Directory.Build.targets = Directory.Build.targets EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UglyToad.PdfPig.Fonts", "UglyToad.PdfPig.Fonts\UglyToad.PdfPig.Fonts.csproj", "{BBC8F94C-6E94-43FF-AB2E-47FF3C2B999F}"