Skip to content

Commit

Permalink
Activate AOT and cleanup compiler warnings (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
sungaila authored Jun 28, 2024
1 parent 303cc7a commit cff7a4e
Show file tree
Hide file tree
Showing 16 changed files with 241 additions and 88 deletions.
26 changes: 13 additions & 13 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ jobs:
distribution: 'zulu'
- name: Setup Android SDK 29
run: C:\Android\android-sdk\cmdline-tools\latest\bin\sdkmanager "platforms;android-29"
- name: Setup Windows 11 SDK (10.0.22621.0)
- name: Setup Windows 11 SDK (10.0.26100.0)
uses: GuillaumeFalourd/setup-windows10-sdk-action@main
with:
sdk-version: 22621
sdk-version: 26100
- name: Restore
run: msbuild src/PDFtoImage.Build.slnf /t:restore
- name: Build
Expand All @@ -115,7 +115,7 @@ jobs:
with:
subject-path: src/PDFtoImage/bin/${{ github.event_name != 'workflow_dispatch' && 'Debug' || inputs.build_configuration }}/*.nupkg
- name: Publish libraries
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@main
with:
name: Library assemblies
path: |
Expand All @@ -124,7 +124,7 @@ jobs:
!**/*.snupkg
if-no-files-found: error
- name: Publish NuGet packages
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@main
with:
name: NuGet packages
path: |
Expand All @@ -135,7 +135,7 @@ jobs:
shell: pwsh
run: Compress-Archive -Path src/Tests/bin/${{ github.event_name != 'workflow_dispatch' && 'Debug' || inputs.build_configuration }}/* -DestinationPath TestAssemblies.zip -CompressionLevel "Fastest"
- name: Publish tests
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@main
if: success() && (github.event_name != 'workflow_dispatch' && true || inputs.run_tests) == true
with:
name: Test assemblies
Expand All @@ -144,7 +144,7 @@ jobs:
retention-days: 1
compression-level: 0
- name: Publish test project MonoConsole
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@main
if: success() && (github.event_name != 'workflow_dispatch' && true || inputs.run_tests) == true
with:
name: MonoConsole
Expand Down Expand Up @@ -177,7 +177,7 @@ jobs:
with:
dotnet-version: 8.x
- name: Download test project MonoConsole
uses: actions/download-artifact@v4
uses: actions/download-artifact@main
with:
name: MonoConsole
path: MonoConsole
Expand All @@ -192,7 +192,7 @@ jobs:
run: mono MonoConsole/net481/PDFtoImage.FrameworkTests.MonoConsole.exe
- name: Download test assemblies
if: success() || failure()
uses: actions/download-artifact@v4
uses: actions/download-artifact@main
with:
name: Test assemblies
- name: Expand tests
Expand All @@ -219,15 +219,15 @@ jobs:
run: dotnet test net8.0/*.Tests.dll --logger trx --verbosity detailed --results-directory "${{ matrix.os }}/TestResults" ${{ (github.event_name == 'workflow_dispatch' && inputs.generate_assets) == true && '--settings net8.0/SaveOutputInGeneratedFolder.runsettings' || '' }}
- name: Upload test results
if: success() || failure()
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@main
with:
name: Test results (${{ matrix.os }})
path: ./**/*.trx
if-no-files-found: error
retention-days: 1
- name: Upload generated assets
if: (success() || failure()) && (github.event_name == 'workflow_dispatch' && inputs.generate_assets) == true
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@main
with:
name: Generated assets (${{ matrix.os }})
path: ./**/Assets/Generated
Expand All @@ -246,7 +246,7 @@ jobs:
if: (success() || failure()) && (github.event_name != 'workflow_dispatch' && true || inputs.publish_testresults) == true
steps:
- name: Download Artifacts
uses: actions/download-artifact@v4
uses: actions/download-artifact@main
with:
pattern: Test results (*)
merge-multiple: true
Expand Down Expand Up @@ -307,14 +307,14 @@ jobs:
- name: Setup Android SDK 29
run: C:\Android\android-sdk\cmdline-tools\latest\bin\sdkmanager "platforms;android-29"
- name: Cache SonarCloud packages
uses: actions/cache@v3
uses: actions/cache@main
with:
path: ~\sonar\cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache SonarCloud scanner
id: cache-sonar-scanner
uses: actions/cache@v3
uses: actions/cache@main
with:
path: .\.sonar\scanner
key: ${{ runner.os }}-sonar-scanner
Expand Down
6 changes: 6 additions & 0 deletions src/PDFtoImage/Internals/IsExternalInit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#if NETSTANDARD || MONOANDROID || NETFRAMEWORK
namespace System.Runtime.CompilerServices
{
internal static class IsExternalInit { }

Check warning on line 4 in src/PDFtoImage/Internals/IsExternalInit.cs

View workflow job for this annotation

GitHub Actions / SonarCloud

Remove this empty class, write its code or make it an "interface". (https://rules.sonarsource.com/csharp/RSPEC-2094)
}
#endif
23 changes: 18 additions & 5 deletions src/PDFtoImage/Internals/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

namespace PDFtoImage.Internals
{
#if NET6_0_OR_GREATER
#pragma warning disable CA2101 // Specify marshalling for P/Invoke string arguments
#pragma warning disable SYSLIB1054 // Use LibraryImportAttribute instead of DllImportAttribute to generate p/invoke marshalling code at compile time.
#endif
internal static partial class NativeMethods
{
// Interned strings are cached over AppDomains. This means that when we
Expand Down Expand Up @@ -379,7 +375,18 @@ private static partial class Imports
[LibraryImport("pdfium")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void FPDF_RemoveFormFieldHighlight(IntPtr form);

[LibraryImport("pdfium", StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial IntPtr FPDF_LoadCustomDocument(FPDF_FILEACCESS access, string? password);

[LibraryImport("pdfium")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial IntPtr FPDFDOC_InitFormFillEnvironment(IntPtr document, FPDF_FORMFILLINFO formInfo);
#else
#if NET7_0_OR_GREATER
#pragma warning disable SYSLIB1054
#endif
[DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)]
public static extern void FPDF_InitLibrary();

Check warning on line 391 in src/PDFtoImage/Internals/NativeMethods.cs

View workflow job for this annotation

GitHub Actions / SonarCloud

Rename this method to not shadow the outer class' member with the same name. (https://rules.sonarsource.com/csharp/RSPEC-3218)

Expand Down Expand Up @@ -451,13 +458,19 @@ private static partial class Imports

[DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)]
public static extern void FPDF_RemoveFormFieldHighlight(IntPtr form);
#endif

[DllImport("pdfium", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
#if NET6_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA2101:Specify marshalling for P/Invoke string arguments")]
#endif
public static extern IntPtr FPDF_LoadCustomDocument(FPDF_FILEACCESS access, string? password);

[DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr FPDFDOC_InitFormFillEnvironment(IntPtr document, FPDF_FORMFILLINFO formInfo);
#if NET7_0_OR_GREATER
#pragma warning restore SYSLIB1054
#endif
#endif
}

[StructLayout(LayoutKind.Sequential)]
Expand Down
11 changes: 3 additions & 8 deletions src/PDFtoImage/Internals/PdfDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@

namespace PDFtoImage.Internals
{
#if NET8_0_OR_GREATER
#pragma warning disable CA1510 // Use ArgumentNullException throw helper
#pragma warning disable CA1513 // Use ObjectDisposedException throw helper
#endif
/// <summary>
/// Provides functionality to render a PDF document.
/// </summary>
Expand All @@ -29,10 +25,9 @@ internal struct PdfDocument : IDisposable
/// <param name="disposeStream">Decides if <paramref name="stream"/> will closed on dispose as well.</param>
public static PdfDocument Load(Stream stream, string? password, bool disposeStream)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));

return new PdfDocument(stream, password, disposeStream);
return stream != null
? new PdfDocument(stream, password, disposeStream)
: throw new ArgumentNullException(nameof(stream));
}

/// <summary>
Expand Down
65 changes: 26 additions & 39 deletions src/PDFtoImage/Internals/PdfFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@

namespace PDFtoImage.Internals
{
#if NET8_0_OR_GREATER
#pragma warning disable CA1513 // Use ObjectDisposedException throw helper
#pragma warning disable IDE0056 // Use index operator
#pragma warning disable IDE0057 // Use range operator
#endif
internal struct PdfFile : IDisposable
{
private IntPtr _document;
private IntPtr _form;
private bool _disposed;
private GCHandle _formCallbacksHandle;
private readonly GCHandle _formCallbacksHandle;
private readonly int _id;
private Stream? _stream;
private readonly bool _disposeStream;
Expand Down Expand Up @@ -48,8 +43,29 @@ public PdfFile(Stream stream, string? password, bool disposeStream)
if (document == IntPtr.Zero)
throw PdfException.CreateException(NativeMethods.FPDF_GetLastError())!;

LoadDocument(document);
_disposeStream=disposeStream;
_disposeStream = disposeStream;

_document = document;

NativeMethods.FPDF_GetDocPermissions(_document);

var formCallbacks = new NativeMethods.FPDF_FORMFILLINFO();
_formCallbacksHandle = GCHandle.Alloc(formCallbacks, GCHandleType.Pinned);

// Depending on whether XFA support is built into the PDFium library, the version
// needs to be 1 or 2. We don't really care, so we just try one or the other.

for (int i = 1; i <= 2; i++)
{
formCallbacks.version = i;

_form = NativeMethods.FPDFDOC_InitFormFillEnvironment(_document, formCallbacks);
if (_form != IntPtr.Zero)
break;
}

NativeMethods.FPDF_SetFormFieldHighlightColor(_form, 0, 0xFFE4DD);
NativeMethods.FPDF_SetFormFieldHighlightAlpha(_form, 100);
}

public readonly bool RenderPDFPageToBitmap(int pageNumber, IntPtr bitmapHandle, int boundsOriginX, int boundsOriginY, int boundsWidth, int boundsHeight, int rotate, NativeMethods.FPDF flags, bool renderFormFill)
Expand All @@ -70,7 +86,7 @@ public readonly bool RenderPDFPageToBitmap(int pageNumber, IntPtr bitmapHandle,
return true;
}

public List<SizeF> GetPDFDocInfo()
public readonly List<SizeF> GetPDFDocInfo()
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
Expand All @@ -93,38 +109,14 @@ public readonly SizeF GetPDFDocInfo(int pageNumber)
return new SizeF((float)width, (float)height);
}

private void LoadDocument(IntPtr document)
{
_document = document;

NativeMethods.FPDF_GetDocPermissions(_document);

var formCallbacks = new NativeMethods.FPDF_FORMFILLINFO();
_formCallbacksHandle = GCHandle.Alloc(formCallbacks, GCHandleType.Pinned);

// Depending on whether XFA support is built into the PDFium library, the version
// needs to be 1 or 2. We don't really care, so we just try one or the other.

for (int i = 1; i <= 2; i++)
{
formCallbacks.version = i;

_form = NativeMethods.FPDFDOC_InitFormFillEnvironment(_document, formCallbacks);
if (_form != IntPtr.Zero)
break;
}

NativeMethods.FPDF_SetFormFieldHighlightColor(_form, 0, 0xFFE4DD);
NativeMethods.FPDF_SetFormFieldHighlightAlpha(_form, 100);
}

public void Dispose()
{
Dispose(true);

GC.SuppressFinalize(this);
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter")]
private void Dispose(bool disposing)
{
if (_disposed)
Expand All @@ -134,11 +126,6 @@ private void Dispose(bool disposing)

if (_form != IntPtr.Zero)
{
// this call is needed for PDFium builds with PDF_ENABLE_XFA enabled
// and PDF_ENABLE_XFA implies JS support (PDF_ENABLE_V8 is needed for that)
// since we don't ship PDFium with V8 we can skip this
// otherwise we have to deal with some nasty unmanaged memory corruption
//NativeMethods.FPDFDOC_ExitFormFillEnvironment(_form);
_form = IntPtr.Zero;
}

Expand Down
3 changes: 2 additions & 1 deletion src/PDFtoImage/Internals/PdfLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static void EnsureLoaded()
if (_library == null)
LibraryLoader.LoadLocalLibrary<PdfDocument>("pdfium");
#else
// .NET (Core) and Xamarin resolve the pdfium lib on their own
// .NET (Core) and Xamarin resolve the pdfium lib on their own
#endif
_library ??= new PdfLibrary();
}
Expand All @@ -32,6 +32,7 @@ private PdfLibrary()
Dispose(disposing: false);
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter")]
private void Dispose(bool disposing)
{
if (!disposedValue)
Expand Down
1 change: 1 addition & 0 deletions src/PDFtoImage/PDFtoImage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Configurations>Debug;Release;ReleaseSigned</Configurations>
<ProjectGuid>{A6F86FA0-87FE-406E-BA86-5A128D59DA12}</ProjectGuid>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
</PropertyGroup>

<!-- NuGet -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
#nullable enable
#nullable enable
PDFtoImage.Resource
PDFtoImage.Resource.Attribute
PDFtoImage.Resource.Resource() -> void
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
#nullable enable
#nullable enable
PDFtoImage.Resource
PDFtoImage.Resource.Attribute
PDFtoImage.Resource.Resource() -> void
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#nullable enable
#nullable enable
PDFtoImage.Resource
PDFtoImage.Resource.Resource() -> void
8 changes: 1 addition & 7 deletions src/PDFtoImage/RenderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,4 @@ public readonly record struct RenderOptions(
/// </summary>
public RenderOptions() : this(300, null, null, false, false, false, PdfRotation.Rotate0, PdfAntiAliasing.All, null, null, false, false) { }
}
}
#if NETSTANDARD || MONOANDROID || NETFRAMEWORK
namespace System.Runtime.CompilerServices
{
internal static class IsExternalInit { }
}
#endif
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit cff7a4e

Please sign in to comment.