import type { Font } from '@capsizecss/unpack'; import type * as unifont from 'unifont'; import type * as z from 'zod/v4'; import type { DisplaySchema, StyleSchema, WeightSchema } from './config.js'; import type { FONT_TYPES, GENERIC_FALLBACK_NAMES } from './constants.js'; import type { CollectedFontForMetrics } from './core/optimize-fallbacks.js'; export type Weight = z.infer; type Display = z.infer; export interface FontProviderInitContext { /** * Useful for caching. */ storage: { getItem: { (key: string): Promise; (key: string, init: () => Awaitable): Promise; }; setItem: (key: string, value: unknown) => Awaitable; }; /** * The project root, useful for resolving local files paths. */ root: URL; } type Awaitable = T | Promise; export interface FontProvider | undefined | never = never> { /** * A unique name for the provider, used in logs and for identification. */ name: string; /** * A serializable object, used for identification. */ config?: Record | undefined; /** * Optional callback, used to perform any initialization logic. */ init?: ((context: FontProviderInitContext) => Awaitable) | undefined; /** * Used to retrieve and return font face data based on the given options. */ resolveFont: (options: ResolveFontOptions) => Awaitable<{ fonts: Array; } | undefined>; /** * Optional callback, used to return the list of available font names. */ listFonts?: (() => Awaitable | undefined>) | undefined; } export interface FamilyProperties { /** * @default `"swap"` * * Defines [how a font displays](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display) based on when it is downloaded and ready for use. */ display?: Display | undefined; /** * A [font stretch](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-stretch). */ stretch?: string | undefined; /** * Controls the [typographic font features](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-feature-settings) (e.g. ligatures, small caps, or swashes). */ featureSettings?: string | undefined; /** * Font [variation settings](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-variation-settings). */ variationSettings?: string | undefined; /** * Determines when a font must be downloaded and used based on a specific [range of unicode characters](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/unicode-range). * If a character on the page matches the configured range, the browser will download the font and all characters will be available for use on the page. To configure a subset of * characters preloaded for a single font, see the [subsets](#fontsubsets) property instead. * * This can be useful for localization to avoid unnecessary font downloads when a specific part of your website uses a different alphabet and will be displayed with a separate * font. For example, a website that offers both English and Japanese versions can prevent the browser from downloading the Japanese font on English versions of the page that do * not contain any of the Japanese characters provided in `unicodeRange`. */ unicodeRange?: [string, ...Array] | undefined; } type WithOptions = TFontProvider extends FontProvider ? [TFamilyOptions] extends [never] ? { /** * An object to pass provider specific options. It is typed automatically based on the font family provider. */ options?: undefined; } : undefined extends TFamilyOptions ? { /** * An object to pass provider specific options. It is typed automatically based on the font family provider. */ options?: TFamilyOptions; } : { /** * An object to pass provider specific options. It is typed automatically based on the font family provider. */ options: TFamilyOptions; } : { /** * An object to pass provider specific options. It is typed automatically based on the font family provider. */ options?: undefined; }; export type FontFamily = FamilyProperties & WithOptions> & { /** * The font family name, as identified by your font provider. */ name: string; /** * A valid [ident](https://developer.mozilla.org/en-US/docs/Web/CSS/ident) in the form of a CSS variable (i.e. starting with `--`). */ cssVariable: string; /** * The source of your font files. You can use a built-in provider or write your own custom provider. */ provider: TFontProvider; /** * @default `[400]` * * An array of [font weights](https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight). If no value is specified in your configuration, only weight `400` is * included by default to prevent unnecessary downloads. You will need to include this property to access any other font weights. * * If the associated font is a [variable font](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_fonts/Variable_fonts_guide), you can specify a range of weights: * * ```js * weight: "100 900" * ``` */ weights?: [Weight, ...Array] | undefined; /** * @default `["normal", "italic"]` * * An array of [font styles](https://developer.mozilla.org/en-US/docs/Web/CSS/font-style). */ styles?: [Style, ...Array