Files
RobustToolbox/Robust.Client/Interop/MacOS/CoreText.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

55 lines
2.6 KiB
C#

#if MACOS
using System.Runtime.InteropServices;
namespace Robust.Client.Interop.MacOS;
// ReSharper disable InconsistentNaming
/// <summary>
/// Binding to macOS Core Text.
/// </summary>
internal static unsafe class CoreText
{
private const string CoreTextLibrary = "/System/Library/Frameworks/CoreText.framework/CoreText";
public static readonly __CFString* kCTFontURLAttribute;
public static readonly __CFString* kCTFontNameAttribute;
public static readonly __CFString* kCTFontDisplayNameAttribute;
public static readonly __CFString* kCTFontFamilyNameAttribute;
public static readonly __CFString* kCTFontStyleNameAttribute;
public static readonly __CFString* kCTFontTraitsAttribute;
public static readonly __CFString* kCTFontWeightTrait;
public static readonly __CFString* kCTFontWidthTrait;
public static readonly __CFString* kCTFontSlantTrait;
static CoreText()
{
var lib = NativeLibrary.Load(CoreTextLibrary);
kCTFontURLAttribute = *(__CFString**)NativeLibrary.GetExport(lib, nameof(kCTFontURLAttribute));
kCTFontNameAttribute = *(__CFString**)NativeLibrary.GetExport(lib, nameof(kCTFontNameAttribute));
kCTFontDisplayNameAttribute = *(__CFString**)NativeLibrary.GetExport(lib, nameof(kCTFontDisplayNameAttribute));
kCTFontFamilyNameAttribute = *(__CFString**)NativeLibrary.GetExport(lib, nameof(kCTFontFamilyNameAttribute));
kCTFontStyleNameAttribute = *(__CFString**)NativeLibrary.GetExport(lib, nameof(kCTFontStyleNameAttribute));
kCTFontTraitsAttribute = *(__CFString**)NativeLibrary.GetExport(lib, nameof(kCTFontTraitsAttribute));
kCTFontWeightTrait = *(__CFString**)NativeLibrary.GetExport(lib, nameof(kCTFontWeightTrait));
kCTFontWidthTrait = *(__CFString**)NativeLibrary.GetExport(lib, nameof(kCTFontWidthTrait));
kCTFontSlantTrait = *(__CFString**)NativeLibrary.GetExport(lib, nameof(kCTFontSlantTrait));
}
[DllImport(CoreTextLibrary)]
public static extern __CTFontCollection* CTFontCollectionCreateFromAvailableFonts(__CFDictionary* options);
[DllImport(CoreTextLibrary)]
public static extern __CFArray* CTFontCollectionCreateMatchingFontDescriptors(__CTFontCollection* collection);
[DllImport(CoreTextLibrary)]
public static extern void* CTFontDescriptorCopyAttribute(__CTFontDescriptor* descriptor, __CFString* attribute);
[DllImport(CoreTextLibrary)]
public static extern __CFDictionary* CTFontDescriptorCopyAttributes(__CTFontDescriptor* descriptor);
}
internal struct __CTFontCollection;
internal struct __CTFontDescriptor;
#endif