Skip to content

Instantly share code, notes, and snippets.

@lyquix-owner
Created April 17, 2018 21:10
Show Gist options
  • Save lyquix-owner/cf30a3f0a6f955bda049b37c5c6e894b to your computer and use it in GitHub Desktop.
Save lyquix-owner/cf30a3f0a6f955bda049b37c5c6e894b to your computer and use it in GitHub Desktop.
JavaScript font that detects if a font is installed by measuring its dimensions, and comparing them to the dimensions of the default monospace, sans-serif, and serif fonts. The idea is that if a font is not installed it will be replaced by one of those. A know limitation of this approach is that it returns a false-negative for the actual monospa…
var baseFonts = ['monospace', 'sans-serif', 'serif'];
var testChars = ['M', 'g', 'l', 'i', 'ff', 'fi'];
var b = document.getElementsByTagName('body')[0];
var s = document.createElement('span');
s.style.position = 'absolute';
s.style.left = '-9999px';
s.style.fontSize = '100px';
s.style.fontStyle = 'normal';
s.style.fontWeight = 'normal';
s.style.letterSpacing = 'normal';
s.style.lineBreak = 'auto';
s.style.lineHeight = 'normal';
s.style.textTransform = 'none';
s.style.textAlign = 'left';
s.style.textDecoration = 'none';
s.style.textShadow = 'none';
s.style.whiteSpace = 'normal';
s.style.wordBreak = 'normal';
s.style.wordSpacing = 'normal';
s.style.fontFeatureSettings = '"kern" 0, "liga" 0, "calt" 0, "onum" 0, "pnum" 0, "dlig" 0, "tnum" 0, "lnum" 0';
for (var i in baseFonts) {
s.style.fontFamily = baseFonts[i];
var r = {};
for (var j in testChars) {
s.innerText = testChars[j];
b.appendChild(s);
r[testChars[j]] = s.offsetWidth;
b.removeChild(s);
}
baseFonts[i] = btoa(JSON.stringify(r));
}
var testFonts = [
'Andale Mono', 'Arial', 'Arial Black', 'Arial Hebrew', 'Arial MT', 'Arial Narrow', 'Arial Rounded MT Bold', 'Arial Unicode MS',
'Bitstream Vera Sans Mono', 'Book Antiqua', 'Bookman Old Style',
'Calibri', 'Cambria', 'Cambria Math', 'Century', 'Century Gothic', 'Century Schoolbook', 'Comic Sans', 'Comic Sans MS', 'Consolas', 'Courier', 'Courier New',
'Geneva', 'Georgia',
'Helvetica', 'Helvetica Neue',
'Impact',
'Lucida Bright', 'Lucida Calligraphy', 'Lucida Console', 'Lucida Fax', 'LUCIDA GRANDE', 'Lucida Handwriting', 'Lucida Sans', 'Lucida Sans Typewriter', 'Lucida Sans Unicode',
'Microsoft Sans Serif', 'Monaco', 'Monotype Corsiva', 'MS Gothic', 'MS Outlook', 'MS PGothic', 'MS Reference Sans Serif', 'MS Sans Serif', 'MS Serif', 'MYRIAD', 'MYRIAD PRO',
'Palatino', 'Palatino Linotype',
'Segoe Print', 'Segoe Script', 'Segoe UI', 'Segoe UI Light', 'Segoe UI Semibold', 'Segoe UI Symbol',
'Tahoma', 'Times', 'Times New Roman', 'Times New Roman PS', 'Trebuchet MS',
'Verdana', 'Wingdings', 'Wingdings 2', 'Wingdings 3'
];
for (var i in testFonts) {
s.style.fontFamily = testFonts[i];
var r = {};
for (var j in testChars) {
s.innerText = testChars[j];
b.appendChild(s);
r[testChars[j]] = s.offsetWidth;
b.removeChild(s);
}
r = btoa(JSON.stringify(r));
if(baseFonts.indexOf(r) == -1) r = ''
else r = ' not';
console.log(testFonts[i] + r + ' installed');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment