Skip to content

fontExists

Given either a single font-family or a list of fonts, returns the name of the first font that can be rendered, or false if none are installed on the user’s machine.

Returns the first family in a list that the browser can actually render, or false when none can. A family is judged by measuring a test string with it and comparing against the generic fallbacks, so the answers depend on the fonts installed on the viewing machine, and a web font that has not finished loading reads as missing until it arrives. Charts use this to pick a real font from a fontFamily stack.

import {fontExists} from "@d3plus/dom";

fontExists("Inter");
fontExists(["Nope Font", "Georgia"]);
fontExists("Comic Sans MS, Arial, sans-serif");
fontExists("Nope Font");

Parameters

import {fontExists} from "@d3plus/dom";
fontrequiredstring | Array.<string>

Can be either a valid CSS font-family string (single or comma-separated names) or an Array of string names.