3 ms·
This bit is quite funny: Function to work out approximate width of a word based on Arial font letter widths. Probably possible to do by putting the word in an
by axod 18y ago
This bit is quite funny:
Function to work out approximate width of a word based on Arial font letter widths.
Probably possible to do by putting the word in an offscreen div with style set so it's only as wide as content, then checking the size of the div...
function fontWidth(word){
// arial font
var S = "a68b68c68d68e68f31g68h68i31j31k68l31m108\
n68o68p68q68r39s68t39u68v50w90x68y68z68A\
90B90C90D90E90F78G98H90I28J59K90L68M108N\
90O98P90Q98R90S90T68U90V90W130X68Y90Z68'\
30.42!29?68-41/41=72";
var l = word.length;
var s = 0;
var x = 0;
var c = 0;
for(i=0;i<l;i++){
c = word.charAt(i);
x = S.indexOf(c);
if(c>="0" && c<="9")s+=69;
else
if(x>=0)s+=parseInt(S.substring(x+1,x+4));
else s+=68;
}
return s/10;
}
- andreyf 18y agoWhy not just do it with an object of letter -> width key/pairs? then... fontWidth(word) { return sum(map(word.split(''), function (l) letterWidth[l])); } I bet it would be faster in firefox.