Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the avg calculations. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md) Co-authored-by: JJ Kasper <jj@jjsweb.site>
- Loading branch information
Showing
12 changed files
with
130 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { Font } from 'fontkit' | ||
import type { AdjustFontFallback } from 'next/font' | ||
|
||
const DEFAULT_SANS_SERIF_FONT = { | ||
name: 'Arial', | ||
azAvgWidth: 934.5116279069767, | ||
unitsPerEm: 2048, | ||
} | ||
const DEFAULT_SERIF_FONT = { | ||
name: 'Times New Roman', | ||
azAvgWidth: 854.3953488372093, | ||
unitsPerEm: 2048, | ||
} | ||
|
||
function calcAverageWidth(font: Font): number | undefined { | ||
const avgCharacters = 'aaabcdeeeefghiijklmnnoopqrrssttuvwxyz ' | ||
const hasAllChars = font | ||
.glyphsForString(avgCharacters) | ||
.flatMap((glyph) => glyph.codePoints) | ||
.every((codePoint) => font.hasGlyphForCodePoint(codePoint)) | ||
|
||
if (!hasAllChars) return undefined | ||
|
||
const widths = font | ||
.glyphsForString(avgCharacters) | ||
.map((glyph) => glyph.advanceWidth) | ||
const totalWidth = widths.reduce((sum, width) => sum + width, 0) | ||
return totalWidth / widths.length | ||
} | ||
|
||
function formatOverrideValue(val: number) { | ||
return Math.abs(val * 100).toFixed(2) + '%' | ||
} | ||
|
||
export function calculateFallbackFontValues( | ||
font: Font, | ||
category = 'serif' | ||
): AdjustFontFallback { | ||
const fallbackFont = | ||
category === 'serif' ? DEFAULT_SERIF_FONT : DEFAULT_SANS_SERIF_FONT | ||
|
||
const azAvgWidth = calcAverageWidth(font) | ||
const { ascent, descent, lineGap, unitsPerEm } = font | ||
|
||
const fallbackFontAvgWidth = fallbackFont.azAvgWidth / fallbackFont.unitsPerEm | ||
let sizeAdjust = azAvgWidth | ||
? azAvgWidth / unitsPerEm / fallbackFontAvgWidth | ||
: 1 | ||
|
||
return { | ||
ascentOverride: formatOverrideValue(ascent / (unitsPerEm * sizeAdjust)), | ||
descentOverride: formatOverrideValue(descent / (unitsPerEm * sizeAdjust)), | ||
lineGapOverride: formatOverrideValue(lineGap / (unitsPerEm * sizeAdjust)), | ||
fallbackFont: fallbackFont.name, | ||
sizeAdjust: formatOverrideValue(sizeAdjust), | ||
} | ||
} |
Binary file added
BIN
+15.3 KB
...vDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib1603gg7S2nfgRYIctxuTB_7Tp05GNyXkb24.woff2
Binary file not shown.
Binary file added
BIN
+5.4 KB
...vDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib1603gg7S2nfgRYIctxuTBv7Tp05GNyXkb24.woff2
Binary file not shown.
Binary file added
BIN
+17.1 KB
...bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib1603gg7S2nfgRYIctxuTCf7Tp05GNyXk.woff2
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.