Calculate the carat position (0..text.length inclusive) for a given x in pixels for a given text.
0
text.length
The current implementation is very inefficient when text is long - O(n²). But for simple text fields, this should be sufficient.