|Disregard my pevious answer - that's for HTML only.
If it's for an e-reader you have bigger problems: use a library to do this because there are multiple things involved in flowing text. If you've never used LaTeX you are probably not familiar with all the complexities involved:
1. Font kerning: changes the width of a line.
2. Inter-word spacing: algorithm must ensure no rivers run through paragraphs and edges line up.
3. Paragraph indentation: in literary prose all paragraphs have an indentation.
4. Long word wrapping: You need to use a table of pre-calculated breakpoints that are specific to a glyph.
5. Language: Some languages read right-to-left, and these may be in the middle of a sentence in a language which reads left-to-right.
If you're implementing an E-reader then you need to know all of the following concepts:
3. Unicode code-points
4. Unicode BMP
5. Unicode surrogates
6. Unicode characters
7. Unicode glyphs
8. Rivers/runs in text
9. Struts and rules in text
10. Baselines, Caplines
11. Ascenders, descenders
and probably a hundred other typesetting things I forgot or don't know about. Flowing text for an E-reader (or PDF, or book or any typesetting) is an entire Phd topic on its own and can take years of work to implement.
It does help if you've used LaTeX in the past, because it chooses good defaults for all of the above, and if you want to change anything you're forced to learn what all those things mean.