|
| 1 | +import type * as unifont from 'unifont'; |
| 2 | +import { FONT_FORMATS } from '../constants.js'; |
| 3 | +import type { FontFileIdGenerator, Hasher } from '../definitions.js'; |
| 4 | +import type { Defaults, FontFileById, PreloadData, ResolvedFontFamily } from '../types.js'; |
| 5 | +import { renderFontWeight } from '../utils.js'; |
| 6 | +import type { CollectedFontForMetrics } from './optimize-fallbacks.js'; |
| 7 | + |
| 8 | +export function collectFontAssetsFromFaces({ |
| 9 | + fonts, |
| 10 | + fontFileIdGenerator, |
| 11 | + family, |
| 12 | + fontFilesIds, |
| 13 | + collectedFontsIds, |
| 14 | + hasher, |
| 15 | + defaults, |
| 16 | +}: { |
| 17 | + fonts: Array<unifont.FontFaceData>; |
| 18 | + fontFileIdGenerator: FontFileIdGenerator; |
| 19 | + family: Pick<ResolvedFontFamily, 'cssVariable' | 'fallbacks'>; |
| 20 | + fontFilesIds: Set<string>; |
| 21 | + collectedFontsIds: Set<string>; |
| 22 | + hasher: Hasher; |
| 23 | + defaults: Pick<Defaults, 'fallbacks'>; |
| 24 | +}) { |
| 25 | + const fontFileById: FontFileById = new Map(); |
| 26 | + const collectedFontsForMetricsByUniqueKey = new Map<string, CollectedFontForMetrics>(); |
| 27 | + const preloads: Array<PreloadData> = []; |
| 28 | + |
| 29 | + for (const font of fonts) { |
| 30 | + // The index keeps track of encountered URLs. We can't use a regular for loop |
| 31 | + // below because it may contain sources without urls, which would prevent preloading completely |
| 32 | + let index = 0; |
| 33 | + for (const source of font.src) { |
| 34 | + if ('name' in source) { |
| 35 | + continue; |
| 36 | + } |
| 37 | + const format = FONT_FORMATS.find((e) => e.format === source.format)!; |
| 38 | + const originalUrl = source.originalURL!; |
| 39 | + const id = fontFileIdGenerator.generate({ |
| 40 | + cssVariable: family.cssVariable, |
| 41 | + font, |
| 42 | + originalUrl, |
| 43 | + type: format.type, |
| 44 | + }); |
| 45 | + |
| 46 | + if (!fontFilesIds.has(id) && !fontFileById.has(id)) { |
| 47 | + fontFileById.set(id, { url: originalUrl, init: font.meta?.init }); |
| 48 | + // We only collect the first URL to avoid preloading fallback sources (eg. we only |
| 49 | + // preload woff2 if woff is available) |
| 50 | + if (index === 0) { |
| 51 | + preloads.push({ |
| 52 | + style: font.style, |
| 53 | + subset: font.meta?.subset, |
| 54 | + type: format.type, |
| 55 | + url: source.url, |
| 56 | + weight: renderFontWeight(font.weight), |
| 57 | + }); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + const collected: CollectedFontForMetrics = { |
| 62 | + id, |
| 63 | + url: originalUrl, |
| 64 | + init: font.meta?.init, |
| 65 | + data: { |
| 66 | + weight: font.weight, |
| 67 | + style: font.style, |
| 68 | + meta: { |
| 69 | + subset: font.meta?.subset, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }; |
| 73 | + const collectedKey = hasher.hashObject(collected.data); |
| 74 | + const fallbacks = family.fallbacks ?? defaults.fallbacks; |
| 75 | + if ( |
| 76 | + fallbacks.length > 0 && |
| 77 | + // If the same data has already been sent for this family, we don't want to have |
| 78 | + // duplicated fallbacks. Such scenario can occur with unicode ranges. |
| 79 | + !collectedFontsIds.has(collectedKey) && |
| 80 | + !collectedFontsForMetricsByUniqueKey.has(collectedKey) |
| 81 | + ) { |
| 82 | + // If a family has fallbacks, we store the first url we get that may |
| 83 | + // be used for the fallback generation. |
| 84 | + collectedFontsForMetricsByUniqueKey.set(collectedKey, collected); |
| 85 | + } |
| 86 | + |
| 87 | + index++; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return { |
| 92 | + fontFileById, |
| 93 | + preloads, |
| 94 | + collectedFontsForMetricsByUniqueKey, |
| 95 | + }; |
| 96 | +} |
0 commit comments