Initial implementation, taken from RichTextFX.