SystemFontCollection Class

The fonts installed on the machine, or the fonts of chosen directories: an index of their faces by family and by name, read without loading the fonts.

public sealed class SystemFontCollection

Remarks

A collection is immutable and can be used by several threads at the same time. It is a snapshot of the files when it is created: a font installed or removed later is not seen. Creating it reads the names and the styles of the faces, not their characters: SystemFontDescriptor.Covers reads them when they are first asked for, and the collection keeps them within a bounded amount of memory.

The installed fonts are found in the font directories of the operating system:

  • Windows: the fonts directory of Windows, the fonts installed for the current user, and the files the registry lists.
  • macOS: the Library/Fonts directories of the user, of the computer, of the network and of the system, and the fonts the system downloads on demand.
  • Linux and the other systems: the directories of the configuration of fontconfig, and the font directories of the XDG Base Directory Specification.

Properties

NameDescription
DefaultGets the fonts installed on the machine, scanned when the property is first read.
FontsGets the faces, ordered by family, then by file, then by index in the file.

Gets the fonts installed on the machine, scanned when the property is first read.

public static SystemFontCollection Default { get; }

Property value

SystemFontCollection

Remarks

The scan reads the directories of the fonts of the operating system once per process; a font installed later is not seen.

Gets the faces, ordered by family, then by file, then by index in the file.

public IReadOnlyList<SystemFontDescriptor> Fonts { get; }

Methods

NameDescription
Create(IEnumerable<string>)Creates the collection of the fonts of directories and of font files.
FindByName(string)Finds a face by its full name or its PostScript name, as the local() function of CSS does.
GetFamily(string)Gets the faces of a family.
Match(string, int, bool)Finds the face of a family that is the closest to a style, with the font matching of CSS (CSS Fonts Module Level 4, §5.2): the normal width first, then the style, then the weight.

Create(IEnumerable<string>)

SystemFontCollection.cs

Creates the collection of the fonts of directories and of font files.

public static SystemFontCollection Create(IEnumerable<string> paths)

Parameters

paths IEnumerable<string>
The directories, whose subdirectories are scanned too, and the font files. A path that does not exist is ignored.

Returns

SystemFontCollection The faces of the .ttf, .otf, .ttc, .otc, .woff and .woff2 files the font code loads; the other files are ignored.

Exceptions

ArgumentNullException
paths is null.

FindByName(string)

SystemFontCollection.cs

Finds a face by its full name or its PostScript name, as the local() function of CSS does.

public SystemFontDescriptor? FindByName(string name)

Parameters

name string
The full name or the PostScript name, compared without case.

Returns

SystemFontDescriptor? The first face of SystemFontCollection.Fonts with the name, or null.

Exceptions

ArgumentNullException
name is null.

GetFamily(string)

SystemFontCollection.cs

Gets the faces of a family.

public IReadOnlyList<SystemFontDescriptor> GetFamily(string familyName)

Parameters

familyName string
A typographic family name or a family name, in any language of the fonts, compared without case.

Returns

IReadOnlyList<SystemFontDescriptor> The faces of the family, in the order of SystemFontCollection.Fonts; empty when no font has the family.

Exceptions

ArgumentNullException
familyName is null.

Match(string, int, bool)

SystemFontCollection.cs

Finds the face of a family that is the closest to a style, with the font matching of CSS (CSS Fonts Module Level 4, §5.2): the normal width first, then the style, then the weight.

public SystemFontDescriptor? Match(string familyName, int weight = 400, bool isItalic = false)

Parameters

familyName string
A typographic family name or a family name, in any language of the fonts, compared without case.
weight int
The weight, from 1 to 1000: 400 is normal, 700 is bold.
isItalic bool
Whether an italic face is preferred; an oblique face, then an upright face, are used when the family has none.

Returns

SystemFontDescriptor? The face, or null when no font has the family.

Exceptions

ArgumentNullException
familyName is null.
ArgumentOutOfRangeException
weight is not between 1 and 1000.

Remarks

A variable font matches the weights and the widths of its wght and wdth axes, and the italic of its ital axis. Selecting the instance of the variable font is left to the caller.