Skip to content

Instantly share code, notes, and snippets.

@harthur
Created April 18, 2012 16:15
Show Gist options
  • Save harthur/2414636 to your computer and use it in GitHub Desktop.
Save harthur/2414636 to your computer and use it in GitHub Desktop.
Get rendered font-family
function getRenderedFontFamily(fontFamily) {
var canvas = document.createElement("canvas");
var context = canvas.getContext("2d");
let families = fontFamily.split(/\s*,\s*/);
for each (let family in families) {
if (family == 'inherit') {
return family;
}
var 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) {
return family;
}
}
return '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment