Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CoreGraphics] Make P/Invokes in CGPDFDictionary have blittable signatures. #20274

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 48 additions & 24 deletions src/CoreGraphics/CGPDFDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#nullable enable

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using Foundation;
Expand Down Expand Up @@ -77,101 +78,121 @@ public int Count {
// CGPDFBoolean -> unsigned char -> CGPDFObject.h

[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool CGPDFDictionaryGetBoolean (/* CGPDFDictionaryRef */ IntPtr dict, /* const char* */ IntPtr key, /* CGPDFBoolean* */ [MarshalAs (UnmanagedType.I1)] out bool value);
unsafe extern static byte CGPDFDictionaryGetBoolean (/* CGPDFDictionaryRef */ IntPtr dict, /* const char* */ IntPtr key, /* CGPDFBoolean* */ byte* value);

public bool GetBoolean (string key, out bool result)
{
if (key is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (key));
using var keyPtr = new TransientString (key);
return CGPDFDictionaryGetBoolean (Handle, keyPtr, out result);
byte byteresult;
unsafe {
var rv = CGPDFDictionaryGetBoolean (Handle, keyPtr, &byteresult) != 0;
result = byteresult != 0;
return rv;
}
}

// CGPDFInteger -> long int so 32/64 bits -> CGPDFObject.h

[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool CGPDFDictionaryGetInteger (/* CGPDFDictionaryRef */ IntPtr dict, /* const char* */ IntPtr key, /* CGPDFInteger* */ out nint value);
unsafe extern static byte CGPDFDictionaryGetInteger (/* CGPDFDictionaryRef */ IntPtr dict, /* const char* */ IntPtr key, /* CGPDFInteger* */ nint* value);

public bool GetInt (string key, out nint result)
{
if (key is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (key));
using var keyPtr = new TransientString (key);
return CGPDFDictionaryGetInteger (Handle, keyPtr, out result);
result = default;
unsafe {
return CGPDFDictionaryGetInteger (Handle, keyPtr, (nint*) Unsafe.AsPointer<nint> (ref result)) != 0;
}
}

// CGPDFReal -> CGFloat -> CGPDFObject.h

[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool CGPDFDictionaryGetNumber (/* CGPDFDictionaryRef */ IntPtr dict, /* const char* */ IntPtr key, /* CGPDFReal* */ out nfloat value);
unsafe extern static byte CGPDFDictionaryGetNumber (/* CGPDFDictionaryRef */ IntPtr dict, /* const char* */ IntPtr key, /* CGPDFReal* */ nfloat* value);

public bool GetFloat (string key, out nfloat result)
{
if (key is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (key));

using var keyPtr = new TransientString (key);
return CGPDFDictionaryGetNumber (Handle, keyPtr, out result);
result = default;
unsafe {
return CGPDFDictionaryGetNumber (Handle, keyPtr, (nfloat*) Unsafe.AsPointer<nfloat> (ref result)) != 0;
}
}

[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool CGPDFDictionaryGetName (/* CGPDFDictionaryRef */ IntPtr dict, /* const char* */ IntPtr key, /* const char ** */ out IntPtr value);
unsafe extern static byte CGPDFDictionaryGetName (/* CGPDFDictionaryRef */ IntPtr dict, /* const char* */ IntPtr key, /* const char ** */ IntPtr* value);

public bool GetName (string key, out string? result)
{
if (key is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (key));
using var keyPtr = new TransientString (key);
var r = CGPDFDictionaryGetName (Handle, keyPtr, out var res);
bool r;
IntPtr res;
unsafe {
r = CGPDFDictionaryGetName (Handle, keyPtr, &res) != 0;
}
result = r ? Marshal.PtrToStringAnsi (res) : null;
return r;
}

[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool CGPDFDictionaryGetDictionary (/* CGPDFDictionaryRef */ IntPtr dict, /* const char* */ IntPtr key, /* CGPDFDictionaryRef* */ out IntPtr result);
unsafe extern static byte CGPDFDictionaryGetDictionary (/* CGPDFDictionaryRef */ IntPtr dict, /* const char* */ IntPtr key, /* CGPDFDictionaryRef* */ IntPtr* result);

public bool GetDictionary (string key, out CGPDFDictionary? result)
{
if (key is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (key));

using var keyPtr = new TransientString (key);
var r = CGPDFDictionaryGetDictionary (Handle, keyPtr, out var res);
IntPtr res;
bool r;
unsafe {
r = CGPDFDictionaryGetDictionary (Handle, keyPtr, &res) != 0;
}
result = r ? new CGPDFDictionary (res) : null;
return r;
}

[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool CGPDFDictionaryGetStream (/* CGPDFDictionaryRef */ IntPtr dict, /* const char* */ IntPtr key, /* CGPDFStreamRef* */ out IntPtr value);
unsafe extern static byte CGPDFDictionaryGetStream (/* CGPDFDictionaryRef */ IntPtr dict, /* const char* */ IntPtr key, /* CGPDFStreamRef* */ IntPtr* value);

public bool GetStream (string key, out CGPDFStream? result)
{
if (key is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (key));

using var keyPtr = new TransientString (key);
var r = CGPDFDictionaryGetStream (Handle, keyPtr, out var ptr);
bool r;
IntPtr ptr;
unsafe {
r = CGPDFDictionaryGetStream (Handle, keyPtr, &ptr) != 0;
}
result = r ? new CGPDFStream (ptr) : null;
return r;
}

[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool CGPDFDictionaryGetArray (/* CGPDFDictionaryRef */ IntPtr dict, /* const char* */ IntPtr key, /* CGPDFArrayRef* */ out IntPtr value);
unsafe extern static byte CGPDFDictionaryGetArray (/* CGPDFDictionaryRef */ IntPtr dict, /* const char* */ IntPtr key, /* CGPDFArrayRef* */ IntPtr* value);

public bool GetArray (string key, out CGPDFArray? array)
{
if (key is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (key));

using var keyPtr = new TransientString (key);
var r = CGPDFDictionaryGetArray (Handle, keyPtr, out var ptr);
bool r;
IntPtr ptr;
unsafe {
r = CGPDFDictionaryGetArray (Handle, keyPtr, &ptr) != 0;
}
array = r ? new CGPDFArray (ptr) : null;
return r;
}
Expand Down Expand Up @@ -256,16 +277,19 @@ public void Apply (Action<string?, CGPDFObject> callback)
// CGPDFDictionary.h

[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool CGPDFDictionaryGetString (/* CGPDFDictionaryRef */ IntPtr dict, /* const char* */ IntPtr key, /* CGPDFStringRef* */ out IntPtr value);
unsafe extern static byte CGPDFDictionaryGetString (/* CGPDFDictionaryRef */ IntPtr dict, /* const char* */ IntPtr key, /* CGPDFStringRef* */ IntPtr* value);

public bool GetString (string key, out string? result)
{
if (key is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (key));

using var keyPtr = new TransientString (key);
var r = CGPDFDictionaryGetString (Handle, keyPtr, out var res);
bool r;
IntPtr res;
unsafe {
r = CGPDFDictionaryGetString (Handle, keyPtr, &res) != 0;
}
result = r ? CGPDFString.ToString (res) : null;
return r;
}
Expand Down
8 changes: 0 additions & 8 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,6 @@ public partial class BlittablePInvokes {
"System.Boolean CoreGraphics.CGDisplay::CGDisplayIsCaptured(System.UInt32)",
"System.Boolean CoreGraphics.CGEventSource::GetButtonState(CoreGraphics.CGEventSourceStateID,CoreGraphics.CGMouseButton)",
"System.Boolean CoreGraphics.CGEventSource::GetKeyState(CoreGraphics.CGEventSourceStateID,System.UInt16)",
"System.Boolean CoreGraphics.CGPDFDictionary::CGPDFDictionaryGetArray(System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.Boolean CoreGraphics.CGPDFDictionary::CGPDFDictionaryGetBoolean(System.IntPtr,System.IntPtr,System.Boolean&)",
"System.Boolean CoreGraphics.CGPDFDictionary::CGPDFDictionaryGetDictionary(System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.Boolean CoreGraphics.CGPDFDictionary::CGPDFDictionaryGetInteger(System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.Boolean CoreGraphics.CGPDFDictionary::CGPDFDictionaryGetName(System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.Boolean CoreGraphics.CGPDFDictionary::CGPDFDictionaryGetNumber(System.IntPtr,System.IntPtr,System.Runtime.InteropServices.NFloat&)",
"System.Boolean CoreGraphics.CGPDFDictionary::CGPDFDictionaryGetStream(System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.Boolean CoreGraphics.CGPDFDictionary::CGPDFDictionaryGetString(System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.Boolean CoreGraphics.CGPDFDocument::CGPDFDocumentAllowsCopying(System.IntPtr)",
"System.Boolean CoreGraphics.CGPDFDocument::CGPDFDocumentAllowsPrinting(System.IntPtr)",
"System.Boolean CoreGraphics.CGPDFDocument::CGPDFDocumentIsEncrypted(System.IntPtr)",
Expand Down
Loading