Files
RobustToolbox/Robust.Client/Interop/MacOS/CoreFoundation.cs
Pieter-Jan Briers f3a3f564e1 System font API (#5393)
* System font API

This is a new API that allows operating system fonts to be loaded by the engine and used by content.

Fonts are provided in a flat list exposing all the relevant metadata. They are loaded from disk with a Load call.

Initial implementation is only for Windows DirectWrite.

* Load system fonts as memory mapped files if possible.

This allows sharing the font file memory with other processes which is always good.

* Use ArrayPool to reduce char array allocations

* Disable verbose logging

* Implement system font support on Linux via Fontconfig

* Implement macOS support

* Add "FREEDESKTOP" define constant

This is basically LINUX || FREEBSD. Though FreeBSD currently gets detected as LINUX too. Oh well.

* Compile out Fontconfig and CoreText system font backends when not on those platforms

* Don't add Fontconfig package dep on Mac/Windows

* Allow disabling system font support via CVar

Cuz why not.
2025-10-28 22:07:55 +01:00

98 lines
2.9 KiB
C#

#if MACOS
using System.Runtime.InteropServices;
using CFIndex = System.Runtime.InteropServices.CLong;
using Boolean = byte;
namespace Robust.Client.Interop.MacOS;
// ReSharper disable InconsistentNaming
/// <summary>
/// Binding to macOS Core Foundation.
/// </summary>
internal static unsafe class CoreFoundation
{
private const string CoreFoundationLibrary = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation";
public const int kCFNumberFloat32Type = 5;
public static string CFStringToManaged(__CFString* str)
{
var length = CFStringGetLength(str);
return string.Create(
checked((int)length.Value),
(nint)str,
static (span, arg) =>
{
fixed (char* pBuffer = span)
{
CFStringGetCharacters((__CFString*)arg,
new CFRange
{
location = new CFIndex(0),
length = new CFIndex(span.Length),
},
pBuffer);
}
});
}
[DllImport(CoreFoundationLibrary)]
internal static extern void* CFRetain(void* cf);
[DllImport(CoreFoundationLibrary)]
internal static extern void CFRelease(void* cf);
[DllImport(CoreFoundationLibrary)]
internal static extern CFIndex CFArrayGetCount(__CFArray* array);
[DllImport(CoreFoundationLibrary)]
internal static extern void* CFArrayGetValueAtIndex(__CFArray* array, CFIndex index);
[DllImport(CoreFoundationLibrary)]
internal static extern CFIndex CFStringGetLength(__CFString* str);
[DllImport(CoreFoundationLibrary)]
internal static extern void CFStringGetCharacters(__CFString* str, CFRange range, char* buffer);
[DllImport(CoreFoundationLibrary)]
internal static extern Boolean CFURLGetFileSystemRepresentation(
__CFURL* url,
Boolean resolveAgainstBase,
byte* buffer,
CFIndex maxBufLen);
[DllImport(CoreFoundationLibrary)]
internal static extern __CFString* CFURLGetString(__CFURL* url);
[DllImport(CoreFoundationLibrary)]
internal static extern CFIndex CFDictionaryGetCount(__CFDictionary* theDict);
[DllImport(CoreFoundationLibrary)]
internal static extern void* CFDictionaryGetValue(__CFDictionary* theDict, void* key);
[DllImport(CoreFoundationLibrary)]
internal static extern void CFDictionaryGetKeysAndValues(__CFDictionary* theDict, void** keys, void** values);
[DllImport(CoreFoundationLibrary)]
internal static extern void CFNumberGetValue(__CFNumber* number, CLong theType, void* valuePtr);
}
internal struct __CFNumber;
internal struct __CFString;
internal struct __CFURL;
internal struct __CFArray;
internal struct __CFDictionary;
internal struct CFRange
{
public CFIndex location;
public CFIndex length;
}
#endif