% Author..........: C. Pierquet
% licence.........: Released under the LaTeX Project Public License v1.3c or later, see http://www.latex-project.org/lppl.txt

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{customenvs-macros}[2025/12/22 0.1.2 Small macros, for several usages]

%====HISTORY
% v 0.1.2	Improvements with vimg
% v 0.1.1	Improvements with latex3
% v 0.1.0	Initial version subpackage of customenvs

%====PACKAGES
\RequirePackage{graphicx}
\RequirePackage{simplekv}
\RequirePackage{calc}
\RequirePackage{xstring}
\RequirePackage{etoolbox}
\RequirePackage{xintexpr}
\RequirePackage{pgf}
\RequirePackage{tikz}
\RequirePackage{ifthen}

%====vincludegraphics [fr]
\newlength{\tmp@includegraphics@vfill@offset}

\defKV[vfillincludegphcs]{vOffset=\setlength\tmp@includegraphics@vfill@offset{#1},vCentrer=\def\tmpincludegpvcenter{#1}}
\setKVdefault[vfillincludegphcs]{vRemplir=false,vOffset=0pt,vCentrer=n}

\NewDocumentCommand\InsererImage{ D<>{} O{} m }{%
	\restoreKV[vfillincludegphcs]%
	\setKV[vfillincludegphcs]{#1}%
	\ifboolKV[vfillincludegphcs]{vRemplir}%
		{%
			\def\tmpimg{\includegraphics[height=\dimexpr\pagegoal-\pagetotal-\tmp@includegraphics@vfill@offset\relax,#2]{#3}}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{hg}}{\noindent\tmpimg}{}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{h}}{\noindent\hfill\tmpimg\hfill\null}{}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{hd}}{\noindent\hfill\tmpimg}{}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{g}}{\vspace*{\fill}\tmpimg\vspace*{\fill}}{}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{m}}{\vspace*{\fill}\noindent\hfill\tmpimg\hfill\null\vspace*{\fill}}{}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{d}}{\vspace*{\fill}\noindent\hfill\tmpimg\vspace*{\fill}}{}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{bg}}{\vspace*{\fill}\tmpimg}{}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{b}}{\vspace*{\fill}\noindent\hfill\tmpimg\hfill\null}{}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{bd}}{\vspace*{\fill}\noindent\hfill\tmpimg}{}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{n}}{\tmpimg}{}%
			% \IfStrEqCase{\tmpincludegpvcenter}{%
				% {gh}%
					% {%
						% \includegraphics[height=\dimexpr\pagegoal-\pagetotal-\tmp@includegraphics@vfill@offset\relax,#2]{#3}%
					% }%
				% {h}%
					% {%
						% \noindent\hfill\includegraphics[height=\dimexpr\pagegoal-\pagetotal-\tmp@includegraphics@vfill@offset\relax,#2]{#3}\hfill\null%
					% }%
				% {dh}%
					% {%
						% \hfill\includegraphics[height=\dimexpr\pagegoal-\pagetotal-\tmp@includegraphics@vfill@offset\relax,#2]{#3}%
					% }%
				% {g}%
					% {%
						% \vspace*{\fill}%
						% \includegraphics[height=\dimexpr\pagegoal-\pagetotal-\tmp@includegraphics@vfill@offset\relax,#2]{#3}%
						% \vspace*{\fill}%
					% }%
				% {m}%
					% {%
						% \vspace*{\fill}%
						% \noindent\hfill\includegraphics[height=\dimexpr\pagegoal-\pagetotal-\tmp@includegraphics@vfill@offset\relax,#2]{#3}\hfill\null%
						% \vspace*{\fill}%
					% }%
				% {gb}%
					% {%
						% \vspace*{\fill}%
						% \includegraphics[height=\dimexpr\pagegoal-\pagetotal-\tmp@includegraphics@vfill@offset\relax,#2]{#3}%
					% }%
				% {b}%
					% {%
						% \vspace*{\fill}%
						% \noindent\hfill\includegraphics[height=\dimexpr\pagegoal-\pagetotal-\tmp@includegraphics@vfill@offset\relax,#2]{#3}\hfill\null%
					% }%
				% {bd}%
					% {%
						% \vspace*{\fill}%
						% \noindent\hfill\includegraphics[height=\dimexpr\pagegoal-\pagetotal-\tmp@includegraphics@vfill@offset\relax,#2]{#3}%
					% }%
				% {n}{\includegraphics[height=\dimexpr\pagegoal-\pagetotal-\tmp@includegraphics@vfill@offset\relax,#2]{#3}}%
			% }[{\includegraphics[height=\dimexpr\pagegoal-\pagetotal-\tmp@includegraphics@vfill@offset\relax,#2]{#3}}]%
		}%
		{%
			\includegraphics[#2]{#3}%
		}%
}

\defKV[vfillincludegraphics]{voffset=\setlength\tmp@includegraphics@vfill@offset{#1},vcenter=\def\tmpincludegpvcenter{#1}}
\setKVdefault[vfillincludegraphics]{vfill=false,voffset=0pt,vcenter=n}

\NewDocumentCommand\vfillincludegraphics{ D<>{} O{} m }{%
	\restoreKV[vfillincludegraphics]%
	\setKV[vfillincludegraphics]{#1}%
	\ifboolKV[vfillincludegraphics]{vfill}%
		{%
			\def\tmpimg{\includegraphics[height=\dimexpr\pagegoal-\pagetotal-\tmp@includegraphics@vfill@offset\relax,#2]{#3}}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{ul}}{\noindent\tmpimg}{}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{u}}{\noindent\hfill\tmpimg\hfill\null}{}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{ur}}{\noindent\hfill\tmpimg}{}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{l}}{\vspace*{\fill}\tmpimg\vspace*{\fill}}{}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{m}}{\vspace*{\fill}\noindent\hfill\tmpimg\hfill\null\vspace*{\fill}}{}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{r}}{\vspace*{\fill}\noindent\hfill\tmpimg\vspace*{\fill}}{}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{dl}}{\vspace*{\fill}\tmpimg}{}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{d}}{\vspace*{\fill}\noindent\hfill\tmpimg\hfill\null}{}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{dr}}{\vspace*{\fill}\noindent\hfill\tmpimg}{}%
			\ifthenelse{\equal{\tmpincludegpvcenter}{n}}{\tmpimg}{}%
			% \IfStrEqCase{\tmpincludegpvcenter}{%
				% {m}{\vfill\includegraphics[height=\dimexpr\pagegoal-\pagetotal-\tmp@includegraphics@vfill@offset\relax,#2]{#3}\vfill\null}%
				% {t}{\includegraphics[height=\dimexpr\pagegoal-\pagetotal-\tmp@includegraphics@vfill@offset\relax,#2]{#3}\vfill\null}%
				% {b}{\vfill\includegraphics[height=\dimexpr\pagegoal-\pagetotal-\tmp@includegraphics@vfill@offset\relax,#2]{#3}}%
				% {n}{\includegraphics[height=\dimexpr\pagegoal-\pagetotal-\tmp@includegraphics@vfill@offset\relax,#2]{#3}}%
			% }[{\includegraphics[height=\dimexpr\pagegoal-\pagetotal-\tmp@includegraphics@vfill@offset\relax,#2]{#3}}]%
		}%
		{%
			\includegraphics[#2]{#3}%
		}%
}

%====lengths
\NewDocumentCommand\storewidthtolength{omm}{%
	\ifdeflength{#3}{\settowidth#3{\hbox{#2}}}{\newlength{#3}\settowidth#3{\hbox{#2}}}%
	\IfNoValueF{#1}{\addtolength#3{#1}}%
}

\NewDocumentCommand\storeheighttolength{omm}{%
	\ifdeflength{#3}{\settoheight#3{\hbox{#2}}}{\newlength{#3}\settoheight#3{\hbox{#2}}}%
	\IfNoValueF{#1}{\addtolength#3{#1}}%
}

\NewDocumentCommand\storetotalheighttolength{omm}{%
	\ifdeflength{#3}{\settototalheight#3{\hbox{#2}}}{\newlength{#3}\settototalheight#3{\hbox{#2}}}%
	\IfNoValueF{#1}{\addtolength#3{#1}}%
}

\NewDocumentCommand\storedepthtolength{omm}{%
	\ifdeflength{#3}{\settodepth#3{\hbox{#2}}}{\newlength{#3}\settodepth#3{\hbox{#2}}}%
	\IfNoValueF{#1}{\addtolength#3{#1}}%
}

\newlength\tmpwideststring
\newlength\tmpwideststringelt
\newsavebox{\my@tmp@width@box}

\NewDocumentCommand\getwideststring{ O{\tmpwideststring} m }{%
	\ifdeflength{#1}{}{\newlength#1}%
	\setlength\tmpwideststring{0pt}%
	\xintFor* ##1 in {\xintCSVtoList{#2}}\do{%
		\savebox{\my@tmp@width@box}{##1}%
		\settowidth{\tmpwideststringelt}{\usebox{\my@tmp@width@box}}%
		\pgfmathsetlength{\global#1}{max(#1,\tmpwideststringelt)}%
	}%
}

\newlength\tmpheigheststring
\newlength\tmpheigheststringelt
\newsavebox{\my@tmp@height@box}

\NewDocumentCommand\getheigheststring{ s O{\tmpheigheststring} m }{%
	\ifdeflength{#2}{}{\newlength#2}%
	\setlength#2{0pt}%
	\IfBooleanTF{#1}%
		{%
			\xintFor* ##1 in {\xintCSVtoList{#3}}\do{%
				\savebox{\my@tmp@height@box}{##1}%
				\settoheight{\tmpheigheststringelt}{\usebox{\my@tmp@height@box}}%
				\pgfmathsetlength{\global#2}{max(#2,\tmpheigheststringelt)}%
			}%
		}%
		{%
			\xintFor* ##1 in {\xintCSVtoList{#3}}\do{%
				\savebox{\my@tmp@height@box}{##1}%
				\settototalheight{\tmpheigheststringelt}{\usebox{\my@tmp@height@box}}%
				\pgfmathsetlength{\global#2}{max(#2,\tmpheigheststringelt)}%
			}%
		}%
}

\newlength\my@var@height@of
\newlength\my@var@width@of
\newlength\my@fixed@height@of
\newlength\my@fixed@width@of
\newsavebox\tmp@box@to@fit@h
\newsavebox\tmp@box@to@fit@v

\NewDocumentCommand\fittexttobox{smmm}{%
	%*=box with tikz
	%#1=text
	%#2=width
	%#3=height
	\setlength\my@fixed@width@of{#3}%
	\setlength\my@fixed@height@of{#4}%
	\savebox{\tmp@box@to@fit@v}{\resizebox*{!}{\my@fixed@height@of}{#2}}%v-stretch
	\savebox{\tmp@box@to@fit@h}{\resizebox*{\my@fixed@width@of}{!}{#2}}%h-stretch
	\settowidth\my@var@width@of{\usebox{\tmp@box@to@fit@v}}%
	\IfBooleanTF{#1}%
		{%
			\begin{tikzpicture}
				\draw (0,0) rectangle (\my@fixed@width@of,\my@fixed@height@of) ;
				\ifthenelse{\my@var@width@of < \my@fixed@width@of}%
					{%width ok
						\node[inner sep=0pt] at (0.5\my@fixed@width@of,0.5\my@fixed@height@of) {\usebox{\tmp@box@to@fit@v}} ;
					}%
					{%width ko => h-stretch
						\node[inner sep=0pt] at (0.5\my@fixed@width@of,0.5\my@fixed@height@of) {\usebox{\tmp@box@to@fit@h}} ;
					}%
			\end{tikzpicture}%
		}%
		{%
			\ifthenelse{\my@var@width@of < \my@fixed@width@of}%
				{%width ok
					{\usebox{\tmp@box@to@fit@v}}%
				}%
				{%width ko => h-stretch
					{\usebox{\tmp@box@to@fit@h}}%
				}%
		}%
}

\NewDocumentCommand\halignnb{ O{0,1,2,3,4,5,6,7,8,9} D<>{c} m }{%
	%#1 = ref strings
	%#2 = string
	\getwideststring{#1}%
	\makebox[\tmpwideststring][#2]{#3}%
}

\NewDocumentCommand\halignmakebox{ O{c} m m }{%
	%#1 = align makebox
	%#2 = string
	%#3 = ref strings
	\getwideststring{#3}%
	\makebox[\tmpwideststring][#1]{#2}%
}

%====testifsubandsplit
\ExplSyntaxOn

\NewDocumentCommand\testifandsplit{ O{norm} m m m m }{%
	% #2 = substr (le séparateur, par exemple "/")
	% #3 = str to test (la chaîne à tester, par exemple "10pt/5pt")
	% #4 = left macro (ou #2 si non trouvé)
	% #5 = right macro (ou #2 si non trouvé)
	\str_case:nnF { #1 }
	{
		{ dim }{ \testdimandsplit{#2}{#3}{#4}{#5} }
		{ col }{ \testcolandsplit{#2}{#3}{#4}{#5} }
	}
	{ \testandsplit{#2}{#3}{#4}{#5} }
}

\NewDocumentCommand\testdimandsplit{ m m m m }
{
	\tl_set:Ne \l_tmpa_tl { #2 }
	\tl_if_in:NnTF \l_tmpa_tl { #1 }
	{
		\seq_set_split:Nne \l_tmpa_seq { #1 } { #2 }
		\tl_gset:Ne \l_tmpb_tl { \seq_item:Nn \l_tmpa_seq {1} }
		\tl_gset:Ne \l_tmpc_tl { \seq_item:Nn \l_tmpa_seq {2} }
		\setlength{#3}{\l_tmpb_tl}
		\setlength{#4}{\l_tmpc_tl}
	}
	{
		\setlength{#3}{#2}
		\setlength{#4}{#2}
	}
}

\NewDocumentCommand\testandsplit{ m m m m }
{
	%#1 = substr
	%#2 = str to test
	%#3 = left macro (or #2 if not found)
	%#4 = right macro (or #2 if not found)
	\tl_set:Ne \l_tmpa_tl { #2 }
	\tl_if_in:NnTF \l_tmpa_tl { #1 }
	{
		\seq_set_split:Nne \l_tmpa_seq { #1 } { #2 }
		\tl_set:Ne #3 { \seq_item:Nn \l_tmpa_seq {1} }
		\tl_set:Ne #4 { \seq_item:Nn \l_tmpa_seq {2} }
	}
	{
		\tl_gset:Ne #3 {#2}
		\tl_gset:Ne #4 {#2}
	}
}

\NewDocumentCommand\testcolandsplit{ m m m m }
{
	%#1 = substr
	%#2 = str to test
	%#3 = left macro (or #2 if not found)
	%#4 = right macro (or #2 if not found)
	\tl_set:Ne \l_tmpa_tl { #2 }
	\tl_if_in:NnTF \l_tmpa_tl { #1 }
	{
		\seq_set_split:Nne \l_tmpa_seq { #1 } { #2 }
		\tl_set:Ne \l_tmpb_tl { \seq_item:Nn \l_tmpa_seq {1} }
		\tl_set:Ne \l_tmpc_tl { \seq_item:Nn \l_tmpa_seq {2} }
		\colorlet{#3}{\l_tmpb_tl}
		\colorlet{#4}{\l_tmpc_tl}
	}
	{
		\colorlet{#3}{#2}
		\colorlet{#4}{#2}
	}
}

\ExplSyntaxOff

\endinput