\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 - \twinfigspace
]

\NewDocumentCommand{\twinfig}{%
	% Big Float overhang.
	O{\bigfloatoverhang}
	% Float placement.
	O{htpb}
	% BigFloat offset ratio.
	O{\bigfloatoffsetratio}
	% First graphic file.
	m 
	% First label.
	>{\TrimSpaces}m 
	% First caption.
	>{\TrimSpaces}m 
	% Second graphic file.
	m 
	% Second label.
	>{\TrimSpaces}m 
	% Second caption.
	>{\TrimSpaces}m
}{%
	\begin{BigFloat}[figure][#1][#2][#3]%
		\begin{tblr}{%
			colspec	= {*{2}{Q[c,m,wd=\twinfigwidth]}},%
			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	= \twinfigspace,%
				rightspace	= \twinfigspace,%
			},%
		}%
			\vfill
			\captionsetup{type=figure}%
			\includegraphics[%
				keepaspectratio,width=\twinfigwidth%
			]{#4}%
			\captionof{figure}{#6}%
			\label{fig:#5}
			\vfill%
			&%
				\vfill
				\captionsetup{type=figure}%
				\includegraphics[%
					keepaspectratio,width=\twinfigwidth%
				]{#7}%
				\captionof{figure}{#9}%
				\label{fig:#8}
				\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}%
}