Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Assuming that you're using a single non-proportional font, (so the width of a line is specified as a number of characters rather than a number of centimetres)...</p> <p>There are several parts to your problem.</p> <p>First, you want to word-wrap your text paragraphs into lines of no more than <em>n</em> characters. The basic approach is to first process each paragraph so that it is a single line of text (if you don't already have an input in that form), then use a variable as a 'cursor' - place it at index <em>n</em> and then step it backwards until you find some whitespace. This is the end of the last word that will fit in that line. Copy this line out into a list of lines, and repeat to break the string up into word-wrapped paragraphs.</p> <p>(Note: There are some cases you will have to handle here: You may have to split at punctuation characters and hyphens, and you may have to cope with a "single word" that is longer than the formatting width. For more advanced formatting you may want to add a hyphenation dictionary so you can split words with a hyphen)</p> <p>Once you have your paragraphs you need to apply a similar algorithm to break the document into pages one at a time. Again, start with a 'cursor' position that is <em>m</em> lines into the line-list (where <em>m</em> is the page length). However, you want "widow and orphan" control, so you need to add some logic, e.g:</p> <ul> <li>If the first line(s) of a page are blank, delete them (so you don't get whitespace at the top of the page). This of course means more lines will flow onto the bottom of the page.</li> <li>If the first line of a page is the end of a paragraph (i.e. the second line of the page is blank) then you may want to fix the orphaned line by moving the final line of the previous page to the top of this page. (But only if the orphan is the tail if a paragraph rather than just a very short paragraph!)</li> <li>If the last line of a page is the start of a new paragraph (i.e. the second-to-last line is blank) then move it to the start of the next page.</li> </ul> <p>Fundamentally the process is pretty simple, but there are a lot of little complexities relating to how you want to handle the word-wrap and page-wrap. A simple algorithm won't take long to knock up, but you could spend a lot of time tweaking and improving it to achieve the "best" (in your eyes et least) results.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload