Created
November 14, 2013 15:58
-
-
Save gka/7469245 to your computer and use it in GitHub Desktop.
Approximate text width based on estimated character widths...
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
var textWidth = (function() { | |
function charW(w, c) { | |
if (c == 'W' || c == 'M') w += 15; | |
else if (c == 'w' || c == 'm') w += 12; | |
else if (c == 'I' || c == 'i' || c == 'l' || c == 't' || c == 'f') w += 4; | |
else if (c == 'r') w += 8; | |
else if (c == c.toUpperCase()) w += 12; | |
else w += 10; | |
return w; | |
} | |
return function(s) { | |
return _.reduce(s.split(''), charW, 0); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
feel free to multiply the result according to your font size