Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 22 additions & 1 deletion src/CoreText/CTFontCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,24 @@ public CTFontDescriptor [] GetMatchingFontDescriptors (CTFontCollectionOptions?
return CFArray.ArrayFromHandleFunc (cfArrayRef, fd => new CTFontDescriptor (fd, false), true)!;
}

#if NET
[DllImport (Constants.CoreTextLibrary)]
static unsafe extern IntPtr CTFontCollectionCreateMatchingFontDescriptorsSortedWithCallback (
IntPtr collection, delegate* unmanaged<IntPtr, IntPtr, IntPtr, CFIndex> sortCallback,
IntPtr refCon);
#else
[DllImport (Constants.CoreTextLibrary)]
static extern IntPtr CTFontCollectionCreateMatchingFontDescriptorsSortedWithCallback (
IntPtr collection, CTFontCollectionSortDescriptorsCallback sortCallback, IntPtr refCon);
#endif

delegate CFIndex CTFontCollectionSortDescriptorsCallback (IntPtr first, IntPtr second, IntPtr refCon);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: we don't need this delegate declaration in .NET anymore, so it can be inside #if !NET


#if NET
[UnmanagedCallersOnly]
#else
[MonoPInvokeCallback (typeof (CTFontCollectionSortDescriptorsCallback))]
#endif
static CFIndex CompareDescriptors (IntPtr first, IntPtr second, IntPtr context)
{
GCHandle c = GCHandle.FromIntPtr (context);
Expand All @@ -217,10 +228,20 @@ static CFIndex CompareDescriptors (IntPtr first, IntPtr second, IntPtr context)
{
GCHandle comparison = GCHandle.Alloc (comparer);
try {
var cfArrayRef = CTFontCollectionCreateMatchingFontDescriptorsSortedWithCallback (
IntPtr cfArrayRef;
#if NET
unsafe {
cfArrayRef = CTFontCollectionCreateMatchingFontDescriptorsSortedWithCallback (
Handle,
&CompareDescriptors,
GCHandle.ToIntPtr (comparison));
}
#else
cfArrayRef = CTFontCollectionCreateMatchingFontDescriptorsSortedWithCallback (
Handle,
new CTFontCollectionSortDescriptorsCallback (CompareDescriptors),
GCHandle.ToIntPtr (comparison));
#endif
if (cfArrayRef == IntPtr.Zero)
return Array.Empty<CTFontDescriptor> ();
return CFArray.ArrayFromHandleFunc (cfArrayRef, fd => new CTFontDescriptor (fd, false), true)!;
Expand Down
7 changes: 7 additions & 0 deletions src/CoreText/CTFontDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -740,10 +740,17 @@ public CTFontDescriptor [] GetMatchingFontDescriptors (NSSet? mandatoryAttribute
#else
[Mac (10, 9)]
#endif
#if NET
[DllImport (Constants.CoreTextLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static unsafe extern bool CTFontDescriptorMatchFontDescriptorsWithProgressHandler (IntPtr descriptors, IntPtr mandatoryAttributes,
delegate* unmanaged<CTFontDescriptorMatchingState, IntPtr, bool> progressHandler);
#else
[DllImport (Constants.CoreTextLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool CTFontDescriptorMatchFontDescriptorsWithProgressHandler (IntPtr descriptors, IntPtr mandatoryAttributes,
Func<CTFontDescriptorMatchingState, IntPtr, bool> progressHandler);
#endif

#if NET
[SupportedOSPlatform ("macos")]
Expand Down
39 changes: 39 additions & 0 deletions src/CoreText/CTRunDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ namespace CoreText {
delegate void CTRunDelegateDeallocateCallback (IntPtr refCon);
delegate nfloat CTRunDelegateGetCallback (IntPtr refCon);

#if NET
[StructLayout (LayoutKind.Sequential)]
struct CTRunDelegateCallbacks {
public /* CFIndex */ nint version;
public unsafe delegate* unmanaged<IntPtr, void> dealloc;
public unsafe delegate* unmanaged<IntPtr, nfloat> getAscent;
public unsafe delegate* unmanaged<IntPtr, nfloat> getDescent;
public unsafe delegate* unmanaged<IntPtr, nfloat> getWidth;
}
#else
[StructLayout (LayoutKind.Sequential)]
struct CTRunDelegateCallbacks {
public /* CFIndex */ nint version;
Expand All @@ -54,6 +64,7 @@ struct CTRunDelegateCallbacks {
public CTRunDelegateGetCallback getDescent;
public CTRunDelegateGetCallback getWidth;
}
#endif
#endregion

#if NET
Expand Down Expand Up @@ -128,18 +139,34 @@ public virtual float GetWidth ()
internal CTRunDelegateCallbacks GetCallbacks ()
{
if (!callbacks.HasValue) {
#if NET
unsafe {
callbacks = new CTRunDelegateCallbacks () {
version = 1,
dealloc = &Deallocate,
getAscent = &GetAscent,
getDescent = &GetDescent,
getWidth = &GetWidth,
};
}
#else
callbacks = new CTRunDelegateCallbacks () {
version = 1, // kCTRunDelegateVersion1
dealloc = Deallocate,
getAscent = GetAscent,
getDescent = GetDescent,
getWidth = GetWidth,
};
#endif
}
return callbacks.Value;
}

#if NET
[UnmanagedCallersOnly]
#else
[MonoPInvokeCallback (typeof (CTRunDelegateDeallocateCallback))]
#endif
static void Deallocate (IntPtr refCon)
{
var self = GetOperations (refCon);
Expand All @@ -160,7 +187,11 @@ static void Deallocate (IntPtr refCon)
return c.Target as CTRunDelegateOperations;
}

#if NET
[UnmanagedCallersOnly]
#else
[MonoPInvokeCallback (typeof (CTRunDelegateGetCallback))]
#endif
static nfloat GetAscent (IntPtr refCon)
{
var self = GetOperations (refCon);
Expand All @@ -169,7 +200,11 @@ static nfloat GetAscent (IntPtr refCon)
return (nfloat) self.GetAscent ();
}

#if NET
[UnmanagedCallersOnly]
#else
[MonoPInvokeCallback (typeof (CTRunDelegateGetCallback))]
#endif
static nfloat GetDescent (IntPtr refCon)
{
var self = GetOperations (refCon);
Expand All @@ -178,7 +213,11 @@ static nfloat GetDescent (IntPtr refCon)
return (nfloat) self.GetDescent ();
}

#if NET
[UnmanagedCallersOnly]
#else
[MonoPInvokeCallback (typeof (CTRunDelegateGetCallback))]
#endif
static nfloat GetWidth (IntPtr refCon)
{
var self = GetOperations (refCon);
Expand Down