\NeedsTeXFormat{LaTeX2e}
\ProvidesExplPackage{PrimeTeX/packages/Graphics}{2025-09-03}{1.0}{
	Custom non-floating Graphics commands.
}

\dim_const:Nn \c_Graphics_icon_dim {0.25in}

\dim_const:Nn \c_InlineGraphic_offset_dim {-2pt}

\NewDocumentCommand{\IconSize}{}{\c_Graphics_icon_dim}

\keys_define:nn{InlineGraphic}{
	height	.dim_set:N	= \l_InlineGraphic_height_dim,
	height	.initial:e	= {\baselineskip},

	offset	.dim_set:N	= \l_InlineGraphic_offset_dim,
	offset	.initial:e	= {\c_InlineGraphic_offset_dim},
}

\NewDocumentCommand{\InlineGraphic}{
	% Key=value options.
	O{}
	% Graphic file.
	m
}{
	\keys_set:nn{InlineGraphic}{#1}
	\raisebox{\l_InlineGraphic_offset_dim}{
		\expanded{\noexpand\includegraphics[
			keepaspectratio,
			height=\l_InlineGraphic_height_dim,
		] {#2}}
	}
}

\dim_const:Nn \c_ig_width_dim {0.5in}

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

\dim_const:Nn \c_WrapFig_hoffset_dim {0in}
\dim_const:Nn \c_WrapFig_im_hoffset_dim {1in}
\dim_const:Nn \c_WrapFig_om_hoffset_dim {\marparwidth}
\str_const:Nn \c_WrapFig_placement_str {o}

\dim_new:N \l_WrapFig_width_dim
\str_new:N \l_WrapFig_passed_keys_str
\tl_new:N \l_WrapFig_format_tl

\newsavebox{\__WrapFig_graphic:}

\keys_define:nn {WrapFig} {
	hoffset.dim_set:N	= \l_WrapFig_hoffset_dim,
	hoffset.initial:e	= {\c_WrapFig_hoffset_dim},

	margin.choices:nn	= {none,inside,outside,} {
		\str_set:Ne \l_WrapFig_margin_str {\l_keys_choice_tl}
	},
	margin.initial:n	= {none},
	margin.default:n	= {none},

	placement.choices:nn= {c,i,l,o,r} {
		\str_set:Ne \l_WrapFig_placement_str {\l_keys_choice_tl}
	},
	placement.initial:e	= {\c_WrapFig_placement_str},

	unknown.code:n		= {
		\str_put_right:Ne \l_WrapFig_passed_keys_str {\l_keys_key_str=\l_keys_value_tl,}
	}
}

\NewDocumentCommand{\WrapFig}{
	% #1: Key-value options
	O{}
	% #2: graphic_file
	m
	% #3: Options passed to \includegraphics
	O{}
	% #4: label
	>{\TrimSpaces}m
	% #5: caption
	m
	% #6: Options passed to wrapstuff
	O{}
}{ \group_begin:
	\keys_set:nn {WrapFig} {#1}
	\__WrapFig_process_options:
	% \message{
	% 	WrapFig:~#4,~
	% 	placement:~\l_WrapFig_placement_str,~
	% 	hoffset:~\dim_use:N \l_WrapFig_hoffset_dim,
	% 	passed_keys:~\l_WrapFig_passed_keys_str
	% }
	\expanded{\noexpand\begin{wrapstuff}[
		\l_WrapFig_placement_str,
		hoffset	= {\dim_use:N \l_WrapFig_hoffset_dim},
		#6
	]}
		\sbox{\__WrapFig_graphic:}{
			\includegraphics[
				keepaspectratio,
				#3
			]{#2}
		}
		\settowidth{\l_WrapFig_width_dim}{\usebox{\__WrapFig_graphic:}}
		\begin{minipage}{\l_WrapFig_width_dim}
			\begin{MyCaption}[format=\l_WrapFig_format_tl]{fig:#4}{#5}
				\usebox{\__WrapFig_graphic:}
			\end{MyCaption}
		\end{minipage}
	\end{wrapstuff}
\group_end: }

\cs_new:Nn \__WrapFig_process_options: {
	\str_case:en \l_WrapFig_margin_str {
		{inside} {
			\dim_set_eq:NN \l_WrapFig_hoffset_dim \c_WrapFig_im_hoffset_dim
			\str_set:Nn \l_WrapFig_placement_str {i}
		}
		{outside} {
			\dim_set_eq:NN \l_WrapFig_hoffset_dim \c_WrapFig_om_hoffset_dim
			\str_set:Nn \l_WrapFig_placement_str {o}
		}
	}
	\bool_if:NTF \g_electronic_bool {
		\str_replace_all:Nnn \l_WrapFig_placement_str {i} {l}
		\str_replace_all:Nnn \l_WrapFig_placement_str {o} {r}
	} {
		\checkoddpage
		\ifoddpage
			\str_replace_all:Nnn \l_WrapFig_placement_str {i} {l}
			\str_replace_all:Nnn \l_WrapFig_placement_str {o} {r}
		\else
			\str_replace_all:Nnn \l_WrapFig_placement_str {i} {r}
			\str_replace_all:Nnn \l_WrapFig_placement_str {o} {l}
		\fi
	}
	\str_case:en \l_WrapFig_placement_str {
		{c} {\tl_set:Nn \l_WrapFig_format_tl {\Centering}}
		{l} {\tl_set:Nn \l_WrapFig_format_tl {\RaggedLeft}}
		{r} {\tl_set:Nn \l_WrapFig_format_tl {\RaggedRight}}
	}
}