Given a block of text, I'd like to chunk it into sections N pixels tall--displayed lines, as computed after applying CSS, and rounding down so you end up with an integer number of lines of text displayed.
I'd like to then display an offset of text and show, e.g., lines 100-120, lines 121-140, etc.
The lines may not all be the same height, and will be wrapping.
How can you do this from JavaScript?
Design goals:
- Display a book, one page at a time.
- Allow readers to jump to a particular page.
- Keep lines together for some sections of text, and avoid widows/orphans (solitary lines at top or bottom of page)
- Have some styles start a new page (e.g. Chapter headers).
Things that don't work:
split
ting the text into fixed chunks by character length. This doesn't address any of the display requirements.- Computing height based on
em
s ahead of time. This can't handle wrapping lines. - Rendering to an off-screen area, setting
overflow: hidden
, and fixing it at a large negative location. Still can't compute the height correctly, so you end up with lines cut in half vertically.