MCPcopy Index your code
hub / github.com/fontsource/fontsource / generateVariableCSS

Function generateVariableCSS

packages/cli/src/google/css.ts:236–319  ·  view source on GitHub ↗
(
	metadata: GenerateMetadataV2,
	variableMeta: GenerateMetadataVariable,
	makeFontFilePath: (
		id: string,
		subset: string,
		axes: string,
		style: string,
	) => string,
	tag?: string,
)

Source from the content-addressed store, hash-verified

234};
235
236export const generateVariableCSS = (
237 metadata: GenerateMetadataV2,
238 variableMeta: GenerateMetadataVariable,
239 makeFontFilePath: (
240 id: string,
241 subset: string,
242 axes: string,
243 style: string,
244 ) => string,
245 tag?: string,
246): CSSGenerate => {
247 const { id, family, unicodeRange, weights } = metadata;
248 const { axes, variants } = variableMeta;
249 const cssGenerate: CSSGenerate = [];
250 let indexCSS = '';
251
252 for (const axesKey of Object.keys(variants)) {
253 const variant = variants[axesKey];
254 const styles = Object.keys(variant);
255 const axesLower = axesKey.toLowerCase();
256
257 // These are variable modifiers to change specific CSS selectors
258 // for variable fonts.
259 const variableOpts: FontObject['variable'] = {
260 wght: axes.wght,
261 };
262 if (axesKey === 'standard' || axesKey === 'full' || axesKey === 'wdth')
263 variableOpts.stretch = axes.wdth;
264
265 if (axesKey === 'standard' || axesKey === 'full' || axesKey === 'slnt')
266 variableOpts.slnt = axes.slnt;
267
268 for (const style of styles) {
269 const cssStyle: string[] = [];
270
271 for (const subset of Object.keys(variant[style])) {
272 const fontObj: FontObject = {
273 family: `${family} Variable`,
274 style,
275 display: 'swap',
276 weight: findClosest(weights, 400),
277 unicodeRange: unicodeRange[subset],
278 variable: variableOpts,
279 src: [
280 {
281 url: makeFontFilePath(tag ?? id, subset, axesLower, style),
282 format: 'woff2-variations',
283 },
284 ],
285 comment: `${tag ?? id}-${subset}-${axesLower}-${style}`,
286 };
287
288 // This takes in a font object and returns an @font-face block
289 const css = generateFontFace(fontObj);
290 cssStyle.push(css);
291 }
292
293 // Write down CSS

Callers 2

packagerVariableFunction · 0.90
updateVariableCSSFunction · 0.90

Calls 3

findClosestFunction · 0.90
generateFontFaceFunction · 0.90
makeFontFilePathFunction · 0.50

Tested by

no test coverage detected