Debian Codegraph @

texmf-dist/tex/plain/plain-grid/plaingridT.tex

raw
1 % This file is part of the CTAN package named plain-grid.
2 %
3 % plaingridT.tex: macros to type straight text in a baseline grid
4 % Version 1.0, 05.05.2026
5 %
6 % Copyright (C) 2026 Udo Wermuth (author)
7 %
8 % This program is free software: you can redistribute it and/or modify
9 % it under the terms of the GNU General Public License as published by
10 % the Free Software Foundation, either version 3 of the License, or
11 % (at your option) any later version.
12 %
13 % This program is distributed in the hope that it will be useful,
14 % but WITHOUT ANY WARRANTY; without even the implied warranty of
15 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 % GNU General Public License for more details.
17 %
18 % You should have received a copy of the GNU General Public License
19 % along with this program. If not, see <http://www.gnu.org/licenses/>.
20 %
21 {\catcode`@=11 \let\\=\noexpand
22 \xdef\gridrestoreparams{\vsize=\the\vsize
23 \baselineskip=\the\baselineskip
24 \lineskip=\the\lineskip
25 \lineskiplimit=\the\lineskiplimit
26 \parskip=\the\parskip
27 \ifr@ggedbottom \\\raggedbottom \else
28 \\\normalbottom\fi \topskip=\the\topskip
29 \interlinepenalty=\the\interlinepenalty
30 \clubpenalty=\the\clubpenalty
31 \widowpenalty=\the\widowpenalty
32 \brokenpenalty=\the\brokenpenalty
33 \let\\\pagebody=\\\GRIDpagebody}}
34 \def\gridlog{ 1 }% 1,2,3: level of verbosity
35 \def\GRIDlog#1#2{% #1: level; #2: message
36 \ifnum#1=0 \immediate\write16{#2}% terminal
37 \else\ifnum#1=1 \wlog{#2}\else% rest to log
38 \ifnum#1>\gridlog\else \wlog{#2}\fi\fi\fi}
39 \skip255=0pt \edef\GRIDnext{\the\skip255}
40 \skip255=\baselineskip \advance\skip255 by
41 -1\skip255 \edef\GRIDtmp{\the\skip255}
42 \ifx\GRIDtmp\GRIDnext\else % make it a dimen
43 \baselineskip=1\baselineskip\GRIDlog0{GRID:
44 \baselineskip is now \the\baselineskip}\fi
45 \newdimen\GRIDgrid \GRIDgrid=\baselineskip
46 \normalbottom \topskip=\GRIDgrid
47 \dimendef\GRIDdim=255 \countdef\GRIDcnt=255
48 \GRIDdim=\vsize \divide\vsize by \GRIDgrid
49 \GRIDcnt=\vsize % save the number of lines
50 \edef\GRIDlines{\the\GRIDcnt\space}
51 \multiply\vsize by \GRIDgrid % new vsize
52 \ifdim\GRIDdim=\vsize \else \GRIDlog0{GRID:
53 \vsize changed; new value \the\vsize\space
54 (\number\GRIDlines\space lines)}\fi
55 \interlinepenalty=0 \brokenpenalty=0
56 \clubpenalty=0 \widowpenalty=0
57 \lineskip=0pt % no minimal interline glue
58 \lineskiplimit=0pt % reassure plain's value
59 \def\GRIDprevgrid{% go to previous baseline
60 \begingroup\ifvmode \GRIDdim=\prevdepth
61 \ifdim\GRIDdim<0pt \GRIDdim=0pt \fi
62 \loop \ifdim\GRIDdim>\GRIDgrid
63 \advance\GRIDdim by -\GRIDgrid
64 \repeat
65 \kern-\GRIDdim \penalty0 \fi
66 \endgroup \ifvmode \prevdepth=0pt \fi}
67 \parskip=0pt
68 \def\gridskiponeline{% create an empty line
69 \par\GRIDprevgrid\kern\GRIDgrid\penalty0 }
70 \def\gridbackoneline{% backspace one line
71 \par\GRIDprevgrid\kern-\GRIDgrid\penalty0 }
72 \newcount\GRIDfreelines % lines left on page
73 \def\GRIDlinesavailable{% set \GRIDfreelines
74 \ifdim\pagegoal=\maxdimen % nothing on page
75 \GRIDfreelines=\GRIDlines
76 \else % mid-page; compute \GRIDfreelines
77 \GRIDfreelines=\pagegoal
78 \advance\GRIDfreelines by -\pagetotal
79 \divide\GRIDfreelines by \GRIDgrid
80 \ifnum\GRIDfreelines<0 %page break delayed
81 \advance\GRIDfreelines by \GRIDlines \fi
82 \fi \GRIDlog2{GRID line \the\inputlineno:
83 available lines = \the\GRIDfreelines}}
84 \def\GRIDvskip#1{% #1: lines to skip
85 \ifnum#1<1 \else \GRIDcnt=#1\relax
86 \gridskiponeline % handle \prevdepth first
87 \loop \advance\GRIDcnt by -1
88 \ifnum\GRIDcnt>0 \line{\hfil}%
89 \repeat \fi}
90 \def\gridskipmax#1{% #1: skip <= #1 lines
91 \par \penalty0 \GRIDlinesavailable
92 \ifnum\GRIDfreelines=\GRIDlines% empty page
93 \else\ifnum#1>\GRIDfreelines
94 \GRIDvskip\GRIDfreelines % skip available
95 \else \GRIDvskip{#1}\fi\fi \par}
96 \def\grideject{\gridskipmax{1000}\eject}
97 \def\GRIDsetGRIDsf{% save \spacefactor
98 \let\GRIDsf=\empty \ifhmode \edef\GRIDsf{%
99 \spacefactor=\the\spacefactor}\/\fi}
100 \newwrite\GRIDenfile % file for endnotes
101 \def\gridenfilename{endnotes}% stem for name
102 \def\GRIDfileno{0 }% number for file names
103 \newif\ifGRIDendnotes % true: file is open
104 \def\GRIDnewenfile{{% create a new file name
105 \GRIDcnt=\GRIDfileno % get current file no
106 \xdef\GRIDcurfile{% new name: stem plus no
107 \gridenfilename\romannumeral\GRIDcnt.tex}%
108 \advance\GRIDcnt by 1 % store next number
109 \xdef\GRIDfileno{\number\GRIDcnt\space}}}
110 \def\GRIDencnt{1 }% the counter for endnotes
111 \def\griduseendnotes{% init endnotes
112 \gdef\GRIDencnt{1 }% reset endnotes counter
113 \ifGRIDendnotes \GRIDlog0{GRID: file for
114 endnotes not yet output: \GRIDcurfile}%
115 \immediate\closeout\GRIDenfile \fi \global
116 \GRIDendnotestrue \GRIDnewenfile \immediate
117 \openout\GRIDenfile=\GRIDcurfile \relax}
118 \def\gridprintendnotes{% output endnotes
119 \ifGRIDendnotes \global\GRIDendnotesfalse
120 \immediate\closeout\GRIDenfile % close the
121 \input \GRIDcurfile % file and input it
122 \else \GRIDenerr \fi}
123 \def\GRIDenerr{\errhelp{Call the macro
124 \gridendnote only between \griduseendnotes
125 and \gridprintendnotes (and this one only
126 once after the first).}\errmessage{Missing
127 \string\griduseendnotes}}
128 \def\gridendnote{% create an endnote
129 \GRIDsetGRIDsf$^{\number\GRIDencnt}$\GRIDsf
130 \ifGRIDendnotes \let\GRIDnext=\GRIDprepen
131 \else\let\GRIDnext=\GRIDenerr\fi \GRIDnext}
132 \let\gridnoteend=\relax % end a \gridendnote
133 \def\gridenmark{Endnotes for page/chapter}
134 \def\GRIDprepen{\immediate\write\GRIDenfile
135 {\noexpand\par\mark{\gridenmark}%
136 \noexpand\textindent{\number\GRIDencnt.}}%
137 {\GRIDcnt=\GRIDencnt \advance\GRIDcnt by 1
138 \xdef\GRIDencnt{\number\GRIDcnt\space}}%
139 \begingroup \def\do##1{\catcode`##1=12}%
140 \dospecials \obeylines % change catcodes
141 \let\GRIDnext=\GRIDcopy \GRIDcopy}
142 {\catcode`/=0 \catcode`\\=12 % end with line
143 /gdef/GRIDhalt{\gridnoteend}}% \gridnoteend
144 {\obeylines \gdef\GRIDcopy#1
145 {\def\GRIDtmp{#1}\ifx\GRIDtmp\GRIDhalt %
146 \def\GRIDnext{\endgroup\space}% add space
147 \else\ifx\GRIDtmp\empty \immediate\write %
148 \GRIDenfile{\noexpand\par\indent}\else %
149 \immediate\write\GRIDenfile{\GRIDtmp}%
150 \fi\fi \GRIDnext}}
151 \newif\ifgridhidesectiontitles % true: hide
152 \def\GRIDmessage#1{\ifgridhidesectiontitles
153 \GRIDlog1{#1}\else \GRIDlog0{#1}\fi}
154 \newif\ifgridnewpageOK % true: accept a move
155 \def\GRIDmakeavailable#1{% #1 required lines
156 \par\penalty0 \GRIDlinesavailable
157 \ifnum\GRIDfreelines<#1 \ifgridnewpageOK
158 \gridnewpageOKfalse \else \GRIDlog0{GRID:
159 unexpected page ejection}\fi \grideject
160 \GRIDfreelines=\GRIDlines\fi\ignorespaces}
161 \outer\def\gridbeginsection#1\par{% #1 title
162 \GRIDmakeavailable6 % mid: 2 above/1 below
163 \ifnum\GRIDfreelines=\GRIDlines % top: 0/1
164 \else \GRIDvskip2\fi \GRIDmessage{#1}%
165 \leftline{\bf#1}\gridskiponeline \noindent}
166 \def\gridkeepspace{ 1 }% 0, or 1, or 2
167 % new page 0: 0/1.0; 1: 0.5/0.5; 2: 1.5/0.5
168 \outer\def\gridstartsection#1\par{% #1 title
169 \GRIDmakeavailable5 \GRIDcnt=\gridkeepspace
170 \ifnum\GRIDfreelines=\GRIDlines % new page:
171 \ifnum\GRIDcnt>1 \line{\hfil}\fi% 2: + 1
172 \ifnum\GRIDcnt>0 \line{\hfil}% 1, 2: + 1
173 \kern-0.5\GRIDgrid \fi % 1, 2: - 0.5
174 \else \gridskiponeline % mid-page: 1.5/0.5
175 \kern0.5\GRIDgrid \GRIDcnt=2 % acts like 2
176 \fi \GRIDmessage{#1}\leftline{\bf#1}%
177 \gridskiponeline \ifnum\GRIDcnt=0 \else % 1
178 \kern-0.5\GRIDgrid\fi \noindent}% >0: - 0.5
179 \outer\def\gridbye{\gridskipmax{1000}%
180 \supereject\end}
181 \newbox\GRIDoutputbox % save page contents
182 \newif\ifgridshowlinenos% true: show numbers
183 \newbox\GRIDlnos % box for line numbers
184 \font\sixrm=cmr6 % font for line numbers
185 \let\GRIDpagebody=\pagebody % save for undo
186 \def\pagebody{% plain & output line numbers
187 \setbox\GRIDoutputbox=\vbox to\vsize{%
188 \boxmaxdepth=\maxdepth \pagecontents}%
189 \vbox{\ifgridshowlinenos \vbox to 0pt{%
190 \rlap{\kern\hsize\copy\GRIDlnos}\vss}\fi
191 \box\GRIDoutputbox}}
192 \def\GRIDsetboxlnos{\GRIDcnt=1
193 \setbox\GRIDlnos=\vbox to\vsize{\hsize=12pt
194 \parfillskip=0pt \prevdepth=0pt \noindent
195 \sixrm \loop \null\hfil \number\GRIDcnt
196 \ifnum\GRIDlines>\GRIDcnt
197 \penalty-10000 \advance\GRIDcnt by 1
198 \repeat \endgraf}}% finish the paragraph
199 \GRIDsetboxlnos % create the line number box
200 \def\gridsnaptonextbaseline{% add a skip to
201 \par\GRIDprevgrid % go to the next baseline
202 \begingroup \dimen0=\pagetotal
203 \divide\dimen0 by \GRIDgrid
204 \multiply\dimen0 by \GRIDgrid
205 \advance\dimen0 by -\pagetotal
206 \ifdim\dimen0<0pt \advance\dimen0 by
207 \GRIDgrid\fi \kern\dimen0\penalty0 % skip
208 \GRIDlog1{GRID line \the\inputlineno:
209 \string\gridsnaptonextbaseline\space uses
210 \the\dimen0}\endgroup}
211 \def\gridskiphalfaline{\par\GRIDprevgrid
212 \kern0.5\GRIDgrid \penalty0 }% use as pair
213 \newbox\GRIDsbox % used instead of \strutbox
214 \setbox\GRIDsbox=\copy\strutbox % fix values
215 \def\gridrule#1(#2){% #1: raise, #2: ht & wd
216 \noindent \def\GRIDtmp{#1}% #1 optional
217 \ifx\GRIDtmp\empty \else \raise
218 \ifdim#1>\ht\GRIDsbox \ht\GRIDsbox % limit
219 \else\ifdim#1<-\dp\GRIDsbox -\dp\GRIDsbox
220 \else #1\fi\fi\fi \hbox{\vbox to 0pt{\vss
221 \hrule width 20pt #2 depth 0pt}}}