Skip to content

Commit

Permalink
Use SourceGenerator for native code of PdfInfo.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Jun 14, 2024
1 parent fc0e20e commit 6f1b8b8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ private static void GenerateCode(SourceProductionContext context, NativeInteropI
codeBuilder.AppendLine("using System.Runtime.InteropServices;");

codeBuilder.AppendLine();
codeBuilder.AppendLine("namespace ImageMagick;");
codeBuilder.Append("namespace ");
codeBuilder.Append(info.Namespace);
codeBuilder.AppendLine(";");
codeBuilder.AppendLine();

if (info.UsesQuantumType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public NativeInteropInfo(SemanticModel semanticModel, SyntaxNode syntaxNode)
var parentClass = (ClassDeclarationSyntax)_class.Parent!;
ParentClassName = parentClass.Identifier.Text;
IsInternal = parentClass.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.InternalKeyword));
Namespace = ((FileScopedNamespaceDeclarationSyntax)parentClass.Parent!).Name.ToString();

var nativeInteropAttribute = _class.AttributeLists
.SelectMany(list => list.Attributes)
Expand Down Expand Up @@ -63,6 +64,8 @@ public string ClassName

public List<MethodInfo> Methods { get; }

public string Namespace { get; }

public bool NativeToManaged { get; }

public string ParentClassName { get; }
Expand Down
3 changes: 1 addition & 2 deletions src/Magick.NET/Formats/Pdf/PdfInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public static PdfInfo Create(string fileName, string password)

Throw.IfNull(nameof(password), password);

var nativePdfInfo = new NativePdfInfo();
var pageCount = nativePdfInfo.PageCount(filePath, password);
var pageCount = NativePdfInfo.PageCount(filePath, password);
if (pageCount == 0)
throw new MagickErrorException("Unable to determine the page count.");

Expand Down
65 changes: 6 additions & 59 deletions src/Magick.NET/Native/Formats/Pdf/PdfInfo.cs
Original file line number Diff line number Diff line change
@@ -1,70 +1,17 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.
// <auto-generated/>
#nullable enable

using System;
using System.Security;
using System.Runtime.InteropServices;
using ImageMagick.SourceGenerator;

namespace ImageMagick.Formats;

/// <content />
public partial class PdfInfo
{
[SuppressUnmanagedCodeSecurity]
private static unsafe class NativeMethods
[NativeInterop]
private partial class NativePdfInfo
{
#if PLATFORM_x64 || PLATFORM_AnyCPU
public static class X64
{
[DllImport(NativeLibrary.X64Name, CallingConvention = CallingConvention.Cdecl)]
public static extern UIntPtr PdfInfo_PageCount(IntPtr fileName, IntPtr password, out IntPtr exception);
}
#endif
#if PLATFORM_arm64 || PLATFORM_AnyCPU
public static class ARM64
{
[DllImport(NativeLibrary.ARM64Name, CallingConvention = CallingConvention.Cdecl)]
public static extern UIntPtr PdfInfo_PageCount(IntPtr fileName, IntPtr password, out IntPtr exception);
}
#endif
#if PLATFORM_x86 || PLATFORM_AnyCPU
public static class X86
{
[DllImport(NativeLibrary.X86Name, CallingConvention = CallingConvention.Cdecl)]
public static extern UIntPtr PdfInfo_PageCount(IntPtr fileName, IntPtr password, out IntPtr exception);
}
#endif
}
private unsafe sealed partial class NativePdfInfo : NativeHelper
{
static NativePdfInfo() { Environment.Initialize(); }
public int PageCount(string fileName, string password)
{
using var fileNameNative = UTF8Marshaler.CreateInstance(fileName);
using var passwordNative = UTF8Marshaler.CreateInstance(password);
IntPtr exception = IntPtr.Zero;
UIntPtr result;
#if PLATFORM_AnyCPU
if (Runtime.IsArm64)
#endif
#if PLATFORM_arm64 || PLATFORM_AnyCPU
result = NativeMethods.ARM64.PdfInfo_PageCount(fileNameNative.Instance, passwordNative.Instance, out exception);
#endif
#if PLATFORM_AnyCPU
else if (Runtime.Is64Bit)
#endif
#if PLATFORM_x64 || PLATFORM_AnyCPU
result = NativeMethods.X64.PdfInfo_PageCount(fileNameNative.Instance, passwordNative.Instance, out exception);
#endif
#if PLATFORM_AnyCPU
else
#endif
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.PdfInfo_PageCount(fileNameNative.Instance, passwordNative.Instance, out exception);
#endif
CheckException(exception);
return (int)result;
}
[Throws]
public static partial int PageCount(string fileName, string password);
}
}

0 comments on commit 6f1b8b8

Please sign in to comment.