Fix text line encoder bug + add unit tests