Skip to content

Instantly share code, notes, and snippets.

@VAKHULA
Created April 7, 2024 07:50
Show Gist options
  • Save VAKHULA/a4e2884af77ec2b009487ec76d2943d5 to your computer and use it in GitHub Desktop.
Save VAKHULA/a4e2884af77ec2b009487ec76d2943d5 to your computer and use it in GitHub Desktop.
Fonts Checker 3
function getRenderedFontFamily(fontFamily) {
let f = ''
let canvas = document.createElement("canvas");
let context = canvas.getContext("2d");
let families = fontFamily.split(',');
for(let family of families) {
if (family == 'inherit') {
f = family;
}
let text = "abcdefghijklmnopqrstuvwxyz";
context.font = "1000px " + family + ", serif";
let serifWidth = context.measureText(text).width;
context.font = "1000px " + family;
let width1 = context.measureText(text).width;
context.font = "1000px " + family + ", sans-serif";
let sansWidth = context.measureText(text).width;
context.font = "1000px " + family;
let width2 = context.measureText(text).width;
if (serifWidth == width1 && sansWidth == width2) {
f = family
}
}
return f;
}
const fonts = {}
const elements = document.body.getElementsByTagName("*");
[...elements].map(element => {
element.focus();
const font = getRenderedFontFamily(window.getComputedStyle(element).getPropertyValue("font-family"));
fonts[font] = 0
})
console.log(Object.keys(fonts).join('\n'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment