Note: This feature is available in Web Workers.
The FontFace() constructor creates a new FontFace object.
new FontFace(family, source)
new FontFace(family, source, descriptors)familySpecifies a font family name that can be used to match against this font face when styling elements.
Takes the same type of values as the font-family descriptor of @font-face. This value may also be read and set using the FontFace.family property.
sourceThe font source. This can be either:
ArrayBuffer or a TypedArray.descriptors OptionalA set of optional descriptors passed as an object. It can contain any of the descriptors available for @font-face:
ascentOverrideWith an allowable value for ascent-override.
descentOverrideWith an allowable value for descent-override.
displayWith an allowable value for font-display.
featureSettingsWith an allowable value for font-feature-settings.
lineGapOverrideWith an allowable value for line-gap-override.
stretchWith an allowable value for font-stretch.
styleWith an allowable value for font-style.
unicodeRangeWith an allowable value for unicode-range.
variationSettingsWith an allowable value for font-variation-settings.
weightWith an allowable value for font-weight.
SyntaxError DOMExceptionThrown when a descriptor string does not match the grammar of the corresponding @font-face descriptor, or the specified binary source cannot be loaded. This error results in FontFace.status being set to error.
async function loadFonts() {
const font = new FontFace("my-font", 'url("my-font.woff")', {
style: "normal",
weight: "400",
stretch: "condensed",
});
// wait for font to be loaded
await font.load();
// add font to document
document.fonts.add(font);
// enable font with CSS class
document.body.classList.add("fonts-loaded");
}