-
Notifications
You must be signed in to change notification settings - Fork 559
Expand file tree
/
Copy pathCTFontManager.cs
More file actions
690 lines (630 loc) · 27.2 KB
/
CTFontManager.cs
File metadata and controls
690 lines (630 loc) · 27.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
//
// CTFont.cs: Implements the managed CTFont
//
// Authors: Mono Team
// Rolf Bjarne Kvinge <rolf@xamarin.com>
//
// Copyright 2010 Novell, Inc
// Copyright 2011 - 2014 Xamarin Inc
// Copyright 2019 Microsoft Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#nullable enable
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using CoreFoundation;
using CoreGraphics;
using CGGlyph = System.UInt16;
namespace CoreText {
// defined as uint32_t - /System/Library/Frameworks/CoreText.framework/Headers/CTFontManager.h
/// <summary>An enumeration whose values specify the scope for font registration.</summary>
/// <remarks>To be added.</remarks>
public enum CTFontManagerScope : uint {
/// <summary>To be added.</summary>
None = 0,
/// <summary>To be added.</summary>
Process = 1,
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("tvos13.0")]
[SupportedOSPlatform ("maccatalyst")]
[SupportedOSPlatform ("macos")]
Persistent = 2,
/// <summary>To be added.</summary>
[UnsupportedOSPlatform ("maccatalyst")]
[SupportedOSPlatform ("macos")]
[UnsupportedOSPlatform ("ios")]
[UnsupportedOSPlatform ("tvos")]
Session = 3,
}
// defined as uint32_t - /System/Library/Frameworks/CoreText.framework/Headers/CTFontManager.h
/// <summary>An enumeration whose values specify values for auto-activation of fonts.</summary>
/// <remarks>To be added.</remarks>
public enum CTFontManagerAutoActivation : uint {
/// <summary>To be added.</summary>
Default = 0,
/// <summary>To be added.</summary>
Disabled = 1,
/// <summary>To be added.</summary>
Enabled = 2,
/// <summary>Developers should not use this deprecated field. It's now treated as 'Default'.</summary>
[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("maccatalyst")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos")]
[ObsoletedOSPlatform ("ios", "It's now treated as 'Default'.")]
[ObsoletedOSPlatform ("maccatalyst", "It's now treated as 'Default'.")]
[ObsoletedOSPlatform ("tvos", "It's now treated as 'Default'.")]
[ObsoletedOSPlatform ("macos10.13", "It's now treated as 'Default'.")]
PromptUser = 3,
}
/// <summary>Manages the central CoreText Font System.</summary>
/// <remarks>
/// </remarks>
public partial class CTFontManager {
#if MONOMAC
[DllImport (Constants.CoreTextLibrary)]
static extern byte CTFontManagerIsSupportedFont (IntPtr url);
/// <param name="url">To be added.</param>
/// <summary>Developers should not use this deprecated method. </summary>
/// <returns>To be added.</returns>
/// <remarks>To be added.</remarks>
[UnsupportedOSPlatform ("maccatalyst")]
[UnsupportedOSPlatform ("tvos")]
[UnsupportedOSPlatform ("ios")]
[SupportedOSPlatform ("macos")]
[ObsoletedOSPlatform ("macos10.6")]
public static bool IsFontSupported (NSUrl url)
{
if (url is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (url));
bool result = CTFontManagerIsSupportedFont (url.Handle) != 0;
GC.KeepAlive (url);
return result;
}
#endif
[DllImport (Constants.CoreTextLibrary)]
unsafe static extern byte CTFontManagerRegisterFontsForURL (IntPtr fontUrl, CTFontManagerScope scope, IntPtr* error);
/// <param name="fontUrl">To be added.</param>
/// <param name="scope">To be added.</param>
/// <summary>To be added.</summary>
/// <returns>To be added.</returns>
/// <remarks>To be added.</remarks>
public static NSError? RegisterFontsForUrl (NSUrl fontUrl, CTFontManagerScope scope)
{
if (fontUrl is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (fontUrl));
IntPtr error = IntPtr.Zero;
try {
bool rv;
unsafe {
rv = CTFontManagerRegisterFontsForURL (fontUrl.Handle, scope, &error) != 0;
GC.KeepAlive (fontUrl);
}
if (rv)
return null;
else
return Runtime.GetNSObject<NSError> (error);
} finally {
if (error != IntPtr.Zero)
CFObject.CFRelease (error);
}
}
static NSArray EnsureNonNullArray (object [] items, string name)
{
if (items is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (name));
foreach (var i in items)
if (i is null)
throw new ArgumentException ("Array contains a null entry", name);
return NSArray.FromObjects (items);
}
[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("maccatalyst")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos")]
[ObsoletedOSPlatform ("macos10.15")]
[ObsoletedOSPlatform ("tvos13.0")]
[ObsoletedOSPlatform ("ios13.0")]
[ObsoletedOSPlatform ("maccatalyst13.1")]
[DllImport (Constants.CoreTextLibrary)]
unsafe static extern byte CTFontManagerRegisterFontsForURLs (IntPtr arrayRef, CTFontManagerScope scope, IntPtr* error_array);
/// <param name="fontUrls">To be added.</param>
/// <param name="scope">To be added.</param>
/// <summary>To be added.</summary>
/// <returns>To be added.</returns>
/// <remarks>To be added.</remarks>
[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("maccatalyst")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos")]
[ObsoletedOSPlatform ("macos10.15", "Use 'RegisterFonts' instead.")]
[ObsoletedOSPlatform ("tvos13.0", "Use 'RegisterFonts' instead.")]
[ObsoletedOSPlatform ("ios13.0", "Use 'RegisterFonts' instead.")]
[ObsoletedOSPlatform ("maccatalyst13.1", "Use 'RegisterFonts' instead.")]
public static NSError []? RegisterFontsForUrl (NSUrl [] fontUrls, CTFontManagerScope scope)
{
using (var arr = EnsureNonNullArray (fontUrls, nameof (fontUrls))) {
IntPtr error_array = IntPtr.Zero;
unsafe {
if (CTFontManagerRegisterFontsForURLs (arr.Handle, scope, &error_array) != 0)
return null;
GC.KeepAlive (arr);
}
return NSArray.ArrayFromHandleDropNullElements<NSError> (error_array, releaseHandle: true);
}
}
public delegate bool CTFontRegistrationHandler (NSError [] errors, bool done);
[UnmanagedCallersOnly]
static unsafe byte TrampolineRegistrationHandler (IntPtr block, /* NSArray */ IntPtr errors, byte done)
{
var del = BlockLiteral.GetTarget<CTFontRegistrationHandler> (block);
if (del is null)
return 0;
var rv = del (NSArray.NonNullArrayFromHandleDropNullElements<NSError> (errors), done == 0 ? false : true);
return rv ? (byte) 1 : (byte) 0;
}
[SupportedOSPlatform ("tvos13.0")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
[DllImport (Constants.CoreTextLibrary)]
unsafe static extern void CTFontManagerRegisterFontURLs (/* CFArrayRef */ IntPtr fontUrls, CTFontManagerScope scope, byte enabled, BlockLiteral* registrationHandler);
[SupportedOSPlatform ("tvos13.0")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
[BindingImpl (BindingImplOptions.Optimizable)]
public static void RegisterFonts (NSUrl [] fontUrls, CTFontManagerScope scope, bool enabled, CTFontRegistrationHandler registrationHandler)
{
using (var arr = EnsureNonNullArray (fontUrls, nameof (fontUrls))) {
if (registrationHandler is null) {
unsafe {
CTFontManagerRegisterFontURLs (arr.Handle, scope, enabled.AsByte (), null);
GC.KeepAlive (arr);
}
} else {
unsafe {
delegate* unmanaged<IntPtr, IntPtr, byte, byte> trampoline = &TrampolineRegistrationHandler;
using var block = new BlockLiteral (trampoline, registrationHandler, typeof (CTFontManager), nameof (TrampolineRegistrationHandler));
CTFontManagerRegisterFontURLs (arr.Handle, scope, enabled.AsByte (), &block);
}
}
}
}
[DllImport (Constants.CoreTextLibrary)]
unsafe static extern byte CTFontManagerUnregisterFontsForURL (IntPtr fotUrl, CTFontManagerScope scope, IntPtr* error);
/// <param name="fontUrl">To be added.</param>
/// <param name="scope">To be added.</param>
/// <summary>To be added.</summary>
/// <returns>To be added.</returns>
/// <remarks>To be added.</remarks>
public static NSError? UnregisterFontsForUrl (NSUrl fontUrl, CTFontManagerScope scope)
{
if (fontUrl is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (fontUrl));
IntPtr error = IntPtr.Zero;
try {
bool rv;
unsafe {
rv = CTFontManagerUnregisterFontsForURL (fontUrl.Handle, scope, &error) != 0;
GC.KeepAlive (fontUrl);
}
if (rv)
return null;
else
return Runtime.GetNSObject<NSError> (error);
} finally {
if (error != IntPtr.Zero)
CFObject.CFRelease (error);
}
}
[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("maccatalyst")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos")]
[ObsoletedOSPlatform ("macos10.15")]
[ObsoletedOSPlatform ("tvos13.0")]
[ObsoletedOSPlatform ("ios13.0")]
[DllImport (Constants.CoreTextLibrary)]
unsafe static extern byte CTFontManagerUnregisterFontsForURLs (IntPtr arrayRef, CTFontManagerScope scope, IntPtr* error_array);
/// <param name="fontUrls">To be added.</param>
/// <param name="scope">To be added.</param>
/// <summary>To be added.</summary>
/// <returns>To be added.</returns>
/// <remarks>To be added.</remarks>
[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("maccatalyst")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos")]
[ObsoletedOSPlatform ("macos10.15", "Use 'UnregisterFonts' instead.")]
[ObsoletedOSPlatform ("tvos13.0", "Use 'UnregisterFonts' instead.")]
[ObsoletedOSPlatform ("ios13.0", "Use 'UnregisterFonts' instead.")]
[ObsoletedOSPlatform ("maccatalyst", "Use 'UnregisterFonts' instead.")]
public static NSError []? UnregisterFontsForUrl (NSUrl [] fontUrls, CTFontManagerScope scope)
{
IntPtr error_array = IntPtr.Zero;
using (var arr = EnsureNonNullArray (fontUrls, nameof (fontUrls))) {
unsafe {
if (CTFontManagerUnregisterFontsForURLs (arr.Handle, scope, &error_array) != 0)
return null;
GC.KeepAlive (arr);
}
return NSArray.ArrayFromHandleDropNullElements<NSError> (error_array, releaseHandle: true);
}
}
[SupportedOSPlatform ("tvos13.0")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
[DllImport (Constants.CoreTextLibrary)]
static extern unsafe void CTFontManagerUnregisterFontURLs (/* CFArrayRef */ IntPtr fontUrls, CTFontManagerScope scope, BlockLiteral* registrationHandler);
[SupportedOSPlatform ("tvos13.0")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
[BindingImpl (BindingImplOptions.Optimizable)]
public unsafe static void UnregisterFonts (NSUrl [] fontUrls, CTFontManagerScope scope, CTFontRegistrationHandler registrationHandler)
{
using (var arr = EnsureNonNullArray (fontUrls, nameof (fontUrls))) {
if (registrationHandler is null) {
CTFontManagerUnregisterFontURLs (arr.Handle, scope, null);
GC.KeepAlive (arr);
} else {
delegate* unmanaged<IntPtr, IntPtr, byte, byte> trampoline = &TrampolineRegistrationHandler;
using var block = new BlockLiteral (trampoline, registrationHandler, typeof (CTFontManager), nameof (TrampolineRegistrationHandler));
CTFontManagerUnregisterFontURLs (arr.Handle, scope, &block);
GC.KeepAlive (arr);
}
}
}
[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("maccatalyst")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos")]
[DllImport (Constants.CoreTextLibrary)]
static extern /* CFArrayRef */ IntPtr CTFontManagerCreateFontDescriptorsFromURL (/* CFURLRef */ IntPtr fileURL);
/// <param name="url">To be added.</param>
/// <summary>To be added.</summary>
/// <returns>To be added.</returns>
/// <remarks>To be added.</remarks>
[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("maccatalyst")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos")]
public static CTFontDescriptor [] GetFonts (NSUrl url)
{
if (url is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (url));
var arrayPtr = CTFontManagerCreateFontDescriptorsFromURL (url.Handle);
GC.KeepAlive (url);
if (arrayPtr == IntPtr.Zero)
return new CTFontDescriptor [0];
using (var unmanageFonts = new CFArray (arrayPtr, true)) {
var managedFonts = new CTFontDescriptor [unmanageFonts.Count];
for (int index = 0; index < unmanageFonts.Count; index++) {
managedFonts [index] = new CTFontDescriptor (unmanageFonts.GetValue (index), false);
}
return managedFonts;
}
}
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos")]
[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("maccatalyst")]
[ObsoletedOSPlatform ("macos15.0", "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[ObsoletedOSPlatform ("tvos18.0", "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[ObsoletedOSPlatform ("ios18.0", "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[ObsoletedOSPlatform ("maccatalyst18.0", "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[DllImport (Constants.CoreTextLibrary)]
unsafe static extern byte CTFontManagerRegisterGraphicsFont (IntPtr cgfont, IntPtr* error);
/// <param name="font">The CoreGraphics font to register with the CoreText font system.</param>
/// <param name="error">On return the error, if any.</param>
/// <summary>To be added.</summary>
/// <returns>True on success, false on error.</returns>
/// <remarks>
/// <para>
/// You can use this feature to register fonts that you
/// download from the network, or to use fonts that are for
/// example embedded as a resource in your executable or some
/// other database.
///
/// </para>
/// <example>
/// <code lang="csharp lang-csharp"><![CDATA[
/// //
/// // Load font into byte array from a file.
/// //
/// byte [] myBuffer = GetEmbeddedResource ("myResource");
/// CGFont font = CGFont.CreateFromProvider (new CGDataProvider (myBuffer, 0, myBuffer.Count));
/// if (CTFontManager.RegisterGraphicsFont (font, out error)){
/// //
/// // access font
/// //
///
/// // Release font after we use it.
/// CTFontManager.UnregisterGraphicsFont (font, out error);
/// }
/// ]]></code>
/// </example>
/// </remarks>
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos")]
[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("maccatalyst")]
[ObsoletedOSPlatform ("macos15.0", "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[ObsoletedOSPlatform ("tvos18.0", "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[ObsoletedOSPlatform ("ios18.0", "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
[ObsoletedOSPlatform ("maccatalyst18.0", "Use 'CreateFontDescriptors' or 'RegisterFontsForUrl' instead.")]
public static bool RegisterGraphicsFont (CGFont font, [NotNullWhen (true)] out NSError? error)
{
if (font is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (font));
IntPtr h = IntPtr.Zero;
bool ret;
try {
unsafe {
ret = CTFontManagerRegisterGraphicsFont (font.Handle, &h) != 0;
GC.KeepAlive (font);
}
if (ret)
error = null;
else
error = new NSError (h);
} finally {
if (h != IntPtr.Zero)
CFObject.CFRelease (h);
}
return ret;
}
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos")]
[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("maccatalyst")]
[ObsoletedOSPlatform ("macos15.0")]
[ObsoletedOSPlatform ("tvos18.0")]
[ObsoletedOSPlatform ("ios18.0")]
[ObsoletedOSPlatform ("maccatalyst18.0")]
[DllImport (Constants.CoreTextLibrary)]
unsafe static extern byte CTFontManagerUnregisterGraphicsFont (IntPtr cgfont, IntPtr* error);
/// <param name="font">The CoreGraphics font to unregister with the CoreText font system.</param>
/// <param name="error">On return the error, if any.</param>
/// <summary>Unregisters a CoreGraphics Font from the CoreText font system.</summary>
/// <returns>True on success, false on error.</returns>
/// <remarks>
/// <example>
/// <code lang="csharp lang-csharp"><![CDATA[
/// //
/// // Load font into byte array from a file.
/// //
/// byte [] myBuffer = GetEmbeddedResource ("myResource");
/// CGFont font = CGFont.CreateFromProvider (new CGDataProvider (myBuffer, 0, myBuffer.Count));
/// if (CTFontManager.RegisterGraphicsFont (font, out error)){
/// //
/// // access font
/// //
///
/// // Release font after we use it.
/// CTFontManager.UnregisterGraphicsFont (font, out error);
/// }
/// ]]></code>
/// </example>
/// </remarks>
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos")]
[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("maccatalyst")]
[ObsoletedOSPlatform ("macos15.0")]
[ObsoletedOSPlatform ("tvos18.0")]
[ObsoletedOSPlatform ("ios18.0")]
[ObsoletedOSPlatform ("maccatalyst18.0")]
public static bool UnregisterGraphicsFont (CGFont font, out NSError? error)
{
if (font is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (font));
IntPtr h = IntPtr.Zero;
bool ret;
try {
unsafe {
ret = CTFontManagerUnregisterGraphicsFont (font.Handle, &h) != 0;
GC.KeepAlive (font);
}
if (ret)
error = null;
else
error = new NSError (h);
} finally {
if (h != IntPtr.Zero)
CFObject.CFRelease (h);
}
return ret;
}
/// <summary>Observer for receiving notifications when fonts are added to the registry.</summary>
/// <remarks>To be added.</remarks>
public static partial class Notifications {
/// <include file="../../docs/api/CoreText.CTFontManager/Notifications.xml" path="/Documentation/Docs[@DocId='M:CoreText.CTFontManager.Notifications.ObserveRegisteredFontsChanged(System.EventHandler{Foundation.NSNotificationEventArgs})']/*" />
public static NSObject ObserveRegisteredFontsChanged (EventHandler<NSNotificationEventArgs> handler)
{
return NSNotificationCenter.DefaultCenter.AddObserver (RegisteredFontsChangedNotification, notification => handler (null, new NSNotificationEventArgs (notification)));
}
}
[SupportedOSPlatform ("tvos13.0")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
[DllImport (Constants.CoreTextLibrary)]
static extern unsafe void CTFontManagerRegisterFontDescriptors (/* CFArrayRef */ IntPtr fontDescriptors, CTFontManagerScope scope, byte enabled, BlockLiteral* registrationHandler);
[SupportedOSPlatform ("tvos13.0")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
[BindingImpl (BindingImplOptions.Optimizable)]
public unsafe static void RegisterFontDescriptors (CTFontDescriptor [] fontDescriptors, CTFontManagerScope scope, bool enabled, CTFontRegistrationHandler registrationHandler)
{
using (var arr = EnsureNonNullArray (fontDescriptors, nameof (fontDescriptors))) {
if (registrationHandler is null) {
CTFontManagerRegisterFontDescriptors (arr.Handle, scope, enabled.AsByte (), null);
GC.KeepAlive (arr);
} else {
delegate* unmanaged<IntPtr, IntPtr, byte, byte> trampoline = &TrampolineRegistrationHandler;
using var block = new BlockLiteral (trampoline, registrationHandler, typeof (CTFontManager), nameof (TrampolineRegistrationHandler));
CTFontManagerRegisterFontDescriptors (arr.Handle, scope, enabled.AsByte (), &block);
GC.KeepAlive (arr);
}
}
}
[SupportedOSPlatform ("tvos13.0")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
[DllImport (Constants.CoreTextLibrary)]
static extern unsafe void CTFontManagerUnregisterFontDescriptors (/* CFArrayRef */ IntPtr fontDescriptors, CTFontManagerScope scope, BlockLiteral* registrationHandler);
[SupportedOSPlatform ("tvos13.0")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
[BindingImpl (BindingImplOptions.Optimizable)]
public unsafe static void UnregisterFontDescriptors (CTFontDescriptor [] fontDescriptors, CTFontManagerScope scope, CTFontRegistrationHandler registrationHandler)
{
using (var arr = EnsureNonNullArray (fontDescriptors, nameof (fontDescriptors))) {
if (registrationHandler is null) {
CTFontManagerUnregisterFontDescriptors (arr.Handle, scope, null);
GC.KeepAlive (arr);
} else {
delegate* unmanaged<IntPtr, IntPtr, byte, byte> trampoline = &TrampolineRegistrationHandler;
using var block = new BlockLiteral (trampoline, registrationHandler, typeof (CTFontManager), nameof (TrampolineRegistrationHandler));
CTFontManagerUnregisterFontDescriptors (arr.Handle, scope, &block);
GC.KeepAlive (arr);
}
}
}
#if __IOS__
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
[UnsupportedOSPlatform ("macos")]
[UnsupportedOSPlatform ("tvos")]
[DllImport (Constants.CoreTextLibrary)]
static extern /* CFArrayRef */ IntPtr CTFontManagerCopyRegisteredFontDescriptors (CTFontManagerScope scope, byte enabled);
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
[UnsupportedOSPlatform ("tvos")]
[UnsupportedOSPlatform ("macos")]
public static CTFontDescriptor []? GetRegisteredFontDescriptors (CTFontManagerScope scope, bool enabled)
{
var p = CTFontManagerCopyRegisteredFontDescriptors (scope, enabled.AsByte ());
// Copy/Create rule - we must release the CFArrayRef
return NSArray.ArrayFromHandleDropNullElements<CTFontDescriptor> (p, releaseHandle: true);
}
#endif
[DllImport (Constants.CoreTextLibrary)]
static extern unsafe /* CTFontDescriptorRef _Nullable */ IntPtr CTFontManagerCreateFontDescriptorFromData (/* CFDataRef */ IntPtr data);
public static CTFontDescriptor? CreateFontDescriptor (NSData data)
{
if (data is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (data));
var p = CTFontManagerCreateFontDescriptorFromData (data.Handle);
GC.KeepAlive (data);
if (p == IntPtr.Zero)
return null;
// Copy/Create rule - dont retain it inside the .ctor
return new CTFontDescriptor (p, owns: true);
}
[SupportedOSPlatform ("tvos13.0")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
[DllImport (Constants.CoreTextLibrary)]
static extern unsafe /* CFArrayRef */ IntPtr CTFontManagerCreateFontDescriptorsFromData (/* CFDataRef */ IntPtr data);
[SupportedOSPlatform ("tvos13.0")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
public static CTFontDescriptor []? CreateFontDescriptors (NSData data)
{
if (data is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (data));
var p = CTFontManagerCreateFontDescriptorsFromData (data.Handle);
GC.KeepAlive (data);
// Copy/Create rule - we must release the CFArrayRef
return NSArray.ArrayFromHandleDropNullElements<CTFontDescriptor> (p, releaseHandle: true);
}
#if __IOS__
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
[UnsupportedOSPlatform ("tvos")]
[UnsupportedOSPlatform ("macos")]
[DllImport (Constants.CoreTextLibrary)]
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 :(
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
[UnsupportedOSPlatform ("tvos")]
[UnsupportedOSPlatform ("macos")]
[BindingImpl (BindingImplOptions.Optimizable)]
public unsafe static void RegisterFonts (string [] assetNames, CFBundle bundle, CTFontManagerScope scope, bool enabled, CTFontRegistrationHandler registrationHandler)
{
using (var arr = EnsureNonNullArray (assetNames, nameof (assetNames))) {
if (registrationHandler is null) {
CTFontManagerRegisterFontsWithAssetNames (arr.Handle, bundle.GetHandle (), scope, enabled.AsByte (), null);
GC.KeepAlive (arr);
GC.KeepAlive (bundle);
} else {
delegate* unmanaged<IntPtr, IntPtr, byte, byte> trampoline = &TrampolineRegistrationHandler;
using var block = new BlockLiteral (trampoline, registrationHandler, typeof (CTFontManager), nameof (TrampolineRegistrationHandler));
CTFontManagerRegisterFontsWithAssetNames (arr.Handle, bundle.GetHandle (), scope, enabled.AsByte (), &block);
GC.KeepAlive (arr);
GC.KeepAlive (bundle);
}
}
}
public delegate void CTFontManagerRequestFontsHandler (CTFontDescriptor [] unresolvedFontDescriptors);
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
[UnsupportedOSPlatform ("tvos")]
[UnsupportedOSPlatform ("macos")]
[DllImport (Constants.CoreTextLibrary)]
static extern unsafe void CTFontManagerRequestFonts (/* CFArrayRef */ IntPtr fontDescriptors, BlockLiteral* completionHandler);
[UnmanagedCallersOnly]
static unsafe void TrampolineRequestFonts (IntPtr block, /* CFArray */ IntPtr fontDescriptors)
{
var del = BlockLiteral.GetTarget<CTFontManagerRequestFontsHandler> (block);
if (del is not null)
del (NSArray.NonNullArrayFromHandleDropNullElements<CTFontDescriptor> (fontDescriptors));
}
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
[UnsupportedOSPlatform ("tvos")]
[UnsupportedOSPlatform ("macos")]
[BindingImpl (BindingImplOptions.Optimizable)]
public static void RequestFonts (CTFontDescriptor [] fontDescriptors, CTFontManagerRequestFontsHandler completionHandler)
{
if (completionHandler is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (completionHandler));
using (var arr = EnsureNonNullArray (fontDescriptors, nameof (fontDescriptors))) {
unsafe {
delegate* unmanaged<IntPtr, IntPtr, void> trampoline = &TrampolineRequestFonts;
using var block = new BlockLiteral (trampoline, completionHandler, typeof (CTFontManager), nameof (TrampolineRequestFonts));
CTFontManagerRequestFonts (arr.Handle, &block);
}
}
}
#endif
}
}