Skip to content

Commit

Permalink
[CoreText] Make P/Invokes in CTFontManager have blittable signatures. (
Browse files Browse the repository at this point in the history
…#20464)

Contributes towards #15684.
  • Loading branch information
rolfbjarne committed Apr 22, 2024
1 parent 417236e commit 216ddf7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 45 deletions.
77 changes: 43 additions & 34 deletions src/CoreText/CTFontManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ public partial class CTFontManager {

#if MONOMAC
[DllImport (Constants.CoreTextLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool CTFontManagerIsSupportedFont (IntPtr url);
static extern byte CTFontManagerIsSupportedFont (IntPtr url);

#if NET
[UnsupportedOSPlatform ("maccatalyst")]
Expand All @@ -114,7 +113,7 @@ public static bool IsFontSupported (NSUrl url)
{
if (url is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (url));
return CTFontManagerIsSupportedFont (url.Handle);
return CTFontManagerIsSupportedFont (url.Handle) != 0;
}
#elif !XAMCORE_3_0
#if NET
Expand All @@ -134,8 +133,7 @@ public static bool IsFontSupported (NSUrl url)
#endif

[DllImport (Constants.CoreTextLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool CTFontManagerRegisterFontsForURL (IntPtr fontUrl, CTFontManagerScope scope, ref IntPtr error);
unsafe static extern byte CTFontManagerRegisterFontsForURL (IntPtr fontUrl, CTFontManagerScope scope, IntPtr* error);
public static NSError? RegisterFontsForUrl (NSUrl fontUrl, CTFontManagerScope scope)
{
if (fontUrl is null)
Expand All @@ -144,7 +142,11 @@ public static bool IsFontSupported (NSUrl url)
IntPtr error = IntPtr.Zero;

try {
if (CTFontManagerRegisterFontsForURL (fontUrl.Handle, scope, ref error))
bool rv;
unsafe {
rv = CTFontManagerRegisterFontsForURL (fontUrl.Handle, scope, &error) != 0;
}
if (rv)
return null;
else
return Runtime.GetNSObject<NSError> (error);
Expand Down Expand Up @@ -193,8 +195,7 @@ static NSArray EnsureNonNullArray (object [] items, string name)
[Deprecated (PlatformName.TvOS, 13, 0)]
#endif
[DllImport (Constants.CoreTextLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool CTFontManagerRegisterFontsForURLs (IntPtr arrayRef, CTFontManagerScope scope, ref IntPtr error_array);
unsafe static extern byte CTFontManagerRegisterFontsForURLs (IntPtr arrayRef, CTFontManagerScope scope, IntPtr* error_array);

#if NET
[SupportedOSPlatform ("ios")]
Expand All @@ -214,8 +215,10 @@ static NSArray EnsureNonNullArray (object [] items, string name)
{
using (var arr = EnsureNonNullArray (fontUrls, nameof (fontUrls))) {
IntPtr error_array = IntPtr.Zero;
if (CTFontManagerRegisterFontsForURLs (arr.Handle, scope, ref error_array))
return null;
unsafe {
if (CTFontManagerRegisterFontsForURLs (arr.Handle, scope, &error_array) != 0)
return null;
}
return ArrayFromHandle<NSError> (error_array, releaseAfterUse: true);
}
}
Expand Down Expand Up @@ -261,7 +264,7 @@ static unsafe byte TrampolineRegistrationHandler (IntPtr block, /* NSArray */ In
[iOS (13, 0)]
#endif
[DllImport (Constants.CoreTextLibrary)]
unsafe static extern void CTFontManagerRegisterFontURLs (/* CFArrayRef */ IntPtr fontUrls, CTFontManagerScope scope, [MarshalAs (UnmanagedType.I1)] bool enabled, BlockLiteral* registrationHandler);
unsafe static extern void CTFontManagerRegisterFontURLs (/* CFArrayRef */ IntPtr fontUrls, CTFontManagerScope scope, byte enabled, BlockLiteral* registrationHandler);

#if NET
[SupportedOSPlatform ("tvos13.0")]
Expand All @@ -279,7 +282,7 @@ public static void RegisterFonts (NSUrl [] fontUrls, CTFontManagerScope scope, b
using (var arr = EnsureNonNullArray (fontUrls, nameof (fontUrls))) {
if (registrationHandler is null) {
unsafe {
CTFontManagerRegisterFontURLs (arr.Handle, scope, enabled, null);
CTFontManagerRegisterFontURLs (arr.Handle, scope, enabled.AsByte (), null);
}
} else {
unsafe {
Expand All @@ -290,15 +293,14 @@ public static void RegisterFonts (NSUrl [] fontUrls, CTFontManagerScope scope, b
using var block = new BlockLiteral ();
block.SetupBlockUnsafe (callback, registrationHandler);
#endif
CTFontManagerRegisterFontURLs (arr.Handle, scope, enabled, &block);
CTFontManagerRegisterFontURLs (arr.Handle, scope, enabled.AsByte (), &block);
}
}
}
}

[DllImport (Constants.CoreTextLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool CTFontManagerUnregisterFontsForURL (IntPtr fotUrl, CTFontManagerScope scope, ref IntPtr error);
unsafe static extern byte CTFontManagerUnregisterFontsForURL (IntPtr fotUrl, CTFontManagerScope scope, IntPtr* error);

public static NSError? UnregisterFontsForUrl (NSUrl fontUrl, CTFontManagerScope scope)
{
Expand All @@ -308,7 +310,11 @@ public static void RegisterFonts (NSUrl [] fontUrls, CTFontManagerScope scope, b
IntPtr error = IntPtr.Zero;

try {
if (CTFontManagerUnregisterFontsForURL (fontUrl.Handle, scope, ref error))
bool rv;
unsafe {
rv = CTFontManagerUnregisterFontsForURL (fontUrl.Handle, scope, &error) != 0;
}
if (rv)
return null;
else
return Runtime.GetNSObject<NSError> (error);
Expand All @@ -333,8 +339,7 @@ public static void RegisterFonts (NSUrl [] fontUrls, CTFontManagerScope scope, b
[Deprecated (PlatformName.TvOS, 13, 0)]
#endif
[DllImport (Constants.CoreTextLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool CTFontManagerUnregisterFontsForURLs (IntPtr arrayRef, CTFontManagerScope scope, ref IntPtr error_array);
unsafe static extern byte CTFontManagerUnregisterFontsForURLs (IntPtr arrayRef, CTFontManagerScope scope, IntPtr* error_array);

#if NET
[SupportedOSPlatform ("ios")]
Expand All @@ -354,8 +359,10 @@ public static void RegisterFonts (NSUrl [] fontUrls, CTFontManagerScope scope, b
{
IntPtr error_array = IntPtr.Zero;
using (var arr = EnsureNonNullArray (fontUrls, nameof (fontUrls))) {
if (CTFontManagerUnregisterFontsForURLs (arr.Handle, scope, ref error_array))
return null;
unsafe {
if (CTFontManagerUnregisterFontsForURLs (arr.Handle, scope, &error_array) != 0)
return null;
}
return ArrayFromHandle<NSError> (error_array, releaseAfterUse: true);
}
}
Expand Down Expand Up @@ -436,8 +443,7 @@ public unsafe static void UnregisterFonts (NSUrl [] fontUrls, CTFontManagerScope
}

[DllImport (Constants.CoreTextLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool CTFontManagerRegisterGraphicsFont (IntPtr cgfont, out IntPtr error);
unsafe static extern byte CTFontManagerRegisterGraphicsFont (IntPtr cgfont, IntPtr* error);

public static bool RegisterGraphicsFont (CGFont font, [NotNullWhen (true)] out NSError? error)
{
Expand All @@ -446,7 +452,9 @@ public static bool RegisterGraphicsFont (CGFont font, [NotNullWhen (true)] out N
IntPtr h = IntPtr.Zero;
bool ret;
try {
ret = CTFontManagerRegisterGraphicsFont (font.Handle, out h);
unsafe {
ret = CTFontManagerRegisterGraphicsFont (font.Handle, &h) != 0;
}
if (ret)
error = null;
else
Expand All @@ -459,8 +467,7 @@ public static bool RegisterGraphicsFont (CGFont font, [NotNullWhen (true)] out N
}

[DllImport (Constants.CoreTextLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool CTFontManagerUnregisterGraphicsFont (IntPtr cgfont, out IntPtr error);
unsafe static extern byte CTFontManagerUnregisterGraphicsFont (IntPtr cgfont, IntPtr* error);

public static bool UnregisterGraphicsFont (CGFont font, out NSError? error)
{
Expand All @@ -469,7 +476,9 @@ public static bool UnregisterGraphicsFont (CGFont font, out NSError? error)
IntPtr h = IntPtr.Zero;
bool ret;
try {
ret = CTFontManagerUnregisterGraphicsFont (font.Handle, out h);
unsafe {
ret = CTFontManagerUnregisterGraphicsFont (font.Handle, &h) != 0;
}
if (ret)
error = null;
else
Expand Down Expand Up @@ -537,7 +546,7 @@ public static NSObject ObserveRegisteredFontsChanged (EventHandler<NSNotificatio
[iOS (13, 0)]
#endif
[DllImport (Constants.CoreTextLibrary)]
static extern unsafe void CTFontManagerRegisterFontDescriptors (/* CFArrayRef */ IntPtr fontDescriptors, CTFontManagerScope scope, [MarshalAs (UnmanagedType.I1)] bool enabled, BlockLiteral* registrationHandler);
static extern unsafe void CTFontManagerRegisterFontDescriptors (/* CFArrayRef */ IntPtr fontDescriptors, CTFontManagerScope scope, byte enabled, BlockLiteral* registrationHandler);

#if NET
[SupportedOSPlatform ("tvos13.0")]
Expand All @@ -554,7 +563,7 @@ public unsafe static void RegisterFontDescriptors (CTFontDescriptor [] fontDescr
{
using (var arr = EnsureNonNullArray (fontDescriptors, nameof (fontDescriptors))) {
if (registrationHandler is null) {
CTFontManagerRegisterFontDescriptors (arr.Handle, scope, enabled, null);
CTFontManagerRegisterFontDescriptors (arr.Handle, scope, enabled.AsByte (), null);
} else {
#if NET
delegate* unmanaged<IntPtr, IntPtr, byte, byte> trampoline = &TrampolineRegistrationHandler;
Expand All @@ -563,7 +572,7 @@ public unsafe static void RegisterFontDescriptors (CTFontDescriptor [] fontDescr
using var block = new BlockLiteral ();
block.SetupBlockUnsafe (callback, registrationHandler);
#endif
CTFontManagerRegisterFontDescriptors (arr.Handle, scope, enabled, &block);
CTFontManagerRegisterFontDescriptors (arr.Handle, scope, enabled.AsByte (), &block);
}
}
}
Expand Down Expand Up @@ -620,7 +629,7 @@ public unsafe static void UnregisterFontDescriptors (CTFontDescriptor [] fontDes
[iOS (13,0)]
#endif
[DllImport (Constants.CoreTextLibrary)]
static extern /* CFArrayRef */ IntPtr CTFontManagerCopyRegisteredFontDescriptors (CTFontManagerScope scope, [MarshalAs (UnmanagedType.I1)] bool enabled);
static extern /* CFArrayRef */ IntPtr CTFontManagerCopyRegisteredFontDescriptors (CTFontManagerScope scope, byte enabled);

#if NET
[SupportedOSPlatform ("ios13.0")]
Expand All @@ -635,7 +644,7 @@ public unsafe static void UnregisterFontDescriptors (CTFontDescriptor [] fontDes
#endif
public static CTFontDescriptor[]? GetRegisteredFontDescriptors (CTFontManagerScope scope, bool enabled)
{
var p = CTFontManagerCopyRegisteredFontDescriptors (scope, enabled);
var p = CTFontManagerCopyRegisteredFontDescriptors (scope, enabled.AsByte ());
// Copy/Create rule - we must release the CFArrayRef
return ArrayFromHandle<CTFontDescriptor> (p, releaseAfterUse: true);
}
Expand Down Expand Up @@ -702,7 +711,7 @@ public unsafe static void UnregisterFontDescriptors (CTFontDescriptor [] fontDes
[iOS (13,0)]
#endif
[DllImport (Constants.CoreTextLibrary)]
static extern unsafe void CTFontManagerRegisterFontsWithAssetNames (/* CFArrayRef */ IntPtr fontAssetNames, /* CFBundleRef _Nullable */ IntPtr bundle, CTFontManagerScope scope, [MarshalAs (UnmanagedType.I1)] bool enabled, BlockLiteral* registrationHandler);
static extern unsafe void CTFontManagerRegisterFontsWithAssetNames (/* CFArrayRef */ IntPtr fontAssetNames, /* CFBundleRef _Nullable */ IntPtr bundle, CTFontManagerScope scope, byte enabled, BlockLiteral* registrationHandler);

// reminder that NSBundle and CFBundle are NOT toll-free bridged :(
#if NET
Expand All @@ -721,7 +730,7 @@ public unsafe static void RegisterFonts (string[] assetNames, CFBundle bundle, C
{
using (var arr = EnsureNonNullArray (assetNames, nameof (assetNames))) {
if (registrationHandler is null) {
CTFontManagerRegisterFontsWithAssetNames (arr.Handle, bundle.GetHandle (), scope, enabled, null);
CTFontManagerRegisterFontsWithAssetNames (arr.Handle, bundle.GetHandle (), scope, enabled.AsByte (), null);
} else {
#if NET
delegate* unmanaged<IntPtr, IntPtr, byte, byte> trampoline = &TrampolineRegistrationHandler;
Expand All @@ -730,7 +739,7 @@ public unsafe static void RegisterFonts (string[] assetNames, CFBundle bundle, C
using var block = new BlockLiteral ();
block.SetupBlockUnsafe (callback, registrationHandler);
#endif
CTFontManagerRegisterFontsWithAssetNames (arr.Handle, bundle.GetHandle (), scope, enabled, &block);
CTFontManagerRegisterFontsWithAssetNames (arr.Handle, bundle.GetHandle (), scope, enabled.AsByte (), &block);
}
}
}
Expand Down
11 changes: 0 additions & 11 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,6 @@ public partial class BlittablePInvokes {
"Security.SslStatus Security.SslContext::SSLRead(System.IntPtr,System.Byte*,System.IntPtr,System.IntPtr&)",
"Security.SslStatus Security.SslContext::SSLSetSessionOption(System.IntPtr,Security.SslSessionOption,System.Boolean)",
"Security.SslStatus Security.SslContext::SSLWrite(System.IntPtr,System.Byte*,System.IntPtr,System.IntPtr&)",
"System.Boolean CoreText.CTFontManager::CTFontManagerIsSupportedFont(System.IntPtr)",
"System.Boolean CoreText.CTFontManager::CTFontManagerRegisterFontsForURL(System.IntPtr,CoreText.CTFontManagerScope,System.IntPtr&)",
"System.Boolean CoreText.CTFontManager::CTFontManagerRegisterFontsForURLs(System.IntPtr,CoreText.CTFontManagerScope,System.IntPtr&)",
"System.Boolean CoreText.CTFontManager::CTFontManagerRegisterGraphicsFont(System.IntPtr,System.IntPtr&)",
"System.Boolean CoreText.CTFontManager::CTFontManagerUnregisterFontsForURL(System.IntPtr,CoreText.CTFontManagerScope,System.IntPtr&)",
"System.Boolean CoreText.CTFontManager::CTFontManagerUnregisterFontsForURLs(System.IntPtr,CoreText.CTFontManagerScope,System.IntPtr&)",
"System.Boolean CoreText.CTFontManager::CTFontManagerUnregisterGraphicsFont(System.IntPtr,System.IntPtr&)",
"System.Boolean CoreVideo.CVBuffer::CVBufferHasAttachment(System.IntPtr,System.IntPtr)",
"System.Boolean CoreVideo.CVDisplayLink::CVDisplayLinkIsRunning(System.IntPtr)",
"System.Boolean CoreVideo.CVImageBuffer::CVImageBufferIsFlipped(System.IntPtr)",
Expand Down Expand Up @@ -267,7 +260,6 @@ public partial class BlittablePInvokes {
"System.Int32 Security.SecCertificate::SecCertificateCopyEmailAddresses(System.IntPtr,System.IntPtr&)",
"System.Int32 Security.SslContext::SSLCopyALPNProtocols(System.IntPtr,System.IntPtr&)",
"System.Int32 Security.SslContext::SSLSetSessionTicketsEnabled(System.IntPtr,System.Boolean)",
"System.IntPtr CoreText.CTFontManager::CTFontManagerCopyRegisteredFontDescriptors(CoreText.CTFontManagerScope,System.Boolean)",
"System.IntPtr CoreVideo.CVBuffer::CVBufferCopyAttachment(System.IntPtr,System.IntPtr,CoreVideo.CVAttachmentMode&)",
"System.IntPtr CoreVideo.CVBuffer::CVBufferGetAttachment(System.IntPtr,System.IntPtr,CoreVideo.CVAttachmentMode&)",
"System.IntPtr Foundation.NSSearchPath::NSSearchPathForDirectoriesInDomains(System.UIntPtr,System.UIntPtr,System.Boolean)",
Expand All @@ -289,9 +281,6 @@ public partial class BlittablePInvokes {
"System.IntPtr Security.SecKey::SecKeyCreateSignature(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.IntPtr Security.SecKey::SecKeyCreateWithData(System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.IntPtr Security.SecPolicy::SecPolicyCreateSSL(System.Boolean,System.IntPtr)",
"System.Void CoreText.CTFontManager::CTFontManagerRegisterFontDescriptors(System.IntPtr,CoreText.CTFontManagerScope,System.Boolean,ObjCRuntime.BlockLiteral*)",
"System.Void CoreText.CTFontManager::CTFontManagerRegisterFontsWithAssetNames(System.IntPtr,System.IntPtr,CoreText.CTFontManagerScope,System.Boolean,ObjCRuntime.BlockLiteral*)",
"System.Void CoreText.CTFontManager::CTFontManagerRegisterFontURLs(System.IntPtr,CoreText.CTFontManagerScope,System.Boolean,ObjCRuntime.BlockLiteral*)",
"System.Void CoreVideo.CVPixelBuffer::CVPixelBufferGetExtendedPixels(System.IntPtr,System.UIntPtr&,System.UIntPtr&,System.UIntPtr&,System.UIntPtr&)",
"System.Void Foundation.NSObject::xamarin_release_managed_ref(System.IntPtr,System.Boolean)",
"System.Void Network.NWAdvertiseDescriptor::nw_advertise_descriptor_set_no_auto_rename(System.IntPtr,System.Boolean)",
Expand Down

8 comments on commit 216ddf7

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.