% This file is part of the CTAN package named ifis-macros.
% 
%   gluecomp.tex: a) provide macro \makeTsgm to store a glue specification as
%                    a ``TeX split glue macro'' (Tsgm)
%                 b) provide macros \ifnatwd, \ifstretch, and \ifshink to check
%                    the components of two glue specifications stored as Tsgms.
%
%
%   Usage: a) \makeTsgm <glue specification>!<control sequence>
%             and <control sequence> is defined as the Tsgm.
%          b) \ifnatwd | \ifstretch | \ifshrink
%               <argument1><relation><argument2>\Boolend
%               <true branch>\else <false branch>\fi
%              where <relation> is < | = | >
%
%   Copyright (C) 2025  Udo Wermuth (author)
%
%   This program is free software: you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as published by
%   the Free Software Foundation, either version 3 of the License, or
%   (at your option) any later version.
%
%   This program is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%   GNU General Public License for more details.
%
%   You should have received a copy of the GNU General Public License
%   along with this program.  If not, see <http://www.gnu.org/licenses/>.
%
\input ifisglue.tex
\catcode`\@=11
%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%
%%% PART I: TeX split glue macro (Tsgm)
%%%
% interpret \II@sto & \II@sho from ifisglue.tex
\def\II@getorder #1{% #1: \II@sto or \II@sho
 \ifx#1\empty 0 \else\ifx#1\II@l 1
 \else\ifx#1\II@ll 2 \else 3 \fi\fi\fi}
%
% TSGM MACRO 1:
% convert input to a Tsgm (TeX split glue macro)
%
\def\makeTsgm #1!#2{% #1: glue; #2: name of Tsgm
 \ifcat\relax\noexpand#2%can #2 be a macro name?
  \expandafter\ifisglue #1\Boolend %must be true
   {\ifIImuglue\muskip255\else\skip255\fi=#1%
    \xdef\II@glue{\ifIImuglue\the\muskip255
      \else\the\skip255\fi}}% TeX canonical form
   \expandafter\ifisglue\II@glue\Boolend
    \edef#2##1{\noexpand\ifcase##1 % create Tsgm
     \ifIImuglue1 \else0 \fi\noexpand\or %0:type
     \II@nw\noexpand\or       % 1: natural width
     \ifx\II@st\space 0pt \else\II@st\fi % 2 and
     \noexpand\or \II@getorder\II@sto %3:stretch
     \noexpand\or
     \ifx\II@sh\space 0pt \else\II@sh\fi % 4 and
     \noexpand\or \II@getorder\II@sho %5: shrink
     \noexpand\or \II@glue\noexpand\or %6: value
     314.271\noexpand\else cm\noexpand\fi}\fi%id
  \else\errhelp{A glue specification should have
    been here; I replaced your input by `0pt' to
    build the Tsgm.}\II@setzero #2%
   \errmessage{Missing glue, treated as zero}\fi
 \else \errhelp{The argument after `!' must be a
   control sequence that I can define/redefine.
   Proceed; I'll ignore the \makeTsgm I just
   read.}%
  \errmessage{Invalid name for new macro}\fi}
%
\def\II@setzero #1{% #1: Tsgm name for zero skip
 \def#1##1{\ifcase##1 0 \or 0pt % type and natwd
  \or 0pt \or 0 \or 0pt \or 0 % stretch & shrink
  \or 0.0pt \or 314.271\else cm\fi}}%glue and id
% access components of a Tsgm
\def\II@gluetype #1{#10 }%   the argument #1 is
\def\II@natwd #1{#11 }%      in all these macros
\def\II@stretchdim #1{#12 }% a valid Tsgm, i.e.,
\def\II@stretchord #1{#13 }% it's a control word
\def\II@shrinkdim #1{#14 }%  filled by the macro
\def\II@shrinkord #1{#15 }%  \makeTsgm
%
% TSGM MACRO 2:
% check that a macro is a Tsgm
%
\def\ifIIisTsgm #1\Boolend{%compare to \ifisglue
 \wlog{=+= start ignore}\batchmode\begingroup
  \setbox0=\hbox{\dimen255=0#17cm
   \ifdim\dimen255=314.271#1{-1}\else 0\fi}%
  \xdef\II@wd{\the\wd0 }\endgroup\IIcurrentmode
 \wlog{=+= stop ignore}\ifdim\II@wd=0pt }
%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%
%%% PART II: componentwise comparisions
%%%
%
% USER MACROS 1--3:
% compare two corresponding components of Tsgms
%
% macro arguments #1,#3: Tsgm; #2: relation
\def\ifnatwd #1#2#3\Boolend{\II@sofarfalse %init
 \ifIIisTsgm#1\Boolend \ifIIisTsgm#3\Boolend
  \ifnum\II@gluetype#1=\II@gluetype#3%test types
  \else \II@badtypes \fi  % used muglue and glue
  \II@chkrel#2{\string\ifnatwd}%  check relation
  \ifdim\II@natwd#1\II@rel\II@natwd#3%
   \II@sofartrue \fi
 \else\II@noTsgm2\fi \else\II@noTsgm1 % bad Tsgm
 \fi \ifII@sofar}
\def\ifstretch #1#2#3\Boolend{\II@sofarfalse
 \ifIIisTsgm#1\Boolend \ifIIisTsgm#3\Boolend
  \ifnum\II@gluetype#1=\II@gluetype#3%test types
  \else \II@badtypes \fi  % used muglue and glue
  \II@chkrel#2{\string\ifstretch}% test relation
  \ifnum\II@stretchord#1=\II@stretchord#3\relax
   \ifdim\II@stretchdim#1\II@rel % compare dimen
    \II@stretchdim#3\relax \II@sofartrue \fi
  \else\ifnum\II@stretchord#1\II@rel
    \II@stretchord#3\relax \II@sofartrue \fi\fi
 \else\II@noTsgm2\fi \else\II@noTsgm1 % bad Tsgm
 \fi \ifII@sofar}
\def\ifshrink #1#2#3\Boolend{\II@sofarfalse
 \ifIIisTsgm#1\Boolend \ifIIisTsgm#3\Boolend
  \ifnum\II@gluetype#1=\II@gluetype#3%test types
  \else \II@badtypes \fi  % used muglue and glue
  \II@chkrel#2{\string\ifshrink}%  test relation
  \ifnum\II@shrinkord#1=\II@shrinkord#3\relax
   \ifdim\II@shrinkdim#1\II@rel  % compare dimen
    \II@shrinkdim#3\relax \II@sofartrue \fi
  \else\ifnum\II@shrinkord#1\II@rel
    \II@shrinkord#3\relax \II@sofartrue \fi\fi
 \else\II@noTsgm2\fi\else \II@noTsgm1 % bad Tsgm
 \fi \ifII@sofar}
%
% Error messages
\def\II@noTsgm #1{% #1: 1 or 2 to select 1st/3rd
 \errhelp{I expected a macro with a special
  structure. But the received argument does not
  obey this structure. I cannot perform the test
  and return false.}\errmessage{Not a TeX split
  glue macro (\ifodd#1 1st \else 3rd \fi arg.)}}
\def\II@chkrel #1#2{% #1: character; #2: caller
 \def\II@rel{#1}%              save the relation
 \if#1<\else\if#1=\else\if#1>\else
  \errhelp{I was expecting to see `<', `=', or
   `>'. Didn't.}\def\II@rel{=}% replace relation
  \errmessage{Missing = inserted for #2}%
 \fi\fi\fi}
\def\II@badtypes{\errhelp{I'm going to assume
  that 1mu=1pt when they're mixed.}%
 \errmessage{Incompatible glue units}}
\catcode`\@=12
