summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/autofancyhdr/autofancyhdr.sty
blob: 83613a59b42799dea24f52e7acaf61c0e72c02ae (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
\ProvidesPackage{autofancyhdr}%
           [2019/01/18 v0.1
                  Extensive control of page headers and footers with automatic calculate headheight]%
\RequirePackage{fancyhdr}
\RequirePackage{biditools}
\def\headfootlength{}
% Redefine \f@nch@vbox for auto height
\def\f@nch@vbox#1#2{%
	\setbox0\vbox{#2}%
	\ifdim\ht0=#1\else%
	\f@nch@warning{%
		\string#1 (\the#1) is not equal with \the\ht0.^^J
		Please compile again to correct this problem.%
	}%
	\fi%
	\ifx#1\headheight%
	\edef\eheadfootlength{%
		\string\expandafter\string\def\string\csname\space
		headleng\thepage \string\endcsname{\strip@pt\ht0}%
	}%
	\expandafter\g@addto@macro\expandafter\headfootlength\expandafter{%
		\eheadfootlength^^J%
	}%
	\fi%
	\box0%
}
\bidi@AfterOutputPageShipOut{%
	\newdimen\newheadheight%
	\ifcsname headleng\the\numexpr\value{page}+1\relax\endcsname%
	\setlength{\newheadheight}{%
		\csname headleng\the\numexpr\value{page}+1\relax\endcsname pt%
	}%
	\else%
	\ifcsname headleng1\endcsname%
	\setlength{\newheadheight}{%
		\csname headleng1\endcsname pt%
	}%
	\else%
		\setlength{\newheadheight}{0pt}%
	\fi%
	\fi%
	\global\setlength{\textheight}{
		\dimexpr\textheight+\headheight-\newheadheight\relax
	}%
	\global\setlength{\headheight}{\newheadheight}%
}%
\IfFileExists{\jobname.headfootlength}
{
	\input{\jobname.headfootlength}
}
{}
\ifcsname headleng1\endcsname
\global\setlength\headheight{\csname headleng1\endcsname pt}
\else
\global\setlength\headheight{0pt}
\fi
\bidi@AfterLastShipout{%
	\if@filesw%
	\begingroup
	% same write register as environment `filecontents` uses
	\chardef\reserved@c=15 %
	\immediate\openout\reserved@c=\jobname.headfootlength\relax
	\immediate\write\reserved@c{%
		\headfootlength%
	}%
	\immediate\closeout\reserved@c
	\endgroup
	\fi%
}