Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hannes Bornö
committed
Oct 24, 2022
1 parent
9c07993
commit c7c6fce
Showing
11 changed files
with
130 additions
and
98 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.