| 1 |
%% This is part of the OpTeX project, see http://petr.olsak.net/optex |
| 2 |
|
| 3 |
\_codedecl \nopagenumbers {Output routine <2026-05-19>} % preloaded in format |
| 4 |
|
| 5 |
\_doc ----------------------------- |
| 6 |
\`\_optexoutput` is the default output routine. You can create another |
| 7 |
\_cod ----------------------------- |
| 8 |
|
| 9 |
\_output={\_optexoutput} |
| 10 |
\_def \_optexoutput{\_begoutput \_optexshipout\_completepage \_endoutput} |
| 11 |
|
| 12 |
\_doc ----------------------------- |
| 13 |
Default \`\_begoutput` and \`\_endoutput` is defined. |
| 14 |
If you need another functionality implemented in the output routine, you can |
| 15 |
\^`\addto``\_begoutput{...}` or \^`\addto``\_endoutput{...}`. The settings |
| 16 |
here are local in the `\output` group. |
| 17 |
|
| 18 |
The \`\_prepoffsets` can set `\hoffset` differently for the left or right |
| 19 |
page. It is re-defined by the \^`\margins` macro.. |
| 20 |
|
| 21 |
The \^`\_regmark` tokens list includes accumulated `#2` from the \^`\regmacro`. |
| 22 |
Logos and other macros are re-defined here (locally) for their usage |
| 23 |
in headlines or footlines. |
| 24 |
\_cod ----------------------------- |
| 25 |
|
| 26 |
\_def \_begoutput{\_incr\_gpageno |
| 27 |
\_immediate\_wref\_Xpage{{\_the\_gpageno}{\_folio}}% |
| 28 |
\_setxhsize \_prepoffsets \_the\_regmark} |
| 29 |
\_def \_endoutput{\_advancepageno |
| 30 |
{\_globaldefs=1 \_the\_nextpages \_nextpages={}}% |
| 31 |
\_ifnum\_outputpenalty>-20000 \_else\_dosupereject\_fi |
| 32 |
} |
| 33 |
\_def \_prepoffsets {} |
| 34 |
|
| 35 |
\_doc ----------------------------- |
| 36 |
The \`\_optexshipout` does similar work like the `\_shipout` primitive. |
| 37 |
The color literals are added to the `\box0` using |
| 38 |
the \^`\_preshipout``<destination box number><box specification>` pseudo-primitive. |
| 39 |
It is defined using lua code, see section~\ref[lua]. |
| 40 |
Finally the `\_shipout` primitive is used. |
| 41 |
\_cod ----------------------------- |
| 42 |
|
| 43 |
\_def \_optexshipout #1{\_setbox0=#1\_preshipout0\_box0 \_shipout\_box0 } |
| 44 |
|
| 45 |
\_doc ----------------------------- |
| 46 |
The `\hsize` value can be changed at various places in the document but |
| 47 |
we need to have a constant value \`\_xhsize` in the output routine |
| 48 |
(for headlines and footlines, for instance). This value is set |
| 49 |
from the current value of `\hsize` when \`\_setxhsize` macro is called. |
| 50 |
This macro destroys itself, so the value is set only once. Typically it is |
| 51 |
done in \^`\margins` macro or |
| 52 |
when first \^`\_optexoutput` routine is called (see \^`\_begoutput`). |
| 53 |
Or it is called at the beginning of the \^`\begtt`...`\endtt` environment before |
| 54 |
`\hsize` value is eventually changed by the user in this environment. |
| 55 |
\_cod ----------------------------- |
| 56 |
|
| 57 |
\_newdimen \_xhsize \_xhsize=\_hsize |
| 58 |
\_def\_setxhsize {\_global\_xhsize=\_hsize \_glet\_setxhsize=\_relax} |
| 59 |
|
| 60 |
\_doc ----------------------------- |
| 61 |
\`\gpageno` counts pages from one in the whole document |
| 62 |
\_cod ----------------------------- |
| 63 |
|
| 64 |
\_newcount\_gpageno |
| 65 |
\_public \gpageno ; |
| 66 |
|
| 67 |
\_doc ----------------------------- |
| 68 |
The \`\_completepage` is similar to what plain \TeX/ does in its output routine. |
| 69 |
New is only \`\_backgroundbox`. It is `\vbox` with zero height with its |
| 70 |
contents (from \^`\pgbackground`) extended down. It is shifted directly to the |
| 71 |
left-upper corner of the paper. |
| 72 |
Please, correct the words \"left" and/or \"down" in previous two |
| 73 |
sequences if `\pagedirection` is nonzero.\nl |
| 74 |
The \^`\_resetattrs` used here means that all newly created texts in |
| 75 |
output routine (texts used in headline, footline) have default color |
| 76 |
and no transparency. |
| 77 |
\_cod ----------------------------- |
| 78 |
|
| 79 |
\_def\_completepage{\_vbox bdir\_pagedirection{% |
| 80 |
\_resetattrs \_pdfrunninglinkoff |
| 81 |
\_istoksempty \_pgbackground |
| 82 |
\_iffalse \_backgroundbox{\_the\_pgbackground}\_nointerlineskip \_fi |
| 83 |
\_makeheadline |
| 84 |
\_vbox to\_vsize {\_boxmaxdepth=\_maxdepth \_pagecontents}% \pagebody in plainTeX |
| 85 |
\_makefootline}% |
| 86 |
} |
| 87 |
\_def \_backgroundbox #1{\_moveleft |
| 88 |
\_ifodd\_pagedirection \_dimexpr\_pagerightoffset+1in\_else \_hoffset\_fi |
| 89 |
\_vbox to\_zo{\_kern-\_voffset #1\_vss}} |
| 90 |
|
| 91 |
\_doc ----------------------------- |
| 92 |
\`\_makeheadline` creates `\vbox to0pt` with its contents (the \^`\headline`) |
| 93 |
shifted by \^`\headlinedist` up. |
| 94 |
\_cod ----------------------------- |
| 95 |
|
| 96 |
\_def\_makeheadline {\_istoksempty \_headline \_iffalse |
| 97 |
\_vbox to\_zo{\_vss \_baselineskip=\_headlinedist \_lineskiplimit=-\_maxdimen |
| 98 |
\_hbox to\_xhsize{\_normalbaselines\_the\_headline}\_hbox{}% |
| 99 |
}\_nointerlineskip |
| 100 |
\_fi |
| 101 |
} |
| 102 |
|
| 103 |
\_doc ----------------------------- |
| 104 |
The \`\_makefootline` appends the \^`\footline` to the page-body box. |
| 105 |
\_cod ----------------------------- |
| 106 |
|
| 107 |
\_def\_makefootline{\_istoksempty \_footline \_iffalse |
| 108 |
\_baselineskip=\_footlinedist |
| 109 |
\_lineskiplimit=-\_maxdimen \_hbox to\_xhsize{\_normalbaselines\_the\_footline} |
| 110 |
\_fi |
| 111 |
} |
| 112 |
|
| 113 |
\_doc ----------------------------- |
| 114 |
The \`\_pagecontents` is similar as in plain \TeX/. The only difference is |
| 115 |
that the \`\_pagedest` is inserted at the top of `\_pagecontents`.\nl |
| 116 |
The \`\_footnoterule` is defined here. |
| 117 |
\_cod ----------------------------- |
| 118 |
|
| 119 |
\_def\_pagecontents{\_pagedest % destination of the page |
| 120 |
\_ifvoid\_topins \_else \_unvbox\_topins\_fi |
| 121 |
\_dimen0=\_dp255 \_pdfrunninglinkon \_unvbox255 \_pdfrunninglinkoff % open up \box255 |
| 122 |
\_ifvoid\_footins \_else % footnote info is present |
| 123 |
\_vskip\_skip\_footins |
| 124 |
\_footnoterule \_unvbox\_footins \_fi |
| 125 |
\_kern-\_dimen0 \_vskip \_pgbottomskip |
| 126 |
} |
| 127 |
\_def \_pagedest {{\_def\_destheight{25pt}\_dest[pg:\_the\_gpageno]}} |
| 128 |
\_def \_footnoterule {\_kern-3pt \_hrule width 2truein \_kern 2.6pt } |
| 129 |
|
| 130 |
\_doc ----------------------------- |
| 131 |
\`\pageno`, \`\folio`, \`\nopagenumbers`, \`\advancepageno` |
| 132 |
and \`\normalbottom` |
| 133 |
used in the context of the output routine |
| 134 |
from plain \TeX/ is defined here. |
| 135 |
Only the \`\raggedbottom` macro is defined differently. We use the |
| 136 |
\^`\pgbottomskip` register here which is set to 0\,pt by default. |
| 137 |
\_cod ----------------------------- |
| 138 |
|
| 139 |
\_countdef\_pageno=0 \_pageno=1 % first page is number 1 |
| 140 |
\_def \_folio {\_ifnum\_pageno<0 \_romannumeral-\_pageno \_else \_number\_pageno \_fi} |
| 141 |
\_def \_nopagenumbers {\_footline={}} |
| 142 |
\_def \_advancepageno {% |
| 143 |
\_ifnum\_pageno<0 \_decr\_pageno \_else \_incr\_pageno \_fi |
| 144 |
} % increase |pageno| |
| 145 |
\_def \_raggedbottom {\_topskip=\_dimexpr\_topskip plus60pt \_pgbottomskip=0pt plus1fil\_relax} |
| 146 |
\_def \_normalbottom {\_topskip=\_dimexpr\_topskip \_pgbottomskip=0pt\_relax} |
| 147 |
|
| 148 |
\_public \pageno \folio \nopagenumbers \advancepageno \raggedbottom \normalbottom ; |
| 149 |
|
| 150 |
\_doc ----------------------------- |
| 151 |
Macros for footnotes are the same as in plain \TeX. There is only one |
| 152 |
difference: \`\vfootnote` is implemented as \`\_opfootnote` with empty |
| 153 |
parameter `#1`. This parameter should do local settings inside |
| 154 |
the \`\footins` group and it does it when `\fnote` macro is used.\nl |
| 155 |
The `\_opfootnote` nor `\vfootnote` don't take the footnote text |
| 156 |
as a parameter. This is due to a user can do catcode settings (like inline |
| 157 |
verbatim) in the footnote text. This idea is adapted from plain \TeX.\nl |
| 158 |
The \`\footnote` and \`\footstrut` is defined as in plain \TeX/. |
| 159 |
\_cod ----------------------------- |
| 160 |
|
| 161 |
\_newinsert\_footins |
| 162 |
\_def \_footnote #1{\_let\_osf=\_empty % parameter #2 (the text) is read later |
| 163 |
\_ifhmode \_edef\_osf{\_spacefactor\_the\_spacefactor}\/\_fi |
| 164 |
#1\_osf\_vfootnote{#1}} |
| 165 |
\_def\_vfootnote{\_opfootnote{}} |
| 166 |
\_def \_opfootnote #1#2{\_insert\_footins\_bgroup |
| 167 |
\_interlinepenalty=\_interfootnotelinepenalty |
| 168 |
\_leftskip=\_zo \_rightskip=\_zo \_spaceskip=\_zo \_xspaceskip=\_zo \_relax |
| 169 |
\_resetattrs |
| 170 |
#1\_relax % local settings used by \fnote macro |
| 171 |
\_splittopskip=\_ht\_strutbox % top baseline for broken footnotes |
| 172 |
\_splitmaxdepth=\_dp\_strutbox \_floatingpenalty=20000 |
| 173 |
\_textindent{#2}\_footstrut |
| 174 |
\_isnextchar \_bgroup |
| 175 |
{\_bgroup \_aftergroup\_vfootA \_afterassignment\_ignorespaces \_let\_next=}{\_vfootB}% |
| 176 |
} |
| 177 |
\_def\_vfootA{\_unskip\_strut\_egroup} |
| 178 |
\_def\_vfootB #1{#1\_unskip\_strut\_egroup} |
| 179 |
\_def \_footstrut {\_vbox to\_splittopskip{}} |
| 180 |
\_skip\_footins=\_bigskipamount % space added when footnote is present |
| 181 |
\_count\_footins=1000 % footnote magnification factor (1 to 1) |
| 182 |
\_dimen\_footins=8in % maximum footnotes per page |
| 183 |
\_public |
| 184 |
\footins \footnote \vfootnote \footstrut ; |
| 185 |
|
| 186 |
\_doc ----------------------------- |
| 187 |
The \`\topins` macros |
| 188 |
\`\topinsert`, \`\midinsert`, \`\pageinsert`, \`\endinsert` |
| 189 |
are the same as in plain \TeX/. |
| 190 |
\_cod ----------------------------- |
| 191 |
|
| 192 |
\_newinsert\_topins |
| 193 |
\_newifi\_ifupage \_newifi\_ifumid |
| 194 |
\_def \_topinsert {\_umidfalse \_upagefalse \_oins} |
| 195 |
\_def \_midinsert {\_umidtrue \_oins} |
| 196 |
\_def \_pageinsert {\_umidfalse \_upagetrue \_oins} |
| 197 |
\_skip\_topins=\_zoskip % no space added when a topinsert is present |
| 198 |
\_count\_topins=1000 % magnification factor (1 to 1) |
| 199 |
\_dimen\_topins=\_maxdimen % no limit per page |
| 200 |
\_def \_oins {\_par \_begingroup\_setbox0=\_vbox\_bgroup\_resetattrs} % start a \_vbox |
| 201 |
\_def \_endinsert {\_par\_egroup % finish the \_vbox |
| 202 |
\_ifumid \_dimen0=\_ht0 \_advance\_dimen0 by\_dp0 \_advance\_dimen0 by\_baselineskip |
| 203 |
\_advance\_dimen0 by\_pagetotal \_advance\_dimen0 by-\_pageshrink |
| 204 |
\_ifdim\_dimen0>\_pagegoal \_umidfalse \_upagefalse \_fi \_fi |
| 205 |
\_ifumid \_bigskip \_box0 \_bigbreak |
| 206 |
\_else \_insert \_topins {\_penalty100 % floating insertion |
| 207 |
\_splittopskip=0pt |
| 208 |
\_splitmaxdepth=\_maxdimen \_floatingpenalty=0 |
| 209 |
\_ifupage \_dimen0=\_dp0 |
| 210 |
\_vbox to\_vsize {\_unvbox0 \_kern-\_dimen0}% depth is zero |
| 211 |
\_else \_box0 \_nobreak \_bigskip \_fi}\_fi\_endgroup} |
| 212 |
|
| 213 |
\_public \topins \topinsert \midinsert \pageinsert \endinsert ; |
| 214 |
|
| 215 |
\_doc ----------------------------- |
| 216 |
The \`\draft` macro is an example of usage `\_pgbackground` to create |
| 217 |
watercolor marks. |
| 218 |
\_cod ----------------------------- |
| 219 |
|
| 220 |
\_def \_draft {\_pgbackground={\_draftbox{\_draftfont DRAFT}}% |
| 221 |
\_fontdef\_draftfont{\_setfontsize{at10pt}\_bf}% |
| 222 |
\_glet\_draftfont=\_draftfont |
| 223 |
} |
| 224 |
\_def \_draftbox #1{\_setbox0=\_hbox{\_setgreycolor{.8}#1}% |
| 225 |
\_kern.5\_vsize \_kern\_voffset \_kern4.5\_wd0 |
| 226 |
\_hbox to0pt{\_kern.5\_xhsize \_kern\_hoffset \_kern-2\_wd0 |
| 227 |
\_pdfsave \_pdfrotate{55}\_pdfscale{10}{10}% |
| 228 |
\_hbox to0pt{\_box0\_hss}% |
| 229 |
\_pdfrestore |
| 230 |
\_hss}% |
| 231 |
} |
| 232 |
\_public \draft ; |
| 233 |
|
| 234 |
\_endcode % ------------------------------------- |
| 235 |
|
| 236 |
|
| 237 |
The output routine \^`\_optexoutput` is similar as in plain \TeX. It does: |
| 238 |
|
| 239 |
\begitems |
| 240 |
* \^`\_begoutput` which does: |
| 241 |
\begitems |
| 242 |
* increments \^`\gpageno`, |
| 243 |
* prints `\_Xpage{<gpageno>}{<pageno>}` to the `.ref` file |
| 244 |
(if \^`\openref` is active), |
| 245 |
* calculates `\hoffset`, |
| 246 |
* sets local meaning of macros used in headlines/footlines (see \^`\regmacro`). |
| 247 |
\enditems |
| 248 |
* `\shipout`\^`\_completepage`, which is `\vbox` of -- |
| 249 |
\begitems |
| 250 |
* backrground box, if \^`\pgbackground` is non-empty, |
| 251 |
* headline box by \^`\_makeheadline`, if the \^`\headline` is nonempty, |
| 252 |
* `\vbox to\vsize` of \^`\_pagecontents` which cosnists of -- |
| 253 |
\begitems |
| 254 |
* \^`\_pagedest`, the page destination `pg:<gpageno>` for hyperlinks is created here, |
| 255 |
* \^`\topins` box if non-empty (from \^`\topinsert`s), |
| 256 |
* `\box255` with completed vertical material from main vertical mode, |
| 257 |
* \^`\_footnoterule` and \^`\footins` box if nonempty (from \^`\fnote`, \^`\footnote`), |
| 258 |
* \^`\pgbottomskip` (default is 0\,pt). |
| 259 |
\enditems |
| 260 |
* footline box by `\_makefootline`, if the \^`\footline` is nonempty |
| 261 |
\enditems |
| 262 |
* \^`\_endoutput` which does: |
| 263 |
\begitems |
| 264 |
* increments \^`\pageno` using \^`\advancepageno` |
| 265 |
* runs output routine repeatedly if \^`\dosupereject` is activated. |
| 266 |
\enditems |
| 267 |
\enditems |
| 268 |
|
| 269 |
\_endinput |
| 270 |
|
| 271 |
2026-05-19 \_pdfrunninglinkoff, \_pdfrunninglinkon re-arranged |
| 272 |
2026-04-04 \_backgroundox moved left by \_pagerightoffset+1in if \pagedirection is odd |
| 273 |
2026-03-26 set \_vbox direction to \_pagedirection in \_completepage |
| 274 |
2025-05-04 \_pdfrunninglinkoff, \_pdfrunninglinkon moved, used only if non-empty \headline |
| 275 |
2024-02-29 \_pdfrunninglinkoff, \_pdfrunninglinkon used |
| 276 |
2022-04-28 \_optexshipout introduced |
| 277 |
2022-10-22 \_normalbaselines added to \makehead/foot/line |
| 278 |
2022-03-07 \_resetattrs instead \_resetcolor |
| 279 |
2021-07-16 output routine supports colors via attributes |
| 280 |
2021-02-25 \_draftbox improved |
| 281 |
2021-02-15 \_advance -> \_decr |
| 282 |
2020-05-12 \vfootB: \uskip -> \unskip bug fixed |
| 283 |
2020-03-28 Released |