/
FontCollection.cs
61 lines (50 loc) · 2 KB
/
FontCollection.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.InteropServices;
using Gdip = System.Drawing.SafeNativeMethods.Gdip;
namespace System.Drawing.Text;
/// <summary>
/// When inherited, enumerates the FontFamily objects in a collection of fonts.
/// </summary>
public abstract class FontCollection : IDisposable
{
internal IntPtr _nativeFontCollection;
internal FontCollection() => _nativeFontCollection = IntPtr.Zero;
/// <summary>
/// Disposes of this <see cref='System.Drawing.Text.FontCollection'/>
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) { }
/// <summary>
/// Gets the array of <see cref='System.Drawing.FontFamily'/> objects associated
/// with this <see cref='System.Drawing.Text.FontCollection'/>.
/// </summary>
public FontFamily[] Families
{
get
{
int numSought;
int status = Gdip.GdipGetFontCollectionFamilyCount(new HandleRef(this, _nativeFontCollection), out numSought);
Gdip.CheckStatus(status);
var gpfamilies = new IntPtr[numSought];
int numFound;
status = Gdip.GdipGetFontCollectionFamilyList(new HandleRef(this, _nativeFontCollection), numSought, gpfamilies,
out numFound);
Gdip.CheckStatus(status);
Debug.Assert(numSought == numFound, "GDI+ can't give a straight answer about how many fonts there are");
var families = new FontFamily[numFound];
for (int f = 0; f < numFound; f++)
{
IntPtr native;
Gdip.GdipCloneFontFamily(gpfamilies[f], out native);
families[f] = new FontFamily(native);
}
return families;
}
}
~FontCollection() => Dispose(false);
}