summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/novel/lualatex/novel-HeadFootStyles.sty
blob: 6cbc0c487153fe35125929b07f648e245b621804 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
%%
%% This is file `novel-HeadFootStyles.sty', part of `novel' document class.
%% Copyright 2017-2023 Robert Allgeyer.
%%
%% 
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%    http://www.latex-project.org/lppl.txt
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%% 
%%
\ProvidesFile{novel-HeadFootStyles.sty}%
[2023/03/25 v1.81a LaTeX file (header and footer layouts and styles)]
%% 



%% File `novel-LayoutSettings.sty' reserved space for header/footer,
%% but did not place anything there. In this file, `fancyhdr' syntax
%% defines the layout. The layout are not activated until
%% \AtEndPreamble, and only if the user did not already define the layout
%% in the Preamble.

\gdef\@HeadFootStyle{1} % default: header but not footer
\gdef\@VersoEmblem{} % no default emblem
\gdef\@RectoEmblem{} % no default emblem


%% SET HEADER/FOOTER STYLE AND RESERVE SPACE FOR HEADER/FOOTER
%% ----------------------------------------------------------------------------
% There are several pre-configured header/footer styles, addressing every
%   situation likely to be seen in popular fiction. The style details are
%   coded, using `fancyhdr' syntax, in novel-HeadFootStyles.sty.
% If you wish to create a custom head/foot style, you must first choose
%   one of the pre-configured styles, then customize it. That way,
%   the layout engine knows whether header or footer are present.
\newif \if@StyledHeader % true if user-customized header using \fancyhead
\newif \if@StyledFooter % true if user-customized footer using \fancyfoot
%
\gdef\SetHeadFootStyle#1{%
  \if@coverart
    \gdef\@HeadFootStyle{0} % required: no header/footer for coverart
    \ifthenelse{\equal{#1}{0}}{}{%
      \ClassWarning{novel}{^^JClass option `coverart' does not %
        allow header/footer. Ignored. ^^J}
    }%
  \else
    \@tempTFfalse
    \ifthenelse{\equal{#1}{0}}{%
      \global\@HasHeaderfalse\global\@HasFooterfalse\@tempTFtrue%
    }{}
    \ifthenelse{\equal{#1}{1}}{%
      \global\@HasHeadertrue\global\@HasFooterfalse\@tempTFtrue%
    }{}
    \ifthenelse{\equal{#1}{2}}{%
      \global\@HasHeaderfalse\global\@HasFootertrue\@tempTFtrue%
    }{}
    \ifthenelse{\equal{#1}{3}}{%
      \global\@HasHeaderfalse\global\@HasFootertrue\@tempTFtrue%
    }{}
    \ifthenelse{\equal{#1}{4}}{%
      \global\@HasHeadertrue\global\@HasFooterfalse\@tempTFtrue%
    }{}
    \ifthenelse{\equal{#1}{5}}{%
      \global\@HasHeadertrue\global\@HasFootertrue\@tempTFtrue%
    }{}
    \ifthenelse{\equal{#1}{6}}{%
      \global\@HasHeadertrue\global\@HasFootertrue\@tempTFtrue%
    }{}
    % Ensure that user choice was on the allowed list:
    \if@tempTF\else
      \ClassError{novel}{Invalid choice for \string\SetHeadFootStyle}%
        {\string\SetHeadFootStyle\space needs choice of 0,1,2,3,4,5,6, ^^J%
        even if you wish to customize using fancyhdr syntax.}%
    \fi
    \gdef\@HeadFootStyle{#1}
  \fi
}%
\SetHeadFootStyle{1} % default, header only
\let\SetHeadStyle\SetHeadFootStyle\relax % deprecated
%
% \HeadJump is a number from 1 to 3 (may be decimal). It measures the
%   distance from baseline of header text, to first baseline of main text,
%   then divides by the normal baseline.
% So, a \HeadJump of 1 means that the header text is positioned exactly
%   one line above main text (unlikely). A value of 2 skips a whole line.
% The default value is 1.5, which is often used in popular fiction.
% No matter what value is set, it will be re-set to 0 later, if the
%   choice of head/foot style does not have a header.
% Likewise for \FootJump, measuring from baseline of lowermost main text,
%   to baseline of footer text.
\gdef\SetHeadJump#1{%
  \IfDecimal{#1}{%
    \FPiflt{#1}{1}
      \ClassError{novel}{\string\SetHeadJump\space must be at least 1}%
      {\string\SetHeadJump\string from 1 to 3, may be decimal.}%
    \fi
    \FPifgt{#1}{3}
      \ClassError{novel}{\string\SetHeadJump\space must not exceed 3}%
      {\string\SetHeadJump\string from 1 to 3, may be decimal.}%
    \fi
    \gdef\@HeadJump{#1}
  }{%
    \ClassError{novel}{Unable to parse \string\SetHeadJump\space argument}%
    {Needs integer or decimal, but not ending in decimal point or comma. ^^J%
     Must be a number from 1 to 3, but may not be a macro.}%
  }%
}
\SetHeadJump{1.5} % default
%
\gdef\SetFootJump#1{%
  \IfDecimal{#1}{%
    \FPiflt{#1}{1}
      \ClassError{novel}{\string\SetFootJump\space must be at least 1}%
      {\string\SetFootJump\string from 1 to 3, may be decimal.}%
    \fi
    \FPifgt{#1}{3}
      \ClassError{novel}{\string\SetFootJump\space must not exceed 3}%
      {\string\SetFootJump\string from 1 to 3, may be decimal.}%
    \fi
    \gdef\@FootJump{#1}
  }{%
    \ClassError{novel}{Unable to parse \string\SetFootJump\space argument}%
    {Needs integer or decimal, but not ending in decimal point or comma. ^^J%
     Must be a number from 1 to 3, but may not be a macro.}%
  }%
}
\SetFootJump{1.5} % default
%
%% end set style and reserve space for header/footer.


%% Used in novel-headfootstyles.sty:
% \SetLooseHead looseness factor (fontspec LetterSpace) 0=tight, 50=default
\gdef\SetLooseHead#1{
  \FPdiv{\@loosehead}{#1}{10} % change method fontspec w/ microtype
  \FPmin{\@looseheadN}{\@loosehead}{50} % don't want numbers too loose
  \FPdiv{\@looseheadword}{\@loosehead}{30} %
  \FPadd{\@looseheadword}{\@looseheadword}{1} %
}
\SetLooseHead{50}
%%

%
% See docs for what these emblems do, if used:
\newcommand\SetEmblems[2]{ % verso, recto
  \gdef\@VersoEmblem{{\headfont #1}}
  \gdef\@RectoEmblem{{\headfont #2}}
}
\let\SetEmblem\SetEmblems% for convenience
% If you want something fancier than \thepage:
\gdef\SetPageNumberStyle#1{% no small caps, so lowercase roman preserved
  \gdef\pagenumberstyle{{\addfontfeature{Letters=ResetAll}#1}}
}
\SetPageNumberStyle{\thepage}% no small caps
% See novel.cls for the accompanying AtBeginDocument routine.


%% INITIALIZE FANCYHDR
%% ----------------------------------------------------------------------------
% Earlier, space was reserved for header/footer, but the content of
%   headers and footers was not specified. Now, package `fancyhdr'
%   will be used to create the content.
% Start by loading package `fancyhdr' and blanking everything:
\RequirePackage{fancyhdr}
  \renewcommand\headrulewidth{0pt}
  \renewcommand\footrulewidth{0pt}
  \fancyhead[LO,RE,LE,RO,CE,CO]{}
  \fancyfoot[LO,RE,LE,RO,CE,CO]{}
% It might be useful to know whether something is being placed in main text,
%   or within a header/footer. A new boolean is created, which is only true
%   when within  a header or footer. You can define macros so that they test
%   this boolean.
% To make this work, \fancyhead and \fancyfoot are re-defined:
\newtoggle{@inheadfoot} % true within header or footer
\LetLtxMacro{\@myfancyhead}{\fancyhead}
\LetLtxMacro{\@myfancyfoot}{\fancyfoot}
\renewcommand\fancyhead[2][]{%
  \global\@StyledHeadertrue%
  \@myfancyhead[#1]{\toggletrue{@inheadfoot}{\normalsize #2}}% not global
}
\renewcommand\fancyfoot[2][]{%
  \global\@StyledFootertrue%
  \FPsub{\@footraise}{\@FootJump}{1}%
  \FPmul{\@footraise}{-\@footraise}{\strip@pt\nbs}%
  \@myfancyfoot[#1]{%
    \toggletrue{@inheadfoot}% not global
    \stake\smash{\raisebox{\@footraise pt}{\normalsize #2}}%
  }%
}
%% end initialize fancyhdr.


%% THISPAGESTYLE COMMANDS
%% ----------------------------------------------------------------------------
% Re-define \thispagestyle based on layout. When it requests only the page
% number, decide what to do based on this command and whether footer is used:
\newif \if@ThisPageStyle % true if \thispagestyle declared for current page
\newif \if@DeleteCSline % true when using @dropfolioinside
\AtBeginShipout{% re-sets when page done
  \global\@DeleteCSlinefalse%
  \global\@ThisPageStylefalse%
}
\LetLtxMacro{\@myTempTPS}{\thispagestyle}
%
\gdef\thispagestyle#1{%
  \global\@ThisPageStyletrue%
  \@tempTFfalse%
  % Style `fancy' is same as normal style:
  \ifthenelse{\equal{#1}{fancy}}{%
    \@tempTFtrue%
    \@myTempTPS{fancy}%
  }{}%
  % Style `empty' has no visible content in header or footer.
  % This style is pre-defined, so no need to re-define it here.
  \ifthenelse{\equal{#1}{empty}}{%
    \@tempTFtrue%
    \@myTempTPS{empty}%
  }{}%
  % Style `footer' has no visible content in header.
  % If the normal style has a footer, it will be used as-is. However, if the
  %   normal style does not have a footer, then (unlike the above style)
  %   the page number will not be placed at bottom; as with style `empty'.
  \ifthenelse{\equal{#1}{footer}}{%
    \@tempTFtrue%
    \if@HasFooter\@myTempTPS{@footer}%
    \else\@myTempTPS{empty}%
    \fi%
  }{}%
  % Style `forcenumber' is rarely used. If the normal style has a footer,
  %   it will be used as-is (presumably contains the page number), and there
  %   will be no visible content in the header.
  % But if there is no normal footer, then the outside portion of the header
  %   is printed. No other portion of the header. Presumably the page number
  %   is located at the outside of the header.
  \ifthenelse{\equal{#1}{forcenumber}}{%
    \@tempTFtrue%
    \if@HasFooter\@myTempTPS{@footer}%
    \else\@myTempTPS{@forcenumber}%
    \fi%
  }{}%
  % Style `dropfoliobeneath': There will be no visible content in header.
  % If the normal style has a footer, it will be used as-is. This assumes
  %   that the page number is contained in the footer, the usual case.
  % If the normal style has no footer, then the page number will be centered
  %   within the lower margin, at the position where an exta line would be.
  %   Thus, pagination of the main text is unaffected.
  % Since the number is closer than usual to the page bottom edge, you must
  %   ensure that it does not intrude into the unsafe zone (where printers
  %   prohibit any printable matter). TeX cannot check this for you. However,
  %   you can define an Unsafe Zone, and inspect placement of the number
  %   in draft mode.
  % Styles `plain*' and `fancyplain*' (with asterisk) are aliases.
  \ifthenelse{\equal{#1}{dropfoliobeneath}%
    \OR \equal{#1}{plain*} \OR \equal{#1}{fancyplain*}}{%
    \@tempTFtrue%
    \if@HasFooter\@myTempTPS{@footer}%
    \else%
      \@myTempTPS{@dropfoliobeneath}%
      \typeout{^^JClass `novel' Alert: Page no. \thepage\space in margin. ^^J%
        Review layout to ensure that this is within the Safe Area. ^^J%
      }%
    \fi%
  }{}%
  % Style `dropfolioinside': There will be no visible content in header.
  % If the normal style has a footer, it will be used as-is. This assumes
  %   that the page number is contained in the footer, the usual case.
  % If the normal style has not footer, then the page number will be centered
  %   where the last line of main text would ordinarily be. No extra gap.
  %   That may cause the (former) last line of main text to flow to the top of
  %   the next page, and so forth. However, if this page style is used on
  %   a page with ChapterStart, then the height of ChapterStart will be
  %   automatically reduced by one line, so that there is no repagination.
  % Styles `plain', `fancyplain', `dropfolio', and `dropfoliolater' are aliases
  %   for `dropfolioinside'. I couldn't make up my mind regarding the
  %   command name. For compatibility, all these do the same thing.
  \ifthenelse{\equal{#1}{dropfolioinside}%
  \OR \equal{#1}{plain} \OR \equal{#1}{fancyplain}%
  \OR \equal{#1}{dropfolio} \OR \equal{#1}{dropfoliolater}}{%
    \@tempTFtrue%
    \if@HasFooter\@myTempTPS{@footer}%
    \else%
      \global\@DeleteCSlinetrue%
      \enlargethispage{-\nbs}%
      \@myTempTPS{@dropfolioinside}%
    \fi%
  }{}%
  % Verify that an allowable choice was made:
  \if@tempTF\else%
    \ClassError{novel}{^^JPage \thepage\space has \string\thispagestyle ^^J%
     but its argument is not on the list of choices. See docs. ^^J}%
  \fi%
} % end \thispagestyle.
%
\let\dropfolionow\relax % compatibility with earlier version.
%
% Here are the thispagestyle definitions:
\fancypagestyle{@footer}{ % blanks header, but leaves footer as-is
  \renewcommand\headrulewidth{0pt}
  \renewcommand\footrulewidth{0pt}
  \fancyhead[LO,RE,LE,RO,CE,CO]{}
}
\fancypagestyle{@dropfolioinside}{ % page number where last line would be
  \renewcommand\headrulewidth{0pt}
  \renewcommand\footrulewidth{0pt}
  \fancyhead[LO,RE,LE,RO,CE,CO]{}
  \fancyfoot[LO,RE,LE,RO]{}
  \fancyfoot[CO,CE]{%
    \raisebox{\dimexpr\@FootJump\nbs}{\@lspagenum\pagenumberstyle}%
  }
}
\fancypagestyle{@dropfoliobeneath}{ % page number in margin below text
  \renewcommand\headrulewidth{0pt}
  \renewcommand\footrulewidth{0pt}
  \fancyhead[LO,RE,LE,RO,CE,CO]{}
  \fancyfoot[LO,RE,LE,RO]{}
  \fancyfoot[CO,CE]{%
    \raisebox{\dimexpr\@FootJump\nbs-\nbs}{\@lspagenum\pagenumberstyle}%
  }
}
\fancypagestyle{@forcenumber}{ % if has header but not footer
  \renewcommand\headrulewidth{0pt}
  \renewcommand\footrulewidth{0pt}
  \fancyhead[LO,RE,LE,RO,CE,CO]{}
  \fancyhead[LE]{%
    \makebox[2.5em][l]{\@lspagenum\pagenumberstyle}%
    \@VersoEmblem%
  }
  \fancyhead[RO]{%
    \@RectoEmblem%
    \makebox[2.5em][r]{\@lspagenum\pagenumberstyle}%
  }
  \fancyfoot[LO,RE,LE,RO,CE,CO]{}
}
%% end \thispagestyle commands.


%%
\gdef\@lshftext{\headfont\addfontfeature{LetterSpace=\@loosehead}}
\gdef\@lspagenum{\headfont\addfontfeature{LetterSpace=\@looseheadN}}
%%

% Content of header text (if present) can be changed at any time within
%   the body, using the following commands. If not used, then
%   Verso is initialized to \theauthor and Recto is initialized to \thetitle
%   AtBeginDocument, with adjustments to interword spacing.
% If you wish, you may write the commands with empty arguments, in which case
%   the header still appears with page number (if present), but no text.
%
\gdef\NewVersoHeadText#1{\gdef\versoheadtext{#1}}
\let\SetVersoHeadText\NewVersoHeadText\relax % for convenience
\let\RenewVersoHeadText\NewVersoHeadText\relax % for convenience
%
\gdef\NewRectoHeadText#1{\gdef\rectoheadtext{#1}}
\LetLtxMacro\SetRectoHeadText\NewRectoHeadText\relax % for convenience
\LetLtxMacro\RenewRectoHeadText\NewRectoHeadText\relax % for convenience
%
%%


%% See AtBeginDocument routine in novel.cls:
%% ----------------------------------------------------------------------------
\gdef\@ActivateHeadFootStyles{ % called AtBeginDocument by novel.cls
  % Pre-configured header/footer styles, unless user over-rode them:
  % \versoheadtext is initialized to \theauthor
  % \rectoheadtext is initialized to \thetitle
  \@ifundefined{versoheadtext}{
    \NewVersoHeadText{\theauthor}
  }{}
  \@ifundefined{rectoheadtext}{
    \NewRectoHeadText{\thetitle}
  }{}
  %
  % ---------------------------------------------------------------------------
  % IF YOU ARE WRITING YOUR OWN HEADER/FOOTER LAYOUT:
  % Look here for the pre-defined layouts, for use as models.
  % Note that if you write \fancyhead or \fancyfoot in the Preamble,
  %   that sets the \@StyledHeader flag, so these codes do not over-ride yours.
  % \@lshftext loosens text tracking.
  % \@lspagenum loosens page number tracking, like \@lshftext but capped
  %   at a maximum looseness.
  \ifthenelse{\equal{\@HeadFootStyle}{1}}{ % default
    \if@StyledHeader\else
      \fancyhead[LE]{%
        \makebox[2.5em][l]{\@lspagenum\pagenumberstyle}%
        \@VersoEmblem%
      }
      \fancyhead[RO]{%
        \@RectoEmblem%
        \makebox[2.5em][r]{\@lspagenum\pagenumberstyle}%
      }
      \fancyhead[CE]{\@lshftext\versoheadtext}
      \fancyhead[CO]{\@lshftext\rectoheadtext}
    \fi
  }{}
  %
  \ifthenelse{\equal{\@HeadFootStyle}{2}}{
    \global\@StyledHeaderfalse
    \if@StyledFooter\else
      \fancyfoot[LE]{%
        \makebox[2.5em][l]{\@lspagenum\pagenumberstyle}%
        \@VersoEmblem%
      }
      \fancyfoot[RO]{%
        \@RectoEmblem%
        \makebox[2.5em][r]{\@lspagenum\pagenumberstyle}%
      }
    \fi
  }{}
  %
  \ifthenelse{\equal{\@HeadFootStyle}{3}}{
    \if@StyledFooter\else
      \fancyfoot[CO,CE]{\@lspagenum\pagenumberstyle}
    \fi
  }{}
  %
  \ifthenelse{\equal{\@HeadFootStyle}{4}}{
    \if@StyledHeader\else
      \fancyhead[LE]{%
        \makebox[2.5em][l]{\@lspagenum\pagenumberstyle}%
        \@VersoEmblem%
        \hspace{1em}%
        {\@lshftext\versoheadtext}%
      }
      \fancyhead[RO]{%
        {\@lshftext\rectoheadtext}%
        \hspace{1em}%
        \@RectoEmblem%
        \makebox[2.5em][r]{\@lspagenum\pagenumberstyle}%
      }
    \fi
  }{}
  %
  \ifthenelse{\equal{\@HeadFootStyle}{5}}{
    \if@StyledHeader\else
        \fancyhead[CE]{\@lshftext\versoheadtext}
        \fancyhead[CO]{\@lshftext\rectoheadtext}
    \fi
    \if@StyledFooter\else
      \fancyfoot[CO,CE]{%
        {\@lspagenum\pagenumberstyle}%
      }
    \fi
  }{}
  %
  \ifthenelse{\equal{\@HeadFootStyle}{6}}{
    \if@StyledHeader\else
      \fancyhead[LE]{%
        \makebox[2.5em][l]{\@lspagenum\pagenumberstyle}%
        \@VersoEmblem%
      }
      \fancyhead[RO]{%
        \@RectoEmblem%
        \makebox[2.5em][r]{\@lspagenum\pagenumberstyle}%
      }
      \fancyhead[RE]{\@lshftext\versoheadtext}
      \fancyhead[LO]{\@lshftext\rectoheadtext}
    \fi
  }{}
  % END MODELS FOR WRITING YOUR OWN.
  %
  % END PRE-DEFINED LAYOUTS AND STYLES.
  % ---------------------------------------------------------------------------
  %
  % Now put `fancyhdr' to work:
  \pagestyle{fancy} % default unless over-ridden by \thispagestyle{}
  %
} % end \@ActivateHeadFootStyles
%
%%


%% Neutralize settings that cannot be used after Preamble:
\gdef\@DisableHeadFootSettings{% called by `novel.cls' \AtBeginDocument
  \LetLtxMacro\SetHeadFootStyle\relax
  \LetLtxMacro\SetHeadStyle\relax
  \LetLtxMacro\SetHeadJump\relax
  \LetLtxMacro\SetFootJump\relax
  \LetLtxMacro\SetLooseHead\relax
}% end \@DisableHeadFootSettings
%%

% The following settings can continue to be re-issued after \begin{document}:
% \SetEmblems  also \SetEmblem
% \SetPageNumberStyle
% \SetVersoHeadText  also \New and \Renew
% \SetRectoHeadText  also \New and \Renew


%%
\endinput
%%
%% End of file `novel-HeadFootStyles.sty'