Home / Function/ collectFontAssetsFromFaces() — astro Function Reference

collectFontAssetsFromFaces() — astro Function Reference

Architecture documentation for the collectFontAssetsFromFaces() function in collect-font-assets-from-faces.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  e0b61996_a8a6_df6d_d3a8_d4da2256b936["collectFontAssetsFromFaces()"]
  d5221c9f_f56f_63a5_d653_1f0f936eb496["collect-font-assets-from-faces.ts"]
  e0b61996_a8a6_df6d_d3a8_d4da2256b936 -->|defined in| d5221c9f_f56f_63a5_d653_1f0f936eb496
  style e0b61996_a8a6_df6d_d3a8_d4da2256b936 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/assets/fonts/core/collect-font-assets-from-faces.ts lines 8–96

export function collectFontAssetsFromFaces({
	fonts,
	fontFileIdGenerator,
	family,
	fontFilesIds,
	collectedFontsIds,
	hasher,
	defaults,
}: {
	fonts: Array<unifont.FontFaceData>;
	fontFileIdGenerator: FontFileIdGenerator;
	family: Pick<ResolvedFontFamily, 'cssVariable' | 'fallbacks'>;
	fontFilesIds: Set<string>;
	collectedFontsIds: Set<string>;
	hasher: Hasher;
	defaults: Pick<Defaults, 'fallbacks'>;
}) {
	const fontFileById: FontFileById = new Map();
	const collectedFontsForMetricsByUniqueKey = new Map<string, CollectedFontForMetrics>();
	const preloads: Array<PreloadData> = [];

	for (const font of fonts) {
		// The index keeps track of encountered URLs. We can't use a regular for loop
		// below because it may contain sources without urls, which would prevent preloading completely
		let index = 0;
		for (const source of font.src) {
			if ('name' in source) {
				continue;
			}
			const format = FONT_FORMATS.find((e) => e.format === source.format)!;
			const originalUrl = source.originalURL!;
			const id = fontFileIdGenerator.generate({
				cssVariable: family.cssVariable,
				font,
				originalUrl,
				type: format.type,
			});

			if (!fontFilesIds.has(id) && !fontFileById.has(id)) {
				fontFileById.set(id, { url: originalUrl, init: font.meta?.init });
				// We only collect the first URL to avoid preloading fallback sources (eg. we only
				// preload woff2 if woff is available)
				if (index === 0) {
					preloads.push({
						style: font.style,
						subset: font.meta?.subset,
						type: format.type,
						url: source.url,
						weight: renderFontWeight(font.weight),
					});
				}
			}

			const collected: CollectedFontForMetrics = {
				id,
				url: originalUrl,
				init: font.meta?.init,
				data: {
					weight: font.weight,
					style: font.style,
					meta: {
						subset: font.meta?.subset,
					},
				},
			};
			const collectedKey = hasher.hashObject(collected.data);
			const fallbacks = family.fallbacks ?? defaults.fallbacks;
			if (
				fallbacks.length > 0 &&
				// If the same data has already been sent for this family, we don't want to have
				// duplicated fallbacks. Such scenario can occur with unicode ranges.
				!collectedFontsIds.has(collectedKey) &&
				!collectedFontsForMetricsByUniqueKey.has(collectedKey)
			) {
				// If a family has fallbacks, we store the first url we get that may
				// be used for the fallback generation.
				collectedFontsForMetricsByUniqueKey.set(collectedKey, collected);
			}

			index++;
		}

Domain

Subdomains

Frequently Asked Questions

What does collectFontAssetsFromFaces() do?
collectFontAssetsFromFaces() is a function in the astro codebase, defined in packages/astro/src/assets/fonts/core/collect-font-assets-from-faces.ts.
Where is collectFontAssetsFromFaces() defined?
collectFontAssetsFromFaces() is defined in packages/astro/src/assets/fonts/core/collect-font-assets-from-faces.ts at line 8.

Analyze Your Own Codebase

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

Try Supermodel Free