MCPcopy
hub / github.com/unjs/fontaine / handler

Function handler

packages/fontaine/src/transform.ts:112–192  ·  view source on GitHub ↗
(code, id)

Source from the content-addressed store, hash-verified

110 id: [CSS_RE],
111 },
112 async handler(code, id) {
113 const s = new MagicString(code)
114
115 const ast = parse(code, { positions: true })
116
117 for (const { family, source, index, properties } of parseFontFace(ast)) {
118 if (!supportedExtensions.some(e => source?.endsWith(e)))
119 continue
120 if (skipFontFaceGeneration(fallbackName(family)))
121 continue
122
123 const metrics = (await getMetricsForFamily(family)) || (source && (await readMetricsFromId(source, id).catch(() => null)))
124
125 /* v8 ignore next 2 */
126 if (!metrics)
127 continue
128
129 const familyFallbacks = resolveCategoryFallbacks({
130 fontFamily: family,
131 fallbacks: options.fallbacks,
132 metrics,
133 categoryFallbacks: options.categoryFallbacks,
134 })
135
136 // Iterate backwards: Browsers will use the last working font-face in the stylesheet
137 for (let i = familyFallbacks.length - 1; i >= 0; i--) {
138 const fallback = familyFallbacks[i]!
139 const fallbackMetrics = await getMetricsForFamily(fallback)
140
141 if (!fallbackMetrics)
142 continue
143
144 const fontFace = generateFontFace(metrics, {
145 name: fallbackName(family),
146 font: fallback,
147 metrics: fallbackMetrics,
148 ...properties,
149 })
150 cssContext.value += fontFace
151 s.appendLeft(index, fontFace)
152 }
153 }
154
155 walk(ast, {
156 visit: 'Declaration',
157 enter(node) {
158 if (node.property !== 'font-family')
159 return
160 if (this.atrule && this.atrule.name === 'font-face')
161 return
162 if (node.value.type !== 'Value')
163 /* v8 ignore next */ return
164
165 for (const child of node.value.children) {
166 let family: string | undefined
167 if (child.type === 'String') {
168 family = withoutQuotes(child.value)
169 }

Callers 1

index.spec.tsFile · 0.50

Calls 5

parseFontFaceFunction · 0.90
getMetricsForFamilyFunction · 0.90
resolveCategoryFallbacksFunction · 0.90
generateFontFaceFunction · 0.90
readMetricsFromIdFunction · 0.85

Tested by

no test coverage detected