| 1 |
% This file is part of the CTAN package named plain-widow. |
| 2 |
% |
| 3 |
% pxwspread.tex: change \plainoutput to \PXspread |
| 4 |
% that writes a report about problematic lines |
| 5 |
% and avoids widow lines for a spread |
| 6 |
% also: club lines and hyphenated words at page break |
| 7 |
% Version 1.0, 13.05.2025 |
| 8 |
% |
| 9 |
% Copyright (C) 2025 Udo Wermuth (author) |
| 10 |
% |
| 11 |
% This program is free software: you can redistribute it and/or modify |
| 12 |
% it under the terms of the GNU General Public License as published by |
| 13 |
% the Free Software Foundation, either version 3 of the License, or |
| 14 |
% (at your option) any later version. |
| 15 |
% |
| 16 |
% This program is distributed in the hope that it will be useful, |
| 17 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
% GNU General Public License for more details. |
| 20 |
% |
| 21 |
% You should have received a copy of the GNU General Public License |
| 22 |
% along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 |
% |
| 24 |
\input pxwsingle.tex |
| 25 |
\newif\ifPXprinttwo %true:do \PXsinglepage twice |
| 26 |
\def\PXuseSpread{% initialize: work with spreads |
| 27 |
\ifPXprinttwo %print with \PXsinglepage 2 pages |
| 28 |
\global\PXprinttwofalse % first of two done |
| 29 |
\else \PXactivateSpread % printing done; switch |
| 30 |
\fi} |
| 31 |
\let\PXpostsingle=\PXuseSpread |
| 32 |
\newcount\PXppts % sum of points for +1 |
| 33 |
\newcount\PXzpts % sum of points for 0 |
| 34 |
\newcount\PXmpts % sum of points for -1 |
| 35 |
\newif\ifPXrightPage % true: check spread break |
| 36 |
\def\PXactivateSpread{% switch output routine |
| 37 |
% initialize 3 registers, the flag, and \PXpts |
| 38 |
% that stores the 1st letter of a register name |
| 39 |
\gdef\PXpts{p}\global\PXppts=0 \global\PXzpts=0 |
| 40 |
\global\PXmpts=0 \global\PXrightPagetrue |
| 41 |
\PXmaxVsize % start with 2\vsize+2\baselineskip |
| 42 |
\global\output={\PXspread}} |
| 43 |
\def\PXbrkPts #1.{% #1: points; add to register |
| 44 |
\expandafter\global\expandafter\advance |
| 45 |
\csname PX\PXpts pts\endcsname by #1 |
| 46 |
\ifPXrightPage % double them for the right page |
| 47 |
\expandafter\global\expandafter\advance |
| 48 |
\csname PX\PXpts pts\endcsname by #1 \fi} |
| 49 |
\def\PXlogPts{\PXlog{Points: \the\PXmpts, |
| 50 |
\the\PXzpts, \the\PXppts}} |
| 51 |
\def\PXevalPtsSetVsize{\PXresetVsize |
| 52 |
% \PXlogPts % to see the points, uncomment line |
| 53 |
\ifnum\PXzpts>1 % ignore club line on left page |
| 54 |
\ifnum\PXzpts>\PXppts \ifnum\PXmpts>\PXppts |
| 55 |
\global\advance\vsize by 1\baselineskip |
| 56 |
\PXminusdist\wlog{SPREAD: \vsize + 1 line}% |
| 57 |
\else\global\advance\vsize by -1\baselineskip |
| 58 |
\PXplusdist\wlog{SPREAD: \vsize - 1 line}\fi |
| 59 |
\else\ifnum\PXzpts>\PXmpts |
| 60 |
\global\advance\vsize by -1\baselineskip |
| 61 |
\PXplusdist\wlog{SPREAD: \vsize - 1 line}% |
| 62 |
\fi\fi\fi} |
| 63 |
\def\PXmaxVsize{%use max \vsize for current page |
| 64 |
\PXresetVsize %start with saved value of \vsize |
| 65 |
\global\advance\vsize by +1\baselineskip |
| 66 |
\ifPXrightPage % double the (\vsize + 1 line) |
| 67 |
\global\advance\vsize by \vsize |
| 68 |
% second \topskip becomes a \baselineskip |
| 69 |
\global\advance\vsize by -1\topskip |
| 70 |
\global\advance\vsize by +1\baselineskip \fi} |
| 71 |
\def\PXdecVsizeChgReg{% change \vsize & register |
| 72 |
\if\PXpts m\else % change \vsize if > minimum |
| 73 |
\global\advance\vsize by -1\baselineskip |
| 74 |
\ifPXrightPage % change value for two lines |
| 75 |
\global\advance\vsize by -1\baselineskip |
| 76 |
\fi\fi % indicate current register with \PXpts |
| 77 |
\xdef\PXpts{\if\PXpts pz% cycle chars: p -> z |
| 78 |
\else\if\PXpts zm\else p\fi\fi}}% z -> m -> p |
| 79 |
\def\PXspread{% main \output for spreads |
| 80 |
\ifnum\outputpenalty =\PXpenW \PXbrkPts16. |
| 81 |
\else\ifnum\outputpenalty=\PXpenBW \PXbrkPts20. |
| 82 |
\else\ifnum\outputpenalty=\PXpenCW \PXbrkPts17. |
| 83 |
\else\ifnum\outputpenalty=\PXpenBCW\PXbrkPts21. |
| 84 |
\else\ifnum\outputpenalty=\PXpenB \PXbrkPts 4. |
| 85 |
\else\ifnum\outputpenalty=\PXpenBC \PXbrkPts 5. |
| 86 |
\else\ifnum\outputpenalty=\PXpenBD \PXbrkPts 4. |
| 87 |
\else\ifnum\outputpenalty=\PXpenBCD\PXbrkPts 5. |
| 88 |
\else\ifnum\outputpenalty=\PXpenC \PXbrkPts 1. |
| 89 |
\else\ifnum\outputpenalty=\PXpenCD \PXbrkPts 1. |
| 90 |
\else\ifnum\outputpenalty<-9999 % forced break |
| 91 |
% switch \output except for 2\vsize + 2 lines |
| 92 |
\if\PXpts p\PXbrkPts32. % wait, assign 64 pts |
| 93 |
\else \let\PXnextStep=\PXprepOutputOfPage \fi |
| 94 |
\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi |
| 95 |
\if\PXpts m\ifPXrightPage % go to left page |
| 96 |
\global\PXrightPagefalse \PXmaxVsize |
| 97 |
\else \let\PXnextStep=\PXprepOutputOfPage \fi |
| 98 |
\fi \PXnextStep} |
| 99 |
\def\PXnextStep{% default action without switch |
| 100 |
\PXdecVsizeChgReg %prepare next \vsize & \PXpts |
| 101 |
\unvbox255 \ifnum\outputpenalty<10000 |
| 102 |
\penalty\outputpenalty \else \penalty0 \fi} |
| 103 |
\def\PXprepOutputOfPage{\PXevalPtsSetVsize |
| 104 |
\ifnum\outputpenalty<-9999 % a forced break |
| 105 |
\unvbox255 \penalty\outputpenalty |
| 106 |
\global\PXprinttwotrue % print 2 single pages |
| 107 |
\global\output={\PXsinglepage}% |
| 108 |
\else \global\holdinginserts=0 |
| 109 |
\unvbox\PrevPageBotMark % restore old \botmark |
| 110 |
\penalty\RestoreBotMark \unvbox255 |
| 111 |
\ifnum\outputpenalty<10000 |
| 112 |
\penalty\outputpenalty \else \penalty0 \fi |
| 113 |
\global\output={\PXsinglespreadpages}\fi} |
| 114 |
\def\PXsinglespreadpages{% print pages of spread |
| 115 |
\ifnum\outputpenalty=\RestoreBotMark |
| 116 |
\setbox0=\vbox{\unvbox255 }% ignore contents |
| 117 |
\else\ifPXrightPage \SaveCurrentBotMark |
| 118 |
\PXlog{(right) spread }\PXoutput \PXresetVsize |
| 119 |
\global\holdinginserts=1 \PXpostspread |
| 120 |
\else \PXlog{OUTPUT: (left) }\PXoutput |
| 121 |
\global\PXrightPagetrue |
| 122 |
\fi\fi} |
| 123 |
\let\PXpostspread=\PXactivateSpread |
| 124 |
\def\startwithSingle{\let\PXpostsingle=\relax |
| 125 |
\output={\PXsinglepage}} |
| 126 |
\def\fromSpreadtoReport{% |
| 127 |
\def\PXpostspread{\global\holdinginserts=0 |
| 128 |
\global\output={\PXoutput}}} |
| 129 |
\def\PXswitchtoSpread{% |
| 130 |
\global\let\PXpostoutput=\relax |
| 131 |
\global\let\PXpostsingle=\PXuseSpread |
| 132 |
\global\let\PXpostspread=\PXactivateSpread |
| 133 |
\PXactivateSpread} |
| 134 |
\def\fromReporttoSpread{% sometimes delayed |
| 135 |
\ifodd\pageno \gdef\PXpostoutput{% |
| 136 |
\SaveCurrentBotMark \global\holdinginserts=1 |
| 137 |
\PXswitchtoSpread}% |
| 138 |
\else \SaveCurrentBotMark |
| 139 |
\global\holdinginserts=1 \PXswitchtoSpread |
| 140 |
\fi} |
| 141 |
\def\fromSingletoSpread{% sometimes delayed |
| 142 |
\def\PXpostsingle{\ifodd\pageno |
| 143 |
\global\let\PXpostsingle=\PXswitchtoSpread |
| 144 |
\else \PXswitchtoSpread \fi}} |
| 145 |
\def\fromSpreadtoSingle{\def\PXpostspread{% |
| 146 |
\global\let\PXpostsingle=\relax |
| 147 |
\global\output{\PXsinglepage}}} |