@rupert Great tips, thanks! I’ll think about it, but this task may be too advanced for me. To guess which line you’re on, you need to break the text up into lines. Since any character can be bold, italic …, you need to know how many pixels such attributes add to the element width, otherwise you won’t know where the line ends. (You also need to know the width of unstyled characters, which is not a trivial issue for non-monospaced fonts.) Also, since one can drop external css classes on any paragraph (the code highlighting style is external in the demo), you would need access to the external css (for example, to check if there’s margin-top), which, as far as I know, is impossible.
I could get the viewports of all elements on init, but most of the data would have to be updated if the user edits the first half of the text. Asking for more than a few hundred element-viewports adds a big lag, so this method won’t solve lag issues in large documents.
@MartinS: Not sure how you can calculate the width of SVG text characters without adding DOM overhead. You can perhaps use getBBox, but that seems similar to using getViewportOf.