I have a string, that can contain unicode letters. I want to find out, how "wide" will it be (in letters, not pixels) when it is written on screen in a monospace font.
It might not look so trivial as it seems. Consider this code:
var la = "لا"
console.log(la.length); // prints 2
console.log(la.split); // [ 'ل', 'ا' ]
While لا
has the width of 1 letter in almost all monospace fonts, it's actually 2 letters - ل
and ا
(ignore the writing direction issues, that's a separate thing :) )
Is it possible to find the "visual width" (or how to call it exactly) in Javascript?
In my example, I want to have a function, to which I enter لا
and the result will be 1
, not 2
as in .length
.
(Sorry if it's too confusing, I just don't know how to express myself right.)
All the things I can find is finding a width in pixels. I want to know the width in "monospace letters". How much letters will this string take in monospace?
If it isn't possible, then it's all right.
edit: I found out that it's much easier to do what I wanted to do with css and text-overflow: ellipsis. However, I will keep this question here, maybe it will be helpful for someone else.