Created
April 18, 2012 16:15
-
-
Save harthur/2414636 to your computer and use it in GitHub Desktop.
Get rendered font-family
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 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