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

[CoreText] Make P/Invokes in CTFontDescriptor, CTFramesetter and CTParagraphStyle have blittable signatures. #20426

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
16 changes: 10 additions & 6 deletions src/CoreText/CTFontDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -735,20 +735,24 @@ public CTFontDescriptor [] GetMatchingFontDescriptors (NSSet? mandatoryAttribute
return Runtime.GetNSObject<NSObject> (CTFontDescriptorCopyAttribute (Handle, attribute.Handle), true);
}

[DllImport (Constants.CoreTextLibrary)]
static extern IntPtr CTFontDescriptorCopyLocalizedAttribute (IntPtr descriptor, IntPtr attribute, IntPtr language);
public NSObject? GetLocalizedAttribute (NSString attribute)
{
return Runtime.GetNSObject<NSObject> (CTFontDescriptorCopyLocalizedAttribute (Handle, attribute.Handle, IntPtr.Zero), true);
unsafe {
return Runtime.GetNSObject<NSObject> (CTFontDescriptorCopyLocalizedAttribute (Handle, attribute.Handle, null), true);
}
}

[DllImport (Constants.CoreTextLibrary)]
static extern IntPtr CTFontDescriptorCopyLocalizedAttribute (IntPtr descriptor, IntPtr attribute, out IntPtr language);
unsafe static extern IntPtr CTFontDescriptorCopyLocalizedAttribute (IntPtr descriptor, IntPtr attribute, IntPtr* language);
public NSObject? GetLocalizedAttribute (NSString attribute, out NSString? language)
{
var o = Runtime.GetNSObject<NSObject> (CTFontDescriptorCopyLocalizedAttribute (Handle, attribute.Handle, out var lang), true);
IntPtr handle;
IntPtr lang;
unsafe {
handle = CTFontDescriptorCopyLocalizedAttribute (Handle, attribute.Handle, &lang);
}
language = Runtime.GetNSObject<NSString> (lang, true);
return o;
return Runtime.GetNSObject<NSObject> (handle, true);
}
#endregion
#if NET
Expand Down
16 changes: 10 additions & 6 deletions src/CoreText/CTFramesetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#nullable enable

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

using ObjCRuntime;
Expand Down Expand Up @@ -90,14 +91,17 @@ public CTFramesetter (NSAttributedString value)

#region Frame Sizing
[DllImport (Constants.CoreTextLibrary)]
static extern CGSize CTFramesetterSuggestFrameSizeWithConstraints (
IntPtr framesetter, NSRange stringRange, IntPtr frameAttributes, CGSize constraints, out NSRange fitRange);
unsafe static extern CGSize CTFramesetterSuggestFrameSizeWithConstraints (
IntPtr framesetter, NSRange stringRange, IntPtr frameAttributes, CGSize constraints, NSRange* fitRange);
public CGSize SuggestFrameSize (NSRange stringRange, CTFrameAttributes? frameAttributes, CGSize constraints, out NSRange fitRange)
{
return CTFramesetterSuggestFrameSizeWithConstraints (
Handle, stringRange,
frameAttributes.GetHandle (),
constraints, out fitRange);
fitRange = default;
unsafe {
return CTFramesetterSuggestFrameSizeWithConstraints (
Handle, stringRange,
frameAttributes.GetHandle (),
constraints, (NSRange*) Unsafe.AsPointer<NSRange> (ref fitRange));
}
}
#endregion
#if NET
Expand Down
9 changes: 4 additions & 5 deletions src/CoreText/CTParagraphStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,13 +399,12 @@ public CTParagraphStyle Clone ()

