Home / File/ collect-font-assets-from-faces.ts — astro Source File

collect-font-assets-from-faces.ts — astro Source File

Architecture documentation for collect-font-assets-from-faces.ts, a typescript file in the astro codebase. 6 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 6 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  d5221c9f_f56f_63a5_d653_1f0f936eb496["collect-font-assets-from-faces.ts"]
  a2fc192c_cdf9_df62_81bf_9c18a80b317e["./fonts/constants.js"]
  d5221c9f_f56f_63a5_d653_1f0f936eb496 --> a2fc192c_cdf9_df62_81bf_9c18a80b317e
  e92fda44_4a5d_b83b_1a45_958cdae6c91b["./definitions.js"]
  d5221c9f_f56f_63a5_d653_1f0f936eb496 --> e92fda44_4a5d_b83b_1a45_958cdae6c91b
  ea387312_d0a2_bb52_dafc_3872f7307f95["../../src/assets/fonts/types.js"]
  d5221c9f_f56f_63a5_d653_1f0f936eb496 --> ea387312_d0a2_bb52_dafc_3872f7307f95
  aca757ac_dcac_a793_a9c5_394cd0d7d313["../utils.js"]
  d5221c9f_f56f_63a5_d653_1f0f936eb496 --> aca757ac_dcac_a793_a9c5_394cd0d7d313
  2f3de45d_9993_acba_acd0_40014d5fbc2e["./core/optimize-fallbacks.js"]
  d5221c9f_f56f_63a5_d653_1f0f936eb496 --> 2f3de45d_9993_acba_acd0_40014d5fbc2e
  07866fa4_1b50_10c2_1b6b_5b1f385406b5["unifont"]
  d5221c9f_f56f_63a5_d653_1f0f936eb496 --> 07866fa4_1b50_10c2_1b6b_5b1f385406b5
  style d5221c9f_f56f_63a5_d653_1f0f936eb496 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type * as unifont from 'unifont';
import { FONT_FORMATS } from '../constants.js';
import type { FontFileIdGenerator, Hasher } from '../definitions.js';
import type { Defaults, FontFileById, PreloadData, ResolvedFontFamily } from '../types.js';
import { renderFontWeight } from '../utils.js';
import type { CollectedFontForMetrics } from './optimize-fallbacks.js';

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++;
		}
	}

	return {
		fontFileById,
		preloads,
		collectedFontsForMetricsByUniqueKey,
	};
}

Domain

Subdomains

Dependencies

  • ../../src/assets/fonts/types.js
  • ../utils.js
  • ./core/optimize-fallbacks.js
  • ./definitions.js
  • ./fonts/constants.js
  • unifont

Frequently Asked Questions

What does collect-font-assets-from-faces.ts do?
collect-font-assets-from-faces.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in collect-font-assets-from-faces.ts?
collect-font-assets-from-faces.ts defines 1 function(s): collectFontAssetsFromFaces.
What does collect-font-assets-from-faces.ts depend on?
collect-font-assets-from-faces.ts imports 6 module(s): ../../src/assets/fonts/types.js, ../utils.js, ./core/optimize-fallbacks.js, ./definitions.js, ./fonts/constants.js, unifont.
Where is collect-font-assets-from-faces.ts in the architecture?
collect-font-assets-from-faces.ts is located at packages/astro/src/assets/fonts/core/collect-font-assets-from-faces.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/assets/fonts/core).

Analyze Your Own Codebase

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

Try Supermodel Free