| 68 | * Build packaged font assets and matching CSS from raw font buffers. |
| 69 | */ |
| 70 | export const buildFont = async ( |
| 71 | ctx: FontContext, |
| 72 | fontBuffers: Uint8Array[], |
| 73 | config: FontBuildConfig, |
| 74 | ): Promise<FontBuildResult> => { |
| 75 | const { glyphtContext, compressionContext } = ctx; |
| 76 | |
| 77 | const fontRefs = await glyphtContext.loadFonts(fontBuffers); |
| 78 | const familyId = config.id ?? normalizeKebabCase(config.family); |
| 79 | const subsets = generateSubsetData(config); |
| 80 | const isVariableFont = config.type === 'variable'; |
| 81 | |
| 82 | const families = sortFontsIntoFamilies(fontRefs); |
| 83 | const defaultSubset = config.subsets[0]; |
| 84 | const requestedFormats = new Set(config.formats ?? ['woff2']); |
| 85 | const exportFormats = { |
| 86 | woff: requestedFormats.has('woff'), |
| 87 | woff2: requestedFormats.has('woff2'), |
| 88 | }; |
| 89 | |
| 90 | // Determine which formats to export based on config and availability. |
| 91 | const assetFormats: WebFontFormat[] = []; |
| 92 | if (exportFormats.woff2) { |
| 93 | assetFormats.push('woff2'); |
| 94 | } |
| 95 | if (exportFormats.woff) { |
| 96 | assetFormats.push('woff'); |
| 97 | } |
| 98 | |
| 99 | // Build `unicode-range` character sets. |
| 100 | const includeCharacters = config.subsets.flatMap((subsetName) => { |
| 101 | const def = subsets.get(subsetName); |
| 102 | if (!def) return []; |
| 103 | |
| 104 | if (def.type === 'range') { |
| 105 | return [{ name: subsetName, includeUnicodeRanges: def.codepoints }]; |
| 106 | } |
| 107 | |
| 108 | return def.slices.map((slice) => ({ |
| 109 | name: `${subsetName}-${slice.index}`, |
| 110 | includeUnicodeRanges: slice.codepoints, |
| 111 | })); |
| 112 | }); |
| 113 | const subsetUnicodeRanges = new Map<string, string>(); |
| 114 | |
| 115 | for (const [subsetName, subsetDef] of subsets) { |
| 116 | if (subsetDef.type === 'range') { |
| 117 | subsetUnicodeRanges.set(subsetName, subsetDef.unicodeRange); |
| 118 | continue; |
| 119 | } |
| 120 | |
| 121 | for (const slice of subsetDef.slices) { |
| 122 | subsetUnicodeRanges.set( |
| 123 | `${subsetName}-${slice.index}`, |
| 124 | codepointsToRangeString(slice.codepoints), |
| 125 | ); |
| 126 | } |
| 127 | } |