The main problem was that JTextComponent.getText() translates newlines to System.getProperty("line.separator"), which is two characters long on Windows. The internal offset, however, is just based on "\n" newlines. Therefore, the offset always got one character too large per line. Instead, we are now using JTextComponent.getDocument().getText(...), which keeps the internal representation.
Also fixed two problems with boundary conditions.