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.

Revisions

  1. VAKHULA created this gist Apr 7, 2024.
    40 changes: 40 additions & 0 deletions FontsChecker3.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    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'))