import type { Font } from '@capsizecss/unpack'; import type * as unifont from 'unifont'; import type { z } from 'zod'; 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 { storage: { getItem: { (key: string): Promise; (key: string, init: () => Awaitable): Promise; }; setItem: (key: string, value: unknown) => Awaitable; }; root: URL; } type Awaitable = T | Promise; export interface FontProvider | undefined | never = never> { /** * The font provider name, used for display and deduplication. */ name: string; /** * Optional serializable object, used for deduplication. */ config?: Record | undefined; /** * Optional callback, used to perform any initialization logic. */ init?: ((context: FontProviderInitContext) => Awaitable) | undefined; /** * Required callback, 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"` * * A [font display](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display). */ display?: Display | undefined; /** * A [font stretch](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-stretch). */ stretch?: string | undefined; /** * Font [feature settings](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-feature-settings). */ featureSettings?: string | undefined; /** * Font [variation settings](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-variation-settings). */ variationSettings?: string | undefined; /** * A [unicode range](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/unicode-range). */ unicodeRange?: [string, ...Array] | undefined; } type WithOptions = TFontProvider extends FontProvider ? [TFamilyOptions] extends [never] ? { /** * Options forwarded to the font provider while resolving this font family. */ options?: undefined; } : undefined extends TFamilyOptions ? { /** * Options forwarded to the font provider while resolving this font family. */ options?: TFamilyOptions; } : { /** * Options forwarded to the font provider while resolving this font family. */ options: TFamilyOptions; } : { /** * Options forwarded to the font provider while resolving this font family. */ 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 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