-
-
Save Demeter/8787968 to your computer and use it in GitHub Desktop.
Approximating the width of a string in JavaScript
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