Created
November 10, 2023 18:42
-
-
Save caioagiani/253c91b7b38e1c9af397c9567917f9eb to your computer and use it in GitHub Desktop.
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
function getFonts() { | |
const fontArray = [ | |
"sans-serif-thin", | |
"ARNO PRO", | |
"Agency FB", | |
"Arabic Typesetting", | |
"Arial Unicode MS", | |
"AvantGarde Bk BT", | |
"BankGothic Md BT", | |
"Batang", | |
"Bitstream Vera Sans Mono", | |
"Calibri", | |
"Century", | |
"Century Gothic", | |
"Clarendon", | |
"EUROSTILE", | |
"Franklin Gothic", | |
"Futura Bk BT", | |
"Futura Md BT", | |
"GOTHAM", | |
"Gill Sans", | |
"HELV", | |
"Haettenschweiler", | |
"Helvetica Neue", | |
"Humanst521 BT", | |
"Leelawadee", | |
"Letter Gothic", | |
"Levenim MT", | |
"Lucida Bright", | |
"Lucida Sans", | |
"Menlo", | |
"MS Mincho", | |
"MS Outlook", | |
"MS Reference Specialty", | |
"MS UI Gothic", | |
"MT Extra", | |
"MYRIAD PRO", | |
"Marlett", | |
"Meiryo UI", | |
"Microsoft Uighur", | |
"Minion Pro", | |
"Monotype Corsiva", | |
"PMingLiU", | |
"Pristina", | |
"SCRIPTINA", | |
"Segoe UI Light", | |
"Serifa", | |
"SimHei", | |
"Small Fonts", | |
"Staccato222 BT", | |
"TRAJAN PRO", | |
"Univers CE 55 Medium", | |
"Vrinda", | |
"ZWAdobeF", | |
"Inconsolata", | |
]; | |
const baseFonts = ["monospace", "sans-serif", "serif"]; | |
const testString = "L3V4nT3_1nV3ST1M3nT0S"; | |
const testSize = "72px"; | |
const defaultWidth = {}; | |
const defaultHeight = {}; | |
const isFontAvailable = {}; | |
const bodyElement = document.getElementsByTagName("body")[0]; | |
const spanElement = document.createElement("span"); | |
spanElement.style.fontSize = testSize; | |
spanElement.innerHTML = testString; | |
baseFonts.forEach((font) => { | |
spanElement.style.fontFamily = font; | |
bodyElement.appendChild(spanElement); | |
defaultWidth[font] = spanElement.offsetWidth; | |
defaultHeight[font] = spanElement.offsetHeight; | |
bodyElement.removeChild(spanElement); | |
}); | |
fontArray.forEach((font) => { | |
let detected = false; | |
baseFonts.forEach((baseFont) => { | |
spanElement.style.fontFamily = `${font},${baseFont}`; | |
bodyElement.appendChild(spanElement); | |
const matched = | |
spanElement.offsetWidth !== defaultWidth[baseFont] || | |
spanElement.offsetHeight !== defaultHeight[baseFont]; | |
detected = detected || matched; | |
}); | |
if (detected) isFontAvailable[font] = true; | |
}); | |
return isFontAvailable; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment