Skip to content

Commit

Permalink
Enable trimming and AOT (#939)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
madelson and Numpsy authored Nov 20, 2024
1 parent 8ca5399 commit 7db347e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project>
<PropertyGroup Condition="'$(IsTestProject)' != 'true'">
<IsTrimmable Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</IsTrimmable>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">true</IsAotCompatible>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Content;
using DocumentLayoutAnalysis;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Xml;
Expand Down Expand Up @@ -94,6 +95,9 @@ private AltoXmlTextExporter(IWordExtractor wordExtractor, IPageSegmenter pageSeg
/// </summary>
/// <param name="document">The document to extract page layouts from.</param>
/// <param name="includePaths">Draw PdfPaths present in the page.</param>
#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");
Expand All @@ -105,13 +109,19 @@ public string Get(PdfDocument document, bool includePaths = false)
/// Get the Alto (XML) string of the page layout. Excludes <see cref="T:UglyToad.PdfPig.Geometry.PdfSubpath" />s.
/// </summary>
/// <param name="page">The page to export the XML layout for.</param>
#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);

/// <summary>
/// Get the Alto (XML) string of the page layout.
/// </summary>
/// <param name="page">The page to export the XML layout for.</param>
/// <param name="includePaths">Whether the output should include the PdfPaths present in the page.</param>
#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");
Expand Down Expand Up @@ -355,6 +365,9 @@ private AltoDocument.AltoDescription GetAltoDescription(string fileName)
};
}

#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));
Expand All @@ -377,6 +390,9 @@ private string Serialize(AltoDocument altoDocument)
/// <summary>
/// Deserialize an <see cref="AltoDocument"/> from a given Alto format XML document.
/// </summary>
#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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -118,6 +119,9 @@ public string Get(PdfDocument document, bool includePaths = false, bool useHocrj
/// Get the hOCR (HTML) string of the page layout. Excludes PdfPaths.
/// </summary>
/// <param name="page">The page.</param>
#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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace UglyToad.PdfPig.DocumentLayoutAnalysis.Export
{
using Content;
using System.Diagnostics.CodeAnalysis;

/// <summary>
/// Exports the page's text into the desired format.
Expand All @@ -12,6 +13,9 @@ public interface ITextExporter
/// </summary>
/// <param name="page">The page to convert to the format.</param>
/// <returns>The <see langword="string"/> containing the page contents represented in a compatible format.</returns>
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.")]
#endif
string Get(Page page);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -105,6 +106,9 @@ public string Get(PdfDocument document, bool includePaths = false)
/// Get the PAGE-XML (XML) string of the pages layout. Excludes PdfPaths.
/// </summary>
/// <param name="page"></param>
#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);
Expand All @@ -115,6 +119,9 @@ public string Get(Page page)
/// </summary>
/// <param name="page"></param>
/// <param name="includePaths">Draw PdfPaths present in the page.</param>
#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();
Expand Down Expand Up @@ -373,6 +380,9 @@ private PageXmlDocument.PageXmlGlyph ToPageXmlGlyph(Letter letter, PageXmlData d
};
}

#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));
Expand All @@ -395,6 +405,9 @@ private string Serialize(PageXmlDocument pageXmlDocument)
/// <summary>
/// Deserialize an <see cref="PageXmlDocument"/> from a given PAGE format XML document.
/// </summary>
#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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Xml;
Expand Down Expand Up @@ -65,6 +66,9 @@ private SvgTextExporter(InvalidCharStrategy invalidCharacterStrategy, Func<strin
/// <summary>
/// Get the page contents as an SVG.
/// </summary>
#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($"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width='{Math.Round(page.Width, Rounding)}' height='{Math.Round(page.Height, Rounding)}'>\n<g transform=\"scale(1, 1) translate(0, 0)\">\n");
Expand Down
1 change: 1 addition & 0 deletions src/UglyToad.PdfPig.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit 7db347e

Please sign in to comment.