Page Breaker Algorithm

Here is some extra clarification about algorithms you can use for PageBreaker. Don't take it too seriously, we don't care that much about how well the algorithm performs (except as a reflection of whether the program works at all). Concentrate on the design.

Blanks

1: I am a line
2: I am another line
3:
4: I am a line following a blank line
5:
6:
7: I am a line following two blank lines
8: I am just a line

A good place to break is between lines 2 and 4, or between lines 4 and 7. If, for instance, you break between lines 4 and 7, then line 4 will be the last regular line of the file that you print on that page. You then fill up the remainder of the page with blank lines (enough to get you to the end of the page). You then start again at line 7 (not lines 5 or 6).

Indents and Exdents

1: I am a line
2: I am another line
3:     I am indented relative to the previous line
4:         I am indented even more
5:         I am neither indented nor exdented
6:     I am exdented relative to the previous line
7: I am even more exdented
8: I am neutral

It is considered good to break after a large number of exdented lines in a row, so between lines 7 and 8 is good. It is considered good to break prior to non-indented lines, so between 1 and 2 in the example.

Relative Weights

I wrote this as an awk program about 15 years ago. Reverse engineering that program, I find I used the following algorithm.

For each line we compute the goodness of breaking just before that line to be G.

G = F + B + E + I
F - Filled Pages

F is effectively -infinity if the page is less than 60% filled. At 60% filled, F is -40. At 100% filled, F is 0. It is linear between.

B - Number of blanks lines in a row preceding
0 blank lines before, B = 0
1 blank line before, B = +10
2 blank lines before, B = +25
3 blank lines before, B = +40
4 blank lines before, B = +55
...
E - Number of exdents preceding
0 exdents before, E = 0
1 exdent before, E = +5
2 exdents before, E = +10
3 exdents before, E = +20
4 exdents before, E = +30
...
I - Number of lines since last indent
0 lines since last indent, I = -20
1 line since last indent, I = -16
2 lines since last indent, I = -14.5
3 lines since last indent, I = 0
4 lines isnce last indent, I = 0
...