#region Paragraph Style Access
[DllImport (Constants.CoreTextLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern unsafe bool CTParagraphStyleGetValueForSpecifier (IntPtr paragraphStyle, CTParagraphStyleSpecifier spec, nuint valueBufferSize, void* valueBuffer);
static extern unsafe byte CTParagraphStyleGetValueForSpecifier (IntPtr paragraphStyle, CTParagraphStyleSpecifier spec, nuint valueBufferSize, void* valueBuffer);

public unsafe CTTextTab? []? GetTabStops ()
{
IntPtr cfArrayRef;
if (!CTParagraphStyleGetValueForSpecifier (Handle, CTParagraphStyleSpecifier.TabStops, (uint) IntPtr.Size, (void*) &cfArrayRef))
if (CTParagraphStyleGetValueForSpecifier (Handle, CTParagraphStyleSpecifier.TabStops, (uint) IntPtr.Size, (void*) &cfArrayRef) == 0)
throw new InvalidOperationException ("Unable to get property value.");
if (cfArrayRef == IntPtr.Zero)
return Array.Empty<CTTextTab> ();
Expand All @@ -419,7 +418,7 @@ public CTTextAlignment Alignment {
unsafe byte GetByteValue (CTParagraphStyleSpecifier spec)
{
byte value;
if (!CTParagraphStyleGetValueForSpecifier (Handle, spec, sizeof (byte), &value))
if (CTParagraphStyleGetValueForSpecifier (Handle, spec, sizeof (byte), &value) == 0)
throw new InvalidOperationException ("Unable to get property value.");
return value;
}
Expand Down Expand Up @@ -447,7 +446,7 @@ unsafe float GetFloatValue (CTParagraphStyleSpecifier spec)
#endif
{
nfloat value;
if (!CTParagraphStyleGetValueForSpecifier (Handle, spec, (nuint) sizeof (nfloat), &value))
if (CTParagraphStyleGetValueForSpecifier (Handle, spec, (nuint) sizeof (nfloat), &value) == 0)
throw new InvalidOperationException ("Unable to get property value.");
#if NET
return value;
Expand Down
3 changes: 0 additions & 3 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public partial class BlittablePInvokes {
"AVFoundation.AVSampleCursorSyncInfo ObjCRuntime.Messaging::AVSampleCursorSyncInfo_objc_msgSend(System.IntPtr,System.IntPtr)",
"AVFoundation.AVSampleCursorSyncInfo ObjCRuntime.Messaging::AVSampleCursorSyncInfo_objc_msgSendSuper_stret(System.IntPtr,System.IntPtr)",
"AVFoundation.AVSampleCursorSyncInfo ObjCRuntime.Messaging::AVSampleCursorSyncInfo_objc_msgSendSuper(System.IntPtr,System.IntPtr)",
"CoreGraphics.CGSize CoreText.CTFramesetter::CTFramesetterSuggestFrameSizeWithConstraints(System.IntPtr,Foundation.NSRange,System.IntPtr,CoreGraphics.CGSize,Foundation.NSRange&)",
"CoreVideo.CVReturn CoreVideo.CVDisplayLink::CVDisplayLinkCreateWithActiveCGDisplays(System.IntPtr&)",
"CoreVideo.CVReturn CoreVideo.CVDisplayLink::CVDisplayLinkCreateWithCGDisplay(System.UInt32,System.IntPtr&)",
"CoreVideo.CVReturn CoreVideo.CVDisplayLink::CVDisplayLinkCreateWithCGDisplays(System.UInt32[],System.IntPtr,System.IntPtr&)",
Expand Down Expand Up @@ -166,7 +165,6 @@ public partial class BlittablePInvokes {
"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 CoreText.CTParagraphStyle::CTParagraphStyleGetValueForSpecifier(System.IntPtr,CoreText.CTParagraphStyleSpecifier,System.UIntPtr,System.Void*)",
"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 @@ -269,7 +267,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.CTFontDescriptor::CTFontDescriptorCopyLocalizedAttribute(System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.IntPtr CoreText.CTFontManager::CTFontManagerCopyRegisteredFontDescriptors(CoreText.CTFontManagerScope,System.Boolean)",
"System.IntPtr CoreText.CTRunDelegate::CTRunDelegateCreate(CoreText.CTRunDelegateCallbacks&,System.IntPtr)",
"System.IntPtr CoreVideo.CVBuffer::CVBufferCopyAttachment(System.IntPtr,System.IntPtr,CoreVideo.CVAttachmentMode&)",
Expand Down