Home / Class/ LocalFontProvider Class — astro Architecture

LocalFontProvider Class — astro Architecture

Architecture documentation for the LocalFontProvider class in local.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  214874ef_a162_c192_431e_95979e65861c["LocalFontProvider"]
  58255ece_c750_2cc1_8857_7a8e6a866fc7["local.ts"]
  214874ef_a162_c192_431e_95979e65861c -->|defined in| 58255ece_c750_2cc1_8857_7a8e6a866fc7
  cac1bc27_7d56_bbe2_e4bf_bb2d1b64bfcc["constructor()"]
  214874ef_a162_c192_431e_95979e65861c -->|method| cac1bc27_7d56_bbe2_e4bf_bb2d1b64bfcc
  37cd042e_89d0_5e08_0f45_ef3ec2c40b1b["init()"]
  214874ef_a162_c192_431e_95979e65861c -->|method| 37cd042e_89d0_5e08_0f45_ef3ec2c40b1b
  bb5ec2cf_dded_7bf4_8e68_8633d6261646["root()"]
  214874ef_a162_c192_431e_95979e65861c -->|method| bb5ec2cf_dded_7bf4_8e68_8633d6261646
  e72e4975_565d_75f6_a99e_9b328c31724a["value()"]
  214874ef_a162_c192_431e_95979e65861c -->|method| e72e4975_565d_75f6_a99e_9b328c31724a
  679bd80d_33eb_0431_d016_0b3f5601ceb1["resolveFont()"]
  214874ef_a162_c192_431e_95979e65861c -->|method| 679bd80d_33eb_0431_d016_0b3f5601ceb1

Relationship Graph

Source Code

packages/astro/src/assets/fonts/providers/local.ts lines 66–147

export class LocalFontProvider implements FontProvider<LocalFamilyOptions> {
	name = 'local';
	config?: Record<string, any> | undefined;

	#fontFileReader: FontFileReader;
	#root: URL | undefined;

	constructor({
		fontFileReader,
	}: {
		fontFileReader: FontFileReader;
	}) {
		this.config = undefined;
		this.#fontFileReader = fontFileReader;
		this.#root = undefined;
	}

	init(context: Pick<FontProviderInitContext, 'root'>): void {
		this.#root = context.root;
	}

	#resolveEntrypoint(root: URL, entrypoint: string): URL {
		const require = createRequire(root);

		try {
			return pathToFileURL(require.resolve(entrypoint));
		} catch {
			return new URL(entrypoint, root);
		}
	}

	#normalizeSource(value: RawSource): NormalizedSource {
		const isValue = typeof value === 'string' || value instanceof URL;
		const url = (isValue ? value : value.url).toString();
		const tech = isValue ? undefined : value.tech;
		return {
			url: fileURLToPath(this.#resolveEntrypoint(this.#root ?? new URL(import.meta.url), url)),
			tech,
		};
	}

	resolveFont(options: ResolveFontOptions<LocalFamilyOptions>): {
		fonts: Array<unifont.FontFaceData>;
	} {
		return {
			fonts:
				options.options?.variants.map((variant) => {
					const shouldInfer = variant.weight === undefined || variant.style === undefined;

					// We prepare the data
					const data: unifont.FontFaceData = {
						// If it should be inferred, we don't want to set the value
						weight: variant.weight,
						style: variant.style,
						src: [],
						unicodeRange: variant.unicodeRange,
						display: variant.display,
						stretch: variant.stretch,
						featureSettings: variant.featureSettings,
						variationSettings: variant.variationSettings,
					};
					// We proxy each source
					data.src = variant.src.map((rawSource, index) => {
						const source = this.#normalizeSource(rawSource);
						// We only try to infer for the first source. Indeed if it doesn't work, the function
						// call will throw an error so that will be interrupted anyways
						if (shouldInfer && index === 0) {
							const result = this.#fontFileReader.extract({
								family: options.familyName,
								url: source.url,
							});
							if (variant.weight === undefined) data.weight = result.weight;
							if (variant.style === undefined) data.style = result.style;
						}

						return source;
					});
					return data;
				}) ?? [],
		};
	}

Domain

Frequently Asked Questions

What is the LocalFontProvider class?
LocalFontProvider is a class in the astro codebase, defined in packages/astro/src/assets/fonts/providers/local.ts.
Where is LocalFontProvider defined?
LocalFontProvider is defined in packages/astro/src/assets/fonts/providers/local.ts at line 66.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free