-
Notifications
You must be signed in to change notification settings - Fork 28k
/
Copy pathfont-utils.ts
181 lines (153 loc) · 4.85 KB
/
font-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import * as Log from '../build/output/log'
import {
GOOGLE_FONT_PROVIDER,
DEFAULT_SERIF_FONT,
DEFAULT_SANS_SERIF_FONT,
} from '../shared/lib/constants'
const capsizeFontsMetrics = require('next/dist/server/capsize-font-metrics.json')
const CHROME_UA =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'
const IE_UA = 'Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko'
export type FontManifest = Array<{
url: string
content: string
}>
export type FontConfig = boolean
function isGoogleFont(url: string): boolean {
return url.startsWith(GOOGLE_FONT_PROVIDER)
}
async function getFontForUA(url: string, UA: string): Promise<string> {
const res = await fetch(url, { headers: { 'user-agent': UA } })
return await res.text()
}
export async function getFontDefinitionFromNetwork(
url: string
): Promise<string> {
let result = ''
/**
* The order of IE -> Chrome is important, other wise chrome starts loading woff1.
* CSS cascading 🤷♂️.
*/
try {
if (isGoogleFont(url)) {
result += await getFontForUA(url, IE_UA)
}
result += await getFontForUA(url, CHROME_UA)
} catch (e) {
Log.warn(
`Failed to download the stylesheet for ${url}. Skipped optimizing this font.`
)
return ''
}
return result
}
function parseGoogleFontName(css: string): Array<string> {
const regex = /font-family: ([^;]*)/g
const matches = css.matchAll(regex)
const fontNames = new Set<string>()
for (let font of matches) {
const fontFamily = font[1].replace(/^['"]|['"]$/g, '')
fontNames.add(fontFamily)
}
return [...fontNames]
}
function formatName(str: string): string {
return str
.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
return index === 0 ? word.toLowerCase() : word.toUpperCase()
})
.replace(/\s+/g, '')
}
function formatOverrideValue(val: number) {
return Math.abs(val * 100).toFixed(2)
}
export function calculateOverrideValues(fontName: string) {
const fontKey = formatName(fontName)
const fontMetrics = capsizeFontsMetrics[fontKey]
let { category, ascent, descent, lineGap, unitsPerEm } = fontMetrics
const fallbackFont =
category === 'serif' ? DEFAULT_SERIF_FONT : DEFAULT_SANS_SERIF_FONT
ascent = formatOverrideValue(ascent / unitsPerEm)
descent = formatOverrideValue(descent / unitsPerEm)
lineGap = formatOverrideValue(lineGap / unitsPerEm)
return {
ascent,
descent,
lineGap,
fallbackFont: fallbackFont.name,
}
}
export function calculateSizeAdjustValues(fontName: string) {
const fontKey = formatName(fontName)
const fontMetrics = capsizeFontsMetrics[fontKey]
let { category, ascent, descent, lineGap, unitsPerEm, xWidthAvg } =
fontMetrics
const mainFontAvgWidth = xWidthAvg / unitsPerEm
const fallbackFont =
category === 'serif' ? DEFAULT_SERIF_FONT : DEFAULT_SANS_SERIF_FONT
const fallbackFontName = formatName(fallbackFont.name)
const fallbackFontMetrics = capsizeFontsMetrics[fallbackFontName]
const fallbackFontAvgWidth =
fallbackFontMetrics.xWidthAvg / fallbackFontMetrics.unitsPerEm
let sizeAdjust = xWidthAvg ? mainFontAvgWidth / fallbackFontAvgWidth : 1
ascent = formatOverrideValue(ascent / (unitsPerEm * sizeAdjust))
descent = formatOverrideValue(descent / (unitsPerEm * sizeAdjust))
lineGap = formatOverrideValue(lineGap / (unitsPerEm * sizeAdjust))
return {
ascent,
descent,
lineGap,
fallbackFont: fallbackFont.name,
sizeAdjust: formatOverrideValue(sizeAdjust),
}
}
function calculateOverrideCSS(font: string) {
const fontName = font.trim()
const { ascent, descent, lineGap, fallbackFont } =
calculateOverrideValues(fontName)
return `
@font-face {
font-family: "${fontName} Fallback";
ascent-override: ${ascent}%;
descent-override: ${descent}%;
line-gap-override: ${lineGap}%;
src: local("${fallbackFont}");
}
`
}
function calculateSizeAdjustCSS(font: string) {
const fontName = font.trim()
const { ascent, descent, lineGap, fallbackFont, sizeAdjust } =
calculateSizeAdjustValues(fontName)
return `
@font-face {
font-family: "${fontName} Fallback";
ascent-override: ${ascent}%;
descent-override: ${descent}%;
line-gap-override: ${lineGap}%;
size-adjust: ${sizeAdjust}%;
src: local("${fallbackFont}");
}
`
}
export function getFontOverrideCss(
url: string,
css: string,
useSizeAdjust = false
) {
if (!isGoogleFont(url)) {
return ''
}
const calcFn = useSizeAdjust ? calculateSizeAdjustCSS : calculateOverrideCSS
try {
const fontNames = parseGoogleFontName(css)
const fontCss = fontNames.reduce((cssStr, fontName) => {
cssStr += calcFn(fontName)
return cssStr
}, '')
return fontCss
} catch (e) {
console.log('Error getting font override values - ', e)
return ''
}
}