\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{PrimeTeX/packages/Graphics}[2025-07-09]

\RequirePackage{PrimeTeX/packages/Layout}

\RequirePackage{calc}

\RequirePackage{wrapfig2}

\ProvideLength{\iconsize}[0.25in]

\ProvideLength{\@InlineGraphicHeight}[12pt]
\ProvideLength{\@InlineGraphicOffset}[-2pt]

\NewDocumentCommand{\InlineGraphic}{
	O{\@InlineGraphicHeight}
	m
	O{\@InlineGraphicOffset}
}{%
	\raisebox{#3}{\includegraphics[keepaspectratio,height=#1]{#2}}%
}

% Shorthand command for \includegraphics.
\NewDocumentCommand{\ig}{O{width=0.5in} m}{%
	\includegraphics[keepaspectratio,#1]{#2}%
}

\NewDocumentCommand{\centerfig}{O{} m >{\TrimSpaces}m m}{
	\begin{figure}[htpb]%
		\Centering%
		\includegraphics[keepaspectratio,#1]{#2}%
		\caption{#4}%
		\label{fig:#3}%
	\end{figure}%
}

\NewDocumentCommand{\bigfig}{
	% BigFloat overhang.
	O{\bigfloatoverhang}
	% Float positioning.
	O{htpb}
	% BigFloat offset ratio.
	O{\bigfloatoffsetratio}
	% Graphic file.
	m
	% Label.
	>{\TrimSpaces}m
	% Caption.
	m
}{%
	\begin{BigFloat}[figure][#1][#2][#3]%
		\captionsetup{type=figure}%
		\includegraphics[keepaspectratio,width=\linewidth]{#4}%
		\Centering\captionof{figure}{#6}%\par
		\label{fig:#5}%
	\end{BigFloat}%
}

\NewDocumentCommand{\doublebigfig}{%
	% BigFloat overhang.
	O{\bigfloatoverhang}
	% BigFloat offet.
	O{\bigfloatoffsetratio}
	% First graphic filename.
	m
	% First graphic label.
	>{\TrimSpaces}m
	% First graphic caption.
	m
	% Second graphic filename.
	m
	% Second graphic label.
	>{\TrimSpaces}m
	% Second Graphic caption.
	m
}{%
	\begin{BigFloat}[figure][#1][p][#2]%
		\captionsetup{type=figure}%
		\includegraphics[keepaspectratio,width=\textwidth]{#3}%
		\Centering\captionof{figure}{#5}%
		\label{fig:#4}
		\bigskip
		\captionsetup{type=figure}%
		\includegraphics[keepaspectratio,width=\textwidth]{#6}%
		\Centering\captionof{figure}{#8}%
		\label{fig:#7}%
	\end{BigFloat}%
}

\RequirePackage{tabularray}

\ProvideLength{\twinfigspace}[3pt]
\ProvideLength{\twinfigwidth}[
	\textwidth / 2 + \bigfloatoverhang * 3 / 2 - \twinfigspace
]

\ExplSyntaxOn

\dim_new:N \l_twinfig_overhang
\dim_new:N \l_twinfig_width
\dim_new:N \l_twinfig_space
\fp_new:N \l_twinfig_offset
\str_new:N \l_twinfig_placement
\str_new:N \l_twinfig_valign

\keys_define:nn{twinfig}{
	overhang	.dim_set:N	= \l_twinfig_overhang,
	overhang	.initial:e	= \bigfloatoverhang,
}
\keys_define:nn{twinfig}{
	placement	.str_set:N	= \l_twinfig_placement,
	placement	.initial:n	= {htpb},%\bigfloatplacement,
}
\keys_define:nn{twinfig}{
	offset		.fp_set:N	= \l_twinfig_offset,
	offset		.initial:n	= {2.0},%\bigfloatoffsetratio,
}
\keys_define:nn{twinfig}{
	valign		.str_set:N	= \l_twinfig_valign,
	valign		.initial:n	= {m},
}
\keys_define:nn{twinfig}{
	width		.dim_set:N	= \l_twinfig_width,
	width		.initial:n	= {3.14in},%\twinfigwidth,
}
\keys_define:nn{twinfig}{
	space		.dim_set:N	= \l_twinfig_space,
	space		.initial:n	= {3pt},%\twinfigspace,
}
	\keys_log:nn{twinfig}{overhang}
	\keys_log:nn{twinfig}{placement}
	\keys_log:nn{twinfig}{offset}
	\keys_log:nn{twinfig}{valign}
	\keys_log:nn{twinfig}{width}
	\keys_log:nn{twinfig}{space}

\typeout{\the\l_twinfig_overhang}
\typeout{\l_twinfig_placement}
\typeout{\fp_use:N \l_twinfig_offset}
\typeout{\l_twinfig_valign}
\typeout{\the\l_twinfig_width}
\typeout{\the\l_twinfig_space}

\ProcessKeyOptions[twinfig]

\NewDocumentCommand{\twinfig}{%
	% #1: Big Float overhang.
	%O{\bigfloatoverhang}
	% #2: Float placement.
	%O{htpb}
	% #3: BigFloat offset ratio.
	%O{\bigfloatoffsetratio}
	% #1: Key=value options.
	O{}
	% #2: First graphic file.
	m 
	% #3: First label.
	>{\TrimSpaces}m 
	% #4: First caption.
	>{\TrimSpaces}m 
	% #5: Second graphic file.
	m 
	% #6: Second label.
	>{\TrimSpaces}m 
	% #7: Second caption.
	>{\TrimSpaces}m
	% #8: Image vertical alignment.
	%O{m}
}{%
	\keys_set:nn{twinfig}{#1}%

\typeout{\the\l_twinfig_overhang}
\typeout{\l_twinfig_placement}
\typeout{\fp_use:N \l_twinfig_offset}
\typeout{\l_twinfig_valign}
\typeout{\the\l_twinfig_width}
\typeout{\the\l_twinfig_space}
	\begin{BigFloat}[figure][\l_twinfig_overhang][\l_twinfig_placement][\l_twinfig_offset]%
		\begin{tblr}{%
			colspec	= {*{2}{Q[c,\l_twinfig_valign,wd=\l_twinfig_width]}},%
			hlines	= {0pt},
			%vlines,% For debugging. Comment out when done.
			hborder{1,Z} = {%
				abovespace	= 0pt,%
				belowspace	= 0pt,%
			},%
			vborder{1,Z} = {%
				leftspace	= 0pt,%
				rightspace	= 0pt,%
			},%
			vborder{2} = {%
				leftspace	= \l_twinfig_space,%
				rightspace	= \l_twinfig_space,%
			},%
		}%
			\vfill
			\captionsetup{type=figure}%
			\includegraphics[%
				keepaspectratio,width=\l_twinfig_width%
			]{#2}%
			\captionof{figure}{#4}%
			\label{fig:#3}
			\vfill%
			&%
				\vfill
				\captionsetup{type=figure}%
				\includegraphics[%
					keepaspectratio,width=\l_twinfig_width%
				]{#5}%
				\captionof{figure}{#7}%
				\label{fig:#6}
				\vfill%
			\\%
		\end{tblr}%
	\end{BigFloat}%
}

\NewDocumentCommand{\wrapfig}{
	% alignment [l,r,i,o]
	O{O}
	% horizontal_offset
	O{\marparwidth}
	% graphic_file
	m
	% (width|height)=lengthvalue
	O{}
	% label
	>{\TrimSpaces}m
	% caption
	m%
}{%
	\begin{wrapfigure}{#1}[#2]%
		\captionsetup{type=figure}%
		\capstart%
		\includegraphics[%
			keepaspectratio,%
			#4%
		]{#3}%
		\Centering\captionof{figure}{#6}%
		\label{fig:#5}%
	\end{wrapfigure}%
}