@next/font
Advanced tools
Comparing version 13.0.2-canary.2 to 13.0.2-canary.3
@@ -46,18 +46,12 @@ "use strict"; | ||
} | ||
let fontFaceDeclarations = ''; | ||
for (const weight of weights) { | ||
for (const style of styles) { | ||
const fontAxes = (0, utils_1.getFontAxes)(fontFamily, weight, style, selectedVariableAxes); | ||
const url = (0, utils_1.getUrl)(fontFamily, fontAxes, display); | ||
let cachedCssRequest = cssCache.get(url); | ||
const fontFaceDeclaration = cachedCssRequest !== null && cachedCssRequest !== void 0 ? cachedCssRequest : (await (0, utils_1.fetchCSSFromGoogleFonts)(url, fontFamily)); | ||
if (!cachedCssRequest) { | ||
cssCache.set(url, fontFaceDeclaration); | ||
} | ||
else { | ||
cssCache.delete(url); | ||
} | ||
fontFaceDeclarations += `${fontFaceDeclaration}\n`; | ||
} | ||
const fontAxes = (0, utils_1.getFontAxes)(fontFamily, weights, styles, selectedVariableAxes); | ||
const url = (0, utils_1.getUrl)(fontFamily, fontAxes, display); | ||
let cachedCssRequest = cssCache.get(url); | ||
const fontFaceDeclarations = cachedCssRequest !== null && cachedCssRequest !== void 0 ? cachedCssRequest : (await (0, utils_1.fetchCSSFromGoogleFonts)(url, fontFamily)); | ||
if (!cachedCssRequest) { | ||
cssCache.set(url, fontFaceDeclarations); | ||
} | ||
else { | ||
cssCache.delete(url); | ||
} | ||
// Find font files to download | ||
@@ -64,0 +58,0 @@ const fontFiles = []; |
@@ -15,6 +15,14 @@ /// <reference types="node" /> | ||
export declare function validateData(functionName: string, data: any): FontOptions; | ||
export declare function getUrl(fontFamily: string, axes: [string, string][], display: string): string; | ||
export declare function getUrl(fontFamily: string, axes: { | ||
wght: string[]; | ||
ital: string[]; | ||
variableAxes?: [string, string][]; | ||
}, display: string): string; | ||
export declare function fetchCSSFromGoogleFonts(url: string, fontFamily: string): Promise<any>; | ||
export declare function fetchFontFile(url: string): Promise<Buffer>; | ||
export declare function getFontAxes(fontFamily: string, weight: string, style: string, selectedVariableAxes?: string[]): [string, string][]; | ||
export declare function getFontAxes(fontFamily: string, weights: string[], styles: string[], selectedVariableAxes?: string[]): { | ||
wght: string[]; | ||
ital: string[]; | ||
variableAxes?: [string, string][]; | ||
}; | ||
export {}; |
@@ -82,15 +82,37 @@ "use strict"; | ||
function getUrl(fontFamily, axes, display) { | ||
var _a, _b; | ||
// Variants are all combinations of weight and style, each variant will result in a separate font file | ||
const variants = []; | ||
for (const wgth of axes.wght) { | ||
if (axes.ital.length === 0) { | ||
variants.push([['wght', wgth], ...((_a = axes.variableAxes) !== null && _a !== void 0 ? _a : [])]); | ||
} | ||
else { | ||
for (const ital of axes.ital) { | ||
variants.push([ | ||
['ital', ital], | ||
['wght', wgth], | ||
...((_b = axes.variableAxes) !== null && _b !== void 0 ? _b : []), | ||
]); | ||
} | ||
} | ||
} | ||
// Google api requires the axes to be sorted, starting with lowercase words | ||
axes.sort(([a], [b]) => { | ||
const aIsLowercase = a.charCodeAt(0) > 96; | ||
const bIsLowercase = b.charCodeAt(0) > 96; | ||
if (aIsLowercase && !bIsLowercase) | ||
return -1; | ||
if (bIsLowercase && !aIsLowercase) | ||
return 1; | ||
return a > b ? 1 : -1; | ||
}); | ||
return `https://fonts.googleapis.com/css2?family=${fontFamily.replace(/ /g, '+')}:${axes.map(([key]) => key).join(',')}@${axes | ||
.map(([, val]) => val) | ||
.join(',')}&display=${display}`; | ||
if (axes.variableAxes) { | ||
variants.forEach((variant) => { | ||
variant.sort(([a], [b]) => { | ||
const aIsLowercase = a.charCodeAt(0) > 96; | ||
const bIsLowercase = b.charCodeAt(0) > 96; | ||
if (aIsLowercase && !bIsLowercase) | ||
return -1; | ||
if (bIsLowercase && !aIsLowercase) | ||
return 1; | ||
return a > b ? 1 : -1; | ||
}); | ||
}); | ||
} | ||
return `https://fonts.googleapis.com/css2?family=${fontFamily.replace(/ /g, '+')}:${variants[0].map(([key]) => key).join(',')}@${variants | ||
.map((variant) => variant.map(([, val]) => val).join(',')) | ||
.sort() | ||
.join(';')}&display=${display}`; | ||
} | ||
@@ -137,6 +159,9 @@ exports.getUrl = getUrl; | ||
exports.fetchFontFile = fetchFontFile; | ||
function getFontAxes(fontFamily, weight, style, selectedVariableAxes) { | ||
function getFontAxes(fontFamily, weights, styles, selectedVariableAxes) { | ||
const allAxes = font_data_json_1.default[fontFamily].axes; | ||
const italicAxis = style === 'italic' ? [['ital', '1']] : []; | ||
if (weight === 'variable') { | ||
const hasItalic = styles.includes('italic'); | ||
const hasNormal = styles.includes('normal'); | ||
const ital = hasItalic ? [...(hasNormal ? ['0'] : []), '1'] : []; | ||
// Weights will always contain one element if it's a variable font | ||
if (weights[0] === 'variable') { | ||
if (selectedVariableAxes) { | ||
@@ -158,11 +183,25 @@ const defineAbleAxes = allAxes | ||
} | ||
const variableAxes = allAxes | ||
.filter(({ tag }) => tag === 'wght' || (selectedVariableAxes === null || selectedVariableAxes === void 0 ? void 0 : selectedVariableAxes.includes(tag))) | ||
.map(({ tag, min, max }) => [tag, `${min}..${max}`]); | ||
return [...italicAxis, ...variableAxes]; | ||
let weightAxis; | ||
const variableAxes = []; | ||
for (const { tag, min, max } of allAxes) { | ||
if (tag === 'wght') { | ||
weightAxis = `${min}..${max}`; | ||
} | ||
else if (selectedVariableAxes === null || selectedVariableAxes === void 0 ? void 0 : selectedVariableAxes.includes(tag)) { | ||
variableAxes.push([tag, `${min}..${max}`]); | ||
} | ||
} | ||
return { | ||
wght: [weightAxis], | ||
ital, | ||
variableAxes, | ||
}; | ||
} | ||
else { | ||
return [...italicAxis, ['wght', weight]]; | ||
return { | ||
ital, | ||
wght: weights, | ||
}; | ||
} | ||
} | ||
exports.getFontAxes = getFontAxes; |
{ | ||
"name": "@next/font", | ||
"version": "13.0.2-canary.2", | ||
"version": "13.0.2-canary.3", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "url": "vercel/next.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2235619
52092