FontCollection Class

The faces of a font file: every face of a font collection (.ttc), or the single face of another font file.

public sealed class FontCollection : IEnumerable<Font>, IReadOnlyCollection<Font>, IReadOnlyList<Font>, IEnumerable

Remarks

A collection is immutable and can be used by several threads at the same time. Its faces share the bytes of the file, which stay in memory as long as one of the fonts is referenced.

Implements

Properties

NameDescription
CountGets the number of faces.

Gets the number of faces.

public int Count { get; }

Property value

int

Indexers

NameDescription
this[int]Gets a face.

Gets a face.

public Font this[int index] { get; }

Parameters

index int
The index of the face, in the order of the file.

Property value

Font

Exceptions

ArgumentOutOfRangeException
index is negative, or is not less than FontCollection.Count.

Methods

NameDescription
GetEnumerator()Returns an enumerator of the faces, in the order of the file.
Load(Stream, FontLoadOptions?)Loads every face of a font file from a stream, read from its current position to its end.
Load(ReadOnlySpan<byte>, FontLoadOptions?)Loads every face of a font file from its bytes.
Load(string, FontLoadOptions?)Loads every face of a font file.
LoadAsync(Stream, FontLoadOptions?, CancellationToken)Asynchronously loads every face of a font file from a stream, read from its current position to its end.
LoadAsync(string, FontLoadOptions?, CancellationToken)Asynchronously loads every face of a font file, using asynchronous file I/O.
System.Collections.IEnumerable.GetEnumerator()Returns an enumerator that iterates through a collection.

GetEnumerator()

FontCollection.cs

Returns an enumerator of the faces, in the order of the file.

public IEnumerator<Font> GetEnumerator()

Returns

IEnumerator<Font> The enumerator.

Load overloads

Load(Stream, FontLoadOptions?)

FontCollection.cs

Loads every face of a font file from a stream, read from its current position to its end.

public static FontCollection Load(Stream stream, FontLoadOptions? options = null)
Parameters
stream Stream
The readable source stream. It is left open and is not retained after the method returns.
options FontLoadOptions?
The options, or null for FontLoadOptions.Default.
Returns

FontCollection The faces of the file.

Exceptions
ArgumentNullException
stream is null.
ArgumentException
stream is not readable.
UnknownImageFormatException
The data is not a font.
InvalidImageContentException
A font is malformed or truncated.
UnsupportedImageFeatureException
A font uses a feature that is not supported.
ImageResourceLimitException
A configured limit is exceeded.
IOException
An I/O error occurred.

Load(ReadOnlySpan<byte>, FontLoadOptions?)

FontCollection.cs

Loads every face of a font file from its bytes.

public static FontCollection Load(ReadOnlySpan<byte> data, FontLoadOptions? options = null)
Parameters
data ReadOnlySpan<byte>
The bytes of the file. They are copied: the fonts do not refer to the span after the method returns.
options FontLoadOptions?
The options, or null for FontLoadOptions.Default.
Returns

FontCollection The faces of the file.

Exceptions
UnknownImageFormatException
The data is not a font.
InvalidImageContentException
A font is malformed or truncated.
UnsupportedImageFeatureException
A font uses a feature that is not supported.
ImageResourceLimitException
A configured limit is exceeded.

Load(string, FontLoadOptions?)

FontCollection.cs

Loads every face of a font file.

public static FontCollection Load(string path, FontLoadOptions? options = null)
Parameters
path string
The path of the file. The file is closed before the method returns.
options FontLoadOptions?
The options, or null for FontLoadOptions.Default.
Returns

FontCollection The faces of the file.

Exceptions
ArgumentException
path is empty.
UnknownImageFormatException
The file is not a font.
InvalidImageContentException
A font is malformed or truncated.
UnsupportedImageFeatureException
A font uses a feature that is not supported.
ImageResourceLimitException
A configured limit is exceeded.
IOException
An I/O error occurred.

LoadAsync overloads

LoadAsync(Stream, FontLoadOptions?, CancellationToken)

FontCollection.cs

Asynchronously loads every face of a font file from a stream, read from its current position to its end.

public static Task<FontCollection> LoadAsync(Stream stream, FontLoadOptions? options = null, CancellationToken cancellationToken = default)
Parameters
stream Stream
The readable source stream. It is left open.
options FontLoadOptions?
The options, or null for FontLoadOptions.Default.
cancellationToken CancellationToken
The token to monitor for cancellation requests.
Returns

Task<FontCollection> The faces of the file.

Exceptions
ArgumentNullException
stream is null.
ArgumentException
stream is not readable.
OperationCanceledException
The operation was canceled.

LoadAsync(string, FontLoadOptions?, CancellationToken)

FontCollection.cs

Asynchronously loads every face of a font file, using asynchronous file I/O.

public static Task<FontCollection> LoadAsync(string path, FontLoadOptions? options = null, CancellationToken cancellationToken = default)
Parameters
path string
The path of the file.
options FontLoadOptions?
The options, or null for FontLoadOptions.Default.
cancellationToken CancellationToken
The token to monitor for cancellation requests.
Returns

Task<FontCollection> The faces of the file.

Exceptions
ArgumentException
path is empty.
OperationCanceledException
The operation was canceled.

System.Collections.IEnumerable.GetEnumerator()

FontCollection.cs

Returns an enumerator that iterates through a collection.

IEnumerator IEnumerable.GetEnumerator()

Returns

IEnumerator An IEnumerator object that can be used to iterate through the collection.