%%
%% This is file `swfigure.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% swfigure.dtx  (with options: `package')
%% 
%%   Copyright (C)  2020-2022 Claudio Beccari  all rights reserved.
%%   License information appended
%% 
\NeedsTeXFormat{LaTeX2e}[2020/01/01]
\ProvidesPackage{swfigure}%
        [2022-04-24 v.0.9.20 Managing large and spread wide figures]
\RequirePackage{etoolbox}
\RequirePackage{xfp}

\ExplSyntaxOn
\ProvideExpandableDocumentCommand\CompStrings{m m}{%
  \str_if_eq_p:nn{#1}{#2}}

  \ProvideExpandableDocumentCommand\fptest{m m m}{%
     \fp_compare:nTF{#1}{#2}{#3}}

  \ProvideExpandableDocumentCommand\SetList{m m}{%
  \seq_clear_new:N #1
  \seq_set_from_clist:Nn \DisplayModeList {#2}}

  \ProvideExpandableDocumentCommand\TestList{m m m m}{%
  \seq_if_in:NnTF #1 {#2}{#3}{#4}}

\ExplSyntaxOff

\RequirePackage{graphicx}
\RequirePackage{afterpage}
\IfFileExists{wrapfig2.sty}{%
  \RequirePackage{wrapfig2}}{\RequirePackage{wrapfig}}

\newdimen\internalmargin
    \internalmargin=\dimexpr\oddsidemargin+1in+1bp\relax
\newdimen\externalmargin
    \externalmargin=\dimexpr\evensidemargin+1in
\newdimen\spreadwidth
    \spreadwidth=\dimexpr 2\textwidth+2\internalmargin\relax
\newdimen\DFwidth
\newdimen\DFheight
\newdimen\DFhalfwidth
\newdimen\DFheight
\newdimen\DFhalfheight
\newdimen\FigSpace
\newdimen\TScaptionwidth
\def\SWcaptionShift{1em}%
\def\FScaptionShift{2em}%
\newsavebox\DFtotalimage
\newsavebox\DFimageI
\newsavebox\DFimageII
\newsavebox\RFbox
\newdimen\VS@textwidth
\newcount\VS@lines
\newif\if@SWtmode

\NewDocumentCommand\DFcaption{O{#2} m o}{\refstepcounter{figure}%
  \vtop to 0pt{\hsize=\textheight\parindent=0pt\leavevmode
  \figurename\ \thefigure\quad #2\vss}%
  \addcontentsline{lof}{figure}{\protect\numberline{\thefigure}#1}
  \IfValueT{#3}{\label{#3}}\relax%
}
\NewDocumentCommand\DFcaptionP{O{#2} m o D!!{\color{black}}}%
  {\refstepcounter{figure}%
  \vbox to 0pt{\vss\hsize=\textheight\parindent=0pt\leavevmode
  #4\relax \figurename\ \thefigure\quad #2}%
  \addcontentsline{lof}{figure}{\protect\numberline{\thefigure}#1}
  \IfValueT{#3}{\label{#3}}\relax%
}


\newcommand\set@tmode@newpage{%
\ifvmode
  \@SWtmodefalse
\else
  \unskip\@SWtmodetrue\linebreak
  \vadjust{\vspace{0pt plus1fill}\par}%
\fi}

\newcommand\reset@tmode{\if@SWtmode\expandafter\noindent\ignorespaces\fi}

\newcommand\cleartoeven[1]{%
    \set@tmode@newpage\clearpage
    \ifodd\c@page\afterpage{#1}\else#1\fi%
    }

\newcommand\cleartopage{\set@tmode@newpage\clearpage}

\NewDocumentEnvironment{DFimage}%
  { O{SW} m O{#4} m o D(){0.8} D<>{0} D||{0.25} D!!{} }%
{%
  \SetList{\DisplayModeList}{SW,HS,VS,FS,NF,RF,TH,TW}%
  \TestList{\DisplayModeList}{#1}%
  {\csuse{#1figure}{#2}[#3]{#4}[#5](#6)<#7>|#8|!#9!%
    \fptest{\CompStrings{\@currenv}{DFimage}}{}{\reset@tmode}%
  }%
  {%
    \DFwarning[#1]{#2}[#3]{#4}
  }%
}%
{%
  \aftergroup\reset@tmode
}%

\NewDocumentCommand\DFwarning{ o m o m }{%
\PackageWarning{swfigure}%
    {********************************************\MessageBreak
      Option #1\space is not valid. Nothing done \MessageBreak
                                                 \MessageBreak
      Image #2 was not processed                 \MessageBreak
     ********************************************\MessageBreak
    }%
}

\NewDocumentCommand\SWfigure{m o m o d() d<> d|| d!!}{%
\setbox\DFtotalimage=\hbox{\includegraphics{#1}}%
\DFwidth=\wd\DFtotalimage \DFhalfwidth=0.5\DFwidth
\DFheight=\ht\DFtotalimage
\FigSpace=\dimexpr\textwidth+\internalmargin\relax
\setbox\DFimageI\hbox{\bgroup
\edef\x{\egroup\noexpand\includegraphics*[%
trim = 0 0 \the\DFhalfwidth\space 0]}\x{#1}}%
\setbox\DFimageII\hbox{\bgroup
\edef\x{\egroup\noexpand\includegraphics*[%
trim = \the\DFhalfwidth\space 0 0 0]}\x{#1}}%
\fptest{\DFheight/\DFhalfwidth > \textheight/\FigSpace}%
  {\edef\DFscalefactor{\fpeval{\textheight/\DFheight}}}%
  {\edef\DFscalefactor{\fpeval{\FigSpace/\DFhalfwidth}}}%
\setbox\DFimageI=\hbox{\scalebox{\DFscalefactor}{\usebox{\DFimageI}}}%
\setbox\DFimageII=\hbox{\scalebox{\DFscalefactor}{\usebox{\DFimageII}}}%
\cleartoeven{%
  \begin{figure}[p]%
  \vbox to\textheight{\vss\hsize=\textwidth%
  \makebox[\hsize][l]{\makebox[\FigSpace][r]{\box\DFimageI}}\vss}%
  \end{figure}\newpage
  \begin{figure}[p]%
  \vbox to\textheight{\vss\hsize=\textwidth
  \makebox[\hsize][r]{\makebox[\FigSpace][l]{\box\DFimageII}%
  \makebox(0,0)[lb]{\hspace*{\SWcaptionShift}\raisebox{0.5\textheight}{%
  \rotatebox[origin=tc]{90}{\DFcaption[#2]{#3}[#4]%
  %
  }}}}\vss}%
  \end{figure}%
}\clearpage\reset@tmode}

\NewDocumentCommand\NFfigure{m o m o d() d<> d|| d!!}{%
  \begin{figure}[p]
    \includegraphics[width=\linewidth, height=#5\textheight,
                     keepaspectratio]{#1}%
    \caption[#2]{#3}%
    \IfValueT{#4}{\label{#4}}\relax
  \end{figure}
}

\NewDocumentCommand\RFfigure{m o m o d() d<> d|| d!!}{%
      \dimen8=\textwidth\dimen10=\textheight
      \figure[p]\setbox\RFbox=\hbox{%
      \rotatebox[origin=cc]{90}{\parbox[b][\dimen8][c]{\dimen10}%
        {\centering\includegraphics[width=\dimen10, height=\dimen8,
                        keepaspectratio]{#1}%
          \caption[#2]{#3}%
          \IfValueT{#4}{\label{#4}}\relax%
        }}}%
        \edef\RFx{\fpeval{\ht\RFbox/\textheight}}%
        \edef\RFy{\fpeval{\wd\RFbox/\textwidth}}%
        \fptest{\RFx > \RFy}%
          {\scalebox{\RFx}{\box\RFbox}}%
          {\scalebox{\RFy}{\box\RFbox}}%
        \endfigure}

\NewDocumentCommand\VSfigure{m o m o d() d<> d|| d!!}{%
   \setbox\DFtotalimage=\hbox{\includegraphics{#1}}
   \DFwidth=\wd\DFtotalimage \DFheight=\ht\DFtotalimage
   \edef\VS@aspectratio{\fpeval{\DFheight/\DFwidth}}
   \fptest{ \VS@aspectratio < 2 }%
     {\begin{center}\ttfamily\relax
      ===========================================\\
      Image #1 is not tall enough.               \\
      Consider using a display mode different    \\
      from VS; may be NF or RF are better suited.\\
      ===========================================\\
      Nothing done!                              \\
      ===========================================
      \end{center}
     }%
     {\edef\VS@factor{\fpeval{#5\textheight/\DFheight}}%
      \fptest{\VS@factor>1 || \VS@factor\DFwidth < #7\textwidth}%
       {%
         \begin{center}\ttfamily\relax
         =================================================\\
         The scaled image #1 is too slim.                 \\
         Maybe directly using the wrapfig package might   \\
         solve this problem.                              \\
         =================================================\\
         Nothing done!\\
         =================================================
         \end{center}
       }%
       {\setbox\DFtotalimage=
        \hbox{\scalebox{\VS@factor}{\box\DFtotalimage}}%
        \edef\VS@width{\fpeval{\VS@factor*\DFwidth}\p@}%
        \setbox\DFtotalimage=\vbox{\hsize=\VS@width%
        \def\@captype{figure}\box\DFtotalimage
        \caption[#2]{#3}%
        \IfValueT{#4}{\label{#4}}\relax}
        \VS@lines=
        \fpeval{round(\ht\DFtotalimage/\baselineskip,0)+#6}%
        \begin{wrapfigure}[\VS@lines]{o}[0pt]{\VS@width}%
        \box\DFtotalimage
        \end{wrapfigure}%
      }%
    }%
  }%

\NewDocumentCommand\HSfigure{m o m o d() d<> d|| d!!}{%
  \setbox\DFtotalimage=\hbox{\includegraphics{#1}}%
  \DFwidth=\wd\DFtotalimage \DFhalfwidth=0.5\DFwidth
  \FigSpace=0.5\spreadwidth%
  \setbox\DFimageI\hbox{\bgroup
  \edef\x{\egroup\noexpand\includegraphics*[%
          trim = 0 0 \the\DFhalfwidth\space 0]}\x{#1}}%
  \setbox\DFimageII\hbox{\bgroup
  \edef\x{\egroup\noexpand\includegraphics*[%
          trim = \the\DFhalfwidth\space 0 0 0]}\x{#1}}%
  \edef\DFscalefactor{\fpeval{\FigSpace/\DFhalfwidth}}%
  \setbox\DFimageI=\hbox{%
    \scalebox{\DFscalefactor}{\usebox{\DFimageI}}}%
  \setbox\DFimageII=\hbox{%
    \scalebox{\DFscalefactor}{\usebox{\DFimageII}}}%
  \setbox\DFimageII=
    \hbox{\dimen10=\linewidth\dimen8\internalmargin
           \vbox{\hsize\DFhalfwidth\parindent\z@
             \box\DFimageII\par
             \leavevmode\hspace*{\dimen8}%
             \vtop{\hsize\dimen10\parindent\z@
               \textwidth=\hsize
               \DFcaption[#2]{#3}[#4]%
              }\vspace*{2\baselineskip}%
            }%
          }%
  \setbox\DFimageI=\vbox to\ht\DFimageII{\box\DFimageI\vss}%
  \cleartoeven{%
    \hb@xt@\textwidth{%
      \makebox[\DFhalfwidth][l]{\box\DFimageI}\hss}%
     \afterpage{\hb@xt@\textwidth{%
         \hss\makebox[\DFhalfwidth][r]{\box\DFimageII}}%
    }%
  }%
}

\NewDocumentCommand\FSfigure{m o m o  d() d<> d|| d!!}{%
  \fptest{\CompStrings{\@currenv}{DFimage}}{}{\bgroup}%
  \pagestyle{empty}%
  \setbox\DFtotalimage=\hbox{\includegraphics{#1}}%
  \DFwidth=\wd\DFtotalimage \DFheight=\ht\DFtotalimage
  \ifdim \DFheight < \DFwidth
    \DFhalfwidth=0.5\DFwidth
    \setbox\DFimageI\hbox{\bgroup
    \edef\x{\egroup\noexpand\includegraphics*[%
    trim = 0 0 \the\DFhalfwidth\space 0]}\x{#1}}%
    \setbox\DFimageII\hbox{\bgroup
    \edef\x{\egroup\noexpand\includegraphics*[%
    trim = \the\DFhalfwidth\space 0 0 0]}\x{#1}}%
    \DFwidth=\DFhalfwidth \DFheight=\ht\DFtotalimage
  \else
    \DFhalfheight=0.5\DFheight
    \setbox\DFimageII\hbox{\bgroup
    \edef\x{\egroup\noexpand\includegraphics*[%
    trim = 0 0 0 \the\DFhalfheight\space]}\x{#1}}% bottom half
    \setbox\DFimageI\hbox{\bgroup
    \edef\x{\egroup\noexpand\includegraphics*[%
    trim =  0 \the\DFhalfheight\space 0 0]}\x{#1}}% tophalf
    \setbox\DFimageI=\hbox{\rotatebox[origin=cc]{90}{\box\DFimageI}}
    \setbox\DFimageII=\hbox{\rotatebox[origin=cc]{90}{\box\DFimageII}}
    \DFwidth=\DFhalfheight \DFheight=\wd\DFtotalimage
  \fi
\fptest{\DFheight/\DFwidth > \paperheight/\paperwidth}%
  {\edef\DFscalefactor{\fpeval{\paperheight/\DFheight}}}%
  {\edef\DFscalefactor{\fpeval{\paperwidth/\DFwidth}}}%
  \setbox\DFimageI=\hbox{\scalebox{\DFscalefactor}{\usebox{\DFimageI}}}%
  \setbox\DFimageII=\hbox{\scalebox{\DFscalefactor}{\usebox{\DFimageII}}}%
\cleartoeven{%
  \begin{figure}[p]%
  \vbox to\textheight{\vss\hsize=\textwidth%      vbox 1
    \hbox to\hsize{\hspace*{-\externalmargin}%    hbox 1
      \vbox to\paperheight{\vss%                  vbox 2
        \hbox to\paperwidth{\hss\box\DFimageI}%   hbox 2 end hbox 2
      \vss}%                                      end vbox 2
    \hss}%                                        end hbox 1
  \vss}%                                          end vbox 1
  \end{figure}\newpage
  \begin{figure}[p]%
    \vbox to\textheight{\vss\hsize=\textwidth%    vbox 1
      \hbox to\hsize{\hspace*{-\internalmargin}%  hbox 1
        \vbox to\paperheight{\vss%                vbox 2
          \hbox to\paperwidth{%                   hbox 2
            \box\DFimageII\makebox(0,0)[lb]{%     mbox 3
              \hspace*{-\FScaptionShift}%
              \raisebox{0.5\textheight}{%         raisebox
                \rotatebox[origin=bc]{90}{%       rotatebox
                  \DFcaptionP[#2]{#3}[#4]!#8!%
                 }%                               end rotatebox
              }%                                  end raisebox
            }%                                    end mbox 3
          }%                                      end hbox 2
        \vss}%                                    end vbox 2
      \hss}%                                      end hbox 1
    \vss}%                                        end vbox 1
  \end{figure}%
}\clearpage
\fptest{\CompStrings{\@currenv}{DFimage}}{}{\egroup}%
\reset@tmode}

\NewDocumentCommand\THfigure{m o m o  d() d<> d|| d!!}{%
  \setbox\DFtotalimage=\hbox{\includegraphics{#1}}%
  \DFheight=\ht\DFtotalimage \DFwidth=\wd\DFtotalimage
  \edef\DFscalefactor{\fpeval{\paperheight/\DFheight}}
  \setbox\DFimageI\hbox{%
    \scalebox{\DFscalefactor}{\usebox{\DFtotalimage}}}%
  \DFwidth=\wd\DFimageI
  \ifdim\dimexpr\paperwidth-\DFwidth < 2\externalmargin\relax
    \PackageWarning{swfigure}{%
      *******************************************\MessageBreak
      Figure #1 is too wide to be set in a       \MessageBreak
      Total Height display mode.                 \MessageBreak
      There is not enough space for its caption. \MessageBreak
      Expect questionable results.               \MessageBreak
      *******************************************\MessageBreak}%
  \fi
  \FigSpace=\DFwidth
  \dimen10=\dimexpr\topmargin+1in-(\paperheight-\headsep-\headheight
    -\textheight-\footskip)/2\relax
  \TScaptionwidth=\dimexpr\paperwidth-\FigSpace-3\columnsep\relax
  \cleartopage
  \thispagestyle{empty}%
  \begin{figure}
    \ifodd\c@page\relax%                                    odd page
      \begin{minipage}[c][\textheight][t]{\textwidth}%      minibox1
        \vspace*{\dimen10}%
        \makebox[\textwidth][l]{\hspace*{-\internalmargin}%    hbox2
          \vbox to\textheight{\vss%                            vbox3
            \hbox to\paperwidth{%                              hbox4
              \parbox{\FigSpace}{\box\DFimageI}%
              \hspace{\columnsep}%
              \makebox[\TScaptionwidth]{%
                \parbox{#5\TScaptionwidth}{%
                \caption[#2]{#3}\IfValueT{#4}{\label{#4}}\relax}%
              }%
            }%                                             end hbox4
          \vss}%                                           end vbox3
        }%                                                 end hbox2
      \end{minipage}%                                   end minibox1
    \else %                                                even page
      \begin{minipage}[c][\textheight][t]{\textwidth}
        \vspace*{\dimen10}%
        \makebox[\textwidth][l]{\kern-\externalmargin
          \vbox to\textheight{\vss
            \hbox to\paperwidth{\hss
              \hbox to\TScaptionwidth{\hss
                \parbox{#5\TScaptionwidth}{%
                  \caption[#2]{#3}\IfValueT{#4}{\label{#4}}\relax}%
              }\hspace{\columnsep}%
              \parbox{\FigSpace}{\box\DFimageI}%
            }\vss
          }%
        }%
      \end{minipage}%
    \fi
  \end{figure}%
  \clearpage\reset@tmode
}

\NewDocumentCommand\TWfigure{m o m o  d() d<> d|| d!!}{%
  \setbox\DFtotalimage=\hbox{\includegraphics{#1}}%
  \DFheight=\ht\DFtotalimage \DFwidth=\wd\DFtotalimage
  \FigSpace=\dimexpr\internalmargin+0.80\textwidth\relax
  \TScaptionwidth=\dimexpr\paperwidth-\FigSpace-2\columnsep\relax
  \edef\DFscalefactor{\fpeval{\FigSpace/\DFwidth}}
  \setbox\DFimageI=\hbox{\scalebox{\DFscalefactor}{\box\DFtotalimage}}
  \DFwidth=\wd\DFimageI \DFheight\ht\DFimageI
  \cleartopage
  \thispagestyle{empty}%
    \ifodd\value{page}%                         odd numbered page
      \begin{figure}
         \setbox\DFimageII=\hbox{%
           \parbox{\FigSpace}{\box\DFimageI}\hspace{\columnsep}%
           \begin{minipage}{\TScaptionwidth}\centering
             \parbox{#5\hsize}{\caption[#2]{#3}%
               \IfValueT{#4}{\label{#4}}\relax}%
           \end{minipage}
         }%
         \DFheight=\ht\DFimageII \dimen8=\dp\DFimageII
         \advance\DFheight by\dp\DFimageII
         \dp\DFimageII=\z@ \ht\DFimageII=\DFheight
         \dimen10=\fpeval{\topskip+1in+\headheight+\headsep}\p@
         \advance\dimen8 by\baselineskip
         \advance\DFheight by-\dimen10\relax
         \vbox to\DFheight{\vss
         \hbox{\raise \dimen8\hbox to\textwidth{%
            \hspace{-\internalmargin}\box\DFimageII\hss}}%
         }%
      \end{figure}
    \else%                                     even numbered page
      \begin{figure}
         \setbox\DFimageII=\hbox{%
           \begin{minipage}{\TScaptionwidth}\centering
             \parbox{#5\hsize}{\caption[#2]{#3}%
               \IfValueT{#4}{\label{#4}}\relax}%
           \end{minipage}
           \hspace{\columnsep}%
           \parbox{\FigSpace}{\box\DFimageI}
         }%
         \DFheight=\ht\DFimageII \dimen8=\dp\DFimageII
         \advance\DFheight by\dp\DFimageII
         \dp\DFimageII=\z@ \ht\DFimageII=\DFheight
         \dimen10=\fpeval{\topskip+1in+\headheight+\headsep}\p@
         \advance\dimen8 by\baselineskip
         \advance\DFheight by-\dimen10\relax
         \vbox to\DFheight{\vss
         \hbox{\raise \dimen8\hbox to\textwidth{\hss%
            \box\DFimageII\hspace{-\internalmargin}}}%
         }%
       \hbox to\textwidth{\hspace{-\internalmargin}\box\DFimageII\hss}
    \end{figure}%
    \fi
  \reset@tmode
}

%% 
%% Distributable under the LaTeX Project Public License,
%% version 1.3c or higher (your choice). The latest version of
%% this license is at: http://www.latex-project.org/lppl.txt
%% 
%% This work is "maintained"
%% 
%% This work consists of files swfigure.dtx and README.txt, and the derived
%% files swfigure.sty and swfigure.pdf
%% 
%%
%% End of file `swfigure.sty'.
