\NeedsTeXFormat{LaTeX2e}
\ProvidesExplPackage{PrimeTeX/packages/Layout}{2025-09-03}{1.0}{Layout.}

% Suppress warning about \showhyphens being changed.
\let\CheckCommand\providecommand

\NewDocumentCommand{\ProvideLength}{m O{0pt}}{%
	\ifdeflength{#1}{% It is already defined!
	}{% Not defined, so define it!
		\newlength{#1}%
	}%
	\setlength{#1}{#2}%
}

% Redefine the commands geometry uses to draw the page frame to customize the
% color.
\renewcommand{\Gm@vrule}{\color{layoutColor}\vrule width 0.2pt height\textheight depth\z@}
\renewcommand{\Gm@hrule}{\color{layoutColor}\hrule height 0.2pt depth\z@ width\textwidth}
\renewcommand{\Gm@hruled}{\color{layoutColor}\hrule height\z@ depth0.2pt width\textwidth}

% Suppress "Underfull \hbox" infos.
\hbadness=10000

\bool_if:NTF \g_draft_bool{
	\geometry{showframe}
	\hfuzz = 0.5pt
	\vfuzz = 0.0pt
}{
	% Suppress small overfull infos.
	\hfuzz = 6.0pt
	\vfuzz = 8.0pt
}

\geometry{
	letterpaper,
	width			= 4.75in,	% 342pt,
	height			= 8.0in,	% 576pt,
	left			= 1.5in,	% 108pt,
	top 			= 1.5in,	% 108pt,
	head			= 12pt,		% 1/6in
	headsep			= 24pt,		% 1/3in
	marginparwidth	= 1.75in,	% 126pt,
	marginparsep	= 12pt,		% 1/6in
	footskip		= 0.5in,	% 36pt,
}

\bool_if:NTF \g_electronic_bool {
%	\geometry{}
}{
	\geometry{twoside}
}

% Lua-Widow-Control Setup.
\lwcsetup{
	%debug,
	balanced,
	emergencystretch = 3em,
	max-cost		= 25000,
	widowpenalty	=  1000,
	orphanpenalty	=  1000,
	brokenpenalty	= 10000,
	nobreak = keep,
}

\ProvideLength{\marparwidth}[\marginparsep + \marginparwidth]
\ProvideLength{\contentwidth}[\textwidth + \marparwidth]

\bool_if:NTF \g_draft_bool{
	\lwcsetup{showcolours=true,}
}{}

% Paragraph formatting.
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 3pt minus 3pt}

% Float Page Configuration
\renewcommand{\topfraction}{0.9}	% max fraction of floats at top
\renewcommand{\bottomfraction}{0.8}	% max fraction of floats at bottom
%   Parameters for TEXT pages (not float pages):
\setcounter{topnumber}{2}
\setcounter{bottomnumber}{2}
\setcounter{totalnumber}{4}     % 2 may work better
\setcounter{dbltopnumber}{2}    % for 2-column pages
\renewcommand{\dbltopfraction}{0.9}	% fit big float above 2-col. text
\renewcommand{\textfraction}{0.07}	% allow minimal text w. figs
%   Parameters for FLOAT pages (not text pages):
\renewcommand{\floatpagefraction}{0.8}	% require fuller float pages
% N.B.: floatpagefraction MUST be less than topfraction !!
\renewcommand{\dblfloatpagefraction}{0.8}	% require fuller float pages

\NewDocumentEnvironment{SmartAdjustWidth}{
	O{\IsElectronic}
	m
	m
	+b
}{
	\bool_if:nTF {#1} {
		\begin{adjustwidth}{#2}{#3}
			#4
		\end{adjustwidth}
	}{
		\begin{adjustwidth*}{#2}{#3}
			#4
		\end{adjustwidth*}
	}
}{}


\ExplSyntaxOff
\AtBeginDocument{
	\If{\IsElectronic}{
		\newgeometry{
			inner	= 0.75in,
			top		= 66pt,	% 11/12in
			marginratio = 2:3,
		}
		\savegeometry{smallMargin}
		\newgeometry{
			inner	= 24pt,	% 1/3in
			top		= 0.25in,
			marginratio = 2:3,
		}
		\savegeometry{tinyMargin}
	}[
		\newgeometry{
			twoside	= true,
			inner	= 0.75in,
			top		= 66pt,	% 11/12in
			marginratio = 2:3,
		}
		\savegeometry{smallMargin}
		\newgeometry{
			twoside	= true,
			inner	= 24pt,	% 1/3in
			top		= 0.25in,
			marginratio = 2:3,
		}
		\savegeometry{tinyMargin}
	]
	\restoregeometry

	\NewDocumentEnvironment{SmallMargin}{O{0.0in} O{-0.25in}}{
		\clearpage
		\loadgeometry{smallMargin}
		\setlength{\HeaderInsideOffset}{#1}
		\setlength{\HeaderOutsideOffset}{#2}
		\pagestyle{fancy}
	}{
		\clearpage
		\pagestyle{fancy}
		\restoregeometry
	}
}