summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/lua-widow-control/lwc-sample.tex
blob: 6951f7a5bbd536a63ab8f0ce58fb4d6cb6ae3b6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
% lua-widow-control
% https://github.com/gucci-on-fleek/lua-widow-control
% SPDX-License-Identifier: MPL-2.0+ OR CC-BY-SA-4.0+
% SPDX-FileCopyrightText: 2022 Max Chernoff

\startbuffer[demo-text]
    \definepapersize[smallpaper][
        width=6cm,
        height=8.3cm
    ]\setuppapersize[smallpaper]

    \setuplayout[
        topspace=0.1cm,
        backspace=0.1cm,
        width=middle,
        height=middle,
        header=0pt,
        footer=0pt,
    ]

    \def\lwc/{\sans{lua-\allowbreak widow-\allowbreak control}}
    \def\Lwc/{\sans{Lua-\allowbreak widow-\allowbreak control}}

    \setupbodyfont[9pt]
    \setupindenting[yes, 2em]

    \definepalet[layout][grid=middlegray]
    \showgrid[nonumber, none, lines]

    \definefontfeature[default][default][expansion=quality,protrusion=quality]

    \usetypescript[modern-base]
    \setupbodyfont[reset,modern]

    \setupalign[hz,hanging,tolerant]

    \setuplanguage[en][spacing=packed]

    \starttext
        \Lwc/ can remove most widows and orphans from a document, \emph{without} stretching any glue or shortening any pages.

        It does so by automatically lengthening a paragraph on a page where a widow or orphan would otherwise occur. While \TeX{} breaks paragraphs into their natural length, \lwc/ is breaking the paragraph 1~line longer than its natural length. \TeX{}'s paragraph is output to the page, but \lwc/'s paragraph is just stored for later. When a widow or orphan occurs, \lwc/ can take over. It selects the previously-saved paragraph with the least badness; then, it replaces \TeX{}'s paragraph with its saved paragraph. This increases the text block height of the page by 1~line.

        Now, the last line of the current page can be pushed to the top of the next page. This removes the widow or the orphan without creating any additional work.
    \stoptext
\stopbuffer
\savebuffer[list=demo-text]

\startbuffer[shorten]
    \parskip=0pt
    \input lwc-manual-demo-text.tmp
\stopbuffer

\startbuffer[shorten-code]
    \parskip=0pt

    \clubpenalty=10000
    \widowpenalty=10000
\stopbuffer

\startbuffer[stretch]
    \parskip=0pt plus 1fill
    \input lwc-manual-demo-text.tmp
\stopbuffer

\startbuffer[stretch-code]
    \parskip=0pt plus 1fill

    \clubpenalty=10000
    \widowpenalty=10000
\stopbuffer

\startbuffer[ignore]
    \startsetups[*default]
        \clubpenalty=0
        \widowpenalty=0
        \displaywidowpenalty=0
        \interlinepenalty=0
        \brokenpenalty=0
    \stopsetups

    \setups[*default]

    \input lwc-manual-demo-text.tmp
\stopbuffer

\startbuffer[ignore-code]
    \parskip=0pt

    \clubpenalty=0
    \widowpenalty=0
\stopbuffer

\startbuffer[lwc]
    \usemodule[lua-widow-control]
    \input lwc-manual-demo-text.tmp
\stopbuffer

\startbuffer[lwc-code]
    \usepackage{lua-widow-control}
\stopbuffer

\startbuffer[widow-orphan]
    % TODO This is all really quite hacky
    \define[1]\rulewords{\dorecurse{#1}{\blackrule[height=1.5ex, width=1em] \blackrule[height=1.5ex, width=2em] \blackrule[height=1.5ex, width=1.5em] \blackrule[height=1.5ex, width=3em] }}

    \define[2]\fakestart{\framed[width=broad, align=normal, frame=off]{\parfillskip=0pt\spaceskip=0.2em plus 1fill\hskip 5em\rulewords{#1}\blackrule[height=1.5ex, width=#2]}}

    \define[2]\fakeend{\framed[width=broad, align=normal, frame=off]{\parfillskip=5em\spaceskip=0.2em plus 1fill\rulewords{#1}\blackrule[height=1.5ex, width=#2]}}

    \setupTABLE[width=broad, frame=off]
    \setupTABLE[row][1][align=middle, style=\ssbf]
    \setupTABLE[row][2][align=low, frame=on]
    \setupTABLE[row][3][align=high, frame=on]
    \setupTABLE[column][2][frame=off, width=1em]
    \startTABLE
        \NC Widow \NC\NC Orphan \NC\NR
        \NC\fakestart{5}{1.5em}\NC\NC\fakestart{1}{2em}\NC\NR
        \NC\fakeend{1}{2em}\NC\NC\fakeend{5}{1.5em}\NC\NR
    \stopTABLE
\stopbuffer

\startbuffer[nobreak]
    % This is also really hacky and terrible
    \parfillskip=0pt
    \define\lineone{%
        \hbox to 0.3\textwidth{\blackrule[height=1.5ex, width=1em]\hfill%
        \blackrule[height=1.5ex, width=3em]\hfill%
        \blackrule[height=1.5ex, width=1em]\hfill%
        \blackrule[height=1.5ex, width=3em]\hfill%
        \blackrule[height=1.5ex, width=1em]}%
    }

    \define\linetwo{%
        \hbox to 0.3\textwidth{\blackrule[height=1.5ex, width=2em]\hfill%
        \blackrule[height=1.5ex, width=1em]\hfill%
        \blackrule[height=1.5ex, width=2.5em]\hfill%
        \blackrule[height=1.5ex, width=1em]\hfill%
        \blackrule[height=1.5ex, width=2.5em]}%
    }

    \define\linethree{%
        \hbox to 0.3\textwidth{\blackrule[height=1.5ex, width=1em]\hfill%
        \blackrule[height=1.5ex, width=3em]\hfill%
        \blackrule[height=1.5ex, width=1em]\hfill%
        \blackrule[height=1.5ex, width=3em]\hfill%
        \hskip1em\relax}%
    }

    \define\heading{\bold{Heading}}

    \setupTABLE[
        width=broad,
        topframe=off,
        bottomframe=off,
        leftframe=on,
        rightframe=on,
        height=1.2\baselineskip
    ]
    \setupTABLE[row][1][
        align=middle,
        style=\ttbf,
        bottomframe=on,
        leftframe=off,
        rightframe=off
    ]
    \setupTABLE[row][3][bottomframe=on]
    \setupTABLE[row][5][bottomframe=on]
    \setupTABLE[column][2][bottomframe=off, width=1em]
    \setupTABLE[column][4][bottomframe=off, width=1em]
    \startTABLE
        \NC keep     \NC\NC split    \NC\NC warn     \NC\NR
        \NC          \NC\NC          \NC\NC \heading \NC\NR
        \NC          \NC\NC \heading \NC\NC \lineone    \NC\NR
        \NC \heading \NC\NC \lineone \NC\NC \linetwo    \NC\NR
        \NC \lineone \NC\NC \linetwo \NC\NC \linethree  \NC\NR
    \stopTABLE
\stopbuffer